Statistics
| Branch: | Revision:

root / block.c @ 182735ef

History | View | Annotate | Download (125.7 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 d616b224 Stefan Hajnoczi
    notifier_with_return_list_init(&bs->before_write_notifiers);
309 d7d512f6 Paolo Bonzini
310 b338082b bellard
    return bs;
311 b338082b bellard
}
312 b338082b bellard
313 d7d512f6 Paolo Bonzini
void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify)
314 d7d512f6 Paolo Bonzini
{
315 d7d512f6 Paolo Bonzini
    notifier_list_add(&bs->close_notifiers, notify);
316 d7d512f6 Paolo Bonzini
}
317 d7d512f6 Paolo Bonzini
318 ea2384d3 bellard
BlockDriver *bdrv_find_format(const char *format_name)
319 ea2384d3 bellard
{
320 ea2384d3 bellard
    BlockDriver *drv1;
321 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
322 8a22f02a Stefan Hajnoczi
        if (!strcmp(drv1->format_name, format_name)) {
323 ea2384d3 bellard
            return drv1;
324 8a22f02a Stefan Hajnoczi
        }
325 ea2384d3 bellard
    }
326 ea2384d3 bellard
    return NULL;
327 ea2384d3 bellard
}
328 ea2384d3 bellard
329 b64ec4e4 Fam Zheng
static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
330 eb852011 Markus Armbruster
{
331 b64ec4e4 Fam Zheng
    static const char *whitelist_rw[] = {
332 b64ec4e4 Fam Zheng
        CONFIG_BDRV_RW_WHITELIST
333 b64ec4e4 Fam Zheng
    };
334 b64ec4e4 Fam Zheng
    static const char *whitelist_ro[] = {
335 b64ec4e4 Fam Zheng
        CONFIG_BDRV_RO_WHITELIST
336 eb852011 Markus Armbruster
    };
337 eb852011 Markus Armbruster
    const char **p;
338 eb852011 Markus Armbruster
339 b64ec4e4 Fam Zheng
    if (!whitelist_rw[0] && !whitelist_ro[0]) {
340 eb852011 Markus Armbruster
        return 1;               /* no whitelist, anything goes */
341 b64ec4e4 Fam Zheng
    }
342 eb852011 Markus Armbruster
343 b64ec4e4 Fam Zheng
    for (p = whitelist_rw; *p; p++) {
344 eb852011 Markus Armbruster
        if (!strcmp(drv->format_name, *p)) {
345 eb852011 Markus Armbruster
            return 1;
346 eb852011 Markus Armbruster
        }
347 eb852011 Markus Armbruster
    }
348 b64ec4e4 Fam Zheng
    if (read_only) {
349 b64ec4e4 Fam Zheng
        for (p = whitelist_ro; *p; p++) {
350 b64ec4e4 Fam Zheng
            if (!strcmp(drv->format_name, *p)) {
351 b64ec4e4 Fam Zheng
                return 1;
352 b64ec4e4 Fam Zheng
            }
353 b64ec4e4 Fam Zheng
        }
354 b64ec4e4 Fam Zheng
    }
355 eb852011 Markus Armbruster
    return 0;
356 eb852011 Markus Armbruster
}
357 eb852011 Markus Armbruster
358 b64ec4e4 Fam Zheng
BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
359 b64ec4e4 Fam Zheng
                                          bool read_only)
360 eb852011 Markus Armbruster
{
361 eb852011 Markus Armbruster
    BlockDriver *drv = bdrv_find_format(format_name);
362 b64ec4e4 Fam Zheng
    return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL;
363 eb852011 Markus Armbruster
}
364 eb852011 Markus Armbruster
365 5b7e1542 Zhi Yong Wu
typedef struct CreateCo {
366 5b7e1542 Zhi Yong Wu
    BlockDriver *drv;
367 5b7e1542 Zhi Yong Wu
    char *filename;
368 5b7e1542 Zhi Yong Wu
    QEMUOptionParameter *options;
369 5b7e1542 Zhi Yong Wu
    int ret;
370 5b7e1542 Zhi Yong Wu
} CreateCo;
371 5b7e1542 Zhi Yong Wu
372 5b7e1542 Zhi Yong Wu
static void coroutine_fn bdrv_create_co_entry(void *opaque)
373 5b7e1542 Zhi Yong Wu
{
374 5b7e1542 Zhi Yong Wu
    CreateCo *cco = opaque;
375 5b7e1542 Zhi Yong Wu
    assert(cco->drv);
376 5b7e1542 Zhi Yong Wu
377 5b7e1542 Zhi Yong Wu
    cco->ret = cco->drv->bdrv_create(cco->filename, cco->options);
378 5b7e1542 Zhi Yong Wu
}
379 5b7e1542 Zhi Yong Wu
380 0e7e1989 Kevin Wolf
int bdrv_create(BlockDriver *drv, const char* filename,
381 0e7e1989 Kevin Wolf
    QEMUOptionParameter *options)
382 ea2384d3 bellard
{
383 5b7e1542 Zhi Yong Wu
    int ret;
384 5b7e1542 Zhi Yong Wu
385 5b7e1542 Zhi Yong Wu
    Coroutine *co;
386 5b7e1542 Zhi Yong Wu
    CreateCo cco = {
387 5b7e1542 Zhi Yong Wu
        .drv = drv,
388 5b7e1542 Zhi Yong Wu
        .filename = g_strdup(filename),
389 5b7e1542 Zhi Yong Wu
        .options = options,
390 5b7e1542 Zhi Yong Wu
        .ret = NOT_DONE,
391 5b7e1542 Zhi Yong Wu
    };
392 5b7e1542 Zhi Yong Wu
393 5b7e1542 Zhi Yong Wu
    if (!drv->bdrv_create) {
394 80168bff Luiz Capitulino
        ret = -ENOTSUP;
395 80168bff Luiz Capitulino
        goto out;
396 5b7e1542 Zhi Yong Wu
    }
397 5b7e1542 Zhi Yong Wu
398 5b7e1542 Zhi Yong Wu
    if (qemu_in_coroutine()) {
399 5b7e1542 Zhi Yong Wu
        /* Fast-path if already in coroutine context */
400 5b7e1542 Zhi Yong Wu
        bdrv_create_co_entry(&cco);
401 5b7e1542 Zhi Yong Wu
    } else {
402 5b7e1542 Zhi Yong Wu
        co = qemu_coroutine_create(bdrv_create_co_entry);
403 5b7e1542 Zhi Yong Wu
        qemu_coroutine_enter(co, &cco);
404 5b7e1542 Zhi Yong Wu
        while (cco.ret == NOT_DONE) {
405 5b7e1542 Zhi Yong Wu
            qemu_aio_wait();
406 5b7e1542 Zhi Yong Wu
        }
407 5b7e1542 Zhi Yong Wu
    }
408 5b7e1542 Zhi Yong Wu
409 5b7e1542 Zhi Yong Wu
    ret = cco.ret;
410 0e7e1989 Kevin Wolf
411 80168bff Luiz Capitulino
out:
412 80168bff Luiz Capitulino
    g_free(cco.filename);
413 5b7e1542 Zhi Yong Wu
    return ret;
414 ea2384d3 bellard
}
415 ea2384d3 bellard
416 84a12e66 Christoph Hellwig
int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
417 84a12e66 Christoph Hellwig
{
418 84a12e66 Christoph Hellwig
    BlockDriver *drv;
419 84a12e66 Christoph Hellwig
420 b50cbabc MORITA Kazutaka
    drv = bdrv_find_protocol(filename);
421 84a12e66 Christoph Hellwig
    if (drv == NULL) {
422 16905d71 Stefan Hajnoczi
        return -ENOENT;
423 84a12e66 Christoph Hellwig
    }
424 84a12e66 Christoph Hellwig
425 84a12e66 Christoph Hellwig
    return bdrv_create(drv, filename, options);
426 84a12e66 Christoph Hellwig
}
427 84a12e66 Christoph Hellwig
428 eba25057 Jim Meyering
/*
429 eba25057 Jim Meyering
 * Create a uniquely-named empty temporary file.
430 eba25057 Jim Meyering
 * Return 0 upon success, otherwise a negative errno value.
431 eba25057 Jim Meyering
 */
432 eba25057 Jim Meyering
int get_tmp_filename(char *filename, int size)
433 d5249393 bellard
{
434 eba25057 Jim Meyering
#ifdef _WIN32
435 3b9f94e1 bellard
    char temp_dir[MAX_PATH];
436 eba25057 Jim Meyering
    /* GetTempFileName requires that its output buffer (4th param)
437 eba25057 Jim Meyering
       have length MAX_PATH or greater.  */
438 eba25057 Jim Meyering
    assert(size >= MAX_PATH);
439 eba25057 Jim Meyering
    return (GetTempPath(MAX_PATH, temp_dir)
440 eba25057 Jim Meyering
            && GetTempFileName(temp_dir, "qem", 0, filename)
441 eba25057 Jim Meyering
            ? 0 : -GetLastError());
442 d5249393 bellard
#else
443 67b915a5 bellard
    int fd;
444 7ccfb2eb blueswir1
    const char *tmpdir;
445 0badc1ee aurel32
    tmpdir = getenv("TMPDIR");
446 0badc1ee aurel32
    if (!tmpdir)
447 0badc1ee aurel32
        tmpdir = "/tmp";
448 eba25057 Jim Meyering
    if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
449 eba25057 Jim Meyering
        return -EOVERFLOW;
450 eba25057 Jim Meyering
    }
451 ea2384d3 bellard
    fd = mkstemp(filename);
452 fe235a06 Dunrong Huang
    if (fd < 0) {
453 fe235a06 Dunrong Huang
        return -errno;
454 fe235a06 Dunrong Huang
    }
455 fe235a06 Dunrong Huang
    if (close(fd) != 0) {
456 fe235a06 Dunrong Huang
        unlink(filename);
457 eba25057 Jim Meyering
        return -errno;
458 eba25057 Jim Meyering
    }
459 eba25057 Jim Meyering
    return 0;
460 d5249393 bellard
#endif
461 eba25057 Jim Meyering
}
462 fc01f7e7 bellard
463 84a12e66 Christoph Hellwig
/*
464 84a12e66 Christoph Hellwig
 * Detect host devices. By convention, /dev/cdrom[N] is always
465 84a12e66 Christoph Hellwig
 * recognized as a host CDROM.
466 84a12e66 Christoph Hellwig
 */
467 84a12e66 Christoph Hellwig
static BlockDriver *find_hdev_driver(const char *filename)
468 84a12e66 Christoph Hellwig
{
469 84a12e66 Christoph Hellwig
    int score_max = 0, score;
470 84a12e66 Christoph Hellwig
    BlockDriver *drv = NULL, *d;
471 84a12e66 Christoph Hellwig
472 84a12e66 Christoph Hellwig
    QLIST_FOREACH(d, &bdrv_drivers, list) {
473 84a12e66 Christoph Hellwig
        if (d->bdrv_probe_device) {
474 84a12e66 Christoph Hellwig
            score = d->bdrv_probe_device(filename);
475 84a12e66 Christoph Hellwig
            if (score > score_max) {
476 84a12e66 Christoph Hellwig
                score_max = score;
477 84a12e66 Christoph Hellwig
                drv = d;
478 84a12e66 Christoph Hellwig
            }
479 84a12e66 Christoph Hellwig
        }
480 84a12e66 Christoph Hellwig
    }
481 84a12e66 Christoph Hellwig
482 84a12e66 Christoph Hellwig
    return drv;
483 84a12e66 Christoph Hellwig
}
484 84a12e66 Christoph Hellwig
485 b50cbabc MORITA Kazutaka
BlockDriver *bdrv_find_protocol(const char *filename)
486 83f64091 bellard
{
487 83f64091 bellard
    BlockDriver *drv1;
488 83f64091 bellard
    char protocol[128];
489 1cec71e3 Anthony Liguori
    int len;
490 83f64091 bellard
    const char *p;
491 19cb3738 bellard
492 66f82cee Kevin Wolf
    /* TODO Drivers without bdrv_file_open must be specified explicitly */
493 66f82cee Kevin Wolf
494 39508e7a Christoph Hellwig
    /*
495 39508e7a Christoph Hellwig
     * XXX(hch): we really should not let host device detection
496 39508e7a Christoph Hellwig
     * override an explicit protocol specification, but moving this
497 39508e7a Christoph Hellwig
     * later breaks access to device names with colons in them.
498 39508e7a Christoph Hellwig
     * Thanks to the brain-dead persistent naming schemes on udev-
499 39508e7a Christoph Hellwig
     * based Linux systems those actually are quite common.
500 39508e7a Christoph Hellwig
     */
501 39508e7a Christoph Hellwig
    drv1 = find_hdev_driver(filename);
502 39508e7a Christoph Hellwig
    if (drv1) {
503 39508e7a Christoph Hellwig
        return drv1;
504 39508e7a Christoph Hellwig
    }
505 39508e7a Christoph Hellwig
506 9e0b22f4 Stefan Hajnoczi
    if (!path_has_protocol(filename)) {
507 39508e7a Christoph Hellwig
        return bdrv_find_format("file");
508 84a12e66 Christoph Hellwig
    }
509 9e0b22f4 Stefan Hajnoczi
    p = strchr(filename, ':');
510 9e0b22f4 Stefan Hajnoczi
    assert(p != NULL);
511 1cec71e3 Anthony Liguori
    len = p - filename;
512 1cec71e3 Anthony Liguori
    if (len > sizeof(protocol) - 1)
513 1cec71e3 Anthony Liguori
        len = sizeof(protocol) - 1;
514 1cec71e3 Anthony Liguori
    memcpy(protocol, filename, len);
515 1cec71e3 Anthony Liguori
    protocol[len] = '\0';
516 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
517 5fafdf24 ths
        if (drv1->protocol_name &&
518 8a22f02a Stefan Hajnoczi
            !strcmp(drv1->protocol_name, protocol)) {
519 83f64091 bellard
            return drv1;
520 8a22f02a Stefan Hajnoczi
        }
521 83f64091 bellard
    }
522 83f64091 bellard
    return NULL;
523 83f64091 bellard
}
524 83f64091 bellard
525 f500a6d3 Kevin Wolf
static int find_image_format(BlockDriverState *bs, const char *filename,
526 f500a6d3 Kevin Wolf
                             BlockDriver **pdrv)
527 f3a5d3f8 Christoph Hellwig
{
528 f500a6d3 Kevin Wolf
    int score, score_max;
529 f3a5d3f8 Christoph Hellwig
    BlockDriver *drv1, *drv;
530 f3a5d3f8 Christoph Hellwig
    uint8_t buf[2048];
531 f500a6d3 Kevin Wolf
    int ret = 0;
532 f8ea0b00 Nicholas Bellinger
533 08a00559 Kevin Wolf
    /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
534 8e895599 Paolo Bonzini
    if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
535 c98ac35d Stefan Weil
        drv = bdrv_find_format("raw");
536 c98ac35d Stefan Weil
        if (!drv) {
537 c98ac35d Stefan Weil
            ret = -ENOENT;
538 c98ac35d Stefan Weil
        }
539 c98ac35d Stefan Weil
        *pdrv = drv;
540 c98ac35d Stefan Weil
        return ret;
541 1a396859 Nicholas A. Bellinger
    }
542 f8ea0b00 Nicholas Bellinger
543 83f64091 bellard
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
544 83f64091 bellard
    if (ret < 0) {
545 c98ac35d Stefan Weil
        *pdrv = NULL;
546 c98ac35d Stefan Weil
        return ret;
547 83f64091 bellard
    }
548 83f64091 bellard
549 ea2384d3 bellard
    score_max = 0;
550 84a12e66 Christoph Hellwig
    drv = NULL;
551 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
552 83f64091 bellard
        if (drv1->bdrv_probe) {
553 83f64091 bellard
            score = drv1->bdrv_probe(buf, ret, filename);
554 83f64091 bellard
            if (score > score_max) {
555 83f64091 bellard
                score_max = score;
556 83f64091 bellard
                drv = drv1;
557 83f64091 bellard
            }
558 0849bf08 bellard
        }
559 fc01f7e7 bellard
    }
560 c98ac35d Stefan Weil
    if (!drv) {
561 c98ac35d Stefan Weil
        ret = -ENOENT;
562 c98ac35d Stefan Weil
    }
563 c98ac35d Stefan Weil
    *pdrv = drv;
564 c98ac35d Stefan Weil
    return ret;
565 ea2384d3 bellard
}
566 ea2384d3 bellard
567 51762288 Stefan Hajnoczi
/**
568 51762288 Stefan Hajnoczi
 * Set the current 'total_sectors' value
569 51762288 Stefan Hajnoczi
 */
570 51762288 Stefan Hajnoczi
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
571 51762288 Stefan Hajnoczi
{
572 51762288 Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
573 51762288 Stefan Hajnoczi
574 396759ad Nicholas Bellinger
    /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
575 396759ad Nicholas Bellinger
    if (bs->sg)
576 396759ad Nicholas Bellinger
        return 0;
577 396759ad Nicholas Bellinger
578 51762288 Stefan Hajnoczi
    /* query actual device if possible, otherwise just trust the hint */
579 51762288 Stefan Hajnoczi
    if (drv->bdrv_getlength) {
580 51762288 Stefan Hajnoczi
        int64_t length = drv->bdrv_getlength(bs);
581 51762288 Stefan Hajnoczi
        if (length < 0) {
582 51762288 Stefan Hajnoczi
            return length;
583 51762288 Stefan Hajnoczi
        }
584 51762288 Stefan Hajnoczi
        hint = length >> BDRV_SECTOR_BITS;
585 51762288 Stefan Hajnoczi
    }
586 51762288 Stefan Hajnoczi
587 51762288 Stefan Hajnoczi
    bs->total_sectors = hint;
588 51762288 Stefan Hajnoczi
    return 0;
589 51762288 Stefan Hajnoczi
}
590 51762288 Stefan Hajnoczi
591 c3993cdc Stefan Hajnoczi
/**
592 9e8f1835 Paolo Bonzini
 * Set open flags for a given discard mode
593 9e8f1835 Paolo Bonzini
 *
594 9e8f1835 Paolo Bonzini
 * Return 0 on success, -1 if the discard mode was invalid.
595 9e8f1835 Paolo Bonzini
 */
596 9e8f1835 Paolo Bonzini
int bdrv_parse_discard_flags(const char *mode, int *flags)
597 9e8f1835 Paolo Bonzini
{
598 9e8f1835 Paolo Bonzini
    *flags &= ~BDRV_O_UNMAP;
599 9e8f1835 Paolo Bonzini
600 9e8f1835 Paolo Bonzini
    if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
601 9e8f1835 Paolo Bonzini
        /* do nothing */
602 9e8f1835 Paolo Bonzini
    } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
603 9e8f1835 Paolo Bonzini
        *flags |= BDRV_O_UNMAP;
604 9e8f1835 Paolo Bonzini
    } else {
605 9e8f1835 Paolo Bonzini
        return -1;
606 9e8f1835 Paolo Bonzini
    }
607 9e8f1835 Paolo Bonzini
608 9e8f1835 Paolo Bonzini
    return 0;
609 9e8f1835 Paolo Bonzini
}
610 9e8f1835 Paolo Bonzini
611 9e8f1835 Paolo Bonzini
/**
612 c3993cdc Stefan Hajnoczi
 * Set open flags for a given cache mode
613 c3993cdc Stefan Hajnoczi
 *
614 c3993cdc Stefan Hajnoczi
 * Return 0 on success, -1 if the cache mode was invalid.
615 c3993cdc Stefan Hajnoczi
 */
616 c3993cdc Stefan Hajnoczi
int bdrv_parse_cache_flags(const char *mode, int *flags)
617 c3993cdc Stefan Hajnoczi
{
618 c3993cdc Stefan Hajnoczi
    *flags &= ~BDRV_O_CACHE_MASK;
619 c3993cdc Stefan Hajnoczi
620 c3993cdc Stefan Hajnoczi
    if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
621 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
622 92196b2f Stefan Hajnoczi
    } else if (!strcmp(mode, "directsync")) {
623 92196b2f Stefan Hajnoczi
        *flags |= BDRV_O_NOCACHE;
624 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "writeback")) {
625 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_CACHE_WB;
626 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "unsafe")) {
627 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_CACHE_WB;
628 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_NO_FLUSH;
629 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "writethrough")) {
630 c3993cdc Stefan Hajnoczi
        /* this is the default */
631 c3993cdc Stefan Hajnoczi
    } else {
632 c3993cdc Stefan Hajnoczi
        return -1;
633 c3993cdc Stefan Hajnoczi
    }
634 c3993cdc Stefan Hajnoczi
635 c3993cdc Stefan Hajnoczi
    return 0;
636 c3993cdc Stefan Hajnoczi
}
637 c3993cdc Stefan Hajnoczi
638 53fec9d3 Stefan Hajnoczi
/**
639 53fec9d3 Stefan Hajnoczi
 * The copy-on-read flag is actually a reference count so multiple users may
640 53fec9d3 Stefan Hajnoczi
 * use the feature without worrying about clobbering its previous state.
641 53fec9d3 Stefan Hajnoczi
 * Copy-on-read stays enabled until all users have called to disable it.
642 53fec9d3 Stefan Hajnoczi
 */
643 53fec9d3 Stefan Hajnoczi
void bdrv_enable_copy_on_read(BlockDriverState *bs)
644 53fec9d3 Stefan Hajnoczi
{
645 53fec9d3 Stefan Hajnoczi
    bs->copy_on_read++;
646 53fec9d3 Stefan Hajnoczi
}
647 53fec9d3 Stefan Hajnoczi
648 53fec9d3 Stefan Hajnoczi
void bdrv_disable_copy_on_read(BlockDriverState *bs)
649 53fec9d3 Stefan Hajnoczi
{
650 53fec9d3 Stefan Hajnoczi
    assert(bs->copy_on_read > 0);
651 53fec9d3 Stefan Hajnoczi
    bs->copy_on_read--;
652 53fec9d3 Stefan Hajnoczi
}
653 53fec9d3 Stefan Hajnoczi
654 7b272452 Kevin Wolf
static int bdrv_open_flags(BlockDriverState *bs, int flags)
655 7b272452 Kevin Wolf
{
656 7b272452 Kevin Wolf
    int open_flags = flags | BDRV_O_CACHE_WB;
657 7b272452 Kevin Wolf
658 7b272452 Kevin Wolf
    /*
659 7b272452 Kevin Wolf
     * Clear flags that are internal to the block layer before opening the
660 7b272452 Kevin Wolf
     * image.
661 7b272452 Kevin Wolf
     */
662 7b272452 Kevin Wolf
    open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
663 7b272452 Kevin Wolf
664 7b272452 Kevin Wolf
    /*
665 7b272452 Kevin Wolf
     * Snapshots should be writable.
666 7b272452 Kevin Wolf
     */
667 7b272452 Kevin Wolf
    if (bs->is_temporary) {
668 7b272452 Kevin Wolf
        open_flags |= BDRV_O_RDWR;
669 7b272452 Kevin Wolf
    }
670 7b272452 Kevin Wolf
671 7b272452 Kevin Wolf
    return open_flags;
672 7b272452 Kevin Wolf
}
673 7b272452 Kevin Wolf
674 b6ce07aa Kevin Wolf
/*
675 57915332 Kevin Wolf
 * Common part for opening disk images and files
676 b6ad491a Kevin Wolf
 *
677 b6ad491a Kevin Wolf
 * Removes all processed options from *options.
678 57915332 Kevin Wolf
 */
679 f500a6d3 Kevin Wolf
static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
680 035fccdf Kevin Wolf
    QDict *options, int flags, BlockDriver *drv)
681 57915332 Kevin Wolf
{
682 57915332 Kevin Wolf
    int ret, open_flags;
683 035fccdf Kevin Wolf
    const char *filename;
684 57915332 Kevin Wolf
685 57915332 Kevin Wolf
    assert(drv != NULL);
686 6405875c Paolo Bonzini
    assert(bs->file == NULL);
687 707ff828 Kevin Wolf
    assert(options != NULL && bs->options != options);
688 57915332 Kevin Wolf
689 45673671 Kevin Wolf
    if (file != NULL) {
690 45673671 Kevin Wolf
        filename = file->filename;
691 45673671 Kevin Wolf
    } else {
692 45673671 Kevin Wolf
        filename = qdict_get_try_str(options, "filename");
693 45673671 Kevin Wolf
    }
694 45673671 Kevin Wolf
695 45673671 Kevin Wolf
    trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
696 28dcee10 Stefan Hajnoczi
697 5d186eb0 Kevin Wolf
    /* bdrv_open() with directly using a protocol as drv. This layer is already
698 5d186eb0 Kevin Wolf
     * opened, so assign it to bs (while file becomes a closed BlockDriverState)
699 5d186eb0 Kevin Wolf
     * and return immediately. */
700 5d186eb0 Kevin Wolf
    if (file != NULL && drv->bdrv_file_open) {
701 5d186eb0 Kevin Wolf
        bdrv_swap(file, bs);
702 5d186eb0 Kevin Wolf
        return 0;
703 5d186eb0 Kevin Wolf
    }
704 5d186eb0 Kevin Wolf
705 57915332 Kevin Wolf
    bs->open_flags = flags;
706 57915332 Kevin Wolf
    bs->buffer_alignment = 512;
707 b64ec4e4 Fam Zheng
    open_flags = bdrv_open_flags(bs, flags);
708 b64ec4e4 Fam Zheng
    bs->read_only = !(open_flags & BDRV_O_RDWR);
709 b64ec4e4 Fam Zheng
710 b64ec4e4 Fam Zheng
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
711 b64ec4e4 Fam Zheng
        return -ENOTSUP;
712 b64ec4e4 Fam Zheng
    }
713 57915332 Kevin Wolf
714 53fec9d3 Stefan Hajnoczi
    assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
715 b64ec4e4 Fam Zheng
    if (!bs->read_only && (flags & BDRV_O_COPY_ON_READ)) {
716 53fec9d3 Stefan Hajnoczi
        bdrv_enable_copy_on_read(bs);
717 53fec9d3 Stefan Hajnoczi
    }
718 53fec9d3 Stefan Hajnoczi
719 c2ad1b0c Kevin Wolf
    if (filename != NULL) {
720 c2ad1b0c Kevin Wolf
        pstrcpy(bs->filename, sizeof(bs->filename), filename);
721 c2ad1b0c Kevin Wolf
    } else {
722 c2ad1b0c Kevin Wolf
        bs->filename[0] = '\0';
723 c2ad1b0c Kevin Wolf
    }
724 57915332 Kevin Wolf
725 57915332 Kevin Wolf
    bs->drv = drv;
726 7267c094 Anthony Liguori
    bs->opaque = g_malloc0(drv->instance_size);
727 57915332 Kevin Wolf
728 03f541bd Stefan Hajnoczi
    bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
729 e7c63796 Stefan Hajnoczi
730 66f82cee Kevin Wolf
    /* Open the image, either directly or using a protocol */
731 66f82cee Kevin Wolf
    if (drv->bdrv_file_open) {
732 5d186eb0 Kevin Wolf
        assert(file == NULL);
733 5d186eb0 Kevin Wolf
        assert(drv->bdrv_parse_filename || filename != NULL);
734 56d1b4d2 Kevin Wolf
        ret = drv->bdrv_file_open(bs, options, open_flags);
735 f500a6d3 Kevin Wolf
    } else {
736 2af5ef70 Kevin Wolf
        if (file == NULL) {
737 2af5ef70 Kevin Wolf
            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a "
738 2af5ef70 Kevin Wolf
                          "block driver for the protocol level",
739 2af5ef70 Kevin Wolf
                          drv->format_name);
740 2af5ef70 Kevin Wolf
            ret = -EINVAL;
741 2af5ef70 Kevin Wolf
            goto free_and_fail;
742 2af5ef70 Kevin Wolf
        }
743 f500a6d3 Kevin Wolf
        assert(file != NULL);
744 f500a6d3 Kevin Wolf
        bs->file = file;
745 b6ad491a Kevin Wolf
        ret = drv->bdrv_open(bs, options, open_flags);
746 66f82cee Kevin Wolf
    }
747 66f82cee Kevin Wolf
748 57915332 Kevin Wolf
    if (ret < 0) {
749 57915332 Kevin Wolf
        goto free_and_fail;
750 57915332 Kevin Wolf
    }
751 57915332 Kevin Wolf
752 51762288 Stefan Hajnoczi
    ret = refresh_total_sectors(bs, bs->total_sectors);
