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