root / block.c @ d616b224
History | View | Annotate | Download (125.6 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 | 80ccf93b | Liu Yuan | bdrv_flush(bs); |
1362 | 3cbc002c | Paolo Bonzini | if (bs->job) {
|
1363 | 3cbc002c | Paolo Bonzini | block_job_cancel_sync(bs->job); |
1364 | 3cbc002c | Paolo Bonzini | } |
1365 | 3cbc002c | Paolo Bonzini | bdrv_drain_all(); |
1366 | d7d512f6 | Paolo Bonzini | notifier_list_notify(&bs->close_notifiers, bs); |
1367 | 7094f12f | Kevin Wolf | |
1368 | 3cbc002c | Paolo Bonzini | if (bs->drv) {
|
1369 | 557df6ac | Stefan Hajnoczi | if (bs->backing_hd) {
|
1370 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
1371 | 557df6ac | Stefan Hajnoczi | bs->backing_hd = NULL;
|
1372 | 557df6ac | Stefan Hajnoczi | } |
1373 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
1374 | 7267c094 | Anthony Liguori | g_free(bs->opaque); |
1375 | ea2384d3 | bellard | #ifdef _WIN32
|
1376 | ea2384d3 | bellard | if (bs->is_temporary) {
|
1377 | ea2384d3 | bellard | unlink(bs->filename); |
1378 | ea2384d3 | bellard | } |
1379 | 67b915a5 | bellard | #endif
|
1380 | ea2384d3 | bellard | bs->opaque = NULL;
|
1381 | ea2384d3 | bellard | bs->drv = NULL;
|
1382 | 53fec9d3 | Stefan Hajnoczi | bs->copy_on_read = 0;
|
1383 | a275fa42 | Paolo Bonzini | bs->backing_file[0] = '\0'; |
1384 | a275fa42 | Paolo Bonzini | bs->backing_format[0] = '\0'; |
1385 | 6405875c | Paolo Bonzini | bs->total_sectors = 0;
|
1386 | 6405875c | Paolo Bonzini | bs->encrypted = 0;
|
1387 | 6405875c | Paolo Bonzini | bs->valid_key = 0;
|
1388 | 6405875c | Paolo Bonzini | bs->sg = 0;
|
1389 | 6405875c | Paolo Bonzini | bs->growable = 0;
|
1390 | de9c0cec | Kevin Wolf | QDECREF(bs->options); |
1391 | de9c0cec | Kevin Wolf | bs->options = NULL;
|
1392 | b338082b | bellard | |
1393 | 66f82cee | Kevin Wolf | if (bs->file != NULL) { |
1394 | 0ac9377d | Paolo Bonzini | bdrv_delete(bs->file); |
1395 | 0ac9377d | Paolo Bonzini | bs->file = NULL;
|
1396 | 66f82cee | Kevin Wolf | } |
1397 | b338082b | bellard | } |
1398 | 98f90dba | Zhi Yong Wu | |
1399 | 9ca11154 | Pavel Hrdina | bdrv_dev_change_media_cb(bs, false);
|
1400 | 9ca11154 | Pavel Hrdina | |
1401 | 98f90dba | Zhi Yong Wu | /*throttling disk I/O limits*/
|
1402 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
1403 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_disable(bs); |
1404 | 98f90dba | Zhi Yong Wu | } |
1405 | b338082b | bellard | } |
1406 | b338082b | bellard | |
1407 | 2bc93fed | MORITA Kazutaka | void bdrv_close_all(void) |
1408 | 2bc93fed | MORITA Kazutaka | { |
1409 | 2bc93fed | MORITA Kazutaka | BlockDriverState *bs; |
1410 | 2bc93fed | MORITA Kazutaka | |
1411 | 2bc93fed | MORITA Kazutaka | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1412 | 2bc93fed | MORITA Kazutaka | bdrv_close(bs); |
1413 | 2bc93fed | MORITA Kazutaka | } |
1414 | 2bc93fed | MORITA Kazutaka | } |
1415 | 2bc93fed | MORITA Kazutaka | |
1416 | 922453bc | Stefan Hajnoczi | /*
|
1417 | 922453bc | Stefan Hajnoczi | * Wait for pending requests to complete across all BlockDriverStates
|
1418 | 922453bc | Stefan Hajnoczi | *
|
1419 | 922453bc | Stefan Hajnoczi | * This function does not flush data to disk, use bdrv_flush_all() for that
|
1420 | 922453bc | Stefan Hajnoczi | * after calling this function.
|
1421 | 4c355d53 | Zhi Yong Wu | *
|
1422 | 4c355d53 | Zhi Yong Wu | * Note that completion of an asynchronous I/O operation can trigger any
|
1423 | 4c355d53 | Zhi Yong Wu | * number of other I/O operations on other devices---for example a coroutine
|
1424 | 4c355d53 | Zhi Yong Wu | * can be arbitrarily complex and a constant flow of I/O can come until the
|
1425 | 4c355d53 | Zhi Yong Wu | * coroutine is complete. Because of this, it is not possible to have a
|
1426 | 4c355d53 | Zhi Yong Wu | * function to drain a single device's I/O queue.
|
1427 | 922453bc | Stefan Hajnoczi | */
|
1428 | 922453bc | Stefan Hajnoczi | void bdrv_drain_all(void) |
1429 | 922453bc | Stefan Hajnoczi | { |
1430 | 922453bc | Stefan Hajnoczi | BlockDriverState *bs; |
1431 | 4c355d53 | Zhi Yong Wu | bool busy;
|
1432 | 4c355d53 | Zhi Yong Wu | |
1433 | 4c355d53 | Zhi Yong Wu | do {
|
1434 | 4c355d53 | Zhi Yong Wu | busy = qemu_aio_wait(); |
1435 | 922453bc | Stefan Hajnoczi | |
1436 | 4c355d53 | Zhi Yong Wu | /* FIXME: We do not have timer support here, so this is effectively
|
1437 | 4c355d53 | Zhi Yong Wu | * a busy wait.
|
1438 | 4c355d53 | Zhi Yong Wu | */
|
1439 | 4c355d53 | Zhi Yong Wu | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1440 | 4c355d53 | Zhi Yong Wu | if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
|
1441 | 4c355d53 | Zhi Yong Wu | qemu_co_queue_restart_all(&bs->throttled_reqs); |
1442 | 4c355d53 | Zhi Yong Wu | busy = true;
|
1443 | 4c355d53 | Zhi Yong Wu | } |
1444 | 4c355d53 | Zhi Yong Wu | } |
1445 | 4c355d53 | Zhi Yong Wu | } while (busy);
|
1446 | 922453bc | Stefan Hajnoczi | |
1447 | 922453bc | Stefan Hajnoczi | /* If requests are still pending there is a bug somewhere */
|
1448 | 922453bc | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1449 | 922453bc | Stefan Hajnoczi | assert(QLIST_EMPTY(&bs->tracked_requests)); |
1450 | 922453bc | Stefan Hajnoczi | assert(qemu_co_queue_empty(&bs->throttled_reqs)); |
1451 | 922453bc | Stefan Hajnoczi | } |
1452 | 922453bc | Stefan Hajnoczi | } |
1453 | 922453bc | Stefan Hajnoczi | |
1454 | d22b2f41 | Ryan Harper | /* make a BlockDriverState anonymous by removing from bdrv_state list.
|
1455 | d22b2f41 | Ryan Harper | Also, NULL terminate the device_name to prevent double remove */
|
1456 | d22b2f41 | Ryan Harper | void bdrv_make_anon(BlockDriverState *bs)
|
1457 | d22b2f41 | Ryan Harper | { |
1458 | d22b2f41 | Ryan Harper | if (bs->device_name[0] != '\0') { |
1459 | d22b2f41 | Ryan Harper | QTAILQ_REMOVE(&bdrv_states, bs, list); |
1460 | d22b2f41 | Ryan Harper | } |
1461 | d22b2f41 | Ryan Harper | bs->device_name[0] = '\0'; |
1462 | d22b2f41 | Ryan Harper | } |
1463 | d22b2f41 | Ryan Harper | |
1464 | e023b2e2 | Paolo Bonzini | static void bdrv_rebind(BlockDriverState *bs) |
1465 | e023b2e2 | Paolo Bonzini | { |
1466 | e023b2e2 | Paolo Bonzini | if (bs->drv && bs->drv->bdrv_rebind) {
|
1467 | e023b2e2 | Paolo Bonzini | bs->drv->bdrv_rebind(bs); |
1468 | e023b2e2 | Paolo Bonzini | } |
1469 | e023b2e2 | Paolo Bonzini | } |
1470 | e023b2e2 | Paolo Bonzini | |
1471 | 4ddc07ca | Paolo Bonzini | static void bdrv_move_feature_fields(BlockDriverState *bs_dest, |
1472 | 4ddc07ca | Paolo Bonzini | BlockDriverState *bs_src) |
1473 | 8802d1fd | Jeff Cody | { |
1474 | 4ddc07ca | Paolo Bonzini | /* move some fields that need to stay attached to the device */
|
1475 | 4ddc07ca | Paolo Bonzini | bs_dest->open_flags = bs_src->open_flags; |
1476 | 8802d1fd | Jeff Cody | |
1477 | 8802d1fd | Jeff Cody | /* dev info */
|
1478 | 4ddc07ca | Paolo Bonzini | bs_dest->dev_ops = bs_src->dev_ops; |
1479 | 4ddc07ca | Paolo Bonzini | bs_dest->dev_opaque = bs_src->dev_opaque; |
1480 | 4ddc07ca | Paolo Bonzini | bs_dest->dev = bs_src->dev; |
1481 | 4ddc07ca | Paolo Bonzini | bs_dest->buffer_alignment = bs_src->buffer_alignment; |
1482 | 4ddc07ca | Paolo Bonzini | bs_dest->copy_on_read = bs_src->copy_on_read; |
1483 | 8802d1fd | Jeff Cody | |
1484 | 4ddc07ca | Paolo Bonzini | bs_dest->enable_write_cache = bs_src->enable_write_cache; |
1485 | c4a248a1 | Paolo Bonzini | |
1486 | 8802d1fd | Jeff Cody | /* i/o timing parameters */
|
1487 | 4ddc07ca | Paolo Bonzini | bs_dest->slice_start = bs_src->slice_start; |
1488 | 4ddc07ca | Paolo Bonzini | bs_dest->slice_end = bs_src->slice_end; |
1489 | 5905fbc9 | Stefan Hajnoczi | bs_dest->slice_submitted = bs_src->slice_submitted; |
1490 | 4ddc07ca | Paolo Bonzini | bs_dest->io_limits = bs_src->io_limits; |
1491 | 4ddc07ca | Paolo Bonzini | bs_dest->throttled_reqs = bs_src->throttled_reqs; |
1492 | 4ddc07ca | Paolo Bonzini | bs_dest->block_timer = bs_src->block_timer; |
1493 | 4ddc07ca | Paolo Bonzini | bs_dest->io_limits_enabled = bs_src->io_limits_enabled; |
1494 | 8802d1fd | Jeff Cody | |
1495 | 8802d1fd | Jeff Cody | /* r/w error */
|
1496 | 4ddc07ca | Paolo Bonzini | bs_dest->on_read_error = bs_src->on_read_error; |
1497 | 4ddc07ca | Paolo Bonzini | bs_dest->on_write_error = bs_src->on_write_error; |
1498 | 8802d1fd | Jeff Cody | |
1499 | 8802d1fd | Jeff Cody | /* i/o status */
|
1500 | 4ddc07ca | Paolo Bonzini | bs_dest->iostatus_enabled = bs_src->iostatus_enabled; |
1501 | 4ddc07ca | Paolo Bonzini | bs_dest->iostatus = bs_src->iostatus; |
1502 | 8802d1fd | Jeff Cody | |
1503 | a9fc4408 | Paolo Bonzini | /* dirty bitmap */
|
1504 | 4ddc07ca | Paolo Bonzini | bs_dest->dirty_bitmap = bs_src->dirty_bitmap; |
1505 | a9fc4408 | Paolo Bonzini | |
1506 | a9fc4408 | Paolo Bonzini | /* job */
|
1507 | 4ddc07ca | Paolo Bonzini | bs_dest->in_use = bs_src->in_use; |
1508 | 4ddc07ca | Paolo Bonzini | bs_dest->job = bs_src->job; |
1509 | a9fc4408 | Paolo Bonzini | |
1510 | 8802d1fd | Jeff Cody | /* keep the same entry in bdrv_states */
|
1511 | 4ddc07ca | Paolo Bonzini | pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name),
|
1512 | 4ddc07ca | Paolo Bonzini | bs_src->device_name); |
1513 | 4ddc07ca | Paolo Bonzini | bs_dest->list = bs_src->list; |
1514 | 4ddc07ca | Paolo Bonzini | } |
1515 | 8802d1fd | Jeff Cody | |
1516 | 4ddc07ca | Paolo Bonzini | /*
|
1517 | 4ddc07ca | Paolo Bonzini | * Swap bs contents for two image chains while they are live,
|
1518 | 4ddc07ca | Paolo Bonzini | * while keeping required fields on the BlockDriverState that is
|
1519 | 4ddc07ca | Paolo Bonzini | * actually attached to a device.
|
1520 | 4ddc07ca | Paolo Bonzini | *
|
1521 | 4ddc07ca | Paolo Bonzini | * This will modify the BlockDriverState fields, and swap contents
|
1522 | 4ddc07ca | Paolo Bonzini | * between bs_new and bs_old. Both bs_new and bs_old are modified.
|
1523 | 4ddc07ca | Paolo Bonzini | *
|
1524 | 4ddc07ca | Paolo Bonzini | * bs_new is required to be anonymous.
|
1525 | 4ddc07ca | Paolo Bonzini | *
|
1526 | 4ddc07ca | Paolo Bonzini | * This function does not create any image files.
|
1527 | 4ddc07ca | Paolo Bonzini | */
|
1528 | 4ddc07ca | Paolo Bonzini | void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
|
1529 | 4ddc07ca | Paolo Bonzini | { |
1530 | 4ddc07ca | Paolo Bonzini | BlockDriverState tmp; |
1531 | f6801b83 | Jeff Cody | |
1532 | 4ddc07ca | Paolo Bonzini | /* bs_new must be anonymous and shouldn't have anything fancy enabled */
|
1533 | 4ddc07ca | Paolo Bonzini | assert(bs_new->device_name[0] == '\0'); |
1534 | 4ddc07ca | Paolo Bonzini | assert(bs_new->dirty_bitmap == NULL);
|
1535 | 4ddc07ca | Paolo Bonzini | assert(bs_new->job == NULL);
|
1536 | 4ddc07ca | Paolo Bonzini | assert(bs_new->dev == NULL);
|
1537 | 4ddc07ca | Paolo Bonzini | assert(bs_new->in_use == 0);
|
1538 | 4ddc07ca | Paolo Bonzini | assert(bs_new->io_limits_enabled == false);
|
1539 | 4ddc07ca | Paolo Bonzini | assert(bs_new->block_timer == NULL);
|
1540 | 8802d1fd | Jeff Cody | |
1541 | 4ddc07ca | Paolo Bonzini | tmp = *bs_new; |
1542 | 4ddc07ca | Paolo Bonzini | *bs_new = *bs_old; |
1543 | 4ddc07ca | Paolo Bonzini | *bs_old = tmp; |
1544 | a9fc4408 | Paolo Bonzini | |
1545 | 4ddc07ca | Paolo Bonzini | /* there are some fields that should not be swapped, move them back */
|
1546 | 4ddc07ca | Paolo Bonzini | bdrv_move_feature_fields(&tmp, bs_old); |
1547 | 4ddc07ca | Paolo Bonzini | bdrv_move_feature_fields(bs_old, bs_new); |
1548 | 4ddc07ca | Paolo Bonzini | bdrv_move_feature_fields(bs_new, &tmp); |
1549 | 8802d1fd | Jeff Cody | |
1550 | 4ddc07ca | Paolo Bonzini | /* bs_new shouldn't be in bdrv_states even after the swap! */
|
1551 | 4ddc07ca | Paolo Bonzini | assert(bs_new->device_name[0] == '\0'); |
1552 | 4ddc07ca | Paolo Bonzini | |
1553 | 4ddc07ca | Paolo Bonzini | /* Check a few fields that should remain attached to the device */
|
1554 | 4ddc07ca | Paolo Bonzini | assert(bs_new->dev == NULL);
|
1555 | 4ddc07ca | Paolo Bonzini | assert(bs_new->job == NULL);
|
1556 | 4ddc07ca | Paolo Bonzini | assert(bs_new->in_use == 0);
|
1557 | 4ddc07ca | Paolo Bonzini | assert(bs_new->io_limits_enabled == false);
|
1558 | 4ddc07ca | Paolo Bonzini | assert(bs_new->block_timer == NULL);
|
1559 | e023b2e2 | Paolo Bonzini | |
1560 | e023b2e2 | Paolo Bonzini | bdrv_rebind(bs_new); |
1561 | 4ddc07ca | Paolo Bonzini | bdrv_rebind(bs_old); |
1562 | 4ddc07ca | Paolo Bonzini | } |
1563 | 4ddc07ca | Paolo Bonzini | |
1564 | 4ddc07ca | Paolo Bonzini | /*
|
1565 | 4ddc07ca | Paolo Bonzini | * Add new bs contents at the top of an image chain while the chain is
|
1566 | 4ddc07ca | Paolo Bonzini | * live, while keeping required fields on the top layer.
|
1567 | 4ddc07ca | Paolo Bonzini | *
|
1568 | 4ddc07ca | Paolo Bonzini | * This will modify the BlockDriverState fields, and swap contents
|
1569 | 4ddc07ca | Paolo Bonzini | * between bs_new and bs_top. Both bs_new and bs_top are modified.
|
1570 | 4ddc07ca | Paolo Bonzini | *
|
1571 | 4ddc07ca | Paolo Bonzini | * bs_new is required to be anonymous.
|
1572 | 4ddc07ca | Paolo Bonzini | *
|
1573 | 4ddc07ca | Paolo Bonzini | * This function does not create any image files.
|
1574 | 4ddc07ca | Paolo Bonzini | */
|
1575 | 4ddc07ca | Paolo Bonzini | void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
|
1576 | 4ddc07ca | Paolo Bonzini | { |
1577 | 4ddc07ca | Paolo Bonzini | bdrv_swap(bs_new, bs_top); |
1578 | 4ddc07ca | Paolo Bonzini | |
1579 | 4ddc07ca | Paolo Bonzini | /* The contents of 'tmp' will become bs_top, as we are
|
1580 | 4ddc07ca | Paolo Bonzini | * swapping bs_new and bs_top contents. */
|
1581 | 4ddc07ca | Paolo Bonzini | bs_top->backing_hd = bs_new; |
1582 | 4ddc07ca | Paolo Bonzini | bs_top->open_flags &= ~BDRV_O_NO_BACKING; |
1583 | 4ddc07ca | Paolo Bonzini | pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file),
|
1584 | 4ddc07ca | Paolo Bonzini | bs_new->filename); |
1585 | 4ddc07ca | Paolo Bonzini | pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format),
|
1586 | 4ddc07ca | Paolo Bonzini | bs_new->drv ? bs_new->drv->format_name : "");
|
1587 | 8802d1fd | Jeff Cody | } |
1588 | 8802d1fd | Jeff Cody | |
1589 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
1590 | b338082b | bellard | { |
1591 | fa879d62 | Markus Armbruster | assert(!bs->dev); |
1592 | 3e914655 | Paolo Bonzini | assert(!bs->job); |
1593 | 3e914655 | Paolo Bonzini | assert(!bs->in_use); |
1594 | 18846dee | Markus Armbruster | |
1595 | 1b7bdbc1 | Stefan Hajnoczi | /* remove from list, if necessary */
|
1596 | d22b2f41 | Ryan Harper | bdrv_make_anon(bs); |
1597 | 34c6f050 | aurel32 | |
1598 | b338082b | bellard | bdrv_close(bs); |
1599 | 66f82cee | Kevin Wolf | |
1600 | 7267c094 | Anthony Liguori | g_free(bs); |
1601 | fc01f7e7 | bellard | } |
1602 | fc01f7e7 | bellard | |
1603 | fa879d62 | Markus Armbruster | int bdrv_attach_dev(BlockDriverState *bs, void *dev) |
1604 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
1605 | 18846dee | Markus Armbruster | { |
1606 | fa879d62 | Markus Armbruster | if (bs->dev) {
|
1607 | 18846dee | Markus Armbruster | return -EBUSY;
|
1608 | 18846dee | Markus Armbruster | } |
1609 | fa879d62 | Markus Armbruster | bs->dev = dev; |
1610 | 28a7282a | Luiz Capitulino | bdrv_iostatus_reset(bs); |
1611 | 18846dee | Markus Armbruster | return 0; |
1612 | 18846dee | Markus Armbruster | } |
1613 | 18846dee | Markus Armbruster | |
1614 | fa879d62 | Markus Armbruster | /* TODO qdevified devices don't use this, remove when devices are qdevified */
|
1615 | fa879d62 | Markus Armbruster | void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) |
1616 | 18846dee | Markus Armbruster | { |
1617 | fa879d62 | Markus Armbruster | if (bdrv_attach_dev(bs, dev) < 0) { |
1618 | fa879d62 | Markus Armbruster | abort(); |
1619 | fa879d62 | Markus Armbruster | } |
1620 | fa879d62 | Markus Armbruster | } |
1621 | fa879d62 | Markus Armbruster | |
1622 | fa879d62 | Markus Armbruster | void bdrv_detach_dev(BlockDriverState *bs, void *dev) |
1623 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
1624 | fa879d62 | Markus Armbruster | { |
1625 | fa879d62 | Markus Armbruster | assert(bs->dev == dev); |
1626 | fa879d62 | Markus Armbruster | bs->dev = NULL;
|
1627 | 0e49de52 | Markus Armbruster | bs->dev_ops = NULL;
|
1628 | 0e49de52 | Markus Armbruster | bs->dev_opaque = NULL;
|
1629 | 29e05f20 | Markus Armbruster | bs->buffer_alignment = 512;
|
1630 | 18846dee | Markus Armbruster | } |
1631 | 18846dee | Markus Armbruster | |
1632 | fa879d62 | Markus Armbruster | /* TODO change to return DeviceState * when all users are qdevified */
|
1633 | fa879d62 | Markus Armbruster | void *bdrv_get_attached_dev(BlockDriverState *bs)
|
1634 | 18846dee | Markus Armbruster | { |
1635 | fa879d62 | Markus Armbruster | return bs->dev;
|
1636 | 18846dee | Markus Armbruster | } |
1637 | 18846dee | Markus Armbruster | |
1638 | 0e49de52 | Markus Armbruster | void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, |
1639 | 0e49de52 | Markus Armbruster | void *opaque)
|
1640 | 0e49de52 | Markus Armbruster | { |
1641 | 0e49de52 | Markus Armbruster | bs->dev_ops = ops; |
1642 | 0e49de52 | Markus Armbruster | bs->dev_opaque = opaque; |
1643 | 0e49de52 | Markus Armbruster | } |
1644 | 0e49de52 | Markus Armbruster | |
1645 | 32c81a4a | Paolo Bonzini | void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, |
1646 | 32c81a4a | Paolo Bonzini | enum MonitorEvent ev,
|
1647 | 32c81a4a | Paolo Bonzini | BlockErrorAction action, bool is_read)
|
1648 | 329c0a48 | Luiz Capitulino | { |
1649 | 329c0a48 | Luiz Capitulino | QObject *data; |
1650 | 329c0a48 | Luiz Capitulino | const char *action_str; |
1651 | 329c0a48 | Luiz Capitulino | |
1652 | 329c0a48 | Luiz Capitulino | switch (action) {
|
1653 | 329c0a48 | Luiz Capitulino | case BDRV_ACTION_REPORT:
|
1654 | 329c0a48 | Luiz Capitulino | action_str = "report";
|
1655 | 329c0a48 | Luiz Capitulino | break;
|
1656 | 329c0a48 | Luiz Capitulino | case BDRV_ACTION_IGNORE:
|
1657 | 329c0a48 | Luiz Capitulino | action_str = "ignore";
|
1658 | 329c0a48 | Luiz Capitulino | break;
|
1659 | 329c0a48 | Luiz Capitulino | case BDRV_ACTION_STOP:
|
1660 | 329c0a48 | Luiz Capitulino | action_str = "stop";
|
1661 | 329c0a48 | Luiz Capitulino | break;
|
1662 | 329c0a48 | Luiz Capitulino | default:
|
1663 | 329c0a48 | Luiz Capitulino | abort(); |
1664 | 329c0a48 | Luiz Capitulino | } |
1665 | 329c0a48 | Luiz Capitulino | |
1666 | 329c0a48 | Luiz Capitulino | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
|
1667 | 329c0a48 | Luiz Capitulino | bdrv->device_name, |
1668 | 329c0a48 | Luiz Capitulino | action_str, |
1669 | 329c0a48 | Luiz Capitulino | is_read ? "read" : "write"); |
1670 | 32c81a4a | Paolo Bonzini | monitor_protocol_event(ev, data); |
1671 | 329c0a48 | Luiz Capitulino | |
1672 | 329c0a48 | Luiz Capitulino | qobject_decref(data); |
1673 | 329c0a48 | Luiz Capitulino | } |
1674 | 329c0a48 | Luiz Capitulino | |
1675 | 6f382ed2 | Luiz Capitulino | static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) |
1676 | 6f382ed2 | Luiz Capitulino | { |
1677 | 6f382ed2 | Luiz Capitulino | QObject *data; |
1678 | 6f382ed2 | Luiz Capitulino | |
1679 | 6f382ed2 | Luiz Capitulino | data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
|
1680 | 6f382ed2 | Luiz Capitulino | bdrv_get_device_name(bs), ejected); |
1681 | 6f382ed2 | Luiz Capitulino | monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); |
1682 | 6f382ed2 | Luiz Capitulino | |
1683 | 6f382ed2 | Luiz Capitulino | qobject_decref(data); |
1684 | 6f382ed2 | Luiz Capitulino | } |
1685 | 6f382ed2 | Luiz Capitulino | |
1686 | 7d4b4ba5 | Markus Armbruster | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) |
1687 | 0e49de52 | Markus Armbruster | { |
1688 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->change_media_cb) {
|
1689 | 6f382ed2 | Luiz Capitulino | bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
|
1690 | 7d4b4ba5 | Markus Armbruster | bs->dev_ops->change_media_cb(bs->dev_opaque, load); |
1691 | 6f382ed2 | Luiz Capitulino | if (tray_was_closed) {
|
1692 | 6f382ed2 | Luiz Capitulino | /* tray open */
|
1693 | 6f382ed2 | Luiz Capitulino | bdrv_emit_qmp_eject_event(bs, true);
|
1694 | 6f382ed2 | Luiz Capitulino | } |
1695 | 6f382ed2 | Luiz Capitulino | if (load) {
|
1696 | 6f382ed2 | Luiz Capitulino | /* tray close */
|
1697 | 6f382ed2 | Luiz Capitulino | bdrv_emit_qmp_eject_event(bs, false);
|
1698 | 6f382ed2 | Luiz Capitulino | } |
1699 | 145feb17 | Markus Armbruster | } |
1700 | 145feb17 | Markus Armbruster | } |
1701 | 145feb17 | Markus Armbruster | |
1702 | 2c6942fa | Markus Armbruster | bool bdrv_dev_has_removable_media(BlockDriverState *bs)
|
1703 | 2c6942fa | Markus Armbruster | { |
1704 | 2c6942fa | Markus Armbruster | return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
|
1705 | 2c6942fa | Markus Armbruster | } |
1706 | 2c6942fa | Markus Armbruster | |
1707 | 025ccaa7 | Paolo Bonzini | void bdrv_dev_eject_request(BlockDriverState *bs, bool force) |
1708 | 025ccaa7 | Paolo Bonzini | { |
1709 | 025ccaa7 | Paolo Bonzini | if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
|
1710 | 025ccaa7 | Paolo Bonzini | bs->dev_ops->eject_request_cb(bs->dev_opaque, force); |
1711 | 025ccaa7 | Paolo Bonzini | } |
1712 | 025ccaa7 | Paolo Bonzini | } |
1713 | 025ccaa7 | Paolo Bonzini | |
1714 | e4def80b | Markus Armbruster | bool bdrv_dev_is_tray_open(BlockDriverState *bs)
|
1715 | e4def80b | Markus Armbruster | { |
1716 | e4def80b | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_tray_open) {
|
1717 | e4def80b | Markus Armbruster | return bs->dev_ops->is_tray_open(bs->dev_opaque);
|
1718 | e4def80b | Markus Armbruster | } |
1719 | e4def80b | Markus Armbruster | return false; |
1720 | e4def80b | Markus Armbruster | } |
1721 | e4def80b | Markus Armbruster | |
1722 | 145feb17 | Markus Armbruster | static void bdrv_dev_resize_cb(BlockDriverState *bs) |
1723 | 145feb17 | Markus Armbruster | { |
1724 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->resize_cb) {
|
1725 | 145feb17 | Markus Armbruster | bs->dev_ops->resize_cb(bs->dev_opaque); |
1726 | 0e49de52 | Markus Armbruster | } |
1727 | 0e49de52 | Markus Armbruster | } |
1728 | 0e49de52 | Markus Armbruster | |
1729 | f107639a | Markus Armbruster | bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
|
1730 | f107639a | Markus Armbruster | { |
1731 | f107639a | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
|
1732 | f107639a | Markus Armbruster | return bs->dev_ops->is_medium_locked(bs->dev_opaque);
|
1733 | f107639a | Markus Armbruster | } |
1734 | f107639a | Markus Armbruster | return false; |
1735 | f107639a | Markus Armbruster | } |
1736 | f107639a | Markus Armbruster | |
1737 | e97fc193 | aliguori | /*
|
1738 | e97fc193 | aliguori | * Run consistency checks on an image
|
1739 | e97fc193 | aliguori | *
|
1740 | e076f338 | Kevin Wolf | * Returns 0 if the check could be completed (it doesn't mean that the image is
|
1741 | a1c7273b | Stefan Weil | * free of errors) or -errno when an internal error occurred. The results of the
|
1742 | e076f338 | Kevin Wolf | * check are stored in res.
|
1743 | e97fc193 | aliguori | */
|
1744 | 4534ff54 | Kevin Wolf | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
|
1745 | e97fc193 | aliguori | { |
1746 | e97fc193 | aliguori | if (bs->drv->bdrv_check == NULL) { |
1747 | e97fc193 | aliguori | return -ENOTSUP;
|
1748 | e97fc193 | aliguori | } |
1749 | e97fc193 | aliguori | |
1750 | e076f338 | Kevin Wolf | memset(res, 0, sizeof(*res)); |
1751 | 4534ff54 | Kevin Wolf | return bs->drv->bdrv_check(bs, res, fix);
|
1752 | e97fc193 | aliguori | } |
1753 | e97fc193 | aliguori | |
1754 | 8a426614 | Kevin Wolf | #define COMMIT_BUF_SECTORS 2048 |
1755 | 8a426614 | Kevin Wolf | |
1756 | 33e3963e | bellard | /* commit COW file into the raw image */
|
1757 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
1758 | 33e3963e | bellard | { |
1759 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
1760 | 8a426614 | Kevin Wolf | int64_t sector, total_sectors; |
1761 | 8a426614 | Kevin Wolf | int n, ro, open_flags;
|
1762 | 0bce597d | Jeff Cody | int ret = 0; |
1763 | 8a426614 | Kevin Wolf | uint8_t *buf; |
1764 | c2cba3d9 | Jim Meyering | char filename[PATH_MAX];
|
1765 | 33e3963e | bellard | |
1766 | 19cb3738 | bellard | if (!drv)
|
1767 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1768 | 4dca4b63 | Naphtali Sprei | |
1769 | 4dca4b63 | Naphtali Sprei | if (!bs->backing_hd) {
|
1770 | 4dca4b63 | Naphtali Sprei | return -ENOTSUP;
|
1771 | 33e3963e | bellard | } |
1772 | 33e3963e | bellard | |
1773 | 2d3735d3 | Stefan Hajnoczi | if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
|
1774 | 2d3735d3 | Stefan Hajnoczi | return -EBUSY;
|
1775 | 2d3735d3 | Stefan Hajnoczi | } |
1776 | 2d3735d3 | Stefan Hajnoczi | |
1777 | 4dca4b63 | Naphtali Sprei | ro = bs->backing_hd->read_only; |
1778 | c2cba3d9 | Jim Meyering | /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */
|
1779 | c2cba3d9 | Jim Meyering | pstrcpy(filename, sizeof(filename), bs->backing_hd->filename);
|
1780 | 4dca4b63 | Naphtali Sprei | open_flags = bs->backing_hd->open_flags; |
1781 | 4dca4b63 | Naphtali Sprei | |
1782 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
1783 | 0bce597d | Jeff Cody | if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { |
1784 | 0bce597d | Jeff Cody | return -EACCES;
|
1785 | 4dca4b63 | Naphtali Sprei | } |
1786 | ea2384d3 | bellard | } |
1787 | 33e3963e | bellard | |
1788 | 6ea44308 | Jan Kiszka | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
1789 | 7267c094 | Anthony Liguori | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
1790 | 8a426614 | Kevin Wolf | |
1791 | 8a426614 | Kevin Wolf | for (sector = 0; sector < total_sectors; sector += n) { |
1792 | 05c4af54 | Stefan Hajnoczi | if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
|
1793 | 8a426614 | Kevin Wolf | |
1794 | 8a426614 | Kevin Wolf | if (bdrv_read(bs, sector, buf, n) != 0) { |
1795 | 8a426614 | Kevin Wolf | ret = -EIO; |
1796 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
1797 | 8a426614 | Kevin Wolf | } |
1798 | 8a426614 | Kevin Wolf | |
1799 | 8a426614 | Kevin Wolf | if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { |
1800 | 8a426614 | Kevin Wolf | ret = -EIO; |
1801 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
1802 | 8a426614 | Kevin Wolf | } |
1803 | ea2384d3 | bellard | } |
1804 | 33e3963e | bellard | } |
1805 | 95389c86 | bellard | |
1806 | 1d44952f | Christoph Hellwig | if (drv->bdrv_make_empty) {
|
1807 | 1d44952f | Christoph Hellwig | ret = drv->bdrv_make_empty(bs); |
1808 | 1d44952f | Christoph Hellwig | bdrv_flush(bs); |
1809 | 1d44952f | Christoph Hellwig | } |
1810 | 95389c86 | bellard | |
1811 | 3f5075ae | Christoph Hellwig | /*
|
1812 | 3f5075ae | Christoph Hellwig | * Make sure all data we wrote to the backing device is actually
|
1813 | 3f5075ae | Christoph Hellwig | * stable on disk.
|
1814 | 3f5075ae | Christoph Hellwig | */
|
1815 | 3f5075ae | Christoph Hellwig | if (bs->backing_hd)
|
1816 | 3f5075ae | Christoph Hellwig | bdrv_flush(bs->backing_hd); |
1817 | 4dca4b63 | Naphtali Sprei | |
1818 | 4dca4b63 | Naphtali Sprei | ro_cleanup:
|
1819 | 7267c094 | Anthony Liguori | g_free(buf); |
1820 | 4dca4b63 | Naphtali Sprei | |
1821 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
1822 | 0bce597d | Jeff Cody | /* ignoring error return here */
|
1823 | 0bce597d | Jeff Cody | bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);
|
1824 | 4dca4b63 | Naphtali Sprei | } |
1825 | 4dca4b63 | Naphtali Sprei | |
1826 | 1d44952f | Christoph Hellwig | return ret;
|
1827 | 33e3963e | bellard | } |
1828 | 33e3963e | bellard | |
1829 | e8877497 | Stefan Hajnoczi | int bdrv_commit_all(void) |
1830 | 6ab4b5ab | Markus Armbruster | { |
1831 | 6ab4b5ab | Markus Armbruster | BlockDriverState *bs; |
1832 | 6ab4b5ab | Markus Armbruster | |
1833 | 6ab4b5ab | Markus Armbruster | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1834 | 272d2d8e | Jeff Cody | if (bs->drv && bs->backing_hd) {
|
1835 | 272d2d8e | Jeff Cody | int ret = bdrv_commit(bs);
|
1836 | 272d2d8e | Jeff Cody | if (ret < 0) { |
1837 | 272d2d8e | Jeff Cody | return ret;
|
1838 | 272d2d8e | Jeff Cody | } |
1839 | e8877497 | Stefan Hajnoczi | } |
1840 | 6ab4b5ab | Markus Armbruster | } |
1841 | e8877497 | Stefan Hajnoczi | return 0; |
1842 | 6ab4b5ab | Markus Armbruster | } |
1843 | 6ab4b5ab | Markus Armbruster | |
1844 | dbffbdcf | Stefan Hajnoczi | /**
|
1845 | dbffbdcf | Stefan Hajnoczi | * Remove an active request from the tracked requests list
|
1846 | dbffbdcf | Stefan Hajnoczi | *
|
1847 | dbffbdcf | Stefan Hajnoczi | * This function should be called when a tracked request is completing.
|
1848 | dbffbdcf | Stefan Hajnoczi | */
|
1849 | dbffbdcf | Stefan Hajnoczi | static void tracked_request_end(BdrvTrackedRequest *req) |
1850 | dbffbdcf | Stefan Hajnoczi | { |
1851 | dbffbdcf | Stefan Hajnoczi | QLIST_REMOVE(req, list); |
1852 | f4658285 | Stefan Hajnoczi | qemu_co_queue_restart_all(&req->wait_queue); |
1853 | dbffbdcf | Stefan Hajnoczi | } |
1854 | dbffbdcf | Stefan Hajnoczi | |
1855 | dbffbdcf | Stefan Hajnoczi | /**
|
1856 | dbffbdcf | Stefan Hajnoczi | * Add an active request to the tracked requests list
|
1857 | dbffbdcf | Stefan Hajnoczi | */
|
1858 | dbffbdcf | Stefan Hajnoczi | static void tracked_request_begin(BdrvTrackedRequest *req, |
1859 | dbffbdcf | Stefan Hajnoczi | BlockDriverState *bs, |
1860 | dbffbdcf | Stefan Hajnoczi | int64_t sector_num, |
1861 | dbffbdcf | Stefan Hajnoczi | int nb_sectors, bool is_write) |
1862 | dbffbdcf | Stefan Hajnoczi | { |
1863 | dbffbdcf | Stefan Hajnoczi | *req = (BdrvTrackedRequest){ |
1864 | dbffbdcf | Stefan Hajnoczi | .bs = bs, |
1865 | dbffbdcf | Stefan Hajnoczi | .sector_num = sector_num, |
1866 | dbffbdcf | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
1867 | dbffbdcf | Stefan Hajnoczi | .is_write = is_write, |
1868 | 5f8b6491 | Stefan Hajnoczi | .co = qemu_coroutine_self(), |
1869 | dbffbdcf | Stefan Hajnoczi | }; |
1870 | dbffbdcf | Stefan Hajnoczi | |
1871 | f4658285 | Stefan Hajnoczi | qemu_co_queue_init(&req->wait_queue); |
1872 | f4658285 | Stefan Hajnoczi | |
1873 | dbffbdcf | Stefan Hajnoczi | QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); |
1874 | dbffbdcf | Stefan Hajnoczi | } |
1875 | dbffbdcf | Stefan Hajnoczi | |
1876 | d83947ac | Stefan Hajnoczi | /**
|
1877 | d83947ac | Stefan Hajnoczi | * Round a region to cluster boundaries
|
1878 | d83947ac | Stefan Hajnoczi | */
|
1879 | 343bded4 | Paolo Bonzini | void bdrv_round_to_clusters(BlockDriverState *bs,
|
1880 | 343bded4 | Paolo Bonzini | int64_t sector_num, int nb_sectors,
|
1881 | 343bded4 | Paolo Bonzini | int64_t *cluster_sector_num, |
1882 | 343bded4 | Paolo Bonzini | int *cluster_nb_sectors)
|
1883 | d83947ac | Stefan Hajnoczi | { |
1884 | d83947ac | Stefan Hajnoczi | BlockDriverInfo bdi; |
1885 | d83947ac | Stefan Hajnoczi | |
1886 | d83947ac | Stefan Hajnoczi | if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { |
1887 | d83947ac | Stefan Hajnoczi | *cluster_sector_num = sector_num; |
1888 | d83947ac | Stefan Hajnoczi | *cluster_nb_sectors = nb_sectors; |
1889 | d83947ac | Stefan Hajnoczi | } else {
|
1890 | d83947ac | Stefan Hajnoczi | int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; |
1891 | d83947ac | Stefan Hajnoczi | *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); |
1892 | d83947ac | Stefan Hajnoczi | *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + |
1893 | d83947ac | Stefan Hajnoczi | nb_sectors, c); |
1894 | d83947ac | Stefan Hajnoczi | } |
1895 | d83947ac | Stefan Hajnoczi | } |
1896 | d83947ac | Stefan Hajnoczi | |
1897 | f4658285 | Stefan Hajnoczi | static bool tracked_request_overlaps(BdrvTrackedRequest *req, |
1898 | f4658285 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors) {
|
1899 | d83947ac | Stefan Hajnoczi | /* aaaa bbbb */
|
1900 | d83947ac | Stefan Hajnoczi | if (sector_num >= req->sector_num + req->nb_sectors) {
|
1901 | d83947ac | Stefan Hajnoczi | return false; |
1902 | d83947ac | Stefan Hajnoczi | } |
1903 | d83947ac | Stefan Hajnoczi | /* bbbb aaaa */
|
1904 | d83947ac | Stefan Hajnoczi | if (req->sector_num >= sector_num + nb_sectors) {
|
1905 | d83947ac | Stefan Hajnoczi | return false; |
1906 | d83947ac | Stefan Hajnoczi | } |
1907 | d83947ac | Stefan Hajnoczi | return true; |
1908 | f4658285 | Stefan Hajnoczi | } |
1909 | f4658285 | Stefan Hajnoczi | |
1910 | f4658285 | Stefan Hajnoczi | static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, |
1911 | f4658285 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors)
|
1912 | f4658285 | Stefan Hajnoczi | { |
1913 | f4658285 | Stefan Hajnoczi | BdrvTrackedRequest *req; |
1914 | d83947ac | Stefan Hajnoczi | int64_t cluster_sector_num; |
1915 | d83947ac | Stefan Hajnoczi | int cluster_nb_sectors;
|
1916 | f4658285 | Stefan Hajnoczi | bool retry;
|
1917 | f4658285 | Stefan Hajnoczi | |
1918 | d83947ac | Stefan Hajnoczi | /* If we touch the same cluster it counts as an overlap. This guarantees
|
1919 | d83947ac | Stefan Hajnoczi | * that allocating writes will be serialized and not race with each other
|
1920 | d83947ac | Stefan Hajnoczi | * for the same cluster. For example, in copy-on-read it ensures that the
|
1921 | d83947ac | Stefan Hajnoczi | * CoR read and write operations are atomic and guest writes cannot
|
1922 | d83947ac | Stefan Hajnoczi | * interleave between them.
|
1923 | d83947ac | Stefan Hajnoczi | */
|
1924 | 343bded4 | Paolo Bonzini | bdrv_round_to_clusters(bs, sector_num, nb_sectors, |
1925 | 343bded4 | Paolo Bonzini | &cluster_sector_num, &cluster_nb_sectors); |
1926 | d83947ac | Stefan Hajnoczi | |
1927 | f4658285 | Stefan Hajnoczi | do {
|
1928 | f4658285 | Stefan Hajnoczi | retry = false;
|
1929 | f4658285 | Stefan Hajnoczi | QLIST_FOREACH(req, &bs->tracked_requests, list) { |
1930 | d83947ac | Stefan Hajnoczi | if (tracked_request_overlaps(req, cluster_sector_num,
|
1931 | d83947ac | Stefan Hajnoczi | cluster_nb_sectors)) { |
1932 | 5f8b6491 | Stefan Hajnoczi | /* Hitting this means there was a reentrant request, for
|
1933 | 5f8b6491 | Stefan Hajnoczi | * example, a block driver issuing nested requests. This must
|
1934 | 5f8b6491 | Stefan Hajnoczi | * never happen since it means deadlock.
|
1935 | 5f8b6491 | Stefan Hajnoczi | */
|
1936 | 5f8b6491 | Stefan Hajnoczi | assert(qemu_coroutine_self() != req->co); |
1937 | 5f8b6491 | Stefan Hajnoczi | |
1938 | f4658285 | Stefan Hajnoczi | qemu_co_queue_wait(&req->wait_queue); |
1939 | f4658285 | Stefan Hajnoczi | retry = true;
|
1940 | f4658285 | Stefan Hajnoczi | break;
|
1941 | f4658285 | Stefan Hajnoczi | } |
1942 | f4658285 | Stefan Hajnoczi | } |
1943 | f4658285 | Stefan Hajnoczi | } while (retry);
|
1944 | f4658285 | Stefan Hajnoczi | } |
1945 | f4658285 | Stefan Hajnoczi | |
1946 | 756e6736 | Kevin Wolf | /*
|
1947 | 756e6736 | Kevin Wolf | * Return values:
|
1948 | 756e6736 | Kevin Wolf | * 0 - success
|
1949 | 756e6736 | Kevin Wolf | * -EINVAL - backing format specified, but no file
|
1950 | 756e6736 | Kevin Wolf | * -ENOSPC - can't update the backing file because no space is left in the
|
1951 | 756e6736 | Kevin Wolf | * image file header
|
1952 | 756e6736 | Kevin Wolf | * -ENOTSUP - format driver doesn't support changing the backing file
|
1953 | 756e6736 | Kevin Wolf | */
|
1954 | 756e6736 | Kevin Wolf | int bdrv_change_backing_file(BlockDriverState *bs,
|
1955 | 756e6736 | Kevin Wolf | const char *backing_file, const char *backing_fmt) |
1956 | 756e6736 | Kevin Wolf | { |
1957 | 756e6736 | Kevin Wolf | BlockDriver *drv = bs->drv; |
1958 | 469ef350 | Paolo Bonzini | int ret;
|
1959 | 756e6736 | Kevin Wolf | |
1960 | 5f377794 | Paolo Bonzini | /* Backing file format doesn't make sense without a backing file */
|
1961 | 5f377794 | Paolo Bonzini | if (backing_fmt && !backing_file) {
|
1962 | 5f377794 | Paolo Bonzini | return -EINVAL;
|
1963 | 5f377794 | Paolo Bonzini | } |
1964 | 5f377794 | Paolo Bonzini | |
1965 | 756e6736 | Kevin Wolf | if (drv->bdrv_change_backing_file != NULL) { |
1966 | 469ef350 | Paolo Bonzini | ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); |
1967 | 756e6736 | Kevin Wolf | } else {
|
1968 | 469ef350 | Paolo Bonzini | ret = -ENOTSUP; |
1969 | 756e6736 | Kevin Wolf | } |
1970 | 469ef350 | Paolo Bonzini | |
1971 | 469ef350 | Paolo Bonzini | if (ret == 0) { |
1972 | 469ef350 | Paolo Bonzini | pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); |
1973 | 469ef350 | Paolo Bonzini | pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); |
1974 | 469ef350 | Paolo Bonzini | } |
1975 | 469ef350 | Paolo Bonzini | return ret;
|
1976 | 756e6736 | Kevin Wolf | } |
1977 | 756e6736 | Kevin Wolf | |
1978 | 6ebdcee2 | Jeff Cody | /*
|
1979 | 6ebdcee2 | Jeff Cody | * Finds the image layer in the chain that has 'bs' as its backing file.
|
1980 | 6ebdcee2 | Jeff Cody | *
|
1981 | 6ebdcee2 | Jeff Cody | * active is the current topmost image.
|
1982 | 6ebdcee2 | Jeff Cody | *
|
1983 | 6ebdcee2 | Jeff Cody | * Returns NULL if bs is not found in active's image chain,
|
1984 | 6ebdcee2 | Jeff Cody | * or if active == bs.
|
1985 | 6ebdcee2 | Jeff Cody | */
|
1986 | 6ebdcee2 | Jeff Cody | BlockDriverState *bdrv_find_overlay(BlockDriverState *active, |
1987 | 6ebdcee2 | Jeff Cody | BlockDriverState *bs) |
1988 | 6ebdcee2 | Jeff Cody | { |
1989 | 6ebdcee2 | Jeff Cody | BlockDriverState *overlay = NULL;
|
1990 | 6ebdcee2 | Jeff Cody | BlockDriverState *intermediate; |
1991 | 6ebdcee2 | Jeff Cody | |
1992 | 6ebdcee2 | Jeff Cody | assert(active != NULL);
|
1993 | 6ebdcee2 | Jeff Cody | assert(bs != NULL);
|
1994 | 6ebdcee2 | Jeff Cody | |
1995 | 6ebdcee2 | Jeff Cody | /* if bs is the same as active, then by definition it has no overlay
|
1996 | 6ebdcee2 | Jeff Cody | */
|
1997 | 6ebdcee2 | Jeff Cody | if (active == bs) {
|
1998 | 6ebdcee2 | Jeff Cody | return NULL; |
1999 | 6ebdcee2 | Jeff Cody | } |
2000 | 6ebdcee2 | Jeff Cody | |
2001 | 6ebdcee2 | Jeff Cody | intermediate = active; |
2002 | 6ebdcee2 | Jeff Cody | while (intermediate->backing_hd) {
|
2003 | 6ebdcee2 | Jeff Cody | if (intermediate->backing_hd == bs) {
|
2004 | 6ebdcee2 | Jeff Cody | overlay = intermediate; |
2005 | 6ebdcee2 | Jeff Cody | break;
|
2006 | 6ebdcee2 | Jeff Cody | } |
2007 | 6ebdcee2 | Jeff Cody | intermediate = intermediate->backing_hd; |
2008 | 6ebdcee2 | Jeff Cody | } |
2009 | 6ebdcee2 | Jeff Cody | |
2010 | 6ebdcee2 | Jeff Cody | return overlay;
|
2011 | 6ebdcee2 | Jeff Cody | } |
2012 | 6ebdcee2 | Jeff Cody | |
2013 | 6ebdcee2 | Jeff Cody | typedef struct BlkIntermediateStates { |
2014 | 6ebdcee2 | Jeff Cody | BlockDriverState *bs; |
2015 | 6ebdcee2 | Jeff Cody | QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; |
2016 | 6ebdcee2 | Jeff Cody | } BlkIntermediateStates; |
2017 | 6ebdcee2 | Jeff Cody | |
2018 | 6ebdcee2 | Jeff Cody | |
2019 | 6ebdcee2 | Jeff Cody | /*
|
2020 | 6ebdcee2 | Jeff Cody | * Drops images above 'base' up to and including 'top', and sets the image
|
2021 | 6ebdcee2 | Jeff Cody | * above 'top' to have base as its backing file.
|
2022 | 6ebdcee2 | Jeff Cody | *
|
2023 | 6ebdcee2 | Jeff Cody | * Requires that the overlay to 'top' is opened r/w, so that the backing file
|
2024 | 6ebdcee2 | Jeff Cody | * information in 'bs' can be properly updated.
|
2025 | 6ebdcee2 | Jeff Cody | *
|
2026 | 6ebdcee2 | Jeff Cody | * E.g., this will convert the following chain:
|
2027 | 6ebdcee2 | Jeff Cody | * bottom <- base <- intermediate <- top <- active
|
2028 | 6ebdcee2 | Jeff Cody | *
|
2029 | 6ebdcee2 | Jeff Cody | * to
|
2030 | 6ebdcee2 | Jeff Cody | *
|
2031 | 6ebdcee2 | Jeff Cody | * bottom <- base <- active
|
2032 | 6ebdcee2 | Jeff Cody | *
|
2033 | 6ebdcee2 | Jeff Cody | * It is allowed for bottom==base, in which case it converts:
|
2034 | 6ebdcee2 | Jeff Cody | *
|
2035 | 6ebdcee2 | Jeff Cody | * base <- intermediate <- top <- active
|
2036 | 6ebdcee2 | Jeff Cody | *
|
2037 | 6ebdcee2 | Jeff Cody | * to
|
2038 | 6ebdcee2 | Jeff Cody | *
|
2039 | 6ebdcee2 | Jeff Cody | * base <- active
|
2040 | 6ebdcee2 | Jeff Cody | *
|
2041 | 6ebdcee2 | Jeff Cody | * Error conditions:
|
2042 | 6ebdcee2 | Jeff Cody | * if active == top, that is considered an error
|
2043 | 6ebdcee2 | Jeff Cody | *
|
2044 | 6ebdcee2 | Jeff Cody | */
|
2045 | 6ebdcee2 | Jeff Cody | int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
|
2046 | 6ebdcee2 | Jeff Cody | BlockDriverState *base) |
2047 | 6ebdcee2 | Jeff Cody | { |
2048 | 6ebdcee2 | Jeff Cody | BlockDriverState *intermediate; |
2049 | 6ebdcee2 | Jeff Cody | BlockDriverState *base_bs = NULL;
|
2050 | 6ebdcee2 | Jeff Cody | BlockDriverState *new_top_bs = NULL;
|
2051 | 6ebdcee2 | Jeff Cody | BlkIntermediateStates *intermediate_state, *next; |
2052 | 6ebdcee2 | Jeff Cody | int ret = -EIO;
|
2053 | 6ebdcee2 | Jeff Cody | |
2054 | 6ebdcee2 | Jeff Cody | QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; |
2055 | 6ebdcee2 | Jeff Cody | QSIMPLEQ_INIT(&states_to_delete); |
2056 | 6ebdcee2 | Jeff Cody | |
2057 | 6ebdcee2 | Jeff Cody | if (!top->drv || !base->drv) {
|
2058 | 6ebdcee2 | Jeff Cody | goto exit;
|
2059 | 6ebdcee2 | Jeff Cody | } |
2060 | 6ebdcee2 | Jeff Cody | |
2061 | 6ebdcee2 | Jeff Cody | new_top_bs = bdrv_find_overlay(active, top); |
2062 | 6ebdcee2 | Jeff Cody | |
2063 | 6ebdcee2 | Jeff Cody | if (new_top_bs == NULL) { |
2064 | 6ebdcee2 | Jeff Cody | /* we could not find the image above 'top', this is an error */
|
2065 | 6ebdcee2 | Jeff Cody | goto exit;
|
2066 | 6ebdcee2 | Jeff Cody | } |
2067 | 6ebdcee2 | Jeff Cody | |
2068 | 6ebdcee2 | Jeff Cody | /* special case of new_top_bs->backing_hd already pointing to base - nothing
|
2069 | 6ebdcee2 | Jeff Cody | * to do, no intermediate images */
|
2070 | 6ebdcee2 | Jeff Cody | if (new_top_bs->backing_hd == base) {
|
2071 | 6ebdcee2 | Jeff Cody | ret = 0;
|
2072 | 6ebdcee2 | Jeff Cody | goto exit;
|
2073 | 6ebdcee2 | Jeff Cody | } |
2074 | 6ebdcee2 | Jeff Cody | |
2075 | 6ebdcee2 | Jeff Cody | intermediate = top; |
2076 | 6ebdcee2 | Jeff Cody | |
2077 | 6ebdcee2 | Jeff Cody | /* now we will go down through the list, and add each BDS we find
|
2078 | 6ebdcee2 | Jeff Cody | * into our deletion queue, until we hit the 'base'
|
2079 | 6ebdcee2 | Jeff Cody | */
|
2080 | 6ebdcee2 | Jeff Cody | while (intermediate) {
|
2081 | 6ebdcee2 | Jeff Cody | intermediate_state = g_malloc0(sizeof(BlkIntermediateStates));
|
2082 | 6ebdcee2 | Jeff Cody | intermediate_state->bs = intermediate; |
2083 | 6ebdcee2 | Jeff Cody | QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); |
2084 | 6ebdcee2 | Jeff Cody | |
2085 | 6ebdcee2 | Jeff Cody | if (intermediate->backing_hd == base) {
|
2086 | 6ebdcee2 | Jeff Cody | base_bs = intermediate->backing_hd; |
2087 | 6ebdcee2 | Jeff Cody | break;
|
2088 | 6ebdcee2 | Jeff Cody | } |
2089 | 6ebdcee2 | Jeff Cody | intermediate = intermediate->backing_hd; |
2090 | 6ebdcee2 | Jeff Cody | } |
2091 | 6ebdcee2 | Jeff Cody | if (base_bs == NULL) { |
2092 | 6ebdcee2 | Jeff Cody | /* something went wrong, we did not end at the base. safely
|
2093 | 6ebdcee2 | Jeff Cody | * unravel everything, and exit with error */
|
2094 | 6ebdcee2 | Jeff Cody | goto exit;
|
2095 | 6ebdcee2 | Jeff Cody | } |
2096 | 6ebdcee2 | Jeff Cody | |
2097 | 6ebdcee2 | Jeff Cody | /* success - we can delete the intermediate states, and link top->base */
|
2098 | 6ebdcee2 | Jeff Cody | ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, |
2099 | 6ebdcee2 | Jeff Cody | base_bs->drv ? base_bs->drv->format_name : "");
|
2100 | 6ebdcee2 | Jeff Cody | if (ret) {
|
2101 | 6ebdcee2 | Jeff Cody | goto exit;
|
2102 | 6ebdcee2 | Jeff Cody | } |
2103 | 6ebdcee2 | Jeff Cody | new_top_bs->backing_hd = base_bs; |
2104 | 6ebdcee2 | Jeff Cody | |
2105 | 6ebdcee2 | Jeff Cody | |
2106 | 6ebdcee2 | Jeff Cody | QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { |
2107 | 6ebdcee2 | Jeff Cody | /* so that bdrv_close() does not recursively close the chain */
|
2108 | 6ebdcee2 | Jeff Cody | intermediate_state->bs->backing_hd = NULL;
|
2109 | 6ebdcee2 | Jeff Cody | bdrv_delete(intermediate_state->bs); |
2110 | 6ebdcee2 | Jeff Cody | } |
2111 | 6ebdcee2 | Jeff Cody | ret = 0;
|
2112 | 6ebdcee2 | Jeff Cody | |
2113 | 6ebdcee2 | Jeff Cody | exit:
|
2114 | 6ebdcee2 | Jeff Cody | QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { |
2115 | 6ebdcee2 | Jeff Cody | g_free(intermediate_state); |
2116 | 6ebdcee2 | Jeff Cody | } |
2117 | 6ebdcee2 | Jeff Cody | return ret;
|
2118 | 6ebdcee2 | Jeff Cody | } |
2119 | 6ebdcee2 | Jeff Cody | |
2120 | 6ebdcee2 | Jeff Cody | |
2121 | 71d0770c | aliguori | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
2122 | 71d0770c | aliguori | size_t size) |
2123 | 71d0770c | aliguori | { |
2124 | 71d0770c | aliguori | int64_t len; |
2125 | 71d0770c | aliguori | |
2126 | 71d0770c | aliguori | if (!bdrv_is_inserted(bs))
|
2127 | 71d0770c | aliguori | return -ENOMEDIUM;
|
2128 | 71d0770c | aliguori | |
2129 | 71d0770c | aliguori | if (bs->growable)
|
2130 | 71d0770c | aliguori | return 0; |
2131 | 71d0770c | aliguori | |
2132 | 71d0770c | aliguori | len = bdrv_getlength(bs); |
2133 | 71d0770c | aliguori | |
2134 | fbb7b4e0 | Kevin Wolf | if (offset < 0) |
2135 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
2136 | fbb7b4e0 | Kevin Wolf | |
2137 | fbb7b4e0 | Kevin Wolf | if ((offset > len) || (len - offset < size))
|
2138 | 71d0770c | aliguori | return -EIO;
|
2139 | 71d0770c | aliguori | |
2140 | 71d0770c | aliguori | return 0; |
2141 | 71d0770c | aliguori | } |
2142 | 71d0770c | aliguori | |
2143 | 71d0770c | aliguori | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
2144 | 71d0770c | aliguori | int nb_sectors)
|
2145 | 71d0770c | aliguori | { |
2146 | eb5a3165 | Jes Sorensen | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
|
2147 | eb5a3165 | Jes Sorensen | nb_sectors * BDRV_SECTOR_SIZE); |
2148 | 71d0770c | aliguori | } |
2149 | 71d0770c | aliguori | |
2150 | 1c9805a3 | Stefan Hajnoczi | typedef struct RwCo { |
2151 | 1c9805a3 | Stefan Hajnoczi | BlockDriverState *bs; |
2152 | 1c9805a3 | Stefan Hajnoczi | int64_t sector_num; |
2153 | 1c9805a3 | Stefan Hajnoczi | int nb_sectors;
|
2154 | 1c9805a3 | Stefan Hajnoczi | QEMUIOVector *qiov; |
2155 | 1c9805a3 | Stefan Hajnoczi | bool is_write;
|
2156 | 1c9805a3 | Stefan Hajnoczi | int ret;
|
2157 | 1c9805a3 | Stefan Hajnoczi | } RwCo; |
2158 | 1c9805a3 | Stefan Hajnoczi | |
2159 | 1c9805a3 | Stefan Hajnoczi | static void coroutine_fn bdrv_rw_co_entry(void *opaque) |
2160 | fc01f7e7 | bellard | { |
2161 | 1c9805a3 | Stefan Hajnoczi | RwCo *rwco = opaque; |
2162 | ea2384d3 | bellard | |
2163 | 1c9805a3 | Stefan Hajnoczi | if (!rwco->is_write) {
|
2164 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, |
2165 | 470c0504 | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov, 0);
|
2166 | 1c9805a3 | Stefan Hajnoczi | } else {
|
2167 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, |
2168 | f08f2dda | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov, 0);
|
2169 | 1c9805a3 | Stefan Hajnoczi | } |
2170 | 1c9805a3 | Stefan Hajnoczi | } |
2171 | e7a8a783 | Kevin Wolf | |
2172 | 1c9805a3 | Stefan Hajnoczi | /*
|
2173 | 8d3b1a2d | Kevin Wolf | * Process a vectored synchronous request using coroutines
|
2174 | 1c9805a3 | Stefan Hajnoczi | */
|
2175 | 8d3b1a2d | Kevin Wolf | static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, |
2176 | 8d3b1a2d | Kevin Wolf | QEMUIOVector *qiov, bool is_write)
|
2177 | 1c9805a3 | Stefan Hajnoczi | { |
2178 | 1c9805a3 | Stefan Hajnoczi | Coroutine *co; |
2179 | 1c9805a3 | Stefan Hajnoczi | RwCo rwco = { |
2180 | 1c9805a3 | Stefan Hajnoczi | .bs = bs, |
2181 | 1c9805a3 | Stefan Hajnoczi | .sector_num = sector_num, |
2182 | 8d3b1a2d | Kevin Wolf | .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, |
2183 | 8d3b1a2d | Kevin Wolf | .qiov = qiov, |
2184 | 1c9805a3 | Stefan Hajnoczi | .is_write = is_write, |
2185 | 1c9805a3 | Stefan Hajnoczi | .ret = NOT_DONE, |
2186 | 1c9805a3 | Stefan Hajnoczi | }; |
2187 | 8d3b1a2d | Kevin Wolf | assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); |
2188 | e7a8a783 | Kevin Wolf | |
2189 | 498e386c | Zhi Yong Wu | /**
|
2190 | 498e386c | Zhi Yong Wu | * In sync call context, when the vcpu is blocked, this throttling timer
|
2191 | 498e386c | Zhi Yong Wu | * will not fire; so the I/O throttling function has to be disabled here
|
2192 | 498e386c | Zhi Yong Wu | * if it has been enabled.
|
2193 | 498e386c | Zhi Yong Wu | */
|
2194 | 498e386c | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
2195 | 498e386c | Zhi Yong Wu | fprintf(stderr, "Disabling I/O throttling on '%s' due "
|
2196 | 498e386c | Zhi Yong Wu | "to synchronous I/O.\n", bdrv_get_device_name(bs));
|
2197 | 498e386c | Zhi Yong Wu | bdrv_io_limits_disable(bs); |
2198 | 498e386c | Zhi Yong Wu | } |
2199 | 498e386c | Zhi Yong Wu | |
2200 | 1c9805a3 | Stefan Hajnoczi | if (qemu_in_coroutine()) {
|
2201 | 1c9805a3 | Stefan Hajnoczi | /* Fast-path if already in coroutine context */
|
2202 | 1c9805a3 | Stefan Hajnoczi | bdrv_rw_co_entry(&rwco); |
2203 | 1c9805a3 | Stefan Hajnoczi | } else {
|
2204 | 1c9805a3 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_rw_co_entry); |
2205 | 1c9805a3 | Stefan Hajnoczi | qemu_coroutine_enter(co, &rwco); |
2206 | 1c9805a3 | Stefan Hajnoczi | while (rwco.ret == NOT_DONE) {
|
2207 | 1c9805a3 | Stefan Hajnoczi | qemu_aio_wait(); |
2208 | 1c9805a3 | Stefan Hajnoczi | } |
2209 | 1c9805a3 | Stefan Hajnoczi | } |
2210 | 1c9805a3 | Stefan Hajnoczi | return rwco.ret;
|
2211 | 1c9805a3 | Stefan Hajnoczi | } |
2212 | b338082b | bellard | |
2213 | 8d3b1a2d | Kevin Wolf | /*
|
2214 | 8d3b1a2d | Kevin Wolf | * Process a synchronous request using coroutines
|
2215 | 8d3b1a2d | Kevin Wolf | */
|
2216 | 8d3b1a2d | Kevin Wolf | static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, |
2217 | 8d3b1a2d | Kevin Wolf | int nb_sectors, bool is_write) |
2218 | 8d3b1a2d | Kevin Wolf | { |
2219 | 8d3b1a2d | Kevin Wolf | QEMUIOVector qiov; |
2220 | 8d3b1a2d | Kevin Wolf | struct iovec iov = {
|
2221 | 8d3b1a2d | Kevin Wolf | .iov_base = (void *)buf,
|
2222 | 8d3b1a2d | Kevin Wolf | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
2223 | 8d3b1a2d | Kevin Wolf | }; |
2224 | 8d3b1a2d | Kevin Wolf | |
2225 | 8d3b1a2d | Kevin Wolf | qemu_iovec_init_external(&qiov, &iov, 1);
|
2226 | 8d3b1a2d | Kevin Wolf | return bdrv_rwv_co(bs, sector_num, &qiov, is_write);
|
2227 | 8d3b1a2d | Kevin Wolf | } |
2228 | 8d3b1a2d | Kevin Wolf | |
2229 | 1c9805a3 | Stefan Hajnoczi | /* return < 0 if error. See bdrv_write() for the return codes */
|
2230 | 1c9805a3 | Stefan Hajnoczi | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
2231 | 1c9805a3 | Stefan Hajnoczi | uint8_t *buf, int nb_sectors)
|
2232 | 1c9805a3 | Stefan Hajnoczi | { |
2233 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); |
2234 | fc01f7e7 | bellard | } |
2235 | fc01f7e7 | bellard | |
2236 | 07d27a44 | Markus Armbruster | /* Just like bdrv_read(), but with I/O throttling temporarily disabled */
|
2237 | 07d27a44 | Markus Armbruster | int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
|
2238 | 07d27a44 | Markus Armbruster | uint8_t *buf, int nb_sectors)
|
2239 | 07d27a44 | Markus Armbruster | { |
2240 | 07d27a44 | Markus Armbruster | bool enabled;
|
2241 | 07d27a44 | Markus Armbruster | int ret;
|
2242 | 07d27a44 | Markus Armbruster | |
2243 | 07d27a44 | Markus Armbruster | enabled = bs->io_limits_enabled; |
2244 | 07d27a44 | Markus Armbruster | bs->io_limits_enabled = false;
|
2245 | 07d27a44 | Markus Armbruster | ret = bdrv_read(bs, 0, buf, 1); |
2246 | 07d27a44 | Markus Armbruster | bs->io_limits_enabled = enabled; |
2247 | 07d27a44 | Markus Armbruster | return ret;
|
2248 | 07d27a44 | Markus Armbruster | } |
2249 | 07d27a44 | Markus Armbruster | |
2250 | 5fafdf24 | ths | /* Return < 0 if error. Important errors are:
|
2251 | 19cb3738 | bellard | -EIO generic I/O error (may happen for all errors)
|
2252 | 19cb3738 | bellard | -ENOMEDIUM No media inserted.
|
2253 | 19cb3738 | bellard | -EINVAL Invalid sector number or nb_sectors
|
2254 | 19cb3738 | bellard | -EACCES Trying to write a read-only device
|
2255 | 19cb3738 | bellard | */
|
2256 | 5fafdf24 | ths | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
2257 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
2258 | fc01f7e7 | bellard | { |
2259 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); |
2260 | 83f64091 | bellard | } |
2261 | 83f64091 | bellard | |
2262 | 8d3b1a2d | Kevin Wolf | int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
|
2263 | 8d3b1a2d | Kevin Wolf | { |
2264 | 8d3b1a2d | Kevin Wolf | return bdrv_rwv_co(bs, sector_num, qiov, true); |
2265 | 8d3b1a2d | Kevin Wolf | } |
2266 | 8d3b1a2d | Kevin Wolf | |
2267 | eda578e5 | aliguori | int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
2268 | eda578e5 | aliguori | void *buf, int count1) |
2269 | 83f64091 | bellard | { |
2270 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
2271 | 83f64091 | bellard | int len, nb_sectors, count;
|
2272 | 83f64091 | bellard | int64_t sector_num; |
2273 | 9a8c4cce | Kevin Wolf | int ret;
|
2274 | 83f64091 | bellard | |
2275 | 83f64091 | bellard | count = count1; |
2276 | 83f64091 | bellard | /* first read to align to sector start */
|
2277 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
2278 | 83f64091 | bellard | if (len > count)
|
2279 | 83f64091 | bellard | len = count; |
2280 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
2281 | 83f64091 | bellard | if (len > 0) { |
2282 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2283 | 9a8c4cce | Kevin Wolf | return ret;
|
2284 | 6ea44308 | Jan Kiszka | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
|
2285 | 83f64091 | bellard | count -= len; |
2286 | 83f64091 | bellard | if (count == 0) |
2287 | 83f64091 | bellard | return count1;
|
2288 | 83f64091 | bellard | sector_num++; |
2289 | 83f64091 | bellard | buf += len; |
2290 | 83f64091 | bellard | } |
2291 | 83f64091 | bellard | |
2292 | 83f64091 | bellard | /* read the sectors "in place" */
|
2293 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
2294 | 83f64091 | bellard | if (nb_sectors > 0) { |
2295 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
2296 | 9a8c4cce | Kevin Wolf | return ret;
|
2297 | 83f64091 | bellard | sector_num += nb_sectors; |
2298 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
2299 | 83f64091 | bellard | buf += len; |
2300 | 83f64091 | bellard | count -= len; |
2301 | 83f64091 | bellard | } |
2302 | 83f64091 | bellard | |
2303 | 83f64091 | bellard | /* add data from the last sector */
|
2304 | 83f64091 | bellard | if (count > 0) { |
2305 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2306 | 9a8c4cce | Kevin Wolf | return ret;
|
2307 | 83f64091 | bellard | memcpy(buf, tmp_buf, count); |
2308 | 83f64091 | bellard | } |
2309 | 83f64091 | bellard | return count1;
|
2310 | 83f64091 | bellard | } |
2311 | 83f64091 | bellard | |
2312 | 8d3b1a2d | Kevin Wolf | int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov)
|
2313 | 83f64091 | bellard | { |
2314 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
2315 | 83f64091 | bellard | int len, nb_sectors, count;
|
2316 | 83f64091 | bellard | int64_t sector_num; |
2317 | 9a8c4cce | Kevin Wolf | int ret;
|
2318 | 83f64091 | bellard | |
2319 | 8d3b1a2d | Kevin Wolf | count = qiov->size; |
2320 | 8d3b1a2d | Kevin Wolf | |
2321 | 83f64091 | bellard | /* first write to align to sector start */
|
2322 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
2323 | 83f64091 | bellard | if (len > count)
|
2324 | 83f64091 | bellard | len = count; |
2325 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
2326 | 83f64091 | bellard | if (len > 0) { |
2327 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2328 | 9a8c4cce | Kevin Wolf | return ret;
|
2329 | 8d3b1a2d | Kevin Wolf | qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), |
2330 | 8d3b1a2d | Kevin Wolf | len); |
2331 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
2332 | 9a8c4cce | Kevin Wolf | return ret;
|
2333 | 83f64091 | bellard | count -= len; |
2334 | 83f64091 | bellard | if (count == 0) |
2335 | 8d3b1a2d | Kevin Wolf | return qiov->size;
|
2336 | 83f64091 | bellard | sector_num++; |
2337 | 83f64091 | bellard | } |
2338 | 83f64091 | bellard | |
2339 | 83f64091 | bellard | /* write the sectors "in place" */
|
2340 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
2341 | 83f64091 | bellard | if (nb_sectors > 0) { |
2342 | 8d3b1a2d | Kevin Wolf | QEMUIOVector qiov_inplace; |
2343 | 8d3b1a2d | Kevin Wolf | |
2344 | 8d3b1a2d | Kevin Wolf | qemu_iovec_init(&qiov_inplace, qiov->niov); |
2345 | 8d3b1a2d | Kevin Wolf | qemu_iovec_concat(&qiov_inplace, qiov, len, |
2346 | 8d3b1a2d | Kevin Wolf | nb_sectors << BDRV_SECTOR_BITS); |
2347 | 8d3b1a2d | Kevin Wolf | ret = bdrv_writev(bs, sector_num, &qiov_inplace); |
2348 | 8d3b1a2d | Kevin Wolf | qemu_iovec_destroy(&qiov_inplace); |
2349 | 8d3b1a2d | Kevin Wolf | if (ret < 0) { |
2350 | 9a8c4cce | Kevin Wolf | return ret;
|
2351 | 8d3b1a2d | Kevin Wolf | } |
2352 | 8d3b1a2d | Kevin Wolf | |
2353 | 83f64091 | bellard | sector_num += nb_sectors; |
2354 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
2355 | 83f64091 | bellard | count -= len; |
2356 | 83f64091 | bellard | } |
2357 | 83f64091 | bellard | |
2358 | 83f64091 | bellard | /* add data from the last sector */
|
2359 | 83f64091 | bellard | if (count > 0) { |
2360 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
2361 | 9a8c4cce | Kevin Wolf | return ret;
|
2362 | 8d3b1a2d | Kevin Wolf | qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); |
2363 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
2364 | 9a8c4cce | Kevin Wolf | return ret;
|
2365 | 83f64091 | bellard | } |
2366 | 8d3b1a2d | Kevin Wolf | return qiov->size;
|
2367 | 8d3b1a2d | Kevin Wolf | } |
2368 | 8d3b1a2d | Kevin Wolf | |
2369 | 8d3b1a2d | Kevin Wolf | int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
2370 | 8d3b1a2d | Kevin Wolf | const void *buf, int count1) |
2371 | 8d3b1a2d | Kevin Wolf | { |
2372 | 8d3b1a2d | Kevin Wolf | QEMUIOVector qiov; |
2373 | 8d3b1a2d | Kevin Wolf | struct iovec iov = {
|
2374 | 8d3b1a2d | Kevin Wolf | .iov_base = (void *) buf,
|
2375 | 8d3b1a2d | Kevin Wolf | .iov_len = count1, |
2376 | 8d3b1a2d | Kevin Wolf | }; |
2377 | 8d3b1a2d | Kevin Wolf | |
2378 | 8d3b1a2d | Kevin Wolf | qemu_iovec_init_external(&qiov, &iov, 1);
|
2379 | 8d3b1a2d | Kevin Wolf | return bdrv_pwritev(bs, offset, &qiov);
|
2380 | 83f64091 | bellard | } |
2381 | 83f64091 | bellard | |
2382 | f08145fe | Kevin Wolf | /*
|
2383 | f08145fe | Kevin Wolf | * Writes to the file and ensures that no writes are reordered across this
|
2384 | f08145fe | Kevin Wolf | * request (acts as a barrier)
|
2385 | f08145fe | Kevin Wolf | *
|
2386 | f08145fe | Kevin Wolf | * Returns 0 on success, -errno in error cases.
|
2387 | f08145fe | Kevin Wolf | */
|
2388 | f08145fe | Kevin Wolf | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
|
2389 | f08145fe | Kevin Wolf | const void *buf, int count) |
2390 | f08145fe | Kevin Wolf | { |
2391 | f08145fe | Kevin Wolf | int ret;
|
2392 | f08145fe | Kevin Wolf | |
2393 | f08145fe | Kevin Wolf | ret = bdrv_pwrite(bs, offset, buf, count); |
2394 | f08145fe | Kevin Wolf | if (ret < 0) { |
2395 | f08145fe | Kevin Wolf | return ret;
|
2396 | f08145fe | Kevin Wolf | } |
2397 | f08145fe | Kevin Wolf | |
2398 | f05fa4ad | Paolo Bonzini | /* No flush needed for cache modes that already do it */
|
2399 | f05fa4ad | Paolo Bonzini | if (bs->enable_write_cache) {
|
2400 | f08145fe | Kevin Wolf | bdrv_flush(bs); |
2401 | f08145fe | Kevin Wolf | } |
2402 | f08145fe | Kevin Wolf | |
2403 | f08145fe | Kevin Wolf | return 0; |
2404 | f08145fe | Kevin Wolf | } |
2405 | f08145fe | Kevin Wolf | |
2406 | 470c0504 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, |
2407 | ab185921 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
2408 | ab185921 | Stefan Hajnoczi | { |
2409 | ab185921 | Stefan Hajnoczi | /* Perform I/O through a temporary buffer so that users who scribble over
|
2410 | ab185921 | Stefan Hajnoczi | * their read buffer while the operation is in progress do not end up
|
2411 | ab185921 | Stefan Hajnoczi | * modifying the image file. This is critical for zero-copy guest I/O
|
2412 | ab185921 | Stefan Hajnoczi | * where anything might happen inside guest memory.
|
2413 | ab185921 | Stefan Hajnoczi | */
|
2414 | ab185921 | Stefan Hajnoczi | void *bounce_buffer;
|
2415 | ab185921 | Stefan Hajnoczi | |
2416 | 79c053bd | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
2417 | ab185921 | Stefan Hajnoczi | struct iovec iov;
|
2418 | ab185921 | Stefan Hajnoczi | QEMUIOVector bounce_qiov; |
2419 | ab185921 | Stefan Hajnoczi | int64_t cluster_sector_num; |
2420 | ab185921 | Stefan Hajnoczi | int cluster_nb_sectors;
|
2421 | ab185921 | Stefan Hajnoczi | size_t skip_bytes; |
2422 | ab185921 | Stefan Hajnoczi | int ret;
|
2423 | ab185921 | Stefan Hajnoczi | |
2424 | ab185921 | Stefan Hajnoczi | /* Cover entire cluster so no additional backing file I/O is required when
|
2425 | ab185921 | Stefan Hajnoczi | * allocating cluster in the image file.
|
2426 | ab185921 | Stefan Hajnoczi | */
|
2427 | 343bded4 | Paolo Bonzini | bdrv_round_to_clusters(bs, sector_num, nb_sectors, |
2428 | 343bded4 | Paolo Bonzini | &cluster_sector_num, &cluster_nb_sectors); |
2429 | ab185921 | Stefan Hajnoczi | |
2430 | 470c0504 | Stefan Hajnoczi | trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, |
2431 | 470c0504 | Stefan Hajnoczi | cluster_sector_num, cluster_nb_sectors); |
2432 | ab185921 | Stefan Hajnoczi | |
2433 | ab185921 | Stefan Hajnoczi | iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; |
2434 | ab185921 | Stefan Hajnoczi | iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); |
2435 | ab185921 | Stefan Hajnoczi | qemu_iovec_init_external(&bounce_qiov, &iov, 1);
|
2436 | ab185921 | Stefan Hajnoczi | |
2437 | 79c053bd | Stefan Hajnoczi | ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, |
2438 | 79c053bd | Stefan Hajnoczi | &bounce_qiov); |
2439 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
2440 | ab185921 | Stefan Hajnoczi | goto err;
|
2441 | ab185921 | Stefan Hajnoczi | } |
2442 | ab185921 | Stefan Hajnoczi | |
2443 | 79c053bd | Stefan Hajnoczi | if (drv->bdrv_co_write_zeroes &&
|
2444 | 79c053bd | Stefan Hajnoczi | buffer_is_zero(bounce_buffer, iov.iov_len)) { |
2445 | 621f0589 | Kevin Wolf | ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, |
2446 | 621f0589 | Kevin Wolf | cluster_nb_sectors); |
2447 | 79c053bd | Stefan Hajnoczi | } else {
|
2448 | f05fa4ad | Paolo Bonzini | /* This does not change the data on the disk, it is not necessary
|
2449 | f05fa4ad | Paolo Bonzini | * to flush even in cache=writethrough mode.
|
2450 | f05fa4ad | Paolo Bonzini | */
|
2451 | 79c053bd | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, |
2452 | ab185921 | Stefan Hajnoczi | &bounce_qiov); |
2453 | 79c053bd | Stefan Hajnoczi | } |
2454 | 79c053bd | Stefan Hajnoczi | |
2455 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
2456 | ab185921 | Stefan Hajnoczi | /* It might be okay to ignore write errors for guest requests. If this
|
2457 | ab185921 | Stefan Hajnoczi | * is a deliberate copy-on-read then we don't want to ignore the error.
|
2458 | ab185921 | Stefan Hajnoczi | * Simply report it in all cases.
|
2459 | ab185921 | Stefan Hajnoczi | */
|
2460 | ab185921 | Stefan Hajnoczi | goto err;
|
2461 | ab185921 | Stefan Hajnoczi | } |
2462 | ab185921 | Stefan Hajnoczi | |
2463 | ab185921 | Stefan Hajnoczi | skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; |
2464 | 03396148 | Michael Tokarev | qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
|
2465 | 03396148 | Michael Tokarev | nb_sectors * BDRV_SECTOR_SIZE); |
2466 | ab185921 | Stefan Hajnoczi | |
2467 | ab185921 | Stefan Hajnoczi | err:
|
2468 | ab185921 | Stefan Hajnoczi | qemu_vfree(bounce_buffer); |
2469 | ab185921 | Stefan Hajnoczi | return ret;
|
2470 | ab185921 | Stefan Hajnoczi | } |
2471 | ab185921 | Stefan Hajnoczi | |
2472 | c5fbe571 | Stefan Hajnoczi | /*
|
2473 | c5fbe571 | Stefan Hajnoczi | * Handle a read request in coroutine context
|
2474 | c5fbe571 | Stefan Hajnoczi | */
|
2475 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
2476 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
2477 | 470c0504 | Stefan Hajnoczi | BdrvRequestFlags flags) |
2478 | da1fa91d | Kevin Wolf | { |
2479 | da1fa91d | Kevin Wolf | BlockDriver *drv = bs->drv; |
2480 | dbffbdcf | Stefan Hajnoczi | BdrvTrackedRequest req; |
2481 | dbffbdcf | Stefan Hajnoczi | int ret;
|
2482 | da1fa91d | Kevin Wolf | |
2483 | da1fa91d | Kevin Wolf | if (!drv) {
|
2484 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
2485 | da1fa91d | Kevin Wolf | } |
2486 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
2487 | da1fa91d | Kevin Wolf | return -EIO;
|
2488 | da1fa91d | Kevin Wolf | } |
2489 | da1fa91d | Kevin Wolf | |
2490 | 98f90dba | Zhi Yong Wu | /* throttling disk read I/O */
|
2491 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
2492 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_intercept(bs, false, nb_sectors);
|
2493 | 98f90dba | Zhi Yong Wu | } |
2494 | 98f90dba | Zhi Yong Wu | |
2495 | f4658285 | Stefan Hajnoczi | if (bs->copy_on_read) {
|
2496 | 470c0504 | Stefan Hajnoczi | flags |= BDRV_REQ_COPY_ON_READ; |
2497 | 470c0504 | Stefan Hajnoczi | } |
2498 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
2499 | 470c0504 | Stefan Hajnoczi | bs->copy_on_read_in_flight++; |
2500 | 470c0504 | Stefan Hajnoczi | } |
2501 | 470c0504 | Stefan Hajnoczi | |
2502 | 470c0504 | Stefan Hajnoczi | if (bs->copy_on_read_in_flight) {
|
2503 | f4658285 | Stefan Hajnoczi | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
2504 | f4658285 | Stefan Hajnoczi | } |
2505 | f4658285 | Stefan Hajnoczi | |
2506 | dbffbdcf | Stefan Hajnoczi | tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
|
2507 | ab185921 | Stefan Hajnoczi | |
2508 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
2509 | ab185921 | Stefan Hajnoczi | int pnum;
|
2510 | ab185921 | Stefan Hajnoczi | |
2511 | ab185921 | Stefan Hajnoczi | ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); |
2512 | ab185921 | Stefan Hajnoczi | if (ret < 0) { |
2513 | ab185921 | Stefan Hajnoczi | goto out;
|
2514 | ab185921 | Stefan Hajnoczi | } |
2515 | ab185921 | Stefan Hajnoczi | |
2516 | ab185921 | Stefan Hajnoczi | if (!ret || pnum != nb_sectors) {
|
2517 | 470c0504 | Stefan Hajnoczi | ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); |
2518 | ab185921 | Stefan Hajnoczi | goto out;
|
2519 | ab185921 | Stefan Hajnoczi | } |
2520 | ab185921 | Stefan Hajnoczi | } |
2521 | ab185921 | Stefan Hajnoczi | |
2522 | dbffbdcf | Stefan Hajnoczi | ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); |
2523 | ab185921 | Stefan Hajnoczi | |
2524 | ab185921 | Stefan Hajnoczi | out:
|
2525 | dbffbdcf | Stefan Hajnoczi | tracked_request_end(&req); |
2526 | 470c0504 | Stefan Hajnoczi | |
2527 | 470c0504 | Stefan Hajnoczi | if (flags & BDRV_REQ_COPY_ON_READ) {
|
2528 | 470c0504 | Stefan Hajnoczi | bs->copy_on_read_in_flight--; |
2529 | 470c0504 | Stefan Hajnoczi | } |
2530 | 470c0504 | Stefan Hajnoczi | |
2531 | dbffbdcf | Stefan Hajnoczi | return ret;
|
2532 | da1fa91d | Kevin Wolf | } |
2533 | da1fa91d | Kevin Wolf | |
2534 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
|
2535 | da1fa91d | Kevin Wolf | int nb_sectors, QEMUIOVector *qiov)
|
2536 | da1fa91d | Kevin Wolf | { |
2537 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_readv(bs, sector_num, nb_sectors); |
2538 | da1fa91d | Kevin Wolf | |
2539 | 470c0504 | Stefan Hajnoczi | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); |
2540 | 470c0504 | Stefan Hajnoczi | } |
2541 | 470c0504 | Stefan Hajnoczi | |
2542 | 470c0504 | Stefan Hajnoczi | int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
|
2543 | 470c0504 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
2544 | 470c0504 | Stefan Hajnoczi | { |
2545 | 470c0504 | Stefan Hajnoczi | trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); |
2546 | 470c0504 | Stefan Hajnoczi | |
2547 | 470c0504 | Stefan Hajnoczi | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
|
2548 | 470c0504 | Stefan Hajnoczi | BDRV_REQ_COPY_ON_READ); |
2549 | c5fbe571 | Stefan Hajnoczi | } |
2550 | c5fbe571 | Stefan Hajnoczi | |
2551 | f08f2dda | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, |
2552 | f08f2dda | Stefan Hajnoczi | int64_t sector_num, int nb_sectors)
|
2553 | f08f2dda | Stefan Hajnoczi | { |
2554 | f08f2dda | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
2555 | f08f2dda | Stefan Hajnoczi | QEMUIOVector qiov; |
2556 | f08f2dda | Stefan Hajnoczi | struct iovec iov;
|
2557 | f08f2dda | Stefan Hajnoczi | int ret;
|
2558 | f08f2dda | Stefan Hajnoczi | |
2559 | 621f0589 | Kevin Wolf | /* TODO Emulate only part of misaligned requests instead of letting block
|
2560 | 621f0589 | Kevin Wolf | * drivers return -ENOTSUP and emulate everything */
|
2561 | 621f0589 | Kevin Wolf | |
2562 | f08f2dda | Stefan Hajnoczi | /* First try the efficient write zeroes operation */
|
2563 | f08f2dda | Stefan Hajnoczi | if (drv->bdrv_co_write_zeroes) {
|
2564 | 621f0589 | Kevin Wolf | ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); |
2565 | 621f0589 | Kevin Wolf | if (ret != -ENOTSUP) {
|
2566 | 621f0589 | Kevin Wolf | return ret;
|
2567 | 621f0589 | Kevin Wolf | } |
2568 | f08f2dda | Stefan Hajnoczi | } |
2569 | f08f2dda | Stefan Hajnoczi | |
2570 | f08f2dda | Stefan Hajnoczi | /* Fall back to bounce buffer if write zeroes is unsupported */
|
2571 | f08f2dda | Stefan Hajnoczi | iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; |
2572 | f08f2dda | Stefan Hajnoczi | iov.iov_base = qemu_blockalign(bs, iov.iov_len); |
2573 | f08f2dda | Stefan Hajnoczi | memset(iov.iov_base, 0, iov.iov_len);
|
2574 | f08f2dda | Stefan Hajnoczi | qemu_iovec_init_external(&qiov, &iov, 1);
|
2575 | f08f2dda | Stefan Hajnoczi | |
2576 | f08f2dda | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); |
2577 | f08f2dda | Stefan Hajnoczi | |
2578 | f08f2dda | Stefan Hajnoczi | qemu_vfree(iov.iov_base); |
2579 | f08f2dda | Stefan Hajnoczi | return ret;
|
2580 | f08f2dda | Stefan Hajnoczi | } |
2581 | f08f2dda | Stefan Hajnoczi | |
2582 | c5fbe571 | Stefan Hajnoczi | /*
|
2583 | c5fbe571 | Stefan Hajnoczi | * Handle a write request in coroutine context
|
2584 | c5fbe571 | Stefan Hajnoczi | */
|
2585 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
2586 | f08f2dda | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
|
2587 | f08f2dda | Stefan Hajnoczi | BdrvRequestFlags flags) |
2588 | c5fbe571 | Stefan Hajnoczi | { |
2589 | c5fbe571 | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
2590 | dbffbdcf | Stefan Hajnoczi | BdrvTrackedRequest req; |
2591 | 6b7cb247 | Stefan Hajnoczi | int ret;
|
2592 | da1fa91d | Kevin Wolf | |
2593 | da1fa91d | Kevin Wolf | if (!bs->drv) {
|
2594 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
2595 | da1fa91d | Kevin Wolf | } |
2596 | da1fa91d | Kevin Wolf | if (bs->read_only) {
|
2597 | da1fa91d | Kevin Wolf | return -EACCES;
|
2598 | da1fa91d | Kevin Wolf | } |
2599 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
2600 | da1fa91d | Kevin Wolf | return -EIO;
|
2601 | da1fa91d | Kevin Wolf | } |
2602 | da1fa91d | Kevin Wolf | |
2603 | 98f90dba | Zhi Yong Wu | /* throttling disk write I/O */
|
2604 | 98f90dba | Zhi Yong Wu | if (bs->io_limits_enabled) {
|
2605 | 98f90dba | Zhi Yong Wu | bdrv_io_limits_intercept(bs, true, nb_sectors);
|
2606 | 98f90dba | Zhi Yong Wu | } |
2607 | 98f90dba | Zhi Yong Wu | |
2608 | 470c0504 | Stefan Hajnoczi | if (bs->copy_on_read_in_flight) {
|
2609 | f4658285 | Stefan Hajnoczi | wait_for_overlapping_requests(bs, sector_num, nb_sectors); |
2610 | f4658285 | Stefan Hajnoczi | } |
2611 | f4658285 | Stefan Hajnoczi | |
2612 | dbffbdcf | Stefan Hajnoczi | tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
|
2613 | dbffbdcf | Stefan Hajnoczi | |
2614 | d616b224 | Stefan Hajnoczi | ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); |
2615 | d616b224 | Stefan Hajnoczi | |
2616 | d616b224 | Stefan Hajnoczi | if (ret < 0) { |
2617 | d616b224 | Stefan Hajnoczi | /* Do nothing, write notifier decided to fail this request */
|
2618 | d616b224 | Stefan Hajnoczi | } else if (flags & BDRV_REQ_ZERO_WRITE) { |
2619 | f08f2dda | Stefan Hajnoczi | ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); |
2620 | f08f2dda | Stefan Hajnoczi | } else {
|
2621 | f08f2dda | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); |
2622 | f08f2dda | Stefan Hajnoczi | } |
2623 | 6b7cb247 | Stefan Hajnoczi | |
2624 | f05fa4ad | Paolo Bonzini | if (ret == 0 && !bs->enable_write_cache) { |
2625 | f05fa4ad | Paolo Bonzini | ret = bdrv_co_flush(bs); |
2626 | f05fa4ad | Paolo Bonzini | } |
2627 | f05fa4ad | Paolo Bonzini | |
2628 | da1fa91d | Kevin Wolf | if (bs->dirty_bitmap) {
|
2629 | 1755da16 | Paolo Bonzini | bdrv_set_dirty(bs, sector_num, nb_sectors); |
2630 | da1fa91d | Kevin Wolf | } |
2631 | da1fa91d | Kevin Wolf | |
2632 | da1fa91d | Kevin Wolf | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
2633 | da1fa91d | Kevin Wolf | bs->wr_highest_sector = sector_num + nb_sectors - 1;
|
2634 | da1fa91d | Kevin Wolf | } |
2635 | da1fa91d | Kevin Wolf | |
2636 | dbffbdcf | Stefan Hajnoczi | tracked_request_end(&req); |
2637 | dbffbdcf | Stefan Hajnoczi | |
2638 | 6b7cb247 | Stefan Hajnoczi | return ret;
|
2639 | da1fa91d | Kevin Wolf | } |
2640 | da1fa91d | Kevin Wolf | |
2641 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
|
2642 | c5fbe571 | Stefan Hajnoczi | int nb_sectors, QEMUIOVector *qiov)
|
2643 | c5fbe571 | Stefan Hajnoczi | { |
2644 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_writev(bs, sector_num, nb_sectors); |
2645 | c5fbe571 | Stefan Hajnoczi | |
2646 | f08f2dda | Stefan Hajnoczi | return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); |
2647 | f08f2dda | Stefan Hajnoczi | } |
2648 | f08f2dda | Stefan Hajnoczi | |
2649 | f08f2dda | Stefan Hajnoczi | int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
|
2650 | f08f2dda | Stefan Hajnoczi | int64_t sector_num, int nb_sectors)
|
2651 | f08f2dda | Stefan Hajnoczi | { |
2652 | f08f2dda | Stefan Hajnoczi | trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); |
2653 | f08f2dda | Stefan Hajnoczi | |
2654 | f08f2dda | Stefan Hajnoczi | return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, |
2655 | f08f2dda | Stefan Hajnoczi | BDRV_REQ_ZERO_WRITE); |
2656 | c5fbe571 | Stefan Hajnoczi | } |
2657 | c5fbe571 | Stefan Hajnoczi | |
2658 | 83f64091 | bellard | /**
|
2659 | 83f64091 | bellard | * Truncate file to 'offset' bytes (needed only for file protocols)
|
2660 | 83f64091 | bellard | */
|
2661 | 83f64091 | bellard | int bdrv_truncate(BlockDriverState *bs, int64_t offset)
|
2662 | 83f64091 | bellard | { |
2663 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
2664 | 51762288 | Stefan Hajnoczi | int ret;
|
2665 | 83f64091 | bellard | if (!drv)
|
2666 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2667 | 83f64091 | bellard | if (!drv->bdrv_truncate)
|
2668 | 83f64091 | bellard | return -ENOTSUP;
|
2669 | 59f2689d | Naphtali Sprei | if (bs->read_only)
|
2670 | 59f2689d | Naphtali Sprei | return -EACCES;
|
2671 | 8591675f | Marcelo Tosatti | if (bdrv_in_use(bs))
|
2672 | 8591675f | Marcelo Tosatti | return -EBUSY;
|
2673 | 51762288 | Stefan Hajnoczi | ret = drv->bdrv_truncate(bs, offset); |
2674 | 51762288 | Stefan Hajnoczi | if (ret == 0) { |
2675 | 51762288 | Stefan Hajnoczi | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
2676 | 145feb17 | Markus Armbruster | bdrv_dev_resize_cb(bs); |
2677 | 51762288 | Stefan Hajnoczi | } |
2678 | 51762288 | Stefan Hajnoczi | return ret;
|
2679 | 83f64091 | bellard | } |
2680 | 83f64091 | bellard | |
2681 | 83f64091 | bellard | /**
|
2682 | 4a1d5e1f | Fam Zheng | * Length of a allocated file in bytes. Sparse files are counted by actual
|
2683 | 4a1d5e1f | Fam Zheng | * allocated space. Return < 0 if error or unknown.
|
2684 | 4a1d5e1f | Fam Zheng | */
|
2685 | 4a1d5e1f | Fam Zheng | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) |
2686 | 4a1d5e1f | Fam Zheng | { |
2687 | 4a1d5e1f | Fam Zheng | BlockDriver *drv = bs->drv; |
2688 | 4a1d5e1f | Fam Zheng | if (!drv) {
|
2689 | 4a1d5e1f | Fam Zheng | return -ENOMEDIUM;
|
2690 | 4a1d5e1f | Fam Zheng | } |
2691 | 4a1d5e1f | Fam Zheng | if (drv->bdrv_get_allocated_file_size) {
|
2692 | 4a1d5e1f | Fam Zheng | return drv->bdrv_get_allocated_file_size(bs);
|
2693 | 4a1d5e1f | Fam Zheng | } |
2694 | 4a1d5e1f | Fam Zheng | if (bs->file) {
|
2695 | 4a1d5e1f | Fam Zheng | return bdrv_get_allocated_file_size(bs->file);
|
2696 | 4a1d5e1f | Fam Zheng | } |
2697 | 4a1d5e1f | Fam Zheng | return -ENOTSUP;
|
2698 | 4a1d5e1f | Fam Zheng | } |
2699 | 4a1d5e1f | Fam Zheng | |
2700 | 4a1d5e1f | Fam Zheng | /**
|
2701 | 83f64091 | bellard | * Length of a file in bytes. Return < 0 if error or unknown.
|
2702 | 83f64091 | bellard | */
|
2703 | 83f64091 | bellard | int64_t bdrv_getlength(BlockDriverState *bs) |
2704 | 83f64091 | bellard | { |
2705 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
2706 | 83f64091 | bellard | if (!drv)
|
2707 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2708 | 51762288 | Stefan Hajnoczi | |
2709 | 2c6942fa | Markus Armbruster | if (bs->growable || bdrv_dev_has_removable_media(bs)) {
|
2710 | 46a4e4e6 | Stefan Hajnoczi | if (drv->bdrv_getlength) {
|
2711 | 46a4e4e6 | Stefan Hajnoczi | return drv->bdrv_getlength(bs);
|
2712 | 46a4e4e6 | Stefan Hajnoczi | } |
2713 | 83f64091 | bellard | } |
2714 | 46a4e4e6 | Stefan Hajnoczi | return bs->total_sectors * BDRV_SECTOR_SIZE;
|
2715 | fc01f7e7 | bellard | } |
2716 | fc01f7e7 | bellard | |
2717 | 19cb3738 | bellard | /* return 0 as number of sectors if no device present or error */
|
2718 | 96b8f136 | ths | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
|
2719 | fc01f7e7 | bellard | { |
2720 | 19cb3738 | bellard | int64_t length; |
2721 | 19cb3738 | bellard | length = bdrv_getlength(bs); |
2722 | 19cb3738 | bellard | if (length < 0) |
2723 | 19cb3738 | bellard | length = 0;
|
2724 | 19cb3738 | bellard | else
|
2725 | 6ea44308 | Jan Kiszka | length = length >> BDRV_SECTOR_BITS; |
2726 | 19cb3738 | bellard | *nb_sectors_ptr = length; |
2727 | fc01f7e7 | bellard | } |
2728 | cf98951b | bellard | |
2729 | 0563e191 | Zhi Yong Wu | /* throttling disk io limits */
|
2730 | 0563e191 | Zhi Yong Wu | void bdrv_set_io_limits(BlockDriverState *bs,
|
2731 | 0563e191 | Zhi Yong Wu | BlockIOLimit *io_limits) |
2732 | 0563e191 | Zhi Yong Wu | { |
2733 | 0563e191 | Zhi Yong Wu | bs->io_limits = *io_limits; |
2734 | 0563e191 | Zhi Yong Wu | bs->io_limits_enabled = bdrv_io_limits_enabled(bs); |
2735 | 0563e191 | Zhi Yong Wu | } |
2736 | 0563e191 | Zhi Yong Wu | |
2737 | ff06f5f3 | Paolo Bonzini | void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
|
2738 | ff06f5f3 | Paolo Bonzini | BlockdevOnError on_write_error) |
2739 | abd7f68d | Markus Armbruster | { |
2740 | abd7f68d | Markus Armbruster | bs->on_read_error = on_read_error; |
2741 | abd7f68d | Markus Armbruster | bs->on_write_error = on_write_error; |
2742 | abd7f68d | Markus Armbruster | } |
2743 | abd7f68d | Markus Armbruster | |
2744 | 1ceee0d5 | Paolo Bonzini | BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read)
|
2745 | abd7f68d | Markus Armbruster | { |
2746 | abd7f68d | Markus Armbruster | return is_read ? bs->on_read_error : bs->on_write_error;
|
2747 | abd7f68d | Markus Armbruster | } |
2748 | abd7f68d | Markus Armbruster | |
2749 | 3e1caa5f | Paolo Bonzini | BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) |
2750 | 3e1caa5f | Paolo Bonzini | { |
2751 | 3e1caa5f | Paolo Bonzini | BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; |
2752 | 3e1caa5f | Paolo Bonzini | |
2753 | 3e1caa5f | Paolo Bonzini | switch (on_err) {
|
2754 | 3e1caa5f | Paolo Bonzini | case BLOCKDEV_ON_ERROR_ENOSPC:
|
2755 | 3e1caa5f | Paolo Bonzini | return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT;
|
2756 | 3e1caa5f | Paolo Bonzini | case BLOCKDEV_ON_ERROR_STOP:
|
2757 | 3e1caa5f | Paolo Bonzini | return BDRV_ACTION_STOP;
|
2758 | 3e1caa5f | Paolo Bonzini | case BLOCKDEV_ON_ERROR_REPORT:
|
2759 | 3e1caa5f | Paolo Bonzini | return BDRV_ACTION_REPORT;
|
2760 | 3e1caa5f | Paolo Bonzini | case BLOCKDEV_ON_ERROR_IGNORE:
|
2761 | 3e1caa5f | Paolo Bonzini | return BDRV_ACTION_IGNORE;
|
2762 | 3e1caa5f | Paolo Bonzini | default:
|
2763 | 3e1caa5f | Paolo Bonzini | abort(); |
2764 | 3e1caa5f | Paolo Bonzini | } |
2765 | 3e1caa5f | Paolo Bonzini | } |
2766 | 3e1caa5f | Paolo Bonzini | |
2767 | 3e1caa5f | Paolo Bonzini | /* This is done by device models because, while the block layer knows
|
2768 | 3e1caa5f | Paolo Bonzini | * about the error, it does not know whether an operation comes from
|
2769 | 3e1caa5f | Paolo Bonzini | * the device or the block layer (from a job, for example).
|
2770 | 3e1caa5f | Paolo Bonzini | */
|
2771 | 3e1caa5f | Paolo Bonzini | void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
|
2772 | 3e1caa5f | Paolo Bonzini | bool is_read, int error) |
2773 | 3e1caa5f | Paolo Bonzini | { |
2774 | 3e1caa5f | Paolo Bonzini | assert(error >= 0);
|
2775 | 32c81a4a | Paolo Bonzini | bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); |
2776 | 3e1caa5f | Paolo Bonzini | if (action == BDRV_ACTION_STOP) {
|
2777 | 3e1caa5f | Paolo Bonzini | vm_stop(RUN_STATE_IO_ERROR); |
2778 | 3e1caa5f | Paolo Bonzini | bdrv_iostatus_set_err(bs, error); |
2779 | 3e1caa5f | Paolo Bonzini | } |
2780 | 3e1caa5f | Paolo Bonzini | } |
2781 | 3e1caa5f | Paolo Bonzini | |
2782 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
2783 | b338082b | bellard | { |
2784 | b338082b | bellard | return bs->read_only;
|
2785 | b338082b | bellard | } |
2786 | b338082b | bellard | |
2787 | 985a03b0 | ths | int bdrv_is_sg(BlockDriverState *bs)
|
2788 | 985a03b0 | ths | { |
2789 | 985a03b0 | ths | return bs->sg;
|
2790 | 985a03b0 | ths | } |
2791 | 985a03b0 | ths | |
2792 | e900a7b7 | Christoph Hellwig | int bdrv_enable_write_cache(BlockDriverState *bs)
|
2793 | e900a7b7 | Christoph Hellwig | { |
2794 | e900a7b7 | Christoph Hellwig | return bs->enable_write_cache;
|
2795 | e900a7b7 | Christoph Hellwig | } |
2796 | e900a7b7 | Christoph Hellwig | |
2797 | 425b0148 | Paolo Bonzini | void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) |
2798 | 425b0148 | Paolo Bonzini | { |
2799 | 425b0148 | Paolo Bonzini | bs->enable_write_cache = wce; |
2800 | 55b110f2 | Jeff Cody | |
2801 | 55b110f2 | Jeff Cody | /* so a reopen() will preserve wce */
|
2802 | 55b110f2 | Jeff Cody | if (wce) {
|
2803 | 55b110f2 | Jeff Cody | bs->open_flags |= BDRV_O_CACHE_WB; |
2804 | 55b110f2 | Jeff Cody | } else {
|
2805 | 55b110f2 | Jeff Cody | bs->open_flags &= ~BDRV_O_CACHE_WB; |
2806 | 55b110f2 | Jeff Cody | } |
2807 | 425b0148 | Paolo Bonzini | } |
2808 | 425b0148 | Paolo Bonzini | |
2809 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
2810 | ea2384d3 | bellard | { |
2811 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
2812 | ea2384d3 | bellard | return 1; |
2813 | ea2384d3 | bellard | return bs->encrypted;
|
2814 | ea2384d3 | bellard | } |
2815 | ea2384d3 | bellard | |
2816 | c0f4ce77 | aliguori | int bdrv_key_required(BlockDriverState *bs)
|
2817 | c0f4ce77 | aliguori | { |
2818 | c0f4ce77 | aliguori | BlockDriverState *backing_hd = bs->backing_hd; |
2819 | c0f4ce77 | aliguori | |
2820 | c0f4ce77 | aliguori | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
|
2821 | c0f4ce77 | aliguori | return 1; |
2822 | c0f4ce77 | aliguori | return (bs->encrypted && !bs->valid_key);
|
2823 | c0f4ce77 | aliguori | } |
2824 | c0f4ce77 | aliguori | |
2825 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
2826 | ea2384d3 | bellard | { |
2827 | ea2384d3 | bellard | int ret;
|
2828 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
2829 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
2830 | ea2384d3 | bellard | if (ret < 0) |
2831 | ea2384d3 | bellard | return ret;
|
2832 | ea2384d3 | bellard | if (!bs->encrypted)
|
2833 | ea2384d3 | bellard | return 0; |
2834 | ea2384d3 | bellard | } |
2835 | fd04a2ae | Shahar Havivi | if (!bs->encrypted) {
|
2836 | fd04a2ae | Shahar Havivi | return -EINVAL;
|
2837 | fd04a2ae | Shahar Havivi | } else if (!bs->drv || !bs->drv->bdrv_set_key) { |
2838 | fd04a2ae | Shahar Havivi | return -ENOMEDIUM;
|
2839 | fd04a2ae | Shahar Havivi | } |
2840 | c0f4ce77 | aliguori | ret = bs->drv->bdrv_set_key(bs, key); |
2841 | bb5fc20f | aliguori | if (ret < 0) { |
2842 | bb5fc20f | aliguori | bs->valid_key = 0;
|
2843 | bb5fc20f | aliguori | } else if (!bs->valid_key) { |
2844 | bb5fc20f | aliguori | bs->valid_key = 1;
|
2845 | bb5fc20f | aliguori | /* call the change callback now, we skipped it on open */
|
2846 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, true);
|
2847 | bb5fc20f | aliguori | } |
2848 | c0f4ce77 | aliguori | return ret;
|
2849 | ea2384d3 | bellard | } |
2850 | ea2384d3 | bellard | |
2851 | f8d6bba1 | Markus Armbruster | const char *bdrv_get_format_name(BlockDriverState *bs) |
2852 | ea2384d3 | bellard | { |
2853 | f8d6bba1 | Markus Armbruster | return bs->drv ? bs->drv->format_name : NULL; |
2854 | ea2384d3 | bellard | } |
2855 | ea2384d3 | bellard | |
2856 | 5fafdf24 | ths | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
2857 | ea2384d3 | bellard | void *opaque)
|
2858 | ea2384d3 | bellard | { |
2859 | ea2384d3 | bellard | BlockDriver *drv; |
2860 | ea2384d3 | bellard | |
2861 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
2862 | ea2384d3 | bellard | it(opaque, drv->format_name); |
2863 | ea2384d3 | bellard | } |
2864 | ea2384d3 | bellard | } |
2865 | ea2384d3 | bellard | |
2866 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
2867 | b338082b | bellard | { |
2868 | b338082b | bellard | BlockDriverState *bs; |
2869 | b338082b | bellard | |
2870 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2871 | 1b7bdbc1 | Stefan Hajnoczi | if (!strcmp(name, bs->device_name)) {
|
2872 | b338082b | bellard | return bs;
|
2873 | 1b7bdbc1 | Stefan Hajnoczi | } |
2874 | b338082b | bellard | } |
2875 | b338082b | bellard | return NULL; |
2876 | b338082b | bellard | } |
2877 | b338082b | bellard | |
2878 | 2f399b0a | Markus Armbruster | BlockDriverState *bdrv_next(BlockDriverState *bs) |
2879 | 2f399b0a | Markus Armbruster | { |
2880 | 2f399b0a | Markus Armbruster | if (!bs) {
|
2881 | 2f399b0a | Markus Armbruster | return QTAILQ_FIRST(&bdrv_states);
|
2882 | 2f399b0a | Markus Armbruster | } |
2883 | 2f399b0a | Markus Armbruster | return QTAILQ_NEXT(bs, list);
|
2884 | 2f399b0a | Markus Armbruster | } |
2885 | 2f399b0a | Markus Armbruster | |
2886 | 51de9760 | aliguori | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
2887 | 81d0912d | bellard | { |
2888 | 81d0912d | bellard | BlockDriverState *bs; |
2889 | 81d0912d | bellard | |
2890 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2891 | 51de9760 | aliguori | it(opaque, bs); |
2892 | 81d0912d | bellard | } |
2893 | 81d0912d | bellard | } |
2894 | 81d0912d | bellard | |
2895 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
2896 | ea2384d3 | bellard | { |
2897 | ea2384d3 | bellard | return bs->device_name;
|
2898 | ea2384d3 | bellard | } |
2899 | ea2384d3 | bellard | |
2900 | c8433287 | Markus Armbruster | int bdrv_get_flags(BlockDriverState *bs)
|
2901 | c8433287 | Markus Armbruster | { |
2902 | c8433287 | Markus Armbruster | return bs->open_flags;
|
2903 | c8433287 | Markus Armbruster | } |
2904 | c8433287 | Markus Armbruster | |
2905 | c6ca28d6 | aliguori | void bdrv_flush_all(void) |
2906 | c6ca28d6 | aliguori | { |
2907 | c6ca28d6 | aliguori | BlockDriverState *bs; |
2908 | c6ca28d6 | aliguori | |
2909 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2910 | 29cdb251 | Paolo Bonzini | bdrv_flush(bs); |
2911 | 1b7bdbc1 | Stefan Hajnoczi | } |
2912 | c6ca28d6 | aliguori | } |
2913 | c6ca28d6 | aliguori | |
2914 | f2feebbd | Kevin Wolf | int bdrv_has_zero_init(BlockDriverState *bs)
|
2915 | f2feebbd | Kevin Wolf | { |
2916 | f2feebbd | Kevin Wolf | assert(bs->drv); |
2917 | f2feebbd | Kevin Wolf | |
2918 | 336c1c12 | Kevin Wolf | if (bs->drv->bdrv_has_zero_init) {
|
2919 | 336c1c12 | Kevin Wolf | return bs->drv->bdrv_has_zero_init(bs);
|
2920 | f2feebbd | Kevin Wolf | } |
2921 | f2feebbd | Kevin Wolf | |
2922 | f2feebbd | Kevin Wolf | return 1; |
2923 | f2feebbd | Kevin Wolf | } |
2924 | f2feebbd | Kevin Wolf | |
2925 | 376ae3f1 | Stefan Hajnoczi | typedef struct BdrvCoIsAllocatedData { |
2926 | 376ae3f1 | Stefan Hajnoczi | BlockDriverState *bs; |
2927 | b35b2bba | Miroslav Rezanina | BlockDriverState *base; |
2928 | 376ae3f1 | Stefan Hajnoczi | int64_t sector_num; |
2929 | 376ae3f1 | Stefan Hajnoczi | int nb_sectors;
|
2930 | 376ae3f1 | Stefan Hajnoczi | int *pnum;
|
2931 | 376ae3f1 | Stefan Hajnoczi | int ret;
|
2932 | 376ae3f1 | Stefan Hajnoczi | bool done;
|
2933 | 376ae3f1 | Stefan Hajnoczi | } BdrvCoIsAllocatedData; |
2934 | 376ae3f1 | Stefan Hajnoczi | |
2935 | f58c7b35 | ths | /*
|
2936 | f58c7b35 | ths | * Returns true iff the specified sector is present in the disk image. Drivers
|
2937 | f58c7b35 | ths | * not implementing the functionality are assumed to not support backing files,
|
2938 | f58c7b35 | ths | * hence all their sectors are reported as allocated.
|
2939 | f58c7b35 | ths | *
|
2940 | bd9533e3 | Stefan Hajnoczi | * If 'sector_num' is beyond the end of the disk image the return value is 0
|
2941 | bd9533e3 | Stefan Hajnoczi | * and 'pnum' is set to 0.
|
2942 | bd9533e3 | Stefan Hajnoczi | *
|
2943 | f58c7b35 | ths | * 'pnum' is set to the number of sectors (including and immediately following
|
2944 | f58c7b35 | ths | * the specified sector) that are known to be in the same
|
2945 | f58c7b35 | ths | * allocated/unallocated state.
|
2946 | f58c7b35 | ths | *
|
2947 | bd9533e3 | Stefan Hajnoczi | * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes
|
2948 | bd9533e3 | Stefan Hajnoczi | * beyond the end of the disk image it will be clamped.
|
2949 | f58c7b35 | ths | */
|
2950 | 060f51c9 | Stefan Hajnoczi | int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
|
2951 | 060f51c9 | Stefan Hajnoczi | int nb_sectors, int *pnum) |
2952 | f58c7b35 | ths | { |
2953 | bd9533e3 | Stefan Hajnoczi | int64_t n; |
2954 | bd9533e3 | Stefan Hajnoczi | |
2955 | bd9533e3 | Stefan Hajnoczi | if (sector_num >= bs->total_sectors) {
|
2956 | bd9533e3 | Stefan Hajnoczi | *pnum = 0;
|
2957 | bd9533e3 | Stefan Hajnoczi | return 0; |
2958 | bd9533e3 | Stefan Hajnoczi | } |
2959 | bd9533e3 | Stefan Hajnoczi | |
2960 | bd9533e3 | Stefan Hajnoczi | n = bs->total_sectors - sector_num; |
2961 | bd9533e3 | Stefan Hajnoczi | if (n < nb_sectors) {
|
2962 | bd9533e3 | Stefan Hajnoczi | nb_sectors = n; |
2963 | bd9533e3 | Stefan Hajnoczi | } |
2964 | bd9533e3 | Stefan Hajnoczi | |
2965 | 6aebab14 | Stefan Hajnoczi | if (!bs->drv->bdrv_co_is_allocated) {
|
2966 | bd9533e3 | Stefan Hajnoczi | *pnum = nb_sectors; |
2967 | f58c7b35 | ths | return 1; |
2968 | f58c7b35 | ths | } |
2969 | 6aebab14 | Stefan Hajnoczi | |
2970 | 060f51c9 | Stefan Hajnoczi | return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum);
|
2971 | 060f51c9 | Stefan Hajnoczi | } |
2972 | 060f51c9 | Stefan Hajnoczi | |
2973 | 060f51c9 | Stefan Hajnoczi | /* Coroutine wrapper for bdrv_is_allocated() */
|
2974 | 060f51c9 | Stefan Hajnoczi | static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) |
2975 | 060f51c9 | Stefan Hajnoczi | { |
2976 | 060f51c9 | Stefan Hajnoczi | BdrvCoIsAllocatedData *data = opaque; |
2977 | 060f51c9 | Stefan Hajnoczi | BlockDriverState *bs = data->bs; |
2978 | 060f51c9 | Stefan Hajnoczi | |
2979 | 060f51c9 | Stefan Hajnoczi | data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, |
2980 | 060f51c9 | Stefan Hajnoczi | data->pnum); |
2981 | 060f51c9 | Stefan Hajnoczi | data->done = true;
|
2982 | 060f51c9 | Stefan Hajnoczi | } |
2983 | 060f51c9 | Stefan Hajnoczi | |
2984 | 060f51c9 | Stefan Hajnoczi | /*
|
2985 | 060f51c9 | Stefan Hajnoczi | * Synchronous wrapper around bdrv_co_is_allocated().
|
2986 | 060f51c9 | Stefan Hajnoczi | *
|
2987 | 060f51c9 | Stefan Hajnoczi | * See bdrv_co_is_allocated() for details.
|
2988 | 060f51c9 | Stefan Hajnoczi | */
|
2989 | 060f51c9 | Stefan Hajnoczi | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
2990 | 060f51c9 | Stefan Hajnoczi | int *pnum)
|
2991 | 060f51c9 | Stefan Hajnoczi | { |
2992 | 6aebab14 | Stefan Hajnoczi | Coroutine *co; |
2993 | 6aebab14 | Stefan Hajnoczi | BdrvCoIsAllocatedData data = { |
2994 | 6aebab14 | Stefan Hajnoczi | .bs = bs, |
2995 | 6aebab14 | Stefan Hajnoczi | .sector_num = sector_num, |
2996 | 6aebab14 | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
2997 | 6aebab14 | Stefan Hajnoczi | .pnum = pnum, |
2998 | 6aebab14 | Stefan Hajnoczi | .done = false,
|
2999 | 6aebab14 | Stefan Hajnoczi | }; |
3000 | 6aebab14 | Stefan Hajnoczi | |
3001 | 6aebab14 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_is_allocated_co_entry); |
3002 | 6aebab14 | Stefan Hajnoczi | qemu_coroutine_enter(co, &data); |
3003 | 6aebab14 | Stefan Hajnoczi | while (!data.done) {
|
3004 | 6aebab14 | Stefan Hajnoczi | qemu_aio_wait(); |
3005 | 6aebab14 | Stefan Hajnoczi | } |
3006 | 6aebab14 | Stefan Hajnoczi | return data.ret;
|
3007 | f58c7b35 | ths | } |
3008 | f58c7b35 | ths | |
3009 | 188a7bbf | Paolo Bonzini | /*
|
3010 | 188a7bbf | Paolo Bonzini | * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
|
3011 | 188a7bbf | Paolo Bonzini | *
|
3012 | 188a7bbf | Paolo Bonzini | * Return true if the given sector is allocated in any image between
|
3013 | 188a7bbf | Paolo Bonzini | * BASE and TOP (inclusive). BASE can be NULL to check if the given
|
3014 | 188a7bbf | Paolo Bonzini | * sector is allocated in any image of the chain. Return false otherwise.
|
3015 | 188a7bbf | Paolo Bonzini | *
|
3016 | 188a7bbf | Paolo Bonzini | * 'pnum' is set to the number of sectors (including and immediately following
|
3017 | 188a7bbf | Paolo Bonzini | * the specified sector) that are known to be in the same
|
3018 | 188a7bbf | Paolo Bonzini | * allocated/unallocated state.
|
3019 | 188a7bbf | Paolo Bonzini | *
|
3020 | 188a7bbf | Paolo Bonzini | */
|
3021 | 188a7bbf | Paolo Bonzini | int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
|
3022 | 188a7bbf | Paolo Bonzini | BlockDriverState *base, |
3023 | 188a7bbf | Paolo Bonzini | int64_t sector_num, |
3024 | 188a7bbf | Paolo Bonzini | int nb_sectors, int *pnum) |
3025 | 188a7bbf | Paolo Bonzini | { |
3026 | 188a7bbf | Paolo Bonzini | BlockDriverState *intermediate; |
3027 | 188a7bbf | Paolo Bonzini | int ret, n = nb_sectors;
|
3028 | 188a7bbf | Paolo Bonzini | |
3029 | 188a7bbf | Paolo Bonzini | intermediate = top; |
3030 | 188a7bbf | Paolo Bonzini | while (intermediate && intermediate != base) {
|
3031 | 188a7bbf | Paolo Bonzini | int pnum_inter;
|
3032 | 188a7bbf | Paolo Bonzini | ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, |
3033 | 188a7bbf | Paolo Bonzini | &pnum_inter); |
3034 | 188a7bbf | Paolo Bonzini | if (ret < 0) { |
3035 | 188a7bbf | Paolo Bonzini | return ret;
|
3036 | 188a7bbf | Paolo Bonzini | } else if (ret) { |
3037 | 188a7bbf | Paolo Bonzini | *pnum = pnum_inter; |
3038 | 188a7bbf | Paolo Bonzini | return 1; |
3039 | 188a7bbf | Paolo Bonzini | } |
3040 | 188a7bbf | Paolo Bonzini | |
3041 | 188a7bbf | Paolo Bonzini | /*
|
3042 | 188a7bbf | Paolo Bonzini | * [sector_num, nb_sectors] is unallocated on top but intermediate
|
3043 | 188a7bbf | Paolo Bonzini | * might have
|
3044 | 188a7bbf | Paolo Bonzini | *
|
3045 | 188a7bbf | Paolo Bonzini | * [sector_num+x, nr_sectors] allocated.
|
3046 | 188a7bbf | Paolo Bonzini | */
|
3047 | 63ba17d3 | Vishvananda Ishaya | if (n > pnum_inter &&
|
3048 | 63ba17d3 | Vishvananda Ishaya | (intermediate == top || |
3049 | 63ba17d3 | Vishvananda Ishaya | sector_num + pnum_inter < intermediate->total_sectors)) { |
3050 | 188a7bbf | Paolo Bonzini | n = pnum_inter; |
3051 | 188a7bbf | Paolo Bonzini | } |
3052 | 188a7bbf | Paolo Bonzini | |
3053 | 188a7bbf | Paolo Bonzini | intermediate = intermediate->backing_hd; |
3054 | 188a7bbf | Paolo Bonzini | } |
3055 | 188a7bbf | Paolo Bonzini | |
3056 | 188a7bbf | Paolo Bonzini | *pnum = n; |
3057 | 188a7bbf | Paolo Bonzini | return 0; |
3058 | 188a7bbf | Paolo Bonzini | } |
3059 | 188a7bbf | Paolo Bonzini | |
3060 | b35b2bba | Miroslav Rezanina | /* Coroutine wrapper for bdrv_is_allocated_above() */
|
3061 | b35b2bba | Miroslav Rezanina | static void coroutine_fn bdrv_is_allocated_above_co_entry(void *opaque) |
3062 | b35b2bba | Miroslav Rezanina | { |
3063 | b35b2bba | Miroslav Rezanina | BdrvCoIsAllocatedData *data = opaque; |
3064 | b35b2bba | Miroslav Rezanina | BlockDriverState *top = data->bs; |
3065 | b35b2bba | Miroslav Rezanina | BlockDriverState *base = data->base; |
3066 | b35b2bba | Miroslav Rezanina | |
3067 | b35b2bba | Miroslav Rezanina | data->ret = bdrv_co_is_allocated_above(top, base, data->sector_num, |
3068 | b35b2bba | Miroslav Rezanina | data->nb_sectors, data->pnum); |
3069 | b35b2bba | Miroslav Rezanina | data->done = true;
|
3070 | b35b2bba | Miroslav Rezanina | } |
3071 | b35b2bba | Miroslav Rezanina | |
3072 | b35b2bba | Miroslav Rezanina | /*
|
3073 | b35b2bba | Miroslav Rezanina | * Synchronous wrapper around bdrv_co_is_allocated_above().
|
3074 | b35b2bba | Miroslav Rezanina | *
|
3075 | b35b2bba | Miroslav Rezanina | * See bdrv_co_is_allocated_above() for details.
|
3076 | b35b2bba | Miroslav Rezanina | */
|
3077 | b35b2bba | Miroslav Rezanina | int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
|
3078 | b35b2bba | Miroslav Rezanina | int64_t sector_num, int nb_sectors, int *pnum) |
3079 | b35b2bba | Miroslav Rezanina | { |
3080 | b35b2bba | Miroslav Rezanina | Coroutine *co; |
3081 | b35b2bba | Miroslav Rezanina | BdrvCoIsAllocatedData data = { |
3082 | b35b2bba | Miroslav Rezanina | .bs = top, |
3083 | b35b2bba | Miroslav Rezanina | .base = base, |
3084 | b35b2bba | Miroslav Rezanina | .sector_num = sector_num, |
3085 | b35b2bba | Miroslav Rezanina | .nb_sectors = nb_sectors, |
3086 | b35b2bba | Miroslav Rezanina | .pnum = pnum, |
3087 | b35b2bba | Miroslav Rezanina | .done = false,
|
3088 | b35b2bba | Miroslav Rezanina | }; |
3089 | b35b2bba | Miroslav Rezanina | |
3090 | b35b2bba | Miroslav Rezanina | co = qemu_coroutine_create(bdrv_is_allocated_above_co_entry); |
3091 | b35b2bba | Miroslav Rezanina | qemu_coroutine_enter(co, &data); |
3092 | b35b2bba | Miroslav Rezanina | while (!data.done) {
|
3093 | b35b2bba | Miroslav Rezanina | qemu_aio_wait(); |
3094 | b35b2bba | Miroslav Rezanina | } |
3095 | b35b2bba | Miroslav Rezanina | return data.ret;
|
3096 | b35b2bba | Miroslav Rezanina | } |
3097 | b35b2bba | Miroslav Rezanina | |
3098 | 045df330 | aliguori | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
3099 | 045df330 | aliguori | { |
3100 | 045df330 | aliguori | if (bs->backing_hd && bs->backing_hd->encrypted)
|
3101 | 045df330 | aliguori | return bs->backing_file;
|
3102 | 045df330 | aliguori | else if (bs->encrypted) |
3103 | 045df330 | aliguori | return bs->filename;
|
3104 | 045df330 | aliguori | else
|
3105 | 045df330 | aliguori | return NULL; |
3106 | 045df330 | aliguori | } |
3107 | 045df330 | aliguori | |
3108 | 5fafdf24 | ths | void bdrv_get_backing_filename(BlockDriverState *bs,
|
3109 | 83f64091 | bellard | char *filename, int filename_size) |
3110 | 83f64091 | bellard | { |
3111 | 3574c608 | Kevin Wolf | pstrcpy(filename, filename_size, bs->backing_file); |
3112 | 83f64091 | bellard | } |
3113 | 83f64091 | bellard | |
3114 | 5fafdf24 | ths | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
3115 | faea38e7 | bellard | const uint8_t *buf, int nb_sectors) |
3116 | faea38e7 | bellard | { |
3117 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
3118 | faea38e7 | bellard | if (!drv)
|
3119 | 19cb3738 | bellard | return -ENOMEDIUM;
|
3120 | faea38e7 | bellard | if (!drv->bdrv_write_compressed)
|
3121 | faea38e7 | bellard | return -ENOTSUP;
|
3122 | fbb7b4e0 | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
3123 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
3124 | a55eb92c | Jan Kiszka | |
3125 | 1755da16 | Paolo Bonzini | assert(!bs->dirty_bitmap); |
3126 | a55eb92c | Jan Kiszka | |
3127 | faea38e7 | bellard | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
|
3128 | faea38e7 | bellard | } |
3129 | 3b46e624 | ths | |
3130 | faea38e7 | bellard | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
3131 | faea38e7 | bellard | { |
3132 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
3133 | faea38e7 | bellard | if (!drv)
|
3134 | 19cb3738 | bellard | return -ENOMEDIUM;
|
3135 | faea38e7 | bellard | if (!drv->bdrv_get_info)
|
3136 | faea38e7 | bellard | return -ENOTSUP;
|
3137 | faea38e7 | bellard | memset(bdi, 0, sizeof(*bdi)); |
3138 | faea38e7 | bellard | return drv->bdrv_get_info(bs, bdi);
|
3139 | faea38e7 | bellard | } |
3140 | faea38e7 | bellard | |
3141 | 45566e9c | Christoph Hellwig | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
3142 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
3143 | 178e08a5 | aliguori | { |
3144 | cf8074b3 | Kevin Wolf | QEMUIOVector qiov; |
3145 | cf8074b3 | Kevin Wolf | struct iovec iov = {
|
3146 | cf8074b3 | Kevin Wolf | .iov_base = (void *) buf,
|
3147 | cf8074b3 | Kevin Wolf | .iov_len = size, |
3148 | cf8074b3 | Kevin Wolf | }; |
3149 | cf8074b3 | Kevin Wolf | |
3150 | cf8074b3 | Kevin Wolf | qemu_iovec_init_external(&qiov, &iov, 1);
|
3151 | cf8074b3 | Kevin Wolf | return bdrv_writev_vmstate(bs, &qiov, pos);
|
3152 | cf8074b3 | Kevin Wolf | } |
3153 | cf8074b3 | Kevin Wolf | |
3154 | cf8074b3 | Kevin Wolf | int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
|
3155 | cf8074b3 | Kevin Wolf | { |
3156 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
3157 | cf8074b3 | Kevin Wolf | |
3158 | cf8074b3 | Kevin Wolf | if (!drv) {
|
3159 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
3160 | cf8074b3 | Kevin Wolf | } else if (drv->bdrv_save_vmstate) { |
3161 | cf8074b3 | Kevin Wolf | return drv->bdrv_save_vmstate(bs, qiov, pos);
|
3162 | cf8074b3 | Kevin Wolf | } else if (bs->file) { |
3163 | cf8074b3 | Kevin Wolf | return bdrv_writev_vmstate(bs->file, qiov, pos);
|
3164 | cf8074b3 | Kevin Wolf | } |
3165 | cf8074b3 | Kevin Wolf | |
3166 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
3167 | 178e08a5 | aliguori | } |
3168 | 178e08a5 | aliguori | |
3169 | 45566e9c | Christoph Hellwig | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
|
3170 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
3171 | 178e08a5 | aliguori | { |
3172 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
3173 | 178e08a5 | aliguori | if (!drv)
|
3174 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
3175 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_load_vmstate)
|
3176 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_load_vmstate(bs, buf, pos, size);
|
3177 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
3178 | 7cdb1f6d | MORITA Kazutaka | return bdrv_load_vmstate(bs->file, buf, pos, size);
|
3179 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
3180 | 178e08a5 | aliguori | } |
3181 | 178e08a5 | aliguori | |
3182 | 8b9b0cc2 | Kevin Wolf | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
|
3183 | 8b9b0cc2 | Kevin Wolf | { |
3184 | bf736fe3 | Kevin Wolf | if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) {
|
3185 | 8b9b0cc2 | Kevin Wolf | return;
|
3186 | 8b9b0cc2 | Kevin Wolf | } |
3187 | 8b9b0cc2 | Kevin Wolf | |
3188 | bf736fe3 | Kevin Wolf | bs->drv->bdrv_debug_event(bs, event); |
3189 | 41c695c7 | Kevin Wolf | } |
3190 | 41c695c7 | Kevin Wolf | |
3191 | 41c695c7 | Kevin Wolf | int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, |
3192 | 41c695c7 | Kevin Wolf | const char *tag) |
3193 | 41c695c7 | Kevin Wolf | { |
3194 | 41c695c7 | Kevin Wolf | while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
|
3195 | 41c695c7 | Kevin Wolf | bs = bs->file; |
3196 | 41c695c7 | Kevin Wolf | } |
3197 | 41c695c7 | Kevin Wolf | |
3198 | 41c695c7 | Kevin Wolf | if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
|
3199 | 41c695c7 | Kevin Wolf | return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
|
3200 | 41c695c7 | Kevin Wolf | } |
3201 | 41c695c7 | Kevin Wolf | |
3202 | 41c695c7 | Kevin Wolf | return -ENOTSUP;
|
3203 | 41c695c7 | Kevin Wolf | } |
3204 | 41c695c7 | Kevin Wolf | |
3205 | 41c695c7 | Kevin Wolf | int bdrv_debug_resume(BlockDriverState *bs, const char *tag) |
3206 | 41c695c7 | Kevin Wolf | { |
3207 | 41c695c7 | Kevin Wolf | while (bs && bs->drv && !bs->drv->bdrv_debug_resume) {
|
3208 | 41c695c7 | Kevin Wolf | bs = bs->file; |
3209 | 41c695c7 | Kevin Wolf | } |
3210 | 8b9b0cc2 | Kevin Wolf | |
3211 | 41c695c7 | Kevin Wolf | if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
|
3212 | 41c695c7 | Kevin Wolf | return bs->drv->bdrv_debug_resume(bs, tag);
|
3213 | 41c695c7 | Kevin Wolf | } |
3214 | 41c695c7 | Kevin Wolf | |
3215 | 41c695c7 | Kevin Wolf | return -ENOTSUP;
|
3216 | 41c695c7 | Kevin Wolf | } |
3217 | 41c695c7 | Kevin Wolf | |
3218 | 41c695c7 | Kevin Wolf | bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) |
3219 | 41c695c7 | Kevin Wolf | { |
3220 | 41c695c7 | Kevin Wolf | while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
|
3221 | 41c695c7 | Kevin Wolf | bs = bs->file; |
3222 | 41c695c7 | Kevin Wolf | } |
3223 | 41c695c7 | Kevin Wolf | |
3224 | 41c695c7 | Kevin Wolf | if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
|
3225 | 41c695c7 | Kevin Wolf | return bs->drv->bdrv_debug_is_suspended(bs, tag);
|
3226 | 41c695c7 | Kevin Wolf | } |
3227 | 41c695c7 | Kevin Wolf | |
3228 | 41c695c7 | Kevin Wolf | return false; |
3229 | 8b9b0cc2 | Kevin Wolf | } |
3230 | 8b9b0cc2 | Kevin Wolf | |
3231 | 199630b6 | Blue Swirl | int bdrv_is_snapshot(BlockDriverState *bs)
|
3232 | 199630b6 | Blue Swirl | { |
3233 | 199630b6 | Blue Swirl | return !!(bs->open_flags & BDRV_O_SNAPSHOT);
|
3234 | 199630b6 | Blue Swirl | } |
3235 | 199630b6 | Blue Swirl | |
3236 | b1b1d783 | Jeff Cody | /* backing_file can either be relative, or absolute, or a protocol. If it is
|
3237 | b1b1d783 | Jeff Cody | * relative, it must be relative to the chain. So, passing in bs->filename
|
3238 | b1b1d783 | Jeff Cody | * from a BDS as backing_file should not be done, as that may be relative to
|
3239 | b1b1d783 | Jeff Cody | * the CWD rather than the chain. */
|
3240 | e8a6bb9c | Marcelo Tosatti | BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, |
3241 | e8a6bb9c | Marcelo Tosatti | const char *backing_file) |
3242 | e8a6bb9c | Marcelo Tosatti | { |
3243 | b1b1d783 | Jeff Cody | char *filename_full = NULL; |
3244 | b1b1d783 | Jeff Cody | char *backing_file_full = NULL; |
3245 | b1b1d783 | Jeff Cody | char *filename_tmp = NULL; |
3246 | b1b1d783 | Jeff Cody | int is_protocol = 0; |
3247 | b1b1d783 | Jeff Cody | BlockDriverState *curr_bs = NULL;
|
3248 | b1b1d783 | Jeff Cody | BlockDriverState *retval = NULL;
|
3249 | b1b1d783 | Jeff Cody | |
3250 | b1b1d783 | Jeff Cody | if (!bs || !bs->drv || !backing_file) {
|
3251 | e8a6bb9c | Marcelo Tosatti | return NULL; |
3252 | e8a6bb9c | Marcelo Tosatti | } |
3253 | e8a6bb9c | Marcelo Tosatti | |
3254 | b1b1d783 | Jeff Cody | filename_full = g_malloc(PATH_MAX); |
3255 | b1b1d783 | Jeff Cody | backing_file_full = g_malloc(PATH_MAX); |
3256 | b1b1d783 | Jeff Cody | filename_tmp = g_malloc(PATH_MAX); |
3257 | b1b1d783 | Jeff Cody | |
3258 | b1b1d783 | Jeff Cody | is_protocol = path_has_protocol(backing_file); |
3259 | b1b1d783 | Jeff Cody | |
3260 | b1b1d783 | Jeff Cody | for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) {
|
3261 | b1b1d783 | Jeff Cody | |
3262 | b1b1d783 | Jeff Cody | /* If either of the filename paths is actually a protocol, then
|
3263 | b1b1d783 | Jeff Cody | * compare unmodified paths; otherwise make paths relative */
|
3264 | b1b1d783 | Jeff Cody | if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
|
3265 | b1b1d783 | Jeff Cody | if (strcmp(backing_file, curr_bs->backing_file) == 0) { |
3266 | b1b1d783 | Jeff Cody | retval = curr_bs->backing_hd; |
3267 | b1b1d783 | Jeff Cody | break;
|
3268 | b1b1d783 | Jeff Cody | } |
3269 | e8a6bb9c | Marcelo Tosatti | } else {
|
3270 | b1b1d783 | Jeff Cody | /* If not an absolute filename path, make it relative to the current
|
3271 | b1b1d783 | Jeff Cody | * image's filename path */
|
3272 | b1b1d783 | Jeff Cody | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, |
3273 | b1b1d783 | Jeff Cody | backing_file); |
3274 | b1b1d783 | Jeff Cody | |
3275 | b1b1d783 | Jeff Cody | /* We are going to compare absolute pathnames */
|
3276 | b1b1d783 | Jeff Cody | if (!realpath(filename_tmp, filename_full)) {
|
3277 | b1b1d783 | Jeff Cody | continue;
|
3278 | b1b1d783 | Jeff Cody | } |
3279 | b1b1d783 | Jeff Cody | |
3280 | b1b1d783 | Jeff Cody | /* We need to make sure the backing filename we are comparing against
|
3281 | b1b1d783 | Jeff Cody | * is relative to the current image filename (or absolute) */
|
3282 | b1b1d783 | Jeff Cody | path_combine(filename_tmp, PATH_MAX, curr_bs->filename, |
3283 | b1b1d783 | Jeff Cody | curr_bs->backing_file); |
3284 | b1b1d783 | Jeff Cody | |
3285 | b1b1d783 | Jeff Cody | if (!realpath(filename_tmp, backing_file_full)) {
|
3286 | b1b1d783 | Jeff Cody | continue;
|
3287 | b1b1d783 | Jeff Cody | } |
3288 | b1b1d783 | Jeff Cody | |
3289 | b1b1d783 | Jeff Cody | if (strcmp(backing_file_full, filename_full) == 0) { |
3290 | b1b1d783 | Jeff Cody | retval = curr_bs->backing_hd; |
3291 | b1b1d783 | Jeff Cody | break;
|
3292 | b1b1d783 | Jeff Cody | } |
3293 | e8a6bb9c | Marcelo Tosatti | } |
3294 | e8a6bb9c | Marcelo Tosatti | } |
3295 | e8a6bb9c | Marcelo Tosatti | |
3296 | b1b1d783 | Jeff Cody | g_free(filename_full); |
3297 | b1b1d783 | Jeff Cody | g_free(backing_file_full); |
3298 | b1b1d783 | Jeff Cody | g_free(filename_tmp); |
3299 | b1b1d783 | Jeff Cody | return retval;
|
3300 | e8a6bb9c | Marcelo Tosatti | } |
3301 | e8a6bb9c | Marcelo Tosatti | |
3302 | f198fd1c | Benoรฎt Canet | int bdrv_get_backing_file_depth(BlockDriverState *bs)
|
3303 | f198fd1c | Benoรฎt Canet | { |
3304 | f198fd1c | Benoรฎt Canet | if (!bs->drv) {
|
3305 | f198fd1c | Benoรฎt Canet | return 0; |
3306 | f198fd1c | Benoรฎt Canet | } |
3307 | f198fd1c | Benoรฎt Canet | |
3308 | f198fd1c | Benoรฎt Canet | if (!bs->backing_hd) {
|
3309 | f198fd1c | Benoรฎt Canet | return 0; |
3310 | f198fd1c | Benoรฎt Canet | } |
3311 | f198fd1c | Benoรฎt Canet | |
3312 | f198fd1c | Benoรฎt Canet | return 1 + bdrv_get_backing_file_depth(bs->backing_hd); |
3313 | f198fd1c | Benoรฎt Canet | } |
3314 | f198fd1c | Benoรฎt Canet | |
3315 | 79fac568 | Jeff Cody | BlockDriverState *bdrv_find_base(BlockDriverState *bs) |
3316 | 79fac568 | Jeff Cody | { |
3317 | 79fac568 | Jeff Cody | BlockDriverState *curr_bs = NULL;
|
3318 | 79fac568 | Jeff Cody | |
3319 | 79fac568 | Jeff Cody | if (!bs) {
|
3320 | 79fac568 | Jeff Cody | return NULL; |
3321 | 79fac568 | Jeff Cody | } |
3322 | 79fac568 | Jeff Cody | |
3323 | 79fac568 | Jeff Cody | curr_bs = bs; |
3324 | 79fac568 | Jeff Cody | |
3325 | 79fac568 | Jeff Cody | while (curr_bs->backing_hd) {
|
3326 | 79fac568 | Jeff Cody | curr_bs = curr_bs->backing_hd; |
3327 | 79fac568 | Jeff Cody | } |
3328 | 79fac568 | Jeff Cody | return curr_bs;
|
3329 | 79fac568 | Jeff Cody | } |
3330 | 79fac568 | Jeff Cody | |
3331 | ea2384d3 | bellard | /**************************************************************/
|
3332 | 83f64091 | bellard | /* async I/Os */
|
3333 | ea2384d3 | bellard | |
3334 | 3b69e4b9 | aliguori | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
3335 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
3336 | 3b69e4b9 | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
3337 | 3b69e4b9 | aliguori | { |
3338 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
3339 | bbf0a440 | Stefan Hajnoczi | |
3340 | b2a61371 | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
3341 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, false);
|
3342 | ea2384d3 | bellard | } |
3343 | ea2384d3 | bellard | |
3344 | f141eafe | aliguori | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
3345 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
3346 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
3347 | ea2384d3 | bellard | { |
3348 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
3349 | bbf0a440 | Stefan Hajnoczi | |
3350 | 1a6e115b | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
3351 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, true);
|
3352 | 83f64091 | bellard | } |
3353 | 83f64091 | bellard | |
3354 | 40b4f539 | Kevin Wolf | |
3355 | 40b4f539 | Kevin Wolf | typedef struct MultiwriteCB { |
3356 | 40b4f539 | Kevin Wolf | int error;
|
3357 | 40b4f539 | Kevin Wolf | int num_requests;
|
3358 | 40b4f539 | Kevin Wolf | int num_callbacks;
|
3359 | 40b4f539 | Kevin Wolf | struct {
|
3360 | 40b4f539 | Kevin Wolf | BlockDriverCompletionFunc *cb; |
3361 | 40b4f539 | Kevin Wolf | void *opaque;
|
3362 | 40b4f539 | Kevin Wolf | QEMUIOVector *free_qiov; |
3363 | 40b4f539 | Kevin Wolf | } callbacks[]; |
3364 | 40b4f539 | Kevin Wolf | } MultiwriteCB; |
3365 | 40b4f539 | Kevin Wolf | |
3366 | 40b4f539 | Kevin Wolf | static void multiwrite_user_cb(MultiwriteCB *mcb) |
3367 | 40b4f539 | Kevin Wolf | { |
3368 | 40b4f539 | Kevin Wolf | int i;
|
3369 | 40b4f539 | Kevin Wolf | |
3370 | 40b4f539 | Kevin Wolf | for (i = 0; i < mcb->num_callbacks; i++) { |
3371 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
3372 | 1e1ea48d | Stefan Hajnoczi | if (mcb->callbacks[i].free_qiov) {
|
3373 | 1e1ea48d | Stefan Hajnoczi | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); |
3374 | 1e1ea48d | Stefan Hajnoczi | } |
3375 | 7267c094 | Anthony Liguori | g_free(mcb->callbacks[i].free_qiov); |
3376 | 40b4f539 | Kevin Wolf | } |
3377 | 40b4f539 | Kevin Wolf | } |
3378 | 40b4f539 | Kevin Wolf | |
3379 | 40b4f539 | Kevin Wolf | static void multiwrite_cb(void *opaque, int ret) |
3380 | 40b4f539 | Kevin Wolf | { |
3381 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb = opaque; |
3382 | 40b4f539 | Kevin Wolf | |
3383 | 6d519a5f | Stefan Hajnoczi | trace_multiwrite_cb(mcb, ret); |
3384 | 6d519a5f | Stefan Hajnoczi | |
3385 | cb6d3ca0 | Kevin Wolf | if (ret < 0 && !mcb->error) { |
3386 | 40b4f539 | Kevin Wolf | mcb->error = ret; |
3387 | 40b4f539 | Kevin Wolf | } |
3388 | 40b4f539 | Kevin Wolf | |
3389 | 40b4f539 | Kevin Wolf | mcb->num_requests--; |
3390 | 40b4f539 | Kevin Wolf | if (mcb->num_requests == 0) { |
3391 | de189a1b | Kevin Wolf | multiwrite_user_cb(mcb); |
3392 | 7267c094 | Anthony Liguori | g_free(mcb); |
3393 | 40b4f539 | Kevin Wolf | } |
3394 | 40b4f539 | Kevin Wolf | } |
3395 | 40b4f539 | Kevin Wolf | |
3396 | 40b4f539 | Kevin Wolf | static int multiwrite_req_compare(const void *a, const void *b) |
3397 | 40b4f539 | Kevin Wolf | { |
3398 | 77be4366 | Christoph Hellwig | const BlockRequest *req1 = a, *req2 = b;
|
3399 | 77be4366 | Christoph Hellwig | |
3400 | 77be4366 | Christoph Hellwig | /*
|
3401 | 77be4366 | Christoph Hellwig | * Note that we can't simply subtract req2->sector from req1->sector
|
3402 | 77be4366 | Christoph Hellwig | * here as that could overflow the return value.
|
3403 | 77be4366 | Christoph Hellwig | */
|
3404 | 77be4366 | Christoph Hellwig | if (req1->sector > req2->sector) {
|
3405 | 77be4366 | Christoph Hellwig | return 1; |
3406 | 77be4366 | Christoph Hellwig | } else if (req1->sector < req2->sector) { |
3407 | 77be4366 | Christoph Hellwig | return -1; |
3408 | 77be4366 | Christoph Hellwig | } else {
|
3409 | 77be4366 | Christoph Hellwig | return 0; |
3410 | 77be4366 | Christoph Hellwig | } |
3411 | 40b4f539 | Kevin Wolf | } |
3412 | 40b4f539 | Kevin Wolf | |
3413 | 40b4f539 | Kevin Wolf | /*
|
3414 | 40b4f539 | Kevin Wolf | * Takes a bunch of requests and tries to merge them. Returns the number of
|
3415 | 40b4f539 | Kevin Wolf | * requests that remain after merging.
|
3416 | 40b4f539 | Kevin Wolf | */
|
3417 | 40b4f539 | Kevin Wolf | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
3418 | 40b4f539 | Kevin Wolf | int num_reqs, MultiwriteCB *mcb)
|
3419 | 40b4f539 | Kevin Wolf | { |
3420 | 40b4f539 | Kevin Wolf | int i, outidx;
|
3421 | 40b4f539 | Kevin Wolf | |
3422 | 40b4f539 | Kevin Wolf | // Sort requests by start sector
|
3423 | 40b4f539 | Kevin Wolf | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
|
3424 | 40b4f539 | Kevin Wolf | |
3425 | 40b4f539 | Kevin Wolf | // Check if adjacent requests touch the same clusters. If so, combine them,
|
3426 | 40b4f539 | Kevin Wolf | // filling up gaps with zero sectors.
|
3427 | 40b4f539 | Kevin Wolf | outidx = 0;
|
3428 | 40b4f539 | Kevin Wolf | for (i = 1; i < num_reqs; i++) { |
3429 | 40b4f539 | Kevin Wolf | int merge = 0; |
3430 | 40b4f539 | Kevin Wolf | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
3431 | 40b4f539 | Kevin Wolf | |
3432 | b6a127a1 | Paolo Bonzini | // Handle exactly sequential writes and overlapping writes.
|
3433 | 40b4f539 | Kevin Wolf | if (reqs[i].sector <= oldreq_last) {
|
3434 | 40b4f539 | Kevin Wolf | merge = 1;
|
3435 | 40b4f539 | Kevin Wolf | } |
3436 | 40b4f539 | Kevin Wolf | |
3437 | e2a305fb | Christoph Hellwig | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
3438 | e2a305fb | Christoph Hellwig | merge = 0;
|
3439 | e2a305fb | Christoph Hellwig | } |
3440 | e2a305fb | Christoph Hellwig | |
3441 | 40b4f539 | Kevin Wolf | if (merge) {
|
3442 | 40b4f539 | Kevin Wolf | size_t size; |
3443 | 7267c094 | Anthony Liguori | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
|
3444 | 40b4f539 | Kevin Wolf | qemu_iovec_init(qiov, |
3445 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
|
3446 | 40b4f539 | Kevin Wolf | |
3447 | 40b4f539 | Kevin Wolf | // Add the first request to the merged one. If the requests are
|
3448 | 40b4f539 | Kevin Wolf | // overlapping, drop the last sectors of the first request.
|
3449 | 40b4f539 | Kevin Wolf | size = (reqs[i].sector - reqs[outidx].sector) << 9;
|
3450 | 1b093c48 | Michael Tokarev | qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
|
3451 | 40b4f539 | Kevin Wolf | |
3452 | b6a127a1 | Paolo Bonzini | // We should need to add any zeros between the two requests
|
3453 | b6a127a1 | Paolo Bonzini | assert (reqs[i].sector <= oldreq_last); |
3454 | 40b4f539 | Kevin Wolf | |
3455 | 40b4f539 | Kevin Wolf | // Add the second request
|
3456 | 1b093c48 | Michael Tokarev | qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
|
3457 | 40b4f539 | Kevin Wolf | |
3458 | cbf1dff2 | Kevin Wolf | reqs[outidx].nb_sectors = qiov->size >> 9;
|
3459 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = qiov; |
3460 | 40b4f539 | Kevin Wolf | |
3461 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
3462 | 40b4f539 | Kevin Wolf | } else {
|
3463 | 40b4f539 | Kevin Wolf | outidx++; |
3464 | 40b4f539 | Kevin Wolf | reqs[outidx].sector = reqs[i].sector; |
3465 | 40b4f539 | Kevin Wolf | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
3466 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = reqs[i].qiov; |
3467 | 40b4f539 | Kevin Wolf | } |
3468 | 40b4f539 | Kevin Wolf | } |
3469 | 40b4f539 | Kevin Wolf | |
3470 | 40b4f539 | Kevin Wolf | return outidx + 1; |
3471 | 40b4f539 | Kevin Wolf | } |
3472 | 40b4f539 | Kevin Wolf | |
3473 | 40b4f539 | Kevin Wolf | /*
|
3474 | 40b4f539 | Kevin Wolf | * Submit multiple AIO write requests at once.
|
3475 | 40b4f539 | Kevin Wolf | *
|
3476 | 40b4f539 | Kevin Wolf | * On success, the function returns 0 and all requests in the reqs array have
|
3477 | 40b4f539 | Kevin Wolf | * been submitted. In error case this function returns -1, and any of the
|
3478 | 40b4f539 | Kevin Wolf | * requests may or may not be submitted yet. In particular, this means that the
|
3479 | 40b4f539 | Kevin Wolf | * callback will be called for some of the requests, for others it won't. The
|
3480 | 40b4f539 | Kevin Wolf | * caller must check the error field of the BlockRequest to wait for the right
|
3481 | 40b4f539 | Kevin Wolf | * callbacks (if error != 0, no callback will be called).
|
3482 | 40b4f539 | Kevin Wolf | *
|
3483 | 40b4f539 | Kevin Wolf | * The implementation may modify the contents of the reqs array, e.g. to merge
|
3484 | 40b4f539 | Kevin Wolf | * requests. However, the fields opaque and error are left unmodified as they
|
3485 | 40b4f539 | Kevin Wolf | * are used to signal failure for a single request to the caller.
|
3486 | 40b4f539 | Kevin Wolf | */
|
3487 | 40b4f539 | Kevin Wolf | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
3488 | 40b4f539 | Kevin Wolf | { |
3489 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb; |
3490 | 40b4f539 | Kevin Wolf | int i;
|
3491 | 40b4f539 | Kevin Wolf | |
3492 | 301db7c2 | Ryan Harper | /* don't submit writes if we don't have a medium */
|
3493 | 301db7c2 | Ryan Harper | if (bs->drv == NULL) { |
3494 | 301db7c2 | Ryan Harper | for (i = 0; i < num_reqs; i++) { |
3495 | 301db7c2 | Ryan Harper | reqs[i].error = -ENOMEDIUM; |
3496 | 301db7c2 | Ryan Harper | } |
3497 | 301db7c2 | Ryan Harper | return -1; |
3498 | 301db7c2 | Ryan Harper | } |
3499 | 301db7c2 | Ryan Harper | |
3500 | 40b4f539 | Kevin Wolf | if (num_reqs == 0) { |
3501 | 40b4f539 | Kevin Wolf | return 0; |
3502 | 40b4f539 | Kevin Wolf | } |
3503 | 40b4f539 | Kevin Wolf | |
3504 | 40b4f539 | Kevin Wolf | // Create MultiwriteCB structure
|
3505 | 7267c094 | Anthony Liguori | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
3506 | 40b4f539 | Kevin Wolf | mcb->num_requests = 0;
|
3507 | 40b4f539 | Kevin Wolf | mcb->num_callbacks = num_reqs; |
3508 | 40b4f539 | Kevin Wolf | |
3509 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
3510 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb = reqs[i].cb; |
3511 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].opaque = reqs[i].opaque; |
3512 | 40b4f539 | Kevin Wolf | } |
3513 | 40b4f539 | Kevin Wolf | |
3514 | 40b4f539 | Kevin Wolf | // Check for mergable requests
|
3515 | 40b4f539 | Kevin Wolf | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
3516 | 40b4f539 | Kevin Wolf | |
3517 | 6d519a5f | Stefan Hajnoczi | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
3518 | 6d519a5f | Stefan Hajnoczi | |
3519 | df9309fb | Paolo Bonzini | /* Run the aio requests. */
|
3520 | df9309fb | Paolo Bonzini | mcb->num_requests = num_reqs; |
3521 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
3522 | ad54ae80 | Paolo Bonzini | bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
3523 | 40b4f539 | Kevin Wolf | reqs[i].nb_sectors, multiwrite_cb, mcb); |
3524 | 40b4f539 | Kevin Wolf | } |
3525 | 40b4f539 | Kevin Wolf | |
3526 | 40b4f539 | Kevin Wolf | return 0; |
3527 | 40b4f539 | Kevin Wolf | } |
3528 | 40b4f539 | Kevin Wolf | |
3529 | 83f64091 | bellard | void bdrv_aio_cancel(BlockDriverAIOCB *acb)
|
3530 | 83f64091 | bellard | { |
3531 | d7331bed | Stefan Hajnoczi | acb->aiocb_info->cancel(acb); |
3532 | 83f64091 | bellard | } |
3533 | 83f64091 | bellard | |
3534 | 98f90dba | Zhi Yong Wu | /* block I/O throttling */
|
3535 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, |
3536 | 98f90dba | Zhi Yong Wu | bool is_write, double elapsed_time, uint64_t *wait) |
3537 | 98f90dba | Zhi Yong Wu | { |
3538 | 98f90dba | Zhi Yong Wu | uint64_t bps_limit = 0;
|
3539 | ae29d6c6 | Stefan Hajnoczi | uint64_t extension; |
3540 | 98f90dba | Zhi Yong Wu | double bytes_limit, bytes_base, bytes_res;
|
3541 | 98f90dba | Zhi Yong Wu | double slice_time, wait_time;
|
3542 | 98f90dba | Zhi Yong Wu | |
3543 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
|
3544 | 98f90dba | Zhi Yong Wu | bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; |
3545 | 98f90dba | Zhi Yong Wu | } else if (bs->io_limits.bps[is_write]) { |
3546 | 98f90dba | Zhi Yong Wu | bps_limit = bs->io_limits.bps[is_write]; |
3547 | 98f90dba | Zhi Yong Wu | } else {
|
3548 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3549 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3550 | 98f90dba | Zhi Yong Wu | } |
3551 | 98f90dba | Zhi Yong Wu | |
3552 | 98f90dba | Zhi Yong Wu | return false; |
3553 | 98f90dba | Zhi Yong Wu | } |
3554 | 98f90dba | Zhi Yong Wu | |
3555 | 98f90dba | Zhi Yong Wu | slice_time = bs->slice_end - bs->slice_start; |
3556 | 98f90dba | Zhi Yong Wu | slice_time /= (NANOSECONDS_PER_SECOND); |
3557 | 98f90dba | Zhi Yong Wu | bytes_limit = bps_limit * slice_time; |
3558 | 5905fbc9 | Stefan Hajnoczi | bytes_base = bs->slice_submitted.bytes[is_write]; |
3559 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
|
3560 | 5905fbc9 | Stefan Hajnoczi | bytes_base += bs->slice_submitted.bytes[!is_write]; |
3561 | 98f90dba | Zhi Yong Wu | } |
3562 | 98f90dba | Zhi Yong Wu | |
3563 | 98f90dba | Zhi Yong Wu | /* bytes_base: the bytes of data which have been read/written; and
|
3564 | 98f90dba | Zhi Yong Wu | * it is obtained from the history statistic info.
|
3565 | 98f90dba | Zhi Yong Wu | * bytes_res: the remaining bytes of data which need to be read/written.
|
3566 | 98f90dba | Zhi Yong Wu | * (bytes_base + bytes_res) / bps_limit: used to calcuate
|
3567 | 98f90dba | Zhi Yong Wu | * the total time for completing reading/writting all data.
|
3568 | 98f90dba | Zhi Yong Wu | */
|
3569 | 98f90dba | Zhi Yong Wu | bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
|
3570 | 98f90dba | Zhi Yong Wu | |
3571 | 98f90dba | Zhi Yong Wu | if (bytes_base + bytes_res <= bytes_limit) {
|
3572 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3573 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3574 | 98f90dba | Zhi Yong Wu | } |
3575 | 98f90dba | Zhi Yong Wu | |
3576 | 98f90dba | Zhi Yong Wu | return false; |
3577 | 98f90dba | Zhi Yong Wu | } |
3578 | 98f90dba | Zhi Yong Wu | |
3579 | 98f90dba | Zhi Yong Wu | /* Calc approx time to dispatch */
|
3580 | 98f90dba | Zhi Yong Wu | wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; |
3581 | 98f90dba | Zhi Yong Wu | |
3582 | 98f90dba | Zhi Yong Wu | /* When the I/O rate at runtime exceeds the limits,
|
3583 | 98f90dba | Zhi Yong Wu | * bs->slice_end need to be extended in order that the current statistic
|
3584 | 98f90dba | Zhi Yong Wu | * info can be kept until the timer fire, so it is increased and tuned
|
3585 | 98f90dba | Zhi Yong Wu | * based on the result of experiment.
|
3586 | 98f90dba | Zhi Yong Wu | */
|
3587 | ae29d6c6 | Stefan Hajnoczi | extension = wait_time * NANOSECONDS_PER_SECOND; |
3588 | ae29d6c6 | Stefan Hajnoczi | extension = DIV_ROUND_UP(extension, BLOCK_IO_SLICE_TIME) * |
3589 | ae29d6c6 | Stefan Hajnoczi | BLOCK_IO_SLICE_TIME; |
3590 | ae29d6c6 | Stefan Hajnoczi | bs->slice_end += extension; |
3591 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3592 | 0775437f | Stefan Hajnoczi | *wait = wait_time * NANOSECONDS_PER_SECOND; |
3593 | 98f90dba | Zhi Yong Wu | } |
3594 | 98f90dba | Zhi Yong Wu | |
3595 | 98f90dba | Zhi Yong Wu | return true; |
3596 | 98f90dba | Zhi Yong Wu | } |
3597 | 98f90dba | Zhi Yong Wu | |
3598 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, |
3599 | 98f90dba | Zhi Yong Wu | double elapsed_time, uint64_t *wait)
|
3600 | 98f90dba | Zhi Yong Wu | { |
3601 | 98f90dba | Zhi Yong Wu | uint64_t iops_limit = 0;
|
3602 | 98f90dba | Zhi Yong Wu | double ios_limit, ios_base;
|
3603 | 98f90dba | Zhi Yong Wu | double slice_time, wait_time;
|
3604 | 98f90dba | Zhi Yong Wu | |
3605 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
|
3606 | 98f90dba | Zhi Yong Wu | iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; |
3607 | 98f90dba | Zhi Yong Wu | } else if (bs->io_limits.iops[is_write]) { |
3608 | 98f90dba | Zhi Yong Wu | iops_limit = bs->io_limits.iops[is_write]; |
3609 | 98f90dba | Zhi Yong Wu | } else {
|
3610 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3611 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3612 | 98f90dba | Zhi Yong Wu | } |
3613 | 98f90dba | Zhi Yong Wu | |
3614 | 98f90dba | Zhi Yong Wu | return false; |
3615 | 98f90dba | Zhi Yong Wu | } |
3616 | 98f90dba | Zhi Yong Wu | |
3617 | 98f90dba | Zhi Yong Wu | slice_time = bs->slice_end - bs->slice_start; |
3618 | 98f90dba | Zhi Yong Wu | slice_time /= (NANOSECONDS_PER_SECOND); |
3619 | 98f90dba | Zhi Yong Wu | ios_limit = iops_limit * slice_time; |
3620 | 5905fbc9 | Stefan Hajnoczi | ios_base = bs->slice_submitted.ios[is_write]; |
3621 | 98f90dba | Zhi Yong Wu | if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
|
3622 | 5905fbc9 | Stefan Hajnoczi | ios_base += bs->slice_submitted.ios[!is_write]; |
3623 | 98f90dba | Zhi Yong Wu | } |
3624 | 98f90dba | Zhi Yong Wu | |
3625 | 98f90dba | Zhi Yong Wu | if (ios_base + 1 <= ios_limit) { |
3626 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3627 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3628 | 98f90dba | Zhi Yong Wu | } |
3629 | 98f90dba | Zhi Yong Wu | |
3630 | 98f90dba | Zhi Yong Wu | return false; |
3631 | 98f90dba | Zhi Yong Wu | } |
3632 | 98f90dba | Zhi Yong Wu | |
3633 | 0775437f | Stefan Hajnoczi | /* Calc approx time to dispatch, in seconds */
|
3634 | 98f90dba | Zhi Yong Wu | wait_time = (ios_base + 1) / iops_limit;
|
3635 | 98f90dba | Zhi Yong Wu | if (wait_time > elapsed_time) {
|
3636 | 98f90dba | Zhi Yong Wu | wait_time = wait_time - elapsed_time; |
3637 | 98f90dba | Zhi Yong Wu | } else {
|
3638 | 98f90dba | Zhi Yong Wu | wait_time = 0;
|
3639 | 98f90dba | Zhi Yong Wu | } |
3640 | 98f90dba | Zhi Yong Wu | |
3641 | ae29d6c6 | Stefan Hajnoczi | /* Exceeded current slice, extend it by another slice time */
|
3642 | ae29d6c6 | Stefan Hajnoczi | bs->slice_end += BLOCK_IO_SLICE_TIME; |
3643 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3644 | 0775437f | Stefan Hajnoczi | *wait = wait_time * NANOSECONDS_PER_SECOND; |
3645 | 98f90dba | Zhi Yong Wu | } |
3646 | 98f90dba | Zhi Yong Wu | |
3647 | 98f90dba | Zhi Yong Wu | return true; |
3648 | 98f90dba | Zhi Yong Wu | } |
3649 | 98f90dba | Zhi Yong Wu | |
3650 | 98f90dba | Zhi Yong Wu | static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, |
3651 | 98f90dba | Zhi Yong Wu | bool is_write, int64_t *wait)
|
3652 | 98f90dba | Zhi Yong Wu | { |
3653 | 98f90dba | Zhi Yong Wu | int64_t now, max_wait; |
3654 | 98f90dba | Zhi Yong Wu | uint64_t bps_wait = 0, iops_wait = 0; |
3655 | 98f90dba | Zhi Yong Wu | double elapsed_time;
|
3656 | 98f90dba | Zhi Yong Wu | int bps_ret, iops_ret;
|
3657 | 98f90dba | Zhi Yong Wu | |
3658 | 98f90dba | Zhi Yong Wu | now = qemu_get_clock_ns(vm_clock); |
3659 | e660fb8b | Stefan Hajnoczi | if (now > bs->slice_end) {
|
3660 | 98f90dba | Zhi Yong Wu | bs->slice_start = now; |
3661 | ae29d6c6 | Stefan Hajnoczi | bs->slice_end = now + BLOCK_IO_SLICE_TIME; |
3662 | 5905fbc9 | Stefan Hajnoczi | memset(&bs->slice_submitted, 0, sizeof(bs->slice_submitted)); |
3663 | 98f90dba | Zhi Yong Wu | } |
3664 | 98f90dba | Zhi Yong Wu | |
3665 | 98f90dba | Zhi Yong Wu | elapsed_time = now - bs->slice_start; |
3666 | 98f90dba | Zhi Yong Wu | elapsed_time /= (NANOSECONDS_PER_SECOND); |
3667 | 98f90dba | Zhi Yong Wu | |
3668 | 98f90dba | Zhi Yong Wu | bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, |
3669 | 98f90dba | Zhi Yong Wu | is_write, elapsed_time, &bps_wait); |
3670 | 98f90dba | Zhi Yong Wu | iops_ret = bdrv_exceed_iops_limits(bs, is_write, |
3671 | 98f90dba | Zhi Yong Wu | elapsed_time, &iops_wait); |
3672 | 98f90dba | Zhi Yong Wu | if (bps_ret || iops_ret) {
|
3673 | 98f90dba | Zhi Yong Wu | max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; |
3674 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3675 | 98f90dba | Zhi Yong Wu | *wait = max_wait; |
3676 | 98f90dba | Zhi Yong Wu | } |
3677 | 98f90dba | Zhi Yong Wu | |
3678 | 98f90dba | Zhi Yong Wu | now = qemu_get_clock_ns(vm_clock); |
3679 | 98f90dba | Zhi Yong Wu | if (bs->slice_end < now + max_wait) {
|
3680 | 98f90dba | Zhi Yong Wu | bs->slice_end = now + max_wait; |
3681 | 98f90dba | Zhi Yong Wu | } |
3682 | 98f90dba | Zhi Yong Wu | |
3683 | 98f90dba | Zhi Yong Wu | return true; |
3684 | 98f90dba | Zhi Yong Wu | } |
3685 | 98f90dba | Zhi Yong Wu | |
3686 | 98f90dba | Zhi Yong Wu | if (wait) {
|
3687 | 98f90dba | Zhi Yong Wu | *wait = 0;
|
3688 | 98f90dba | Zhi Yong Wu | } |
3689 | 98f90dba | Zhi Yong Wu | |
3690 | 5905fbc9 | Stefan Hajnoczi | bs->slice_submitted.bytes[is_write] += (int64_t)nb_sectors * |
3691 | 5905fbc9 | Stefan Hajnoczi | BDRV_SECTOR_SIZE; |
3692 | 5905fbc9 | Stefan Hajnoczi | bs->slice_submitted.ios[is_write]++; |
3693 | 5905fbc9 | Stefan Hajnoczi | |
3694 | 98f90dba | Zhi Yong Wu | return false; |
3695 | 98f90dba | Zhi Yong Wu | } |
3696 | ce1a14dc | pbrook | |
3697 | 83f64091 | bellard | /**************************************************************/
|
3698 | 83f64091 | bellard | /* async block device emulation */
|
3699 | 83f64091 | bellard | |
3700 | c16b5a2c | Christoph Hellwig | typedef struct BlockDriverAIOCBSync { |
3701 | c16b5a2c | Christoph Hellwig | BlockDriverAIOCB common; |
3702 | c16b5a2c | Christoph Hellwig | QEMUBH *bh; |
3703 | c16b5a2c | Christoph Hellwig | int ret;
|
3704 | c16b5a2c | Christoph Hellwig | /* vector translation state */
|
3705 | c16b5a2c | Christoph Hellwig | QEMUIOVector *qiov; |
3706 | c16b5a2c | Christoph Hellwig | uint8_t *bounce; |
3707 | c16b5a2c | Christoph Hellwig | int is_write;
|
3708 | c16b5a2c | Christoph Hellwig | } BlockDriverAIOCBSync; |
3709 | c16b5a2c | Christoph Hellwig | |
3710 | c16b5a2c | Christoph Hellwig | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
3711 | c16b5a2c | Christoph Hellwig | { |
3712 | b666d239 | Kevin Wolf | BlockDriverAIOCBSync *acb = |
3713 | b666d239 | Kevin Wolf | container_of(blockacb, BlockDriverAIOCBSync, common); |
3714 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
3715 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
3716 | c16b5a2c | Christoph Hellwig | qemu_aio_release(acb); |
3717 | c16b5a2c | Christoph Hellwig | } |
3718 | c16b5a2c | Christoph Hellwig | |
3719 | d7331bed | Stefan Hajnoczi | static const AIOCBInfo bdrv_em_aiocb_info = { |
3720 | c16b5a2c | Christoph Hellwig | .aiocb_size = sizeof(BlockDriverAIOCBSync),
|
3721 | c16b5a2c | Christoph Hellwig | .cancel = bdrv_aio_cancel_em, |
3722 | c16b5a2c | Christoph Hellwig | }; |
3723 | c16b5a2c | Christoph Hellwig | |
3724 | ce1a14dc | pbrook | static void bdrv_aio_bh_cb(void *opaque) |
3725 | 83f64091 | bellard | { |
3726 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = opaque; |
3727 | f141eafe | aliguori | |
3728 | f141eafe | aliguori | if (!acb->is_write)
|
3729 | 03396148 | Michael Tokarev | qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
|
3730 | ceb42de8 | aliguori | qemu_vfree(acb->bounce); |
3731 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, acb->ret); |
3732 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
3733 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
3734 | ce1a14dc | pbrook | qemu_aio_release(acb); |
3735 | 83f64091 | bellard | } |
3736 | beac80cd | bellard | |
3737 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
|
3738 | f141eafe | aliguori | int64_t sector_num, |
3739 | f141eafe | aliguori | QEMUIOVector *qiov, |
3740 | f141eafe | aliguori | int nb_sectors,
|
3741 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, |
3742 | f141eafe | aliguori | void *opaque,
|
3743 | f141eafe | aliguori | int is_write)
|
3744 | f141eafe | aliguori | |
3745 | 83f64091 | bellard | { |
3746 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
3747 | ce1a14dc | pbrook | |
3748 | d7331bed | Stefan Hajnoczi | acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); |
3749 | f141eafe | aliguori | acb->is_write = is_write; |
3750 | f141eafe | aliguori | acb->qiov = qiov; |
3751 | e268ca52 | aliguori | acb->bounce = qemu_blockalign(bs, qiov->size); |
3752 | 3f3aace8 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
3753 | f141eafe | aliguori | |
3754 | f141eafe | aliguori | if (is_write) {
|
3755 | d5e6b161 | Michael Tokarev | qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
|
3756 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
3757 | f141eafe | aliguori | } else {
|
3758 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
3759 | f141eafe | aliguori | } |
3760 | f141eafe | aliguori | |
3761 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
3762 | f141eafe | aliguori | |
3763 | ce1a14dc | pbrook | return &acb->common;
|
3764 | beac80cd | bellard | } |
3765 | beac80cd | bellard | |
3766 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
3767 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
3768 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
3769 | beac80cd | bellard | { |
3770 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
3771 | f141eafe | aliguori | } |
3772 | 83f64091 | bellard | |
3773 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
3774 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
3775 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
3776 | f141eafe | aliguori | { |
3777 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
3778 | beac80cd | bellard | } |
3779 | beac80cd | bellard | |
3780 | 68485420 | Kevin Wolf | |
3781 | 68485420 | Kevin Wolf | typedef struct BlockDriverAIOCBCoroutine { |
3782 | 68485420 | Kevin Wolf | BlockDriverAIOCB common; |
3783 | 68485420 | Kevin Wolf | BlockRequest req; |
3784 | 68485420 | Kevin Wolf | bool is_write;
|
3785 | d318aea9 | Kevin Wolf | bool *done;
|
3786 | 68485420 | Kevin Wolf | QEMUBH* bh; |
3787 | 68485420 | Kevin Wolf | } BlockDriverAIOCBCoroutine; |
3788 | 68485420 | Kevin Wolf | |
3789 | 68485420 | Kevin Wolf | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) |
3790 | 68485420 | Kevin Wolf | { |
3791 | d318aea9 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb = |
3792 | d318aea9 | Kevin Wolf | container_of(blockacb, BlockDriverAIOCBCoroutine, common); |
3793 | d318aea9 | Kevin Wolf | bool done = false; |
3794 | d318aea9 | Kevin Wolf | |
3795 | d318aea9 | Kevin Wolf | acb->done = &done; |
3796 | d318aea9 | Kevin Wolf | while (!done) {
|
3797 | d318aea9 | Kevin Wolf | qemu_aio_wait(); |
3798 | d318aea9 | Kevin Wolf | } |
3799 | 68485420 | Kevin Wolf | } |
3800 | 68485420 | Kevin Wolf | |
3801 | d7331bed | Stefan Hajnoczi | static const AIOCBInfo bdrv_em_co_aiocb_info = { |
3802 | 68485420 | Kevin Wolf | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
|
3803 | 68485420 | Kevin Wolf | .cancel = bdrv_aio_co_cancel_em, |
3804 | 68485420 | Kevin Wolf | }; |
3805 | 68485420 | Kevin Wolf | |
3806 | 35246a68 | Paolo Bonzini | static void bdrv_co_em_bh(void *opaque) |
3807 | 68485420 | Kevin Wolf | { |
3808 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb = opaque; |
3809 | 68485420 | Kevin Wolf | |
3810 | 68485420 | Kevin Wolf | acb->common.cb(acb->common.opaque, acb->req.error); |
3811 | d318aea9 | Kevin Wolf | |
3812 | d318aea9 | Kevin Wolf | if (acb->done) {
|
3813 | d318aea9 | Kevin Wolf | *acb->done = true;
|
3814 | d318aea9 | Kevin Wolf | } |
3815 | d318aea9 | Kevin Wolf | |
3816 | 68485420 | Kevin Wolf | qemu_bh_delete(acb->bh); |
3817 | 68485420 | Kevin Wolf | qemu_aio_release(acb); |
3818 | 68485420 | Kevin Wolf | } |
3819 | 68485420 | Kevin Wolf | |
3820 | b2a61371 | Stefan Hajnoczi | /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
|
3821 | b2a61371 | Stefan Hajnoczi | static void coroutine_fn bdrv_co_do_rw(void *opaque) |
3822 | b2a61371 | Stefan Hajnoczi | { |
3823 | b2a61371 | Stefan Hajnoczi | BlockDriverAIOCBCoroutine *acb = opaque; |
3824 | b2a61371 | Stefan Hajnoczi | BlockDriverState *bs = acb->common.bs; |
3825 | b2a61371 | Stefan Hajnoczi | |
3826 | b2a61371 | Stefan Hajnoczi | if (!acb->is_write) {
|
3827 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, |
3828 | 470c0504 | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov, 0);
|
3829 | b2a61371 | Stefan Hajnoczi | } else {
|
3830 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, |
3831 | f08f2dda | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov, 0);
|
3832 | b2a61371 | Stefan Hajnoczi | } |
3833 | b2a61371 | Stefan Hajnoczi | |
3834 | 35246a68 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3835 | b2a61371 | Stefan Hajnoczi | qemu_bh_schedule(acb->bh); |
3836 | b2a61371 | Stefan Hajnoczi | } |
3837 | b2a61371 | Stefan Hajnoczi | |
3838 | 68485420 | Kevin Wolf | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
|
3839 | 68485420 | Kevin Wolf | int64_t sector_num, |
3840 | 68485420 | Kevin Wolf | QEMUIOVector *qiov, |
3841 | 68485420 | Kevin Wolf | int nb_sectors,
|
3842 | 68485420 | Kevin Wolf | BlockDriverCompletionFunc *cb, |
3843 | 68485420 | Kevin Wolf | void *opaque,
|
3844 | 8c5873d6 | Stefan Hajnoczi | bool is_write)
|
3845 | 68485420 | Kevin Wolf | { |
3846 | 68485420 | Kevin Wolf | Coroutine *co; |
3847 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb; |
3848 | 68485420 | Kevin Wolf | |
3849 | d7331bed | Stefan Hajnoczi | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
3850 | 68485420 | Kevin Wolf | acb->req.sector = sector_num; |
3851 | 68485420 | Kevin Wolf | acb->req.nb_sectors = nb_sectors; |
3852 | 68485420 | Kevin Wolf | acb->req.qiov = qiov; |
3853 | 68485420 | Kevin Wolf | acb->is_write = is_write; |
3854 | d318aea9 | Kevin Wolf | acb->done = NULL;
|
3855 | 68485420 | Kevin Wolf | |
3856 | 8c5873d6 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_co_do_rw); |
3857 | 68485420 | Kevin Wolf | qemu_coroutine_enter(co, acb); |
3858 | 68485420 | Kevin Wolf | |
3859 | 68485420 | Kevin Wolf | return &acb->common;
|
3860 | 68485420 | Kevin Wolf | } |
3861 | 68485420 | Kevin Wolf | |
3862 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) |
3863 | b2e12bc6 | Christoph Hellwig | { |
3864 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb = opaque; |
3865 | 07f07615 | Paolo Bonzini | BlockDriverState *bs = acb->common.bs; |
3866 | b2e12bc6 | Christoph Hellwig | |
3867 | 07f07615 | Paolo Bonzini | acb->req.error = bdrv_co_flush(bs); |
3868 | 07f07615 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3869 | b2e12bc6 | Christoph Hellwig | qemu_bh_schedule(acb->bh); |
3870 | b2e12bc6 | Christoph Hellwig | } |
3871 | b2e12bc6 | Christoph Hellwig | |
3872 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
3873 | 016f5cf6 | Alexander Graf | BlockDriverCompletionFunc *cb, void *opaque)
|
3874 | 016f5cf6 | Alexander Graf | { |
3875 | 07f07615 | Paolo Bonzini | trace_bdrv_aio_flush(bs, opaque); |
3876 | 016f5cf6 | Alexander Graf | |
3877 | 07f07615 | Paolo Bonzini | Coroutine *co; |
3878 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb; |
3879 | 016f5cf6 | Alexander Graf | |
3880 | d7331bed | Stefan Hajnoczi | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
3881 | d318aea9 | Kevin Wolf | acb->done = NULL;
|
3882 | d318aea9 | Kevin Wolf | |
3883 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_aio_flush_co_entry); |
3884 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, acb); |
3885 | 016f5cf6 | Alexander Graf | |
3886 | 016f5cf6 | Alexander Graf | return &acb->common;
|
3887 | 016f5cf6 | Alexander Graf | } |
3888 | 016f5cf6 | Alexander Graf | |
3889 | 4265d620 | Paolo Bonzini | static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) |
3890 | 4265d620 | Paolo Bonzini | { |
3891 | 4265d620 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb = opaque; |
3892 | 4265d620 | Paolo Bonzini | BlockDriverState *bs = acb->common.bs; |
3893 | 4265d620 | Paolo Bonzini | |
3894 | 4265d620 | Paolo Bonzini | acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); |
3895 | 4265d620 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
3896 | 4265d620 | Paolo Bonzini | qemu_bh_schedule(acb->bh); |
3897 | 4265d620 | Paolo Bonzini | } |
3898 | 4265d620 | Paolo Bonzini | |
3899 | 4265d620 | Paolo Bonzini | BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, |
3900 | 4265d620 | Paolo Bonzini | int64_t sector_num, int nb_sectors,
|
3901 | 4265d620 | Paolo Bonzini | BlockDriverCompletionFunc *cb, void *opaque)
|
3902 | 4265d620 | Paolo Bonzini | { |
3903 | 4265d620 | Paolo Bonzini | Coroutine *co; |
3904 | 4265d620 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb; |
3905 | 4265d620 | Paolo Bonzini | |
3906 | 4265d620 | Paolo Bonzini | trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); |
3907 | 4265d620 | Paolo Bonzini | |
3908 | d7331bed | Stefan Hajnoczi | acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); |
3909 | 4265d620 | Paolo Bonzini | acb->req.sector = sector_num; |
3910 | 4265d620 | Paolo Bonzini | acb->req.nb_sectors = nb_sectors; |
3911 | d318aea9 | Kevin Wolf | acb->done = NULL;
|
3912 | 4265d620 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_aio_discard_co_entry); |
3913 | 4265d620 | Paolo Bonzini | qemu_coroutine_enter(co, acb); |
3914 | 4265d620 | Paolo Bonzini | |
3915 | 4265d620 | Paolo Bonzini | return &acb->common;
|
3916 | 4265d620 | Paolo Bonzini | } |
3917 | 4265d620 | Paolo Bonzini | |
3918 | ea2384d3 | bellard | void bdrv_init(void) |
3919 | ea2384d3 | bellard | { |
3920 | 5efa9d5a | Anthony Liguori | module_call_init(MODULE_INIT_BLOCK); |
3921 | ea2384d3 | bellard | } |
3922 | ce1a14dc | pbrook | |
3923 | eb852011 | Markus Armbruster | void bdrv_init_with_whitelist(void) |
3924 | eb852011 | Markus Armbruster | { |
3925 | eb852011 | Markus Armbruster | use_bdrv_whitelist = 1;
|
3926 | eb852011 | Markus Armbruster | bdrv_init(); |
3927 | eb852011 | Markus Armbruster | } |
3928 | eb852011 | Markus Armbruster | |
3929 | d7331bed | Stefan Hajnoczi | void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, |
3930 | c16b5a2c | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque)
|
3931 | ce1a14dc | pbrook | { |
3932 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
3933 | ce1a14dc | pbrook | |
3934 | d7331bed | Stefan Hajnoczi | acb = g_slice_alloc(aiocb_info->aiocb_size); |
3935 | d7331bed | Stefan Hajnoczi | acb->aiocb_info = aiocb_info; |
3936 | ce1a14dc | pbrook | acb->bs = bs; |
3937 | ce1a14dc | pbrook | acb->cb = cb; |
3938 | ce1a14dc | pbrook | acb->opaque = opaque; |
3939 | ce1a14dc | pbrook | return acb;
|
3940 | ce1a14dc | pbrook | } |
3941 | ce1a14dc | pbrook | |
3942 | ce1a14dc | pbrook | void qemu_aio_release(void *p) |
3943 | ce1a14dc | pbrook | { |
3944 | d37c975f | Stefan Hajnoczi | BlockDriverAIOCB *acb = p; |
3945 | d7331bed | Stefan Hajnoczi | g_slice_free1(acb->aiocb_info->aiocb_size, acb); |
3946 | ce1a14dc | pbrook | } |
3947 | 19cb3738 | bellard | |
3948 | 19cb3738 | bellard | /**************************************************************/
|
3949 | f9f05dc5 | Kevin Wolf | /* Coroutine block device emulation */
|
3950 | f9f05dc5 | Kevin Wolf | |
3951 | f9f05dc5 | Kevin Wolf | typedef struct CoroutineIOCompletion { |
3952 | f9f05dc5 | Kevin Wolf | Coroutine *coroutine; |
3953 | f9f05dc5 | Kevin Wolf | int ret;
|
3954 | f9f05dc5 | Kevin Wolf | } CoroutineIOCompletion; |
3955 | f9f05dc5 | Kevin Wolf | |
3956 | f9f05dc5 | Kevin Wolf | static void bdrv_co_io_em_complete(void *opaque, int ret) |
3957 | f9f05dc5 | Kevin Wolf | { |
3958 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion *co = opaque; |
3959 | f9f05dc5 | Kevin Wolf | |
3960 | f9f05dc5 | Kevin Wolf | co->ret = ret; |
3961 | f9f05dc5 | Kevin Wolf | qemu_coroutine_enter(co->coroutine, NULL);
|
3962 | f9f05dc5 | Kevin Wolf | } |
3963 | f9f05dc5 | Kevin Wolf | |
3964 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, |
3965 | f9f05dc5 | Kevin Wolf | int nb_sectors, QEMUIOVector *iov,
|
3966 | f9f05dc5 | Kevin Wolf | bool is_write)
|
3967 | f9f05dc5 | Kevin Wolf | { |
3968 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion co = { |
3969 | f9f05dc5 | Kevin Wolf | .coroutine = qemu_coroutine_self(), |
3970 | f9f05dc5 | Kevin Wolf | }; |
3971 | f9f05dc5 | Kevin Wolf | BlockDriverAIOCB *acb; |
3972 | f9f05dc5 | Kevin Wolf | |
3973 | f9f05dc5 | Kevin Wolf | if (is_write) {
|
3974 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, |
3975 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
3976 | f9f05dc5 | Kevin Wolf | } else {
|
3977 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, |
3978 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
3979 | f9f05dc5 | Kevin Wolf | } |
3980 | f9f05dc5 | Kevin Wolf | |
3981 | 59370aaa | Stefan Hajnoczi | trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); |
3982 | f9f05dc5 | Kevin Wolf | if (!acb) {
|
3983 | f9f05dc5 | Kevin Wolf | return -EIO;
|
3984 | f9f05dc5 | Kevin Wolf | } |
3985 | f9f05dc5 | Kevin Wolf | qemu_coroutine_yield(); |
3986 | f9f05dc5 | Kevin Wolf | |
3987 | f9f05dc5 | Kevin Wolf | return co.ret;
|
3988 | f9f05dc5 | Kevin Wolf | } |
3989 | f9f05dc5 | Kevin Wolf | |
3990 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
3991 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
3992 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov) |
3993 | f9f05dc5 | Kevin Wolf | { |
3994 | f9f05dc5 | Kevin Wolf | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); |
3995 | f9f05dc5 | Kevin Wolf | } |
3996 | f9f05dc5 | Kevin Wolf | |
3997 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_writev_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, true); |
4002 | f9f05dc5 | Kevin Wolf | } |
4003 | f9f05dc5 | Kevin Wolf | |
4004 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
4005 | e7a8a783 | Kevin Wolf | { |
4006 | 07f07615 | Paolo Bonzini | RwCo *rwco = opaque; |
4007 | 07f07615 | Paolo Bonzini | |
4008 | 07f07615 | Paolo Bonzini | rwco->ret = bdrv_co_flush(rwco->bs); |
4009 | 07f07615 | Paolo Bonzini | } |
4010 | 07f07615 | Paolo Bonzini | |
4011 | 07f07615 | Paolo Bonzini | int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
|
4012 | 07f07615 | Paolo Bonzini | { |
4013 | eb489bb1 | Kevin Wolf | int ret;
|
4014 | eb489bb1 | Kevin Wolf | |
4015 | 29cdb251 | Paolo Bonzini | if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
4016 | 07f07615 | Paolo Bonzini | return 0; |
4017 | eb489bb1 | Kevin Wolf | } |
4018 | eb489bb1 | Kevin Wolf | |
4019 | ca716364 | Kevin Wolf | /* Write back cached data to the OS even with cache=unsafe */
|
4020 | bf736fe3 | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); |
4021 | eb489bb1 | Kevin Wolf | if (bs->drv->bdrv_co_flush_to_os) {
|
4022 | eb489bb1 | Kevin Wolf | ret = bs->drv->bdrv_co_flush_to_os(bs); |
4023 | eb489bb1 | Kevin Wolf | if (ret < 0) { |
4024 | eb489bb1 | Kevin Wolf | return ret;
|
4025 | eb489bb1 | Kevin Wolf | } |
4026 | eb489bb1 | Kevin Wolf | } |
4027 | eb489bb1 | Kevin Wolf | |
4028 | ca716364 | Kevin Wolf | /* But don't actually force it to the disk with cache=unsafe */
|
4029 | ca716364 | Kevin Wolf | if (bs->open_flags & BDRV_O_NO_FLUSH) {
|
4030 | d4c82329 | Kevin Wolf | goto flush_parent;
|
4031 | ca716364 | Kevin Wolf | } |
4032 | ca716364 | Kevin Wolf | |
4033 | bf736fe3 | Kevin Wolf | BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); |
4034 | eb489bb1 | Kevin Wolf | if (bs->drv->bdrv_co_flush_to_disk) {
|
4035 | 29cdb251 | Paolo Bonzini | ret = bs->drv->bdrv_co_flush_to_disk(bs); |
4036 | 07f07615 | Paolo Bonzini | } else if (bs->drv->bdrv_aio_flush) { |
4037 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *acb; |
4038 | 07f07615 | Paolo Bonzini | CoroutineIOCompletion co = { |
4039 | 07f07615 | Paolo Bonzini | .coroutine = qemu_coroutine_self(), |
4040 | 07f07615 | Paolo Bonzini | }; |
4041 | 07f07615 | Paolo Bonzini | |
4042 | 07f07615 | Paolo Bonzini | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); |
4043 | 07f07615 | Paolo Bonzini | if (acb == NULL) { |
4044 | 29cdb251 | Paolo Bonzini | ret = -EIO; |
4045 | 07f07615 | Paolo Bonzini | } else {
|
4046 | 07f07615 | Paolo Bonzini | qemu_coroutine_yield(); |
4047 | 29cdb251 | Paolo Bonzini | ret = co.ret; |
4048 | 07f07615 | Paolo Bonzini | } |
4049 | 07f07615 | Paolo Bonzini | } else {
|
4050 | 07f07615 | Paolo Bonzini | /*
|
4051 | 07f07615 | Paolo Bonzini | * Some block drivers always operate in either writethrough or unsafe
|
4052 | 07f07615 | Paolo Bonzini | * mode and don't support bdrv_flush therefore. Usually qemu doesn't
|
4053 | 07f07615 | Paolo Bonzini | * know how the server works (because the behaviour is hardcoded or
|
4054 | 07f07615 | Paolo Bonzini | * depends on server-side configuration), so we can't ensure that
|
4055 | 07f07615 | Paolo Bonzini | * everything is safe on disk. Returning an error doesn't work because
|
4056 | 07f07615 | Paolo Bonzini | * that would break guests even if the server operates in writethrough
|
4057 | 07f07615 | Paolo Bonzini | * mode.
|
4058 | 07f07615 | Paolo Bonzini | *
|
4059 | 07f07615 | Paolo Bonzini | * Let's hope the user knows what he's doing.
|
4060 | 07f07615 | Paolo Bonzini | */
|
4061 | 29cdb251 | Paolo Bonzini | ret = 0;
|
4062 | 07f07615 | Paolo Bonzini | } |
4063 | 29cdb251 | Paolo Bonzini | if (ret < 0) { |
4064 | 29cdb251 | Paolo Bonzini | return ret;
|
4065 | 29cdb251 | Paolo Bonzini | } |
4066 | 29cdb251 | Paolo Bonzini | |
4067 | 29cdb251 | Paolo Bonzini | /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH
|
4068 | 29cdb251 | Paolo Bonzini | * in the case of cache=unsafe, so there are no useless flushes.
|
4069 | 29cdb251 | Paolo Bonzini | */
|
4070 | d4c82329 | Kevin Wolf | flush_parent:
|
4071 | 29cdb251 | Paolo Bonzini | return bdrv_co_flush(bs->file);
|
4072 | 07f07615 | Paolo Bonzini | } |
4073 | 07f07615 | Paolo Bonzini | |
4074 | 0f15423c | Anthony Liguori | void bdrv_invalidate_cache(BlockDriverState *bs)
|
4075 | 0f15423c | Anthony Liguori | { |
4076 | 0f15423c | Anthony Liguori | if (bs->drv && bs->drv->bdrv_invalidate_cache) {
|
4077 | 0f15423c | Anthony Liguori | bs->drv->bdrv_invalidate_cache(bs); |
4078 | 0f15423c | Anthony Liguori | } |
4079 | 0f15423c | Anthony Liguori | } |
4080 | 0f15423c | Anthony Liguori | |
4081 | 0f15423c | Anthony Liguori | void bdrv_invalidate_cache_all(void) |
4082 | 0f15423c | Anthony Liguori | { |
4083 | 0f15423c | Anthony Liguori | BlockDriverState *bs; |
4084 | 0f15423c | Anthony Liguori | |
4085 | 0f15423c | Anthony Liguori | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
4086 | 0f15423c | Anthony Liguori | bdrv_invalidate_cache(bs); |
4087 | 0f15423c | Anthony Liguori | } |
4088 | 0f15423c | Anthony Liguori | } |
4089 | 0f15423c | Anthony Liguori | |
4090 | 07789269 | Benoรฎt Canet | void bdrv_clear_incoming_migration_all(void) |
4091 | 07789269 | Benoรฎt Canet | { |
4092 | 07789269 | Benoรฎt Canet | BlockDriverState *bs; |
4093 | 07789269 | Benoรฎt Canet | |
4094 | 07789269 | Benoรฎt Canet | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
4095 | 07789269 | Benoรฎt Canet | bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); |
4096 | 07789269 | Benoรฎt Canet | } |
4097 | 07789269 | Benoรฎt Canet | } |
4098 | 07789269 | Benoรฎt Canet | |
4099 | 07f07615 | Paolo Bonzini | int bdrv_flush(BlockDriverState *bs)
|
4100 | 07f07615 | Paolo Bonzini | { |
4101 | 07f07615 | Paolo Bonzini | Coroutine *co; |
4102 | 07f07615 | Paolo Bonzini | RwCo rwco = { |
4103 | 07f07615 | Paolo Bonzini | .bs = bs, |
4104 | 07f07615 | Paolo Bonzini | .ret = NOT_DONE, |
4105 | e7a8a783 | Kevin Wolf | }; |
4106 | e7a8a783 | Kevin Wolf | |
4107 | 07f07615 | Paolo Bonzini | if (qemu_in_coroutine()) {
|
4108 | 07f07615 | Paolo Bonzini | /* Fast-path if already in coroutine context */
|
4109 | 07f07615 | Paolo Bonzini | bdrv_flush_co_entry(&rwco); |
4110 | 07f07615 | Paolo Bonzini | } else {
|
4111 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_flush_co_entry); |
4112 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, &rwco); |
4113 | 07f07615 | Paolo Bonzini | while (rwco.ret == NOT_DONE) {
|
4114 | 07f07615 | Paolo Bonzini | qemu_aio_wait(); |
4115 | 07f07615 | Paolo Bonzini | } |
4116 | e7a8a783 | Kevin Wolf | } |
4117 | 07f07615 | Paolo Bonzini | |
4118 | 07f07615 | Paolo Bonzini | return rwco.ret;
|
4119 | e7a8a783 | Kevin Wolf | } |
4120 | e7a8a783 | Kevin Wolf | |
4121 | 4265d620 | Paolo Bonzini | static void coroutine_fn bdrv_discard_co_entry(void *opaque) |
4122 | 4265d620 | Paolo Bonzini | { |
4123 | 4265d620 | Paolo Bonzini | RwCo *rwco = opaque; |
4124 | 4265d620 | Paolo Bonzini | |
4125 | 4265d620 | Paolo Bonzini | rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); |
4126 | 4265d620 | Paolo Bonzini | } |
4127 | 4265d620 | Paolo Bonzini | |
4128 | 4265d620 | Paolo Bonzini | int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
|
4129 | 4265d620 | Paolo Bonzini | int nb_sectors)
|
4130 | 4265d620 | Paolo Bonzini | { |
4131 | 4265d620 | Paolo Bonzini | if (!bs->drv) {
|
4132 | 4265d620 | Paolo Bonzini | return -ENOMEDIUM;
|
4133 | 4265d620 | Paolo Bonzini | } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { |
4134 | 4265d620 | Paolo Bonzini | return -EIO;
|
4135 | 4265d620 | Paolo Bonzini | } else if (bs->read_only) { |
4136 | 4265d620 | Paolo Bonzini | return -EROFS;
|
4137 | df702c9b | Paolo Bonzini | } |
4138 | df702c9b | Paolo Bonzini | |
4139 | df702c9b | Paolo Bonzini | if (bs->dirty_bitmap) {
|
4140 | 8f0720ec | Paolo Bonzini | bdrv_reset_dirty(bs, sector_num, nb_sectors); |
4141 | df702c9b | Paolo Bonzini | } |
4142 | df702c9b | Paolo Bonzini | |
4143 | 9e8f1835 | Paolo Bonzini | /* Do nothing if disabled. */
|
4144 | 9e8f1835 | Paolo Bonzini | if (!(bs->open_flags & BDRV_O_UNMAP)) {
|
4145 | 9e8f1835 | Paolo Bonzini | return 0; |
4146 | 9e8f1835 | Paolo Bonzini | } |
4147 | 9e8f1835 | Paolo Bonzini | |
4148 | df702c9b | Paolo Bonzini | if (bs->drv->bdrv_co_discard) {
|
4149 | 4265d620 | Paolo Bonzini | return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
|
4150 | 4265d620 | Paolo Bonzini | } else if (bs->drv->bdrv_aio_discard) { |
4151 | 4265d620 | Paolo Bonzini | BlockDriverAIOCB *acb; |
4152 | 4265d620 | Paolo Bonzini | CoroutineIOCompletion co = { |
4153 | 4265d620 | Paolo Bonzini | .coroutine = qemu_coroutine_self(), |
4154 | 4265d620 | Paolo Bonzini | }; |
4155 | 4265d620 | Paolo Bonzini | |
4156 | 4265d620 | Paolo Bonzini | acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, |
4157 | 4265d620 | Paolo Bonzini | bdrv_co_io_em_complete, &co); |
4158 | 4265d620 | Paolo Bonzini | if (acb == NULL) { |
4159 | 4265d620 | Paolo Bonzini | return -EIO;
|
4160 | 4265d620 | Paolo Bonzini | } else {
|
4161 | 4265d620 | Paolo Bonzini | qemu_coroutine_yield(); |
4162 | 4265d620 | Paolo Bonzini | return co.ret;
|
4163 | 4265d620 | Paolo Bonzini | } |
4164 | 4265d620 | Paolo Bonzini | } else {
|
4165 | 4265d620 | Paolo Bonzini | return 0; |
4166 | 4265d620 | Paolo Bonzini | } |
4167 | 4265d620 | Paolo Bonzini | } |
4168 | 4265d620 | Paolo Bonzini | |
4169 | 4265d620 | Paolo Bonzini | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) |
4170 | 4265d620 | Paolo Bonzini | { |
4171 | 4265d620 | Paolo Bonzini | Coroutine *co; |
4172 | 4265d620 | Paolo Bonzini | RwCo rwco = { |
4173 | 4265d620 | Paolo Bonzini | .bs = bs, |
4174 | 4265d620 | Paolo Bonzini | .sector_num = sector_num, |
4175 | 4265d620 | Paolo Bonzini | .nb_sectors = nb_sectors, |
4176 | 4265d620 | Paolo Bonzini | .ret = NOT_DONE, |
4177 | 4265d620 | Paolo Bonzini | }; |
4178 | 4265d620 | Paolo Bonzini | |
4179 | 4265d620 | Paolo Bonzini | if (qemu_in_coroutine()) {
|
4180 | 4265d620 | Paolo Bonzini | /* Fast-path if already in coroutine context */
|
4181 | 4265d620 | Paolo Bonzini | bdrv_discard_co_entry(&rwco); |
4182 | 4265d620 | Paolo Bonzini | } else {
|
4183 | 4265d620 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_discard_co_entry); |
4184 | 4265d620 | Paolo Bonzini | qemu_coroutine_enter(co, &rwco); |
4185 | 4265d620 | Paolo Bonzini | while (rwco.ret == NOT_DONE) {
|
4186 | 4265d620 | Paolo Bonzini | qemu_aio_wait(); |
4187 | 4265d620 | Paolo Bonzini | } |
4188 | 4265d620 | Paolo Bonzini | } |
4189 | 4265d620 | Paolo Bonzini | |
4190 | 4265d620 | Paolo Bonzini | return rwco.ret;
|
4191 | 4265d620 | Paolo Bonzini | } |
4192 | 4265d620 | Paolo Bonzini | |
4193 | f9f05dc5 | Kevin Wolf | /**************************************************************/
|
4194 | 19cb3738 | bellard | /* removable device support */
|
4195 | 19cb3738 | bellard | |
4196 | 19cb3738 | bellard | /**
|
4197 | 19cb3738 | bellard | * Return TRUE if the media is present
|
4198 | 19cb3738 | bellard | */
|
4199 | 19cb3738 | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
4200 | 19cb3738 | bellard | { |
4201 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
4202 | a1aff5bf | Markus Armbruster | |
4203 | 19cb3738 | bellard | if (!drv)
|
4204 | 19cb3738 | bellard | return 0; |
4205 | 19cb3738 | bellard | if (!drv->bdrv_is_inserted)
|
4206 | a1aff5bf | Markus Armbruster | return 1; |
4207 | a1aff5bf | Markus Armbruster | return drv->bdrv_is_inserted(bs);
|
4208 | 19cb3738 | bellard | } |
4209 | 19cb3738 | bellard | |
4210 | 19cb3738 | bellard | /**
|
4211 | 8e49ca46 | Markus Armbruster | * Return whether the media changed since the last call to this
|
4212 | 8e49ca46 | Markus Armbruster | * function, or -ENOTSUP if we don't know. Most drivers don't know.
|
4213 | 19cb3738 | bellard | */
|
4214 | 19cb3738 | bellard | int bdrv_media_changed(BlockDriverState *bs)
|
4215 | 19cb3738 | bellard | { |
4216 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
4217 | 19cb3738 | bellard | |
4218 | 8e49ca46 | Markus Armbruster | if (drv && drv->bdrv_media_changed) {
|
4219 | 8e49ca46 | Markus Armbruster | return drv->bdrv_media_changed(bs);
|
4220 | 8e49ca46 | Markus Armbruster | } |
4221 | 8e49ca46 | Markus Armbruster | return -ENOTSUP;
|
4222 | 19cb3738 | bellard | } |
4223 | 19cb3738 | bellard | |
4224 | 19cb3738 | bellard | /**
|
4225 | 19cb3738 | bellard | * If eject_flag is TRUE, eject the media. Otherwise, close the tray
|
4226 | 19cb3738 | bellard | */
|
4227 | f36f3949 | Luiz Capitulino | void bdrv_eject(BlockDriverState *bs, bool eject_flag) |
4228 | 19cb3738 | bellard | { |
4229 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
4230 | 19cb3738 | bellard | |
4231 | 822e1cd1 | Markus Armbruster | if (drv && drv->bdrv_eject) {
|
4232 | 822e1cd1 | Markus Armbruster | drv->bdrv_eject(bs, eject_flag); |
4233 | 19cb3738 | bellard | } |
4234 | 6f382ed2 | Luiz Capitulino | |
4235 | 6f382ed2 | Luiz Capitulino | if (bs->device_name[0] != '\0') { |
4236 | 6f382ed2 | Luiz Capitulino | bdrv_emit_qmp_eject_event(bs, eject_flag); |
4237 | 6f382ed2 | Luiz Capitulino | } |
4238 | 19cb3738 | bellard | } |
4239 | 19cb3738 | bellard | |
4240 | 19cb3738 | bellard | /**
|
4241 | 19cb3738 | bellard | * Lock or unlock the media (if it is locked, the user won't be able
|
4242 | 19cb3738 | bellard | * to eject it manually).
|
4243 | 19cb3738 | bellard | */
|
4244 | 025e849a | Markus Armbruster | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
4245 | 19cb3738 | bellard | { |
4246 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
4247 | 19cb3738 | bellard | |
4248 | 025e849a | Markus Armbruster | trace_bdrv_lock_medium(bs, locked); |
4249 | b8c6d095 | Stefan Hajnoczi | |
4250 | 025e849a | Markus Armbruster | if (drv && drv->bdrv_lock_medium) {
|
4251 | 025e849a | Markus Armbruster | drv->bdrv_lock_medium(bs, locked); |
4252 | 19cb3738 | bellard | } |
4253 | 19cb3738 | bellard | } |
4254 | 985a03b0 | ths | |
4255 | 985a03b0 | ths | /* needed for generic scsi interface */
|
4256 | 985a03b0 | ths | |
4257 | 985a03b0 | ths | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
4258 | 985a03b0 | ths | { |
4259 | 985a03b0 | ths | BlockDriver *drv = bs->drv; |
4260 | 985a03b0 | ths | |
4261 | 985a03b0 | ths | if (drv && drv->bdrv_ioctl)
|
4262 | 985a03b0 | ths | return drv->bdrv_ioctl(bs, req, buf);
|
4263 | 985a03b0 | ths | return -ENOTSUP;
|
4264 | 985a03b0 | ths | } |
4265 | 7d780669 | aliguori | |
4266 | 221f715d | aliguori | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
4267 | 221f715d | aliguori | unsigned long int req, void *buf, |
4268 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
4269 | 7d780669 | aliguori | { |
4270 | 221f715d | aliguori | BlockDriver *drv = bs->drv; |
4271 | 7d780669 | aliguori | |
4272 | 221f715d | aliguori | if (drv && drv->bdrv_aio_ioctl)
|
4273 | 221f715d | aliguori | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
|
4274 | 221f715d | aliguori | return NULL; |
4275 | 7d780669 | aliguori | } |
4276 | e268ca52 | aliguori | |
4277 | 7b6f9300 | Markus Armbruster | void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) |
4278 | 7b6f9300 | Markus Armbruster | { |
4279 | 7b6f9300 | Markus Armbruster | bs->buffer_alignment = align; |
4280 | 7b6f9300 | Markus Armbruster | } |
4281 | 7cd1e32a | lirans@il.ibm.com | |
4282 | e268ca52 | aliguori | void *qemu_blockalign(BlockDriverState *bs, size_t size)
|
4283 | e268ca52 | aliguori | { |
4284 | e268ca52 | aliguori | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
4285 | e268ca52 | aliguori | } |
4286 | 7cd1e32a | lirans@il.ibm.com | |
4287 | c53b1c51 | Stefan Hajnoczi | /*
|
4288 | c53b1c51 | Stefan Hajnoczi | * Check if all memory in this vector is sector aligned.
|
4289 | c53b1c51 | Stefan Hajnoczi | */
|
4290 | c53b1c51 | Stefan Hajnoczi | bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
|
4291 | c53b1c51 | Stefan Hajnoczi | { |
4292 | c53b1c51 | Stefan Hajnoczi | int i;
|
4293 | c53b1c51 | Stefan Hajnoczi | |
4294 | c53b1c51 | Stefan Hajnoczi | for (i = 0; i < qiov->niov; i++) { |
4295 | c53b1c51 | Stefan Hajnoczi | if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
|
4296 | c53b1c51 | Stefan Hajnoczi | return false; |
4297 | c53b1c51 | Stefan Hajnoczi | } |
4298 | c53b1c51 | Stefan Hajnoczi | } |
4299 | c53b1c51 | Stefan Hajnoczi | |
4300 | c53b1c51 | Stefan Hajnoczi | return true; |
4301 | c53b1c51 | Stefan Hajnoczi | } |
4302 | c53b1c51 | Stefan Hajnoczi | |
4303 | 50717e94 | Paolo Bonzini | void bdrv_set_dirty_tracking(BlockDriverState *bs, int granularity) |
4304 | 7cd1e32a | lirans@il.ibm.com | { |
4305 | 7cd1e32a | lirans@il.ibm.com | int64_t bitmap_size; |
4306 | a55eb92c | Jan Kiszka | |
4307 | 50717e94 | Paolo Bonzini | assert((granularity & (granularity - 1)) == 0); |
4308 | 50717e94 | Paolo Bonzini | |
4309 | 50717e94 | Paolo Bonzini | if (granularity) {
|
4310 | 50717e94 | Paolo Bonzini | granularity >>= BDRV_SECTOR_BITS; |
4311 | 50717e94 | Paolo Bonzini | assert(!bs->dirty_bitmap); |
4312 | 50717e94 | Paolo Bonzini | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); |
4313 | 50717e94 | Paolo Bonzini | bs->dirty_bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1);
|
4314 | 7cd1e32a | lirans@il.ibm.com | } else {
|
4315 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
4316 | 8f0720ec | Paolo Bonzini | hbitmap_free(bs->dirty_bitmap); |
4317 | c6d22830 | Jan Kiszka | bs->dirty_bitmap = NULL;
|
4318 | a55eb92c | Jan Kiszka | } |
4319 | 7cd1e32a | lirans@il.ibm.com | } |
4320 | 7cd1e32a | lirans@il.ibm.com | } |
4321 | 7cd1e32a | lirans@il.ibm.com | |
4322 | 7cd1e32a | lirans@il.ibm.com | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
|
4323 | 7cd1e32a | lirans@il.ibm.com | { |
4324 | 8f0720ec | Paolo Bonzini | if (bs->dirty_bitmap) {
|
4325 | 8f0720ec | Paolo Bonzini | return hbitmap_get(bs->dirty_bitmap, sector);
|
4326 | 7cd1e32a | lirans@il.ibm.com | } else {
|
4327 | 7cd1e32a | lirans@il.ibm.com | return 0; |
4328 | 7cd1e32a | lirans@il.ibm.com | } |
4329 | 7cd1e32a | lirans@il.ibm.com | } |
4330 | 7cd1e32a | lirans@il.ibm.com | |
4331 | 8f0720ec | Paolo Bonzini | void bdrv_dirty_iter_init(BlockDriverState *bs, HBitmapIter *hbi)
|
4332 | 1755da16 | Paolo Bonzini | { |
4333 | 8f0720ec | Paolo Bonzini | hbitmap_iter_init(hbi, bs->dirty_bitmap, 0);
|
4334 | 1755da16 | Paolo Bonzini | } |
4335 | 1755da16 | Paolo Bonzini | |
4336 | 1755da16 | Paolo Bonzini | void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
|
4337 | 1755da16 | Paolo Bonzini | int nr_sectors)
|
4338 | 1755da16 | Paolo Bonzini | { |
4339 | 8f0720ec | Paolo Bonzini | hbitmap_set(bs->dirty_bitmap, cur_sector, nr_sectors); |
4340 | 1755da16 | Paolo Bonzini | } |
4341 | 1755da16 | Paolo Bonzini | |
4342 | a55eb92c | Jan Kiszka | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
|
4343 | a55eb92c | Jan Kiszka | int nr_sectors)
|
4344 | 7cd1e32a | lirans@il.ibm.com | { |
4345 | 8f0720ec | Paolo Bonzini | hbitmap_reset(bs->dirty_bitmap, cur_sector, nr_sectors); |
4346 | 7cd1e32a | lirans@il.ibm.com | } |
4347 | aaa0eb75 | Liran Schour | |
4348 | aaa0eb75 | Liran Schour | int64_t bdrv_get_dirty_count(BlockDriverState *bs) |
4349 | aaa0eb75 | Liran Schour | { |
4350 | 8f0720ec | Paolo Bonzini | if (bs->dirty_bitmap) {
|
4351 | acc906c6 | Paolo Bonzini | return hbitmap_count(bs->dirty_bitmap);
|
4352 | 8f0720ec | Paolo Bonzini | } else {
|
4353 | 8f0720ec | Paolo Bonzini | return 0; |
4354 | 8f0720ec | Paolo Bonzini | } |
4355 | aaa0eb75 | Liran Schour | } |
4356 | f88e1a42 | Jes Sorensen | |
4357 | db593f25 | Marcelo Tosatti | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
4358 | db593f25 | Marcelo Tosatti | { |
4359 | db593f25 | Marcelo Tosatti | assert(bs->in_use != in_use); |
4360 | db593f25 | Marcelo Tosatti | bs->in_use = in_use; |
4361 | db593f25 | Marcelo Tosatti | } |
4362 | db593f25 | Marcelo Tosatti | |
4363 | db593f25 | Marcelo Tosatti | int bdrv_in_use(BlockDriverState *bs)
|
4364 | db593f25 | Marcelo Tosatti | { |
4365 | db593f25 | Marcelo Tosatti | return bs->in_use;
|
4366 | db593f25 | Marcelo Tosatti | } |
4367 | db593f25 | Marcelo Tosatti | |
4368 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_enable(BlockDriverState *bs)
|
4369 | 28a7282a | Luiz Capitulino | { |
4370 | d6bf279e | Luiz Capitulino | bs->iostatus_enabled = true;
|
4371 | 58e21ef5 | Luiz Capitulino | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
4372 | 28a7282a | Luiz Capitulino | } |
4373 | 28a7282a | Luiz Capitulino | |
4374 | 28a7282a | Luiz Capitulino | /* The I/O status is only enabled if the drive explicitly
|
4375 | 28a7282a | Luiz Capitulino | * enables it _and_ the VM is configured to stop on errors */
|
4376 | 28a7282a | Luiz Capitulino | bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) |
4377 | 28a7282a | Luiz Capitulino | { |
4378 | d6bf279e | Luiz Capitulino | return (bs->iostatus_enabled &&
|
4379 | 92aa5c6d | Paolo Bonzini | (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || |
4380 | 92aa5c6d | Paolo Bonzini | bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || |
4381 | 92aa5c6d | Paolo Bonzini | bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); |
4382 | 28a7282a | Luiz Capitulino | } |
4383 | 28a7282a | Luiz Capitulino | |
4384 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_disable(BlockDriverState *bs)
|
4385 | 28a7282a | Luiz Capitulino | { |
4386 | d6bf279e | Luiz Capitulino | bs->iostatus_enabled = false;
|
4387 | 28a7282a | Luiz Capitulino | } |
4388 | 28a7282a | Luiz Capitulino | |
4389 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_reset(BlockDriverState *bs)
|
4390 | 28a7282a | Luiz Capitulino | { |
4391 | 28a7282a | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs)) {
|
4392 | 58e21ef5 | Luiz Capitulino | bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; |
4393 | 3bd293c3 | Paolo Bonzini | if (bs->job) {
|
4394 | 3bd293c3 | Paolo Bonzini | block_job_iostatus_reset(bs->job); |
4395 | 3bd293c3 | Paolo Bonzini | } |
4396 | 28a7282a | Luiz Capitulino | } |
4397 | 28a7282a | Luiz Capitulino | } |
4398 | 28a7282a | Luiz Capitulino | |
4399 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_set_err(BlockDriverState *bs, int error) |
4400 | 28a7282a | Luiz Capitulino | { |
4401 | 3e1caa5f | Paolo Bonzini | assert(bdrv_iostatus_is_enabled(bs)); |
4402 | 3e1caa5f | Paolo Bonzini | if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
|
4403 | 58e21ef5 | Luiz Capitulino | bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : |
4404 | 58e21ef5 | Luiz Capitulino | BLOCK_DEVICE_IO_STATUS_FAILED; |
4405 | 28a7282a | Luiz Capitulino | } |
4406 | 28a7282a | Luiz Capitulino | } |
4407 | 28a7282a | Luiz Capitulino | |
4408 | a597e79c | Christoph Hellwig | void
|
4409 | a597e79c | Christoph Hellwig | bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, |
4410 | a597e79c | Christoph Hellwig | enum BlockAcctType type)
|
4411 | a597e79c | Christoph Hellwig | { |
4412 | a597e79c | Christoph Hellwig | assert(type < BDRV_MAX_IOTYPE); |
4413 | a597e79c | Christoph Hellwig | |
4414 | a597e79c | Christoph Hellwig | cookie->bytes = bytes; |
4415 | c488c7f6 | Christoph Hellwig | cookie->start_time_ns = get_clock(); |
4416 | a597e79c | Christoph Hellwig | cookie->type = type; |
4417 | a597e79c | Christoph Hellwig | } |
4418 | a597e79c | Christoph Hellwig | |
4419 | a597e79c | Christoph Hellwig | void
|
4420 | a597e79c | Christoph Hellwig | bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) |
4421 | a597e79c | Christoph Hellwig | { |
4422 | a597e79c | Christoph Hellwig | assert(cookie->type < BDRV_MAX_IOTYPE); |
4423 | a597e79c | Christoph Hellwig | |
4424 | a597e79c | Christoph Hellwig | bs->nr_bytes[cookie->type] += cookie->bytes; |
4425 | a597e79c | Christoph Hellwig | bs->nr_ops[cookie->type]++; |
4426 | c488c7f6 | Christoph Hellwig | bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; |
4427 | a597e79c | Christoph Hellwig | } |
4428 | a597e79c | Christoph Hellwig | |
4429 | d92ada22 | Luiz Capitulino | void bdrv_img_create(const char *filename, const char *fmt, |
4430 | d92ada22 | Luiz Capitulino | const char *base_filename, const char *base_fmt, |
4431 | f382d43a | Miroslav Rezanina | char *options, uint64_t img_size, int flags, |
4432 | f382d43a | Miroslav Rezanina | Error **errp, bool quiet)
|
4433 | f88e1a42 | Jes Sorensen | { |
4434 | f88e1a42 | Jes Sorensen | QEMUOptionParameter *param = NULL, *create_options = NULL; |
4435 | d220894e | Kevin Wolf | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
4436 | f88e1a42 | Jes Sorensen | BlockDriverState *bs = NULL;
|
4437 | f88e1a42 | Jes Sorensen | BlockDriver *drv, *proto_drv; |
4438 | 96df67d1 | Stefan Hajnoczi | BlockDriver *backing_drv = NULL;
|
4439 | f88e1a42 | Jes Sorensen | int ret = 0; |
4440 | f88e1a42 | Jes Sorensen | |
4441 | f88e1a42 | Jes Sorensen | /* Find driver and parse its options */
|
4442 | f88e1a42 | Jes Sorensen | drv = bdrv_find_format(fmt); |
4443 | f88e1a42 | Jes Sorensen | if (!drv) {
|
4444 | 71c79813 | Luiz Capitulino | error_setg(errp, "Unknown file format '%s'", fmt);
|
4445 | d92ada22 | Luiz Capitulino | return;
|
4446 | f88e1a42 | Jes Sorensen | } |
4447 | f88e1a42 | Jes Sorensen | |
4448 | f88e1a42 | Jes Sorensen | proto_drv = bdrv_find_protocol(filename); |
4449 | f88e1a42 | Jes Sorensen | if (!proto_drv) {
|
4450 | 71c79813 | Luiz Capitulino | error_setg(errp, "Unknown protocol '%s'", filename);
|
4451 | d92ada22 | Luiz Capitulino | return;
|
4452 | f88e1a42 | Jes Sorensen | } |
4453 | f88e1a42 | Jes Sorensen | |
4454 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
4455 | f88e1a42 | Jes Sorensen | drv->create_options); |
4456 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
4457 | f88e1a42 | Jes Sorensen | proto_drv->create_options); |
4458 | f88e1a42 | Jes Sorensen | |
4459 | f88e1a42 | Jes Sorensen | /* Create parameter list with default values */
|
4460 | f88e1a42 | Jes Sorensen | param = parse_option_parameters("", create_options, param);
|
4461 | f88e1a42 | Jes Sorensen | |
4462 | f88e1a42 | Jes Sorensen | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); |
4463 | f88e1a42 | Jes Sorensen | |
4464 | f88e1a42 | Jes Sorensen | /* Parse -o options */
|
4465 | f88e1a42 | Jes Sorensen | if (options) {
|
4466 | f88e1a42 | Jes Sorensen | param = parse_option_parameters(options, create_options, param); |
4467 | f88e1a42 | Jes Sorensen | if (param == NULL) { |
4468 | 71c79813 | Luiz Capitulino | error_setg(errp, "Invalid options for file format '%s'.", fmt);
|
4469 | f88e1a42 | Jes Sorensen | goto out;
|
4470 | f88e1a42 | Jes Sorensen | } |
4471 | f88e1a42 | Jes Sorensen | } |
4472 | f88e1a42 | Jes Sorensen | |
4473 | f88e1a42 | Jes Sorensen | if (base_filename) {
|
4474 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
|
4475 | f88e1a42 | Jes Sorensen | base_filename)) { |
4476 | 71c79813 | Luiz Capitulino | error_setg(errp, "Backing file not supported for file format '%s'",
|
4477 | 71c79813 | Luiz Capitulino | fmt); |
4478 | f88e1a42 | Jes Sorensen | goto out;
|
4479 | f88e1a42 | Jes Sorensen | } |
4480 | f88e1a42 | Jes Sorensen | } |
4481 | f88e1a42 | Jes Sorensen | |
4482 | f88e1a42 | Jes Sorensen | if (base_fmt) {
|
4483 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
|
4484 | 71c79813 | Luiz Capitulino | error_setg(errp, "Backing file format not supported for file "
|
4485 | 71c79813 | Luiz Capitulino | "format '%s'", fmt);
|
4486 | f88e1a42 | Jes Sorensen | goto out;
|
4487 | f88e1a42 | Jes Sorensen | } |
4488 | f88e1a42 | Jes Sorensen | } |
4489 | f88e1a42 | Jes Sorensen | |
4490 | 792da93a | Jes Sorensen | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
4491 | 792da93a | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
4492 | 792da93a | Jes Sorensen | if (!strcmp(filename, backing_file->value.s)) {
|
4493 | 71c79813 | Luiz Capitulino | error_setg(errp, "Error: Trying to create an image with the "
|
4494 | 71c79813 | Luiz Capitulino | "same filename as the backing file");
|
4495 | 792da93a | Jes Sorensen | goto out;
|
4496 | 792da93a | Jes Sorensen | } |
4497 | 792da93a | Jes Sorensen | } |
4498 | 792da93a | Jes Sorensen | |
4499 | f88e1a42 | Jes Sorensen | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
4500 | f88e1a42 | Jes Sorensen | if (backing_fmt && backing_fmt->value.s) {
|
4501 | 96df67d1 | Stefan Hajnoczi | backing_drv = bdrv_find_format(backing_fmt->value.s); |
4502 | 96df67d1 | Stefan Hajnoczi | if (!backing_drv) {
|
4503 | 71c79813 | Luiz Capitulino | error_setg(errp, "Unknown backing file format '%s'",
|
4504 | 71c79813 | Luiz Capitulino | backing_fmt->value.s); |
4505 | f88e1a42 | Jes Sorensen | goto out;
|
4506 | f88e1a42 | Jes Sorensen | } |
4507 | f88e1a42 | Jes Sorensen | } |
4508 | f88e1a42 | Jes Sorensen | |
4509 | f88e1a42 | Jes Sorensen | // The size for the image must always be specified, with one exception:
|
4510 | f88e1a42 | Jes Sorensen | // If we are using a backing file, we can obtain the size from there
|
4511 | d220894e | Kevin Wolf | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
4512 | d220894e | Kevin Wolf | if (size && size->value.n == -1) { |
4513 | f88e1a42 | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
4514 | f88e1a42 | Jes Sorensen | uint64_t size; |
4515 | f88e1a42 | Jes Sorensen | char buf[32]; |
4516 | 63090dac | Paolo Bonzini | int back_flags;
|
4517 | 63090dac | Paolo Bonzini | |
4518 | 63090dac | Paolo Bonzini | /* backing files always opened read-only */
|
4519 | 63090dac | Paolo Bonzini | back_flags = |
4520 | 63090dac | Paolo Bonzini | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
4521 | f88e1a42 | Jes Sorensen | |
4522 | f88e1a42 | Jes Sorensen | bs = bdrv_new("");
|
4523 | f88e1a42 | Jes Sorensen | |
4524 | de9c0cec | Kevin Wolf | ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags,
|
4525 | de9c0cec | Kevin Wolf | backing_drv); |
4526 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
4527 | 71c79813 | Luiz Capitulino | error_setg_errno(errp, -ret, "Could not open '%s'",
|
4528 | 71c79813 | Luiz Capitulino | backing_file->value.s); |
4529 | f88e1a42 | Jes Sorensen | goto out;
|
4530 | f88e1a42 | Jes Sorensen | } |
4531 | f88e1a42 | Jes Sorensen | bdrv_get_geometry(bs, &size); |
4532 | f88e1a42 | Jes Sorensen | size *= 512;
|
4533 | f88e1a42 | Jes Sorensen | |
4534 | f88e1a42 | Jes Sorensen | snprintf(buf, sizeof(buf), "%" PRId64, size); |
4535 | f88e1a42 | Jes Sorensen | set_option_parameter(param, BLOCK_OPT_SIZE, buf); |
4536 | f88e1a42 | Jes Sorensen | } else {
|
4537 | 71c79813 | Luiz Capitulino | error_setg(errp, "Image creation needs a size parameter");
|
4538 | f88e1a42 | Jes Sorensen | goto out;
|
4539 | f88e1a42 | Jes Sorensen | } |
4540 | f88e1a42 | Jes Sorensen | } |
4541 | f88e1a42 | Jes Sorensen | |
4542 | f382d43a | Miroslav Rezanina | if (!quiet) {
|
4543 | f382d43a | Miroslav Rezanina | printf("Formatting '%s', fmt=%s ", filename, fmt);
|
4544 | f382d43a | Miroslav Rezanina | print_option_parameters(param); |
4545 | f382d43a | Miroslav Rezanina | puts("");
|
4546 | f382d43a | Miroslav Rezanina | } |
4547 | f88e1a42 | Jes Sorensen | ret = bdrv_create(drv, filename, param); |
4548 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
4549 | f88e1a42 | Jes Sorensen | if (ret == -ENOTSUP) {
|
4550 | 71c79813 | Luiz Capitulino | error_setg(errp,"Formatting or formatting option not supported for "
|
4551 | 71c79813 | Luiz Capitulino | "file format '%s'", fmt);
|
4552 | f88e1a42 | Jes Sorensen | } else if (ret == -EFBIG) { |
4553 | f3f4d2c0 | Kevin Wolf | const char *cluster_size_hint = ""; |
4554 | f3f4d2c0 | Kevin Wolf | if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
|
4555 | f3f4d2c0 | Kevin Wolf | cluster_size_hint = " (try using a larger cluster size)";
|
4556 | f3f4d2c0 | Kevin Wolf | } |
4557 | f3f4d2c0 | Kevin Wolf | error_setg(errp, "The image size is too large for file format '%s'%s",
|
4558 | f3f4d2c0 | Kevin Wolf | fmt, cluster_size_hint); |
4559 | f88e1a42 | Jes Sorensen | } else {
|
4560 | 71c79813 | Luiz Capitulino | error_setg(errp, "%s: error while creating %s: %s", filename, fmt,
|
4561 | 71c79813 | Luiz Capitulino | strerror(-ret)); |
4562 | f88e1a42 | Jes Sorensen | } |
4563 | f88e1a42 | Jes Sorensen | } |
4564 | f88e1a42 | Jes Sorensen | |
4565 | f88e1a42 | Jes Sorensen | out:
|
4566 | f88e1a42 | Jes Sorensen | free_option_parameters(create_options); |
4567 | f88e1a42 | Jes Sorensen | free_option_parameters(param); |
4568 | f88e1a42 | Jes Sorensen | |
4569 | f88e1a42 | Jes Sorensen | if (bs) {
|
4570 | f88e1a42 | Jes Sorensen | bdrv_delete(bs); |
4571 | f88e1a42 | Jes Sorensen | } |
4572 | f88e1a42 | Jes Sorensen | } |
4573 | 85d126f3 | Stefan Hajnoczi | |
4574 | 85d126f3 | Stefan Hajnoczi | AioContext *bdrv_get_aio_context(BlockDriverState *bs) |
4575 | 85d126f3 | Stefan Hajnoczi | { |
4576 | 85d126f3 | Stefan Hajnoczi | /* Currently BlockDriverState always uses the main loop AioContext */
|
4577 | 85d126f3 | Stefan Hajnoczi | return qemu_get_aio_context();
|
4578 | 85d126f3 | Stefan Hajnoczi | } |
4579 | d616b224 | Stefan Hajnoczi | |
4580 | d616b224 | Stefan Hajnoczi | void bdrv_add_before_write_notifier(BlockDriverState *bs,
|
4581 | d616b224 | Stefan Hajnoczi | NotifierWithReturn *notifier) |
4582 | d616b224 | Stefan Hajnoczi | { |
4583 | d616b224 | Stefan Hajnoczi | notifier_with_return_list_add(&bs->before_write_notifiers, notifier); |
4584 | d616b224 | Stefan Hajnoczi | } |