753 51762288 Stefan Hajnoczi
    if (ret < 0) {
754 51762288 Stefan Hajnoczi
        goto free_and_fail;
755 57915332 Kevin Wolf
    }
756 51762288 Stefan Hajnoczi
757 57915332 Kevin Wolf
#ifndef _WIN32
758 57915332 Kevin Wolf
    if (bs->is_temporary) {
759 c2ad1b0c Kevin Wolf
        assert(filename != NULL);
760 57915332 Kevin Wolf
        unlink(filename);
761 57915332 Kevin Wolf
    }
762 57915332 Kevin Wolf
#endif
763 57915332 Kevin Wolf
    return 0;
764 57915332 Kevin Wolf
765 57915332 Kevin Wolf
free_and_fail:
766 f500a6d3 Kevin Wolf
    bs->file = NULL;
767 7267c094 Anthony Liguori
    g_free(bs->opaque);
768 57915332 Kevin Wolf
    bs->opaque = NULL;
769 57915332 Kevin Wolf
    bs->drv = NULL;
770 57915332 Kevin Wolf
    return ret;
771 57915332 Kevin Wolf
}
772 57915332 Kevin Wolf
773 57915332 Kevin Wolf
/*
774 b6ce07aa Kevin Wolf
 * Opens a file using a protocol (file, host_device, nbd, ...)
775 787e4a85 Kevin Wolf
 *
776 787e4a85 Kevin Wolf
 * options is a QDict of options to pass to the block drivers, or NULL for an
777 787e4a85 Kevin Wolf
 * empty set of options. The reference to the QDict belongs to the block layer
778 787e4a85 Kevin Wolf
 * after the call (even on failure), so if the caller intends to reuse the
779 787e4a85 Kevin Wolf
 * dictionary, it needs to use QINCREF() before calling bdrv_file_open.
780 b6ce07aa Kevin Wolf
 */
781 787e4a85 Kevin Wolf
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
782 787e4a85 Kevin Wolf
                   QDict *options, int flags)
783 ea2384d3 bellard
{
784 83f64091 bellard
    BlockDriverState *bs;
785 6db95603 Christoph Hellwig
    BlockDriver *drv;
786 c2ad1b0c Kevin Wolf
    const char *drvname;
787 83f64091 bellard
    int ret;
788 83f64091 bellard
789 707ff828 Kevin Wolf
    /* NULL means an empty set of options */
790 707ff828 Kevin Wolf
    if (options == NULL) {
791 707ff828 Kevin Wolf
        options = qdict_new();
792 707ff828 Kevin Wolf
    }
793 707ff828 Kevin Wolf
794 83f64091 bellard
    bs = bdrv_new("");
795 707ff828 Kevin Wolf
    bs->options = options;
796 707ff828 Kevin Wolf
    options = qdict_clone_shallow(options);
797 707ff828 Kevin Wolf
798 035fccdf Kevin Wolf
    /* Fetch the file name from the options QDict if necessary */
799 035fccdf Kevin Wolf
    if (!filename) {
800 035fccdf Kevin Wolf
        filename = qdict_get_try_str(options, "filename");
801 035fccdf Kevin Wolf
    } else if (filename && !qdict_haskey(options, "filename")) {
802 035fccdf Kevin Wolf
        qdict_put(options, "filename", qstring_from_str(filename));
803 035fccdf Kevin Wolf
    } else {
804 035fccdf Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't specify 'file' and "
805 035fccdf Kevin Wolf
                      "'filename' options at the same time");
806 035fccdf Kevin Wolf
        ret = -EINVAL;
807 035fccdf Kevin Wolf
        goto fail;
808 035fccdf Kevin Wolf
    }
809 035fccdf Kevin Wolf
810 c2ad1b0c Kevin Wolf
    /* Find the right block driver */
811 c2ad1b0c Kevin Wolf
    drvname = qdict_get_try_str(options, "driver");
812 c2ad1b0c Kevin Wolf
    if (drvname) {
813 b64ec4e4 Fam Zheng
        drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR));
814 c2ad1b0c Kevin Wolf
        qdict_del(options, "driver");
815 c2ad1b0c Kevin Wolf
    } else if (filename) {
816 c2ad1b0c Kevin Wolf
        drv = bdrv_find_protocol(filename);
817 c2ad1b0c Kevin Wolf
    } else {
818 c2ad1b0c Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
819 c2ad1b0c Kevin Wolf
                      "Must specify either driver or file");
820 c2ad1b0c Kevin Wolf
        drv = NULL;
821 c2ad1b0c Kevin Wolf
    }
822 c2ad1b0c Kevin Wolf
823 c2ad1b0c Kevin Wolf
    if (!drv) {
824 c2ad1b0c Kevin Wolf
        ret = -ENOENT;
825 c2ad1b0c Kevin Wolf
        goto fail;
826 c2ad1b0c Kevin Wolf
    }
827 c2ad1b0c Kevin Wolf
828 c2ad1b0c Kevin Wolf
    /* Parse the filename and open it */
829 c2ad1b0c Kevin Wolf
    if (drv->bdrv_parse_filename && filename) {
830 6963a30d Kevin Wolf
        Error *local_err = NULL;
831 6963a30d Kevin Wolf
        drv->bdrv_parse_filename(filename, options, &local_err);
832 6963a30d Kevin Wolf
        if (error_is_set(&local_err)) {
833 6963a30d Kevin Wolf
            qerror_report_err(local_err);
834 6963a30d Kevin Wolf
            error_free(local_err);
835 6963a30d Kevin Wolf
            ret = -EINVAL;
836 6963a30d Kevin Wolf
            goto fail;
837 6963a30d Kevin Wolf
        }
838 56d1b4d2 Kevin Wolf
        qdict_del(options, "filename");
839 c2ad1b0c Kevin Wolf
    } else if (!drv->bdrv_parse_filename && !filename) {
840 c2ad1b0c Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
841 c2ad1b0c Kevin Wolf
                      "The '%s' block driver requires a file name",
842 c2ad1b0c Kevin Wolf
                      drv->format_name);
843 c2ad1b0c Kevin Wolf
        ret = -EINVAL;
844 c2ad1b0c Kevin Wolf
        goto fail;
845 6963a30d Kevin Wolf
    }
846 6963a30d Kevin Wolf
847 035fccdf Kevin Wolf
    ret = bdrv_open_common(bs, NULL, options, flags, drv);
848 83f64091 bellard
    if (ret < 0) {
849 707ff828 Kevin Wolf
        goto fail;
850 707ff828 Kevin Wolf
    }
851 707ff828 Kevin Wolf
852 707ff828 Kevin Wolf
    /* Check if any unknown options were used */
853 707ff828 Kevin Wolf
    if (qdict_size(options) != 0) {
854 707ff828 Kevin Wolf
        const QDictEntry *entry = qdict_first(options);
855 707ff828 Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block protocol '%s' doesn't "
856 707ff828 Kevin Wolf
                      "support the option '%s'",
857 707ff828 Kevin Wolf
                      drv->format_name, entry->key);
858 707ff828 Kevin Wolf
        ret = -EINVAL;
859 707ff828 Kevin Wolf
        goto fail;
860 3b0d4f61 bellard
    }
861 707ff828 Kevin Wolf
    QDECREF(options);
862 707ff828 Kevin Wolf
863 71d0770c aliguori
    bs->growable = 1;
864 83f64091 bellard
    *pbs = bs;
865 83f64091 bellard
    return 0;
866 707ff828 Kevin Wolf
867 707ff828 Kevin Wolf
fail:
868 707ff828 Kevin Wolf
    QDECREF(options);
869 707ff828 Kevin Wolf
    if (!bs->drv) {
870 707ff828 Kevin Wolf
        QDECREF(bs->options);
871 707ff828 Kevin Wolf
    }
872 707ff828 Kevin Wolf
    bdrv_delete(bs);
873 707ff828 Kevin Wolf
    return ret;
874 83f64091 bellard
}
875 83f64091 bellard
876 31ca6d07 Kevin Wolf
/*
877 31ca6d07 Kevin Wolf
 * Opens the backing file for a BlockDriverState if not yet open
878 31ca6d07 Kevin Wolf
 *
879 31ca6d07 Kevin Wolf
 * options is a QDict of options to pass to the block drivers, or NULL for an
880 31ca6d07 Kevin Wolf
 * empty set of options. The reference to the QDict is transferred to this
881 31ca6d07 Kevin Wolf
 * function (even on failure), so if the caller intends to reuse the dictionary,
882 31ca6d07 Kevin Wolf
 * it needs to use QINCREF() before calling bdrv_file_open.
883 31ca6d07 Kevin Wolf
 */
884 31ca6d07 Kevin Wolf
int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)
885 9156df12 Paolo Bonzini
{
886 9156df12 Paolo Bonzini
    char backing_filename[PATH_MAX];
887 9156df12 Paolo Bonzini
    int back_flags, ret;
888 9156df12 Paolo Bonzini
    BlockDriver *back_drv = NULL;
889 9156df12 Paolo Bonzini
890 9156df12 Paolo Bonzini
    if (bs->backing_hd != NULL) {
891 31ca6d07 Kevin Wolf
        QDECREF(options);
892 9156df12 Paolo Bonzini
        return 0;
893 9156df12 Paolo Bonzini
    }
894 9156df12 Paolo Bonzini
895 31ca6d07 Kevin Wolf
    /* NULL means an empty set of options */
896 31ca6d07 Kevin Wolf
    if (options == NULL) {
897 31ca6d07 Kevin Wolf
        options = qdict_new();
898 31ca6d07 Kevin Wolf
    }
899 31ca6d07 Kevin Wolf
900 9156df12 Paolo Bonzini
    bs->open_flags &= ~BDRV_O_NO_BACKING;
901 1cb6f506 Kevin Wolf
    if (qdict_haskey(options, "file.filename")) {
902 1cb6f506 Kevin Wolf
        backing_filename[0] = '\0';
903 1cb6f506 Kevin Wolf
    } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
904 31ca6d07 Kevin Wolf
        QDECREF(options);
905 9156df12 Paolo Bonzini
        return 0;
906 9156df12 Paolo Bonzini
    }
907 9156df12 Paolo Bonzini
908 9156df12 Paolo Bonzini
    bs->backing_hd = bdrv_new("");
909 9156df12 Paolo Bonzini
    bdrv_get_full_backing_filename(bs, backing_filename,
910 9156df12 Paolo Bonzini
                                   sizeof(backing_filename));
911 9156df12 Paolo Bonzini
912 9156df12 Paolo Bonzini
    if (bs->backing_format[0] != '\0') {
913 9156df12 Paolo Bonzini
        back_drv = bdrv_find_format(bs->backing_format);
914 9156df12 Paolo Bonzini
    }
915 9156df12 Paolo Bonzini
916 9156df12 Paolo Bonzini
    /* backing files always opened read-only */
917 9156df12 Paolo Bonzini
    back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT);
918 9156df12 Paolo Bonzini
919 31ca6d07 Kevin Wolf
    ret = bdrv_open(bs->backing_hd,
920 31ca6d07 Kevin Wolf
                    *backing_filename ? backing_filename : NULL, options,
921 de9c0cec Kevin Wolf
                    back_flags, back_drv);
922 9156df12 Paolo Bonzini
    if (ret < 0) {
923 9156df12 Paolo Bonzini
        bdrv_delete(bs->backing_hd);
924 9156df12 Paolo Bonzini
        bs->backing_hd = NULL;
925 9156df12 Paolo Bonzini
        bs->open_flags |= BDRV_O_NO_BACKING;
926 9156df12 Paolo Bonzini
        return ret;
927 9156df12 Paolo Bonzini
    }
928 9156df12 Paolo Bonzini
    return 0;
929 9156df12 Paolo Bonzini
}
930 9156df12 Paolo Bonzini
931 707ff828 Kevin Wolf
static void extract_subqdict(QDict *src, QDict **dst, const char *start)
932 707ff828 Kevin Wolf
{
933 707ff828 Kevin Wolf
    const QDictEntry *entry, *next;
934 707ff828 Kevin Wolf
    const char *p;
935 707ff828 Kevin Wolf
936 707ff828 Kevin Wolf
    *dst = qdict_new();
937 707ff828 Kevin Wolf
    entry = qdict_first(src);
938 707ff828 Kevin Wolf
939 707ff828 Kevin Wolf
    while (entry != NULL) {
940 707ff828 Kevin Wolf
        next = qdict_next(src, entry);
941 707ff828 Kevin Wolf
        if (strstart(entry->key, start, &p)) {
942 707ff828 Kevin Wolf
            qobject_incref(entry->value);
943 707ff828 Kevin Wolf
            qdict_put_obj(*dst, p, entry->value);
944 707ff828 Kevin Wolf
            qdict_del(src, entry->key);
945 707ff828 Kevin Wolf
        }
946 707ff828 Kevin Wolf
        entry = next;
947 707ff828 Kevin Wolf
    }
948 707ff828 Kevin Wolf
}
949 707ff828 Kevin Wolf
950 b6ce07aa Kevin Wolf
/*
951 b6ce07aa Kevin Wolf
 * Opens a disk image (raw, qcow2, vmdk, ...)
952 de9c0cec Kevin Wolf
 *
953 de9c0cec Kevin Wolf
 * options is a QDict of options to pass to the block drivers, or NULL for an
954 de9c0cec Kevin Wolf
 * empty set of options. The reference to the QDict belongs to the block layer
955 de9c0cec Kevin Wolf
 * after the call (even on failure), so if the caller intends to reuse the
956 de9c0cec Kevin Wolf
 * dictionary, it needs to use QINCREF() before calling bdrv_open.
957 b6ce07aa Kevin Wolf
 */
958 de9c0cec Kevin Wolf
int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
959 de9c0cec Kevin Wolf
              int flags, BlockDriver *drv)
960 ea2384d3 bellard
{
961 b6ce07aa Kevin Wolf
    int ret;
962 89c9bc3d Stefan Weil
    /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
963 89c9bc3d Stefan Weil
    char tmp_filename[PATH_MAX + 1];
964 f500a6d3 Kevin Wolf
    BlockDriverState *file = NULL;
965 707ff828 Kevin Wolf
    QDict *file_options = NULL;
966 712e7874 bellard
967 de9c0cec Kevin Wolf
    /* NULL means an empty set of options */
968 de9c0cec Kevin Wolf
    if (options == NULL) {
969 de9c0cec Kevin Wolf
        options = qdict_new();
970 de9c0cec Kevin Wolf
    }
971 de9c0cec Kevin Wolf
972 de9c0cec Kevin Wolf
    bs->options = options;
973 b6ad491a Kevin Wolf
    options = qdict_clone_shallow(options);
974 de9c0cec Kevin Wolf
975 de9c0cec Kevin Wolf
    /* For snapshot=on, create a temporary qcow2 overlay */
976 83f64091 bellard
    if (flags & BDRV_O_SNAPSHOT) {
977 ea2384d3 bellard
        BlockDriverState *bs1;
978 ea2384d3 bellard
        int64_t total_size;
979 91a073a9 Kevin Wolf
        BlockDriver *bdrv_qcow2;
980 08b392e1 Kevin Wolf
        QEMUOptionParameter *create_options;
981 b6ce07aa Kevin Wolf
        char backing_filename[PATH_MAX];
982 3b46e624 ths
983 c2ad1b0c Kevin Wolf
        if (qdict_size(options) != 0) {
984 c2ad1b0c Kevin Wolf
            error_report("Can't use snapshot=on with driver-specific options");
985 c2ad1b0c Kevin Wolf
            ret = -EINVAL;
986 c2ad1b0c Kevin Wolf
            goto fail;
987 c2ad1b0c Kevin Wolf
        }
988 c2ad1b0c Kevin Wolf
        assert(filename != NULL);
989 c2ad1b0c Kevin Wolf
990 ea2384d3 bellard
        /* if snapshot, we create a temporary backing file and open it
991 ea2384d3 bellard
           instead of opening 'filename' directly */
992 33e3963e bellard
993 ea2384d3 bellard
        /* if there is a backing file, use it */
994 ea2384d3 bellard
        bs1 = bdrv_new("");
995 de9c0cec Kevin Wolf
        ret = bdrv_open(bs1, filename, NULL, 0, drv);
996 51d7c00c aliguori
        if (ret < 0) {
997 ea2384d3 bellard
            bdrv_delete(bs1);
998 de9c0cec Kevin Wolf
            goto fail;
999 ea2384d3 bellard
        }
1000 3e82990b Jes Sorensen
        total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
1001 7c96d46e aliguori
1002 ea2384d3 bellard
        bdrv_delete(bs1);
1003 3b46e624 ths
1004 eba25057 Jim Meyering
        ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename));
1005 eba25057 Jim Meyering
        if (ret < 0) {
1006 de9c0cec Kevin Wolf
            goto fail;
1007 eba25057 Jim Meyering
        }
1008 7c96d46e aliguori
1009 7c96d46e aliguori
        /* Real path is meaningless for protocols */
1010 4d70655b Stefan Hajnoczi
        if (path_has_protocol(filename)) {
1011 7c96d46e aliguori
            snprintf(backing_filename, sizeof(backing_filename),
1012 7c96d46e aliguori
                     "%s", filename);
1013 de9c0cec Kevin Wolf
        } else if (!realpath(filename, backing_filename)) {
1014 de9c0cec Kevin Wolf
            ret = -errno;
1015 de9c0cec Kevin Wolf
            goto fail;
1016 de9c0cec Kevin Wolf
        }
1017 7c96d46e aliguori
1018 91a073a9 Kevin Wolf
        bdrv_qcow2 = bdrv_find_format("qcow2");
1019 08b392e1 Kevin Wolf
        create_options = parse_option_parameters("", bdrv_qcow2->create_options,
1020 08b392e1 Kevin Wolf
                                                 NULL);
1021 91a073a9 Kevin Wolf
1022 08b392e1 Kevin Wolf
        set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size);
1023 08b392e1 Kevin Wolf
        set_option_parameter(create_options, BLOCK_OPT_BACKING_FILE,
1024 08b392e1 Kevin Wolf
                             backing_filename);
1025 91a073a9 Kevin Wolf
        if (drv) {
1026 08b392e1 Kevin Wolf
            set_option_parameter(create_options, BLOCK_OPT_BACKING_FMT,
1027 91a073a9 Kevin Wolf
                drv->format_name);
1028 91a073a9 Kevin Wolf
        }
1029 91a073a9 Kevin Wolf
1030 08b392e1 Kevin Wolf
        ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options);
1031 08b392e1 Kevin Wolf
        free_option_parameters(create_options);
1032 51d7c00c aliguori
        if (ret < 0) {
1033 de9c0cec Kevin Wolf
            goto fail;
1034 ea2384d3 bellard
        }
1035 91a073a9 Kevin Wolf
1036 ea2384d3 bellard
        filename = tmp_filename;
1037 91a073a9 Kevin Wolf
        drv = bdrv_qcow2;
1038 ea2384d3 bellard
        bs->is_temporary = 1;
1039 ea2384d3 bellard
    }
1040 712e7874 bellard
1041 f500a6d3 Kevin Wolf
    /* Open image file without format layer */
1042 f500a6d3 Kevin Wolf
    if (flags & BDRV_O_RDWR) {
1043 f500a6d3 Kevin Wolf
        flags |= BDRV_O_ALLOW_RDWR;
1044 f500a6d3 Kevin Wolf
    }
1045 f500a6d3 Kevin Wolf
1046 707ff828 Kevin Wolf
    extract_subqdict(options, &file_options, "file.");
1047 707ff828 Kevin Wolf
1048 707ff828 Kevin Wolf
    ret = bdrv_file_open(&file, filename, file_options,
1049 50b05b6f Kevin Wolf
                         bdrv_open_flags(bs, flags | BDRV_O_UNMAP));
1050 f500a6d3 Kevin Wolf
    if (ret < 0) {
1051 de9c0cec Kevin Wolf
        goto fail;
1052 f500a6d3 Kevin Wolf
    }
1053 f500a6d3 Kevin Wolf
1054 b6ce07aa Kevin Wolf
    /* Find the right image format driver */
1055 6db95603 Christoph Hellwig
    if (!drv) {
1056 f500a6d3 Kevin Wolf
        ret = find_image_format(file, filename, &drv);
1057 51d7c00c aliguori
    }
1058 6987307c Christoph Hellwig
1059 51d7c00c aliguori
    if (!drv) {
1060 51d7c00c aliguori
        goto unlink_and_fail;
1061 ea2384d3 bellard
    }
1062 b6ce07aa Kevin Wolf
1063 b6ce07aa Kevin Wolf
    /* Open the image */
1064 035fccdf Kevin Wolf
    ret = bdrv_open_common(bs, file, options, flags, drv);
1065 b6ce07aa Kevin Wolf
    if (ret < 0) {
1066 6987307c Christoph Hellwig
        goto unlink_and_fail;
1067 6987307c Christoph Hellwig
    }
1068 6987307c Christoph Hellwig
1069 f500a6d3 Kevin Wolf
    if (bs->file != file) {
1070 f500a6d3 Kevin Wolf
        bdrv_delete(file);
1071 f500a6d3 Kevin Wolf
        file = NULL;
1072 f500a6d3 Kevin Wolf
    }
1073 f500a6d3 Kevin Wolf
1074 b6ce07aa Kevin Wolf
    /* If there is a backing file, use it */
1075 9156df12 Paolo Bonzini
    if ((flags & BDRV_O_NO_BACKING) == 0) {
1076 31ca6d07 Kevin Wolf
        QDict *backing_options;
1077 31ca6d07 Kevin Wolf
1078 31ca6d07 Kevin Wolf
        extract_subqdict(options, &backing_options, "backing.");
1079 31ca6d07 Kevin Wolf
        ret = bdrv_open_backing_file(bs, backing_options);
1080 b6ce07aa Kevin Wolf
        if (ret < 0) {
1081 b6ad491a Kevin Wolf
            goto close_and_fail;
1082 b6ce07aa Kevin Wolf
        }
1083 b6ce07aa Kevin Wolf
    }
1084 b6ce07aa Kevin Wolf
1085 b6ad491a Kevin Wolf
    /* Check if any unknown options were used */
1086 b6ad491a Kevin Wolf
    if (qdict_size(options) != 0) {
1087 b6ad491a Kevin Wolf
        const QDictEntry *entry = qdict_first(options);
1088 b6ad491a Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block format '%s' used by "
1089 b6ad491a Kevin Wolf
            "device '%s' doesn't support the option '%s'",
1090 b6ad491a Kevin Wolf
            drv->format_name, bs->device_name, entry->key);
1091 b6ad491a Kevin Wolf
1092 b6ad491a Kevin Wolf
        ret = -EINVAL;
1093 b6ad491a Kevin Wolf
        goto close_and_fail;
1094 b6ad491a Kevin Wolf
    }
1095 b6ad491a Kevin Wolf
    QDECREF(options);
1096 b6ad491a Kevin Wolf
1097 b6ce07aa Kevin Wolf
    if (!bdrv_key_required(bs)) {
1098 7d4b4ba5 Markus Armbruster
        bdrv_dev_change_media_cb(bs, true);
1099 b6ce07aa Kevin Wolf
    }
1100 b6ce07aa Kevin Wolf
1101 98f90dba Zhi Yong Wu
    /* throttling disk I/O limits */
1102 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
1103 98f90dba Zhi Yong Wu
        bdrv_io_limits_enable(bs);
1104 98f90dba Zhi Yong Wu
    }
1105 98f90dba Zhi Yong Wu
1106 b6ce07aa Kevin Wolf
    return 0;
1107 b6ce07aa Kevin Wolf
1108 b6ce07aa Kevin Wolf
unlink_and_fail:
1109 f500a6d3 Kevin Wolf
    if (file != NULL) {
1110 f500a6d3 Kevin Wolf
        bdrv_delete(file);
1111 f500a6d3 Kevin Wolf
    }
1112 b6ce07aa Kevin Wolf
    if (bs->is_temporary) {
1113 b6ce07aa Kevin Wolf
        unlink(filename);
1114 b6ce07aa Kevin Wolf
    }
1115 de9c0cec Kevin Wolf
fail:
1116 de9c0cec Kevin Wolf
    QDECREF(bs->options);
1117 b6ad491a Kevin Wolf
    QDECREF(options);
1118 de9c0cec Kevin Wolf
    bs->options = NULL;
1119 b6ad491a Kevin Wolf
    return ret;
1120 de9c0cec Kevin Wolf
1121 b6ad491a Kevin Wolf
close_and_fail:
1122 b6ad491a Kevin Wolf
    bdrv_close(bs);
1123 b6ad491a Kevin Wolf
    QDECREF(options);
1124 b6ce07aa Kevin Wolf
    return ret;
1125 b6ce07aa Kevin Wolf
}
1126 b6ce07aa Kevin Wolf
1127 e971aa12 Jeff Cody
typedef struct BlockReopenQueueEntry {
1128 e971aa12 Jeff Cody
     bool prepared;
1129 e971aa12 Jeff Cody
     BDRVReopenState state;
1130 e971aa12 Jeff Cody
     QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1131 e971aa12 Jeff Cody
} BlockReopenQueueEntry;
1132 e971aa12 Jeff Cody
1133 e971aa12 Jeff Cody
/*
1134 e971aa12 Jeff Cody
 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1135 e971aa12 Jeff Cody
 * reopen of multiple devices.
1136 e971aa12 Jeff Cody
 *
1137 e971aa12 Jeff Cody
 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1138 e971aa12 Jeff Cody
 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1139 e971aa12 Jeff Cody
 * be created and initialized. This newly created BlockReopenQueue should be
1140 e971aa12 Jeff Cody
 * passed back in for subsequent calls that are intended to be of the same
1141 e971aa12 Jeff Cody
 * atomic 'set'.
1142 e971aa12 Jeff Cody
 *
1143 e971aa12 Jeff Cody
 * bs is the BlockDriverState to add to the reopen queue.
1144 e971aa12 Jeff Cody
 *
1145 e971aa12 Jeff Cody
 * flags contains the open flags for the associated bs
1146 e971aa12 Jeff Cody
 *
1147 e971aa12 Jeff Cody
 * returns a pointer to bs_queue, which is either the newly allocated
1148 e971aa12 Jeff Cody
 * bs_queue, or the existing bs_queue being used.
1149 e971aa12 Jeff Cody
 *
1150 e971aa12 Jeff Cody
 */
1151 e971aa12 Jeff Cody
BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1152 e971aa12 Jeff Cody
                                    BlockDriverState *bs, int flags)
1153 e971aa12 Jeff Cody
{
1154 e971aa12 Jeff Cody
    assert(bs != NULL);
1155 e971aa12 Jeff Cody
1156 e971aa12 Jeff Cody
    BlockReopenQueueEntry *bs_entry;
1157 e971aa12 Jeff Cody
    if (bs_queue == NULL) {
1158 e971aa12 Jeff Cody
        bs_queue = g_new0(BlockReopenQueue, 1);
1159 e971aa12 Jeff Cody
        QSIMPLEQ_INIT(bs_queue);
1160 e971aa12 Jeff Cody
    }
1161 e971aa12 Jeff Cody
1162 e971aa12 Jeff Cody
    if (bs->file) {
1163 e971aa12 Jeff Cody
        bdrv_reopen_queue(bs_queue, bs->file, flags);
1164 e971aa12 Jeff Cody
    }
1165 e971aa12 Jeff Cody
1166 e971aa12 Jeff Cody
    bs_entry = g_new0(BlockReopenQueueEntry, 1);
1167 e971aa12 Jeff Cody
    QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1168 e971aa12 Jeff Cody
1169 e971aa12 Jeff Cody
    bs_entry->state.bs = bs;
1170 e971aa12 Jeff Cody
    bs_entry->state.flags = flags;
1171 e971aa12 Jeff Cody
1172 e971aa12 Jeff Cody
    return bs_queue;
1173 e971aa12 Jeff Cody
}
1174 e971aa12 Jeff Cody
1175 e971aa12 Jeff Cody
/*
1176 e971aa12 Jeff Cody
 * Reopen multiple BlockDriverStates atomically & transactionally.
1177 e971aa12 Jeff Cody
 *
1178 e971aa12 Jeff Cody
 * The queue passed in (bs_queue) must have been built up previous
1179 e971aa12 Jeff Cody
 * via bdrv_reopen_queue().
1180 e971aa12 Jeff Cody
 *
1181 e971aa12 Jeff Cody
 * Reopens all BDS specified in the queue, with the appropriate
1182 e971aa12 Jeff Cody
 * flags.  All devices are prepared for reopen, and failure of any
1183 e971aa12 Jeff Cody
 * device will cause all device changes to be abandonded, and intermediate
1184 e971aa12 Jeff Cody
 * data cleaned up.
1185 e971aa12 Jeff Cody
 *
1186 e971aa12 Jeff Cody
 * If all devices prepare successfully, then the changes are committed
1187 e971aa12 Jeff Cody
 * to all devices.
1188 e971aa12 Jeff Cody
 *
1189 e971aa12 Jeff Cody
 */
1190 e971aa12 Jeff Cody
int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1191 e971aa12 Jeff Cody
{
1192 e971aa12 Jeff Cody
    int ret = -1;
1193 e971aa12 Jeff Cody
    BlockReopenQueueEntry *bs_entry, *next;
1194 e971aa12 Jeff Cody
    Error *local_err = NULL;
1195 e971aa12 Jeff Cody
1196 e971aa12 Jeff Cody
    assert(bs_queue != NULL);
1197 e971aa12 Jeff Cody
1198 e971aa12 Jeff Cody
    bdrv_drain_all();
1199 e971aa12 Jeff Cody
1200 e971aa12 Jeff Cody
    QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1201 e971aa12 Jeff Cody
        if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1202 e971aa12 Jeff Cody
            error_propagate(errp, local_err);
1203 e971aa12 Jeff Cody
            goto cleanup;
1204 e971aa12 Jeff Cody
        }
1205 e971aa12 Jeff Cody
        bs_entry->prepared = true;
1206 e971aa12 Jeff Cody
    }
1207 e971aa12 Jeff Cody
1208 e971aa12 Jeff Cody
    /* If we reach this point, we have success and just need to apply the
1209 e971aa12 Jeff Cody
     * changes
1210 e971aa12 Jeff Cody
     */
1211 e971aa12 Jeff Cody
    QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1212 e971aa12 Jeff Cody
        bdrv_reopen_commit(&bs_entry->state);
1213 e971aa12 Jeff Cody
    }
1214 e971aa12 Jeff Cody
1215 e971aa12 Jeff Cody
    ret = 0;
1216 e971aa12 Jeff Cody
1217 e971aa12 Jeff Cody
cleanup:
1218 e971aa12 Jeff Cody
    QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1219 e971aa12 Jeff Cody
        if (ret && bs_entry->prepared) {
1220 e971aa12 Jeff Cody
            bdrv_reopen_abort(&bs_entry->state);
1221 e971aa12 Jeff Cody
        }
1222 e971aa12 Jeff Cody
        g_free(bs_entry);
1223 e971aa12 Jeff Cody
    }
1224 e971aa12 Jeff Cody
    g_free(bs_queue);
1225 e971aa12 Jeff Cody
    return ret;
1226 e971aa12 Jeff Cody
}
1227 e971aa12 Jeff Cody
1228 e971aa12 Jeff Cody
1229 e971aa12 Jeff Cody
/* Reopen a single BlockDriverState with the specified flags. */
1230 e971aa12 Jeff Cody
int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1231 e971aa12 Jeff Cody
{
1232 e971aa12 Jeff Cody
    int ret = -1;
1233 e971aa12 Jeff Cody
    Error *local_err = NULL;
1234 e971aa12 Jeff Cody
    BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags);
1235 e971aa12 Jeff Cody
1236 e971aa12 Jeff Cody
    ret = bdrv_reopen_multiple(queue, &local_err);
1237 e971aa12 Jeff Cody
    if (local_err != NULL) {
1238 e971aa12 Jeff Cody
        error_propagate(errp, local_err);
1239 e971aa12 Jeff Cody
    }
1240 e971aa12 Jeff Cody
    return ret;
1241 e971aa12 Jeff Cody
}
1242 e971aa12 Jeff Cody
1243 e971aa12 Jeff Cody
1244 e971aa12 Jeff Cody
/*
1245 e971aa12 Jeff Cody
 * Prepares a BlockDriverState for reopen. All changes are staged in the
1246 e971aa12 Jeff Cody
 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1247 e971aa12 Jeff Cody
 * the block driver layer .bdrv_reopen_prepare()
1248 e971aa12 Jeff Cody
 *
1249 e971aa12 Jeff Cody
 * bs is the BlockDriverState to reopen
1250 e971aa12 Jeff Cody
 * flags are the new open flags
1251 e971aa12 Jeff Cody
 * queue is the reopen queue
1252 e971aa12 Jeff Cody
 *
1253 e971aa12 Jeff Cody
 * Returns 0 on success, non-zero on error.  On error errp will be set
1254 e971aa12 Jeff Cody
 * as well.
1255 e971aa12 Jeff Cody
 *
1256 e971aa12 Jeff Cody
 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1257 e971aa12 Jeff Cody
 * It is the responsibility of the caller to then call the abort() or
1258 e971aa12 Jeff Cody
 * commit() for any other BDS that have been left in a prepare() state
1259 e971aa12 Jeff Cody
 *
1260 e971aa12 Jeff Cody
 */
1261 e971aa12 Jeff Cody
int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1262 e971aa12 Jeff Cody
                        Error **errp)
1263 e971aa12 Jeff Cody
{
1264 e971aa12 Jeff Cody
    int ret = -1;
1265 e971aa12 Jeff Cody
    Error *local_err = NULL;
1266 e971aa12 Jeff Cody
    BlockDriver *drv;
1267 e971aa12 Jeff Cody
1268 e971aa12 Jeff Cody
    assert(reopen_state != NULL);
1269 e971aa12 Jeff Cody
    assert(reopen_state->bs->drv != NULL);
1270 e971aa12 Jeff Cody
    drv = reopen_state->bs->drv;
1271 e971aa12 Jeff Cody
1272 e971aa12 Jeff Cody
    /* if we are to stay read-only, do not allow permission change
1273 e971aa12 Jeff Cody
     * to r/w */
1274 e971aa12 Jeff Cody
    if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1275 e971aa12 Jeff Cody
        reopen_state->flags & BDRV_O_RDWR) {
1276 e971aa12 Jeff Cody
        error_set(errp, QERR_DEVICE_IS_READ_ONLY,
1277 e971aa12 Jeff Cody
                  reopen_state->bs->device_name);
1278 e971aa12 Jeff Cody
        goto error;
1279 e971aa12 Jeff Cody
    }
1280 e971aa12 Jeff Cody
1281 e971aa12 Jeff Cody
1282 e971aa12 Jeff Cody
    ret = bdrv_flush(reopen_state->bs);
1283 e971aa12 Jeff Cody
    if (ret) {
1284 e971aa12 Jeff Cody
        error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive",
1285 e971aa12 Jeff Cody
                  strerror(-ret));
1286 e971aa12 Jeff Cody
        goto error;
1287 e971aa12 Jeff Cody
    }
1288 e971aa12 Jeff Cody
1289 e971aa12 Jeff Cody
    if (drv->bdrv_reopen_prepare) {
1290 e971aa12 Jeff Cody
        ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
1291 e971aa12 Jeff Cody
        if (ret) {
1292 e971aa12 Jeff Cody
            if (local_err != NULL) {
1293 e971aa12 Jeff Cody
                error_propagate(errp, local_err);
1294 e971aa12 Jeff Cody
            } else {
1295 d8b6895f Luiz Capitulino
                error_setg(errp, "failed while preparing to reopen image '%s'",
1296 d8b6895f Luiz Capitulino
                           reopen_state->bs->filename);
1297 e971aa12 Jeff Cody
            }
1298 e971aa12 Jeff Cody
            goto error;
1299 e971aa12 Jeff Cody
        }
1300 e971aa12 Jeff Cody
    } else {
1301 e971aa12 Jeff Cody
        /* It is currently mandatory to have a bdrv_reopen_prepare()
1302 e971aa12 Jeff Cody
         * handler for each supported drv. */
1303 e971aa12 Jeff Cody
        error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1304 e971aa12 Jeff Cody
                  drv->format_name, reopen_state->bs->device_name,
1305 e971aa12 Jeff Cody
                 "reopening of file");
1306 e971aa12 Jeff Cody
        ret = -1;
1307 e971aa12 Jeff Cody
        goto error;
1308 e971aa12 Jeff Cody
    }
1309 e971aa12 Jeff Cody
1310 e971aa12 Jeff Cody
    ret = 0;
1311 e971aa12 Jeff Cody
1312 e971aa12 Jeff Cody
error:
1313 e971aa12 Jeff Cody
    return ret;
1314 e971aa12 Jeff Cody
}
1315 e971aa12 Jeff Cody
1316 e971aa12 Jeff Cody
/*
1317 e971aa12 Jeff Cody
 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
1318 e971aa12 Jeff Cody
 * makes them final by swapping the staging BlockDriverState contents into
1319 e971aa12 Jeff Cody
 * the active BlockDriverState contents.
1320 e971aa12 Jeff Cody
 */
1321 e971aa12 Jeff Cody
void bdrv_reopen_commit(BDRVReopenState *reopen_state)
1322 e971aa12 Jeff Cody
{
1323 e971aa12 Jeff Cody
    BlockDriver *drv;
1324 e971aa12 Jeff Cody
1325 e971aa12 Jeff Cody
    assert(reopen_state != NULL);
1326 e971aa12 Jeff Cody
    drv = reopen_state->bs->drv;
1327 e971aa12 Jeff Cody
    assert(drv != NULL);
1328 e971aa12 Jeff Cody
1329 e971aa12 Jeff Cody
    /* If there are any driver level actions to take */
1330 e971aa12 Jeff Cody
    if (drv->bdrv_reopen_commit) {
1331 e971aa12 Jeff Cody
        drv->bdrv_reopen_commit(reopen_state);
1332 e971aa12 Jeff Cody
    }
1333 e971aa12 Jeff Cody
1334 e971aa12 Jeff Cody
    /* set BDS specific flags now */
1335 e971aa12 Jeff Cody
    reopen_state->bs->open_flags         = reopen_state->flags;
1336 e971aa12 Jeff Cody
    reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
1337 e971aa12 Jeff Cody
                                              BDRV_O_CACHE_WB);
1338 e971aa12 Jeff Cody
    reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
1339 e971aa12 Jeff Cody
}
1340 e971aa12 Jeff Cody
1341 e971aa12 Jeff Cody
/*
1342 e971aa12 Jeff Cody
 * Abort the reopen, and delete and free the staged changes in
1343 e971aa12 Jeff Cody
 * reopen_state
1344 e971aa12 Jeff Cody
 */
1345 e971aa12 Jeff Cody
void bdrv_reopen_abort(BDRVReopenState *reopen_state)
1346 e971aa12 Jeff Cody
{
1347 e971aa12 Jeff Cody
    BlockDriver *drv;
1348 e971aa12 Jeff Cody
1349 e971aa12 Jeff Cody
    assert(reopen_state != NULL);
1350 e971aa12 Jeff Cody
    drv = reopen_state->bs->drv;
1351 e971aa12 Jeff Cody
    assert(drv != NULL);
1352 e971aa12 Jeff Cody
1353 e971aa12 Jeff Cody
    if (drv->bdrv_reopen_abort) {
1354 e971aa12 Jeff Cody
        drv->bdrv_reopen_abort(reopen_state);
1355 e971aa12 Jeff Cody
    }
1356 e971aa12 Jeff Cody
}
1357 e971aa12 Jeff Cody
1358 e971aa12 Jeff Cody
1359 fc01f7e7 bellard
void bdrv_close(BlockDriverState *bs)
1360 fc01f7e7 bellard
{
1361 3cbc002c Paolo Bonzini
    if (bs->job) {
1362 3cbc002c Paolo Bonzini
        block_job_cancel_sync(bs->job);
1363 3cbc002c Paolo Bonzini
    }
1364 58fda173 Stefan Hajnoczi
    bdrv_drain_all(); /* complete I/O */
1365 58fda173 Stefan Hajnoczi
    bdrv_flush(bs);
1366 58fda173 Stefan Hajnoczi
    bdrv_drain_all(); /* in case flush left pending I/O */
1367 d7d512f6 Paolo Bonzini
    notifier_list_notify(&bs->close_notifiers, bs);
1368 7094f12f Kevin Wolf
1369 3cbc002c Paolo Bonzini
    if (bs->drv) {
1370 557df6ac Stefan Hajnoczi
        if (bs->backing_hd) {
1371 ea2384d3 bellard
            bdrv_delete(bs->backing_hd);
1372 557df6ac Stefan Hajnoczi
            bs->backing_hd = NULL;
1373 557df6ac Stefan Hajnoczi
        }
1374 ea2384d3 bellard
        bs->drv->bdrv_close(bs);
1375 7267c094 Anthony Liguori
        g_free(bs->opaque);
1376 ea2384d3 bellard
#ifdef _WIN32
1377 ea2384d3 bellard
        if (bs->is_temporary) {
1378 ea2384d3 bellard
            unlink(bs->filename);
1379 ea2384d3 bellard
        }
1380 67b915a5 bellard
#endif
1381 ea2384d3 bellard
        bs->opaque = NULL;
1382 ea2384d3 bellard
        bs->drv = NULL;
1383 53fec9d3 Stefan Hajnoczi
        bs->copy_on_read = 0;
1384 a275fa42 Paolo Bonzini
        bs->backing_file[0] = '\0';
1385 a275fa42 Paolo Bonzini
        bs->backing_format[0] = '\0';
1386 6405875c Paolo Bonzini
        bs->total_sectors = 0;
1387 6405875c Paolo Bonzini
        bs->encrypted = 0;
1388 6405875c Paolo Bonzini
        bs->valid_key = 0;
1389 6405875c Paolo Bonzini
        bs->sg = 0;
1390 6405875c Paolo Bonzini
        bs->growable = 0;
1391 de9c0cec Kevin Wolf
        QDECREF(bs->options);
1392 de9c0cec Kevin Wolf
        bs->options = NULL;
1393 b338082b bellard
1394 66f82cee Kevin Wolf
        if (bs->file != NULL) {
1395 0ac9377d Paolo Bonzini
            bdrv_delete(bs->file);
1396 0ac9377d Paolo Bonzini
            bs->file = NULL;
1397 66f82cee Kevin Wolf
        }
1398 b338082b bellard
    }
1399 98f90dba Zhi Yong Wu
1400 9ca11154 Pavel Hrdina
    bdrv_dev_change_media_cb(bs, false);
1401 9ca11154 Pavel Hrdina
1402 98f90dba Zhi Yong Wu
    /*throttling disk I/O limits*/
1403 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
1404 98f90dba Zhi Yong Wu
        bdrv_io_limits_disable(bs);
1405 98f90dba Zhi Yong Wu
    }
1406 b338082b bellard
}
1407 b338082b bellard
1408 2bc93fed MORITA Kazutaka
void bdrv_close_all(void)
1409 2bc93fed MORITA Kazutaka
{
1410 2bc93fed MORITA Kazutaka
    BlockDriverState *bs;
1411 2bc93fed MORITA Kazutaka
1412 2bc93fed MORITA Kazutaka
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1413 2bc93fed MORITA Kazutaka
        bdrv_close(bs);
1414 2bc93fed MORITA Kazutaka
    }
1415 2bc93fed MORITA Kazutaka
}
1416 2bc93fed MORITA Kazutaka
1417 922453bc Stefan Hajnoczi
/*
1418 922453bc Stefan Hajnoczi
 * Wait for pending requests to complete across all BlockDriverStates
1419 922453bc Stefan Hajnoczi
 *
1420 922453bc Stefan Hajnoczi
 * This function does not flush data to disk, use bdrv_flush_all() for that
1421 922453bc Stefan Hajnoczi
 * after calling this function.
1422 4c355d53 Zhi Yong Wu
 *
1423 4c355d53 Zhi Yong Wu
 * Note that completion of an asynchronous I/O operation can trigger any
1424 4c355d53 Zhi Yong Wu
 * number of other I/O operations on other devices---for example a coroutine
1425 4c355d53 Zhi Yong Wu
 * can be arbitrarily complex and a constant flow of I/O can come until the
1426 4c355d53 Zhi Yong Wu
 * coroutine is complete.  Because of this, it is not possible to have a
1427 4c355d53 Zhi Yong Wu
 * function to drain a single device's I/O queue.
1428 922453bc Stefan Hajnoczi
 */
1429 922453bc Stefan Hajnoczi
void bdrv_drain_all(void)
1430 922453bc Stefan Hajnoczi
{
1431 922453bc Stefan Hajnoczi
    BlockDriverState *bs;
1432 4c355d53 Zhi Yong Wu
    bool busy;
1433 4c355d53 Zhi Yong Wu
1434 4c355d53 Zhi Yong Wu
    do {
1435 4c355d53 Zhi Yong Wu
        busy = qemu_aio_wait();
1436 922453bc Stefan Hajnoczi
1437 4c355d53 Zhi Yong Wu
        /* FIXME: We do not have timer support here, so this is effectively
1438 4c355d53 Zhi Yong Wu
         * a busy wait.
1439 4c355d53 Zhi Yong Wu
         */
1440 4c355d53 Zhi Yong Wu
        QTAILQ_FOREACH(bs, &bdrv_states, list) {
1441 4c355d53 Zhi Yong Wu
            if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
1442 4c355d53 Zhi Yong Wu
                qemu_co_queue_restart_all(&bs->throttled_reqs);
1443 4c355d53 Zhi Yong Wu
                busy = true;
1444 4c355d53 Zhi Yong Wu
            }
1445 4c355d53 Zhi Yong Wu
        }
1446 4c355d53 Zhi Yong Wu
    } while (busy);
1447 922453bc Stefan Hajnoczi
1448 922453bc Stefan Hajnoczi
    /* If requests are still pending there is a bug somewhere */
1449 922453bc Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1450 922453bc Stefan Hajnoczi
        assert(QLIST_EMPTY(&bs->tracked_requests));
1451 922453bc Stefan Hajnoczi
        assert(qemu_co_queue_empty(&bs->throttled_reqs));
1452 922453bc Stefan Hajnoczi
    }
1453 922453bc Stefan Hajnoczi
}
1454 922453bc Stefan Hajnoczi
1455 d22b2f41 Ryan Harper
/* make a BlockDriverState anonymous by removing from bdrv_state list.
1456 d22b2f41 Ryan Harper
   Also, NULL terminate the device_name to prevent double remove */
1457 d22b2f41 Ryan Harper
void bdrv_make_anon(BlockDriverState *bs)
1458 d22b2f41 Ryan Harper
{
1459 d22b2f41 Ryan Harper
    if (bs->device_name[0] != '\0') {
1460 d22b2f41 Ryan Harper
        QTAILQ_REMOVE(&bdrv_states, bs, list);
1461 d22b2f41 Ryan Harper
    }
1462 d22b2f41 Ryan Harper
    bs->device_name[0] = '\0';
1463 d22b2f41 Ryan Harper
}
1464 d22b2f41 Ryan Harper
1465 e023b2e2 Paolo Bonzini
static void bdrv_rebind(BlockDriverState *bs)
1466 e023b2e2 Paolo Bonzini
{
1467 e023b2e2 Paolo Bonzini
    if (bs->drv && bs->drv->bdrv_rebind) {
1468 e023b2e2 Paolo Bonzini
        bs->drv->bdrv_rebind(bs);
1469 e023b2e2 Paolo Bonzini
    }
1470 e023b2e2 Paolo Bonzini
}
1471 e023b2e2 Paolo Bonzini
1472 4ddc07ca Paolo Bonzini
static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
1473 4ddc07ca Paolo Bonzini
                                     BlockDriverState *bs_src)
1474 8802d1fd Jeff Cody
{
1475 4ddc07ca Paolo Bonzini
    /* move some fields that need to stay attached to the device */
1476 4ddc07ca Paolo Bonzini
    bs_dest->open_flags         = bs_src->open_flags;
1477 8802d1fd Jeff Cody
1478 8802d1fd Jeff Cody
    /* dev info */
1479 4ddc07ca Paolo Bonzini
    bs_dest->dev_ops            = bs_src->dev_ops;
1480 4ddc07ca Paolo Bonzini
    bs_dest->dev_opaque         = bs_src->dev_opaque;
1481 4ddc07ca Paolo Bonzini
    bs_dest->dev                = bs_src->dev;
1482 4ddc07ca Paolo Bonzini
    bs_dest->buffer_alignment   = bs_src->buffer_alignment;
1483 4ddc07ca Paolo Bonzini
    bs_dest->copy_on_read       = bs_src->copy_on_read;
1484 8802d1fd Jeff Cody
1485 4ddc07ca Paolo Bonzini
    bs_dest->enable_write_cache = bs_src->enable_write_cache;
1486 c4a248a1 Paolo Bonzini
1487 8802d1fd Jeff Cody
    /* i/o timing parameters */
1488 4ddc07ca Paolo Bonzini
    bs_dest->slice_start        = bs_src->slice_start;
1489 4ddc07ca Paolo Bonzini
    bs_dest->slice_end          = bs_src->slice_end;
1490 5905fbc9 Stefan Hajnoczi
    bs_dest->slice_submitted    = bs_src->slice_submitted;
1491 4ddc07ca Paolo Bonzini
    bs_dest->io_limits          = bs_src->io_limits;
1492 4ddc07ca Paolo Bonzini
    bs_dest->throttled_reqs     = bs_src->throttled_reqs;
1493 4ddc07ca Paolo Bonzini
    bs_dest->block_timer        = bs_src->block_timer;
1494 4ddc07ca Paolo Bonzini
    bs_dest->io_limits_enabled  = bs_src->io_limits_enabled;
1495 8802d1fd Jeff Cody
1496 8802d1fd Jeff Cody
    /* r/w error */
1497 4ddc07ca Paolo Bonzini
    bs_dest->on_read_error      = bs_src->on_read_error;
1498 4ddc07ca Paolo Bonzini
    bs_dest->on_write_error     = bs_src->on_write_error;
1499 8802d1fd Jeff Cody
1500 8802d1fd Jeff Cody
    /* i/o status */
1501 4ddc07ca Paolo Bonzini
    bs_dest->iostatus_enabled   = bs_src->iostatus_enabled;
1502 4ddc07ca Paolo Bonzini
    bs_dest->iostatus           = bs_src->iostatus;
1503 8802d1fd Jeff Cody
1504 a9fc4408 Paolo Bonzini
    /* dirty bitmap */
1505 4ddc07ca Paolo Bonzini
    bs_dest->dirty_bitmap       = bs_src->dirty_bitmap;
1506 a9fc4408 Paolo Bonzini
1507 a9fc4408 Paolo Bonzini
    /* job */
1508 4ddc07ca Paolo Bonzini
    bs_dest->in_use             = bs_src->in_use;
1509 4ddc07ca Paolo Bonzini
    bs_dest->job                = bs_src->job;
1510 a9fc4408 Paolo Bonzini
1511 8802d1fd Jeff Cody
    /* keep the same entry in bdrv_states */
1512 4ddc07ca Paolo Bonzini
    pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name),
1513 4ddc07ca Paolo Bonzini
            bs_src->device_name);
1514 4ddc07ca Paolo Bonzini
    bs_dest->list = bs_src->list;
1515 4ddc07ca Paolo Bonzini
}
1516 8802d1fd Jeff Cody
1517 4ddc07ca Paolo Bonzini
/*
1518 4ddc07ca Paolo Bonzini
 * Swap bs contents for two image chains while they are live,
1519 4ddc07ca Paolo Bonzini
 * while keeping required fields on the BlockDriverState that is
1520 4ddc07ca Paolo Bonzini
 * actually attached to a device.
1521 4ddc07ca Paolo Bonzini
 *
1522 4ddc07ca Paolo Bonzini
 * This will modify the BlockDriverState fields, and swap contents
1523 4ddc07ca Paolo Bonzini
 * between bs_new and bs_old. Both bs_new and bs_old are modified.
1524 4ddc07ca Paolo Bonzini
 *
1525 4ddc07ca Paolo Bonzini
 * bs_new is required to be anonymous.
1526 4ddc07ca Paolo Bonzini
 *
1527 4ddc07ca Paolo Bonzini
 * This function does not create any image files.
1528 4ddc07ca Paolo Bonzini
 */
1529 4ddc07ca Paolo Bonzini
void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
1530 4ddc07ca Paolo Bonzini
{
1531 4ddc07ca Paolo Bonzini
    BlockDriverState tmp;
1532 f6801b83 Jeff Cody
1533 4ddc07ca Paolo Bonzini
    /* bs_new must be anonymous and shouldn't have anything fancy enabled */
1534 4ddc07ca Paolo Bonzini
    assert(bs_new->device_name[0] == '\0');
1535 4ddc07ca Paolo Bonzini
    assert(bs_new->dirty_bitmap == NULL);
1536 4ddc07ca Paolo Bonzini
    assert(bs_new->job == NULL);
1537 4ddc07ca Paolo Bonzini
    assert(bs_new->dev == NULL);
1538 4ddc07ca Paolo Bonzini
    assert(bs_new->in_use == 0);
1539 4ddc07ca Paolo Bonzini
    assert(bs_new->io_limits_enabled == false);
1540 4ddc07ca Paolo Bonzini
    assert(bs_new->block_timer == NULL);
1541 8802d1fd Jeff Cody
1542 4ddc07ca Paolo Bonzini
    tmp = *bs_new;
1543 4ddc07ca Paolo Bonzini
    *bs_new = *bs_old;
1544 4ddc07ca Paolo Bonzini
    *bs_old = tmp;
1545 a9fc4408 Paolo Bonzini
1546 4ddc07ca Paolo Bonzini
    /* there are some fields that should not be swapped, move them back */
1547 4ddc07ca Paolo Bonzini
    bdrv_move_feature_fields(&tmp, bs_old);
1548 4ddc07ca Paolo Bonzini
    bdrv_move_feature_fields(bs_old, bs_new);
1549 4ddc07ca Paolo Bonzini
    bdrv_move_feature_fields(bs_new, &tmp);
1550 8802d1fd Jeff Cody
1551 4ddc07ca Paolo Bonzini
    /* bs_new shouldn't be in bdrv_states even after the swap!  */
1552 4ddc07ca Paolo Bonzini
    assert(bs_new->device_name[0] == '\0');
1553 4ddc07ca Paolo Bonzini
1554 4ddc07ca Paolo Bonzini
    /* Check a few fields that should remain attached to the device */
1555 4ddc07ca Paolo Bonzini
    assert(bs_new->dev == NULL);
1556 4ddc07ca Paolo Bonzini
    assert(bs_new->job == NULL);
1557 4ddc07ca Paolo Bonzini
    assert(bs_new->in_use == 0);
1558 4ddc07ca Paolo Bonzini
    assert(bs_new->io_limits_enabled == false);
1559 4ddc07ca Paolo Bonzini
    assert(bs_new->block_timer == NULL);
1560 e023b2e2 Paolo Bonzini
1561 e023b2e2 Paolo Bonzini
    bdrv_rebind(bs_new);
1562 4ddc07ca Paolo Bonzini
    bdrv_rebind(bs_old);
1563 4ddc07ca Paolo Bonzini
}
1564 4ddc07ca Paolo Bonzini
1565 4ddc07ca Paolo Bonzini
/*
1566 4ddc07ca Paolo Bonzini
 * Add new bs contents at the top of an image chain while the chain is
1567 4ddc07ca Paolo Bonzini
 * live, while keeping required fields on the top layer.
1568 4ddc07ca Paolo Bonzini
 *
1569 4ddc07ca Paolo Bonzini
 * This will modify the BlockDriverState fields, and swap contents
1570 4ddc07ca Paolo Bonzini
 * between bs_new and bs_top. Both bs_new and bs_top are modified.
1571 4ddc07ca Paolo Bonzini
 *
1572 4ddc07ca Paolo Bonzini
 * bs_new is required to be anonymous.
1573 4ddc07ca Paolo Bonzini
 *
1574 4ddc07ca Paolo Bonzini
 * This function does not create any image files.
1575 4ddc07ca Paolo Bonzini
 */
1576 4ddc07ca Paolo Bonzini
void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
1577 4ddc07ca Paolo Bonzini
{
1578 4ddc07ca Paolo Bonzini
    bdrv_swap(bs_new, bs_top);
1579 4ddc07ca Paolo Bonzini
1580 4ddc07ca Paolo Bonzini
    /* The contents of 'tmp' will become bs_top, as we are
1581 4ddc07ca Paolo Bonzini
     * swapping bs_new and bs_top contents. */
1582 4ddc07ca Paolo Bonzini
    bs_top->backing_hd = bs_new;
1583 4ddc07ca Paolo Bonzini
    bs_top->open_flags &= ~BDRV_O_NO_BACKING;
1584 4ddc07ca Paolo Bonzini
    pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file),
1585 4ddc07ca Paolo Bonzini
            bs_new->filename);
1586 4ddc07ca Paolo Bonzini
    pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format),
1587 4ddc07ca Paolo Bonzini
            bs_new->drv ? bs_new->drv->format_name : "");
1588 8802d1fd Jeff Cody
}
1589 8802d1fd Jeff Cody
1590 b338082b bellard
void bdrv_delete(BlockDriverState *bs)
1591 b338082b bellard
{
1592 fa879d62 Markus Armbruster
    assert(!bs->dev);
1593 3e914655 Paolo Bonzini
    assert(!bs->job);
1594 3e914655 Paolo Bonzini
    assert(!bs->in_use);
1595 18846dee Markus Armbruster
1596 1b7bdbc1 Stefan Hajnoczi
    /* remove from list, if necessary */
1597 d22b2f41 Ryan Harper
    bdrv_make_anon(bs);
1598 34c6f050 aurel32
1599 b338082b bellard
    bdrv_close(bs);
1600 66f82cee Kevin Wolf
1601 7267c094 Anthony Liguori
    g_free(bs);
1602 fc01f7e7 bellard
}
1603 fc01f7e7 bellard
1604 fa879d62 Markus Armbruster
int bdrv_attach_dev(BlockDriverState *bs, void *dev)
1605 fa879d62 Markus Armbruster
/* TODO change to DeviceState *dev when all users are qdevified */
1606 18846dee Markus Armbruster
{
1607 fa879d62 Markus Armbruster
    if (bs->dev) {
1608 18846dee Markus Armbruster
        return -EBUSY;
1609 18846dee Markus Armbruster
    }
1610 fa879d62 Markus Armbruster
    bs->dev = dev;
1611 28a7282a Luiz Capitulino
    bdrv_iostatus_reset(bs);
1612 18846dee Markus Armbruster
    return 0;
1613 18846dee Markus Armbruster
}
1614 18846dee Markus Armbruster
1615 fa879d62 Markus Armbruster
/* TODO qdevified devices don't use this, remove when devices are qdevified */
1616 fa879d62 Markus Armbruster
void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
1617 18846dee Markus Armbruster
{
1618 fa879d62 Markus Armbruster
    if (bdrv_attach_dev(bs, dev) < 0) {
1619 fa879d62 Markus Armbruster
        abort();
1620 fa879d62 Markus Armbruster
    }
1621 fa879d62 Markus Armbruster
}
1622 fa879d62 Markus Armbruster
1623 fa879d62 Markus Armbruster
void bdrv_detach_dev(BlockDriverState *bs, void *dev)
1624 fa879d62 Markus Armbruster
/* TODO change to DeviceState *dev when all users are qdevified */
1625 fa879d62 Markus Armbruster
{
1626 fa879d62 Markus Armbruster
    assert(bs->dev == dev);
1627 fa879d62 Markus Armbruster
    bs->dev = NULL;
1628 0e49de52 Markus Armbruster
    bs->dev_ops = NULL;
1629 0e49de52 Markus Armbruster
    bs->dev_opaque = NULL;
1630 29e05f20 Markus Armbruster
    bs->buffer_alignment = 512;
1631 18846dee Markus Armbruster
}
1632 18846dee Markus Armbruster
1633 fa879d62 Markus Armbruster
/* TODO change to return DeviceState * when all users are qdevified */
1634 fa879d62 Markus Armbruster
void *bdrv_get_attached_dev(BlockDriverState *bs)
1635 18846dee Markus Armbruster
{
1636 fa879d62 Markus Armbruster
    return bs->dev;
1637 18846dee Markus Armbruster
}
1638 18846dee Markus Armbruster
1639 0e49de52 Markus Armbruster
void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
1640 0e49de52 Markus Armbruster
                      void *opaque)
1641 0e49de52 Markus Armbruster
{
1642 0e49de52 Markus Armbruster
    bs->dev_ops = ops;
1643 0e49de52 Markus Armbruster
    bs->dev_opaque = opaque;
1644 0e49de52 Markus Armbruster
}
1645 0e49de52 Markus Armbruster
1646 32c81a4a Paolo Bonzini
void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
1647 32c81a4a Paolo Bonzini
                               enum MonitorEvent ev,
1648 32c81a4a Paolo Bonzini
                               BlockErrorAction action, bool is_read)
1649 329c0a48 Luiz Capitulino
{
1650 329c0a48 Luiz Capitulino
    QObject *data;
1651 329c0a48 Luiz Capitulino
    const char *action_str;
1652 329c0a48 Luiz Capitulino
1653 329c0a48 Luiz Capitulino
    switch (action) {
1654 329c0a48 Luiz Capitulino
    case BDRV_ACTION_REPORT:
1655 329c0a48 Luiz Capitulino
        action_str = "report";
1656 329c0a48 Luiz Capitulino
        break;
1657 329c0a48 Luiz Capitulino
    case BDRV_ACTION_IGNORE:
1658 329c0a48 Luiz Capitulino
        action_str = "ignore";
1659 329c0a48 Luiz Capitulino
        break;
1660 329c0a48 Luiz Capitulino
    case BDRV_ACTION_STOP:
1661 329c0a48 Luiz Capitulino
        action_str = "stop";
1662 329c0a48 Luiz Capitulino
        break;
1663 329c0a48 Luiz Capitulino
    default:
1664 329c0a48 Luiz Capitulino
        abort();
1665 329c0a48 Luiz Capitulino
    }
1666 329c0a48 Luiz Capitulino
1667 329c0a48 Luiz Capitulino
    data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1668 329c0a48 Luiz Capitulino
                              bdrv->device_name,
1669 329c0a48 Luiz Capitulino
                              action_str,
1670 329c0a48 Luiz Capitulino
                              is_read ? "read" : "write");
1671 32c81a4a Paolo Bonzini
    monitor_protocol_event(ev, data);
1672 329c0a48 Luiz Capitulino
1673 329c0a48 Luiz Capitulino
    qobject_decref(data);
1674 329c0a48 Luiz Capitulino
}
1675 329c0a48 Luiz Capitulino
1676 6f382ed2 Luiz Capitulino
static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected)
1677 6f382ed2 Luiz Capitulino
{
1678 6f382ed2 Luiz Capitulino
    QObject *data;
1679 6f382ed2 Luiz Capitulino
1680 6f382ed2 Luiz Capitulino
    data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
1681 6f382ed2 Luiz Capitulino
                              bdrv_get_device_name(bs), ejected);
1682 6f382ed2 Luiz Capitulino
    monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data);
1683 6f382ed2 Luiz Capitulino
1684 6f382ed2 Luiz Capitulino
    qobject_decref(data);
1685 6f382ed2 Luiz Capitulino
}
1686 6f382ed2 Luiz Capitulino
1687 7d4b4ba5 Markus Armbruster
static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
1688 0e49de52 Markus Armbruster
{
1689 145feb17 Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->change_media_cb) {
1690 6f382ed2 Luiz Capitulino
        bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
1691 7d4b4ba5 Markus Armbruster
        bs->dev_ops->change_media_cb(bs->dev_opaque, load);
1692 6f382ed2 Luiz Capitulino
        if (tray_was_closed) {
1693 6f382ed2 Luiz Capitulino
            /* tray open */
1694 6f382ed2 Luiz Capitulino
            bdrv_emit_qmp_eject_event(bs, true);
1695 6f382ed2 Luiz Capitulino
        }
1696 6f382ed2 Luiz Capitulino
        if (load) {
1697 6f382ed2 Luiz Capitulino
            /* tray close */
1698 6f382ed2 Luiz Capitulino
            bdrv_emit_qmp_eject_event(bs, false);
1699 6f382ed2 Luiz Capitulino
        }
1700 145feb17 Markus Armbruster
    }
1701 145feb17 Markus Armbruster
}
1702 145feb17 Markus Armbruster
1703 2c6942fa Markus Armbruster
bool bdrv_dev_has_removable_media(BlockDriverState *bs)
1704 2c6942fa Markus Armbruster
{
1705 2c6942fa Markus Armbruster
    return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
1706 2c6942fa Markus Armbruster
}
1707 2c6942fa Markus Armbruster
1708 025ccaa7 Paolo Bonzini
void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
1709 025ccaa7 Paolo Bonzini
{
1710 025ccaa7 Paolo Bonzini
    if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
1711 025ccaa7 Paolo Bonzini
        bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
1712 025ccaa7 Paolo Bonzini
    }
1713 025ccaa7 Paolo Bonzini
}
1714 025ccaa7 Paolo Bonzini
1715 e4def80b Markus Armbruster
bool bdrv_dev_is_tray_open(BlockDriverState *bs)
1716 e4def80b Markus Armbruster
{
1717 e4def80b Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->is_tray_open) {
1718 e4def80b Markus Armbruster
        return bs->dev_ops->is_tray_open(bs->dev_opaque);
1719 e4def80b Markus Armbruster
    }
1720 e4def80b Markus Armbruster
    return false;
1721 e4def80b Markus Armbruster
}
1722 e4def80b Markus Armbruster
1723 145feb17 Markus Armbruster
static void bdrv_dev_resize_cb(BlockDriverState *bs)
1724 145feb17 Markus Armbruster
{
1725 145feb17 Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->resize_cb) {
1726 145feb17 Markus Armbruster
        bs->dev_ops->resize_cb(bs->dev_opaque);
1727 0e49de52 Markus Armbruster
    }
1728 0e49de52 Markus Armbruster
}
1729 0e49de52 Markus Armbruster
1730 f107639a Markus Armbruster
bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
1731 f107639a Markus Armbruster
{
1732 f107639a Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
1733 f107639a Markus Armbruster
        return bs->dev_ops->is_medium_locked(bs->dev_opaque);
1734 f107639a Markus Armbruster
    }
1735 f107639a Markus Armbruster
    return false;
1736 f107639a Markus Armbruster
}
1737 f107639a Markus Armbruster
1738 e97fc193 aliguori
/*
1739 e97fc193 aliguori
 * Run consistency checks on an image
1740 e97fc193 aliguori
 *
1741 e076f338 Kevin Wolf
 * Returns 0 if the check could be completed (it doesn't mean that the image is
1742 a1c7273b Stefan Weil
 * free of errors) or -errno when an internal error occurred. The results of the
1743 e076f338 Kevin Wolf
 * check are stored in res.
1744 e97fc193 aliguori
 */
1745 4534ff54 Kevin Wolf
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
1746 e97fc193 aliguori
{
1747 e97fc193 aliguori
    if (bs->drv->bdrv_check == NULL) {
1748 e97fc193 aliguori
        return -ENOTSUP;
1749 e97fc193 aliguori
    }
1750 e97fc193 aliguori
1751 e076f338 Kevin Wolf
    memset(res, 0, sizeof(*res));
1752 4534ff54 Kevin Wolf
    return bs->drv->bdrv_check(bs, res, fix);
1753 e97fc193 aliguori
}
1754 e97fc193 aliguori
1755 8a426614 Kevin Wolf
#define COMMIT_BUF_SECTORS 2048
1756 8a426614 Kevin Wolf
1757 33e3963e bellard
/* commit COW file into the raw image */
1758 33e3963e bellard
int bdrv_commit(BlockDriverState *bs)
1759 33e3963e bellard
{
1760 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1761 8a426614 Kevin Wolf
    int64_t sector, total_sectors;
1762 8a426614 Kevin Wolf
    int n, ro, open_flags;
1763 0bce597d Jeff Cody
    int ret = 0;
1764 8a426614 Kevin Wolf
    uint8_t *buf;
1765 c2cba3d9 Jim Meyering
    char filename[PATH_MAX];
1766 33e3963e bellard
1767 19cb3738 bellard
    if (!drv)
1768 19cb3738 bellard
        return -ENOMEDIUM;
1769 4dca4b63 Naphtali Sprei
    
1770 4dca4b63 Naphtali Sprei
    if (!bs->backing_hd) {
1771 4dca4b63 Naphtali Sprei
        return -ENOTSUP;
1772 33e3963e bellard
    }
1773 33e3963e bellard
1774 2d3735d3 Stefan Hajnoczi
    if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
1775 2d3735d3 Stefan Hajnoczi
        return -EBUSY;
1776 2d3735d3 Stefan Hajnoczi
    }
1777 2d3735d3 Stefan Hajnoczi
1778 4dca4b63 Naphtali Sprei
    ro = bs->backing_hd->read_only;
1779 c2cba3d9 Jim Meyering
    /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */
1780 c2cba3d9 Jim Meyering
    pstrcpy(filename, sizeof(filename), bs->backing_hd->filename);
1781 4dca4b63 Naphtali Sprei
    open_flags =  bs->backing_hd->open_flags;
1782 4dca4b63 Naphtali Sprei
1783 4dca4b63 Naphtali Sprei
    if (ro) {
1784 0bce597d Jeff Cody
        if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {
1785 0bce597d Jeff Cody
            return -EACCES;
1786 4dca4b63 Naphtali Sprei
        }
1787 ea2384d3 bellard
    }
1788 33e3963e bellard
1789 6ea44308 Jan Kiszka
    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
1790 7267c094 Anthony Liguori
    buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
1791 8a426614 Kevin Wolf
1792 8a426614 Kevin Wolf
    for (sector = 0; sector < total_sectors; sector += n) {
1793 05c4af54 Stefan Hajnoczi
        if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
1794 8a426614 Kevin Wolf
1795 8a426614 Kevin Wolf
            if (bdrv_read(bs, sector, buf, n) != 0) {
1796 8a426614 Kevin Wolf
                ret = -EIO;
1797 8a426614 Kevin Wolf
                goto ro_cleanup;
1798 8a426614 Kevin Wolf
            }
1799 8a426614 Kevin Wolf
1800 8a426614 Kevin Wolf
            if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
1801 8a426614 Kevin Wolf
                ret = -EIO;
1802 8a426614 Kevin Wolf
                goto ro_cleanup;
1803 8a426614 Kevin Wolf
            }
1804 ea2384d3 bellard
        }
1805 33e3963e bellard
    }
1806 95389c86 bellard
1807 1d44952f Christoph Hellwig
    if (drv->bdrv_make_empty) {
1808 1d44952f Christoph Hellwig
        ret = drv->bdrv_make_empty(bs);
1809 1d44952f Christoph Hellwig
        bdrv_flush(bs);
1810 1d44952f Christoph Hellwig
    }
1811 95389c86 bellard
1812 3f5075ae Christoph Hellwig
    /*
1813 3f5075ae Christoph Hellwig
     * Make sure all data we wrote to the backing device is actually
1814 3f5075ae Christoph Hellwig
     * stable on disk.
1815 3f5075ae Christoph Hellwig
     */
1816 3f5075ae Christoph Hellwig
    if (bs->backing_hd)
1817 3f5075ae Christoph Hellwig
        bdrv_flush(bs->backing_hd);
1818 4dca4b63 Naphtali Sprei
1819 4dca4b63 Naphtali Sprei
ro_cleanup:
1820 7267c094 Anthony Liguori
    g_free(buf);
1821 4dca4b63 Naphtali Sprei
1822 4dca4b63 Naphtali Sprei
    if (ro) {
1823 0bce597d Jeff Cody
        /* ignoring error return here */
1824 0bce597d Jeff Cody
        bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);
1825 4dca4b63 Naphtali Sprei
    }
1826 4dca4b63 Naphtali Sprei
1827 1d44952f Christoph Hellwig
    return ret;
1828 33e3963e bellard
}
1829 33e3963e bellard
1830 e8877497 Stefan Hajnoczi
int bdrv_commit_all(void)
1831 6ab4b5ab Markus Armbruster
{
1832 6ab4b5ab Markus Armbruster
    BlockDriverState *bs;
1833 6ab4b5ab Markus Armbruster
1834 6ab4b5ab Markus Armbruster
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1835 272d2d8e Jeff Cody
        if (bs->drv && bs->backing_hd) {
1836 272d2d8e Jeff Cody
            int ret = bdrv_commit(bs);
1837 272d2d8e Jeff Cody
            if (ret < 0) {
1838 272d2d8e Jeff Cody
                return ret;
1839 272d2d8e Jeff Cody
            }
1840 e8877497 Stefan Hajnoczi
        }
1841 6ab4b5ab Markus Armbruster
    }
1842 e8877497 Stefan Hajnoczi
    return 0;
1843 6ab4b5ab Markus Armbruster
}
1844 6ab4b5ab Markus Armbruster
1845 dbffbdcf Stefan Hajnoczi
/**
1846 dbffbdcf Stefan Hajnoczi
 * Remove an active request from the tracked requests list
1847 dbffbdcf Stefan Hajnoczi
 *
1848 dbffbdcf Stefan Hajnoczi
 * This function should be called when a tracked request is completing.
1849 dbffbdcf Stefan Hajnoczi
 */
1850 dbffbdcf Stefan Hajnoczi
static void tracked_request_end(BdrvTrackedRequest *req)
1851 dbffbdcf Stefan Hajnoczi
{
1852 dbffbdcf Stefan Hajnoczi
    QLIST_REMOVE(req, list);
1853 f4658285 Stefan Hajnoczi
    qemu_co_queue_restart_all(&req->wait_queue);
1854 dbffbdcf Stefan Hajnoczi
}
1855 dbffbdcf Stefan Hajnoczi
1856 dbffbdcf Stefan Hajnoczi
/**
1857 dbffbdcf Stefan Hajnoczi
 * Add an active request to the tracked requests list
1858 dbffbdcf Stefan Hajnoczi
 */
1859 dbffbdcf Stefan Hajnoczi
static void tracked_request_begin(BdrvTrackedRequest *req,
1860 dbffbdcf Stefan Hajnoczi
                                  BlockDriverState *bs,
1861 dbffbdcf Stefan Hajnoczi
                                  int64_t sector_num,
1862 dbffbdcf Stefan Hajnoczi
                                  int nb_sectors, bool is_write)
1863 dbffbdcf Stefan Hajnoczi
{
1864 dbffbdcf Stefan Hajnoczi
    *req = (BdrvTrackedRequest){
1865 dbffbdcf Stefan Hajnoczi
        .bs = bs,
1866 dbffbdcf Stefan Hajnoczi
        .sector_num = sector_num,
1867 dbffbdcf Stefan Hajnoczi
        .nb_sectors = nb_sectors,
1868 dbffbdcf Stefan Hajnoczi
        .is_write = is_write,
1869 5f8b6491 Stefan Hajnoczi
        .co = qemu_coroutine_self(),
1870 dbffbdcf Stefan Hajnoczi
    };
1871 dbffbdcf Stefan Hajnoczi
1872 f4658285 Stefan Hajnoczi
    qemu_co_queue_init(&req->wait_queue);
1873 f4658285 Stefan Hajnoczi
1874 dbffbdcf Stefan Hajnoczi
    QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
1875 dbffbdcf Stefan Hajnoczi
}
1876 dbffbdcf Stefan Hajnoczi
1877 d83947ac Stefan Hajnoczi
/**
1878 d83947ac Stefan Hajnoczi
 * Round a region to cluster boundaries
1879 d83947ac Stefan Hajnoczi
 */
1880 343bded4 Paolo Bonzini
void bdrv_round_to_clusters(BlockDriverState *bs,
1881 343bded4 Paolo Bonzini
                            int64_t sector_num, int nb_sectors,
1882 343bded4 Paolo Bonzini
                            int64_t *cluster_sector_num,
1883 343bded4 Paolo Bonzini
                            int *cluster_nb_sectors)
1884 d83947ac Stefan Hajnoczi
{
1885 d83947ac Stefan Hajnoczi
    BlockDriverInfo bdi;
1886 d83947ac Stefan Hajnoczi
1887 d83947ac Stefan Hajnoczi
    if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
1888 d83947ac Stefan Hajnoczi
        *cluster_sector_num = sector_num;
1889 d83947ac Stefan Hajnoczi
        *cluster_nb_sectors = nb_sectors;
1890 d83947ac Stefan Hajnoczi
    } else {
1891 d83947ac Stefan Hajnoczi
        int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
1892 d83947ac Stefan Hajnoczi
        *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
1893 d83947ac Stefan Hajnoczi
        *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
1894 d83947ac Stefan Hajnoczi
                                            nb_sectors, c);
1895 d83947ac Stefan Hajnoczi
    }
1896 d83947ac Stefan Hajnoczi
}
1897 d83947ac Stefan Hajnoczi
1898 f4658285 Stefan Hajnoczi
static bool tracked_request_overlaps(BdrvTrackedRequest *req,
1899 f4658285 Stefan Hajnoczi
                                     int64_t sector_num, int nb_sectors) {
1900 d83947ac Stefan Hajnoczi
    /*        aaaa   bbbb */
1901 d83947ac Stefan Hajnoczi
    if (sector_num >= req->sector_num + req->nb_sectors) {
1902 d83947ac Stefan Hajnoczi
        return false;
1903 d83947ac Stefan Hajnoczi
    }
1904 d83947ac Stefan Hajnoczi
    /* bbbb   aaaa        */
1905 d83947ac Stefan Hajnoczi
    if (req->sector_num >= sector_num + nb_sectors) {
1906 d83947ac Stefan Hajnoczi
        return false;
1907 d83947ac Stefan Hajnoczi
    }
1908 d83947ac Stefan Hajnoczi
    return true;
1909 f4658285 Stefan Hajnoczi
}
1910 f4658285 Stefan Hajnoczi
1911 f4658285 Stefan Hajnoczi
static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
1912 f4658285 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors)
1913 f4658285 Stefan Hajnoczi
{
1914 f4658285 Stefan Hajnoczi
    BdrvTrackedRequest *req;
1915 d83947ac Stefan Hajnoczi
    int64_t cluster_sector_num;
1916 d83947ac Stefan Hajnoczi
    int cluster_nb_sectors;
1917 f4658285 Stefan Hajnoczi
    bool retry;
1918 f4658285 Stefan Hajnoczi
1919 d83947ac Stefan Hajnoczi
    /* If we touch the same cluster it counts as an overlap.  This guarantees
1920 d83947ac Stefan Hajnoczi
     * that allocating writes will be serialized and not race with each other
1921 d83947ac Stefan Hajnoczi
     * for the same cluster.  For example, in copy-on-read it ensures that the
1922 d83947ac Stefan Hajnoczi
     * CoR read and write operations are atomic and guest writes cannot
1923 d83947ac Stefan Hajnoczi
     * interleave between them.
1924 d83947ac Stefan Hajnoczi
     */
1925 343bded4 Paolo Bonzini
    bdrv_round_to_clusters(bs, sector_num, nb_sectors,
1926 343bded4 Paolo Bonzini
                           &cluster_sector_num, &cluster_nb_sectors);
1927 d83947ac Stefan Hajnoczi
1928 f4658285 Stefan Hajnoczi
    do {
1929 f4658285 Stefan Hajnoczi
        retry = false;
1930 f4658285 Stefan Hajnoczi
        QLIST_FOREACH(req, &bs->tracked_requests, list) {
1931 d83947ac Stefan Hajnoczi
            if (tracked_request_overlaps(req, cluster_sector_num,
1932 d83947ac Stefan Hajnoczi
                                         cluster_nb_sectors)) {
1933 5f8b6491 Stefan Hajnoczi
                /* Hitting this means there was a reentrant request, for
1934 5f8b6491 Stefan Hajnoczi
                 * example, a block driver issuing nested requests.  This must
1935 5f8b6491 Stefan Hajnoczi
                 * never happen since it means deadlock.
1936 5f8b6491 Stefan Hajnoczi
                 */
1937 5f8b6491 Stefan Hajnoczi
                assert(qemu_coroutine_self() != req->co);
1938 5f8b6491 Stefan Hajnoczi
1939 f4658285 Stefan Hajnoczi
                qemu_co_queue_wait(&req->wait_queue);
1940 f4658285 Stefan Hajnoczi
                retry = true;
1941 f4658285 Stefan Hajnoczi
                break;
1942 f4658285 Stefan Hajnoczi
            }
1943 f4658285 Stefan Hajnoczi
        }
1944 f4658285 Stefan Hajnoczi
    } while (retry);
1945 f4658285 Stefan Hajnoczi
}
1946 f4658285 Stefan Hajnoczi
1947 756e6736 Kevin Wolf
/*
1948 756e6736 Kevin Wolf
 * Return values:
1949 756e6736 Kevin Wolf
 * 0        - success
1950 756e6736 Kevin Wolf
 * -EINVAL  - backing format specified, but no file
1951 756e6736 Kevin Wolf
 * -ENOSPC  - can't update the backing file because no space is left in the
1952 756e6736 Kevin Wolf
 *            image file header
1953 756e6736 Kevin Wolf
 * -ENOTSUP - format driver doesn't support changing the backing file
1954 756e6736 Kevin Wolf
 */
1955 756e6736 Kevin Wolf
int bdrv_change_backing_file(BlockDriverState *bs,
1956 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
1957 756e6736 Kevin Wolf
{
1958 756e6736 Kevin Wolf
    BlockDriver *drv = bs->drv;
1959 469ef350 Paolo Bonzini
    int ret;
1960 756e6736 Kevin Wolf
1961 5f377794 Paolo Bonzini
    /* Backing file format doesn't make sense without a backing file */
1962 5f377794 Paolo Bonzini
    if (backing_fmt && !backing_file) {
1963 5f377794 Paolo Bonzini
        return -EINVAL;
1964 5f377794 Paolo Bonzini
    }
1965 5f377794 Paolo Bonzini
1966 756e6736 Kevin Wolf
    if (drv->bdrv_change_backing_file != NULL) {
1967 469ef350 Paolo Bonzini
        ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
1968 756e6736 Kevin Wolf
    } else {
1969 469ef350 Paolo Bonzini
        ret = -ENOTSUP;
1970 756e6736 Kevin Wolf
    }
1971 469ef350 Paolo Bonzini
1972 469ef350 Paolo Bonzini
    if (ret == 0) {
1973 469ef350 Paolo Bonzini
        pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1974 469ef350 Paolo Bonzini
        pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1975 469ef350 Paolo Bonzini
    }
1976 469ef350 Paolo Bonzini
    return ret;
1977 756e6736 Kevin Wolf
}
1978 756e6736 Kevin Wolf
1979 6ebdcee2 Jeff Cody
/*
1980 6ebdcee2 Jeff Cody
 * Finds the image layer in the chain that has 'bs' as its backing file.
1981 6ebdcee2 Jeff Cody
 *
1982 6ebdcee2 Jeff Cody
 * active is the current topmost image.
1983 6ebdcee2 Jeff Cody
 *
1984 6ebdcee2 Jeff Cody
 * Returns NULL if bs is not found in active's image chain,
1985 6ebdcee2 Jeff Cody
 * or if active == bs.
1986 6ebdcee2 Jeff Cody
 */
1987 6ebdcee2 Jeff Cody
BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
1988 6ebdcee2 Jeff Cody
                                    BlockDriverState *bs)
1989 6ebdcee2 Jeff Cody
{
1990 6ebdcee2 Jeff Cody
    BlockDriverState *overlay = NULL;
1991 6ebdcee2 Jeff Cody
    BlockDriverState *intermediate;
1992 6ebdcee2 Jeff Cody
1993 6ebdcee2 Jeff Cody
    assert(active != NULL);
1994 6ebdcee2 Jeff Cody
    assert(bs != NULL);
1995 6ebdcee2 Jeff Cody
1996 6ebdcee2 Jeff Cody
    /* if bs is the same as active, then by definition it has no overlay
1997 6ebdcee2 Jeff Cody
     */
1998 6ebdcee2 Jeff Cody
    if (active == bs) {
1999 6ebdcee2 Jeff Cody
        return NULL;
2000 6ebdcee2 Jeff Cody
    }
2001 6ebdcee2 Jeff Cody
2002 6ebdcee2 Jeff Cody
    intermediate = active;
2003 6ebdcee2 Jeff Cody
    while (intermediate->backing_hd) {
2004 6ebdcee2 Jeff Cody
        if (intermediate->backing_hd == bs) {
2005 6ebdcee2 Jeff Cody
            overlay = intermediate;
2006 6ebdcee2 Jeff Cody
            break;
2007 6ebdcee2 Jeff Cody
        }
2008 6ebdcee2 Jeff Cody
        intermediate = intermediate->backing_hd;
2009 6ebdcee2 Jeff Cody
    }
2010 6ebdcee2 Jeff Cody
2011 6ebdcee2 Jeff Cody
    return overlay;
2012 6ebdcee2 Jeff Cody
}
2013 6ebdcee2 Jeff Cody
2014 6ebdcee2 Jeff Cody
typedef struct BlkIntermediateStates {
2015 6ebdcee2 Jeff Cody
    BlockDriverState *bs;
2016 6ebdcee2 Jeff Cody
    QSIMPLEQ_ENTRY(BlkIntermediateStates) entry;
2017 6ebdcee2 Jeff Cody
} BlkIntermediateStates;
2018 6ebdcee2 Jeff Cody
2019 6ebdcee2 Jeff Cody
2020 6ebdcee2 Jeff Cody
/*
2021 6ebdcee2 Jeff Cody
 * Drops images above 'base' up to and including 'top', and sets the image
2022 6ebdcee2 Jeff Cody
 * above 'top' to have base as its backing file.
2023 6ebdcee2 Jeff Cody
 *
2024 6ebdcee2 Jeff Cody
 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2025 6ebdcee2 Jeff Cody
 * information in 'bs' can be properly updated.
2026 6ebdcee2 Jeff Cody
 *
2027 6ebdcee2 Jeff Cody
 * E.g., this will convert the following chain:
2028 6ebdcee2 Jeff Cody
 * bottom <- base <- intermediate <- top <- active
2029 6ebdcee2 Jeff Cody
 *
2030 6ebdcee2 Jeff Cody
 * to
2031 6ebdcee2 Jeff Cody
 *
2032 6ebdcee2 Jeff Cody
 * bottom <- base <- active
2033 6ebdcee2 Jeff Cody
 *
2034 6ebdcee2 Jeff Cody
 * It is allowed for bottom==base, in which case it converts:
2035 6ebdcee2 Jeff Cody
 *
2036 6ebdcee2 Jeff Cody
 * base <- intermediate <- top <- active
2037 6ebdcee2 Jeff Cody
 *
2038 6ebdcee2 Jeff Cody
 * to
2039 6ebdcee2 Jeff Cody
 *
2040 6ebdcee2 Jeff Cody
 * base <- active
2041 6ebdcee2 Jeff Cody
 *
2042 6ebdcee2 Jeff Cody
 * Error conditions:
2043 6ebdcee2 Jeff Cody
 *  if active == top, that is considered an error
2044 6ebdcee2 Jeff Cody
 *
2045 6ebdcee2 Jeff Cody
 */
2046 6ebdcee2 Jeff Cody
int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
2047 6ebdcee2 Jeff Cody
                           BlockDriverState *base)
2048 6ebdcee2 Jeff Cody
{
2049 6ebdcee2 Jeff Cody
    BlockDriverState *intermediate;
2050 6ebdcee2 Jeff Cody
    BlockDriverState *base_bs = NULL;
2051 6ebdcee2 Jeff Cody
    BlockDriverState *new_top_bs = NULL;
2052 6ebdcee2 Jeff Cody
    BlkIntermediateStates *intermediate_state, *next;
2053 6ebdcee2 Jeff Cody
    int ret = -EIO;
2054 6ebdcee2 Jeff Cody
2055 6ebdcee2 Jeff Cody
    QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete;
2056 6ebdcee2 Jeff Cody
    QSIMPLEQ_INIT(&states_to_delete);
2057 6ebdcee2 Jeff Cody
2058 6ebdcee2 Jeff Cody
    if (!top->drv || !base->drv) {
2059 6ebdcee2 Jeff Cody
        goto exit;
2060 6ebdcee2 Jeff Cody
    }
2061 6ebdcee2 Jeff Cody
2062 6ebdcee2 Jeff Cody
    new_top_bs = bdrv_find_overlay(active, top);
2063 6ebdcee2 Jeff Cody
2064 6ebdcee2 Jeff Cody
    if (new_top_bs == NULL) {
2065 6ebdcee2 Jeff Cody
        /* we could not find the image above 'top', this is an error */
2066 6ebdcee2 Jeff Cody
        goto exit;
2067 6ebdcee2 Jeff Cody
    }
2068 6ebdcee2 Jeff Cody
2069 6ebdcee2 Jeff Cody
    /* special case of new_top_bs->backing_hd already pointing to base - nothing
2070 6ebdcee2 Jeff Cody
     * to do, no intermediate images */
2071 6ebdcee2 Jeff Cody
    if (new_top_bs->backing_hd == base) {
2072 6ebdcee2 Jeff Cody
        ret = 0;
2073 6ebdcee2 Jeff Cody
        goto exit;
2074 6ebdcee2 Jeff Cody
    }
2075 6ebdcee2 Jeff Cody
2076 6ebdcee2 Jeff Cody
    intermediate = top;
2077 6ebdcee2 Jeff Cody
2078 6ebdcee2 Jeff Cody
    /* now we will go down through the list, and add each BDS we find
2079 6ebdcee2 Jeff Cody
     * into our deletion queue, until we hit the 'base'
2080 6ebdcee2 Jeff Cody
     */
2081 6ebdcee2 Jeff Cody
    while (intermediate) {
2082 6ebdcee2 Jeff Cody
        intermediate_state = g_malloc0(sizeof(BlkIntermediateStates));
2083 6ebdcee2 Jeff Cody
        intermediate_state->bs = intermediate;
2084 6ebdcee2 Jeff Cody
        QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry);
2085 6ebdcee2 Jeff Cody
2086 6ebdcee2 Jeff Cody
        if (intermediate->backing_hd == base) {
2087 6ebdcee2 Jeff Cody
            base_bs = intermediate->backing_hd;
2088 6ebdcee2 Jeff Cody
            break;
2089 6ebdcee2 Jeff Cody
        }
2090 6ebdcee2 Jeff Cody
        intermediate = intermediate->backing_hd;
2091 6ebdcee2 Jeff Cody
    }
2092 6ebdcee2 Jeff Cody
    if (base_bs == NULL) {
2093 6ebdcee2 Jeff Cody
        /* something went wrong, we did not end at the base. safely
2094 6ebdcee2 Jeff Cody
         * unravel everything, and exit with error */
2095 6ebdcee2 Jeff Cody
        goto exit;
2096 6ebdcee2 Jeff Cody
    }
2097 6ebdcee2 Jeff Cody
2098 6ebdcee2 Jeff Cody
    /* success - we can delete the intermediate states, and link top->base */
2099 6ebdcee2 Jeff Cody
    ret = bdrv_change_backing_file(new_top_bs, base_bs->filename,
2100 6ebdcee2 Jeff Cody
                                   base_bs->drv ? base_bs->drv->format_name : "");
2101 6ebdcee2 Jeff Cody
    if (ret) {
2102 6ebdcee2 Jeff Cody
        goto exit;
2103 6ebdcee2 Jeff Cody
    }
2104 6ebdcee2 Jeff Cody
    new_top_bs->backing_hd = base_bs;
2105 6ebdcee2 Jeff Cody
2106 6ebdcee2 Jeff Cody
2107 6ebdcee2 Jeff Cody
    QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2108 6ebdcee2 Jeff Cody
        /* so that bdrv_close() does not recursively close the chain */
2109 6ebdcee2 Jeff Cody
        intermediate_state->bs->backing_hd = NULL;
2110 6ebdcee2 Jeff Cody
        bdrv_delete(intermediate_state->bs);
2111 6ebdcee2 Jeff Cody
    }
2112 6ebdcee2 Jeff Cody
    ret = 0;
2113 6ebdcee2 Jeff Cody
2114 6ebdcee2 Jeff Cody
exit:
2115 6ebdcee2 Jeff Cody
    QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2116 6ebdcee2 Jeff Cody
        g_free(intermediate_state);
2117 6ebdcee2 Jeff Cody
    }
2118 6ebdcee2 Jeff Cody
    return ret;
2119 6ebdcee2 Jeff Cody
}
2120 6ebdcee2 Jeff Cody
2121 6ebdcee2 Jeff Cody
2122 71d0770c aliguori
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
2123 71d0770c aliguori
                                   size_t size)
2124 71d0770c aliguori
{
2125 71d0770c aliguori
    int64_t len;
2126 71d0770c aliguori
2127 71d0770c aliguori
    if (!bdrv_is_inserted(bs))
2128 71d0770c aliguori
        return -ENOMEDIUM;
2129 71d0770c aliguori
2130 71d0770c aliguori
    if (bs->growable)
2131 71d0770c aliguori
        return 0;
2132 71d0770c aliguori
2133 71d0770c aliguori
    len = bdrv_getlength(bs);
2134 71d0770c aliguori
2135 fbb7b4e0 Kevin Wolf
    if (offset < 0)
2136 fbb7b4e0 Kevin Wolf
        return -EIO;
2137 fbb7b4e0 Kevin Wolf
2138 fbb7b4e0 Kevin Wolf
    if ((offset > len) || (len - offset < size))
2139 71d0770c aliguori
        return -EIO;
2140 71d0770c aliguori
2141 71d0770c aliguori
    return 0;
2142 71d0770c aliguori
}
2143 71d0770c aliguori
2144 71d0770c aliguori
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
2145 71d0770c aliguori
                              int nb_sectors)
2146 71d0770c aliguori
{
2147 eb5a3165 Jes Sorensen
    return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
2148 eb5a3165 Jes Sorensen
                                   nb_sectors * BDRV_SECTOR_SIZE);
2149 71d0770c aliguori
}
2150 71d0770c aliguori
2151 1c9805a3 Stefan Hajnoczi
typedef struct RwCo {
2152 1c9805a3 Stefan Hajnoczi
    BlockDriverState *bs;
2153 1c9805a3 Stefan Hajnoczi
    int64_t sector_num;
2154 1c9805a3 Stefan Hajnoczi
    int nb_sectors;
2155 1c9805a3 Stefan Hajnoczi
    QEMUIOVector *qiov;
2156 1c9805a3 Stefan Hajnoczi
    bool is_write;
2157 1c9805a3 Stefan Hajnoczi
    int ret;
2158 1c9805a3 Stefan Hajnoczi
} RwCo;
2159 1c9805a3 Stefan Hajnoczi
2160 1c9805a3 Stefan Hajnoczi
static void coroutine_fn bdrv_rw_co_entry(void *opaque)
2161 fc01f7e7 bellard
{
2162 1c9805a3 Stefan Hajnoczi
    RwCo *rwco = opaque;
2163 ea2384d3 bellard
2164 1c9805a3 Stefan Hajnoczi
    if (!rwco->is_write) {
2165 1c9805a3 Stefan Hajnoczi
        rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
2166 470c0504 Stefan Hajnoczi
                                     rwco->nb_sectors, rwco->qiov, 0);
2167 1c9805a3 Stefan Hajnoczi
    } else {
2168 1c9805a3 Stefan Hajnoczi
        rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
2169 f08f2dda Stefan Hajnoczi
                                      rwco->nb_sectors, rwco->qiov, 0);
2170 1c9805a3 Stefan Hajnoczi
    }
2171 1c9805a3 Stefan Hajnoczi
}
2172 e7a8a783 Kevin Wolf
2173 1c9805a3 Stefan Hajnoczi
/*
2174 8d3b1a2d Kevin Wolf
 * Process a vectored synchronous request using coroutines
2175 1c9805a3 Stefan Hajnoczi
 */
2176 8d3b1a2d Kevin Wolf
static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num,
2177 8d3b1a2d Kevin Wolf
                       QEMUIOVector *qiov, bool is_write)
2178 1c9805a3 Stefan Hajnoczi
{
2179 1c9805a3 Stefan Hajnoczi
    Coroutine *co;
2180 1c9805a3 Stefan Hajnoczi
    RwCo rwco = {
2181 1c9805a3 Stefan Hajnoczi
        .bs = bs,
2182 1c9805a3 Stefan Hajnoczi
        .sector_num = sector_num,
2183 8d3b1a2d Kevin Wolf
        .nb_sectors = qiov->size >> BDRV_SECTOR_BITS,
2184 8d3b1a2d Kevin Wolf
        .qiov = qiov,
2185 1c9805a3 Stefan Hajnoczi
        .is_write = is_write,
2186 1c9805a3 Stefan Hajnoczi
        .ret = NOT_DONE,
2187 1c9805a3 Stefan Hajnoczi
    };
2188 8d3b1a2d Kevin Wolf
    assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0);
2189 e7a8a783 Kevin Wolf
2190 498e386c Zhi Yong Wu
    /**
2191 498e386c Zhi Yong Wu
     * In sync call context, when the vcpu is blocked, this throttling timer
2192 498e386c Zhi Yong Wu
     * will not fire; so the I/O throttling function has to be disabled here
2193 498e386c Zhi Yong Wu
     * if it has been enabled.
2194 498e386c Zhi Yong Wu
     */
2195 498e386c Zhi Yong Wu
    if (bs->io_limits_enabled) {
2196 498e386c Zhi Yong Wu
        fprintf(stderr, "Disabling I/O throttling on '%s' due "
2197 498e386c Zhi Yong Wu
                        "to synchronous I/O.\n", bdrv_get_device_name(bs));
2198 498e386c Zhi Yong Wu
        bdrv_io_limits_disable(bs);
2199 498e386c Zhi Yong Wu
    }
2200 498e386c Zhi Yong Wu
2201 1c9805a3 Stefan Hajnoczi
    if (qemu_in_coroutine()) {
2202 1c9805a3 Stefan Hajnoczi
        /* Fast-path if already in coroutine context */
2203 1c9805a3 Stefan Hajnoczi
        bdrv_rw_co_entry(&rwco);
2204 1c9805a3 Stefan Hajnoczi
    } else {
2205 1c9805a3 Stefan Hajnoczi
        co = qemu_coroutine_create(bdrv_rw_co_entry);
2206 1c9805a3 Stefan Hajnoczi
        qemu_coroutine_enter(co, &rwco);
2207 1c9805a3 Stefan Hajnoczi
        while (rwco.ret == NOT_DONE) {
2208 1c9805a3 Stefan Hajnoczi
            qemu_aio_wait();
2209 1c9805a3 Stefan Hajnoczi
        }
2210 1c9805a3 Stefan Hajnoczi
    }
2211 1c9805a3 Stefan Hajnoczi
    return rwco.ret;
2212 1c9805a3 Stefan Hajnoczi
}
2213 b338082b bellard
2214 8d3b1a2d Kevin Wolf
/*
2215 8d3b1a2d Kevin Wolf
 * Process a synchronous request using coroutines
2216 8d3b1a2d Kevin Wolf
 */
2217 8d3b1a2d Kevin Wolf
static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
2218 8d3b1a2d Kevin Wolf
                      int nb_sectors, bool is_write)
2219 8d3b1a2d Kevin Wolf
{
2220 8d3b1a2d Kevin Wolf
    QEMUIOVector qiov;
2221 8d3b1a2d Kevin Wolf
    struct iovec iov = {
2222 8d3b1a2d Kevin Wolf
        .iov_base = (void *)buf,
2223 8d3b1a2d Kevin Wolf
        .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
2224 8d3b1a2d Kevin Wolf
    };
2225 8d3b1a2d Kevin Wolf
2226 8d3b1a2d Kevin Wolf
    qemu_iovec_init_external(&qiov, &iov, 1);
2227 8d3b1a2d Kevin Wolf
    return bdrv_rwv_co(bs, sector_num, &qiov, is_write);
2228 8d3b1a2d Kevin Wolf
}
2229 8d3b1a2d Kevin Wolf
2230 1c9805a3 Stefan Hajnoczi
/* return < 0 if error. See bdrv_write() for the return codes */
2231 1c9805a3 Stefan Hajnoczi
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
2232 1c9805a3 Stefan Hajnoczi
              uint8_t *buf, int nb_sectors)
2233 1c9805a3 Stefan Hajnoczi
{
2234 1c9805a3 Stefan Hajnoczi
    return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
2235 fc01f7e7 bellard
}
2236 fc01f7e7 bellard
2237 07d27a44 Markus Armbruster
/* Just like bdrv_read(), but with I/O throttling temporarily disabled */
2238 07d27a44 Markus Armbruster
int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
2239 07d27a44 Markus Armbruster
                          uint8_t *buf, int nb_sectors)
2240 07d27a44 Markus Armbruster
{
2241 07d27a44 Markus Armbruster
    bool enabled;
2242 07d27a44 Markus Armbruster
    int ret;
2243 07d27a44 Markus Armbruster
2244 07d27a44 Markus Armbruster
    enabled = bs->io_limits_enabled;
2245 07d27a44 Markus Armbruster
    bs->io_limits_enabled = false;
2246 07d27a44 Markus Armbruster
    ret = bdrv_read(bs, 0, buf, 1);
2247 07d27a44 Markus Armbruster
    bs->io_limits_enabled = enabled;
2248 07d27a44 Markus Armbruster
    return ret;
2249 07d27a44 Markus Armbruster
}
2250 07d27a44 Markus Armbruster
2251 5fafdf24 ths
/* Return < 0 if error. Important errors are:
2252 19cb3738 bellard
  -EIO         generic I/O error (may happen for all errors)
2253 19cb3738 bellard
  -ENOMEDIUM   No media inserted.
2254 19cb3738 bellard
  -EINVAL      Invalid sector number or nb_sectors
2255 19cb3738 bellard
  -EACCES      Trying to write a read-only device
2256 19cb3738 bellard
*/
2257 5fafdf24 ths
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
2258 fc01f7e7 bellard
               const uint8_t *buf, int nb_sectors)
2259 fc01f7e7 bellard
{
2260 1c9805a3 Stefan Hajnoczi
    return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
2261 83f64091 bellard
}
2262 83f64091 bellard
2263 8d3b1a2d Kevin Wolf
int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
2264 8d3b1a2d Kevin Wolf
{
2265 8d3b1a2d Kevin Wolf
    return bdrv_rwv_co(bs, sector_num, qiov, true);
2266 8d3b1a2d Kevin Wolf
}
2267 8d3b1a2d Kevin Wolf
2268 eda578e5 aliguori
int bdrv_pread(BlockDriverState *bs, int64_t offset,
2269 eda578e5 aliguori
               void *buf, int count1)
2270 83f64091 bellard
{
2271 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
2272 83f64091 bellard
    int len, nb_sectors, count;
2273 83f64091 bellard
    int64_t sector_num;
2274 9a8c4cce Kevin Wolf
    int ret;
2275 83f64091 bellard
2276 83f64091 bellard
    count = count1;
2277 83f64091 bellard
    /* first read to align to sector start */
2278 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
2279 83f64091 bellard
    if (len > count)
2280 83f64091 bellard
        len = count;
2281 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
2282 83f64091 bellard
    if (len > 0) {
2283 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2284 9a8c4cce Kevin Wolf
            return ret;
2285 6ea44308 Jan Kiszka
        memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
2286 83f64091 bellard
        count -= len;
2287 83f64091 bellard
        if (count == 0)
2288 83f64091 bellard
            return count1;
2289 83f64091 bellard
        sector_num++;
2290 83f64091 bellard
        buf += len;
2291 83f64091 bellard
    }
2292 83f64091 bellard
2293 83f64091 bellard
    /* read the sectors "in place" */
2294 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
2295 83f64091 bellard
    if (nb_sectors > 0) {
2296 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
2297 9a8c4cce Kevin Wolf
            return ret;
2298 83f64091 bellard
        sector_num += nb_sectors;
2299 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
2300 83f64091 bellard
        buf += len;
2301 83f64091 bellard
        count -= len;
2302 83f64091 bellard
    }
2303 83f64091 bellard
2304 83f64091 bellard
    /* add data from the last sector */
2305 83f64091 bellard
    if (count > 0) {
2306 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2307 9a8c4cce Kevin Wolf
            return ret;
2308 83f64091 bellard
        memcpy(buf, tmp_buf, count);
2309 83f64091 bellard
    }
2310 83f64091 bellard
    return count1;
2311 83f64091 bellard
}
2312 83f64091 bellard
2313 8d3b1a2d Kevin Wolf
int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov)
2314 83f64091 bellard
{
2315 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
2316 83f64091 bellard
    int len, nb_sectors, count;
2317 83f64091 bellard
    int64_t sector_num;
2318 9a8c4cce Kevin Wolf
    int ret;
2319 83f64091 bellard
2320 8d3b1a2d Kevin Wolf
    count = qiov->size;
2321 8d3b1a2d Kevin Wolf
2322 83f64091 bellard
    /* first write to align to sector start */
2323 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
2324 83f64091 bellard
    if (len > count)
2325 83f64091 bellard
        len = count;
2326 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
2327 83f64091 bellard
    if (len > 0) {
2328 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2329 9a8c4cce Kevin Wolf
            return ret;
2330 8d3b1a2d Kevin Wolf
        qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)),
2331 8d3b1a2d Kevin Wolf
                          len);
2332 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2333 9a8c4cce Kevin Wolf
            return ret;
2334 83f64091 bellard
        count -= len;
2335 83f64091 bellard
        if (count == 0)
2336 8d3b1a2d Kevin Wolf
            return qiov->size;
2337 83f64091 bellard
        sector_num++;
2338 83f64091 bellard
    }
2339 83f64091 bellard
2340 83f64091 bellard
    /* write the sectors "in place" */
2341 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
2342 83f64091 bellard
    if (nb_sectors > 0) {
2343 8d3b1a2d Kevin Wolf
        QEMUIOVector qiov_inplace;
2344 8d3b1a2d Kevin Wolf
2345 8d3b1a2d Kevin Wolf
        qemu_iovec_init(&qiov_inplace, qiov->niov);
2346 8d3b1a2d Kevin Wolf
        qemu_iovec_concat(&qiov_inplace, qiov, len,
2347 8d3b1a2d Kevin Wolf
                          nb_sectors << BDRV_SECTOR_BITS);
2348 8d3b1a2d Kevin Wolf
        ret = bdrv_writev(bs, sector_num, &qiov_inplace);
2349 8d3b1a2d Kevin Wolf
        qemu_iovec_destroy(&qiov_inplace);
2350 8d3b1a2d Kevin Wolf
        if (ret < 0) {
2351 9a8c4cce Kevin Wolf
            return ret;
2352 8d3b1a2d Kevin Wolf
        }
2353 8d3b1a2d Kevin Wolf
2354 83f64091 bellard
        sector_num += nb_sectors;
2355 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
2356 83f64091 bellard
        count -= len;
2357 83f64091 bellard
    }
2358 83f64091 bellard
2359 83f64091 bellard
    /* add data from the last sector */
2360 83f64091 bellard
    if (count > 0) {
2361 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2362 9a8c4cce Kevin Wolf
            return ret;
2363 8d3b1a2d Kevin Wolf
        qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count);
2364 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2365 9a8c4cce Kevin Wolf
            return ret;
2366 83f64091 bellard
    }
2367 8d3b1a2d Kevin Wolf
    return qiov->size;
2368 8d3b1a2d Kevin Wolf
}
2369 8d3b1a2d Kevin Wolf
2370 8d3b1a2d Kevin Wolf
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
2371 8d3b1a2d Kevin Wolf
                const void *buf, int count1)
2372 8d3b1a2d Kevin Wolf
{
2373 8d3b1a2d Kevin Wolf
    QEMUIOVector qiov;
2374 8d3b1a2d Kevin Wolf
    struct iovec iov = {
2375 8d3b1a2d Kevin Wolf
        .iov_base   = (void *) buf,
2376 8d3b1a2d Kevin Wolf
        .iov_len    = count1,
2377 8d3b1a2d Kevin Wolf
    };
2378 8d3b1a2d Kevin Wolf
2379 8d3b1a2d Kevin Wolf
    qemu_iovec_init_external(&qiov, &iov, 1);
2380 8d3b1a2d Kevin Wolf
    return bdrv_pwritev(bs, offset, &qiov);
2381 83f64091 bellard
}
2382 83f64091 bellard
2383 f08145fe Kevin Wolf
/*
2384 f08145fe Kevin Wolf
 * Writes to the file and ensures that no writes are reordered across this
2385 f08145fe Kevin Wolf
 * request (acts as a barrier)
2386 f08145fe Kevin Wolf
 *
2387 f08145fe Kevin Wolf
 * Returns 0 on success, -errno in error cases.
2388 f08145fe Kevin Wolf
 */
2389 f08145fe Kevin Wolf
int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
2390 f08145fe Kevin Wolf
    const void *buf, int count)
2391 f08145fe Kevin Wolf
{
2392 f08145fe Kevin Wolf
    int ret;
2393 f08145fe Kevin Wolf
2394 f08145fe Kevin Wolf
    ret = bdrv_pwrite(bs, offset, buf, count);
2395 f08145fe Kevin Wolf
    if (ret < 0) {
2396 f08145fe Kevin Wolf
        return ret;
2397 f08145fe Kevin Wolf
    }
2398 f08145fe Kevin Wolf
2399 f05fa4ad Paolo Bonzini
    /* No flush needed for cache modes that already do it */
2400 f05fa4ad Paolo Bonzini
    if (bs->enable_write_cache) {
2401 f08145fe Kevin Wolf
        bdrv_flush(bs);
2402 f08145fe Kevin Wolf
    }
2403 f08145fe Kevin Wolf
2404 f08145fe Kevin Wolf
    return 0;
2405 f08145fe Kevin Wolf
}
2406 f08145fe Kevin Wolf
2407 470c0504 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
2408 ab185921 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2409 ab185921 Stefan Hajnoczi
{
2410 ab185921 Stefan Hajnoczi
    /* Perform I/O through a temporary buffer so that users who scribble over
2411 ab185921 Stefan Hajnoczi
     * their read buffer while the operation is in progress do not end up
2412 ab185921 Stefan Hajnoczi
     * modifying the image file.  This is critical for zero-copy guest I/O
2413 ab185921 Stefan Hajnoczi
     * where anything might happen inside guest memory.
2414 ab185921 Stefan Hajnoczi
     */
2415 ab185921 Stefan Hajnoczi
    void *bounce_buffer;
2416 ab185921 Stefan Hajnoczi
2417 79c053bd Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
2418 ab185921 Stefan Hajnoczi
    struct iovec iov;
2419 ab185921 Stefan Hajnoczi
    QEMUIOVector bounce_qiov;
2420 ab185921 Stefan Hajnoczi
    int64_t cluster_sector_num;
2421 ab185921 Stefan Hajnoczi
    int cluster_nb_sectors;
2422 ab185921 Stefan Hajnoczi
    size_t skip_bytes;
2423 ab185921 Stefan Hajnoczi
    int ret;
2424 ab185921 Stefan Hajnoczi
2425 ab185921 Stefan Hajnoczi
    /* Cover entire cluster so no additional backing file I/O is required when
2426 ab185921 Stefan Hajnoczi
     * allocating cluster in the image file.
2427 ab185921 Stefan Hajnoczi
     */
2428 343bded4 Paolo Bonzini
    bdrv_round_to_clusters(bs, sector_num, nb_sectors,
2429 343bded4 Paolo Bonzini
                           &cluster_sector_num, &cluster_nb_sectors);
2430 ab185921 Stefan Hajnoczi
2431 470c0504 Stefan Hajnoczi
    trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors,
2432 470c0504 Stefan Hajnoczi
                                   cluster_sector_num, cluster_nb_sectors);
2433 ab185921 Stefan Hajnoczi
2434 ab185921 Stefan Hajnoczi
    iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE;
2435 ab185921 Stefan Hajnoczi
    iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
2436 ab185921 Stefan Hajnoczi
    qemu_iovec_init_external(&bounce_qiov, &iov, 1);
2437 ab185921 Stefan Hajnoczi
2438 79c053bd Stefan Hajnoczi
    ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
2439 79c053bd Stefan Hajnoczi
                             &bounce_qiov);
2440 ab185921 Stefan Hajnoczi
    if (ret < 0) {
2441 ab185921 Stefan Hajnoczi
        goto err;
2442 ab185921 Stefan Hajnoczi
    }
2443 ab185921 Stefan Hajnoczi
2444 79c053bd Stefan Hajnoczi
    if (drv->bdrv_co_write_zeroes &&
2445 79c053bd Stefan Hajnoczi
        buffer_is_zero(bounce_buffer, iov.iov_len)) {
2446 621f0589 Kevin Wolf
        ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
2447 621f0589 Kevin Wolf
                                      cluster_nb_sectors);
2448 79c053bd Stefan Hajnoczi
    } else {
2449 f05fa4ad Paolo Bonzini
        /* This does not change the data on the disk, it is not necessary
2450 f05fa4ad Paolo Bonzini
         * to flush even in cache=writethrough mode.
2451 f05fa4ad Paolo Bonzini
         */
2452 79c053bd Stefan Hajnoczi
        ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
2453 ab185921 Stefan Hajnoczi
                                  &bounce_qiov);
2454 79c053bd Stefan Hajnoczi
    }
2455 79c053bd Stefan Hajnoczi
2456 ab185921 Stefan Hajnoczi
    if (ret < 0) {
2457 ab185921 Stefan Hajnoczi
        /* It might be okay to ignore write errors for guest requests.  If this
2458 ab185921 Stefan Hajnoczi
         * is a deliberate copy-on-read then we don't want to ignore the error.
2459 ab185921 Stefan Hajnoczi
         * Simply report it in all cases.
2460 ab185921 Stefan Hajnoczi
         */
2461 ab185921 Stefan Hajnoczi
        goto err;
2462 ab185921 Stefan Hajnoczi
    }
2463 ab185921 Stefan Hajnoczi
2464 ab185921 Stefan Hajnoczi
    skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE;
2465 03396148 Michael Tokarev
    qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
2466 03396148 Michael Tokarev
                        nb_sectors * BDRV_SECTOR_SIZE);
2467 ab185921 Stefan Hajnoczi
2468 ab185921 Stefan Hajnoczi
err:
2469 ab185921 Stefan Hajnoczi
    qemu_vfree(bounce_buffer);
2470 ab185921 Stefan Hajnoczi
    return ret;
2471 ab185921 Stefan Hajnoczi
}
2472 ab185921 Stefan Hajnoczi
2473 c5fbe571 Stefan Hajnoczi
/*
2474 c5fbe571 Stefan Hajnoczi
 * Handle a read request in coroutine context
2475 c5fbe571 Stefan Hajnoczi
 */
2476 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
2477 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2478 470c0504 Stefan Hajnoczi
    BdrvRequestFlags flags)
2479 da1fa91d Kevin Wolf
{
2480 da1fa91d Kevin Wolf
    BlockDriver *drv = bs->drv;
2481 dbffbdcf Stefan Hajnoczi
    BdrvTrackedRequest req;
2482 dbffbdcf Stefan Hajnoczi
    int ret;
2483 da1fa91d Kevin Wolf
2484 da1fa91d Kevin Wolf
    if (!drv) {
2485 da1fa91d Kevin Wolf
        return -ENOMEDIUM;
2486 da1fa91d Kevin Wolf
    }
2487 da1fa91d Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2488 da1fa91d Kevin Wolf
        return -EIO;
2489 da1fa91d Kevin Wolf
    }
2490 da1fa91d Kevin Wolf
2491 98f90dba Zhi Yong Wu
    /* throttling disk read I/O */
2492 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
2493 98f90dba Zhi Yong Wu
        bdrv_io_limits_intercept(bs, false, nb_sectors);
2494 98f90dba Zhi Yong Wu
    }
2495 98f90dba Zhi Yong Wu
2496 f4658285 Stefan Hajnoczi
    if (bs->copy_on_read) {
2497 470c0504 Stefan Hajnoczi
        flags |= BDRV_REQ_COPY_ON_READ;
2498 470c0504 Stefan Hajnoczi
    }
2499 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
2500 470c0504 Stefan Hajnoczi
        bs->copy_on_read_in_flight++;
2501 470c0504 Stefan Hajnoczi
    }
2502 470c0504 Stefan Hajnoczi
2503 470c0504 Stefan Hajnoczi
    if (bs->copy_on_read_in_flight) {
2504 f4658285 Stefan Hajnoczi
        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2505 f4658285 Stefan Hajnoczi
    }
2506 f4658285 Stefan Hajnoczi
2507 dbffbdcf Stefan Hajnoczi
    tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
2508 ab185921 Stefan Hajnoczi
2509 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
2510 ab185921 Stefan Hajnoczi
        int pnum;
2511 ab185921 Stefan Hajnoczi
2512 ab185921 Stefan Hajnoczi
        ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum);
2513 ab185921 Stefan Hajnoczi
        if (ret < 0) {
2514 ab185921 Stefan Hajnoczi
            goto out;
2515 ab185921 Stefan Hajnoczi
        }
2516 ab185921 Stefan Hajnoczi
2517 ab185921 Stefan Hajnoczi
        if (!ret || pnum != nb_sectors) {
2518 470c0504 Stefan Hajnoczi
            ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov);
2519 ab185921 Stefan Hajnoczi
            goto out;
2520 ab185921 Stefan Hajnoczi
        }
2521 ab185921 Stefan Hajnoczi
    }
2522 ab185921 Stefan Hajnoczi
2523 dbffbdcf Stefan Hajnoczi
    ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
2524 ab185921 Stefan Hajnoczi
2525 ab185921 Stefan Hajnoczi
out:
2526 dbffbdcf Stefan Hajnoczi
    tracked_request_end(&req);
2527 470c0504 Stefan Hajnoczi
2528 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
2529 470c0504 Stefan Hajnoczi
        bs->copy_on_read_in_flight--;
2530 470c0504 Stefan Hajnoczi
    }
2531 470c0504 Stefan Hajnoczi
2532 dbffbdcf Stefan Hajnoczi
    return ret;
2533 da1fa91d Kevin Wolf
}
2534 da1fa91d Kevin Wolf
2535 c5fbe571 Stefan Hajnoczi
int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
2536 da1fa91d Kevin Wolf
    int nb_sectors, QEMUIOVector *qiov)
2537 da1fa91d Kevin Wolf
{
2538 c5fbe571 Stefan Hajnoczi
    trace_bdrv_co_readv(bs, sector_num, nb_sectors);
2539 da1fa91d Kevin Wolf
2540 470c0504 Stefan Hajnoczi
    return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0);
2541 470c0504 Stefan Hajnoczi
}
2542 470c0504 Stefan Hajnoczi
2543 470c0504 Stefan Hajnoczi
int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
2544 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2545 470c0504 Stefan Hajnoczi
{
2546 470c0504 Stefan Hajnoczi
    trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors);
2547 470c0504 Stefan Hajnoczi
2548 470c0504 Stefan Hajnoczi
    return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
2549 470c0504 Stefan Hajnoczi
                            BDRV_REQ_COPY_ON_READ);
2550 c5fbe571 Stefan Hajnoczi
}
2551 c5fbe571 Stefan Hajnoczi
2552 f08f2dda Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
2553 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors)
2554 f08f2dda Stefan Hajnoczi
{
2555 f08f2dda Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
2556 f08f2dda Stefan Hajnoczi
    QEMUIOVector qiov;
2557 f08f2dda Stefan Hajnoczi
    struct iovec iov;
2558 f08f2dda Stefan Hajnoczi
    int ret;
2559 f08f2dda Stefan Hajnoczi
2560 621f0589 Kevin Wolf
    /* TODO Emulate only part of misaligned requests instead of letting block
2561 621f0589 Kevin Wolf
     * drivers return -ENOTSUP and emulate everything */
2562 621f0589 Kevin Wolf
2563 f08f2dda Stefan Hajnoczi
    /* First try the efficient write zeroes operation */
2564 f08f2dda Stefan Hajnoczi
    if (drv->bdrv_co_write_zeroes) {
2565 621f0589 Kevin Wolf
        ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
2566 621f0589 Kevin Wolf
        if (ret != -ENOTSUP) {
2567 621f0589 Kevin Wolf
            return ret;
2568 621f0589 Kevin Wolf
        }
2569 f08f2dda Stefan Hajnoczi
    }
2570 f08f2dda Stefan Hajnoczi
2571 f08f2dda Stefan Hajnoczi
    /* Fall back to bounce buffer if write zeroes is unsupported */
2572 f08f2dda Stefan Hajnoczi
    iov.iov_len  = nb_sectors * BDRV_SECTOR_SIZE;
2573 f08f2dda Stefan Hajnoczi
    iov.iov_base = qemu_blockalign(bs, iov.iov_len);
2574 f08f2dda Stefan Hajnoczi
    memset(iov.iov_base, 0, iov.iov_len);
2575 f08f2dda Stefan Hajnoczi
    qemu_iovec_init_external(&qiov, &iov, 1);
2576 f08f2dda Stefan Hajnoczi
2577 f08f2dda Stefan Hajnoczi
    ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov);
2578 f08f2dda Stefan Hajnoczi
2579 f08f2dda Stefan Hajnoczi
    qemu_vfree(iov.iov_base);
2580 f08f2dda Stefan Hajnoczi
    return ret;
2581 f08f2dda Stefan Hajnoczi
}
2582 f08f2dda Stefan Hajnoczi
2583 c5fbe571 Stefan Hajnoczi
/*
2584 c5fbe571 Stefan Hajnoczi
 * Handle a write request in coroutine context
2585 c5fbe571 Stefan Hajnoczi
 */
2586 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
2587 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2588 f08f2dda Stefan Hajnoczi
    BdrvRequestFlags flags)
2589 c5fbe571 Stefan Hajnoczi
{
2590 c5fbe571 Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
2591 dbffbdcf Stefan Hajnoczi
    BdrvTrackedRequest req;
2592 6b7cb247 Stefan Hajnoczi
    int ret;
2593 da1fa91d Kevin Wolf
2594 da1fa91d Kevin Wolf
    if (!bs->drv) {
2595 da1fa91d Kevin Wolf
        return -ENOMEDIUM;
2596 da1fa91d Kevin Wolf
    }
2597 da1fa91d Kevin Wolf
    if (bs->read_only) {
2598 da1fa91d Kevin Wolf
        return -EACCES;
2599 da1fa91d Kevin Wolf
    }
2600 da1fa91d Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2601 da1fa91d Kevin Wolf
        return -EIO;
2602 da1fa91d Kevin Wolf
    }
2603 da1fa91d Kevin Wolf
2604 98f90dba Zhi Yong Wu
    /* throttling disk write I/O */
2605 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
2606 98f90dba Zhi Yong Wu
        bdrv_io_limits_intercept(bs, true, nb_sectors);
2607 98f90dba Zhi Yong Wu
    }
2608 98f90dba Zhi Yong Wu
2609 470c0504 Stefan Hajnoczi
    if (bs->copy_on_read_in_flight) {
2610 f4658285 Stefan Hajnoczi
        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2611 f4658285 Stefan Hajnoczi
    }
2612 f4658285 Stefan Hajnoczi
2613 dbffbdcf Stefan Hajnoczi
    tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
2614 dbffbdcf Stefan Hajnoczi
2615 d616b224 Stefan Hajnoczi
    ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req);
2616 d616b224 Stefan Hajnoczi
2617 d616b224 Stefan Hajnoczi
    if (ret < 0) {
2618 d616b224 Stefan Hajnoczi
        /* Do nothing, write notifier decided to fail this request */
2619 d616b224 Stefan Hajnoczi
    } else if (flags & BDRV_REQ_ZERO_WRITE) {
2620 f08f2dda Stefan Hajnoczi
        ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
2621 f08f2dda Stefan Hajnoczi
    } else {
2622 f08f2dda Stefan Hajnoczi
        ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
2623 f08f2dda Stefan Hajnoczi
    }
2624 6b7cb247 Stefan Hajnoczi
2625 f05fa4ad Paolo Bonzini
    if (ret == 0 && !bs->enable_write_cache) {
2626 f05fa4ad Paolo Bonzini
        ret = bdrv_co_flush(bs);
2627 f05fa4ad Paolo Bonzini
    }
2628 f05fa4ad Paolo Bonzini
2629 da1fa91d Kevin Wolf
    if (bs->dirty_bitmap) {
2630 1755da16 Paolo Bonzini
        bdrv_set_dirty(bs, sector_num, nb_sectors);
2631 da1fa91d Kevin Wolf
    }
2632 da1fa91d Kevin Wolf
2633 da1fa91d Kevin Wolf
    if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2634 da1fa91d Kevin Wolf
        bs->wr_highest_sector = sector_num + nb_sectors - 1;
2635 da1fa91d Kevin Wolf
    }
2636 da1fa91d Kevin Wolf
2637 dbffbdcf Stefan Hajnoczi
    tracked_request_end(&req);
2638 dbffbdcf Stefan Hajnoczi
2639 6b7cb247 Stefan Hajnoczi
    return ret;
2640 da1fa91d Kevin Wolf
}
2641 da1fa91d Kevin Wolf
2642 c5fbe571 Stefan Hajnoczi
int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
2643 c5fbe571 Stefan Hajnoczi
    int nb_sectors, QEMUIOVector *qiov)
2644 c5fbe571 Stefan Hajnoczi
{
2645 c5fbe571 Stefan Hajnoczi
    trace_bdrv_co_writev(bs, sector_num, nb_sectors);
2646 c5fbe571 Stefan Hajnoczi
2647 f08f2dda Stefan Hajnoczi
    return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0);
2648 f08f2dda Stefan Hajnoczi
}
2649 f08f2dda Stefan Hajnoczi
2650 f08f2dda Stefan Hajnoczi
int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
2651 f08f2dda Stefan Hajnoczi
                                      int64_t sector_num, int nb_sectors)
2652 f08f2dda Stefan Hajnoczi
{
2653 f08f2dda Stefan Hajnoczi
    trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
2654 f08f2dda Stefan Hajnoczi
2655 f08f2dda Stefan Hajnoczi
    return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
2656 f08f2dda Stefan Hajnoczi
                             BDRV_REQ_ZERO_WRITE);
2657 c5fbe571 Stefan Hajnoczi
}
2658 c5fbe571 Stefan Hajnoczi
2659 83f64091 bellard
/**
2660 83f64091 bellard
 * Truncate file to 'offset' bytes (needed only for file protocols)
2661 83f64091 bellard
 */
2662 83f64091 bellard
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2663 83f64091 bellard
{
2664 83f64091 bellard
    BlockDriver *drv = bs->drv;
2665 51762288 Stefan Hajnoczi
    int ret;
2666 83f64091 bellard
    if (!drv)
2667 19cb3738 bellard
        return -ENOMEDIUM;
2668 83f64091 bellard
    if (!drv->bdrv_truncate)
2669 83f64091 bellard
        return -ENOTSUP;
2670 59f2689d Naphtali Sprei
    if (bs->read_only)
2671 59f2689d Naphtali Sprei
        return -EACCES;
2672 8591675f Marcelo Tosatti
    if (bdrv_in_use(bs))
2673 8591675f Marcelo Tosatti
        return -EBUSY;
2674 51762288 Stefan Hajnoczi
    ret = drv->bdrv_truncate(bs, offset);
2675 51762288 Stefan Hajnoczi
    if (ret == 0) {
2676 51762288 Stefan Hajnoczi
        ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
2677 145feb17 Markus Armbruster
        bdrv_dev_resize_cb(bs);
2678 51762288 Stefan Hajnoczi
    }
2679 51762288 Stefan Hajnoczi
    return ret;
2680 83f64091 bellard
}
2681 83f64091 bellard
2682 83f64091 bellard
/**
2683 4a1d5e1f Fam Zheng
 * Length of a allocated file in bytes. Sparse files are counted by actual
2684 4a1d5e1f Fam Zheng
 * allocated space. Return < 0 if error or unknown.
2685 4a1d5e1f Fam Zheng
 */
2686 4a1d5e1f Fam Zheng
int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2687 4a1d5e1f Fam Zheng
{
2688 4a1d5e1f Fam Zheng
    BlockDriver *drv = bs->drv;
2689 4a1d5e1f Fam Zheng
    if (!drv) {
2690 4a1d5e1f Fam Zheng
        return -ENOMEDIUM;
2691 4a1d5e1f Fam Zheng
    }
2692 4a1d5e1f Fam Zheng
    if (drv->bdrv_get_allocated_file_size) {
2693 4a1d5e1f Fam Zheng
        return drv->bdrv_get_allocated_file_size(bs);
2694 4a1d5e1f Fam Zheng
    }
2695 4a1d5e1f Fam Zheng
    if (bs->file) {
2696 4a1d5e1f Fam Zheng
        return bdrv_get_allocated_file_size(bs->file);
2697 4a1d5e1f Fam Zheng
    }
2698 4a1d5e1f Fam Zheng
    return -ENOTSUP;
2699 4a1d5e1f Fam Zheng
}
2700 4a1d5e1f Fam Zheng
2701 4a1d5e1f Fam Zheng
/**
2702 83f64091 bellard
 * Length of a file in bytes. Return < 0 if error or unknown.
2703 83f64091 bellard
 */
2704 83f64091 bellard
int64_t bdrv_getlength(BlockDriverState *bs)
2705 83f64091 bellard
{
2706 83f64091 bellard
    BlockDriver *drv = bs->drv;
2707 83f64091 bellard
    if (!drv)
2708 19cb3738 bellard
        return -ENOMEDIUM;
2709 51762288 Stefan Hajnoczi
2710 2c6942fa Markus Armbruster
    if (bs->growable || bdrv_dev_has_removable_media(bs)) {
2711 46a4e4e6 Stefan Hajnoczi
        if (drv->bdrv_getlength) {
2712 46a4e4e6 Stefan Hajnoczi
            return drv->bdrv_getlength(bs);
2713 46a4e4e6 Stefan Hajnoczi
        }
2714 83f64091 bellard
    }
2715 46a4e4e6 Stefan Hajnoczi
    return bs->total_sectors * BDRV_SECTOR_SIZE;
2716 fc01f7e7 bellard
}
2717 fc01f7e7 bellard
2718 19cb3738 bellard
/* return 0 as number of sectors if no device present or error */
2719 96b8f136 ths
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
2720 fc01f7e7 bellard
{
2721 19cb3738 bellard
    int64_t length;
2722 19cb3738 bellard
    length = bdrv_getlength(bs);
2723 19cb3738 bellard
    if (length < 0)
2724 19cb3738 bellard
        length = 0;
2725 19cb3738 bellard
    else
2726 6ea44308 Jan Kiszka
        length = length >> BDRV_SECTOR_BITS;
2727 19cb3738 bellard
    *nb_sectors_ptr = length;
2728 fc01f7e7 bellard
}
2729 cf98951b bellard
2730 0563e191 Zhi Yong Wu
/* throttling disk io limits */
2731 0563e191 Zhi Yong Wu
void bdrv_set_io_limits(BlockDriverState *bs,
2732 0563e191 Zhi Yong Wu
                        BlockIOLimit *io_limits)
2733 0563e191 Zhi Yong Wu
{
2734 0563e191 Zhi Yong Wu
    bs->io_limits = *io_limits;
2735 0563e191 Zhi Yong Wu
    bs->io_limits_enabled = bdrv_io_limits_enabled(bs);
2736 0563e191 Zhi Yong Wu
}
2737 0563e191 Zhi Yong Wu
2738 ff06f5f3 Paolo Bonzini
void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
2739 ff06f5f3 Paolo Bonzini
                       BlockdevOnError on_write_error)
2740 abd7f68d Markus Armbruster
{
2741 abd7f68d Markus Armbruster
    bs->on_read_error = on_read_error;
2742 abd7f68d Markus Armbruster
    bs->on_write_error = on_write_error;
2743 abd7f68d Markus Armbruster
}
2744 abd7f68d Markus Armbruster
2745 1ceee0d5 Paolo Bonzini
BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read)
2746 abd7f68d Markus Armbruster
{
2747 abd7f68d Markus Armbruster
    return is_read ? bs->on_read_error : bs->on_write_error;
2748 abd7f68d Markus Armbruster
}
2749 abd7f68d Markus Armbruster
2750 3e1caa5f Paolo Bonzini
BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error)
2751 3e1caa5f Paolo Bonzini
{
2752 3e1caa5f Paolo Bonzini
    BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error;
2753 3e1caa5f Paolo Bonzini
2754 3e1caa5f Paolo Bonzini
    switch (on_err) {
2755 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_ENOSPC:
2756 3e1caa5f Paolo Bonzini
        return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT;
2757 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_STOP:
2758 3e1caa5f Paolo Bonzini
        return BDRV_ACTION_STOP;
2759 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_REPORT:
2760 3e1caa5f Paolo Bonzini
        return BDRV_ACTION_REPORT;
2761 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_IGNORE:
2762 3e1caa5f Paolo Bonzini
        return BDRV_ACTION_IGNORE;
2763 3e1caa5f Paolo Bonzini
    default:
2764 3e1caa5f Paolo Bonzini
        abort();
2765 3e1caa5f Paolo Bonzini
    }
2766 3e1caa5f Paolo Bonzini
}
2767 3e1caa5f Paolo Bonzini
2768 3e1caa5f Paolo Bonzini
/* This is done by device models because, while the block layer knows
2769 3e1caa5f Paolo Bonzini
 * about the error, it does not know whether an operation comes from
2770 3e1caa5f Paolo Bonzini
 * the device or the block layer (from a job, for example).
2771 3e1caa5f Paolo Bonzini
 */
2772 3e1caa5f Paolo Bonzini
void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
2773 3e1caa5f Paolo Bonzini
                       bool is_read, int error)
2774 3e1caa5f Paolo Bonzini
{
2775 3e1caa5f Paolo Bonzini
    assert(error >= 0);
2776 32c81a4a Paolo Bonzini
    bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read);
2777 3e1caa5f Paolo Bonzini
    if (action == BDRV_ACTION_STOP) {
2778 3e1caa5f Paolo Bonzini
        vm_stop(RUN_STATE_IO_ERROR);
2779 3e1caa5f Paolo Bonzini
        bdrv_iostatus_set_err(bs, error);
2780 3e1caa5f Paolo Bonzini
    }
2781 3e1caa5f Paolo Bonzini
}
2782 3e1caa5f Paolo Bonzini
2783 b338082b bellard
int bdrv_is_read_only(BlockDriverState *bs)
2784 b338082b bellard
{
2785 b338082b bellard
    return bs->read_only;
2786 b338082b bellard
}
2787 b338082b bellard
2788 985a03b0 ths
int bdrv_is_sg(BlockDriverState *bs)
2789 985a03b0 ths
{
2790 985a03b0 ths
    return bs->sg;
2791 985a03b0 ths
}
2792 985a03b0 ths
2793 e900a7b7 Christoph Hellwig
int bdrv_enable_write_cache(BlockDriverState *bs)
2794 e900a7b7 Christoph Hellwig
{
2795 e900a7b7 Christoph Hellwig
    return bs->enable_write_cache;
2796 e900a7b7 Christoph Hellwig
}
2797 e900a7b7 Christoph Hellwig
2798 425b0148 Paolo Bonzini
void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
2799 425b0148 Paolo Bonzini
{
2800 425b0148 Paolo Bonzini
    bs->enable_write_cache = wce;
2801 55b110f2 Jeff Cody
2802 55b110f2 Jeff Cody
    /* so a reopen() will preserve wce */
2803 55b110f2 Jeff Cody
    if (wce) {
2804 55b110f2 Jeff Cody
        bs->open_flags |= BDRV_O_CACHE_WB;
2805 55b110f2 Jeff Cody
    } else {
2806 55b110f2 Jeff Cody
        bs->open_flags &= ~BDRV_O_CACHE_WB;
2807 55b110f2 Jeff Cody
    }
2808 425b0148 Paolo Bonzini
}
2809 425b0148 Paolo Bonzini
2810 ea2384d3 bellard
int bdrv_is_encrypted(BlockDriverState *bs)
2811 ea2384d3 bellard
{
2812 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted)
2813 ea2384d3 bellard
        return 1;
2814 ea2384d3 bellard
    return bs->encrypted;
2815 ea2384d3 bellard
}
2816 ea2384d3 bellard
2817 c0f4ce77 aliguori
int bdrv_key_required(BlockDriverState *bs)
2818 c0f4ce77 aliguori
{
2819 c0f4ce77 aliguori
    BlockDriverState *backing_hd = bs->backing_hd;
2820 c0f4ce77 aliguori
2821 c0f4ce77 aliguori
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
2822 c0f4ce77 aliguori
        return 1;
2823 c0f4ce77 aliguori
    return (bs->encrypted && !bs->valid_key);
2824 c0f4ce77 aliguori
}
2825 c0f4ce77 aliguori
2826 ea2384d3 bellard
int bdrv_set_key(BlockDriverState *bs, const char *key)
2827 ea2384d3 bellard
{
2828 ea2384d3 bellard
    int ret;
2829 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted) {
2830 ea2384d3 bellard
        ret = bdrv_set_key(bs->backing_hd, key);
2831 ea2384d3 bellard
        if (ret < 0)
2832 ea2384d3 bellard
            return ret;
2833 ea2384d3 bellard
        if (!bs->encrypted)
2834 ea2384d3 bellard
            return 0;
2835 ea2384d3 bellard
    }
2836 fd04a2ae Shahar Havivi
    if (!bs->encrypted) {
2837 fd04a2ae Shahar Havivi
        return -EINVAL;
2838 fd04a2ae Shahar Havivi
    } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2839 fd04a2ae Shahar Havivi
        return -ENOMEDIUM;
2840 fd04a2ae Shahar Havivi
    }
2841 c0f4ce77 aliguori
    ret = bs->drv->bdrv_set_key(bs, key);
2842 bb5fc20f aliguori
    if (ret < 0) {
2843 bb5fc20f aliguori
        bs->valid_key = 0;
2844 bb5fc20f aliguori
    } else if (!bs->valid_key) {
2845 bb5fc20f aliguori
        bs->valid_key = 1;
2846 bb5fc20f aliguori
        /* call the change callback now, we skipped it on open */
2847 7d4b4ba5 Markus Armbruster
        bdrv_dev_change_media_cb(bs, true);
2848 bb5fc20f aliguori
    }
2849 c0f4ce77 aliguori
    return ret;
2850 ea2384d3 bellard
}
2851 ea2384d3 bellard
2852 f8d6bba1 Markus Armbruster
const char *bdrv_get_format_name(BlockDriverState *bs)
2853 ea2384d3 bellard
{
2854 f8d6bba1 Markus Armbruster
    return bs->drv ? bs->drv->format_name : NULL;
2855 ea2384d3 bellard
}
2856 ea2384d3 bellard
2857 5fafdf24 ths
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
2858 ea2384d3 bellard
                         void *opaque)
2859 ea2384d3 bellard
{
2860 ea2384d3 bellard
    BlockDriver *drv;
2861 ea2384d3 bellard
2862 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv, &bdrv_drivers, list) {
2863 ea2384d3 bellard
        it(opaque, drv->format_name);
2864 ea2384d3 bellard
    }
2865 ea2384d3 bellard
}
2866 ea2384d3 bellard
2867 b338082b bellard
BlockDriverState *bdrv_find(const char *name)
2868 b338082b bellard
{
2869 b338082b bellard
    BlockDriverState *bs;
2870 b338082b bellard
2871 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2872 1b7bdbc1 Stefan Hajnoczi
        if (!strcmp(name, bs->device_name)) {
2873 b338082b bellard
            return bs;
2874 1b7bdbc1 Stefan Hajnoczi
        }
2875 b338082b bellard
    }
2876 b338082b bellard
    return NULL;
2877 b338082b bellard
}
2878 b338082b bellard
2879 2f399b0a Markus Armbruster
BlockDriverState *bdrv_next(BlockDriverState *bs)
2880 2f399b0a Markus Armbruster
{
2881 2f399b0a Markus Armbruster
    if (!bs) {
2882 2f399b0a Markus Armbruster
        return QTAILQ_FIRST(&bdrv_states);
2883 2f399b0a Markus Armbruster
    }
2884 2f399b0a Markus Armbruster
    return QTAILQ_NEXT(bs, list);
2885 2f399b0a Markus Armbruster
}
2886 2f399b0a Markus Armbruster
2887 51de9760 aliguori
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
2888 81d0912d bellard
{
2889 81d0912d bellard
    BlockDriverState *bs;
2890 81d0912d bellard
2891 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2892 51de9760 aliguori
        it(opaque, bs);
2893 81d0912d bellard
    }
2894 81d0912d bellard
}
2895 81d0912d bellard
2896 ea2384d3 bellard
const char *bdrv_get_device_name(BlockDriverState *bs)
2897 ea2384d3 bellard
{
2898 ea2384d3 bellard
    return bs->device_name;
2899 ea2384d3 bellard
}
2900 ea2384d3 bellard
2901 c8433287 Markus Armbruster
int bdrv_get_flags(BlockDriverState *bs)
2902 c8433287 Markus Armbruster
{
2903 c8433287 Markus Armbruster
    return bs->open_flags;
2904 c8433287 Markus Armbruster
}
2905 c8433287 Markus Armbruster
2906 c6ca28d6 aliguori
void bdrv_flush_all(void)
2907 c6ca28d6 aliguori
{
2908 c6ca28d6 aliguori
    BlockDriverState *bs;
2909 c6ca28d6 aliguori
2910 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2911 29cdb251 Paolo Bonzini
        bdrv_flush(bs);
2912 1b7bdbc1 Stefan Hajnoczi
    }
2913 c6ca28d6 aliguori
}
2914 c6ca28d6 aliguori
2915 3ac21627 Peter Lieven
int bdrv_has_zero_init_1(BlockDriverState *bs)
2916 3ac21627 Peter Lieven
{
2917 3ac21627 Peter Lieven
    return 1;
2918 3ac21627 Peter Lieven
}
2919 3ac21627 Peter Lieven
2920 f2feebbd Kevin Wolf
int bdrv_has_zero_init(BlockDriverState *bs)
2921 f2feebbd Kevin Wolf
{
2922 f2feebbd Kevin Wolf
    assert(bs->drv);
2923 f2feebbd Kevin Wolf
2924 336c1c12 Kevin Wolf
    if (bs->drv->bdrv_has_zero_init) {
2925 336c1c12 Kevin Wolf
        return bs->drv->bdrv_has_zero_init(bs);
2926 f2feebbd Kevin Wolf
    }
2927 f2feebbd Kevin Wolf
2928 3ac21627 Peter Lieven
    /* safe default */
2929 3ac21627 Peter Lieven
    return 0;
2930 f2feebbd Kevin Wolf
}
2931 f2feebbd Kevin Wolf
2932 376ae3f1 Stefan Hajnoczi
typedef struct BdrvCoIsAllocatedData {
2933 376ae3f1 Stefan Hajnoczi
    BlockDriverState *bs;
2934 b35b2bba Miroslav Rezanina
    BlockDriverState *base;
2935 376ae3f1 Stefan Hajnoczi
    int64_t sector_num;
2936 376ae3f1 Stefan Hajnoczi
    int nb_sectors;
2937 376ae3f1 Stefan Hajnoczi
    int *pnum;
2938 376ae3f1 Stefan Hajnoczi
    int ret;
2939 376ae3f1 Stefan Hajnoczi
    bool done;
2940 376ae3f1 Stefan Hajnoczi
} BdrvCoIsAllocatedData;
2941 376ae3f1 Stefan Hajnoczi
2942 f58c7b35 ths
/*
2943 f58c7b35 ths
 * Returns true iff the specified sector is present in the disk image. Drivers
2944 f58c7b35 ths
 * not implementing the functionality are assumed to not support backing files,
2945 f58c7b35 ths
 * hence all their sectors are reported as allocated.
2946 f58c7b35 ths
 *
2947 bd9533e3 Stefan Hajnoczi
 * If 'sector_num' is beyond the end of the disk image the return value is 0
2948 bd9533e3 Stefan Hajnoczi
 * and 'pnum' is set to 0.
2949 bd9533e3 Stefan Hajnoczi
 *
2950 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
2951 f58c7b35 ths
 * the specified sector) that are known to be in the same
2952 f58c7b35 ths
 * allocated/unallocated state.
2953 f58c7b35 ths
 *
2954 bd9533e3 Stefan Hajnoczi
 * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
2955 bd9533e3 Stefan Hajnoczi
 * beyond the end of the disk image it will be clamped.
2956 f58c7b35 ths
 */
2957 060f51c9 Stefan Hajnoczi
int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
2958 060f51c9 Stefan Hajnoczi
                                      int nb_sectors, int *pnum)
2959 f58c7b35 ths
{
2960 bd9533e3 Stefan Hajnoczi
    int64_t n;
2961 bd9533e3 Stefan Hajnoczi
2962 bd9533e3 Stefan Hajnoczi
    if (sector_num >= bs->total_sectors) {
2963 bd9533e3 Stefan Hajnoczi
        *pnum = 0;
2964 bd9533e3 Stefan Hajnoczi
        return 0;
2965 bd9533e3 Stefan Hajnoczi
    }
2966 bd9533e3 Stefan Hajnoczi
2967 bd9533e3 Stefan Hajnoczi
    n = bs->total_sectors - sector_num;
2968 bd9533e3 Stefan Hajnoczi
    if (n < nb_sectors) {
2969 bd9533e3 Stefan Hajnoczi
        nb_sectors = n;
2970 bd9533e3 Stefan Hajnoczi
    }
2971 bd9533e3 Stefan Hajnoczi
2972 6aebab14 Stefan Hajnoczi
    if (!bs->drv->bdrv_co_is_allocated) {
2973 bd9533e3 Stefan Hajnoczi
        *pnum = nb_sectors;
2974 f58c7b35 ths
        return 1;
2975 f58c7b35 ths
    }
2976 6aebab14 Stefan Hajnoczi
2977 060f51c9 Stefan Hajnoczi
    return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum);
2978 060f51c9 Stefan Hajnoczi
}
2979 060f51c9 Stefan Hajnoczi
2980 060f51c9 Stefan Hajnoczi
/* Coroutine wrapper for bdrv_is_allocated() */
2981 060f51c9 Stefan Hajnoczi
static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque)
2982 060f51c9 Stefan Hajnoczi
{
2983 060f51c9 Stefan Hajnoczi
    BdrvCoIsAllocatedData *data = opaque;
2984 060f51c9 Stefan Hajnoczi
    BlockDriverState *bs = data->bs;
2985 060f51c9 Stefan Hajnoczi
2986 060f51c9 Stefan Hajnoczi
    data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors,
2987 060f51c9 Stefan Hajnoczi
                                     data->pnum);
2988 060f51c9 Stefan Hajnoczi
    data->done = true;
2989 060f51c9 Stefan Hajnoczi
}
2990 060f51c9 Stefan Hajnoczi
2991 060f51c9 Stefan Hajnoczi
/*
2992 060f51c9 Stefan Hajnoczi
 * Synchronous wrapper around bdrv_co_is_allocated().
2993 060f51c9 Stefan Hajnoczi
 *
2994 060f51c9 Stefan Hajnoczi
 * See bdrv_co_is_allocated() for details.
2995 060f51c9 Stefan Hajnoczi
 */
2996 060f51c9 Stefan Hajnoczi
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
2997 060f51c9 Stefan Hajnoczi
                      int *pnum)
2998 060f51c9 Stefan Hajnoczi
{
2999 6aebab14 Stefan Hajnoczi
    Coroutine *co;
3000 6aebab14 Stefan Hajnoczi
    BdrvCoIsAllocatedData data = {
3001 6aebab14 Stefan Hajnoczi
        .bs = bs,
3002 6aebab14 Stefan Hajnoczi
        .sector_num = sector_num,
3003 6aebab14 Stefan Hajnoczi
        .nb_sectors = nb_sectors,
3004 6aebab14 Stefan Hajnoczi
        .pnum = pnum,
3005 6aebab14 Stefan Hajnoczi
        .done = false,
3006 6aebab14 Stefan Hajnoczi
    };
3007 6aebab14 Stefan Hajnoczi
3008 6aebab14 Stefan Hajnoczi
    co = qemu_coroutine_create(bdrv_is_allocated_co_entry);
3009 6aebab14 Stefan Hajnoczi
    qemu_coroutine_enter(co, &data);
3010 6aebab14 Stefan Hajnoczi
    while (!data.done) {
3011 6aebab14 Stefan Hajnoczi
        qemu_aio_wait();
3012 6aebab14 Stefan Hajnoczi
    }
3013 6aebab14 Stefan Hajnoczi
    return data.ret;
3014 f58c7b35 ths
}
3015 f58c7b35 ths
3016 188a7bbf Paolo Bonzini
/*
3017 188a7bbf Paolo Bonzini
 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
3018 188a7bbf Paolo Bonzini
 *
3019 188a7bbf Paolo Bonzini
 * Return true if the given sector is allocated in any image between
3020 188a7bbf Paolo Bonzini
 * BASE and TOP (inclusive).  BASE can be NULL to check if the given
3021 188a7bbf Paolo Bonzini
 * sector is allocated in any image of the chain.  Return false otherwise.
3022 188a7bbf Paolo Bonzini
 *
3023 188a7bbf Paolo Bonzini
 * 'pnum' is set to the number of sectors (including and immediately following
3024 188a7bbf Paolo Bonzini
 *  the specified sector) that are known to be in the same
3025 188a7bbf Paolo Bonzini
 *  allocated/unallocated state.
3026 188a7bbf Paolo Bonzini
 *
3027 188a7bbf Paolo Bonzini
 */
3028 188a7bbf Paolo Bonzini
int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
3029 188a7bbf Paolo Bonzini
                                            BlockDriverState *base,
3030 188a7bbf Paolo Bonzini
                                            int64_t sector_num,
3031 188a7bbf Paolo Bonzini
                                            int nb_sectors, int *pnum)
3032 188a7bbf Paolo Bonzini
{
3033 188a7bbf Paolo Bonzini
    BlockDriverState *intermediate;
3034 188a7bbf Paolo Bonzini
    int ret, n = nb_sectors;
3035 188a7bbf Paolo Bonzini
3036 188a7bbf Paolo Bonzini
    intermediate = top;
3037 188a7bbf Paolo Bonzini
    while (intermediate && intermediate != base) {
3038 188a7bbf Paolo Bonzini
        int pnum_inter;
3039 188a7bbf Paolo Bonzini
        ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors,
3040 188a7bbf Paolo Bonzini
                                   &pnum_inter);
3041 188a7bbf Paolo Bonzini
        if (ret < 0) {
3042 188a7bbf Paolo Bonzini
            return ret;
3043 188a7bbf Paolo Bonzini
        } else if (ret) {
3044 188a7bbf Paolo Bonzini
            *pnum = pnum_inter;
3045 188a7bbf Paolo Bonzini
            return 1;
3046 188a7bbf Paolo Bonzini
        }
3047 188a7bbf Paolo Bonzini
3048 188a7bbf Paolo Bonzini
        /*
3049 188a7bbf Paolo Bonzini
         * [sector_num, nb_sectors] is unallocated on top but intermediate
3050 188a7bbf Paolo Bonzini
         * might have
3051 188a7bbf Paolo Bonzini
         *
3052 188a7bbf Paolo Bonzini
         * [sector_num+x, nr_sectors] allocated.
3053 188a7bbf Paolo Bonzini
         */
3054 63ba17d3 Vishvananda Ishaya
        if (n > pnum_inter &&
3055 63ba17d3 Vishvananda Ishaya
            (intermediate == top ||
3056 63ba17d3 Vishvananda Ishaya
             sector_num + pnum_inter < intermediate->total_sectors)) {
3057 188a7bbf Paolo Bonzini
            n = pnum_inter;
3058 188a7bbf Paolo Bonzini
        }
3059 188a7bbf Paolo Bonzini
3060 188a7bbf Paolo Bonzini
        intermediate = intermediate->backing_hd;
3061 188a7bbf Paolo Bonzini
    }
3062 188a7bbf Paolo Bonzini
3063 188a7bbf Paolo Bonzini
    *pnum = n;
3064 188a7bbf Paolo Bonzini
    return 0;
3065 188a7bbf Paolo Bonzini
}
3066 188a7bbf Paolo Bonzini
3067 b35b2bba Miroslav Rezanina
/* Coroutine wrapper for bdrv_is_allocated_above() */
3068 b35b2bba Miroslav Rezanina
static void coroutine_fn bdrv_is_allocated_above_co_entry(void *opaque)
3069 b35b2bba Miroslav Rezanina
{
3070 b35b2bba Miroslav Rezanina
    BdrvCoIsAllocatedData *data = opaque;
3071 b35b2bba Miroslav Rezanina
    BlockDriverState *top = data->bs;
3072 b35b2bba Miroslav Rezanina
    BlockDriverState *base = data->base;
3073 b35b2bba Miroslav Rezanina
3074 b35b2bba Miroslav Rezanina
    data->ret = bdrv_co_is_allocated_above(top, base, data->sector_num,
3075 b35b2bba Miroslav Rezanina
                                           data->nb_sectors, data->pnum);
3076 b35b2bba Miroslav Rezanina
    data->done = true;
3077 b35b2bba Miroslav Rezanina
}
3078 b35b2bba Miroslav Rezanina
3079 b35b2bba Miroslav Rezanina
/*
3080 b35b2bba Miroslav Rezanina
 * Synchronous wrapper around bdrv_co_is_allocated_above().
3081 b35b2bba Miroslav Rezanina
 *
3082 b35b2bba Miroslav Rezanina
 * See bdrv_co_is_allocated_above() for details.
3083 b35b2bba Miroslav Rezanina
 */
3084 b35b2bba Miroslav Rezanina
int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
3085 b35b2bba Miroslav Rezanina
                            int64_t sector_num, int nb_sectors, int *pnum)
3086 b35b2bba Miroslav Rezanina
{
3087 b35b2bba Miroslav Rezanina
    Coroutine *co;
3088 b35b2bba Miroslav Rezanina
    BdrvCoIsAllocatedData data = {
3089 b35b2bba Miroslav Rezanina
        .bs = top,
3090 b35b2bba Miroslav Rezanina
        .base = base,
3091 b35b2bba Miroslav Rezanina
        .sector_num = sector_num,
3092 b35b2bba Miroslav Rezanina
        .nb_sectors = nb_sectors,
3093 b35b2bba Miroslav Rezanina
        .pnum = pnum,
3094 b35b2bba Miroslav Rezanina
        .done = false,
3095 b35b2bba Miroslav Rezanina
    };
3096 b35b2bba Miroslav Rezanina
3097 b35b2bba Miroslav Rezanina
    co = qemu_coroutine_create(bdrv_is_allocated_above_co_entry);
3098 b35b2bba Miroslav Rezanina
    qemu_coroutine_enter(co, &data);
3099 b35b2bba Miroslav Rezanina
    while (!data.done) {
3100 b35b2bba Miroslav Rezanina
        qemu_aio_wait();
3101 b35b2bba Miroslav Rezanina
    }
3102 b35b2bba Miroslav Rezanina
    return data.ret;
3103 b35b2bba Miroslav Rezanina
}
3104 b35b2bba Miroslav Rezanina
3105 045df330 aliguori
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3106 045df330 aliguori
{
3107 045df330 aliguori
    if (bs->backing_hd && bs->backing_hd->encrypted)
3108 045df330 aliguori
        return bs->backing_file;
3109 045df330 aliguori
    else if (bs->encrypted)
3110 045df330 aliguori
        return bs->filename;
3111 045df330 aliguori
    else
3112 045df330 aliguori
        return NULL;
3113 045df330 aliguori
}
3114 045df330 aliguori
3115 5fafdf24 ths
void bdrv_get_backing_filename(BlockDriverState *bs,
3116 83f64091 bellard
                               char *filename, int filename_size)
3117 83f64091 bellard
{
3118 3574c608 Kevin Wolf
    pstrcpy(filename, filename_size, bs->backing_file);
3119 83f64091 bellard
}
3120 83f64091 bellard
3121 5fafdf24 ths
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
3122 faea38e7 bellard
                          const uint8_t *buf, int nb_sectors)
3123 faea38e7 bellard
{
3124 faea38e7 bellard
    BlockDriver *drv = bs->drv;
3125 faea38e7 bellard
    if (!drv)
3126 19cb3738 bellard
        return -ENOMEDIUM;
3127 faea38e7 bellard
    if (!drv->bdrv_write_compressed)
3128 faea38e7 bellard
        return -ENOTSUP;
3129 fbb7b4e0 Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors))
3130 fbb7b4e0 Kevin Wolf
        return -EIO;
3131 a55eb92c Jan Kiszka
3132 1755da16 Paolo Bonzini
    assert(!bs->dirty_bitmap);
3133 a55eb92c Jan Kiszka
3134 faea38e7 bellard
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
3135 faea38e7 bellard
}
3136 3b46e624 ths
3137 faea38e7 bellard
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3138 faea38e7 bellard
{
3139 faea38e7 bellard
    BlockDriver *drv = bs->drv;
3140 faea38e7 bellard
    if (!drv)
3141 19cb3738 bellard
        return -ENOMEDIUM;
3142 faea38e7 bellard
    if (!drv->bdrv_get_info)
3143 faea38e7 bellard
        return -ENOTSUP;
3144 faea38e7 bellard
    memset(bdi, 0, sizeof(*bdi));
3145 faea38e7 bellard
    return drv->bdrv_get_info(bs, bdi);
3146 faea38e7 bellard
}
3147 faea38e7 bellard
3148 45566e9c Christoph Hellwig
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
3149 45566e9c Christoph Hellwig
                      int64_t pos, int size)
3150 178e08a5 aliguori
{
3151 cf8074b3 Kevin Wolf
    QEMUIOVector qiov;
3152 cf8074b3 Kevin Wolf
    struct iovec iov = {
3153 cf8074b3 Kevin Wolf
        .iov_base   = (void *) buf,
3154 cf8074b3 Kevin Wolf
        .iov_len    = size,
3155 cf8074b3 Kevin Wolf
    };
3156 cf8074b3 Kevin Wolf
3157 cf8074b3 Kevin Wolf
    qemu_iovec_init_external(&qiov, &iov, 1);
3158 cf8074b3 Kevin Wolf
    return bdrv_writev_vmstate(bs, &qiov, pos);
3159 cf8074b3 Kevin Wolf
}
3160 cf8074b3 Kevin Wolf
3161 cf8074b3 Kevin Wolf
int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
3162 cf8074b3 Kevin Wolf
{
3163 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
3164 cf8074b3 Kevin Wolf
3165 cf8074b3 Kevin Wolf
    if (!drv) {
3166 178e08a5 aliguori
        return -ENOMEDIUM;
3167 cf8074b3 Kevin Wolf
    } else if (drv->bdrv_save_vmstate) {
3168 cf8074b3 Kevin Wolf
        return drv->bdrv_save_vmstate(bs, qiov, pos);
3169 cf8074b3 Kevin Wolf
    } else if (bs->file) {
3170 cf8074b3 Kevin Wolf
        return bdrv_writev_vmstate(bs->file, qiov, pos);
3171 cf8074b3 Kevin Wolf
    }
3172 cf8074b3 Kevin Wolf
3173 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
3174 178e08a5 aliguori
}
3175 178e08a5 aliguori
3176 45566e9c Christoph Hellwig
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
3177 45566e9c Christoph Hellwig
                      int64_t pos, int size)
3178 178e08a5 aliguori
{
3179 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
3180 178e08a5 aliguori
    if (!drv)
3181 178e08a5 aliguori
        return -ENOMEDIUM;
3182 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_load_vmstate)
3183 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_load_vmstate(bs, buf, pos, size);
3184 7cdb1f6d MORITA Kazutaka
    if (bs->file)
3185 7cdb1f6d MORITA Kazutaka
        return bdrv_load_vmstate(bs->file, buf, pos, size);
3186 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
3187 178e08a5 aliguori
}
3188 178e08a5 aliguori
3189 8b9b0cc2 Kevin Wolf
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
3190 8b9b0cc2 Kevin Wolf
{
3191 bf736fe3 Kevin Wolf
    if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
3192 8b9b0cc2 Kevin Wolf
        return;
3193 8b9b0cc2 Kevin Wolf
    }
3194 8b9b0cc2 Kevin Wolf
3195 bf736fe3 Kevin Wolf
    bs->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 bf736fe3 Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS);
4028 eb489bb1 Kevin Wolf
    if (bs->drv->bdrv_co_flush_to_os) {
4029 eb489bb1 Kevin Wolf
        ret = bs->drv->bdrv_co_flush_to_os(bs);
4030 eb489bb1 Kevin Wolf
        if (ret < 0) {
4031 eb489bb1 Kevin Wolf
            return ret;
4032 eb489bb1 Kevin Wolf
        }
4033 eb489bb1 Kevin Wolf
    }
4034 eb489bb1 Kevin Wolf
4035 ca716364 Kevin Wolf
    /* But don't actually force it to the disk with cache=unsafe */
4036 ca716364 Kevin Wolf
    if (bs->open_flags & BDRV_O_NO_FLUSH) {
4037 d4c82329 Kevin Wolf
        goto flush_parent;
4038 ca716364 Kevin Wolf
    }
4039 ca716364 Kevin Wolf
4040 bf736fe3 Kevin Wolf
    BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK);
4041 eb489bb1 Kevin Wolf
    if (bs->drv->bdrv_co_flush_to_disk) {
4042 29cdb251 Paolo Bonzini
        ret = bs->drv->bdrv_co_flush_to_disk(bs);
4043 07f07615 Paolo Bonzini
    } else if (bs->drv->bdrv_aio_flush) {
4044 07f07615 Paolo Bonzini
        BlockDriverAIOCB *acb;
4045 07f07615 Paolo Bonzini
        CoroutineIOCompletion co = {
4046 07f07615 Paolo Bonzini
            .coroutine = qemu_coroutine_self(),
4047 07f07615 Paolo Bonzini
        };
4048 07f07615 Paolo Bonzini
4049 07f07615 Paolo Bonzini
        acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
4050 07f07615 Paolo Bonzini
        if (acb == NULL) {
4051 29cdb251 Paolo Bonzini
            ret = -EIO;
4052 07f07615 Paolo Bonzini
        } else {
4053 07f07615 Paolo Bonzini
            qemu_coroutine_yield();
4054 29cdb251 Paolo Bonzini
            ret = co.ret;
4055 07f07615 Paolo Bonzini
        }
4056 07f07615 Paolo Bonzini
    } else {
4057 07f07615 Paolo Bonzini
        /*
4058 07f07615 Paolo Bonzini
         * Some block drivers always operate in either writethrough or unsafe
4059 07f07615 Paolo Bonzini
         * mode and don't support bdrv_flush therefore. Usually qemu doesn't
4060 07f07615 Paolo Bonzini
         * know how the server works (because the behaviour is hardcoded or
4061 07f07615 Paolo Bonzini
         * depends on server-side configuration), so we can't ensure that
4062 07f07615 Paolo Bonzini
         * everything is safe on disk. Returning an error doesn't work because
4063 07f07615 Paolo Bonzini
         * that would break guests even if the server operates in writethrough
4064 07f07615 Paolo Bonzini
         * mode.
4065 07f07615 Paolo Bonzini
         *
4066 07f07615 Paolo Bonzini
         * Let's hope the user knows what he's doing.
4067 07f07615 Paolo Bonzini
         */
4068 29cdb251 Paolo Bonzini
        ret = 0;
4069 07f07615 Paolo Bonzini
    }
4070 29cdb251 Paolo Bonzini
    if (ret < 0) {
4071 29cdb251 Paolo Bonzini
        return ret;
4072 29cdb251 Paolo Bonzini
    }
4073 29cdb251 Paolo Bonzini
4074 29cdb251 Paolo Bonzini
    /* Now flush the underlying protocol.  It will also have BDRV_O_NO_FLUSH
4075 29cdb251 Paolo Bonzini
     * in the case of cache=unsafe, so there are no useless flushes.
4076 29cdb251 Paolo Bonzini
     */
4077 d4c82329 Kevin Wolf
flush_parent:
4078 29cdb251 Paolo Bonzini
    return bdrv_co_flush(bs->file);
4079 07f07615 Paolo Bonzini
}
4080 07f07615 Paolo Bonzini
4081 0f15423c Anthony Liguori
void bdrv_invalidate_cache(BlockDriverState *bs)
4082 0f15423c Anthony Liguori
{
4083 0f15423c Anthony Liguori
    if (bs->drv && bs->drv->bdrv_invalidate_cache) {
4084 0f15423c Anthony Liguori
        bs->drv->bdrv_invalidate_cache(bs);
4085 0f15423c Anthony Liguori
    }
4086 0f15423c Anthony Liguori
}
4087 0f15423c Anthony Liguori
4088 0f15423c Anthony Liguori
void bdrv_invalidate_cache_all(void)
4089 0f15423c Anthony Liguori
{
4090 0f15423c Anthony Liguori
    BlockDriverState *bs;
4091 0f15423c Anthony Liguori
4092 0f15423c Anthony Liguori
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
4093 0f15423c Anthony Liguori
        bdrv_invalidate_cache(bs);
4094 0f15423c Anthony Liguori
    }
4095 0f15423c Anthony Liguori
}
4096 0f15423c Anthony Liguori
4097 07789269 Benoรฎt Canet
void bdrv_clear_incoming_migration_all(void)
4098 07789269 Benoรฎt Canet
{
4099 07789269 Benoรฎt Canet
    BlockDriverState *bs;
4100 07789269 Benoรฎt Canet
4101 07789269 Benoรฎt Canet
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
4102 07789269 Benoรฎt Canet
        bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING);
4103 07789269 Benoรฎt Canet
    }
4104 07789269 Benoรฎt Canet
}
4105 07789269 Benoรฎt Canet
4106 07f07615 Paolo Bonzini
int bdrv_flush(BlockDriverState *bs)
4107 07f07615 Paolo Bonzini
{
4108 07f07615 Paolo Bonzini
    Coroutine *co;
4109 07f07615 Paolo Bonzini
    RwCo rwco = {
4110 07f07615 Paolo Bonzini
        .bs = bs,
4111 07f07615 Paolo Bonzini
        .ret = NOT_DONE,
4112 e7a8a783 Kevin Wolf
    };
4113 e7a8a783 Kevin Wolf
4114 07f07615 Paolo Bonzini
    if (qemu_in_coroutine()) {
4115 07f07615 Paolo Bonzini
        /* Fast-path if already in coroutine context */
4116 07f07615 Paolo Bonzini
        bdrv_flush_co_entry(&rwco);
4117 07f07615 Paolo Bonzini
    } else {
4118 07f07615 Paolo Bonzini
        co = qemu_coroutine_create(bdrv_flush_co_entry);
4119 07f07615 Paolo Bonzini
        qemu_coroutine_enter(co, &rwco);
4120 07f07615 Paolo Bonzini
        while (rwco.ret == NOT_DONE) {
4121 07f07615 Paolo Bonzini
            qemu_aio_wait();
4122 07f07615 Paolo Bonzini
        }
4123 e7a8a783 Kevin Wolf
    }
4124 07f07615 Paolo Bonzini
4125 07f07615 Paolo Bonzini
    return rwco.ret;
4126 e7a8a783 Kevin Wolf
}
4127 e7a8a783 Kevin Wolf
4128 4265d620 Paolo Bonzini
static void coroutine_fn bdrv_discard_co_entry(void *opaque)
4129 4265d620 Paolo Bonzini
{
4130 4265d620 Paolo Bonzini
    RwCo *rwco = opaque;
4131 4265d620 Paolo Bonzini
4132 4265d620 Paolo Bonzini
    rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
4133 4265d620 Paolo Bonzini
}
4134 4265d620 Paolo Bonzini
4135 4265d620 Paolo Bonzini
int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
4136 4265d620 Paolo Bonzini
                                 int nb_sectors)
4137 4265d620 Paolo Bonzini
{
4138 4265d620 Paolo Bonzini
    if (!bs->drv) {
4139 4265d620 Paolo Bonzini
        return -ENOMEDIUM;
4140 4265d620 Paolo Bonzini
    } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
4141 4265d620 Paolo Bonzini
        return -EIO;
4142 4265d620 Paolo Bonzini
    } else if (bs->read_only) {
4143 4265d620 Paolo Bonzini
        return -EROFS;
4144 df702c9b Paolo Bonzini
    }
4145 df702c9b Paolo Bonzini
4146 df702c9b Paolo Bonzini
    if (bs->dirty_bitmap) {
4147 8f0720ec Paolo Bonzini
        bdrv_reset_dirty(bs, sector_num, nb_sectors);
4148 df702c9b Paolo Bonzini
    }
4149 df702c9b Paolo Bonzini
4150 9e8f1835 Paolo Bonzini
    /* Do nothing if disabled.  */
4151 9e8f1835 Paolo Bonzini
    if (!(bs->open_flags & BDRV_O_UNMAP)) {
4152 9e8f1835 Paolo Bonzini
        return 0;
4153 9e8f1835 Paolo Bonzini
    }
4154 9e8f1835 Paolo Bonzini
4155 df702c9b Paolo Bonzini
    if (bs->drv->bdrv_co_discard) {
4156 4265d620 Paolo Bonzini
        return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
4157 4265d620 Paolo Bonzini
    } else if (bs->drv->bdrv_aio_discard) {
4158 4265d620 Paolo Bonzini
        BlockDriverAIOCB *acb;
4159 4265d620 Paolo Bonzini
        CoroutineIOCompletion co = {
4160 4265d620 Paolo Bonzini
            .coroutine = qemu_coroutine_self(),
4161 4265d620 Paolo Bonzini
        };
4162 4265d620 Paolo Bonzini
4163 4265d620 Paolo Bonzini
        acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
4164 4265d620 Paolo Bonzini
                                        bdrv_co_io_em_complete, &co);
4165 4265d620 Paolo Bonzini
        if (acb == NULL) {
4166 4265d620 Paolo Bonzini
            return -EIO;
4167 4265d620 Paolo Bonzini
        } else {
4168 4265d620 Paolo Bonzini
            qemu_coroutine_yield();
4169 4265d620 Paolo Bonzini
            return co.ret;
4170 4265d620 Paolo Bonzini
        }
4171 4265d620 Paolo Bonzini
    } else {
4172 4265d620 Paolo Bonzini
        return 0;
4173 4265d620 Paolo Bonzini
    }
4174 4265d620 Paolo Bonzini
}
4175 4265d620 Paolo Bonzini
4176 4265d620 Paolo Bonzini
int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
4177 4265d620 Paolo Bonzini
{
4178 4265d620 Paolo Bonzini
    Coroutine *co;
4179 4265d620 Paolo Bonzini
    RwCo rwco = {
4180 4265d620 Paolo Bonzini
        .bs = bs,
4181 4265d620 Paolo Bonzini
        .sector_num = sector_num,
4182 4265d620 Paolo Bonzini
        .nb_sectors = nb_sectors,
4183 4265d620 Paolo Bonzini
        .ret = NOT_DONE,
4184 4265d620 Paolo Bonzini
    };
4185 4265d620 Paolo Bonzini
4186 4265d620 Paolo Bonzini
    if (qemu_in_coroutine()) {
4187 4265d620 Paolo Bonzini
        /* Fast-path if already in coroutine context */
4188 4265d620 Paolo Bonzini
        bdrv_discard_co_entry(&rwco);
4189 4265d620 Paolo Bonzini
    } else {
4190 4265d620 Paolo Bonzini
        co = qemu_coroutine_create(bdrv_discard_co_entry);
4191 4265d620 Paolo Bonzini
        qemu_coroutine_enter(co, &rwco);
4192 4265d620 Paolo Bonzini
        while (rwco.ret == NOT_DONE) {
4193 4265d620 Paolo Bonzini
            qemu_aio_wait();
4194 4265d620 Paolo Bonzini
        }
4195 4265d620 Paolo Bonzini
    }
4196 4265d620 Paolo Bonzini
4197 4265d620 Paolo Bonzini
    return rwco.ret;
4198 4265d620 Paolo Bonzini
}
4199 4265d620 Paolo Bonzini
4200 f9f05dc5 Kevin Wolf
/**************************************************************/
4201 19cb3738 bellard
/* removable device support */
4202 19cb3738 bellard
4203 19cb3738 bellard
/**
4204 19cb3738 bellard
 * Return TRUE if the media is present
4205 19cb3738 bellard
 */
4206 19cb3738 bellard
int bdrv_is_inserted(BlockDriverState *bs)
4207 19cb3738 bellard
{
4208 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4209 a1aff5bf Markus Armbruster
4210 19cb3738 bellard
    if (!drv)
4211 19cb3738 bellard
        return 0;
4212 19cb3738 bellard
    if (!drv->bdrv_is_inserted)
4213 a1aff5bf Markus Armbruster
        return 1;
4214 a1aff5bf Markus Armbruster
    return drv->bdrv_is_inserted(bs);
4215 19cb3738 bellard
}
4216 19cb3738 bellard
4217 19cb3738 bellard
/**
4218 8e49ca46 Markus Armbruster
 * Return whether the media changed since the last call to this
4219 8e49ca46 Markus Armbruster
 * function, or -ENOTSUP if we don't know.  Most drivers don't know.
4220 19cb3738 bellard
 */
4221 19cb3738 bellard
int bdrv_media_changed(BlockDriverState *bs)
4222 19cb3738 bellard
{
4223 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4224 19cb3738 bellard
4225 8e49ca46 Markus Armbruster
    if (drv && drv->bdrv_media_changed) {
4226 8e49ca46 Markus Armbruster
        return drv->bdrv_media_changed(bs);
4227 8e49ca46 Markus Armbruster
    }
4228 8e49ca46 Markus Armbruster
    return -ENOTSUP;
4229 19cb3738 bellard
}
4230 19cb3738 bellard
4231 19cb3738 bellard
/**
4232 19cb3738 bellard
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
4233 19cb3738 bellard
 */
4234 f36f3949 Luiz Capitulino
void bdrv_eject(BlockDriverState *bs, bool eject_flag)
4235 19cb3738 bellard
{
4236 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4237 19cb3738 bellard
4238 822e1cd1 Markus Armbruster
    if (drv && drv->bdrv_eject) {
4239 822e1cd1 Markus Armbruster
        drv->bdrv_eject(bs, eject_flag);
4240 19cb3738 bellard
    }
4241 6f382ed2 Luiz Capitulino
4242 6f382ed2 Luiz Capitulino
    if (bs->device_name[0] != '\0') {
4243 6f382ed2 Luiz Capitulino
        bdrv_emit_qmp_eject_event(bs, eject_flag);
4244 6f382ed2 Luiz Capitulino
    }
4245 19cb3738 bellard
}
4246 19cb3738 bellard
4247 19cb3738 bellard
/**
4248 19cb3738 bellard
 * Lock or unlock the media (if it is locked, the user won't be able
4249 19cb3738 bellard
 * to eject it manually).
4250 19cb3738 bellard
 */
4251 025e849a Markus Armbruster
void bdrv_lock_medium(BlockDriverState *bs, bool locked)
4252 19cb3738 bellard
{
4253 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4254 19cb3738 bellard
4255 025e849a Markus Armbruster
    trace_bdrv_lock_medium(bs, locked);
4256 b8c6d095 Stefan Hajnoczi
4257 025e849a Markus Armbruster
    if (drv && drv->bdrv_lock_medium) {
4258 025e849a Markus Armbruster
        drv->bdrv_lock_medium(bs, locked);
4259 19cb3738 bellard
    }
4260 19cb3738 bellard
}
4261 985a03b0 ths
4262 985a03b0 ths
/* needed for generic scsi interface */
4263 985a03b0 ths
4264 985a03b0 ths
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
4265 985a03b0 ths
{
4266 985a03b0 ths
    BlockDriver *drv = bs->drv;
4267 985a03b0 ths
4268 985a03b0 ths
    if (drv && drv->bdrv_ioctl)
4269 985a03b0 ths
        return drv->bdrv_ioctl(bs, req, buf);
4270 985a03b0 ths
    return -ENOTSUP;
4271 985a03b0 ths
}
4272 7d780669 aliguori
4273 221f715d aliguori
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
4274 221f715d aliguori
        unsigned long int req, void *buf,
4275 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
4276 7d780669 aliguori
{
4277 221f715d aliguori
    BlockDriver *drv = bs->drv;
4278 7d780669 aliguori
4279 221f715d aliguori
    if (drv && drv->bdrv_aio_ioctl)
4280 221f715d aliguori
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
4281 221f715d aliguori
    return NULL;
4282 7d780669 aliguori
}
4283 e268ca52 aliguori
4284 7b6f9300 Markus Armbruster
void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
4285 7b6f9300 Markus Armbruster
{
4286 7b6f9300 Markus Armbruster
    bs->buffer_alignment = align;
4287 7b6f9300 Markus Armbruster
}
4288 7cd1e32a lirans@il.ibm.com
4289 e268ca52 aliguori
void *qemu_blockalign(BlockDriverState *bs, size_t size)
4290 e268ca52 aliguori
{
4291 e268ca52 aliguori
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
4292 e268ca52 aliguori
}
4293 7cd1e32a lirans@il.ibm.com
4294 c53b1c51 Stefan Hajnoczi
/*
4295 c53b1c51 Stefan Hajnoczi
 * Check if all memory in this vector is sector aligned.
4296 c53b1c51 Stefan Hajnoczi
 */
4297 c53b1c51 Stefan Hajnoczi
bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
4298 c53b1c51 Stefan Hajnoczi
{
4299 c53b1c51 Stefan Hajnoczi
    int i;
4300 c53b1c51 Stefan Hajnoczi
4301 c53b1c51 Stefan Hajnoczi
    for (i = 0; i < qiov->niov; i++) {
4302 c53b1c51 Stefan Hajnoczi
        if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
4303 c53b1c51 Stefan Hajnoczi
            return false;
4304 c53b1c51 Stefan Hajnoczi
        }
4305 c53b1c51 Stefan Hajnoczi
    }
4306 c53b1c51 Stefan Hajnoczi
4307 c53b1c51 Stefan Hajnoczi
    return true;
4308 c53b1c51 Stefan Hajnoczi
}
4309 c53b1c51 Stefan Hajnoczi
4310 50717e94 Paolo Bonzini
void bdrv_set_dirty_tracking(BlockDriverState *bs, int granularity)
4311 7cd1e32a lirans@il.ibm.com
{
4312 7cd1e32a lirans@il.ibm.com
    int64_t bitmap_size;
4313 a55eb92c Jan Kiszka
4314 50717e94 Paolo Bonzini
    assert((granularity & (granularity - 1)) == 0);
4315 50717e94 Paolo Bonzini
4316 50717e94 Paolo Bonzini
    if (granularity) {
4317 50717e94 Paolo Bonzini
        granularity >>= BDRV_SECTOR_BITS;
4318 50717e94 Paolo Bonzini
        assert(!bs->dirty_bitmap);
4319 50717e94 Paolo Bonzini
        bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS);
4320 50717e94 Paolo Bonzini
        bs->dirty_bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1);
4321 7cd1e32a lirans@il.ibm.com
    } else {
4322 c6d22830 Jan Kiszka
        if (bs->dirty_bitmap) {
4323 8f0720ec Paolo Bonzini
            hbitmap_free(bs->dirty_bitmap);
4324 c6d22830 Jan Kiszka
            bs->dirty_bitmap = NULL;
4325 a55eb92c Jan Kiszka
        }
4326 7cd1e32a lirans@il.ibm.com
    }
4327 7cd1e32a lirans@il.ibm.com
}
4328 7cd1e32a lirans@il.ibm.com
4329 7cd1e32a lirans@il.ibm.com
int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
4330 7cd1e32a lirans@il.ibm.com
{
4331 8f0720ec Paolo Bonzini
    if (bs->dirty_bitmap) {
4332 8f0720ec Paolo Bonzini
        return hbitmap_get(bs->dirty_bitmap, sector);
4333 7cd1e32a lirans@il.ibm.com
    } else {
4334 7cd1e32a lirans@il.ibm.com
        return 0;
4335 7cd1e32a lirans@il.ibm.com
    }
4336 7cd1e32a lirans@il.ibm.com
}
4337 7cd1e32a lirans@il.ibm.com
4338 8f0720ec Paolo Bonzini
void bdrv_dirty_iter_init(BlockDriverState *bs, HBitmapIter *hbi)
4339 1755da16 Paolo Bonzini
{
4340 8f0720ec Paolo Bonzini
    hbitmap_iter_init(hbi, bs->dirty_bitmap, 0);
4341 1755da16 Paolo Bonzini
}
4342 1755da16 Paolo Bonzini
4343 1755da16 Paolo Bonzini
void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
4344 1755da16 Paolo Bonzini
                    int nr_sectors)
4345 1755da16 Paolo Bonzini
{
4346 8f0720ec Paolo Bonzini
    hbitmap_set(bs->dirty_bitmap, cur_sector, nr_sectors);
4347 1755da16 Paolo Bonzini
}
4348 1755da16 Paolo Bonzini
4349 a55eb92c Jan Kiszka
void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
4350 a55eb92c Jan Kiszka
                      int nr_sectors)
4351 7cd1e32a lirans@il.ibm.com
{
4352 8f0720ec Paolo Bonzini
    hbitmap_reset(bs->dirty_bitmap, cur_sector, nr_sectors);
4353 7cd1e32a lirans@il.ibm.com
}
4354 aaa0eb75 Liran Schour
4355 aaa0eb75 Liran Schour
int64_t bdrv_get_dirty_count(BlockDriverState *bs)
4356 aaa0eb75 Liran Schour
{
4357 8f0720ec Paolo Bonzini
    if (bs->dirty_bitmap) {
4358 acc906c6 Paolo Bonzini
        return hbitmap_count(bs->dirty_bitmap);
4359 8f0720ec Paolo Bonzini
    } else {
4360 8f0720ec Paolo Bonzini
        return 0;
4361 8f0720ec Paolo Bonzini
    }
4362 aaa0eb75 Liran Schour
}
4363 f88e1a42 Jes Sorensen
4364 db593f25 Marcelo Tosatti
void bdrv_set_in_use(BlockDriverState *bs, int in_use)
4365 db593f25 Marcelo Tosatti
{
4366 db593f25 Marcelo Tosatti
    assert(bs->in_use != in_use);
4367 db593f25 Marcelo Tosatti
    bs->in_use = in_use;
4368 db593f25 Marcelo Tosatti
}
4369 db593f25 Marcelo Tosatti
4370 db593f25 Marcelo Tosatti
int bdrv_in_use(BlockDriverState *bs)
4371 db593f25 Marcelo Tosatti
{
4372 db593f25 Marcelo Tosatti
    return bs->in_use;
4373 db593f25 Marcelo Tosatti
}
4374 db593f25 Marcelo Tosatti
4375 28a7282a Luiz Capitulino
void bdrv_iostatus_enable(BlockDriverState *bs)
4376 28a7282a Luiz Capitulino
{
4377 d6bf279e Luiz Capitulino
    bs->iostatus_enabled = true;
4378 58e21ef5 Luiz Capitulino
    bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
4379 28a7282a Luiz Capitulino
}
4380 28a7282a Luiz Capitulino
4381 28a7282a Luiz Capitulino
/* The I/O status is only enabled if the drive explicitly
4382 28a7282a Luiz Capitulino
 * enables it _and_ the VM is configured to stop on errors */
4383 28a7282a Luiz Capitulino
bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
4384 28a7282a Luiz Capitulino
{
4385 d6bf279e Luiz Capitulino
    return (bs->iostatus_enabled &&
4386 92aa5c6d Paolo Bonzini
           (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
4387 92aa5c6d Paolo Bonzini
            bs->on_write_error == BLOCKDEV_ON_ERROR_STOP   ||
4388 92aa5c6d Paolo Bonzini
            bs->on_read_error == BLOCKDEV_ON_ERROR_STOP));
4389 28a7282a Luiz Capitulino
}
4390 28a7282a Luiz Capitulino
4391 28a7282a Luiz Capitulino
void bdrv_iostatus_disable(BlockDriverState *bs)
4392 28a7282a Luiz Capitulino
{
4393 d6bf279e Luiz Capitulino
    bs->iostatus_enabled = false;
4394 28a7282a Luiz Capitulino
}
4395 28a7282a Luiz Capitulino
4396 28a7282a Luiz Capitulino
void bdrv_iostatus_reset(BlockDriverState *bs)
4397 28a7282a Luiz Capitulino
{
4398 28a7282a Luiz Capitulino
    if (bdrv_iostatus_is_enabled(bs)) {
4399 58e21ef5 Luiz Capitulino
        bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
4400 3bd293c3 Paolo Bonzini
        if (bs->job) {
4401 3bd293c3 Paolo Bonzini
            block_job_iostatus_reset(bs->job);
4402 3bd293c3 Paolo Bonzini
        }
4403 28a7282a Luiz Capitulino
    }
4404 28a7282a Luiz Capitulino
}
4405 28a7282a Luiz Capitulino
4406 28a7282a Luiz Capitulino
void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
4407 28a7282a Luiz Capitulino
{
4408 3e1caa5f Paolo Bonzini
    assert(bdrv_iostatus_is_enabled(bs));
4409 3e1caa5f Paolo Bonzini
    if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
4410 58e21ef5 Luiz Capitulino
        bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
4411 58e21ef5 Luiz Capitulino
                                         BLOCK_DEVICE_IO_STATUS_FAILED;
4412 28a7282a Luiz Capitulino
    }
4413 28a7282a Luiz Capitulino
}
4414 28a7282a Luiz Capitulino
4415 a597e79c Christoph Hellwig
void
4416 a597e79c Christoph Hellwig
bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
4417 a597e79c Christoph Hellwig
        enum BlockAcctType type)
4418 a597e79c Christoph Hellwig
{
4419 a597e79c Christoph Hellwig
    assert(type < BDRV_MAX_IOTYPE);
4420 a597e79c Christoph Hellwig
4421 a597e79c Christoph Hellwig
    cookie->bytes = bytes;
4422 c488c7f6 Christoph Hellwig
    cookie->start_time_ns = get_clock();
4423 a597e79c Christoph Hellwig
    cookie->type = type;
4424 a597e79c Christoph Hellwig
}
4425 a597e79c Christoph Hellwig
4426 a597e79c Christoph Hellwig
void
4427 a597e79c Christoph Hellwig
bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
4428 a597e79c Christoph Hellwig
{
4429 a597e79c Christoph Hellwig
    assert(cookie->type < BDRV_MAX_IOTYPE);
4430 a597e79c Christoph Hellwig
4431 a597e79c Christoph Hellwig
    bs->nr_bytes[cookie->type] += cookie->bytes;
4432 a597e79c Christoph Hellwig
    bs->nr_ops[cookie->type]++;
4433 c488c7f6 Christoph Hellwig
    bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
4434 a597e79c Christoph Hellwig
}
4435 a597e79c Christoph Hellwig
4436 d92ada22 Luiz Capitulino
void bdrv_img_create(const char *filename, const char *fmt,
4437 d92ada22 Luiz Capitulino
                     const char *base_filename, const char *base_fmt,
4438 f382d43a Miroslav Rezanina
                     char *options, uint64_t img_size, int flags,
4439 f382d43a Miroslav Rezanina
                     Error **errp, bool quiet)
4440 f88e1a42 Jes Sorensen
{
4441 f88e1a42 Jes Sorensen
    QEMUOptionParameter *param = NULL, *create_options = NULL;
4442 d220894e Kevin Wolf
    QEMUOptionParameter *backing_fmt, *backing_file, *size;
4443 f88e1a42 Jes Sorensen
    BlockDriverState *bs = NULL;
4444 f88e1a42 Jes Sorensen
    BlockDriver *drv, *proto_drv;
4445 96df67d1 Stefan Hajnoczi
    BlockDriver *backing_drv = NULL;
4446 f88e1a42 Jes Sorensen
    int ret = 0;
4447 f88e1a42 Jes Sorensen
4448 f88e1a42 Jes Sorensen
    /* Find driver and parse its options */
4449 f88e1a42 Jes Sorensen
    drv = bdrv_find_format(fmt);
4450 f88e1a42 Jes Sorensen
    if (!drv) {
4451 71c79813 Luiz Capitulino
        error_setg(errp, "Unknown file format '%s'", fmt);
4452 d92ada22 Luiz Capitulino
        return;
4453 f88e1a42 Jes Sorensen
    }
4454 f88e1a42 Jes Sorensen
4455 f88e1a42 Jes Sorensen
    proto_drv = bdrv_find_protocol(filename);
4456 f88e1a42 Jes Sorensen
    if (!proto_drv) {
4457 71c79813 Luiz Capitulino
        error_setg(errp, "Unknown protocol '%s'", filename);
4458 d92ada22 Luiz Capitulino
        return;
4459 f88e1a42 Jes Sorensen
    }
4460 f88e1a42 Jes Sorensen
4461 f88e1a42 Jes Sorensen
    create_options = append_option_parameters(create_options,
4462 f88e1a42 Jes Sorensen
                                              drv->create_options);
4463 f88e1a42 Jes Sorensen
    create_options = append_option_parameters(create_options,
4464 f88e1a42 Jes Sorensen
                                              proto_drv->create_options);
4465 f88e1a42 Jes Sorensen
4466 f88e1a42 Jes Sorensen
    /* Create parameter list with default values */
4467 f88e1a42 Jes Sorensen
    param = parse_option_parameters("", create_options, param);
4468 f88e1a42 Jes Sorensen
4469 f88e1a42 Jes Sorensen
    set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
4470 f88e1a42 Jes Sorensen
4471 f88e1a42 Jes Sorensen
    /* Parse -o options */
4472 f88e1a42 Jes Sorensen
    if (options) {
4473 f88e1a42 Jes Sorensen
        param = parse_option_parameters(options, create_options, param);
4474 f88e1a42 Jes Sorensen
        if (param == NULL) {
4475 71c79813 Luiz Capitulino
            error_setg(errp, "Invalid options for file format '%s'.", fmt);
4476 f88e1a42 Jes Sorensen
            goto out;
4477 f88e1a42 Jes Sorensen
        }
4478 f88e1a42 Jes Sorensen
    }
4479 f88e1a42 Jes Sorensen
4480 f88e1a42 Jes Sorensen
    if (base_filename) {
4481 f88e1a42 Jes Sorensen
        if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
4482 f88e1a42 Jes Sorensen
                                 base_filename)) {
4483 71c79813 Luiz Capitulino
            error_setg(errp, "Backing file not supported for file format '%s'",
4484 71c79813 Luiz Capitulino
                       fmt);
4485 f88e1a42 Jes Sorensen
            goto out;
4486 f88e1a42 Jes Sorensen
        }
4487 f88e1a42 Jes Sorensen
    }
4488 f88e1a42 Jes Sorensen
4489 f88e1a42 Jes Sorensen
    if (base_fmt) {
4490 f88e1a42 Jes Sorensen
        if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
4491 71c79813 Luiz Capitulino
            error_setg(errp, "Backing file format not supported for file "
4492 71c79813 Luiz Capitulino
                             "format '%s'", fmt);
4493 f88e1a42 Jes Sorensen
            goto out;
4494 f88e1a42 Jes Sorensen
        }
4495 f88e1a42 Jes Sorensen
    }
4496 f88e1a42 Jes Sorensen
4497 792da93a Jes Sorensen
    backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
4498 792da93a Jes Sorensen
    if (backing_file && backing_file->value.s) {
4499 792da93a Jes Sorensen
        if (!strcmp(filename, backing_file->value.s)) {
4500 71c79813 Luiz Capitulino
            error_setg(errp, "Error: Trying to create an image with the "
4501 71c79813 Luiz Capitulino
                             "same filename as the backing file");
4502 792da93a Jes Sorensen
            goto out;
4503 792da93a Jes Sorensen
        }
4504 792da93a Jes Sorensen
    }
4505 792da93a Jes Sorensen
4506 f88e1a42 Jes Sorensen
    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
4507 f88e1a42 Jes Sorensen
    if (backing_fmt && backing_fmt->value.s) {
4508 96df67d1 Stefan Hajnoczi
        backing_drv = bdrv_find_format(backing_fmt->value.s);
4509 96df67d1 Stefan Hajnoczi
        if (!backing_drv) {
4510 71c79813 Luiz Capitulino
            error_setg(errp, "Unknown backing file format '%s'",
4511 71c79813 Luiz Capitulino
                       backing_fmt->value.s);
4512 f88e1a42 Jes Sorensen
            goto out;
4513 f88e1a42 Jes Sorensen
        }
4514 f88e1a42 Jes Sorensen
    }
4515 f88e1a42 Jes Sorensen
4516 f88e1a42 Jes Sorensen
    // The size for the image must always be specified, with one exception:
4517 f88e1a42 Jes Sorensen
    // If we are using a backing file, we can obtain the size from there
4518 d220894e Kevin Wolf
    size = get_option_parameter(param, BLOCK_OPT_SIZE);
4519 d220894e Kevin Wolf
    if (size && size->value.n == -1) {
4520 f88e1a42 Jes Sorensen
        if (backing_file && backing_file->value.s) {
4521 f88e1a42 Jes Sorensen
            uint64_t size;
4522 f88e1a42 Jes Sorensen
            char buf[32];
4523 63090dac Paolo Bonzini
            int back_flags;
4524 63090dac Paolo Bonzini
4525 63090dac Paolo Bonzini
            /* backing files always opened read-only */
4526 63090dac Paolo Bonzini
            back_flags =
4527 63090dac Paolo Bonzini
                flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
4528 f88e1a42 Jes Sorensen
4529 f88e1a42 Jes Sorensen
            bs = bdrv_new("");
4530 f88e1a42 Jes Sorensen
4531 de9c0cec Kevin Wolf
            ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags,
4532 de9c0cec Kevin Wolf
                            backing_drv);
4533 f88e1a42 Jes Sorensen
            if (ret < 0) {
4534 71c79813 Luiz Capitulino
                error_setg_errno(errp, -ret, "Could not open '%s'",
4535 71c79813 Luiz Capitulino
                                 backing_file->value.s);
4536 f88e1a42 Jes Sorensen
                goto out;
4537 f88e1a42 Jes Sorensen
            }
4538 f88e1a42 Jes Sorensen
            bdrv_get_geometry(bs, &size);
4539 f88e1a42 Jes Sorensen
            size *= 512;
4540 f88e1a42 Jes Sorensen
4541 f88e1a42 Jes Sorensen
            snprintf(buf, sizeof(buf), "%" PRId64, size);
4542 f88e1a42 Jes Sorensen
            set_option_parameter(param, BLOCK_OPT_SIZE, buf);
4543 f88e1a42 Jes Sorensen
        } else {
4544 71c79813 Luiz Capitulino
            error_setg(errp, "Image creation needs a size parameter");
4545 f88e1a42 Jes Sorensen
            goto out;
4546 f88e1a42 Jes Sorensen
        }
4547 f88e1a42 Jes Sorensen
    }
4548 f88e1a42 Jes Sorensen
4549 f382d43a Miroslav Rezanina
    if (!quiet) {
4550 f382d43a Miroslav Rezanina
        printf("Formatting '%s', fmt=%s ", filename, fmt);
4551 f382d43a Miroslav Rezanina
        print_option_parameters(param);
4552 f382d43a Miroslav Rezanina
        puts("");
4553 f382d43a Miroslav Rezanina
    }
4554 f88e1a42 Jes Sorensen
    ret = bdrv_create(drv, filename, param);
4555 f88e1a42 Jes Sorensen
    if (ret < 0) {
4556 f88e1a42 Jes Sorensen
        if (ret == -ENOTSUP) {
4557 71c79813 Luiz Capitulino
            error_setg(errp,"Formatting or formatting option not supported for "
4558 71c79813 Luiz Capitulino
                            "file format '%s'", fmt);
4559 f88e1a42 Jes Sorensen
        } else if (ret == -EFBIG) {
4560 f3f4d2c0 Kevin Wolf
            const char *cluster_size_hint = "";
4561 f3f4d2c0 Kevin Wolf
            if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
4562 f3f4d2c0 Kevin Wolf
                cluster_size_hint = " (try using a larger cluster size)";
4563 f3f4d2c0 Kevin Wolf
            }
4564 f3f4d2c0 Kevin Wolf
            error_setg(errp, "The image size is too large for file format '%s'%s",
4565 f3f4d2c0 Kevin Wolf
                       fmt, cluster_size_hint);
4566 f88e1a42 Jes Sorensen
        } else {
4567 71c79813 Luiz Capitulino
            error_setg(errp, "%s: error while creating %s: %s", filename, fmt,
4568 71c79813 Luiz Capitulino
                       strerror(-ret));
4569 f88e1a42 Jes Sorensen
        }
4570 f88e1a42 Jes Sorensen
    }
4571 f88e1a42 Jes Sorensen
4572 f88e1a42 Jes Sorensen
out:
4573 f88e1a42 Jes Sorensen
    free_option_parameters(create_options);
4574 f88e1a42 Jes Sorensen
    free_option_parameters(param);
4575 f88e1a42 Jes Sorensen
4576 f88e1a42 Jes Sorensen
    if (bs) {
4577 f88e1a42 Jes Sorensen
        bdrv_delete(bs);
4578 f88e1a42 Jes Sorensen
    }
4579 f88e1a42 Jes Sorensen
}
4580 85d126f3 Stefan Hajnoczi
4581 85d126f3 Stefan Hajnoczi
AioContext *bdrv_get_aio_context(BlockDriverState *bs)
4582 85d126f3 Stefan Hajnoczi
{
4583 85d126f3 Stefan Hajnoczi
    /* Currently BlockDriverState always uses the main loop AioContext */
4584 85d126f3 Stefan Hajnoczi
    return qemu_get_aio_context();
4585 85d126f3 Stefan Hajnoczi
}
4586 d616b224 Stefan Hajnoczi
4587 d616b224 Stefan Hajnoczi
void bdrv_add_before_write_notifier(BlockDriverState *bs,
4588 d616b224 Stefan Hajnoczi
                                    NotifierWithReturn *notifier)
4589 d616b224 Stefan Hajnoczi
{
4590 d616b224 Stefan Hajnoczi
    notifier_with_return_list_add(&bs->before_write_notifiers, notifier);
4591 d616b224 Stefan Hajnoczi
}