root / block.c @ 07f07615
History | View | Annotate | Download (89.3 kB)
1 | fc01f7e7 | bellard | /*
|
---|---|---|---|
2 | fc01f7e7 | bellard | * QEMU System Emulator block driver
|
3 | 5fafdf24 | ths | *
|
4 | fc01f7e7 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | fc01f7e7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | fc01f7e7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | fc01f7e7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | fc01f7e7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | fc01f7e7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | fc01f7e7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | fc01f7e7 | bellard | *
|
13 | fc01f7e7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | fc01f7e7 | bellard | * all copies or substantial portions of the Software.
|
15 | fc01f7e7 | bellard | *
|
16 | fc01f7e7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | fc01f7e7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | fc01f7e7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | fc01f7e7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | fc01f7e7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | fc01f7e7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | fc01f7e7 | bellard | * THE SOFTWARE.
|
23 | fc01f7e7 | bellard | */
|
24 | 3990d09a | blueswir1 | #include "config-host.h" |
25 | faf07963 | pbrook | #include "qemu-common.h" |
26 | 6d519a5f | Stefan Hajnoczi | #include "trace.h" |
27 | 376253ec | aliguori | #include "monitor.h" |
28 | ea2384d3 | bellard | #include "block_int.h" |
29 | 5efa9d5a | Anthony Liguori | #include "module.h" |
30 | d15e5465 | Luiz Capitulino | #include "qemu-objects.h" |
31 | 68485420 | Kevin Wolf | #include "qemu-coroutine.h" |
32 | fc01f7e7 | bellard | |
33 | 71e72a19 | Juan Quintela | #ifdef CONFIG_BSD
|
34 | 7674e7bf | bellard | #include <sys/types.h> |
35 | 7674e7bf | bellard | #include <sys/stat.h> |
36 | 7674e7bf | bellard | #include <sys/ioctl.h> |
37 | 72cf2d4f | Blue Swirl | #include <sys/queue.h> |
38 | c5e97233 | blueswir1 | #ifndef __DragonFly__
|
39 | 7674e7bf | bellard | #include <sys/disk.h> |
40 | 7674e7bf | bellard | #endif
|
41 | c5e97233 | blueswir1 | #endif
|
42 | 7674e7bf | bellard | |
43 | 49dc768d | aliguori | #ifdef _WIN32
|
44 | 49dc768d | aliguori | #include <windows.h> |
45 | 49dc768d | aliguori | #endif
|
46 | 49dc768d | aliguori | |
47 | 1c9805a3 | Stefan Hajnoczi | #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ |
48 | 1c9805a3 | Stefan Hajnoczi | |
49 | 7d4b4ba5 | Markus Armbruster | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); |
50 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
51 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
52 | c87c0672 | aliguori | BlockDriverCompletionFunc *cb, void *opaque);
|
53 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
54 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
55 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque);
|
56 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
57 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
58 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov); |
59 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
60 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
61 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov); |
62 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
63 | c5fbe571 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
|
64 | 1c9805a3 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
65 | 1c9805a3 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
|
66 | b2a61371 | Stefan Hajnoczi | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
|
67 | b2a61371 | Stefan Hajnoczi | int64_t sector_num, |
68 | b2a61371 | Stefan Hajnoczi | QEMUIOVector *qiov, |
69 | b2a61371 | Stefan Hajnoczi | int nb_sectors,
|
70 | b2a61371 | Stefan Hajnoczi | BlockDriverCompletionFunc *cb, |
71 | b2a61371 | Stefan Hajnoczi | void *opaque,
|
72 | 8c5873d6 | Stefan Hajnoczi | bool is_write);
|
73 | b2a61371 | Stefan Hajnoczi | static void coroutine_fn bdrv_co_do_rw(void *opaque); |
74 | ec530c81 | bellard | |
75 | 1b7bdbc1 | Stefan Hajnoczi | static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
|
76 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_HEAD_INITIALIZER(bdrv_states); |
77 | 7ee930d0 | blueswir1 | |
78 | 8a22f02a | Stefan Hajnoczi | static QLIST_HEAD(, BlockDriver) bdrv_drivers =
|
79 | 8a22f02a | Stefan Hajnoczi | QLIST_HEAD_INITIALIZER(bdrv_drivers); |
80 | ea2384d3 | bellard | |
81 | f9092b10 | Markus Armbruster | /* The device to use for VM snapshots */
|
82 | f9092b10 | Markus Armbruster | static BlockDriverState *bs_snapshots;
|
83 | f9092b10 | Markus Armbruster | |
84 | eb852011 | Markus Armbruster | /* If non-zero, use only whitelisted block drivers */
|
85 | eb852011 | Markus Armbruster | static int use_bdrv_whitelist; |
86 | eb852011 | Markus Armbruster | |
87 | 9e0b22f4 | Stefan Hajnoczi | #ifdef _WIN32
|
88 | 9e0b22f4 | Stefan Hajnoczi | static int is_windows_drive_prefix(const char *filename) |
89 | 9e0b22f4 | Stefan Hajnoczi | { |
90 | 9e0b22f4 | Stefan Hajnoczi | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
91 | 9e0b22f4 | Stefan Hajnoczi | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
92 | 9e0b22f4 | Stefan Hajnoczi | filename[1] == ':'); |
93 | 9e0b22f4 | Stefan Hajnoczi | } |
94 | 9e0b22f4 | Stefan Hajnoczi | |
95 | 9e0b22f4 | Stefan Hajnoczi | int is_windows_drive(const char *filename) |
96 | 9e0b22f4 | Stefan Hajnoczi | { |
97 | 9e0b22f4 | Stefan Hajnoczi | if (is_windows_drive_prefix(filename) &&
|
98 | 9e0b22f4 | Stefan Hajnoczi | filename[2] == '\0') |
99 | 9e0b22f4 | Stefan Hajnoczi | return 1; |
100 | 9e0b22f4 | Stefan Hajnoczi | if (strstart(filename, "\\\\.\\", NULL) || |
101 | 9e0b22f4 | Stefan Hajnoczi | strstart(filename, "//./", NULL)) |
102 | 9e0b22f4 | Stefan Hajnoczi | return 1; |
103 | 9e0b22f4 | Stefan Hajnoczi | return 0; |
104 | 9e0b22f4 | Stefan Hajnoczi | } |
105 | 9e0b22f4 | Stefan Hajnoczi | #endif
|
106 | 9e0b22f4 | Stefan Hajnoczi | |
107 | 9e0b22f4 | Stefan Hajnoczi | /* check if the path starts with "<protocol>:" */
|
108 | 9e0b22f4 | Stefan Hajnoczi | static int path_has_protocol(const char *path) |
109 | 9e0b22f4 | Stefan Hajnoczi | { |
110 | 9e0b22f4 | Stefan Hajnoczi | #ifdef _WIN32
|
111 | 9e0b22f4 | Stefan Hajnoczi | if (is_windows_drive(path) ||
|
112 | 9e0b22f4 | Stefan Hajnoczi | is_windows_drive_prefix(path)) { |
113 | 9e0b22f4 | Stefan Hajnoczi | return 0; |
114 | 9e0b22f4 | Stefan Hajnoczi | } |
115 | 9e0b22f4 | Stefan Hajnoczi | #endif
|
116 | 9e0b22f4 | Stefan Hajnoczi | |
117 | 9e0b22f4 | Stefan Hajnoczi | return strchr(path, ':') != NULL; |
118 | 9e0b22f4 | Stefan Hajnoczi | } |
119 | 9e0b22f4 | Stefan Hajnoczi | |
120 | 83f64091 | bellard | int path_is_absolute(const char *path) |
121 | 3b0d4f61 | bellard | { |
122 | 83f64091 | bellard | const char *p; |
123 | 21664424 | bellard | #ifdef _WIN32
|
124 | 21664424 | bellard | /* specific case for names like: "\\.\d:" */
|
125 | 21664424 | bellard | if (*path == '/' || *path == '\\') |
126 | 21664424 | bellard | return 1; |
127 | 21664424 | bellard | #endif
|
128 | 83f64091 | bellard | p = strchr(path, ':');
|
129 | 83f64091 | bellard | if (p)
|
130 | 83f64091 | bellard | p++; |
131 | 83f64091 | bellard | else
|
132 | 83f64091 | bellard | p = path; |
133 | 3b9f94e1 | bellard | #ifdef _WIN32
|
134 | 3b9f94e1 | bellard | return (*p == '/' || *p == '\\'); |
135 | 3b9f94e1 | bellard | #else
|
136 | 3b9f94e1 | bellard | return (*p == '/'); |
137 | 3b9f94e1 | bellard | #endif
|
138 | 3b0d4f61 | bellard | } |
139 | 3b0d4f61 | bellard | |
140 | 83f64091 | bellard | /* if filename is absolute, just copy it to dest. Otherwise, build a
|
141 | 83f64091 | bellard | path to it by considering it is relative to base_path. URL are
|
142 | 83f64091 | bellard | supported. */
|
143 | 83f64091 | bellard | void path_combine(char *dest, int dest_size, |
144 | 83f64091 | bellard | const char *base_path, |
145 | 83f64091 | bellard | const char *filename) |
146 | 3b0d4f61 | bellard | { |
147 | 83f64091 | bellard | const char *p, *p1; |
148 | 83f64091 | bellard | int len;
|
149 | 83f64091 | bellard | |
150 | 83f64091 | bellard | if (dest_size <= 0) |
151 | 83f64091 | bellard | return;
|
152 | 83f64091 | bellard | if (path_is_absolute(filename)) {
|
153 | 83f64091 | bellard | pstrcpy(dest, dest_size, filename); |
154 | 83f64091 | bellard | } else {
|
155 | 83f64091 | bellard | p = strchr(base_path, ':');
|
156 | 83f64091 | bellard | if (p)
|
157 | 83f64091 | bellard | p++; |
158 | 83f64091 | bellard | else
|
159 | 83f64091 | bellard | p = base_path; |
160 | 3b9f94e1 | bellard | p1 = strrchr(base_path, '/');
|
161 | 3b9f94e1 | bellard | #ifdef _WIN32
|
162 | 3b9f94e1 | bellard | { |
163 | 3b9f94e1 | bellard | const char *p2; |
164 | 3b9f94e1 | bellard | p2 = strrchr(base_path, '\\');
|
165 | 3b9f94e1 | bellard | if (!p1 || p2 > p1)
|
166 | 3b9f94e1 | bellard | p1 = p2; |
167 | 3b9f94e1 | bellard | } |
168 | 3b9f94e1 | bellard | #endif
|
169 | 83f64091 | bellard | if (p1)
|
170 | 83f64091 | bellard | p1++; |
171 | 83f64091 | bellard | else
|
172 | 83f64091 | bellard | p1 = base_path; |
173 | 83f64091 | bellard | if (p1 > p)
|
174 | 83f64091 | bellard | p = p1; |
175 | 83f64091 | bellard | len = p - base_path; |
176 | 83f64091 | bellard | if (len > dest_size - 1) |
177 | 83f64091 | bellard | len = dest_size - 1;
|
178 | 83f64091 | bellard | memcpy(dest, base_path, len); |
179 | 83f64091 | bellard | dest[len] = '\0';
|
180 | 83f64091 | bellard | pstrcat(dest, dest_size, filename); |
181 | 3b0d4f61 | bellard | } |
182 | 3b0d4f61 | bellard | } |
183 | 3b0d4f61 | bellard | |
184 | 5efa9d5a | Anthony Liguori | void bdrv_register(BlockDriver *bdrv)
|
185 | ea2384d3 | bellard | { |
186 | 8c5873d6 | Stefan Hajnoczi | /* Block drivers without coroutine functions need emulation */
|
187 | 8c5873d6 | Stefan Hajnoczi | if (!bdrv->bdrv_co_readv) {
|
188 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_co_readv = bdrv_co_readv_em; |
189 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_co_writev = bdrv_co_writev_em; |
190 | f9f05dc5 | Kevin Wolf | |
191 | f8c35c1d | Stefan Hajnoczi | /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
|
192 | f8c35c1d | Stefan Hajnoczi | * the block driver lacks aio we need to emulate that too.
|
193 | f8c35c1d | Stefan Hajnoczi | */
|
194 | f9f05dc5 | Kevin Wolf | if (!bdrv->bdrv_aio_readv) {
|
195 | f9f05dc5 | Kevin Wolf | /* add AIO emulation layer */
|
196 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
197 | f9f05dc5 | Kevin Wolf | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
198 | f9f05dc5 | Kevin Wolf | } |
199 | 83f64091 | bellard | } |
200 | b2e12bc6 | Christoph Hellwig | |
201 | 8a22f02a | Stefan Hajnoczi | QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); |
202 | ea2384d3 | bellard | } |
203 | b338082b | bellard | |
204 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
205 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
206 | b338082b | bellard | { |
207 | 1b7bdbc1 | Stefan Hajnoczi | BlockDriverState *bs; |
208 | b338082b | bellard | |
209 | 7267c094 | Anthony Liguori | bs = g_malloc0(sizeof(BlockDriverState));
|
210 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
211 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
212 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); |
213 | ea2384d3 | bellard | } |
214 | 28a7282a | Luiz Capitulino | bdrv_iostatus_disable(bs); |
215 | b338082b | bellard | return bs;
|
216 | b338082b | bellard | } |
217 | b338082b | bellard | |
218 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
219 | ea2384d3 | bellard | { |
220 | ea2384d3 | bellard | BlockDriver *drv1; |
221 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
222 | 8a22f02a | Stefan Hajnoczi | if (!strcmp(drv1->format_name, format_name)) {
|
223 | ea2384d3 | bellard | return drv1;
|
224 | 8a22f02a | Stefan Hajnoczi | } |
225 | ea2384d3 | bellard | } |
226 | ea2384d3 | bellard | return NULL; |
227 | ea2384d3 | bellard | } |
228 | ea2384d3 | bellard | |
229 | eb852011 | Markus Armbruster | static int bdrv_is_whitelisted(BlockDriver *drv) |
230 | eb852011 | Markus Armbruster | { |
231 | eb852011 | Markus Armbruster | static const char *whitelist[] = { |
232 | eb852011 | Markus Armbruster | CONFIG_BDRV_WHITELIST |
233 | eb852011 | Markus Armbruster | }; |
234 | eb852011 | Markus Armbruster | const char **p; |
235 | eb852011 | Markus Armbruster | |
236 | eb852011 | Markus Armbruster | if (!whitelist[0]) |
237 | eb852011 | Markus Armbruster | return 1; /* no whitelist, anything goes */ |
238 | eb852011 | Markus Armbruster | |
239 | eb852011 | Markus Armbruster | for (p = whitelist; *p; p++) {
|
240 | eb852011 | Markus Armbruster | if (!strcmp(drv->format_name, *p)) {
|
241 | eb852011 | Markus Armbruster | return 1; |
242 | eb852011 | Markus Armbruster | } |
243 | eb852011 | Markus Armbruster | } |
244 | eb852011 | Markus Armbruster | return 0; |
245 | eb852011 | Markus Armbruster | } |
246 | eb852011 | Markus Armbruster | |
247 | eb852011 | Markus Armbruster | BlockDriver *bdrv_find_whitelisted_format(const char *format_name) |
248 | eb852011 | Markus Armbruster | { |
249 | eb852011 | Markus Armbruster | BlockDriver *drv = bdrv_find_format(format_name); |
250 | eb852011 | Markus Armbruster | return drv && bdrv_is_whitelisted(drv) ? drv : NULL; |
251 | eb852011 | Markus Armbruster | } |
252 | eb852011 | Markus Armbruster | |
253 | 0e7e1989 | Kevin Wolf | int bdrv_create(BlockDriver *drv, const char* filename, |
254 | 0e7e1989 | Kevin Wolf | QEMUOptionParameter *options) |
255 | ea2384d3 | bellard | { |
256 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
257 | ea2384d3 | bellard | return -ENOTSUP;
|
258 | 0e7e1989 | Kevin Wolf | |
259 | 0e7e1989 | Kevin Wolf | return drv->bdrv_create(filename, options);
|
260 | ea2384d3 | bellard | } |
261 | ea2384d3 | bellard | |
262 | 84a12e66 | Christoph Hellwig | int bdrv_create_file(const char* filename, QEMUOptionParameter *options) |
263 | 84a12e66 | Christoph Hellwig | { |
264 | 84a12e66 | Christoph Hellwig | BlockDriver *drv; |
265 | 84a12e66 | Christoph Hellwig | |
266 | b50cbabc | MORITA Kazutaka | drv = bdrv_find_protocol(filename); |
267 | 84a12e66 | Christoph Hellwig | if (drv == NULL) { |
268 | 16905d71 | Stefan Hajnoczi | return -ENOENT;
|
269 | 84a12e66 | Christoph Hellwig | } |
270 | 84a12e66 | Christoph Hellwig | |
271 | 84a12e66 | Christoph Hellwig | return bdrv_create(drv, filename, options);
|
272 | 84a12e66 | Christoph Hellwig | } |
273 | 84a12e66 | Christoph Hellwig | |
274 | d5249393 | bellard | #ifdef _WIN32
|
275 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
276 | d5249393 | bellard | { |
277 | 3b9f94e1 | bellard | char temp_dir[MAX_PATH];
|
278 | 3b46e624 | ths | |
279 | 3b9f94e1 | bellard | GetTempPath(MAX_PATH, temp_dir); |
280 | 3b9f94e1 | bellard | GetTempFileName(temp_dir, "qem", 0, filename); |
281 | d5249393 | bellard | } |
282 | d5249393 | bellard | #else
|
283 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
284 | fc01f7e7 | bellard | { |
285 | 67b915a5 | bellard | int fd;
|
286 | 7ccfb2eb | blueswir1 | const char *tmpdir; |
287 | d5249393 | bellard | /* XXX: race condition possible */
|
288 | 0badc1ee | aurel32 | tmpdir = getenv("TMPDIR");
|
289 | 0badc1ee | aurel32 | if (!tmpdir)
|
290 | 0badc1ee | aurel32 | tmpdir = "/tmp";
|
291 | 0badc1ee | aurel32 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
|
292 | ea2384d3 | bellard | fd = mkstemp(filename); |
293 | ea2384d3 | bellard | close(fd); |
294 | ea2384d3 | bellard | } |
295 | d5249393 | bellard | #endif
|
296 | fc01f7e7 | bellard | |
297 | 84a12e66 | Christoph Hellwig | /*
|
298 | 84a12e66 | Christoph Hellwig | * Detect host devices. By convention, /dev/cdrom[N] is always
|
299 | 84a12e66 | Christoph Hellwig | * recognized as a host CDROM.
|
300 | 84a12e66 | Christoph Hellwig | */
|
301 | 84a12e66 | Christoph Hellwig | static BlockDriver *find_hdev_driver(const char *filename) |
302 | 84a12e66 | Christoph Hellwig | { |
303 | 84a12e66 | Christoph Hellwig | int score_max = 0, score; |
304 | 84a12e66 | Christoph Hellwig | BlockDriver *drv = NULL, *d;
|
305 | 84a12e66 | Christoph Hellwig | |
306 | 84a12e66 | Christoph Hellwig | QLIST_FOREACH(d, &bdrv_drivers, list) { |
307 | 84a12e66 | Christoph Hellwig | if (d->bdrv_probe_device) {
|
308 | 84a12e66 | Christoph Hellwig | score = d->bdrv_probe_device(filename); |
309 | 84a12e66 | Christoph Hellwig | if (score > score_max) {
|
310 | 84a12e66 | Christoph Hellwig | score_max = score; |
311 | 84a12e66 | Christoph Hellwig | drv = d; |
312 | 84a12e66 | Christoph Hellwig | } |
313 | 84a12e66 | Christoph Hellwig | } |
314 | 84a12e66 | Christoph Hellwig | } |
315 | 84a12e66 | Christoph Hellwig | |
316 | 84a12e66 | Christoph Hellwig | return drv;
|
317 | 84a12e66 | Christoph Hellwig | } |
318 | 84a12e66 | Christoph Hellwig | |
319 | b50cbabc | MORITA Kazutaka | BlockDriver *bdrv_find_protocol(const char *filename) |
320 | 83f64091 | bellard | { |
321 | 83f64091 | bellard | BlockDriver *drv1; |
322 | 83f64091 | bellard | char protocol[128]; |
323 | 1cec71e3 | Anthony Liguori | int len;
|
324 | 83f64091 | bellard | const char *p; |
325 | 19cb3738 | bellard | |
326 | 66f82cee | Kevin Wolf | /* TODO Drivers without bdrv_file_open must be specified explicitly */
|
327 | 66f82cee | Kevin Wolf | |
328 | 39508e7a | Christoph Hellwig | /*
|
329 | 39508e7a | Christoph Hellwig | * XXX(hch): we really should not let host device detection
|
330 | 39508e7a | Christoph Hellwig | * override an explicit protocol specification, but moving this
|
331 | 39508e7a | Christoph Hellwig | * later breaks access to device names with colons in them.
|
332 | 39508e7a | Christoph Hellwig | * Thanks to the brain-dead persistent naming schemes on udev-
|
333 | 39508e7a | Christoph Hellwig | * based Linux systems those actually are quite common.
|
334 | 39508e7a | Christoph Hellwig | */
|
335 | 39508e7a | Christoph Hellwig | drv1 = find_hdev_driver(filename); |
336 | 39508e7a | Christoph Hellwig | if (drv1) {
|
337 | 39508e7a | Christoph Hellwig | return drv1;
|
338 | 39508e7a | Christoph Hellwig | } |
339 | 39508e7a | Christoph Hellwig | |
340 | 9e0b22f4 | Stefan Hajnoczi | if (!path_has_protocol(filename)) {
|
341 | 39508e7a | Christoph Hellwig | return bdrv_find_format("file"); |
342 | 84a12e66 | Christoph Hellwig | } |
343 | 9e0b22f4 | Stefan Hajnoczi | p = strchr(filename, ':');
|
344 | 9e0b22f4 | Stefan Hajnoczi | assert(p != NULL);
|
345 | 1cec71e3 | Anthony Liguori | len = p - filename; |
346 | 1cec71e3 | Anthony Liguori | if (len > sizeof(protocol) - 1) |
347 | 1cec71e3 | Anthony Liguori | len = sizeof(protocol) - 1; |
348 | 1cec71e3 | Anthony Liguori | memcpy(protocol, filename, len); |
349 | 1cec71e3 | Anthony Liguori | protocol[len] = '\0';
|
350 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
351 | 5fafdf24 | ths | if (drv1->protocol_name &&
|
352 | 8a22f02a | Stefan Hajnoczi | !strcmp(drv1->protocol_name, protocol)) { |
353 | 83f64091 | bellard | return drv1;
|
354 | 8a22f02a | Stefan Hajnoczi | } |
355 | 83f64091 | bellard | } |
356 | 83f64091 | bellard | return NULL; |
357 | 83f64091 | bellard | } |
358 | 83f64091 | bellard | |
359 | c98ac35d | Stefan Weil | static int find_image_format(const char *filename, BlockDriver **pdrv) |
360 | f3a5d3f8 | Christoph Hellwig | { |
361 | f3a5d3f8 | Christoph Hellwig | int ret, score, score_max;
|
362 | f3a5d3f8 | Christoph Hellwig | BlockDriver *drv1, *drv; |
363 | f3a5d3f8 | Christoph Hellwig | uint8_t buf[2048];
|
364 | f3a5d3f8 | Christoph Hellwig | BlockDriverState *bs; |
365 | f3a5d3f8 | Christoph Hellwig | |
366 | f5edb014 | Naphtali Sprei | ret = bdrv_file_open(&bs, filename, 0);
|
367 | c98ac35d | Stefan Weil | if (ret < 0) { |
368 | c98ac35d | Stefan Weil | *pdrv = NULL;
|
369 | c98ac35d | Stefan Weil | return ret;
|
370 | c98ac35d | Stefan Weil | } |
371 | f8ea0b00 | Nicholas Bellinger | |
372 | 08a00559 | Kevin Wolf | /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
|
373 | 08a00559 | Kevin Wolf | if (bs->sg || !bdrv_is_inserted(bs)) {
|
374 | 1a396859 | Nicholas A. Bellinger | bdrv_delete(bs); |
375 | c98ac35d | Stefan Weil | drv = bdrv_find_format("raw");
|
376 | c98ac35d | Stefan Weil | if (!drv) {
|
377 | c98ac35d | Stefan Weil | ret = -ENOENT; |
378 | c98ac35d | Stefan Weil | } |
379 | c98ac35d | Stefan Weil | *pdrv = drv; |
380 | c98ac35d | Stefan Weil | return ret;
|
381 | 1a396859 | Nicholas A. Bellinger | } |
382 | f8ea0b00 | Nicholas Bellinger | |
383 | 83f64091 | bellard | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
384 | 83f64091 | bellard | bdrv_delete(bs); |
385 | 83f64091 | bellard | if (ret < 0) { |
386 | c98ac35d | Stefan Weil | *pdrv = NULL;
|
387 | c98ac35d | Stefan Weil | return ret;
|
388 | 83f64091 | bellard | } |
389 | 83f64091 | bellard | |
390 | ea2384d3 | bellard | score_max = 0;
|
391 | 84a12e66 | Christoph Hellwig | drv = NULL;
|
392 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv1, &bdrv_drivers, list) { |
393 | 83f64091 | bellard | if (drv1->bdrv_probe) {
|
394 | 83f64091 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
395 | 83f64091 | bellard | if (score > score_max) {
|
396 | 83f64091 | bellard | score_max = score; |
397 | 83f64091 | bellard | drv = drv1; |
398 | 83f64091 | bellard | } |
399 | 0849bf08 | bellard | } |
400 | fc01f7e7 | bellard | } |
401 | c98ac35d | Stefan Weil | if (!drv) {
|
402 | c98ac35d | Stefan Weil | ret = -ENOENT; |
403 | c98ac35d | Stefan Weil | } |
404 | c98ac35d | Stefan Weil | *pdrv = drv; |
405 | c98ac35d | Stefan Weil | return ret;
|
406 | ea2384d3 | bellard | } |
407 | ea2384d3 | bellard | |
408 | 51762288 | Stefan Hajnoczi | /**
|
409 | 51762288 | Stefan Hajnoczi | * Set the current 'total_sectors' value
|
410 | 51762288 | Stefan Hajnoczi | */
|
411 | 51762288 | Stefan Hajnoczi | static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) |
412 | 51762288 | Stefan Hajnoczi | { |
413 | 51762288 | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
414 | 51762288 | Stefan Hajnoczi | |
415 | 396759ad | Nicholas Bellinger | /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
|
416 | 396759ad | Nicholas Bellinger | if (bs->sg)
|
417 | 396759ad | Nicholas Bellinger | return 0; |
418 | 396759ad | Nicholas Bellinger | |
419 | 51762288 | Stefan Hajnoczi | /* query actual device if possible, otherwise just trust the hint */
|
420 | 51762288 | Stefan Hajnoczi | if (drv->bdrv_getlength) {
|
421 | 51762288 | Stefan Hajnoczi | int64_t length = drv->bdrv_getlength(bs); |
422 | 51762288 | Stefan Hajnoczi | if (length < 0) { |
423 | 51762288 | Stefan Hajnoczi | return length;
|
424 | 51762288 | Stefan Hajnoczi | } |
425 | 51762288 | Stefan Hajnoczi | hint = length >> BDRV_SECTOR_BITS; |
426 | 51762288 | Stefan Hajnoczi | } |
427 | 51762288 | Stefan Hajnoczi | |
428 | 51762288 | Stefan Hajnoczi | bs->total_sectors = hint; |
429 | 51762288 | Stefan Hajnoczi | return 0; |
430 | 51762288 | Stefan Hajnoczi | } |
431 | 51762288 | Stefan Hajnoczi | |
432 | c3993cdc | Stefan Hajnoczi | /**
|
433 | c3993cdc | Stefan Hajnoczi | * Set open flags for a given cache mode
|
434 | c3993cdc | Stefan Hajnoczi | *
|
435 | c3993cdc | Stefan Hajnoczi | * Return 0 on success, -1 if the cache mode was invalid.
|
436 | c3993cdc | Stefan Hajnoczi | */
|
437 | c3993cdc | Stefan Hajnoczi | int bdrv_parse_cache_flags(const char *mode, int *flags) |
438 | c3993cdc | Stefan Hajnoczi | { |
439 | c3993cdc | Stefan Hajnoczi | *flags &= ~BDRV_O_CACHE_MASK; |
440 | c3993cdc | Stefan Hajnoczi | |
441 | c3993cdc | Stefan Hajnoczi | if (!strcmp(mode, "off") || !strcmp(mode, "none")) { |
442 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
443 | 92196b2f | Stefan Hajnoczi | } else if (!strcmp(mode, "directsync")) { |
444 | 92196b2f | Stefan Hajnoczi | *flags |= BDRV_O_NOCACHE; |
445 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "writeback")) { |
446 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_CACHE_WB; |
447 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "unsafe")) { |
448 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_CACHE_WB; |
449 | c3993cdc | Stefan Hajnoczi | *flags |= BDRV_O_NO_FLUSH; |
450 | c3993cdc | Stefan Hajnoczi | } else if (!strcmp(mode, "writethrough")) { |
451 | c3993cdc | Stefan Hajnoczi | /* this is the default */
|
452 | c3993cdc | Stefan Hajnoczi | } else {
|
453 | c3993cdc | Stefan Hajnoczi | return -1; |
454 | c3993cdc | Stefan Hajnoczi | } |
455 | c3993cdc | Stefan Hajnoczi | |
456 | c3993cdc | Stefan Hajnoczi | return 0; |
457 | c3993cdc | Stefan Hajnoczi | } |
458 | c3993cdc | Stefan Hajnoczi | |
459 | b6ce07aa | Kevin Wolf | /*
|
460 | 57915332 | Kevin Wolf | * Common part for opening disk images and files
|
461 | 57915332 | Kevin Wolf | */
|
462 | 57915332 | Kevin Wolf | static int bdrv_open_common(BlockDriverState *bs, const char *filename, |
463 | 57915332 | Kevin Wolf | int flags, BlockDriver *drv)
|
464 | 57915332 | Kevin Wolf | { |
465 | 57915332 | Kevin Wolf | int ret, open_flags;
|
466 | 57915332 | Kevin Wolf | |
467 | 57915332 | Kevin Wolf | assert(drv != NULL);
|
468 | 57915332 | Kevin Wolf | |
469 | 28dcee10 | Stefan Hajnoczi | trace_bdrv_open_common(bs, filename, flags, drv->format_name); |
470 | 28dcee10 | Stefan Hajnoczi | |
471 | 66f82cee | Kevin Wolf | bs->file = NULL;
|
472 | 51762288 | Stefan Hajnoczi | bs->total_sectors = 0;
|
473 | 57915332 | Kevin Wolf | bs->encrypted = 0;
|
474 | 57915332 | Kevin Wolf | bs->valid_key = 0;
|
475 | 57915332 | Kevin Wolf | bs->open_flags = flags; |
476 | 57915332 | Kevin Wolf | bs->buffer_alignment = 512;
|
477 | 57915332 | Kevin Wolf | |
478 | 57915332 | Kevin Wolf | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
479 | 57915332 | Kevin Wolf | |
480 | 57915332 | Kevin Wolf | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
|
481 | 57915332 | Kevin Wolf | return -ENOTSUP;
|
482 | 57915332 | Kevin Wolf | } |
483 | 57915332 | Kevin Wolf | |
484 | 57915332 | Kevin Wolf | bs->drv = drv; |
485 | 7267c094 | Anthony Liguori | bs->opaque = g_malloc0(drv->instance_size); |
486 | 57915332 | Kevin Wolf | |
487 | a6599793 | Christoph Hellwig | if (flags & BDRV_O_CACHE_WB)
|
488 | 57915332 | Kevin Wolf | bs->enable_write_cache = 1;
|
489 | 57915332 | Kevin Wolf | |
490 | 57915332 | Kevin Wolf | /*
|
491 | 57915332 | Kevin Wolf | * Clear flags that are internal to the block layer before opening the
|
492 | 57915332 | Kevin Wolf | * image.
|
493 | 57915332 | Kevin Wolf | */
|
494 | 57915332 | Kevin Wolf | open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
495 | 57915332 | Kevin Wolf | |
496 | 57915332 | Kevin Wolf | /*
|
497 | ebabb67a | Stefan Weil | * Snapshots should be writable.
|
498 | 57915332 | Kevin Wolf | */
|
499 | 57915332 | Kevin Wolf | if (bs->is_temporary) {
|
500 | 57915332 | Kevin Wolf | open_flags |= BDRV_O_RDWR; |
501 | 57915332 | Kevin Wolf | } |
502 | 57915332 | Kevin Wolf | |
503 | 66f82cee | Kevin Wolf | /* Open the image, either directly or using a protocol */
|
504 | 66f82cee | Kevin Wolf | if (drv->bdrv_file_open) {
|
505 | 66f82cee | Kevin Wolf | ret = drv->bdrv_file_open(bs, filename, open_flags); |
506 | 66f82cee | Kevin Wolf | } else {
|
507 | 66f82cee | Kevin Wolf | ret = bdrv_file_open(&bs->file, filename, open_flags); |
508 | 66f82cee | Kevin Wolf | if (ret >= 0) { |
509 | 66f82cee | Kevin Wolf | ret = drv->bdrv_open(bs, open_flags); |
510 | 66f82cee | Kevin Wolf | } |
511 | 66f82cee | Kevin Wolf | } |
512 | 66f82cee | Kevin Wolf | |
513 | 57915332 | Kevin Wolf | if (ret < 0) { |
514 | 57915332 | Kevin Wolf | goto free_and_fail;
|
515 | 57915332 | Kevin Wolf | } |
516 | 57915332 | Kevin Wolf | |
517 | 57915332 | Kevin Wolf | bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); |
518 | 51762288 | Stefan Hajnoczi | |
519 | 51762288 | Stefan Hajnoczi | ret = refresh_total_sectors(bs, bs->total_sectors); |
520 | 51762288 | Stefan Hajnoczi | if (ret < 0) { |
521 | 51762288 | Stefan Hajnoczi | goto free_and_fail;
|
522 | 57915332 | Kevin Wolf | } |
523 | 51762288 | Stefan Hajnoczi | |
524 | 57915332 | Kevin Wolf | #ifndef _WIN32
|
525 | 57915332 | Kevin Wolf | if (bs->is_temporary) {
|
526 | 57915332 | Kevin Wolf | unlink(filename); |
527 | 57915332 | Kevin Wolf | } |
528 | 57915332 | Kevin Wolf | #endif
|
529 | 57915332 | Kevin Wolf | return 0; |
530 | 57915332 | Kevin Wolf | |
531 | 57915332 | Kevin Wolf | free_and_fail:
|
532 | 66f82cee | Kevin Wolf | if (bs->file) {
|
533 | 66f82cee | Kevin Wolf | bdrv_delete(bs->file); |
534 | 66f82cee | Kevin Wolf | bs->file = NULL;
|
535 | 66f82cee | Kevin Wolf | } |
536 | 7267c094 | Anthony Liguori | g_free(bs->opaque); |
537 | 57915332 | Kevin Wolf | bs->opaque = NULL;
|
538 | 57915332 | Kevin Wolf | bs->drv = NULL;
|
539 | 57915332 | Kevin Wolf | return ret;
|
540 | 57915332 | Kevin Wolf | } |
541 | 57915332 | Kevin Wolf | |
542 | 57915332 | Kevin Wolf | /*
|
543 | b6ce07aa | Kevin Wolf | * Opens a file using a protocol (file, host_device, nbd, ...)
|
544 | b6ce07aa | Kevin Wolf | */
|
545 | 83f64091 | bellard | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
546 | ea2384d3 | bellard | { |
547 | 83f64091 | bellard | BlockDriverState *bs; |
548 | 6db95603 | Christoph Hellwig | BlockDriver *drv; |
549 | 83f64091 | bellard | int ret;
|
550 | 83f64091 | bellard | |
551 | b50cbabc | MORITA Kazutaka | drv = bdrv_find_protocol(filename); |
552 | 6db95603 | Christoph Hellwig | if (!drv) {
|
553 | 6db95603 | Christoph Hellwig | return -ENOENT;
|
554 | 6db95603 | Christoph Hellwig | } |
555 | 6db95603 | Christoph Hellwig | |
556 | 83f64091 | bellard | bs = bdrv_new("");
|
557 | b6ce07aa | Kevin Wolf | ret = bdrv_open_common(bs, filename, flags, drv); |
558 | 83f64091 | bellard | if (ret < 0) { |
559 | 83f64091 | bellard | bdrv_delete(bs); |
560 | 83f64091 | bellard | return ret;
|
561 | 3b0d4f61 | bellard | } |
562 | 71d0770c | aliguori | bs->growable = 1;
|
563 | 83f64091 | bellard | *pbs = bs; |
564 | 83f64091 | bellard | return 0; |
565 | 83f64091 | bellard | } |
566 | 83f64091 | bellard | |
567 | b6ce07aa | Kevin Wolf | /*
|
568 | b6ce07aa | Kevin Wolf | * Opens a disk image (raw, qcow2, vmdk, ...)
|
569 | b6ce07aa | Kevin Wolf | */
|
570 | d6e9098e | Kevin Wolf | int bdrv_open(BlockDriverState *bs, const char *filename, int flags, |
571 | d6e9098e | Kevin Wolf | BlockDriver *drv) |
572 | ea2384d3 | bellard | { |
573 | b6ce07aa | Kevin Wolf | int ret;
|
574 | 712e7874 | bellard | |
575 | 83f64091 | bellard | if (flags & BDRV_O_SNAPSHOT) {
|
576 | ea2384d3 | bellard | BlockDriverState *bs1; |
577 | ea2384d3 | bellard | int64_t total_size; |
578 | 7c96d46e | aliguori | int is_protocol = 0; |
579 | 91a073a9 | Kevin Wolf | BlockDriver *bdrv_qcow2; |
580 | 91a073a9 | Kevin Wolf | QEMUOptionParameter *options; |
581 | b6ce07aa | Kevin Wolf | char tmp_filename[PATH_MAX];
|
582 | b6ce07aa | Kevin Wolf | char backing_filename[PATH_MAX];
|
583 | 3b46e624 | ths | |
584 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
585 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
586 | 33e3963e | bellard | |
587 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
588 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
589 | d6e9098e | Kevin Wolf | ret = bdrv_open(bs1, filename, 0, drv);
|
590 | 51d7c00c | aliguori | if (ret < 0) { |
591 | ea2384d3 | bellard | bdrv_delete(bs1); |
592 | 51d7c00c | aliguori | return ret;
|
593 | ea2384d3 | bellard | } |
594 | 3e82990b | Jes Sorensen | total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; |
595 | 7c96d46e | aliguori | |
596 | 7c96d46e | aliguori | if (bs1->drv && bs1->drv->protocol_name)
|
597 | 7c96d46e | aliguori | is_protocol = 1;
|
598 | 7c96d46e | aliguori | |
599 | ea2384d3 | bellard | bdrv_delete(bs1); |
600 | 3b46e624 | ths | |
601 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
602 | 7c96d46e | aliguori | |
603 | 7c96d46e | aliguori | /* Real path is meaningless for protocols */
|
604 | 7c96d46e | aliguori | if (is_protocol)
|
605 | 7c96d46e | aliguori | snprintf(backing_filename, sizeof(backing_filename),
|
606 | 7c96d46e | aliguori | "%s", filename);
|
607 | 114cdfa9 | Kirill A. Shutemov | else if (!realpath(filename, backing_filename)) |
608 | 114cdfa9 | Kirill A. Shutemov | return -errno;
|
609 | 7c96d46e | aliguori | |
610 | 91a073a9 | Kevin Wolf | bdrv_qcow2 = bdrv_find_format("qcow2");
|
611 | 91a073a9 | Kevin Wolf | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
612 | 91a073a9 | Kevin Wolf | |
613 | 3e82990b | Jes Sorensen | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); |
614 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
615 | 91a073a9 | Kevin Wolf | if (drv) {
|
616 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
617 | 91a073a9 | Kevin Wolf | drv->format_name); |
618 | 91a073a9 | Kevin Wolf | } |
619 | 91a073a9 | Kevin Wolf | |
620 | 91a073a9 | Kevin Wolf | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
621 | d748768c | Jan Kiszka | free_option_parameters(options); |
622 | 51d7c00c | aliguori | if (ret < 0) { |
623 | 51d7c00c | aliguori | return ret;
|
624 | ea2384d3 | bellard | } |
625 | 91a073a9 | Kevin Wolf | |
626 | ea2384d3 | bellard | filename = tmp_filename; |
627 | 91a073a9 | Kevin Wolf | drv = bdrv_qcow2; |
628 | ea2384d3 | bellard | bs->is_temporary = 1;
|
629 | ea2384d3 | bellard | } |
630 | 712e7874 | bellard | |
631 | b6ce07aa | Kevin Wolf | /* Find the right image format driver */
|
632 | 6db95603 | Christoph Hellwig | if (!drv) {
|
633 | c98ac35d | Stefan Weil | ret = find_image_format(filename, &drv); |
634 | 51d7c00c | aliguori | } |
635 | 6987307c | Christoph Hellwig | |
636 | 51d7c00c | aliguori | if (!drv) {
|
637 | 51d7c00c | aliguori | goto unlink_and_fail;
|
638 | ea2384d3 | bellard | } |
639 | b6ce07aa | Kevin Wolf | |
640 | b6ce07aa | Kevin Wolf | /* Open the image */
|
641 | b6ce07aa | Kevin Wolf | ret = bdrv_open_common(bs, filename, flags, drv); |
642 | b6ce07aa | Kevin Wolf | if (ret < 0) { |
643 | 6987307c | Christoph Hellwig | goto unlink_and_fail;
|
644 | 6987307c | Christoph Hellwig | } |
645 | 6987307c | Christoph Hellwig | |
646 | b6ce07aa | Kevin Wolf | /* If there is a backing file, use it */
|
647 | b6ce07aa | Kevin Wolf | if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { |
648 | b6ce07aa | Kevin Wolf | char backing_filename[PATH_MAX];
|
649 | b6ce07aa | Kevin Wolf | int back_flags;
|
650 | b6ce07aa | Kevin Wolf | BlockDriver *back_drv = NULL;
|
651 | b6ce07aa | Kevin Wolf | |
652 | b6ce07aa | Kevin Wolf | bs->backing_hd = bdrv_new("");
|
653 | df2dbb4a | Stefan Hajnoczi | |
654 | df2dbb4a | Stefan Hajnoczi | if (path_has_protocol(bs->backing_file)) {
|
655 | df2dbb4a | Stefan Hajnoczi | pstrcpy(backing_filename, sizeof(backing_filename),
|
656 | df2dbb4a | Stefan Hajnoczi | bs->backing_file); |
657 | df2dbb4a | Stefan Hajnoczi | } else {
|
658 | df2dbb4a | Stefan Hajnoczi | path_combine(backing_filename, sizeof(backing_filename),
|
659 | df2dbb4a | Stefan Hajnoczi | filename, bs->backing_file); |
660 | df2dbb4a | Stefan Hajnoczi | } |
661 | df2dbb4a | Stefan Hajnoczi | |
662 | df2dbb4a | Stefan Hajnoczi | if (bs->backing_format[0] != '\0') { |
663 | b6ce07aa | Kevin Wolf | back_drv = bdrv_find_format(bs->backing_format); |
664 | df2dbb4a | Stefan Hajnoczi | } |
665 | b6ce07aa | Kevin Wolf | |
666 | b6ce07aa | Kevin Wolf | /* backing files always opened read-only */
|
667 | b6ce07aa | Kevin Wolf | back_flags = |
668 | b6ce07aa | Kevin Wolf | flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); |
669 | b6ce07aa | Kevin Wolf | |
670 | b6ce07aa | Kevin Wolf | ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); |
671 | b6ce07aa | Kevin Wolf | if (ret < 0) { |
672 | b6ce07aa | Kevin Wolf | bdrv_close(bs); |
673 | b6ce07aa | Kevin Wolf | return ret;
|
674 | b6ce07aa | Kevin Wolf | } |
675 | b6ce07aa | Kevin Wolf | if (bs->is_temporary) {
|
676 | b6ce07aa | Kevin Wolf | bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); |
677 | b6ce07aa | Kevin Wolf | } else {
|
678 | b6ce07aa | Kevin Wolf | /* base image inherits from "parent" */
|
679 | b6ce07aa | Kevin Wolf | bs->backing_hd->keep_read_only = bs->keep_read_only; |
680 | b6ce07aa | Kevin Wolf | } |
681 | b6ce07aa | Kevin Wolf | } |
682 | b6ce07aa | Kevin Wolf | |
683 | b6ce07aa | Kevin Wolf | if (!bdrv_key_required(bs)) {
|
684 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, true);
|
685 | b6ce07aa | Kevin Wolf | } |
686 | b6ce07aa | Kevin Wolf | |
687 | b6ce07aa | Kevin Wolf | return 0; |
688 | b6ce07aa | Kevin Wolf | |
689 | b6ce07aa | Kevin Wolf | unlink_and_fail:
|
690 | b6ce07aa | Kevin Wolf | if (bs->is_temporary) {
|
691 | b6ce07aa | Kevin Wolf | unlink(filename); |
692 | b6ce07aa | Kevin Wolf | } |
693 | b6ce07aa | Kevin Wolf | return ret;
|
694 | b6ce07aa | Kevin Wolf | } |
695 | b6ce07aa | Kevin Wolf | |
696 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
697 | fc01f7e7 | bellard | { |
698 | 19cb3738 | bellard | if (bs->drv) {
|
699 | f9092b10 | Markus Armbruster | if (bs == bs_snapshots) {
|
700 | f9092b10 | Markus Armbruster | bs_snapshots = NULL;
|
701 | f9092b10 | Markus Armbruster | } |
702 | 557df6ac | Stefan Hajnoczi | if (bs->backing_hd) {
|
703 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
704 | 557df6ac | Stefan Hajnoczi | bs->backing_hd = NULL;
|
705 | 557df6ac | Stefan Hajnoczi | } |
706 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
707 | 7267c094 | Anthony Liguori | g_free(bs->opaque); |
708 | ea2384d3 | bellard | #ifdef _WIN32
|
709 | ea2384d3 | bellard | if (bs->is_temporary) {
|
710 | ea2384d3 | bellard | unlink(bs->filename); |
711 | ea2384d3 | bellard | } |
712 | 67b915a5 | bellard | #endif
|
713 | ea2384d3 | bellard | bs->opaque = NULL;
|
714 | ea2384d3 | bellard | bs->drv = NULL;
|
715 | b338082b | bellard | |
716 | 66f82cee | Kevin Wolf | if (bs->file != NULL) { |
717 | 66f82cee | Kevin Wolf | bdrv_close(bs->file); |
718 | 66f82cee | Kevin Wolf | } |
719 | 66f82cee | Kevin Wolf | |
720 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, false);
|
721 | b338082b | bellard | } |
722 | b338082b | bellard | } |
723 | b338082b | bellard | |
724 | 2bc93fed | MORITA Kazutaka | void bdrv_close_all(void) |
725 | 2bc93fed | MORITA Kazutaka | { |
726 | 2bc93fed | MORITA Kazutaka | BlockDriverState *bs; |
727 | 2bc93fed | MORITA Kazutaka | |
728 | 2bc93fed | MORITA Kazutaka | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
729 | 2bc93fed | MORITA Kazutaka | bdrv_close(bs); |
730 | 2bc93fed | MORITA Kazutaka | } |
731 | 2bc93fed | MORITA Kazutaka | } |
732 | 2bc93fed | MORITA Kazutaka | |
733 | d22b2f41 | Ryan Harper | /* make a BlockDriverState anonymous by removing from bdrv_state list.
|
734 | d22b2f41 | Ryan Harper | Also, NULL terminate the device_name to prevent double remove */
|
735 | d22b2f41 | Ryan Harper | void bdrv_make_anon(BlockDriverState *bs)
|
736 | d22b2f41 | Ryan Harper | { |
737 | d22b2f41 | Ryan Harper | if (bs->device_name[0] != '\0') { |
738 | d22b2f41 | Ryan Harper | QTAILQ_REMOVE(&bdrv_states, bs, list); |
739 | d22b2f41 | Ryan Harper | } |
740 | d22b2f41 | Ryan Harper | bs->device_name[0] = '\0'; |
741 | d22b2f41 | Ryan Harper | } |
742 | d22b2f41 | Ryan Harper | |
743 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
744 | b338082b | bellard | { |
745 | fa879d62 | Markus Armbruster | assert(!bs->dev); |
746 | 18846dee | Markus Armbruster | |
747 | 1b7bdbc1 | Stefan Hajnoczi | /* remove from list, if necessary */
|
748 | d22b2f41 | Ryan Harper | bdrv_make_anon(bs); |
749 | 34c6f050 | aurel32 | |
750 | b338082b | bellard | bdrv_close(bs); |
751 | 66f82cee | Kevin Wolf | if (bs->file != NULL) { |
752 | 66f82cee | Kevin Wolf | bdrv_delete(bs->file); |
753 | 66f82cee | Kevin Wolf | } |
754 | 66f82cee | Kevin Wolf | |
755 | f9092b10 | Markus Armbruster | assert(bs != bs_snapshots); |
756 | 7267c094 | Anthony Liguori | g_free(bs); |
757 | fc01f7e7 | bellard | } |
758 | fc01f7e7 | bellard | |
759 | fa879d62 | Markus Armbruster | int bdrv_attach_dev(BlockDriverState *bs, void *dev) |
760 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
761 | 18846dee | Markus Armbruster | { |
762 | fa879d62 | Markus Armbruster | if (bs->dev) {
|
763 | 18846dee | Markus Armbruster | return -EBUSY;
|
764 | 18846dee | Markus Armbruster | } |
765 | fa879d62 | Markus Armbruster | bs->dev = dev; |
766 | 28a7282a | Luiz Capitulino | bdrv_iostatus_reset(bs); |
767 | 18846dee | Markus Armbruster | return 0; |
768 | 18846dee | Markus Armbruster | } |
769 | 18846dee | Markus Armbruster | |
770 | fa879d62 | Markus Armbruster | /* TODO qdevified devices don't use this, remove when devices are qdevified */
|
771 | fa879d62 | Markus Armbruster | void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) |
772 | 18846dee | Markus Armbruster | { |
773 | fa879d62 | Markus Armbruster | if (bdrv_attach_dev(bs, dev) < 0) { |
774 | fa879d62 | Markus Armbruster | abort(); |
775 | fa879d62 | Markus Armbruster | } |
776 | fa879d62 | Markus Armbruster | } |
777 | fa879d62 | Markus Armbruster | |
778 | fa879d62 | Markus Armbruster | void bdrv_detach_dev(BlockDriverState *bs, void *dev) |
779 | fa879d62 | Markus Armbruster | /* TODO change to DeviceState *dev when all users are qdevified */
|
780 | fa879d62 | Markus Armbruster | { |
781 | fa879d62 | Markus Armbruster | assert(bs->dev == dev); |
782 | fa879d62 | Markus Armbruster | bs->dev = NULL;
|
783 | 0e49de52 | Markus Armbruster | bs->dev_ops = NULL;
|
784 | 0e49de52 | Markus Armbruster | bs->dev_opaque = NULL;
|
785 | 29e05f20 | Markus Armbruster | bs->buffer_alignment = 512;
|
786 | 18846dee | Markus Armbruster | } |
787 | 18846dee | Markus Armbruster | |
788 | fa879d62 | Markus Armbruster | /* TODO change to return DeviceState * when all users are qdevified */
|
789 | fa879d62 | Markus Armbruster | void *bdrv_get_attached_dev(BlockDriverState *bs)
|
790 | 18846dee | Markus Armbruster | { |
791 | fa879d62 | Markus Armbruster | return bs->dev;
|
792 | 18846dee | Markus Armbruster | } |
793 | 18846dee | Markus Armbruster | |
794 | 0e49de52 | Markus Armbruster | void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, |
795 | 0e49de52 | Markus Armbruster | void *opaque)
|
796 | 0e49de52 | Markus Armbruster | { |
797 | 0e49de52 | Markus Armbruster | bs->dev_ops = ops; |
798 | 0e49de52 | Markus Armbruster | bs->dev_opaque = opaque; |
799 | 2c6942fa | Markus Armbruster | if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
|
800 | 2c6942fa | Markus Armbruster | bs_snapshots = NULL;
|
801 | 2c6942fa | Markus Armbruster | } |
802 | 0e49de52 | Markus Armbruster | } |
803 | 0e49de52 | Markus Armbruster | |
804 | 7d4b4ba5 | Markus Armbruster | static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) |
805 | 0e49de52 | Markus Armbruster | { |
806 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->change_media_cb) {
|
807 | 7d4b4ba5 | Markus Armbruster | bs->dev_ops->change_media_cb(bs->dev_opaque, load); |
808 | 145feb17 | Markus Armbruster | } |
809 | 145feb17 | Markus Armbruster | } |
810 | 145feb17 | Markus Armbruster | |
811 | 2c6942fa | Markus Armbruster | bool bdrv_dev_has_removable_media(BlockDriverState *bs)
|
812 | 2c6942fa | Markus Armbruster | { |
813 | 2c6942fa | Markus Armbruster | return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
|
814 | 2c6942fa | Markus Armbruster | } |
815 | 2c6942fa | Markus Armbruster | |
816 | e4def80b | Markus Armbruster | bool bdrv_dev_is_tray_open(BlockDriverState *bs)
|
817 | e4def80b | Markus Armbruster | { |
818 | e4def80b | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_tray_open) {
|
819 | e4def80b | Markus Armbruster | return bs->dev_ops->is_tray_open(bs->dev_opaque);
|
820 | e4def80b | Markus Armbruster | } |
821 | e4def80b | Markus Armbruster | return false; |
822 | e4def80b | Markus Armbruster | } |
823 | e4def80b | Markus Armbruster | |
824 | 145feb17 | Markus Armbruster | static void bdrv_dev_resize_cb(BlockDriverState *bs) |
825 | 145feb17 | Markus Armbruster | { |
826 | 145feb17 | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->resize_cb) {
|
827 | 145feb17 | Markus Armbruster | bs->dev_ops->resize_cb(bs->dev_opaque); |
828 | 0e49de52 | Markus Armbruster | } |
829 | 0e49de52 | Markus Armbruster | } |
830 | 0e49de52 | Markus Armbruster | |
831 | f107639a | Markus Armbruster | bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
|
832 | f107639a | Markus Armbruster | { |
833 | f107639a | Markus Armbruster | if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
|
834 | f107639a | Markus Armbruster | return bs->dev_ops->is_medium_locked(bs->dev_opaque);
|
835 | f107639a | Markus Armbruster | } |
836 | f107639a | Markus Armbruster | return false; |
837 | f107639a | Markus Armbruster | } |
838 | f107639a | Markus Armbruster | |
839 | e97fc193 | aliguori | /*
|
840 | e97fc193 | aliguori | * Run consistency checks on an image
|
841 | e97fc193 | aliguori | *
|
842 | e076f338 | Kevin Wolf | * Returns 0 if the check could be completed (it doesn't mean that the image is
|
843 | a1c7273b | Stefan Weil | * free of errors) or -errno when an internal error occurred. The results of the
|
844 | e076f338 | Kevin Wolf | * check are stored in res.
|
845 | e97fc193 | aliguori | */
|
846 | e076f338 | Kevin Wolf | int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
|
847 | e97fc193 | aliguori | { |
848 | e97fc193 | aliguori | if (bs->drv->bdrv_check == NULL) { |
849 | e97fc193 | aliguori | return -ENOTSUP;
|
850 | e97fc193 | aliguori | } |
851 | e97fc193 | aliguori | |
852 | e076f338 | Kevin Wolf | memset(res, 0, sizeof(*res)); |
853 | 9ac228e0 | Kevin Wolf | return bs->drv->bdrv_check(bs, res);
|
854 | e97fc193 | aliguori | } |
855 | e97fc193 | aliguori | |
856 | 8a426614 | Kevin Wolf | #define COMMIT_BUF_SECTORS 2048 |
857 | 8a426614 | Kevin Wolf | |
858 | 33e3963e | bellard | /* commit COW file into the raw image */
|
859 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
860 | 33e3963e | bellard | { |
861 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
862 | ee181196 | Kevin Wolf | BlockDriver *backing_drv; |
863 | 8a426614 | Kevin Wolf | int64_t sector, total_sectors; |
864 | 8a426614 | Kevin Wolf | int n, ro, open_flags;
|
865 | 4dca4b63 | Naphtali Sprei | int ret = 0, rw_ret = 0; |
866 | 8a426614 | Kevin Wolf | uint8_t *buf; |
867 | 4dca4b63 | Naphtali Sprei | char filename[1024]; |
868 | 4dca4b63 | Naphtali Sprei | BlockDriverState *bs_rw, *bs_ro; |
869 | 33e3963e | bellard | |
870 | 19cb3738 | bellard | if (!drv)
|
871 | 19cb3738 | bellard | return -ENOMEDIUM;
|
872 | 4dca4b63 | Naphtali Sprei | |
873 | 4dca4b63 | Naphtali Sprei | if (!bs->backing_hd) {
|
874 | 4dca4b63 | Naphtali Sprei | return -ENOTSUP;
|
875 | 33e3963e | bellard | } |
876 | 33e3963e | bellard | |
877 | 4dca4b63 | Naphtali Sprei | if (bs->backing_hd->keep_read_only) {
|
878 | 4dca4b63 | Naphtali Sprei | return -EACCES;
|
879 | 4dca4b63 | Naphtali Sprei | } |
880 | ee181196 | Kevin Wolf | |
881 | ee181196 | Kevin Wolf | backing_drv = bs->backing_hd->drv; |
882 | 4dca4b63 | Naphtali Sprei | ro = bs->backing_hd->read_only; |
883 | 4dca4b63 | Naphtali Sprei | strncpy(filename, bs->backing_hd->filename, sizeof(filename));
|
884 | 4dca4b63 | Naphtali Sprei | open_flags = bs->backing_hd->open_flags; |
885 | 4dca4b63 | Naphtali Sprei | |
886 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
887 | 4dca4b63 | Naphtali Sprei | /* re-open as RW */
|
888 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs->backing_hd); |
889 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = NULL;
|
890 | 4dca4b63 | Naphtali Sprei | bs_rw = bdrv_new("");
|
891 | ee181196 | Kevin Wolf | rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, |
892 | ee181196 | Kevin Wolf | backing_drv); |
893 | 4dca4b63 | Naphtali Sprei | if (rw_ret < 0) { |
894 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_rw); |
895 | 4dca4b63 | Naphtali Sprei | /* try to re-open read-only */
|
896 | 4dca4b63 | Naphtali Sprei | bs_ro = bdrv_new("");
|
897 | ee181196 | Kevin Wolf | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
898 | ee181196 | Kevin Wolf | backing_drv); |
899 | 4dca4b63 | Naphtali Sprei | if (ret < 0) { |
900 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_ro); |
901 | 4dca4b63 | Naphtali Sprei | /* drive not functional anymore */
|
902 | 4dca4b63 | Naphtali Sprei | bs->drv = NULL;
|
903 | 4dca4b63 | Naphtali Sprei | return ret;
|
904 | 4dca4b63 | Naphtali Sprei | } |
905 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_ro; |
906 | 4dca4b63 | Naphtali Sprei | return rw_ret;
|
907 | 4dca4b63 | Naphtali Sprei | } |
908 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_rw; |
909 | ea2384d3 | bellard | } |
910 | 33e3963e | bellard | |
911 | 6ea44308 | Jan Kiszka | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
912 | 7267c094 | Anthony Liguori | buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); |
913 | 8a426614 | Kevin Wolf | |
914 | 8a426614 | Kevin Wolf | for (sector = 0; sector < total_sectors; sector += n) { |
915 | 8a426614 | Kevin Wolf | if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
|
916 | 8a426614 | Kevin Wolf | |
917 | 8a426614 | Kevin Wolf | if (bdrv_read(bs, sector, buf, n) != 0) { |
918 | 8a426614 | Kevin Wolf | ret = -EIO; |
919 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
920 | 8a426614 | Kevin Wolf | } |
921 | 8a426614 | Kevin Wolf | |
922 | 8a426614 | Kevin Wolf | if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { |
923 | 8a426614 | Kevin Wolf | ret = -EIO; |
924 | 8a426614 | Kevin Wolf | goto ro_cleanup;
|
925 | 8a426614 | Kevin Wolf | } |
926 | ea2384d3 | bellard | } |
927 | 33e3963e | bellard | } |
928 | 95389c86 | bellard | |
929 | 1d44952f | Christoph Hellwig | if (drv->bdrv_make_empty) {
|
930 | 1d44952f | Christoph Hellwig | ret = drv->bdrv_make_empty(bs); |
931 | 1d44952f | Christoph Hellwig | bdrv_flush(bs); |
932 | 1d44952f | Christoph Hellwig | } |
933 | 95389c86 | bellard | |
934 | 3f5075ae | Christoph Hellwig | /*
|
935 | 3f5075ae | Christoph Hellwig | * Make sure all data we wrote to the backing device is actually
|
936 | 3f5075ae | Christoph Hellwig | * stable on disk.
|
937 | 3f5075ae | Christoph Hellwig | */
|
938 | 3f5075ae | Christoph Hellwig | if (bs->backing_hd)
|
939 | 3f5075ae | Christoph Hellwig | bdrv_flush(bs->backing_hd); |
940 | 4dca4b63 | Naphtali Sprei | |
941 | 4dca4b63 | Naphtali Sprei | ro_cleanup:
|
942 | 7267c094 | Anthony Liguori | g_free(buf); |
943 | 4dca4b63 | Naphtali Sprei | |
944 | 4dca4b63 | Naphtali Sprei | if (ro) {
|
945 | 4dca4b63 | Naphtali Sprei | /* re-open as RO */
|
946 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs->backing_hd); |
947 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = NULL;
|
948 | 4dca4b63 | Naphtali Sprei | bs_ro = bdrv_new("");
|
949 | ee181196 | Kevin Wolf | ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, |
950 | ee181196 | Kevin Wolf | backing_drv); |
951 | 4dca4b63 | Naphtali Sprei | if (ret < 0) { |
952 | 4dca4b63 | Naphtali Sprei | bdrv_delete(bs_ro); |
953 | 4dca4b63 | Naphtali Sprei | /* drive not functional anymore */
|
954 | 4dca4b63 | Naphtali Sprei | bs->drv = NULL;
|
955 | 4dca4b63 | Naphtali Sprei | return ret;
|
956 | 4dca4b63 | Naphtali Sprei | } |
957 | 4dca4b63 | Naphtali Sprei | bs->backing_hd = bs_ro; |
958 | 4dca4b63 | Naphtali Sprei | bs->backing_hd->keep_read_only = 0;
|
959 | 4dca4b63 | Naphtali Sprei | } |
960 | 4dca4b63 | Naphtali Sprei | |
961 | 1d44952f | Christoph Hellwig | return ret;
|
962 | 33e3963e | bellard | } |
963 | 33e3963e | bellard | |
964 | 6ab4b5ab | Markus Armbruster | void bdrv_commit_all(void) |
965 | 6ab4b5ab | Markus Armbruster | { |
966 | 6ab4b5ab | Markus Armbruster | BlockDriverState *bs; |
967 | 6ab4b5ab | Markus Armbruster | |
968 | 6ab4b5ab | Markus Armbruster | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
969 | 6ab4b5ab | Markus Armbruster | bdrv_commit(bs); |
970 | 6ab4b5ab | Markus Armbruster | } |
971 | 6ab4b5ab | Markus Armbruster | } |
972 | 6ab4b5ab | Markus Armbruster | |
973 | 756e6736 | Kevin Wolf | /*
|
974 | 756e6736 | Kevin Wolf | * Return values:
|
975 | 756e6736 | Kevin Wolf | * 0 - success
|
976 | 756e6736 | Kevin Wolf | * -EINVAL - backing format specified, but no file
|
977 | 756e6736 | Kevin Wolf | * -ENOSPC - can't update the backing file because no space is left in the
|
978 | 756e6736 | Kevin Wolf | * image file header
|
979 | 756e6736 | Kevin Wolf | * -ENOTSUP - format driver doesn't support changing the backing file
|
980 | 756e6736 | Kevin Wolf | */
|
981 | 756e6736 | Kevin Wolf | int bdrv_change_backing_file(BlockDriverState *bs,
|
982 | 756e6736 | Kevin Wolf | const char *backing_file, const char *backing_fmt) |
983 | 756e6736 | Kevin Wolf | { |
984 | 756e6736 | Kevin Wolf | BlockDriver *drv = bs->drv; |
985 | 756e6736 | Kevin Wolf | |
986 | 756e6736 | Kevin Wolf | if (drv->bdrv_change_backing_file != NULL) { |
987 | 756e6736 | Kevin Wolf | return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
|
988 | 756e6736 | Kevin Wolf | } else {
|
989 | 756e6736 | Kevin Wolf | return -ENOTSUP;
|
990 | 756e6736 | Kevin Wolf | } |
991 | 756e6736 | Kevin Wolf | } |
992 | 756e6736 | Kevin Wolf | |
993 | 71d0770c | aliguori | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
994 | 71d0770c | aliguori | size_t size) |
995 | 71d0770c | aliguori | { |
996 | 71d0770c | aliguori | int64_t len; |
997 | 71d0770c | aliguori | |
998 | 71d0770c | aliguori | if (!bdrv_is_inserted(bs))
|
999 | 71d0770c | aliguori | return -ENOMEDIUM;
|
1000 | 71d0770c | aliguori | |
1001 | 71d0770c | aliguori | if (bs->growable)
|
1002 | 71d0770c | aliguori | return 0; |
1003 | 71d0770c | aliguori | |
1004 | 71d0770c | aliguori | len = bdrv_getlength(bs); |
1005 | 71d0770c | aliguori | |
1006 | fbb7b4e0 | Kevin Wolf | if (offset < 0) |
1007 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
1008 | fbb7b4e0 | Kevin Wolf | |
1009 | fbb7b4e0 | Kevin Wolf | if ((offset > len) || (len - offset < size))
|
1010 | 71d0770c | aliguori | return -EIO;
|
1011 | 71d0770c | aliguori | |
1012 | 71d0770c | aliguori | return 0; |
1013 | 71d0770c | aliguori | } |
1014 | 71d0770c | aliguori | |
1015 | 71d0770c | aliguori | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
1016 | 71d0770c | aliguori | int nb_sectors)
|
1017 | 71d0770c | aliguori | { |
1018 | eb5a3165 | Jes Sorensen | return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
|
1019 | eb5a3165 | Jes Sorensen | nb_sectors * BDRV_SECTOR_SIZE); |
1020 | 71d0770c | aliguori | } |
1021 | 71d0770c | aliguori | |
1022 | 1c9805a3 | Stefan Hajnoczi | typedef struct RwCo { |
1023 | 1c9805a3 | Stefan Hajnoczi | BlockDriverState *bs; |
1024 | 1c9805a3 | Stefan Hajnoczi | int64_t sector_num; |
1025 | 1c9805a3 | Stefan Hajnoczi | int nb_sectors;
|
1026 | 1c9805a3 | Stefan Hajnoczi | QEMUIOVector *qiov; |
1027 | 1c9805a3 | Stefan Hajnoczi | bool is_write;
|
1028 | 1c9805a3 | Stefan Hajnoczi | int ret;
|
1029 | 1c9805a3 | Stefan Hajnoczi | } RwCo; |
1030 | 1c9805a3 | Stefan Hajnoczi | |
1031 | 1c9805a3 | Stefan Hajnoczi | static void coroutine_fn bdrv_rw_co_entry(void *opaque) |
1032 | fc01f7e7 | bellard | { |
1033 | 1c9805a3 | Stefan Hajnoczi | RwCo *rwco = opaque; |
1034 | ea2384d3 | bellard | |
1035 | 1c9805a3 | Stefan Hajnoczi | if (!rwco->is_write) {
|
1036 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, |
1037 | 1c9805a3 | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov); |
1038 | 1c9805a3 | Stefan Hajnoczi | } else {
|
1039 | 1c9805a3 | Stefan Hajnoczi | rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, |
1040 | 1c9805a3 | Stefan Hajnoczi | rwco->nb_sectors, rwco->qiov); |
1041 | 1c9805a3 | Stefan Hajnoczi | } |
1042 | 1c9805a3 | Stefan Hajnoczi | } |
1043 | e7a8a783 | Kevin Wolf | |
1044 | 1c9805a3 | Stefan Hajnoczi | /*
|
1045 | 1c9805a3 | Stefan Hajnoczi | * Process a synchronous request using coroutines
|
1046 | 1c9805a3 | Stefan Hajnoczi | */
|
1047 | 1c9805a3 | Stefan Hajnoczi | static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, |
1048 | 1c9805a3 | Stefan Hajnoczi | int nb_sectors, bool is_write) |
1049 | 1c9805a3 | Stefan Hajnoczi | { |
1050 | 1c9805a3 | Stefan Hajnoczi | QEMUIOVector qiov; |
1051 | 1c9805a3 | Stefan Hajnoczi | struct iovec iov = {
|
1052 | 1c9805a3 | Stefan Hajnoczi | .iov_base = (void *)buf,
|
1053 | 1c9805a3 | Stefan Hajnoczi | .iov_len = nb_sectors * BDRV_SECTOR_SIZE, |
1054 | 1c9805a3 | Stefan Hajnoczi | }; |
1055 | 1c9805a3 | Stefan Hajnoczi | Coroutine *co; |
1056 | 1c9805a3 | Stefan Hajnoczi | RwCo rwco = { |
1057 | 1c9805a3 | Stefan Hajnoczi | .bs = bs, |
1058 | 1c9805a3 | Stefan Hajnoczi | .sector_num = sector_num, |
1059 | 1c9805a3 | Stefan Hajnoczi | .nb_sectors = nb_sectors, |
1060 | 1c9805a3 | Stefan Hajnoczi | .qiov = &qiov, |
1061 | 1c9805a3 | Stefan Hajnoczi | .is_write = is_write, |
1062 | 1c9805a3 | Stefan Hajnoczi | .ret = NOT_DONE, |
1063 | 1c9805a3 | Stefan Hajnoczi | }; |
1064 | e7a8a783 | Kevin Wolf | |
1065 | 1c9805a3 | Stefan Hajnoczi | qemu_iovec_init_external(&qiov, &iov, 1);
|
1066 | e7a8a783 | Kevin Wolf | |
1067 | 1c9805a3 | Stefan Hajnoczi | if (qemu_in_coroutine()) {
|
1068 | 1c9805a3 | Stefan Hajnoczi | /* Fast-path if already in coroutine context */
|
1069 | 1c9805a3 | Stefan Hajnoczi | bdrv_rw_co_entry(&rwco); |
1070 | 1c9805a3 | Stefan Hajnoczi | } else {
|
1071 | 1c9805a3 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_rw_co_entry); |
1072 | 1c9805a3 | Stefan Hajnoczi | qemu_coroutine_enter(co, &rwco); |
1073 | 1c9805a3 | Stefan Hajnoczi | while (rwco.ret == NOT_DONE) {
|
1074 | 1c9805a3 | Stefan Hajnoczi | qemu_aio_wait(); |
1075 | 1c9805a3 | Stefan Hajnoczi | } |
1076 | 1c9805a3 | Stefan Hajnoczi | } |
1077 | 1c9805a3 | Stefan Hajnoczi | return rwco.ret;
|
1078 | 1c9805a3 | Stefan Hajnoczi | } |
1079 | b338082b | bellard | |
1080 | 1c9805a3 | Stefan Hajnoczi | /* return < 0 if error. See bdrv_write() for the return codes */
|
1081 | 1c9805a3 | Stefan Hajnoczi | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
1082 | 1c9805a3 | Stefan Hajnoczi | uint8_t *buf, int nb_sectors)
|
1083 | 1c9805a3 | Stefan Hajnoczi | { |
1084 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); |
1085 | fc01f7e7 | bellard | } |
1086 | fc01f7e7 | bellard | |
1087 | 7cd1e32a | lirans@il.ibm.com | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
1088 | a55eb92c | Jan Kiszka | int nb_sectors, int dirty) |
1089 | 7cd1e32a | lirans@il.ibm.com | { |
1090 | 7cd1e32a | lirans@il.ibm.com | int64_t start, end; |
1091 | c6d22830 | Jan Kiszka | unsigned long val, idx, bit; |
1092 | a55eb92c | Jan Kiszka | |
1093 | 6ea44308 | Jan Kiszka | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
1094 | c6d22830 | Jan Kiszka | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
|
1095 | a55eb92c | Jan Kiszka | |
1096 | a55eb92c | Jan Kiszka | for (; start <= end; start++) {
|
1097 | c6d22830 | Jan Kiszka | idx = start / (sizeof(unsigned long) * 8); |
1098 | c6d22830 | Jan Kiszka | bit = start % (sizeof(unsigned long) * 8); |
1099 | c6d22830 | Jan Kiszka | val = bs->dirty_bitmap[idx]; |
1100 | c6d22830 | Jan Kiszka | if (dirty) {
|
1101 | 6d59fec1 | Marcelo Tosatti | if (!(val & (1UL << bit))) { |
1102 | aaa0eb75 | Liran Schour | bs->dirty_count++; |
1103 | 6d59fec1 | Marcelo Tosatti | val |= 1UL << bit;
|
1104 | aaa0eb75 | Liran Schour | } |
1105 | c6d22830 | Jan Kiszka | } else {
|
1106 | 6d59fec1 | Marcelo Tosatti | if (val & (1UL << bit)) { |
1107 | aaa0eb75 | Liran Schour | bs->dirty_count--; |
1108 | 6d59fec1 | Marcelo Tosatti | val &= ~(1UL << bit);
|
1109 | aaa0eb75 | Liran Schour | } |
1110 | c6d22830 | Jan Kiszka | } |
1111 | c6d22830 | Jan Kiszka | bs->dirty_bitmap[idx] = val; |
1112 | 7cd1e32a | lirans@il.ibm.com | } |
1113 | 7cd1e32a | lirans@il.ibm.com | } |
1114 | 7cd1e32a | lirans@il.ibm.com | |
1115 | 5fafdf24 | ths | /* Return < 0 if error. Important errors are:
|
1116 | 19cb3738 | bellard | -EIO generic I/O error (may happen for all errors)
|
1117 | 19cb3738 | bellard | -ENOMEDIUM No media inserted.
|
1118 | 19cb3738 | bellard | -EINVAL Invalid sector number or nb_sectors
|
1119 | 19cb3738 | bellard | -EACCES Trying to write a read-only device
|
1120 | 19cb3738 | bellard | */
|
1121 | 5fafdf24 | ths | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
1122 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
1123 | fc01f7e7 | bellard | { |
1124 | 1c9805a3 | Stefan Hajnoczi | return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); |
1125 | 83f64091 | bellard | } |
1126 | 83f64091 | bellard | |
1127 | eda578e5 | aliguori | int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
1128 | eda578e5 | aliguori | void *buf, int count1) |
1129 | 83f64091 | bellard | { |
1130 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
1131 | 83f64091 | bellard | int len, nb_sectors, count;
|
1132 | 83f64091 | bellard | int64_t sector_num; |
1133 | 9a8c4cce | Kevin Wolf | int ret;
|
1134 | 83f64091 | bellard | |
1135 | 83f64091 | bellard | count = count1; |
1136 | 83f64091 | bellard | /* first read to align to sector start */
|
1137 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
1138 | 83f64091 | bellard | if (len > count)
|
1139 | 83f64091 | bellard | len = count; |
1140 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
1141 | 83f64091 | bellard | if (len > 0) { |
1142 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1143 | 9a8c4cce | Kevin Wolf | return ret;
|
1144 | 6ea44308 | Jan Kiszka | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
|
1145 | 83f64091 | bellard | count -= len; |
1146 | 83f64091 | bellard | if (count == 0) |
1147 | 83f64091 | bellard | return count1;
|
1148 | 83f64091 | bellard | sector_num++; |
1149 | 83f64091 | bellard | buf += len; |
1150 | 83f64091 | bellard | } |
1151 | 83f64091 | bellard | |
1152 | 83f64091 | bellard | /* read the sectors "in place" */
|
1153 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
1154 | 83f64091 | bellard | if (nb_sectors > 0) { |
1155 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
1156 | 9a8c4cce | Kevin Wolf | return ret;
|
1157 | 83f64091 | bellard | sector_num += nb_sectors; |
1158 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
1159 | 83f64091 | bellard | buf += len; |
1160 | 83f64091 | bellard | count -= len; |
1161 | 83f64091 | bellard | } |
1162 | 83f64091 | bellard | |
1163 | 83f64091 | bellard | /* add data from the last sector */
|
1164 | 83f64091 | bellard | if (count > 0) { |
1165 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1166 | 9a8c4cce | Kevin Wolf | return ret;
|
1167 | 83f64091 | bellard | memcpy(buf, tmp_buf, count); |
1168 | 83f64091 | bellard | } |
1169 | 83f64091 | bellard | return count1;
|
1170 | 83f64091 | bellard | } |
1171 | 83f64091 | bellard | |
1172 | eda578e5 | aliguori | int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
1173 | eda578e5 | aliguori | const void *buf, int count1) |
1174 | 83f64091 | bellard | { |
1175 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
1176 | 83f64091 | bellard | int len, nb_sectors, count;
|
1177 | 83f64091 | bellard | int64_t sector_num; |
1178 | 9a8c4cce | Kevin Wolf | int ret;
|
1179 | 83f64091 | bellard | |
1180 | 83f64091 | bellard | count = count1; |
1181 | 83f64091 | bellard | /* first write to align to sector start */
|
1182 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
1183 | 83f64091 | bellard | if (len > count)
|
1184 | 83f64091 | bellard | len = count; |
1185 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
1186 | 83f64091 | bellard | if (len > 0) { |
1187 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1188 | 9a8c4cce | Kevin Wolf | return ret;
|
1189 | 6ea44308 | Jan Kiszka | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
|
1190 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1191 | 9a8c4cce | Kevin Wolf | return ret;
|
1192 | 83f64091 | bellard | count -= len; |
1193 | 83f64091 | bellard | if (count == 0) |
1194 | 83f64091 | bellard | return count1;
|
1195 | 83f64091 | bellard | sector_num++; |
1196 | 83f64091 | bellard | buf += len; |
1197 | 83f64091 | bellard | } |
1198 | 83f64091 | bellard | |
1199 | 83f64091 | bellard | /* write the sectors "in place" */
|
1200 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
1201 | 83f64091 | bellard | if (nb_sectors > 0) { |
1202 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
1203 | 9a8c4cce | Kevin Wolf | return ret;
|
1204 | 83f64091 | bellard | sector_num += nb_sectors; |
1205 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
1206 | 83f64091 | bellard | buf += len; |
1207 | 83f64091 | bellard | count -= len; |
1208 | 83f64091 | bellard | } |
1209 | 83f64091 | bellard | |
1210 | 83f64091 | bellard | /* add data from the last sector */
|
1211 | 83f64091 | bellard | if (count > 0) { |
1212 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
1213 | 9a8c4cce | Kevin Wolf | return ret;
|
1214 | 83f64091 | bellard | memcpy(tmp_buf, buf, count); |
1215 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
1216 | 9a8c4cce | Kevin Wolf | return ret;
|
1217 | 83f64091 | bellard | } |
1218 | 83f64091 | bellard | return count1;
|
1219 | 83f64091 | bellard | } |
1220 | 83f64091 | bellard | |
1221 | f08145fe | Kevin Wolf | /*
|
1222 | f08145fe | Kevin Wolf | * Writes to the file and ensures that no writes are reordered across this
|
1223 | f08145fe | Kevin Wolf | * request (acts as a barrier)
|
1224 | f08145fe | Kevin Wolf | *
|
1225 | f08145fe | Kevin Wolf | * Returns 0 on success, -errno in error cases.
|
1226 | f08145fe | Kevin Wolf | */
|
1227 | f08145fe | Kevin Wolf | int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
|
1228 | f08145fe | Kevin Wolf | const void *buf, int count) |
1229 | f08145fe | Kevin Wolf | { |
1230 | f08145fe | Kevin Wolf | int ret;
|
1231 | f08145fe | Kevin Wolf | |
1232 | f08145fe | Kevin Wolf | ret = bdrv_pwrite(bs, offset, buf, count); |
1233 | f08145fe | Kevin Wolf | if (ret < 0) { |
1234 | f08145fe | Kevin Wolf | return ret;
|
1235 | f08145fe | Kevin Wolf | } |
1236 | f08145fe | Kevin Wolf | |
1237 | 92196b2f | Stefan Hajnoczi | /* No flush needed for cache modes that use O_DSYNC */
|
1238 | 92196b2f | Stefan Hajnoczi | if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { |
1239 | f08145fe | Kevin Wolf | bdrv_flush(bs); |
1240 | f08145fe | Kevin Wolf | } |
1241 | f08145fe | Kevin Wolf | |
1242 | f08145fe | Kevin Wolf | return 0; |
1243 | f08145fe | Kevin Wolf | } |
1244 | f08145fe | Kevin Wolf | |
1245 | c5fbe571 | Stefan Hajnoczi | /*
|
1246 | c5fbe571 | Stefan Hajnoczi | * Handle a read request in coroutine context
|
1247 | c5fbe571 | Stefan Hajnoczi | */
|
1248 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, |
1249 | c5fbe571 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
1250 | da1fa91d | Kevin Wolf | { |
1251 | da1fa91d | Kevin Wolf | BlockDriver *drv = bs->drv; |
1252 | da1fa91d | Kevin Wolf | |
1253 | da1fa91d | Kevin Wolf | if (!drv) {
|
1254 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
1255 | da1fa91d | Kevin Wolf | } |
1256 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
1257 | da1fa91d | Kevin Wolf | return -EIO;
|
1258 | da1fa91d | Kevin Wolf | } |
1259 | da1fa91d | Kevin Wolf | |
1260 | da1fa91d | Kevin Wolf | return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
|
1261 | da1fa91d | Kevin Wolf | } |
1262 | da1fa91d | Kevin Wolf | |
1263 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
|
1264 | da1fa91d | Kevin Wolf | int nb_sectors, QEMUIOVector *qiov)
|
1265 | da1fa91d | Kevin Wolf | { |
1266 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_readv(bs, sector_num, nb_sectors); |
1267 | da1fa91d | Kevin Wolf | |
1268 | c5fbe571 | Stefan Hajnoczi | return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov);
|
1269 | c5fbe571 | Stefan Hajnoczi | } |
1270 | c5fbe571 | Stefan Hajnoczi | |
1271 | c5fbe571 | Stefan Hajnoczi | /*
|
1272 | c5fbe571 | Stefan Hajnoczi | * Handle a write request in coroutine context
|
1273 | c5fbe571 | Stefan Hajnoczi | */
|
1274 | c5fbe571 | Stefan Hajnoczi | static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, |
1275 | c5fbe571 | Stefan Hajnoczi | int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
|
1276 | c5fbe571 | Stefan Hajnoczi | { |
1277 | c5fbe571 | Stefan Hajnoczi | BlockDriver *drv = bs->drv; |
1278 | 6b7cb247 | Stefan Hajnoczi | int ret;
|
1279 | da1fa91d | Kevin Wolf | |
1280 | da1fa91d | Kevin Wolf | if (!bs->drv) {
|
1281 | da1fa91d | Kevin Wolf | return -ENOMEDIUM;
|
1282 | da1fa91d | Kevin Wolf | } |
1283 | da1fa91d | Kevin Wolf | if (bs->read_only) {
|
1284 | da1fa91d | Kevin Wolf | return -EACCES;
|
1285 | da1fa91d | Kevin Wolf | } |
1286 | da1fa91d | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors)) {
|
1287 | da1fa91d | Kevin Wolf | return -EIO;
|
1288 | da1fa91d | Kevin Wolf | } |
1289 | da1fa91d | Kevin Wolf | |
1290 | 6b7cb247 | Stefan Hajnoczi | ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); |
1291 | 6b7cb247 | Stefan Hajnoczi | |
1292 | da1fa91d | Kevin Wolf | if (bs->dirty_bitmap) {
|
1293 | da1fa91d | Kevin Wolf | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
1294 | da1fa91d | Kevin Wolf | } |
1295 | da1fa91d | Kevin Wolf | |
1296 | da1fa91d | Kevin Wolf | if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { |
1297 | da1fa91d | Kevin Wolf | bs->wr_highest_sector = sector_num + nb_sectors - 1;
|
1298 | da1fa91d | Kevin Wolf | } |
1299 | da1fa91d | Kevin Wolf | |
1300 | 6b7cb247 | Stefan Hajnoczi | return ret;
|
1301 | da1fa91d | Kevin Wolf | } |
1302 | da1fa91d | Kevin Wolf | |
1303 | c5fbe571 | Stefan Hajnoczi | int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
|
1304 | c5fbe571 | Stefan Hajnoczi | int nb_sectors, QEMUIOVector *qiov)
|
1305 | c5fbe571 | Stefan Hajnoczi | { |
1306 | c5fbe571 | Stefan Hajnoczi | trace_bdrv_co_writev(bs, sector_num, nb_sectors); |
1307 | c5fbe571 | Stefan Hajnoczi | |
1308 | c5fbe571 | Stefan Hajnoczi | return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov);
|
1309 | c5fbe571 | Stefan Hajnoczi | } |
1310 | c5fbe571 | Stefan Hajnoczi | |
1311 | 83f64091 | bellard | /**
|
1312 | 83f64091 | bellard | * Truncate file to 'offset' bytes (needed only for file protocols)
|
1313 | 83f64091 | bellard | */
|
1314 | 83f64091 | bellard | int bdrv_truncate(BlockDriverState *bs, int64_t offset)
|
1315 | 83f64091 | bellard | { |
1316 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1317 | 51762288 | Stefan Hajnoczi | int ret;
|
1318 | 83f64091 | bellard | if (!drv)
|
1319 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1320 | 83f64091 | bellard | if (!drv->bdrv_truncate)
|
1321 | 83f64091 | bellard | return -ENOTSUP;
|
1322 | 59f2689d | Naphtali Sprei | if (bs->read_only)
|
1323 | 59f2689d | Naphtali Sprei | return -EACCES;
|
1324 | 8591675f | Marcelo Tosatti | if (bdrv_in_use(bs))
|
1325 | 8591675f | Marcelo Tosatti | return -EBUSY;
|
1326 | 51762288 | Stefan Hajnoczi | ret = drv->bdrv_truncate(bs, offset); |
1327 | 51762288 | Stefan Hajnoczi | if (ret == 0) { |
1328 | 51762288 | Stefan Hajnoczi | ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); |
1329 | 145feb17 | Markus Armbruster | bdrv_dev_resize_cb(bs); |
1330 | 51762288 | Stefan Hajnoczi | } |
1331 | 51762288 | Stefan Hajnoczi | return ret;
|
1332 | 83f64091 | bellard | } |
1333 | 83f64091 | bellard | |
1334 | 83f64091 | bellard | /**
|
1335 | 4a1d5e1f | Fam Zheng | * Length of a allocated file in bytes. Sparse files are counted by actual
|
1336 | 4a1d5e1f | Fam Zheng | * allocated space. Return < 0 if error or unknown.
|
1337 | 4a1d5e1f | Fam Zheng | */
|
1338 | 4a1d5e1f | Fam Zheng | int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) |
1339 | 4a1d5e1f | Fam Zheng | { |
1340 | 4a1d5e1f | Fam Zheng | BlockDriver *drv = bs->drv; |
1341 | 4a1d5e1f | Fam Zheng | if (!drv) {
|
1342 | 4a1d5e1f | Fam Zheng | return -ENOMEDIUM;
|
1343 | 4a1d5e1f | Fam Zheng | } |
1344 | 4a1d5e1f | Fam Zheng | if (drv->bdrv_get_allocated_file_size) {
|
1345 | 4a1d5e1f | Fam Zheng | return drv->bdrv_get_allocated_file_size(bs);
|
1346 | 4a1d5e1f | Fam Zheng | } |
1347 | 4a1d5e1f | Fam Zheng | if (bs->file) {
|
1348 | 4a1d5e1f | Fam Zheng | return bdrv_get_allocated_file_size(bs->file);
|
1349 | 4a1d5e1f | Fam Zheng | } |
1350 | 4a1d5e1f | Fam Zheng | return -ENOTSUP;
|
1351 | 4a1d5e1f | Fam Zheng | } |
1352 | 4a1d5e1f | Fam Zheng | |
1353 | 4a1d5e1f | Fam Zheng | /**
|
1354 | 83f64091 | bellard | * Length of a file in bytes. Return < 0 if error or unknown.
|
1355 | 83f64091 | bellard | */
|
1356 | 83f64091 | bellard | int64_t bdrv_getlength(BlockDriverState *bs) |
1357 | 83f64091 | bellard | { |
1358 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1359 | 83f64091 | bellard | if (!drv)
|
1360 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1361 | 51762288 | Stefan Hajnoczi | |
1362 | 2c6942fa | Markus Armbruster | if (bs->growable || bdrv_dev_has_removable_media(bs)) {
|
1363 | 46a4e4e6 | Stefan Hajnoczi | if (drv->bdrv_getlength) {
|
1364 | 46a4e4e6 | Stefan Hajnoczi | return drv->bdrv_getlength(bs);
|
1365 | 46a4e4e6 | Stefan Hajnoczi | } |
1366 | 83f64091 | bellard | } |
1367 | 46a4e4e6 | Stefan Hajnoczi | return bs->total_sectors * BDRV_SECTOR_SIZE;
|
1368 | fc01f7e7 | bellard | } |
1369 | fc01f7e7 | bellard | |
1370 | 19cb3738 | bellard | /* return 0 as number of sectors if no device present or error */
|
1371 | 96b8f136 | ths | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
|
1372 | fc01f7e7 | bellard | { |
1373 | 19cb3738 | bellard | int64_t length; |
1374 | 19cb3738 | bellard | length = bdrv_getlength(bs); |
1375 | 19cb3738 | bellard | if (length < 0) |
1376 | 19cb3738 | bellard | length = 0;
|
1377 | 19cb3738 | bellard | else
|
1378 | 6ea44308 | Jan Kiszka | length = length >> BDRV_SECTOR_BITS; |
1379 | 19cb3738 | bellard | *nb_sectors_ptr = length; |
1380 | fc01f7e7 | bellard | } |
1381 | cf98951b | bellard | |
1382 | f3d54fc4 | aliguori | struct partition {
|
1383 | f3d54fc4 | aliguori | uint8_t boot_ind; /* 0x80 - active */
|
1384 | f3d54fc4 | aliguori | uint8_t head; /* starting head */
|
1385 | f3d54fc4 | aliguori | uint8_t sector; /* starting sector */
|
1386 | f3d54fc4 | aliguori | uint8_t cyl; /* starting cylinder */
|
1387 | f3d54fc4 | aliguori | uint8_t sys_ind; /* What partition type */
|
1388 | f3d54fc4 | aliguori | uint8_t end_head; /* end head */
|
1389 | f3d54fc4 | aliguori | uint8_t end_sector; /* end sector */
|
1390 | f3d54fc4 | aliguori | uint8_t end_cyl; /* end cylinder */
|
1391 | f3d54fc4 | aliguori | uint32_t start_sect; /* starting sector counting from 0 */
|
1392 | f3d54fc4 | aliguori | uint32_t nr_sects; /* nr of sectors in partition */
|
1393 | 541dc0d4 | Stefan Weil | } QEMU_PACKED; |
1394 | f3d54fc4 | aliguori | |
1395 | f3d54fc4 | aliguori | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
|
1396 | f3d54fc4 | aliguori | static int guess_disk_lchs(BlockDriverState *bs, |
1397 | f3d54fc4 | aliguori | int *pcylinders, int *pheads, int *psectors) |
1398 | f3d54fc4 | aliguori | { |
1399 | eb5a3165 | Jes Sorensen | uint8_t buf[BDRV_SECTOR_SIZE]; |
1400 | f3d54fc4 | aliguori | int ret, i, heads, sectors, cylinders;
|
1401 | f3d54fc4 | aliguori | struct partition *p;
|
1402 | f3d54fc4 | aliguori | uint32_t nr_sects; |
1403 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
1404 | f3d54fc4 | aliguori | |
1405 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
1406 | f3d54fc4 | aliguori | |
1407 | f3d54fc4 | aliguori | ret = bdrv_read(bs, 0, buf, 1); |
1408 | f3d54fc4 | aliguori | if (ret < 0) |
1409 | f3d54fc4 | aliguori | return -1; |
1410 | f3d54fc4 | aliguori | /* test msdos magic */
|
1411 | f3d54fc4 | aliguori | if (buf[510] != 0x55 || buf[511] != 0xaa) |
1412 | f3d54fc4 | aliguori | return -1; |
1413 | f3d54fc4 | aliguori | for(i = 0; i < 4; i++) { |
1414 | f3d54fc4 | aliguori | p = ((struct partition *)(buf + 0x1be)) + i; |
1415 | f3d54fc4 | aliguori | nr_sects = le32_to_cpu(p->nr_sects); |
1416 | f3d54fc4 | aliguori | if (nr_sects && p->end_head) {
|
1417 | f3d54fc4 | aliguori | /* We make the assumption that the partition terminates on
|
1418 | f3d54fc4 | aliguori | a cylinder boundary */
|
1419 | f3d54fc4 | aliguori | heads = p->end_head + 1;
|
1420 | f3d54fc4 | aliguori | sectors = p->end_sector & 63;
|
1421 | f3d54fc4 | aliguori | if (sectors == 0) |
1422 | f3d54fc4 | aliguori | continue;
|
1423 | f3d54fc4 | aliguori | cylinders = nb_sectors / (heads * sectors); |
1424 | f3d54fc4 | aliguori | if (cylinders < 1 || cylinders > 16383) |
1425 | f3d54fc4 | aliguori | continue;
|
1426 | f3d54fc4 | aliguori | *pheads = heads; |
1427 | f3d54fc4 | aliguori | *psectors = sectors; |
1428 | f3d54fc4 | aliguori | *pcylinders = cylinders; |
1429 | f3d54fc4 | aliguori | #if 0
|
1430 | f3d54fc4 | aliguori | printf("guessed geometry: LCHS=%d %d %d\n",
|
1431 | f3d54fc4 | aliguori | cylinders, heads, sectors);
|
1432 | f3d54fc4 | aliguori | #endif
|
1433 | f3d54fc4 | aliguori | return 0; |
1434 | f3d54fc4 | aliguori | } |
1435 | f3d54fc4 | aliguori | } |
1436 | f3d54fc4 | aliguori | return -1; |
1437 | f3d54fc4 | aliguori | } |
1438 | f3d54fc4 | aliguori | |
1439 | f3d54fc4 | aliguori | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
1440 | f3d54fc4 | aliguori | { |
1441 | f3d54fc4 | aliguori | int translation, lba_detected = 0; |
1442 | f3d54fc4 | aliguori | int cylinders, heads, secs;
|
1443 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
1444 | f3d54fc4 | aliguori | |
1445 | f3d54fc4 | aliguori | /* if a geometry hint is available, use it */
|
1446 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
1447 | f3d54fc4 | aliguori | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
1448 | f3d54fc4 | aliguori | translation = bdrv_get_translation_hint(bs); |
1449 | f3d54fc4 | aliguori | if (cylinders != 0) { |
1450 | f3d54fc4 | aliguori | *pcyls = cylinders; |
1451 | f3d54fc4 | aliguori | *pheads = heads; |
1452 | f3d54fc4 | aliguori | *psecs = secs; |
1453 | f3d54fc4 | aliguori | } else {
|
1454 | f3d54fc4 | aliguori | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
1455 | f3d54fc4 | aliguori | if (heads > 16) { |
1456 | f3d54fc4 | aliguori | /* if heads > 16, it means that a BIOS LBA
|
1457 | f3d54fc4 | aliguori | translation was active, so the default
|
1458 | f3d54fc4 | aliguori | hardware geometry is OK */
|
1459 | f3d54fc4 | aliguori | lba_detected = 1;
|
1460 | f3d54fc4 | aliguori | goto default_geometry;
|
1461 | f3d54fc4 | aliguori | } else {
|
1462 | f3d54fc4 | aliguori | *pcyls = cylinders; |
1463 | f3d54fc4 | aliguori | *pheads = heads; |
1464 | f3d54fc4 | aliguori | *psecs = secs; |
1465 | f3d54fc4 | aliguori | /* disable any translation to be in sync with
|
1466 | f3d54fc4 | aliguori | the logical geometry */
|
1467 | f3d54fc4 | aliguori | if (translation == BIOS_ATA_TRANSLATION_AUTO) {
|
1468 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
1469 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_NONE); |
1470 | f3d54fc4 | aliguori | } |
1471 | f3d54fc4 | aliguori | } |
1472 | f3d54fc4 | aliguori | } else {
|
1473 | f3d54fc4 | aliguori | default_geometry:
|
1474 | f3d54fc4 | aliguori | /* if no geometry, use a standard physical disk geometry */
|
1475 | f3d54fc4 | aliguori | cylinders = nb_sectors / (16 * 63); |
1476 | f3d54fc4 | aliguori | |
1477 | f3d54fc4 | aliguori | if (cylinders > 16383) |
1478 | f3d54fc4 | aliguori | cylinders = 16383;
|
1479 | f3d54fc4 | aliguori | else if (cylinders < 2) |
1480 | f3d54fc4 | aliguori | cylinders = 2;
|
1481 | f3d54fc4 | aliguori | *pcyls = cylinders; |
1482 | f3d54fc4 | aliguori | *pheads = 16;
|
1483 | f3d54fc4 | aliguori | *psecs = 63;
|
1484 | f3d54fc4 | aliguori | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
1485 | f3d54fc4 | aliguori | if ((*pcyls * *pheads) <= 131072) { |
1486 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
1487 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LARGE); |
1488 | f3d54fc4 | aliguori | } else {
|
1489 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
1490 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LBA); |
1491 | f3d54fc4 | aliguori | } |
1492 | f3d54fc4 | aliguori | } |
1493 | f3d54fc4 | aliguori | } |
1494 | f3d54fc4 | aliguori | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
1495 | f3d54fc4 | aliguori | } |
1496 | f3d54fc4 | aliguori | } |
1497 | f3d54fc4 | aliguori | |
1498 | 5fafdf24 | ths | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
1499 | b338082b | bellard | int cyls, int heads, int secs) |
1500 | b338082b | bellard | { |
1501 | b338082b | bellard | bs->cyls = cyls; |
1502 | b338082b | bellard | bs->heads = heads; |
1503 | b338082b | bellard | bs->secs = secs; |
1504 | b338082b | bellard | } |
1505 | b338082b | bellard | |
1506 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
1507 | 46d4767d | bellard | { |
1508 | 46d4767d | bellard | bs->translation = translation; |
1509 | 46d4767d | bellard | } |
1510 | 46d4767d | bellard | |
1511 | 5fafdf24 | ths | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
1512 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
1513 | b338082b | bellard | { |
1514 | b338082b | bellard | *pcyls = bs->cyls; |
1515 | b338082b | bellard | *pheads = bs->heads; |
1516 | b338082b | bellard | *psecs = bs->secs; |
1517 | b338082b | bellard | } |
1518 | b338082b | bellard | |
1519 | 5bbdbb46 | Blue Swirl | /* Recognize floppy formats */
|
1520 | 5bbdbb46 | Blue Swirl | typedef struct FDFormat { |
1521 | 5bbdbb46 | Blue Swirl | FDriveType drive; |
1522 | 5bbdbb46 | Blue Swirl | uint8_t last_sect; |
1523 | 5bbdbb46 | Blue Swirl | uint8_t max_track; |
1524 | 5bbdbb46 | Blue Swirl | uint8_t max_head; |
1525 | 5bbdbb46 | Blue Swirl | } FDFormat; |
1526 | 5bbdbb46 | Blue Swirl | |
1527 | 5bbdbb46 | Blue Swirl | static const FDFormat fd_formats[] = { |
1528 | 5bbdbb46 | Blue Swirl | /* First entry is default format */
|
1529 | 5bbdbb46 | Blue Swirl | /* 1.44 MB 3"1/2 floppy disks */
|
1530 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 18, 80, 1, }, |
1531 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 20, 80, 1, }, |
1532 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 21, 80, 1, }, |
1533 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 21, 82, 1, }, |
1534 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 21, 83, 1, }, |
1535 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 22, 80, 1, }, |
1536 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 23, 80, 1, }, |
1537 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 24, 80, 1, }, |
1538 | 5bbdbb46 | Blue Swirl | /* 2.88 MB 3"1/2 floppy disks */
|
1539 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 36, 80, 1, }, |
1540 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 39, 80, 1, }, |
1541 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 40, 80, 1, }, |
1542 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 44, 80, 1, }, |
1543 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_288, 48, 80, 1, }, |
1544 | 5bbdbb46 | Blue Swirl | /* 720 kB 3"1/2 floppy disks */
|
1545 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 9, 80, 1, }, |
1546 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 10, 80, 1, }, |
1547 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 10, 82, 1, }, |
1548 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 10, 83, 1, }, |
1549 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 13, 80, 1, }, |
1550 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 14, 80, 1, }, |
1551 | 5bbdbb46 | Blue Swirl | /* 1.2 MB 5"1/4 floppy disks */
|
1552 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 15, 80, 1, }, |
1553 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 18, 80, 1, }, |
1554 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 18, 82, 1, }, |
1555 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 18, 83, 1, }, |
1556 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 20, 80, 1, }, |
1557 | 5bbdbb46 | Blue Swirl | /* 720 kB 5"1/4 floppy disks */
|
1558 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 9, 80, 1, }, |
1559 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 11, 80, 1, }, |
1560 | 5bbdbb46 | Blue Swirl | /* 360 kB 5"1/4 floppy disks */
|
1561 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 9, 40, 1, }, |
1562 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 9, 40, 0, }, |
1563 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 10, 41, 1, }, |
1564 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 10, 42, 1, }, |
1565 | 5bbdbb46 | Blue Swirl | /* 320 kB 5"1/4 floppy disks */
|
1566 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 8, 40, 1, }, |
1567 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_120, 8, 40, 0, }, |
1568 | 5bbdbb46 | Blue Swirl | /* 360 kB must match 5"1/4 better than 3"1/2... */
|
1569 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_144, 9, 80, 0, }, |
1570 | 5bbdbb46 | Blue Swirl | /* end */
|
1571 | 5bbdbb46 | Blue Swirl | { FDRIVE_DRV_NONE, -1, -1, 0, }, |
1572 | 5bbdbb46 | Blue Swirl | }; |
1573 | 5bbdbb46 | Blue Swirl | |
1574 | 5bbdbb46 | Blue Swirl | void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, |
1575 | 5bbdbb46 | Blue Swirl | int *max_track, int *last_sect, |
1576 | 5bbdbb46 | Blue Swirl | FDriveType drive_in, FDriveType *drive) |
1577 | 5bbdbb46 | Blue Swirl | { |
1578 | 5bbdbb46 | Blue Swirl | const FDFormat *parse;
|
1579 | 5bbdbb46 | Blue Swirl | uint64_t nb_sectors, size; |
1580 | 5bbdbb46 | Blue Swirl | int i, first_match, match;
|
1581 | 5bbdbb46 | Blue Swirl | |
1582 | 5bbdbb46 | Blue Swirl | bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); |
1583 | 5bbdbb46 | Blue Swirl | if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { |
1584 | 5bbdbb46 | Blue Swirl | /* User defined disk */
|
1585 | 5bbdbb46 | Blue Swirl | } else {
|
1586 | 5bbdbb46 | Blue Swirl | bdrv_get_geometry(bs, &nb_sectors); |
1587 | 5bbdbb46 | Blue Swirl | match = -1;
|
1588 | 5bbdbb46 | Blue Swirl | first_match = -1;
|
1589 | 5bbdbb46 | Blue Swirl | for (i = 0; ; i++) { |
1590 | 5bbdbb46 | Blue Swirl | parse = &fd_formats[i]; |
1591 | 5bbdbb46 | Blue Swirl | if (parse->drive == FDRIVE_DRV_NONE) {
|
1592 | 5bbdbb46 | Blue Swirl | break;
|
1593 | 5bbdbb46 | Blue Swirl | } |
1594 | 5bbdbb46 | Blue Swirl | if (drive_in == parse->drive ||
|
1595 | 5bbdbb46 | Blue Swirl | drive_in == FDRIVE_DRV_NONE) { |
1596 | 5bbdbb46 | Blue Swirl | size = (parse->max_head + 1) * parse->max_track *
|
1597 | 5bbdbb46 | Blue Swirl | parse->last_sect; |
1598 | 5bbdbb46 | Blue Swirl | if (nb_sectors == size) {
|
1599 | 5bbdbb46 | Blue Swirl | match = i; |
1600 | 5bbdbb46 | Blue Swirl | break;
|
1601 | 5bbdbb46 | Blue Swirl | } |
1602 | 5bbdbb46 | Blue Swirl | if (first_match == -1) { |
1603 | 5bbdbb46 | Blue Swirl | first_match = i; |
1604 | 5bbdbb46 | Blue Swirl | } |
1605 | 5bbdbb46 | Blue Swirl | } |
1606 | 5bbdbb46 | Blue Swirl | } |
1607 | 5bbdbb46 | Blue Swirl | if (match == -1) { |
1608 | 5bbdbb46 | Blue Swirl | if (first_match == -1) { |
1609 | 5bbdbb46 | Blue Swirl | match = 1;
|
1610 | 5bbdbb46 | Blue Swirl | } else {
|
1611 | 5bbdbb46 | Blue Swirl | match = first_match; |
1612 | 5bbdbb46 | Blue Swirl | } |
1613 | 5bbdbb46 | Blue Swirl | parse = &fd_formats[match]; |
1614 | 5bbdbb46 | Blue Swirl | } |
1615 | 5bbdbb46 | Blue Swirl | *nb_heads = parse->max_head + 1;
|
1616 | 5bbdbb46 | Blue Swirl | *max_track = parse->max_track; |
1617 | 5bbdbb46 | Blue Swirl | *last_sect = parse->last_sect; |
1618 | 5bbdbb46 | Blue Swirl | *drive = parse->drive; |
1619 | 5bbdbb46 | Blue Swirl | } |
1620 | 5bbdbb46 | Blue Swirl | } |
1621 | 5bbdbb46 | Blue Swirl | |
1622 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
1623 | 46d4767d | bellard | { |
1624 | 46d4767d | bellard | return bs->translation;
|
1625 | 46d4767d | bellard | } |
1626 | 46d4767d | bellard | |
1627 | abd7f68d | Markus Armbruster | void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
|
1628 | abd7f68d | Markus Armbruster | BlockErrorAction on_write_error) |
1629 | abd7f68d | Markus Armbruster | { |
1630 | abd7f68d | Markus Armbruster | bs->on_read_error = on_read_error; |
1631 | abd7f68d | Markus Armbruster | bs->on_write_error = on_write_error; |
1632 | abd7f68d | Markus Armbruster | } |
1633 | abd7f68d | Markus Armbruster | |
1634 | abd7f68d | Markus Armbruster | BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
|
1635 | abd7f68d | Markus Armbruster | { |
1636 | abd7f68d | Markus Armbruster | return is_read ? bs->on_read_error : bs->on_write_error;
|
1637 | abd7f68d | Markus Armbruster | } |
1638 | abd7f68d | Markus Armbruster | |
1639 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
1640 | b338082b | bellard | { |
1641 | b338082b | bellard | return bs->read_only;
|
1642 | b338082b | bellard | } |
1643 | b338082b | bellard | |
1644 | 985a03b0 | ths | int bdrv_is_sg(BlockDriverState *bs)
|
1645 | 985a03b0 | ths | { |
1646 | 985a03b0 | ths | return bs->sg;
|
1647 | 985a03b0 | ths | } |
1648 | 985a03b0 | ths | |
1649 | e900a7b7 | Christoph Hellwig | int bdrv_enable_write_cache(BlockDriverState *bs)
|
1650 | e900a7b7 | Christoph Hellwig | { |
1651 | e900a7b7 | Christoph Hellwig | return bs->enable_write_cache;
|
1652 | e900a7b7 | Christoph Hellwig | } |
1653 | e900a7b7 | Christoph Hellwig | |
1654 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
1655 | ea2384d3 | bellard | { |
1656 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
1657 | ea2384d3 | bellard | return 1; |
1658 | ea2384d3 | bellard | return bs->encrypted;
|
1659 | ea2384d3 | bellard | } |
1660 | ea2384d3 | bellard | |
1661 | c0f4ce77 | aliguori | int bdrv_key_required(BlockDriverState *bs)
|
1662 | c0f4ce77 | aliguori | { |
1663 | c0f4ce77 | aliguori | BlockDriverState *backing_hd = bs->backing_hd; |
1664 | c0f4ce77 | aliguori | |
1665 | c0f4ce77 | aliguori | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
|
1666 | c0f4ce77 | aliguori | return 1; |
1667 | c0f4ce77 | aliguori | return (bs->encrypted && !bs->valid_key);
|
1668 | c0f4ce77 | aliguori | } |
1669 | c0f4ce77 | aliguori | |
1670 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
1671 | ea2384d3 | bellard | { |
1672 | ea2384d3 | bellard | int ret;
|
1673 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
1674 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
1675 | ea2384d3 | bellard | if (ret < 0) |
1676 | ea2384d3 | bellard | return ret;
|
1677 | ea2384d3 | bellard | if (!bs->encrypted)
|
1678 | ea2384d3 | bellard | return 0; |
1679 | ea2384d3 | bellard | } |
1680 | fd04a2ae | Shahar Havivi | if (!bs->encrypted) {
|
1681 | fd04a2ae | Shahar Havivi | return -EINVAL;
|
1682 | fd04a2ae | Shahar Havivi | } else if (!bs->drv || !bs->drv->bdrv_set_key) { |
1683 | fd04a2ae | Shahar Havivi | return -ENOMEDIUM;
|
1684 | fd04a2ae | Shahar Havivi | } |
1685 | c0f4ce77 | aliguori | ret = bs->drv->bdrv_set_key(bs, key); |
1686 | bb5fc20f | aliguori | if (ret < 0) { |
1687 | bb5fc20f | aliguori | bs->valid_key = 0;
|
1688 | bb5fc20f | aliguori | } else if (!bs->valid_key) { |
1689 | bb5fc20f | aliguori | bs->valid_key = 1;
|
1690 | bb5fc20f | aliguori | /* call the change callback now, we skipped it on open */
|
1691 | 7d4b4ba5 | Markus Armbruster | bdrv_dev_change_media_cb(bs, true);
|
1692 | bb5fc20f | aliguori | } |
1693 | c0f4ce77 | aliguori | return ret;
|
1694 | ea2384d3 | bellard | } |
1695 | ea2384d3 | bellard | |
1696 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
1697 | ea2384d3 | bellard | { |
1698 | 19cb3738 | bellard | if (!bs->drv) {
|
1699 | ea2384d3 | bellard | buf[0] = '\0'; |
1700 | ea2384d3 | bellard | } else {
|
1701 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
1702 | ea2384d3 | bellard | } |
1703 | ea2384d3 | bellard | } |
1704 | ea2384d3 | bellard | |
1705 | 5fafdf24 | ths | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
1706 | ea2384d3 | bellard | void *opaque)
|
1707 | ea2384d3 | bellard | { |
1708 | ea2384d3 | bellard | BlockDriver *drv; |
1709 | ea2384d3 | bellard | |
1710 | 8a22f02a | Stefan Hajnoczi | QLIST_FOREACH(drv, &bdrv_drivers, list) { |
1711 | ea2384d3 | bellard | it(opaque, drv->format_name); |
1712 | ea2384d3 | bellard | } |
1713 | ea2384d3 | bellard | } |
1714 | ea2384d3 | bellard | |
1715 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
1716 | b338082b | bellard | { |
1717 | b338082b | bellard | BlockDriverState *bs; |
1718 | b338082b | bellard | |
1719 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1720 | 1b7bdbc1 | Stefan Hajnoczi | if (!strcmp(name, bs->device_name)) {
|
1721 | b338082b | bellard | return bs;
|
1722 | 1b7bdbc1 | Stefan Hajnoczi | } |
1723 | b338082b | bellard | } |
1724 | b338082b | bellard | return NULL; |
1725 | b338082b | bellard | } |
1726 | b338082b | bellard | |
1727 | 2f399b0a | Markus Armbruster | BlockDriverState *bdrv_next(BlockDriverState *bs) |
1728 | 2f399b0a | Markus Armbruster | { |
1729 | 2f399b0a | Markus Armbruster | if (!bs) {
|
1730 | 2f399b0a | Markus Armbruster | return QTAILQ_FIRST(&bdrv_states);
|
1731 | 2f399b0a | Markus Armbruster | } |
1732 | 2f399b0a | Markus Armbruster | return QTAILQ_NEXT(bs, list);
|
1733 | 2f399b0a | Markus Armbruster | } |
1734 | 2f399b0a | Markus Armbruster | |
1735 | 51de9760 | aliguori | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
1736 | 81d0912d | bellard | { |
1737 | 81d0912d | bellard | BlockDriverState *bs; |
1738 | 81d0912d | bellard | |
1739 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1740 | 51de9760 | aliguori | it(opaque, bs); |
1741 | 81d0912d | bellard | } |
1742 | 81d0912d | bellard | } |
1743 | 81d0912d | bellard | |
1744 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
1745 | ea2384d3 | bellard | { |
1746 | ea2384d3 | bellard | return bs->device_name;
|
1747 | ea2384d3 | bellard | } |
1748 | ea2384d3 | bellard | |
1749 | c6ca28d6 | aliguori | void bdrv_flush_all(void) |
1750 | c6ca28d6 | aliguori | { |
1751 | c6ca28d6 | aliguori | BlockDriverState *bs; |
1752 | c6ca28d6 | aliguori | |
1753 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1754 | c602a489 | Markus Armbruster | if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
|
1755 | c6ca28d6 | aliguori | bdrv_flush(bs); |
1756 | 1b7bdbc1 | Stefan Hajnoczi | } |
1757 | 1b7bdbc1 | Stefan Hajnoczi | } |
1758 | c6ca28d6 | aliguori | } |
1759 | c6ca28d6 | aliguori | |
1760 | f2feebbd | Kevin Wolf | int bdrv_has_zero_init(BlockDriverState *bs)
|
1761 | f2feebbd | Kevin Wolf | { |
1762 | f2feebbd | Kevin Wolf | assert(bs->drv); |
1763 | f2feebbd | Kevin Wolf | |
1764 | 336c1c12 | Kevin Wolf | if (bs->drv->bdrv_has_zero_init) {
|
1765 | 336c1c12 | Kevin Wolf | return bs->drv->bdrv_has_zero_init(bs);
|
1766 | f2feebbd | Kevin Wolf | } |
1767 | f2feebbd | Kevin Wolf | |
1768 | f2feebbd | Kevin Wolf | return 1; |
1769 | f2feebbd | Kevin Wolf | } |
1770 | f2feebbd | Kevin Wolf | |
1771 | bb8bf76f | Christoph Hellwig | int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) |
1772 | bb8bf76f | Christoph Hellwig | { |
1773 | bb8bf76f | Christoph Hellwig | if (!bs->drv) {
|
1774 | bb8bf76f | Christoph Hellwig | return -ENOMEDIUM;
|
1775 | bb8bf76f | Christoph Hellwig | } |
1776 | bb8bf76f | Christoph Hellwig | if (!bs->drv->bdrv_discard) {
|
1777 | bb8bf76f | Christoph Hellwig | return 0; |
1778 | bb8bf76f | Christoph Hellwig | } |
1779 | bb8bf76f | Christoph Hellwig | return bs->drv->bdrv_discard(bs, sector_num, nb_sectors);
|
1780 | bb8bf76f | Christoph Hellwig | } |
1781 | bb8bf76f | Christoph Hellwig | |
1782 | f58c7b35 | ths | /*
|
1783 | f58c7b35 | ths | * Returns true iff the specified sector is present in the disk image. Drivers
|
1784 | f58c7b35 | ths | * not implementing the functionality are assumed to not support backing files,
|
1785 | f58c7b35 | ths | * hence all their sectors are reported as allocated.
|
1786 | f58c7b35 | ths | *
|
1787 | f58c7b35 | ths | * 'pnum' is set to the number of sectors (including and immediately following
|
1788 | f58c7b35 | ths | * the specified sector) that are known to be in the same
|
1789 | f58c7b35 | ths | * allocated/unallocated state.
|
1790 | f58c7b35 | ths | *
|
1791 | f58c7b35 | ths | * 'nb_sectors' is the max value 'pnum' should be set to.
|
1792 | f58c7b35 | ths | */
|
1793 | f58c7b35 | ths | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
1794 | f58c7b35 | ths | int *pnum)
|
1795 | f58c7b35 | ths | { |
1796 | f58c7b35 | ths | int64_t n; |
1797 | f58c7b35 | ths | if (!bs->drv->bdrv_is_allocated) {
|
1798 | f58c7b35 | ths | if (sector_num >= bs->total_sectors) {
|
1799 | f58c7b35 | ths | *pnum = 0;
|
1800 | f58c7b35 | ths | return 0; |
1801 | f58c7b35 | ths | } |
1802 | f58c7b35 | ths | n = bs->total_sectors - sector_num; |
1803 | f58c7b35 | ths | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
1804 | f58c7b35 | ths | return 1; |
1805 | f58c7b35 | ths | } |
1806 | f58c7b35 | ths | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
|
1807 | f58c7b35 | ths | } |
1808 | f58c7b35 | ths | |
1809 | 2582bfed | Luiz Capitulino | void bdrv_mon_event(const BlockDriverState *bdrv, |
1810 | 2582bfed | Luiz Capitulino | BlockMonEventAction action, int is_read)
|
1811 | 2582bfed | Luiz Capitulino | { |
1812 | 2582bfed | Luiz Capitulino | QObject *data; |
1813 | 2582bfed | Luiz Capitulino | const char *action_str; |
1814 | 2582bfed | Luiz Capitulino | |
1815 | 2582bfed | Luiz Capitulino | switch (action) {
|
1816 | 2582bfed | Luiz Capitulino | case BDRV_ACTION_REPORT:
|
1817 | 2582bfed | Luiz Capitulino | action_str = "report";
|
1818 | 2582bfed | Luiz Capitulino | break;
|
1819 | 2582bfed | Luiz Capitulino | case BDRV_ACTION_IGNORE:
|
1820 | 2582bfed | Luiz Capitulino | action_str = "ignore";
|
1821 | 2582bfed | Luiz Capitulino | break;
|
1822 | 2582bfed | Luiz Capitulino | case BDRV_ACTION_STOP:
|
1823 | 2582bfed | Luiz Capitulino | action_str = "stop";
|
1824 | 2582bfed | Luiz Capitulino | break;
|
1825 | 2582bfed | Luiz Capitulino | default:
|
1826 | 2582bfed | Luiz Capitulino | abort(); |
1827 | 2582bfed | Luiz Capitulino | } |
1828 | 2582bfed | Luiz Capitulino | |
1829 | 2582bfed | Luiz Capitulino | data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
|
1830 | 2582bfed | Luiz Capitulino | bdrv->device_name, |
1831 | 2582bfed | Luiz Capitulino | action_str, |
1832 | 2582bfed | Luiz Capitulino | is_read ? "read" : "write"); |
1833 | 2582bfed | Luiz Capitulino | monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); |
1834 | 2582bfed | Luiz Capitulino | |
1835 | 2582bfed | Luiz Capitulino | qobject_decref(data); |
1836 | 2582bfed | Luiz Capitulino | } |
1837 | 2582bfed | Luiz Capitulino | |
1838 | d15e5465 | Luiz Capitulino | static void bdrv_print_dict(QObject *obj, void *opaque) |
1839 | b338082b | bellard | { |
1840 | d15e5465 | Luiz Capitulino | QDict *bs_dict; |
1841 | d15e5465 | Luiz Capitulino | Monitor *mon = opaque; |
1842 | d15e5465 | Luiz Capitulino | |
1843 | d15e5465 | Luiz Capitulino | bs_dict = qobject_to_qdict(obj); |
1844 | d15e5465 | Luiz Capitulino | |
1845 | d8aeeb31 | Markus Armbruster | monitor_printf(mon, "%s: removable=%d",
|
1846 | d15e5465 | Luiz Capitulino | qdict_get_str(bs_dict, "device"),
|
1847 | d15e5465 | Luiz Capitulino | qdict_get_bool(bs_dict, "removable"));
|
1848 | d15e5465 | Luiz Capitulino | |
1849 | d15e5465 | Luiz Capitulino | if (qdict_get_bool(bs_dict, "removable")) { |
1850 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); |
1851 | e4def80b | Markus Armbruster | monitor_printf(mon, " tray-open=%d",
|
1852 | e4def80b | Markus Armbruster | qdict_get_bool(bs_dict, "tray-open"));
|
1853 | d15e5465 | Luiz Capitulino | } |
1854 | d2078cc2 | Luiz Capitulino | |
1855 | d2078cc2 | Luiz Capitulino | if (qdict_haskey(bs_dict, "io-status")) { |
1856 | d2078cc2 | Luiz Capitulino | monitor_printf(mon, " io-status=%s", qdict_get_str(bs_dict, "io-status")); |
1857 | d2078cc2 | Luiz Capitulino | } |
1858 | d2078cc2 | Luiz Capitulino | |
1859 | d15e5465 | Luiz Capitulino | if (qdict_haskey(bs_dict, "inserted")) { |
1860 | d15e5465 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
|
1861 | d15e5465 | Luiz Capitulino | |
1862 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " file=");
|
1863 | d15e5465 | Luiz Capitulino | monitor_print_filename(mon, qdict_get_str(qdict, "file"));
|
1864 | d15e5465 | Luiz Capitulino | if (qdict_haskey(qdict, "backing_file")) { |
1865 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " backing_file=");
|
1866 | d15e5465 | Luiz Capitulino | monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
|
1867 | d15e5465 | Luiz Capitulino | } |
1868 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
|
1869 | d15e5465 | Luiz Capitulino | qdict_get_bool(qdict, "ro"),
|
1870 | d15e5465 | Luiz Capitulino | qdict_get_str(qdict, "drv"),
|
1871 | d15e5465 | Luiz Capitulino | qdict_get_bool(qdict, "encrypted"));
|
1872 | d15e5465 | Luiz Capitulino | } else {
|
1873 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " [not inserted]");
|
1874 | d15e5465 | Luiz Capitulino | } |
1875 | d15e5465 | Luiz Capitulino | |
1876 | d15e5465 | Luiz Capitulino | monitor_printf(mon, "\n");
|
1877 | d15e5465 | Luiz Capitulino | } |
1878 | d15e5465 | Luiz Capitulino | |
1879 | d15e5465 | Luiz Capitulino | void bdrv_info_print(Monitor *mon, const QObject *data) |
1880 | d15e5465 | Luiz Capitulino | { |
1881 | d15e5465 | Luiz Capitulino | qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); |
1882 | d15e5465 | Luiz Capitulino | } |
1883 | d15e5465 | Luiz Capitulino | |
1884 | f04ef601 | Luiz Capitulino | static const char *const io_status_name[BDRV_IOS_MAX] = { |
1885 | f04ef601 | Luiz Capitulino | [BDRV_IOS_OK] = "ok",
|
1886 | f04ef601 | Luiz Capitulino | [BDRV_IOS_FAILED] = "failed",
|
1887 | f04ef601 | Luiz Capitulino | [BDRV_IOS_ENOSPC] = "nospace",
|
1888 | f04ef601 | Luiz Capitulino | }; |
1889 | f04ef601 | Luiz Capitulino | |
1890 | d15e5465 | Luiz Capitulino | void bdrv_info(Monitor *mon, QObject **ret_data)
|
1891 | d15e5465 | Luiz Capitulino | { |
1892 | d15e5465 | Luiz Capitulino | QList *bs_list; |
1893 | b338082b | bellard | BlockDriverState *bs; |
1894 | b338082b | bellard | |
1895 | d15e5465 | Luiz Capitulino | bs_list = qlist_new(); |
1896 | d15e5465 | Luiz Capitulino | |
1897 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
1898 | d15e5465 | Luiz Capitulino | QObject *bs_obj; |
1899 | e4def80b | Markus Armbruster | QDict *bs_dict; |
1900 | d15e5465 | Luiz Capitulino | |
1901 | d8aeeb31 | Markus Armbruster | bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', "
|
1902 | d15e5465 | Luiz Capitulino | "'removable': %i, 'locked': %i }",
|
1903 | 2c6942fa | Markus Armbruster | bs->device_name, |
1904 | 2c6942fa | Markus Armbruster | bdrv_dev_has_removable_media(bs), |
1905 | f107639a | Markus Armbruster | bdrv_dev_is_medium_locked(bs)); |
1906 | e4def80b | Markus Armbruster | bs_dict = qobject_to_qdict(bs_obj); |
1907 | d15e5465 | Luiz Capitulino | |
1908 | e4def80b | Markus Armbruster | if (bdrv_dev_has_removable_media(bs)) {
|
1909 | e4def80b | Markus Armbruster | qdict_put(bs_dict, "tray-open",
|
1910 | e4def80b | Markus Armbruster | qbool_from_int(bdrv_dev_is_tray_open(bs))); |
1911 | e4def80b | Markus Armbruster | } |
1912 | f04ef601 | Luiz Capitulino | |
1913 | f04ef601 | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs)) {
|
1914 | f04ef601 | Luiz Capitulino | qdict_put(bs_dict, "io-status",
|
1915 | f04ef601 | Luiz Capitulino | qstring_from_str(io_status_name[bs->iostatus])); |
1916 | f04ef601 | Luiz Capitulino | } |
1917 | f04ef601 | Luiz Capitulino | |
1918 | 19cb3738 | bellard | if (bs->drv) {
|
1919 | d15e5465 | Luiz Capitulino | QObject *obj; |
1920 | d15e5465 | Luiz Capitulino | |
1921 | d15e5465 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
|
1922 | d15e5465 | Luiz Capitulino | "'encrypted': %i }",
|
1923 | d15e5465 | Luiz Capitulino | bs->filename, bs->read_only, |
1924 | d15e5465 | Luiz Capitulino | bs->drv->format_name, |
1925 | d15e5465 | Luiz Capitulino | bdrv_is_encrypted(bs)); |
1926 | fef30743 | ths | if (bs->backing_file[0] != '\0') { |
1927 | d15e5465 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(obj); |
1928 | d15e5465 | Luiz Capitulino | qdict_put(qdict, "backing_file",
|
1929 | d15e5465 | Luiz Capitulino | qstring_from_str(bs->backing_file)); |
1930 | 376253ec | aliguori | } |
1931 | d15e5465 | Luiz Capitulino | |
1932 | d15e5465 | Luiz Capitulino | qdict_put_obj(bs_dict, "inserted", obj);
|
1933 | b338082b | bellard | } |
1934 | d15e5465 | Luiz Capitulino | qlist_append_obj(bs_list, bs_obj); |
1935 | b338082b | bellard | } |
1936 | d15e5465 | Luiz Capitulino | |
1937 | d15e5465 | Luiz Capitulino | *ret_data = QOBJECT(bs_list); |
1938 | b338082b | bellard | } |
1939 | a36e69dd | ths | |
1940 | 218a536a | Luiz Capitulino | static void bdrv_stats_iter(QObject *data, void *opaque) |
1941 | a36e69dd | ths | { |
1942 | 218a536a | Luiz Capitulino | QDict *qdict; |
1943 | 218a536a | Luiz Capitulino | Monitor *mon = opaque; |
1944 | 218a536a | Luiz Capitulino | |
1945 | 218a536a | Luiz Capitulino | qdict = qobject_to_qdict(data); |
1946 | 218a536a | Luiz Capitulino | monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); |
1947 | 218a536a | Luiz Capitulino | |
1948 | 218a536a | Luiz Capitulino | qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
|
1949 | 218a536a | Luiz Capitulino | monitor_printf(mon, " rd_bytes=%" PRId64
|
1950 | 218a536a | Luiz Capitulino | " wr_bytes=%" PRId64
|
1951 | 218a536a | Luiz Capitulino | " rd_operations=%" PRId64
|
1952 | 218a536a | Luiz Capitulino | " wr_operations=%" PRId64
|
1953 | e8045d67 | Christoph Hellwig | " flush_operations=%" PRId64
|
1954 | c488c7f6 | Christoph Hellwig | " wr_total_time_ns=%" PRId64
|
1955 | c488c7f6 | Christoph Hellwig | " rd_total_time_ns=%" PRId64
|
1956 | c488c7f6 | Christoph Hellwig | " flush_total_time_ns=%" PRId64
|
1957 | 218a536a | Luiz Capitulino | "\n",
|
1958 | 218a536a | Luiz Capitulino | qdict_get_int(qdict, "rd_bytes"),
|
1959 | 218a536a | Luiz Capitulino | qdict_get_int(qdict, "wr_bytes"),
|
1960 | 218a536a | Luiz Capitulino | qdict_get_int(qdict, "rd_operations"),
|
1961 | e8045d67 | Christoph Hellwig | qdict_get_int(qdict, "wr_operations"),
|
1962 | c488c7f6 | Christoph Hellwig | qdict_get_int(qdict, "flush_operations"),
|
1963 | c488c7f6 | Christoph Hellwig | qdict_get_int(qdict, "wr_total_time_ns"),
|
1964 | c488c7f6 | Christoph Hellwig | qdict_get_int(qdict, "rd_total_time_ns"),
|
1965 | c488c7f6 | Christoph Hellwig | qdict_get_int(qdict, "flush_total_time_ns"));
|
1966 | 218a536a | Luiz Capitulino | } |
1967 | 218a536a | Luiz Capitulino | |
1968 | 218a536a | Luiz Capitulino | void bdrv_stats_print(Monitor *mon, const QObject *data) |
1969 | 218a536a | Luiz Capitulino | { |
1970 | 218a536a | Luiz Capitulino | qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); |
1971 | 218a536a | Luiz Capitulino | } |
1972 | 218a536a | Luiz Capitulino | |
1973 | 294cc35f | Kevin Wolf | static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
|
1974 | 294cc35f | Kevin Wolf | { |
1975 | 294cc35f | Kevin Wolf | QObject *res; |
1976 | 294cc35f | Kevin Wolf | QDict *dict; |
1977 | 294cc35f | Kevin Wolf | |
1978 | 294cc35f | Kevin Wolf | res = qobject_from_jsonf("{ 'stats': {"
|
1979 | 294cc35f | Kevin Wolf | "'rd_bytes': %" PRId64 "," |
1980 | 294cc35f | Kevin Wolf | "'wr_bytes': %" PRId64 "," |
1981 | 294cc35f | Kevin Wolf | "'rd_operations': %" PRId64 "," |
1982 | 294cc35f | Kevin Wolf | "'wr_operations': %" PRId64 "," |
1983 | e8045d67 | Christoph Hellwig | "'wr_highest_offset': %" PRId64 "," |
1984 | c488c7f6 | Christoph Hellwig | "'flush_operations': %" PRId64 "," |
1985 | c488c7f6 | Christoph Hellwig | "'wr_total_time_ns': %" PRId64 "," |
1986 | c488c7f6 | Christoph Hellwig | "'rd_total_time_ns': %" PRId64 "," |
1987 | c488c7f6 | Christoph Hellwig | "'flush_total_time_ns': %" PRId64
|
1988 | 294cc35f | Kevin Wolf | "} }",
|
1989 | a597e79c | Christoph Hellwig | bs->nr_bytes[BDRV_ACCT_READ], |
1990 | a597e79c | Christoph Hellwig | bs->nr_bytes[BDRV_ACCT_WRITE], |
1991 | a597e79c | Christoph Hellwig | bs->nr_ops[BDRV_ACCT_READ], |
1992 | a597e79c | Christoph Hellwig | bs->nr_ops[BDRV_ACCT_WRITE], |
1993 | 5ffbbc67 | Blue Swirl | bs->wr_highest_sector * |
1994 | e8045d67 | Christoph Hellwig | (uint64_t)BDRV_SECTOR_SIZE, |
1995 | c488c7f6 | Christoph Hellwig | bs->nr_ops[BDRV_ACCT_FLUSH], |
1996 | c488c7f6 | Christoph Hellwig | bs->total_time_ns[BDRV_ACCT_WRITE], |
1997 | c488c7f6 | Christoph Hellwig | bs->total_time_ns[BDRV_ACCT_READ], |
1998 | c488c7f6 | Christoph Hellwig | bs->total_time_ns[BDRV_ACCT_FLUSH]); |
1999 | 294cc35f | Kevin Wolf | dict = qobject_to_qdict(res); |
2000 | 294cc35f | Kevin Wolf | |
2001 | 294cc35f | Kevin Wolf | if (*bs->device_name) {
|
2002 | 294cc35f | Kevin Wolf | qdict_put(dict, "device", qstring_from_str(bs->device_name));
|
2003 | 294cc35f | Kevin Wolf | } |
2004 | 294cc35f | Kevin Wolf | |
2005 | 294cc35f | Kevin Wolf | if (bs->file) {
|
2006 | 294cc35f | Kevin Wolf | QObject *parent = bdrv_info_stats_bs(bs->file); |
2007 | 294cc35f | Kevin Wolf | qdict_put_obj(dict, "parent", parent);
|
2008 | 294cc35f | Kevin Wolf | } |
2009 | 294cc35f | Kevin Wolf | |
2010 | 294cc35f | Kevin Wolf | return res;
|
2011 | 294cc35f | Kevin Wolf | } |
2012 | 294cc35f | Kevin Wolf | |
2013 | 218a536a | Luiz Capitulino | void bdrv_info_stats(Monitor *mon, QObject **ret_data)
|
2014 | 218a536a | Luiz Capitulino | { |
2015 | 218a536a | Luiz Capitulino | QObject *obj; |
2016 | 218a536a | Luiz Capitulino | QList *devices; |
2017 | a36e69dd | ths | BlockDriverState *bs; |
2018 | a36e69dd | ths | |
2019 | 218a536a | Luiz Capitulino | devices = qlist_new(); |
2020 | 218a536a | Luiz Capitulino | |
2021 | 1b7bdbc1 | Stefan Hajnoczi | QTAILQ_FOREACH(bs, &bdrv_states, list) { |
2022 | 294cc35f | Kevin Wolf | obj = bdrv_info_stats_bs(bs); |
2023 | 218a536a | Luiz Capitulino | qlist_append_obj(devices, obj); |
2024 | a36e69dd | ths | } |
2025 | 218a536a | Luiz Capitulino | |
2026 | 218a536a | Luiz Capitulino | *ret_data = QOBJECT(devices); |
2027 | a36e69dd | ths | } |
2028 | ea2384d3 | bellard | |
2029 | 045df330 | aliguori | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
2030 | 045df330 | aliguori | { |
2031 | 045df330 | aliguori | if (bs->backing_hd && bs->backing_hd->encrypted)
|
2032 | 045df330 | aliguori | return bs->backing_file;
|
2033 | 045df330 | aliguori | else if (bs->encrypted) |
2034 | 045df330 | aliguori | return bs->filename;
|
2035 | 045df330 | aliguori | else
|
2036 | 045df330 | aliguori | return NULL; |
2037 | 045df330 | aliguori | } |
2038 | 045df330 | aliguori | |
2039 | 5fafdf24 | ths | void bdrv_get_backing_filename(BlockDriverState *bs,
|
2040 | 83f64091 | bellard | char *filename, int filename_size) |
2041 | 83f64091 | bellard | { |
2042 | b783e409 | Kevin Wolf | if (!bs->backing_file) {
|
2043 | 83f64091 | bellard | pstrcpy(filename, filename_size, "");
|
2044 | 83f64091 | bellard | } else {
|
2045 | 83f64091 | bellard | pstrcpy(filename, filename_size, bs->backing_file); |
2046 | 83f64091 | bellard | } |
2047 | 83f64091 | bellard | } |
2048 | 83f64091 | bellard | |
2049 | 5fafdf24 | ths | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
2050 | faea38e7 | bellard | const uint8_t *buf, int nb_sectors) |
2051 | faea38e7 | bellard | { |
2052 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2053 | faea38e7 | bellard | if (!drv)
|
2054 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2055 | faea38e7 | bellard | if (!drv->bdrv_write_compressed)
|
2056 | faea38e7 | bellard | return -ENOTSUP;
|
2057 | fbb7b4e0 | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
2058 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
2059 | a55eb92c | Jan Kiszka | |
2060 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
2061 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
2062 | 7cd1e32a | lirans@il.ibm.com | } |
2063 | a55eb92c | Jan Kiszka | |
2064 | faea38e7 | bellard | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
|
2065 | faea38e7 | bellard | } |
2066 | 3b46e624 | ths | |
2067 | faea38e7 | bellard | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
2068 | faea38e7 | bellard | { |
2069 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2070 | faea38e7 | bellard | if (!drv)
|
2071 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2072 | faea38e7 | bellard | if (!drv->bdrv_get_info)
|
2073 | faea38e7 | bellard | return -ENOTSUP;
|
2074 | faea38e7 | bellard | memset(bdi, 0, sizeof(*bdi)); |
2075 | faea38e7 | bellard | return drv->bdrv_get_info(bs, bdi);
|
2076 | faea38e7 | bellard | } |
2077 | faea38e7 | bellard | |
2078 | 45566e9c | Christoph Hellwig | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
2079 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
2080 | 178e08a5 | aliguori | { |
2081 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
2082 | 178e08a5 | aliguori | if (!drv)
|
2083 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
2084 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_save_vmstate)
|
2085 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_save_vmstate(bs, buf, pos, size);
|
2086 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2087 | 7cdb1f6d | MORITA Kazutaka | return bdrv_save_vmstate(bs->file, buf, pos, size);
|
2088 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2089 | 178e08a5 | aliguori | } |
2090 | 178e08a5 | aliguori | |
2091 | 45566e9c | Christoph Hellwig | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
|
2092 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
2093 | 178e08a5 | aliguori | { |
2094 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
2095 | 178e08a5 | aliguori | if (!drv)
|
2096 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
2097 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_load_vmstate)
|
2098 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_load_vmstate(bs, buf, pos, size);
|
2099 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2100 | 7cdb1f6d | MORITA Kazutaka | return bdrv_load_vmstate(bs->file, buf, pos, size);
|
2101 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2102 | 178e08a5 | aliguori | } |
2103 | 178e08a5 | aliguori | |
2104 | 8b9b0cc2 | Kevin Wolf | void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
|
2105 | 8b9b0cc2 | Kevin Wolf | { |
2106 | 8b9b0cc2 | Kevin Wolf | BlockDriver *drv = bs->drv; |
2107 | 8b9b0cc2 | Kevin Wolf | |
2108 | 8b9b0cc2 | Kevin Wolf | if (!drv || !drv->bdrv_debug_event) {
|
2109 | 8b9b0cc2 | Kevin Wolf | return;
|
2110 | 8b9b0cc2 | Kevin Wolf | } |
2111 | 8b9b0cc2 | Kevin Wolf | |
2112 | 8b9b0cc2 | Kevin Wolf | return drv->bdrv_debug_event(bs, event);
|
2113 | 8b9b0cc2 | Kevin Wolf | |
2114 | 8b9b0cc2 | Kevin Wolf | } |
2115 | 8b9b0cc2 | Kevin Wolf | |
2116 | faea38e7 | bellard | /**************************************************************/
|
2117 | faea38e7 | bellard | /* handling of snapshots */
|
2118 | faea38e7 | bellard | |
2119 | feeee5ac | Miguel Di Ciurcio Filho | int bdrv_can_snapshot(BlockDriverState *bs)
|
2120 | feeee5ac | Miguel Di Ciurcio Filho | { |
2121 | feeee5ac | Miguel Di Ciurcio Filho | BlockDriver *drv = bs->drv; |
2122 | 07b70bfb | Markus Armbruster | if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
|
2123 | feeee5ac | Miguel Di Ciurcio Filho | return 0; |
2124 | feeee5ac | Miguel Di Ciurcio Filho | } |
2125 | feeee5ac | Miguel Di Ciurcio Filho | |
2126 | feeee5ac | Miguel Di Ciurcio Filho | if (!drv->bdrv_snapshot_create) {
|
2127 | feeee5ac | Miguel Di Ciurcio Filho | if (bs->file != NULL) { |
2128 | feeee5ac | Miguel Di Ciurcio Filho | return bdrv_can_snapshot(bs->file);
|
2129 | feeee5ac | Miguel Di Ciurcio Filho | } |
2130 | feeee5ac | Miguel Di Ciurcio Filho | return 0; |
2131 | feeee5ac | Miguel Di Ciurcio Filho | } |
2132 | feeee5ac | Miguel Di Ciurcio Filho | |
2133 | feeee5ac | Miguel Di Ciurcio Filho | return 1; |
2134 | feeee5ac | Miguel Di Ciurcio Filho | } |
2135 | feeee5ac | Miguel Di Ciurcio Filho | |
2136 | 199630b6 | Blue Swirl | int bdrv_is_snapshot(BlockDriverState *bs)
|
2137 | 199630b6 | Blue Swirl | { |
2138 | 199630b6 | Blue Swirl | return !!(bs->open_flags & BDRV_O_SNAPSHOT);
|
2139 | 199630b6 | Blue Swirl | } |
2140 | 199630b6 | Blue Swirl | |
2141 | f9092b10 | Markus Armbruster | BlockDriverState *bdrv_snapshots(void)
|
2142 | f9092b10 | Markus Armbruster | { |
2143 | f9092b10 | Markus Armbruster | BlockDriverState *bs; |
2144 | f9092b10 | Markus Armbruster | |
2145 | 3ac906f7 | Markus Armbruster | if (bs_snapshots) {
|
2146 | f9092b10 | Markus Armbruster | return bs_snapshots;
|
2147 | 3ac906f7 | Markus Armbruster | } |
2148 | f9092b10 | Markus Armbruster | |
2149 | f9092b10 | Markus Armbruster | bs = NULL;
|
2150 | f9092b10 | Markus Armbruster | while ((bs = bdrv_next(bs))) {
|
2151 | f9092b10 | Markus Armbruster | if (bdrv_can_snapshot(bs)) {
|
2152 | 3ac906f7 | Markus Armbruster | bs_snapshots = bs; |
2153 | 3ac906f7 | Markus Armbruster | return bs;
|
2154 | f9092b10 | Markus Armbruster | } |
2155 | f9092b10 | Markus Armbruster | } |
2156 | f9092b10 | Markus Armbruster | return NULL; |
2157 | f9092b10 | Markus Armbruster | } |
2158 | f9092b10 | Markus Armbruster | |
2159 | 5fafdf24 | ths | int bdrv_snapshot_create(BlockDriverState *bs,
|
2160 | faea38e7 | bellard | QEMUSnapshotInfo *sn_info) |
2161 | faea38e7 | bellard | { |
2162 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2163 | faea38e7 | bellard | if (!drv)
|
2164 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2165 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_create)
|
2166 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_create(bs, sn_info);
|
2167 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2168 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_create(bs->file, sn_info);
|
2169 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2170 | faea38e7 | bellard | } |
2171 | faea38e7 | bellard | |
2172 | 5fafdf24 | ths | int bdrv_snapshot_goto(BlockDriverState *bs,
|
2173 | faea38e7 | bellard | const char *snapshot_id) |
2174 | faea38e7 | bellard | { |
2175 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2176 | 7cdb1f6d | MORITA Kazutaka | int ret, open_ret;
|
2177 | 7cdb1f6d | MORITA Kazutaka | |
2178 | faea38e7 | bellard | if (!drv)
|
2179 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2180 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_goto)
|
2181 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_goto(bs, snapshot_id);
|
2182 | 7cdb1f6d | MORITA Kazutaka | |
2183 | 7cdb1f6d | MORITA Kazutaka | if (bs->file) {
|
2184 | 7cdb1f6d | MORITA Kazutaka | drv->bdrv_close(bs); |
2185 | 7cdb1f6d | MORITA Kazutaka | ret = bdrv_snapshot_goto(bs->file, snapshot_id); |
2186 | 7cdb1f6d | MORITA Kazutaka | open_ret = drv->bdrv_open(bs, bs->open_flags); |
2187 | 7cdb1f6d | MORITA Kazutaka | if (open_ret < 0) { |
2188 | 7cdb1f6d | MORITA Kazutaka | bdrv_delete(bs->file); |
2189 | 7cdb1f6d | MORITA Kazutaka | bs->drv = NULL;
|
2190 | 7cdb1f6d | MORITA Kazutaka | return open_ret;
|
2191 | 7cdb1f6d | MORITA Kazutaka | } |
2192 | 7cdb1f6d | MORITA Kazutaka | return ret;
|
2193 | 7cdb1f6d | MORITA Kazutaka | } |
2194 | 7cdb1f6d | MORITA Kazutaka | |
2195 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2196 | faea38e7 | bellard | } |
2197 | faea38e7 | bellard | |
2198 | faea38e7 | bellard | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
2199 | faea38e7 | bellard | { |
2200 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2201 | faea38e7 | bellard | if (!drv)
|
2202 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2203 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_delete)
|
2204 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_delete(bs, snapshot_id);
|
2205 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2206 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_delete(bs->file, snapshot_id);
|
2207 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2208 | faea38e7 | bellard | } |
2209 | faea38e7 | bellard | |
2210 | 5fafdf24 | ths | int bdrv_snapshot_list(BlockDriverState *bs,
|
2211 | faea38e7 | bellard | QEMUSnapshotInfo **psn_info) |
2212 | faea38e7 | bellard | { |
2213 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
2214 | faea38e7 | bellard | if (!drv)
|
2215 | 19cb3738 | bellard | return -ENOMEDIUM;
|
2216 | 7cdb1f6d | MORITA Kazutaka | if (drv->bdrv_snapshot_list)
|
2217 | 7cdb1f6d | MORITA Kazutaka | return drv->bdrv_snapshot_list(bs, psn_info);
|
2218 | 7cdb1f6d | MORITA Kazutaka | if (bs->file)
|
2219 | 7cdb1f6d | MORITA Kazutaka | return bdrv_snapshot_list(bs->file, psn_info);
|
2220 | 7cdb1f6d | MORITA Kazutaka | return -ENOTSUP;
|
2221 | faea38e7 | bellard | } |
2222 | faea38e7 | bellard | |
2223 | 51ef6727 | edison | int bdrv_snapshot_load_tmp(BlockDriverState *bs,
|
2224 | 51ef6727 | edison | const char *snapshot_name) |
2225 | 51ef6727 | edison | { |
2226 | 51ef6727 | edison | BlockDriver *drv = bs->drv; |
2227 | 51ef6727 | edison | if (!drv) {
|
2228 | 51ef6727 | edison | return -ENOMEDIUM;
|
2229 | 51ef6727 | edison | } |
2230 | 51ef6727 | edison | if (!bs->read_only) {
|
2231 | 51ef6727 | edison | return -EINVAL;
|
2232 | 51ef6727 | edison | } |
2233 | 51ef6727 | edison | if (drv->bdrv_snapshot_load_tmp) {
|
2234 | 51ef6727 | edison | return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
|
2235 | 51ef6727 | edison | } |
2236 | 51ef6727 | edison | return -ENOTSUP;
|
2237 | 51ef6727 | edison | } |
2238 | 51ef6727 | edison | |
2239 | faea38e7 | bellard | #define NB_SUFFIXES 4 |
2240 | faea38e7 | bellard | |
2241 | faea38e7 | bellard | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
2242 | faea38e7 | bellard | { |
2243 | faea38e7 | bellard | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
2244 | faea38e7 | bellard | int64_t base; |
2245 | faea38e7 | bellard | int i;
|
2246 | faea38e7 | bellard | |
2247 | faea38e7 | bellard | if (size <= 999) { |
2248 | faea38e7 | bellard | snprintf(buf, buf_size, "%" PRId64, size);
|
2249 | faea38e7 | bellard | } else {
|
2250 | faea38e7 | bellard | base = 1024;
|
2251 | faea38e7 | bellard | for(i = 0; i < NB_SUFFIXES; i++) { |
2252 | faea38e7 | bellard | if (size < (10 * base)) { |
2253 | 5fafdf24 | ths | snprintf(buf, buf_size, "%0.1f%c",
|
2254 | faea38e7 | bellard | (double)size / base,
|
2255 | faea38e7 | bellard | suffixes[i]); |
2256 | faea38e7 | bellard | break;
|
2257 | faea38e7 | bellard | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
2258 | 5fafdf24 | ths | snprintf(buf, buf_size, "%" PRId64 "%c", |
2259 | faea38e7 | bellard | ((size + (base >> 1)) / base),
|
2260 | faea38e7 | bellard | suffixes[i]); |
2261 | faea38e7 | bellard | break;
|
2262 | faea38e7 | bellard | } |
2263 | faea38e7 | bellard | base = base * 1024;
|
2264 | faea38e7 | bellard | } |
2265 | faea38e7 | bellard | } |
2266 | faea38e7 | bellard | return buf;
|
2267 | faea38e7 | bellard | } |
2268 | faea38e7 | bellard | |
2269 | faea38e7 | bellard | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
2270 | faea38e7 | bellard | { |
2271 | faea38e7 | bellard | char buf1[128], date_buf[128], clock_buf[128]; |
2272 | 3b9f94e1 | bellard | #ifdef _WIN32
|
2273 | 3b9f94e1 | bellard | struct tm *ptm;
|
2274 | 3b9f94e1 | bellard | #else
|
2275 | faea38e7 | bellard | struct tm tm;
|
2276 | 3b9f94e1 | bellard | #endif
|
2277 | faea38e7 | bellard | time_t ti; |
2278 | faea38e7 | bellard | int64_t secs; |
2279 | faea38e7 | bellard | |
2280 | faea38e7 | bellard | if (!sn) {
|
2281 | 5fafdf24 | ths | snprintf(buf, buf_size, |
2282 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
2283 | faea38e7 | bellard | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
2284 | faea38e7 | bellard | } else {
|
2285 | faea38e7 | bellard | ti = sn->date_sec; |
2286 | 3b9f94e1 | bellard | #ifdef _WIN32
|
2287 | 3b9f94e1 | bellard | ptm = localtime(&ti); |
2288 | 3b9f94e1 | bellard | strftime(date_buf, sizeof(date_buf),
|
2289 | 3b9f94e1 | bellard | "%Y-%m-%d %H:%M:%S", ptm);
|
2290 | 3b9f94e1 | bellard | #else
|
2291 | faea38e7 | bellard | localtime_r(&ti, &tm); |
2292 | faea38e7 | bellard | strftime(date_buf, sizeof(date_buf),
|
2293 | faea38e7 | bellard | "%Y-%m-%d %H:%M:%S", &tm);
|
2294 | 3b9f94e1 | bellard | #endif
|
2295 | faea38e7 | bellard | secs = sn->vm_clock_nsec / 1000000000;
|
2296 | faea38e7 | bellard | snprintf(clock_buf, sizeof(clock_buf),
|
2297 | faea38e7 | bellard | "%02d:%02d:%02d.%03d",
|
2298 | faea38e7 | bellard | (int)(secs / 3600), |
2299 | faea38e7 | bellard | (int)((secs / 60) % 60), |
2300 | 5fafdf24 | ths | (int)(secs % 60), |
2301 | faea38e7 | bellard | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
2302 | faea38e7 | bellard | snprintf(buf, buf_size, |
2303 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
2304 | faea38e7 | bellard | sn->id_str, sn->name, |
2305 | faea38e7 | bellard | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
|
2306 | faea38e7 | bellard | date_buf, |
2307 | faea38e7 | bellard | clock_buf); |
2308 | faea38e7 | bellard | } |
2309 | faea38e7 | bellard | return buf;
|
2310 | faea38e7 | bellard | } |
2311 | faea38e7 | bellard | |
2312 | ea2384d3 | bellard | /**************************************************************/
|
2313 | 83f64091 | bellard | /* async I/Os */
|
2314 | ea2384d3 | bellard | |
2315 | 3b69e4b9 | aliguori | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
2316 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
2317 | 3b69e4b9 | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2318 | 3b69e4b9 | aliguori | { |
2319 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); |
2320 | bbf0a440 | Stefan Hajnoczi | |
2321 | b2a61371 | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
2322 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, false);
|
2323 | ea2384d3 | bellard | } |
2324 | ea2384d3 | bellard | |
2325 | f141eafe | aliguori | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
2326 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
2327 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2328 | ea2384d3 | bellard | { |
2329 | bbf0a440 | Stefan Hajnoczi | trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); |
2330 | bbf0a440 | Stefan Hajnoczi | |
2331 | 1a6e115b | Stefan Hajnoczi | return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
|
2332 | 8c5873d6 | Stefan Hajnoczi | cb, opaque, true);
|
2333 | 83f64091 | bellard | } |
2334 | 83f64091 | bellard | |
2335 | 40b4f539 | Kevin Wolf | |
2336 | 40b4f539 | Kevin Wolf | typedef struct MultiwriteCB { |
2337 | 40b4f539 | Kevin Wolf | int error;
|
2338 | 40b4f539 | Kevin Wolf | int num_requests;
|
2339 | 40b4f539 | Kevin Wolf | int num_callbacks;
|
2340 | 40b4f539 | Kevin Wolf | struct {
|
2341 | 40b4f539 | Kevin Wolf | BlockDriverCompletionFunc *cb; |
2342 | 40b4f539 | Kevin Wolf | void *opaque;
|
2343 | 40b4f539 | Kevin Wolf | QEMUIOVector *free_qiov; |
2344 | 40b4f539 | Kevin Wolf | void *free_buf;
|
2345 | 40b4f539 | Kevin Wolf | } callbacks[]; |
2346 | 40b4f539 | Kevin Wolf | } MultiwriteCB; |
2347 | 40b4f539 | Kevin Wolf | |
2348 | 40b4f539 | Kevin Wolf | static void multiwrite_user_cb(MultiwriteCB *mcb) |
2349 | 40b4f539 | Kevin Wolf | { |
2350 | 40b4f539 | Kevin Wolf | int i;
|
2351 | 40b4f539 | Kevin Wolf | |
2352 | 40b4f539 | Kevin Wolf | for (i = 0; i < mcb->num_callbacks; i++) { |
2353 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
2354 | 1e1ea48d | Stefan Hajnoczi | if (mcb->callbacks[i].free_qiov) {
|
2355 | 1e1ea48d | Stefan Hajnoczi | qemu_iovec_destroy(mcb->callbacks[i].free_qiov); |
2356 | 1e1ea48d | Stefan Hajnoczi | } |
2357 | 7267c094 | Anthony Liguori | g_free(mcb->callbacks[i].free_qiov); |
2358 | f8a83245 | Herve Poussineau | qemu_vfree(mcb->callbacks[i].free_buf); |
2359 | 40b4f539 | Kevin Wolf | } |
2360 | 40b4f539 | Kevin Wolf | } |
2361 | 40b4f539 | Kevin Wolf | |
2362 | 40b4f539 | Kevin Wolf | static void multiwrite_cb(void *opaque, int ret) |
2363 | 40b4f539 | Kevin Wolf | { |
2364 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb = opaque; |
2365 | 40b4f539 | Kevin Wolf | |
2366 | 6d519a5f | Stefan Hajnoczi | trace_multiwrite_cb(mcb, ret); |
2367 | 6d519a5f | Stefan Hajnoczi | |
2368 | cb6d3ca0 | Kevin Wolf | if (ret < 0 && !mcb->error) { |
2369 | 40b4f539 | Kevin Wolf | mcb->error = ret; |
2370 | 40b4f539 | Kevin Wolf | } |
2371 | 40b4f539 | Kevin Wolf | |
2372 | 40b4f539 | Kevin Wolf | mcb->num_requests--; |
2373 | 40b4f539 | Kevin Wolf | if (mcb->num_requests == 0) { |
2374 | de189a1b | Kevin Wolf | multiwrite_user_cb(mcb); |
2375 | 7267c094 | Anthony Liguori | g_free(mcb); |
2376 | 40b4f539 | Kevin Wolf | } |
2377 | 40b4f539 | Kevin Wolf | } |
2378 | 40b4f539 | Kevin Wolf | |
2379 | 40b4f539 | Kevin Wolf | static int multiwrite_req_compare(const void *a, const void *b) |
2380 | 40b4f539 | Kevin Wolf | { |
2381 | 77be4366 | Christoph Hellwig | const BlockRequest *req1 = a, *req2 = b;
|
2382 | 77be4366 | Christoph Hellwig | |
2383 | 77be4366 | Christoph Hellwig | /*
|
2384 | 77be4366 | Christoph Hellwig | * Note that we can't simply subtract req2->sector from req1->sector
|
2385 | 77be4366 | Christoph Hellwig | * here as that could overflow the return value.
|
2386 | 77be4366 | Christoph Hellwig | */
|
2387 | 77be4366 | Christoph Hellwig | if (req1->sector > req2->sector) {
|
2388 | 77be4366 | Christoph Hellwig | return 1; |
2389 | 77be4366 | Christoph Hellwig | } else if (req1->sector < req2->sector) { |
2390 | 77be4366 | Christoph Hellwig | return -1; |
2391 | 77be4366 | Christoph Hellwig | } else {
|
2392 | 77be4366 | Christoph Hellwig | return 0; |
2393 | 77be4366 | Christoph Hellwig | } |
2394 | 40b4f539 | Kevin Wolf | } |
2395 | 40b4f539 | Kevin Wolf | |
2396 | 40b4f539 | Kevin Wolf | /*
|
2397 | 40b4f539 | Kevin Wolf | * Takes a bunch of requests and tries to merge them. Returns the number of
|
2398 | 40b4f539 | Kevin Wolf | * requests that remain after merging.
|
2399 | 40b4f539 | Kevin Wolf | */
|
2400 | 40b4f539 | Kevin Wolf | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
2401 | 40b4f539 | Kevin Wolf | int num_reqs, MultiwriteCB *mcb)
|
2402 | 40b4f539 | Kevin Wolf | { |
2403 | 40b4f539 | Kevin Wolf | int i, outidx;
|
2404 | 40b4f539 | Kevin Wolf | |
2405 | 40b4f539 | Kevin Wolf | // Sort requests by start sector
|
2406 | 40b4f539 | Kevin Wolf | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
|
2407 | 40b4f539 | Kevin Wolf | |
2408 | 40b4f539 | Kevin Wolf | // Check if adjacent requests touch the same clusters. If so, combine them,
|
2409 | 40b4f539 | Kevin Wolf | // filling up gaps with zero sectors.
|
2410 | 40b4f539 | Kevin Wolf | outidx = 0;
|
2411 | 40b4f539 | Kevin Wolf | for (i = 1; i < num_reqs; i++) { |
2412 | 40b4f539 | Kevin Wolf | int merge = 0; |
2413 | 40b4f539 | Kevin Wolf | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
2414 | 40b4f539 | Kevin Wolf | |
2415 | 40b4f539 | Kevin Wolf | // This handles the cases that are valid for all block drivers, namely
|
2416 | 40b4f539 | Kevin Wolf | // exactly sequential writes and overlapping writes.
|
2417 | 40b4f539 | Kevin Wolf | if (reqs[i].sector <= oldreq_last) {
|
2418 | 40b4f539 | Kevin Wolf | merge = 1;
|
2419 | 40b4f539 | Kevin Wolf | } |
2420 | 40b4f539 | Kevin Wolf | |
2421 | 40b4f539 | Kevin Wolf | // The block driver may decide that it makes sense to combine requests
|
2422 | 40b4f539 | Kevin Wolf | // even if there is a gap of some sectors between them. In this case,
|
2423 | 40b4f539 | Kevin Wolf | // the gap is filled with zeros (therefore only applicable for yet
|
2424 | 40b4f539 | Kevin Wolf | // unused space in format like qcow2).
|
2425 | 40b4f539 | Kevin Wolf | if (!merge && bs->drv->bdrv_merge_requests) {
|
2426 | 40b4f539 | Kevin Wolf | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); |
2427 | 40b4f539 | Kevin Wolf | } |
2428 | 40b4f539 | Kevin Wolf | |
2429 | e2a305fb | Christoph Hellwig | if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { |
2430 | e2a305fb | Christoph Hellwig | merge = 0;
|
2431 | e2a305fb | Christoph Hellwig | } |
2432 | e2a305fb | Christoph Hellwig | |
2433 | 40b4f539 | Kevin Wolf | if (merge) {
|
2434 | 40b4f539 | Kevin Wolf | size_t size; |
2435 | 7267c094 | Anthony Liguori | QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
|
2436 | 40b4f539 | Kevin Wolf | qemu_iovec_init(qiov, |
2437 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
|
2438 | 40b4f539 | Kevin Wolf | |
2439 | 40b4f539 | Kevin Wolf | // Add the first request to the merged one. If the requests are
|
2440 | 40b4f539 | Kevin Wolf | // overlapping, drop the last sectors of the first request.
|
2441 | 40b4f539 | Kevin Wolf | size = (reqs[i].sector - reqs[outidx].sector) << 9;
|
2442 | 40b4f539 | Kevin Wolf | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); |
2443 | 40b4f539 | Kevin Wolf | |
2444 | 40b4f539 | Kevin Wolf | // We might need to add some zeros between the two requests
|
2445 | 40b4f539 | Kevin Wolf | if (reqs[i].sector > oldreq_last) {
|
2446 | 40b4f539 | Kevin Wolf | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
|
2447 | 40b4f539 | Kevin Wolf | uint8_t *buf = qemu_blockalign(bs, zero_bytes); |
2448 | 40b4f539 | Kevin Wolf | memset(buf, 0, zero_bytes);
|
2449 | 40b4f539 | Kevin Wolf | qemu_iovec_add(qiov, buf, zero_bytes); |
2450 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_buf = buf; |
2451 | 40b4f539 | Kevin Wolf | } |
2452 | 40b4f539 | Kevin Wolf | |
2453 | 40b4f539 | Kevin Wolf | // Add the second request
|
2454 | 40b4f539 | Kevin Wolf | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); |
2455 | 40b4f539 | Kevin Wolf | |
2456 | cbf1dff2 | Kevin Wolf | reqs[outidx].nb_sectors = qiov->size >> 9;
|
2457 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = qiov; |
2458 | 40b4f539 | Kevin Wolf | |
2459 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
2460 | 40b4f539 | Kevin Wolf | } else {
|
2461 | 40b4f539 | Kevin Wolf | outidx++; |
2462 | 40b4f539 | Kevin Wolf | reqs[outidx].sector = reqs[i].sector; |
2463 | 40b4f539 | Kevin Wolf | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
2464 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = reqs[i].qiov; |
2465 | 40b4f539 | Kevin Wolf | } |
2466 | 40b4f539 | Kevin Wolf | } |
2467 | 40b4f539 | Kevin Wolf | |
2468 | 40b4f539 | Kevin Wolf | return outidx + 1; |
2469 | 40b4f539 | Kevin Wolf | } |
2470 | 40b4f539 | Kevin Wolf | |
2471 | 40b4f539 | Kevin Wolf | /*
|
2472 | 40b4f539 | Kevin Wolf | * Submit multiple AIO write requests at once.
|
2473 | 40b4f539 | Kevin Wolf | *
|
2474 | 40b4f539 | Kevin Wolf | * On success, the function returns 0 and all requests in the reqs array have
|
2475 | 40b4f539 | Kevin Wolf | * been submitted. In error case this function returns -1, and any of the
|
2476 | 40b4f539 | Kevin Wolf | * requests may or may not be submitted yet. In particular, this means that the
|
2477 | 40b4f539 | Kevin Wolf | * callback will be called for some of the requests, for others it won't. The
|
2478 | 40b4f539 | Kevin Wolf | * caller must check the error field of the BlockRequest to wait for the right
|
2479 | 40b4f539 | Kevin Wolf | * callbacks (if error != 0, no callback will be called).
|
2480 | 40b4f539 | Kevin Wolf | *
|
2481 | 40b4f539 | Kevin Wolf | * The implementation may modify the contents of the reqs array, e.g. to merge
|
2482 | 40b4f539 | Kevin Wolf | * requests. However, the fields opaque and error are left unmodified as they
|
2483 | 40b4f539 | Kevin Wolf | * are used to signal failure for a single request to the caller.
|
2484 | 40b4f539 | Kevin Wolf | */
|
2485 | 40b4f539 | Kevin Wolf | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
2486 | 40b4f539 | Kevin Wolf | { |
2487 | 40b4f539 | Kevin Wolf | BlockDriverAIOCB *acb; |
2488 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb; |
2489 | 40b4f539 | Kevin Wolf | int i;
|
2490 | 40b4f539 | Kevin Wolf | |
2491 | 301db7c2 | Ryan Harper | /* don't submit writes if we don't have a medium */
|
2492 | 301db7c2 | Ryan Harper | if (bs->drv == NULL) { |
2493 | 301db7c2 | Ryan Harper | for (i = 0; i < num_reqs; i++) { |
2494 | 301db7c2 | Ryan Harper | reqs[i].error = -ENOMEDIUM; |
2495 | 301db7c2 | Ryan Harper | } |
2496 | 301db7c2 | Ryan Harper | return -1; |
2497 | 301db7c2 | Ryan Harper | } |
2498 | 301db7c2 | Ryan Harper | |
2499 | 40b4f539 | Kevin Wolf | if (num_reqs == 0) { |
2500 | 40b4f539 | Kevin Wolf | return 0; |
2501 | 40b4f539 | Kevin Wolf | } |
2502 | 40b4f539 | Kevin Wolf | |
2503 | 40b4f539 | Kevin Wolf | // Create MultiwriteCB structure
|
2504 | 7267c094 | Anthony Liguori | mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
2505 | 40b4f539 | Kevin Wolf | mcb->num_requests = 0;
|
2506 | 40b4f539 | Kevin Wolf | mcb->num_callbacks = num_reqs; |
2507 | 40b4f539 | Kevin Wolf | |
2508 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
2509 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb = reqs[i].cb; |
2510 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].opaque = reqs[i].opaque; |
2511 | 40b4f539 | Kevin Wolf | } |
2512 | 40b4f539 | Kevin Wolf | |
2513 | 40b4f539 | Kevin Wolf | // Check for mergable requests
|
2514 | 40b4f539 | Kevin Wolf | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
2515 | 40b4f539 | Kevin Wolf | |
2516 | 6d519a5f | Stefan Hajnoczi | trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); |
2517 | 6d519a5f | Stefan Hajnoczi | |
2518 | 453f9a16 | Kevin Wolf | /*
|
2519 | 453f9a16 | Kevin Wolf | * Run the aio requests. As soon as one request can't be submitted
|
2520 | 453f9a16 | Kevin Wolf | * successfully, fail all requests that are not yet submitted (we must
|
2521 | 453f9a16 | Kevin Wolf | * return failure for all requests anyway)
|
2522 | 453f9a16 | Kevin Wolf | *
|
2523 | 453f9a16 | Kevin Wolf | * num_requests cannot be set to the right value immediately: If
|
2524 | 453f9a16 | Kevin Wolf | * bdrv_aio_writev fails for some request, num_requests would be too high
|
2525 | 453f9a16 | Kevin Wolf | * and therefore multiwrite_cb() would never recognize the multiwrite
|
2526 | 453f9a16 | Kevin Wolf | * request as completed. We also cannot use the loop variable i to set it
|
2527 | 453f9a16 | Kevin Wolf | * when the first request fails because the callback may already have been
|
2528 | 453f9a16 | Kevin Wolf | * called for previously submitted requests. Thus, num_requests must be
|
2529 | 453f9a16 | Kevin Wolf | * incremented for each request that is submitted.
|
2530 | 453f9a16 | Kevin Wolf | *
|
2531 | 453f9a16 | Kevin Wolf | * The problem that callbacks may be called early also means that we need
|
2532 | 453f9a16 | Kevin Wolf | * to take care that num_requests doesn't become 0 before all requests are
|
2533 | 453f9a16 | Kevin Wolf | * submitted - multiwrite_cb() would consider the multiwrite request
|
2534 | 453f9a16 | Kevin Wolf | * completed. A dummy request that is "completed" by a manual call to
|
2535 | 453f9a16 | Kevin Wolf | * multiwrite_cb() takes care of this.
|
2536 | 453f9a16 | Kevin Wolf | */
|
2537 | 453f9a16 | Kevin Wolf | mcb->num_requests = 1;
|
2538 | 453f9a16 | Kevin Wolf | |
2539 | 6d519a5f | Stefan Hajnoczi | // Run the aio requests
|
2540 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
2541 | 453f9a16 | Kevin Wolf | mcb->num_requests++; |
2542 | 40b4f539 | Kevin Wolf | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
2543 | 40b4f539 | Kevin Wolf | reqs[i].nb_sectors, multiwrite_cb, mcb); |
2544 | 40b4f539 | Kevin Wolf | |
2545 | 40b4f539 | Kevin Wolf | if (acb == NULL) { |
2546 | 40b4f539 | Kevin Wolf | // We can only fail the whole thing if no request has been
|
2547 | 40b4f539 | Kevin Wolf | // submitted yet. Otherwise we'll wait for the submitted AIOs to
|
2548 | 40b4f539 | Kevin Wolf | // complete and report the error in the callback.
|
2549 | 453f9a16 | Kevin Wolf | if (i == 0) { |
2550 | 6d519a5f | Stefan Hajnoczi | trace_bdrv_aio_multiwrite_earlyfail(mcb); |
2551 | 40b4f539 | Kevin Wolf | goto fail;
|
2552 | 40b4f539 | Kevin Wolf | } else {
|
2553 | 6d519a5f | Stefan Hajnoczi | trace_bdrv_aio_multiwrite_latefail(mcb, i); |
2554 | 7eb58a6c | Kevin Wolf | multiwrite_cb(mcb, -EIO); |
2555 | 40b4f539 | Kevin Wolf | break;
|
2556 | 40b4f539 | Kevin Wolf | } |
2557 | 40b4f539 | Kevin Wolf | } |
2558 | 40b4f539 | Kevin Wolf | } |
2559 | 40b4f539 | Kevin Wolf | |
2560 | 453f9a16 | Kevin Wolf | /* Complete the dummy request */
|
2561 | 453f9a16 | Kevin Wolf | multiwrite_cb(mcb, 0);
|
2562 | 453f9a16 | Kevin Wolf | |
2563 | 40b4f539 | Kevin Wolf | return 0; |
2564 | 40b4f539 | Kevin Wolf | |
2565 | 40b4f539 | Kevin Wolf | fail:
|
2566 | 453f9a16 | Kevin Wolf | for (i = 0; i < mcb->num_callbacks; i++) { |
2567 | 453f9a16 | Kevin Wolf | reqs[i].error = -EIO; |
2568 | 453f9a16 | Kevin Wolf | } |
2569 | 7267c094 | Anthony Liguori | g_free(mcb); |
2570 | 40b4f539 | Kevin Wolf | return -1; |
2571 | 40b4f539 | Kevin Wolf | } |
2572 | 40b4f539 | Kevin Wolf | |
2573 | 83f64091 | bellard | void bdrv_aio_cancel(BlockDriverAIOCB *acb)
|
2574 | 83f64091 | bellard | { |
2575 | 6bbff9a0 | aliguori | acb->pool->cancel(acb); |
2576 | 83f64091 | bellard | } |
2577 | 83f64091 | bellard | |
2578 | ce1a14dc | pbrook | |
2579 | 83f64091 | bellard | /**************************************************************/
|
2580 | 83f64091 | bellard | /* async block device emulation */
|
2581 | 83f64091 | bellard | |
2582 | c16b5a2c | Christoph Hellwig | typedef struct BlockDriverAIOCBSync { |
2583 | c16b5a2c | Christoph Hellwig | BlockDriverAIOCB common; |
2584 | c16b5a2c | Christoph Hellwig | QEMUBH *bh; |
2585 | c16b5a2c | Christoph Hellwig | int ret;
|
2586 | c16b5a2c | Christoph Hellwig | /* vector translation state */
|
2587 | c16b5a2c | Christoph Hellwig | QEMUIOVector *qiov; |
2588 | c16b5a2c | Christoph Hellwig | uint8_t *bounce; |
2589 | c16b5a2c | Christoph Hellwig | int is_write;
|
2590 | c16b5a2c | Christoph Hellwig | } BlockDriverAIOCBSync; |
2591 | c16b5a2c | Christoph Hellwig | |
2592 | c16b5a2c | Christoph Hellwig | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
2593 | c16b5a2c | Christoph Hellwig | { |
2594 | b666d239 | Kevin Wolf | BlockDriverAIOCBSync *acb = |
2595 | b666d239 | Kevin Wolf | container_of(blockacb, BlockDriverAIOCBSync, common); |
2596 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
2597 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
2598 | c16b5a2c | Christoph Hellwig | qemu_aio_release(acb); |
2599 | c16b5a2c | Christoph Hellwig | } |
2600 | c16b5a2c | Christoph Hellwig | |
2601 | c16b5a2c | Christoph Hellwig | static AIOPool bdrv_em_aio_pool = {
|
2602 | c16b5a2c | Christoph Hellwig | .aiocb_size = sizeof(BlockDriverAIOCBSync),
|
2603 | c16b5a2c | Christoph Hellwig | .cancel = bdrv_aio_cancel_em, |
2604 | c16b5a2c | Christoph Hellwig | }; |
2605 | c16b5a2c | Christoph Hellwig | |
2606 | ce1a14dc | pbrook | static void bdrv_aio_bh_cb(void *opaque) |
2607 | 83f64091 | bellard | { |
2608 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = opaque; |
2609 | f141eafe | aliguori | |
2610 | f141eafe | aliguori | if (!acb->is_write)
|
2611 | f141eafe | aliguori | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
2612 | ceb42de8 | aliguori | qemu_vfree(acb->bounce); |
2613 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, acb->ret); |
2614 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
2615 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
2616 | ce1a14dc | pbrook | qemu_aio_release(acb); |
2617 | 83f64091 | bellard | } |
2618 | beac80cd | bellard | |
2619 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
|
2620 | f141eafe | aliguori | int64_t sector_num, |
2621 | f141eafe | aliguori | QEMUIOVector *qiov, |
2622 | f141eafe | aliguori | int nb_sectors,
|
2623 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, |
2624 | f141eafe | aliguori | void *opaque,
|
2625 | f141eafe | aliguori | int is_write)
|
2626 | f141eafe | aliguori | |
2627 | 83f64091 | bellard | { |
2628 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
2629 | ce1a14dc | pbrook | |
2630 | c16b5a2c | Christoph Hellwig | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
2631 | f141eafe | aliguori | acb->is_write = is_write; |
2632 | f141eafe | aliguori | acb->qiov = qiov; |
2633 | e268ca52 | aliguori | acb->bounce = qemu_blockalign(bs, qiov->size); |
2634 | f141eafe | aliguori | |
2635 | ce1a14dc | pbrook | if (!acb->bh)
|
2636 | ce1a14dc | pbrook | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
2637 | f141eafe | aliguori | |
2638 | f141eafe | aliguori | if (is_write) {
|
2639 | f141eafe | aliguori | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
2640 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
2641 | f141eafe | aliguori | } else {
|
2642 | 1ed20acf | Stefan Hajnoczi | acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
2643 | f141eafe | aliguori | } |
2644 | f141eafe | aliguori | |
2645 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
2646 | f141eafe | aliguori | |
2647 | ce1a14dc | pbrook | return &acb->common;
|
2648 | beac80cd | bellard | } |
2649 | beac80cd | bellard | |
2650 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
2651 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
2652 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
2653 | beac80cd | bellard | { |
2654 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
2655 | f141eafe | aliguori | } |
2656 | 83f64091 | bellard | |
2657 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
2658 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
2659 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2660 | f141eafe | aliguori | { |
2661 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
2662 | beac80cd | bellard | } |
2663 | beac80cd | bellard | |
2664 | 68485420 | Kevin Wolf | |
2665 | 68485420 | Kevin Wolf | typedef struct BlockDriverAIOCBCoroutine { |
2666 | 68485420 | Kevin Wolf | BlockDriverAIOCB common; |
2667 | 68485420 | Kevin Wolf | BlockRequest req; |
2668 | 68485420 | Kevin Wolf | bool is_write;
|
2669 | 68485420 | Kevin Wolf | QEMUBH* bh; |
2670 | 68485420 | Kevin Wolf | } BlockDriverAIOCBCoroutine; |
2671 | 68485420 | Kevin Wolf | |
2672 | 68485420 | Kevin Wolf | static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) |
2673 | 68485420 | Kevin Wolf | { |
2674 | 68485420 | Kevin Wolf | qemu_aio_flush(); |
2675 | 68485420 | Kevin Wolf | } |
2676 | 68485420 | Kevin Wolf | |
2677 | 68485420 | Kevin Wolf | static AIOPool bdrv_em_co_aio_pool = {
|
2678 | 68485420 | Kevin Wolf | .aiocb_size = sizeof(BlockDriverAIOCBCoroutine),
|
2679 | 68485420 | Kevin Wolf | .cancel = bdrv_aio_co_cancel_em, |
2680 | 68485420 | Kevin Wolf | }; |
2681 | 68485420 | Kevin Wolf | |
2682 | 35246a68 | Paolo Bonzini | static void bdrv_co_em_bh(void *opaque) |
2683 | 68485420 | Kevin Wolf | { |
2684 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb = opaque; |
2685 | 68485420 | Kevin Wolf | |
2686 | 68485420 | Kevin Wolf | acb->common.cb(acb->common.opaque, acb->req.error); |
2687 | 68485420 | Kevin Wolf | qemu_bh_delete(acb->bh); |
2688 | 68485420 | Kevin Wolf | qemu_aio_release(acb); |
2689 | 68485420 | Kevin Wolf | } |
2690 | 68485420 | Kevin Wolf | |
2691 | b2a61371 | Stefan Hajnoczi | /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
|
2692 | b2a61371 | Stefan Hajnoczi | static void coroutine_fn bdrv_co_do_rw(void *opaque) |
2693 | b2a61371 | Stefan Hajnoczi | { |
2694 | b2a61371 | Stefan Hajnoczi | BlockDriverAIOCBCoroutine *acb = opaque; |
2695 | b2a61371 | Stefan Hajnoczi | BlockDriverState *bs = acb->common.bs; |
2696 | b2a61371 | Stefan Hajnoczi | |
2697 | b2a61371 | Stefan Hajnoczi | if (!acb->is_write) {
|
2698 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, |
2699 | b2a61371 | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov); |
2700 | b2a61371 | Stefan Hajnoczi | } else {
|
2701 | b2a61371 | Stefan Hajnoczi | acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, |
2702 | b2a61371 | Stefan Hajnoczi | acb->req.nb_sectors, acb->req.qiov); |
2703 | b2a61371 | Stefan Hajnoczi | } |
2704 | b2a61371 | Stefan Hajnoczi | |
2705 | 35246a68 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
2706 | b2a61371 | Stefan Hajnoczi | qemu_bh_schedule(acb->bh); |
2707 | b2a61371 | Stefan Hajnoczi | } |
2708 | b2a61371 | Stefan Hajnoczi | |
2709 | 68485420 | Kevin Wolf | static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
|
2710 | 68485420 | Kevin Wolf | int64_t sector_num, |
2711 | 68485420 | Kevin Wolf | QEMUIOVector *qiov, |
2712 | 68485420 | Kevin Wolf | int nb_sectors,
|
2713 | 68485420 | Kevin Wolf | BlockDriverCompletionFunc *cb, |
2714 | 68485420 | Kevin Wolf | void *opaque,
|
2715 | 8c5873d6 | Stefan Hajnoczi | bool is_write)
|
2716 | 68485420 | Kevin Wolf | { |
2717 | 68485420 | Kevin Wolf | Coroutine *co; |
2718 | 68485420 | Kevin Wolf | BlockDriverAIOCBCoroutine *acb; |
2719 | 68485420 | Kevin Wolf | |
2720 | 68485420 | Kevin Wolf | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
2721 | 68485420 | Kevin Wolf | acb->req.sector = sector_num; |
2722 | 68485420 | Kevin Wolf | acb->req.nb_sectors = nb_sectors; |
2723 | 68485420 | Kevin Wolf | acb->req.qiov = qiov; |
2724 | 68485420 | Kevin Wolf | acb->is_write = is_write; |
2725 | 68485420 | Kevin Wolf | |
2726 | 8c5873d6 | Stefan Hajnoczi | co = qemu_coroutine_create(bdrv_co_do_rw); |
2727 | 68485420 | Kevin Wolf | qemu_coroutine_enter(co, acb); |
2728 | 68485420 | Kevin Wolf | |
2729 | 68485420 | Kevin Wolf | return &acb->common;
|
2730 | 68485420 | Kevin Wolf | } |
2731 | 68485420 | Kevin Wolf | |
2732 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) |
2733 | b2e12bc6 | Christoph Hellwig | { |
2734 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb = opaque; |
2735 | 07f07615 | Paolo Bonzini | BlockDriverState *bs = acb->common.bs; |
2736 | b2e12bc6 | Christoph Hellwig | |
2737 | 07f07615 | Paolo Bonzini | acb->req.error = bdrv_co_flush(bs); |
2738 | 07f07615 | Paolo Bonzini | acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); |
2739 | b2e12bc6 | Christoph Hellwig | qemu_bh_schedule(acb->bh); |
2740 | b2e12bc6 | Christoph Hellwig | } |
2741 | b2e12bc6 | Christoph Hellwig | |
2742 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
2743 | 016f5cf6 | Alexander Graf | BlockDriverCompletionFunc *cb, void *opaque)
|
2744 | 016f5cf6 | Alexander Graf | { |
2745 | 07f07615 | Paolo Bonzini | trace_bdrv_aio_flush(bs, opaque); |
2746 | 016f5cf6 | Alexander Graf | |
2747 | 07f07615 | Paolo Bonzini | Coroutine *co; |
2748 | 07f07615 | Paolo Bonzini | BlockDriverAIOCBCoroutine *acb; |
2749 | 016f5cf6 | Alexander Graf | |
2750 | 07f07615 | Paolo Bonzini | acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); |
2751 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_aio_flush_co_entry); |
2752 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, acb); |
2753 | 016f5cf6 | Alexander Graf | |
2754 | 016f5cf6 | Alexander Graf | return &acb->common;
|
2755 | 016f5cf6 | Alexander Graf | } |
2756 | 016f5cf6 | Alexander Graf | |
2757 | ea2384d3 | bellard | void bdrv_init(void) |
2758 | ea2384d3 | bellard | { |
2759 | 5efa9d5a | Anthony Liguori | module_call_init(MODULE_INIT_BLOCK); |
2760 | ea2384d3 | bellard | } |
2761 | ce1a14dc | pbrook | |
2762 | eb852011 | Markus Armbruster | void bdrv_init_with_whitelist(void) |
2763 | eb852011 | Markus Armbruster | { |
2764 | eb852011 | Markus Armbruster | use_bdrv_whitelist = 1;
|
2765 | eb852011 | Markus Armbruster | bdrv_init(); |
2766 | eb852011 | Markus Armbruster | } |
2767 | eb852011 | Markus Armbruster | |
2768 | c16b5a2c | Christoph Hellwig | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
|
2769 | c16b5a2c | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque)
|
2770 | ce1a14dc | pbrook | { |
2771 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
2772 | ce1a14dc | pbrook | |
2773 | 6bbff9a0 | aliguori | if (pool->free_aiocb) {
|
2774 | 6bbff9a0 | aliguori | acb = pool->free_aiocb; |
2775 | 6bbff9a0 | aliguori | pool->free_aiocb = acb->next; |
2776 | ce1a14dc | pbrook | } else {
|
2777 | 7267c094 | Anthony Liguori | acb = g_malloc0(pool->aiocb_size); |
2778 | 6bbff9a0 | aliguori | acb->pool = pool; |
2779 | ce1a14dc | pbrook | } |
2780 | ce1a14dc | pbrook | acb->bs = bs; |
2781 | ce1a14dc | pbrook | acb->cb = cb; |
2782 | ce1a14dc | pbrook | acb->opaque = opaque; |
2783 | ce1a14dc | pbrook | return acb;
|
2784 | ce1a14dc | pbrook | } |
2785 | ce1a14dc | pbrook | |
2786 | ce1a14dc | pbrook | void qemu_aio_release(void *p) |
2787 | ce1a14dc | pbrook | { |
2788 | 6bbff9a0 | aliguori | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
2789 | 6bbff9a0 | aliguori | AIOPool *pool = acb->pool; |
2790 | 6bbff9a0 | aliguori | acb->next = pool->free_aiocb; |
2791 | 6bbff9a0 | aliguori | pool->free_aiocb = acb; |
2792 | ce1a14dc | pbrook | } |
2793 | 19cb3738 | bellard | |
2794 | 19cb3738 | bellard | /**************************************************************/
|
2795 | f9f05dc5 | Kevin Wolf | /* Coroutine block device emulation */
|
2796 | f9f05dc5 | Kevin Wolf | |
2797 | f9f05dc5 | Kevin Wolf | typedef struct CoroutineIOCompletion { |
2798 | f9f05dc5 | Kevin Wolf | Coroutine *coroutine; |
2799 | f9f05dc5 | Kevin Wolf | int ret;
|
2800 | f9f05dc5 | Kevin Wolf | } CoroutineIOCompletion; |
2801 | f9f05dc5 | Kevin Wolf | |
2802 | f9f05dc5 | Kevin Wolf | static void bdrv_co_io_em_complete(void *opaque, int ret) |
2803 | f9f05dc5 | Kevin Wolf | { |
2804 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion *co = opaque; |
2805 | f9f05dc5 | Kevin Wolf | |
2806 | f9f05dc5 | Kevin Wolf | co->ret = ret; |
2807 | f9f05dc5 | Kevin Wolf | qemu_coroutine_enter(co->coroutine, NULL);
|
2808 | f9f05dc5 | Kevin Wolf | } |
2809 | f9f05dc5 | Kevin Wolf | |
2810 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, |
2811 | f9f05dc5 | Kevin Wolf | int nb_sectors, QEMUIOVector *iov,
|
2812 | f9f05dc5 | Kevin Wolf | bool is_write)
|
2813 | f9f05dc5 | Kevin Wolf | { |
2814 | f9f05dc5 | Kevin Wolf | CoroutineIOCompletion co = { |
2815 | f9f05dc5 | Kevin Wolf | .coroutine = qemu_coroutine_self(), |
2816 | f9f05dc5 | Kevin Wolf | }; |
2817 | f9f05dc5 | Kevin Wolf | BlockDriverAIOCB *acb; |
2818 | f9f05dc5 | Kevin Wolf | |
2819 | f9f05dc5 | Kevin Wolf | if (is_write) {
|
2820 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, |
2821 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
2822 | f9f05dc5 | Kevin Wolf | } else {
|
2823 | a652d160 | Stefan Hajnoczi | acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, |
2824 | a652d160 | Stefan Hajnoczi | bdrv_co_io_em_complete, &co); |
2825 | f9f05dc5 | Kevin Wolf | } |
2826 | f9f05dc5 | Kevin Wolf | |
2827 | 59370aaa | Stefan Hajnoczi | trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); |
2828 | f9f05dc5 | Kevin Wolf | if (!acb) {
|
2829 | f9f05dc5 | Kevin Wolf | return -EIO;
|
2830 | f9f05dc5 | Kevin Wolf | } |
2831 | f9f05dc5 | Kevin Wolf | qemu_coroutine_yield(); |
2832 | f9f05dc5 | Kevin Wolf | |
2833 | f9f05dc5 | Kevin Wolf | return co.ret;
|
2834 | f9f05dc5 | Kevin Wolf | } |
2835 | f9f05dc5 | Kevin Wolf | |
2836 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, |
2837 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
2838 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov) |
2839 | f9f05dc5 | Kevin Wolf | { |
2840 | f9f05dc5 | Kevin Wolf | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); |
2841 | f9f05dc5 | Kevin Wolf | } |
2842 | f9f05dc5 | Kevin Wolf | |
2843 | f9f05dc5 | Kevin Wolf | static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, |
2844 | f9f05dc5 | Kevin Wolf | int64_t sector_num, int nb_sectors,
|
2845 | f9f05dc5 | Kevin Wolf | QEMUIOVector *iov) |
2846 | f9f05dc5 | Kevin Wolf | { |
2847 | f9f05dc5 | Kevin Wolf | return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); |
2848 | f9f05dc5 | Kevin Wolf | } |
2849 | f9f05dc5 | Kevin Wolf | |
2850 | 07f07615 | Paolo Bonzini | static void coroutine_fn bdrv_flush_co_entry(void *opaque) |
2851 | e7a8a783 | Kevin Wolf | { |
2852 | 07f07615 | Paolo Bonzini | RwCo *rwco = opaque; |
2853 | 07f07615 | Paolo Bonzini | |
2854 | 07f07615 | Paolo Bonzini | rwco->ret = bdrv_co_flush(rwco->bs); |
2855 | 07f07615 | Paolo Bonzini | } |
2856 | 07f07615 | Paolo Bonzini | |
2857 | 07f07615 | Paolo Bonzini | int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
|
2858 | 07f07615 | Paolo Bonzini | { |
2859 | 07f07615 | Paolo Bonzini | if (bs->open_flags & BDRV_O_NO_FLUSH) {
|
2860 | 07f07615 | Paolo Bonzini | return 0; |
2861 | 07f07615 | Paolo Bonzini | } else if (!bs->drv) { |
2862 | 07f07615 | Paolo Bonzini | return 0; |
2863 | 07f07615 | Paolo Bonzini | } else if (bs->drv->bdrv_co_flush) { |
2864 | 07f07615 | Paolo Bonzini | return bs->drv->bdrv_co_flush(bs);
|
2865 | 07f07615 | Paolo Bonzini | } else if (bs->drv->bdrv_aio_flush) { |
2866 | 07f07615 | Paolo Bonzini | BlockDriverAIOCB *acb; |
2867 | 07f07615 | Paolo Bonzini | CoroutineIOCompletion co = { |
2868 | 07f07615 | Paolo Bonzini | .coroutine = qemu_coroutine_self(), |
2869 | 07f07615 | Paolo Bonzini | }; |
2870 | 07f07615 | Paolo Bonzini | |
2871 | 07f07615 | Paolo Bonzini | acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); |
2872 | 07f07615 | Paolo Bonzini | if (acb == NULL) { |
2873 | 07f07615 | Paolo Bonzini | return -EIO;
|
2874 | 07f07615 | Paolo Bonzini | } else {
|
2875 | 07f07615 | Paolo Bonzini | qemu_coroutine_yield(); |
2876 | 07f07615 | Paolo Bonzini | return co.ret;
|
2877 | 07f07615 | Paolo Bonzini | } |
2878 | 07f07615 | Paolo Bonzini | } else if (bs->drv->bdrv_flush) { |
2879 | 07f07615 | Paolo Bonzini | return bs->drv->bdrv_flush(bs);
|
2880 | 07f07615 | Paolo Bonzini | } else {
|
2881 | 07f07615 | Paolo Bonzini | /*
|
2882 | 07f07615 | Paolo Bonzini | * Some block drivers always operate in either writethrough or unsafe
|
2883 | 07f07615 | Paolo Bonzini | * mode and don't support bdrv_flush therefore. Usually qemu doesn't
|
2884 | 07f07615 | Paolo Bonzini | * know how the server works (because the behaviour is hardcoded or
|
2885 | 07f07615 | Paolo Bonzini | * depends on server-side configuration), so we can't ensure that
|
2886 | 07f07615 | Paolo Bonzini | * everything is safe on disk. Returning an error doesn't work because
|
2887 | 07f07615 | Paolo Bonzini | * that would break guests even if the server operates in writethrough
|
2888 | 07f07615 | Paolo Bonzini | * mode.
|
2889 | 07f07615 | Paolo Bonzini | *
|
2890 | 07f07615 | Paolo Bonzini | * Let's hope the user knows what he's doing.
|
2891 | 07f07615 | Paolo Bonzini | */
|
2892 | 07f07615 | Paolo Bonzini | return 0; |
2893 | 07f07615 | Paolo Bonzini | } |
2894 | 07f07615 | Paolo Bonzini | } |
2895 | 07f07615 | Paolo Bonzini | |
2896 | 07f07615 | Paolo Bonzini | int bdrv_flush(BlockDriverState *bs)
|
2897 | 07f07615 | Paolo Bonzini | { |
2898 | 07f07615 | Paolo Bonzini | Coroutine *co; |
2899 | 07f07615 | Paolo Bonzini | RwCo rwco = { |
2900 | 07f07615 | Paolo Bonzini | .bs = bs, |
2901 | 07f07615 | Paolo Bonzini | .ret = NOT_DONE, |
2902 | e7a8a783 | Kevin Wolf | }; |
2903 | e7a8a783 | Kevin Wolf | |
2904 | 07f07615 | Paolo Bonzini | if (qemu_in_coroutine()) {
|
2905 | 07f07615 | Paolo Bonzini | /* Fast-path if already in coroutine context */
|
2906 | 07f07615 | Paolo Bonzini | bdrv_flush_co_entry(&rwco); |
2907 | 07f07615 | Paolo Bonzini | } else {
|
2908 | 07f07615 | Paolo Bonzini | co = qemu_coroutine_create(bdrv_flush_co_entry); |
2909 | 07f07615 | Paolo Bonzini | qemu_coroutine_enter(co, &rwco); |
2910 | 07f07615 | Paolo Bonzini | while (rwco.ret == NOT_DONE) {
|
2911 | 07f07615 | Paolo Bonzini | qemu_aio_wait(); |
2912 | 07f07615 | Paolo Bonzini | } |
2913 | e7a8a783 | Kevin Wolf | } |
2914 | 07f07615 | Paolo Bonzini | |
2915 | 07f07615 | Paolo Bonzini | return rwco.ret;
|
2916 | e7a8a783 | Kevin Wolf | } |
2917 | e7a8a783 | Kevin Wolf | |
2918 | f9f05dc5 | Kevin Wolf | /**************************************************************/
|
2919 | 19cb3738 | bellard | /* removable device support */
|
2920 | 19cb3738 | bellard | |
2921 | 19cb3738 | bellard | /**
|
2922 | 19cb3738 | bellard | * Return TRUE if the media is present
|
2923 | 19cb3738 | bellard | */
|
2924 | 19cb3738 | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
2925 | 19cb3738 | bellard | { |
2926 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2927 | a1aff5bf | Markus Armbruster | |
2928 | 19cb3738 | bellard | if (!drv)
|
2929 | 19cb3738 | bellard | return 0; |
2930 | 19cb3738 | bellard | if (!drv->bdrv_is_inserted)
|
2931 | a1aff5bf | Markus Armbruster | return 1; |
2932 | a1aff5bf | Markus Armbruster | return drv->bdrv_is_inserted(bs);
|
2933 | 19cb3738 | bellard | } |
2934 | 19cb3738 | bellard | |
2935 | 19cb3738 | bellard | /**
|
2936 | 8e49ca46 | Markus Armbruster | * Return whether the media changed since the last call to this
|
2937 | 8e49ca46 | Markus Armbruster | * function, or -ENOTSUP if we don't know. Most drivers don't know.
|
2938 | 19cb3738 | bellard | */
|
2939 | 19cb3738 | bellard | int bdrv_media_changed(BlockDriverState *bs)
|
2940 | 19cb3738 | bellard | { |
2941 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2942 | 19cb3738 | bellard | |
2943 | 8e49ca46 | Markus Armbruster | if (drv && drv->bdrv_media_changed) {
|
2944 | 8e49ca46 | Markus Armbruster | return drv->bdrv_media_changed(bs);
|
2945 | 8e49ca46 | Markus Armbruster | } |
2946 | 8e49ca46 | Markus Armbruster | return -ENOTSUP;
|
2947 | 19cb3738 | bellard | } |
2948 | 19cb3738 | bellard | |
2949 | 19cb3738 | bellard | /**
|
2950 | 19cb3738 | bellard | * If eject_flag is TRUE, eject the media. Otherwise, close the tray
|
2951 | 19cb3738 | bellard | */
|
2952 | fdec4404 | Markus Armbruster | void bdrv_eject(BlockDriverState *bs, int eject_flag) |
2953 | 19cb3738 | bellard | { |
2954 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2955 | 19cb3738 | bellard | |
2956 | 822e1cd1 | Markus Armbruster | if (drv && drv->bdrv_eject) {
|
2957 | 822e1cd1 | Markus Armbruster | drv->bdrv_eject(bs, eject_flag); |
2958 | 19cb3738 | bellard | } |
2959 | 19cb3738 | bellard | } |
2960 | 19cb3738 | bellard | |
2961 | 19cb3738 | bellard | /**
|
2962 | 19cb3738 | bellard | * Lock or unlock the media (if it is locked, the user won't be able
|
2963 | 19cb3738 | bellard | * to eject it manually).
|
2964 | 19cb3738 | bellard | */
|
2965 | 025e849a | Markus Armbruster | void bdrv_lock_medium(BlockDriverState *bs, bool locked) |
2966 | 19cb3738 | bellard | { |
2967 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2968 | 19cb3738 | bellard | |
2969 | 025e849a | Markus Armbruster | trace_bdrv_lock_medium(bs, locked); |
2970 | b8c6d095 | Stefan Hajnoczi | |
2971 | 025e849a | Markus Armbruster | if (drv && drv->bdrv_lock_medium) {
|
2972 | 025e849a | Markus Armbruster | drv->bdrv_lock_medium(bs, locked); |
2973 | 19cb3738 | bellard | } |
2974 | 19cb3738 | bellard | } |
2975 | 985a03b0 | ths | |
2976 | 985a03b0 | ths | /* needed for generic scsi interface */
|
2977 | 985a03b0 | ths | |
2978 | 985a03b0 | ths | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
2979 | 985a03b0 | ths | { |
2980 | 985a03b0 | ths | BlockDriver *drv = bs->drv; |
2981 | 985a03b0 | ths | |
2982 | 985a03b0 | ths | if (drv && drv->bdrv_ioctl)
|
2983 | 985a03b0 | ths | return drv->bdrv_ioctl(bs, req, buf);
|
2984 | 985a03b0 | ths | return -ENOTSUP;
|
2985 | 985a03b0 | ths | } |
2986 | 7d780669 | aliguori | |
2987 | 221f715d | aliguori | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
2988 | 221f715d | aliguori | unsigned long int req, void *buf, |
2989 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2990 | 7d780669 | aliguori | { |
2991 | 221f715d | aliguori | BlockDriver *drv = bs->drv; |
2992 | 7d780669 | aliguori | |
2993 | 221f715d | aliguori | if (drv && drv->bdrv_aio_ioctl)
|
2994 | 221f715d | aliguori | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
|
2995 | 221f715d | aliguori | return NULL; |
2996 | 7d780669 | aliguori | } |
2997 | e268ca52 | aliguori | |
2998 | 7b6f9300 | Markus Armbruster | void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) |
2999 | 7b6f9300 | Markus Armbruster | { |
3000 | 7b6f9300 | Markus Armbruster | bs->buffer_alignment = align; |
3001 | 7b6f9300 | Markus Armbruster | } |
3002 | 7cd1e32a | lirans@il.ibm.com | |
3003 | e268ca52 | aliguori | void *qemu_blockalign(BlockDriverState *bs, size_t size)
|
3004 | e268ca52 | aliguori | { |
3005 | e268ca52 | aliguori | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
3006 | e268ca52 | aliguori | } |
3007 | 7cd1e32a | lirans@il.ibm.com | |
3008 | 7cd1e32a | lirans@il.ibm.com | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) |
3009 | 7cd1e32a | lirans@il.ibm.com | { |
3010 | 7cd1e32a | lirans@il.ibm.com | int64_t bitmap_size; |
3011 | a55eb92c | Jan Kiszka | |
3012 | aaa0eb75 | Liran Schour | bs->dirty_count = 0;
|
3013 | a55eb92c | Jan Kiszka | if (enable) {
|
3014 | c6d22830 | Jan Kiszka | if (!bs->dirty_bitmap) {
|
3015 | c6d22830 | Jan Kiszka | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + |
3016 | c6d22830 | Jan Kiszka | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
3017 | c6d22830 | Jan Kiszka | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
|
3018 | a55eb92c | Jan Kiszka | |
3019 | 7267c094 | Anthony Liguori | bs->dirty_bitmap = g_malloc0(bitmap_size); |
3020 | a55eb92c | Jan Kiszka | } |
3021 | 7cd1e32a | lirans@il.ibm.com | } else {
|
3022 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
3023 | 7267c094 | Anthony Liguori | g_free(bs->dirty_bitmap); |
3024 | c6d22830 | Jan Kiszka | bs->dirty_bitmap = NULL;
|
3025 | a55eb92c | Jan Kiszka | } |
3026 | 7cd1e32a | lirans@il.ibm.com | } |
3027 | 7cd1e32a | lirans@il.ibm.com | } |
3028 | 7cd1e32a | lirans@il.ibm.com | |
3029 | 7cd1e32a | lirans@il.ibm.com | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
|
3030 | 7cd1e32a | lirans@il.ibm.com | { |
3031 | 6ea44308 | Jan Kiszka | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
3032 | a55eb92c | Jan Kiszka | |
3033 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap &&
|
3034 | c6d22830 | Jan Kiszka | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { |
3035 | 6d59fec1 | Marcelo Tosatti | return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
3036 | 6d59fec1 | Marcelo Tosatti | (1UL << (chunk % (sizeof(unsigned long) * 8)))); |
3037 | 7cd1e32a | lirans@il.ibm.com | } else {
|
3038 | 7cd1e32a | lirans@il.ibm.com | return 0; |
3039 | 7cd1e32a | lirans@il.ibm.com | } |
3040 | 7cd1e32a | lirans@il.ibm.com | } |
3041 | 7cd1e32a | lirans@il.ibm.com | |
3042 | a55eb92c | Jan Kiszka | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
|
3043 | a55eb92c | Jan Kiszka | int nr_sectors)
|
3044 | 7cd1e32a | lirans@il.ibm.com | { |
3045 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
|
3046 | 7cd1e32a | lirans@il.ibm.com | } |
3047 | aaa0eb75 | Liran Schour | |
3048 | aaa0eb75 | Liran Schour | int64_t bdrv_get_dirty_count(BlockDriverState *bs) |
3049 | aaa0eb75 | Liran Schour | { |
3050 | aaa0eb75 | Liran Schour | return bs->dirty_count;
|
3051 | aaa0eb75 | Liran Schour | } |
3052 | f88e1a42 | Jes Sorensen | |
3053 | db593f25 | Marcelo Tosatti | void bdrv_set_in_use(BlockDriverState *bs, int in_use) |
3054 | db593f25 | Marcelo Tosatti | { |
3055 | db593f25 | Marcelo Tosatti | assert(bs->in_use != in_use); |
3056 | db593f25 | Marcelo Tosatti | bs->in_use = in_use; |
3057 | db593f25 | Marcelo Tosatti | } |
3058 | db593f25 | Marcelo Tosatti | |
3059 | db593f25 | Marcelo Tosatti | int bdrv_in_use(BlockDriverState *bs)
|
3060 | db593f25 | Marcelo Tosatti | { |
3061 | db593f25 | Marcelo Tosatti | return bs->in_use;
|
3062 | db593f25 | Marcelo Tosatti | } |
3063 | db593f25 | Marcelo Tosatti | |
3064 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_enable(BlockDriverState *bs)
|
3065 | 28a7282a | Luiz Capitulino | { |
3066 | 28a7282a | Luiz Capitulino | bs->iostatus = BDRV_IOS_OK; |
3067 | 28a7282a | Luiz Capitulino | } |
3068 | 28a7282a | Luiz Capitulino | |
3069 | 28a7282a | Luiz Capitulino | /* The I/O status is only enabled if the drive explicitly
|
3070 | 28a7282a | Luiz Capitulino | * enables it _and_ the VM is configured to stop on errors */
|
3071 | 28a7282a | Luiz Capitulino | bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) |
3072 | 28a7282a | Luiz Capitulino | { |
3073 | 28a7282a | Luiz Capitulino | return (bs->iostatus != BDRV_IOS_INVAL &&
|
3074 | 28a7282a | Luiz Capitulino | (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || |
3075 | 28a7282a | Luiz Capitulino | bs->on_write_error == BLOCK_ERR_STOP_ANY || |
3076 | 28a7282a | Luiz Capitulino | bs->on_read_error == BLOCK_ERR_STOP_ANY)); |
3077 | 28a7282a | Luiz Capitulino | } |
3078 | 28a7282a | Luiz Capitulino | |
3079 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_disable(BlockDriverState *bs)
|
3080 | 28a7282a | Luiz Capitulino | { |
3081 | 28a7282a | Luiz Capitulino | bs->iostatus = BDRV_IOS_INVAL; |
3082 | 28a7282a | Luiz Capitulino | } |
3083 | 28a7282a | Luiz Capitulino | |
3084 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_reset(BlockDriverState *bs)
|
3085 | 28a7282a | Luiz Capitulino | { |
3086 | 28a7282a | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs)) {
|
3087 | 28a7282a | Luiz Capitulino | bs->iostatus = BDRV_IOS_OK; |
3088 | 28a7282a | Luiz Capitulino | } |
3089 | 28a7282a | Luiz Capitulino | } |
3090 | 28a7282a | Luiz Capitulino | |
3091 | 28a7282a | Luiz Capitulino | /* XXX: Today this is set by device models because it makes the implementation
|
3092 | 28a7282a | Luiz Capitulino | quite simple. However, the block layer knows about the error, so it's
|
3093 | 28a7282a | Luiz Capitulino | possible to implement this without device models being involved */
|
3094 | 28a7282a | Luiz Capitulino | void bdrv_iostatus_set_err(BlockDriverState *bs, int error) |
3095 | 28a7282a | Luiz Capitulino | { |
3096 | 28a7282a | Luiz Capitulino | if (bdrv_iostatus_is_enabled(bs) && bs->iostatus == BDRV_IOS_OK) {
|
3097 | 28a7282a | Luiz Capitulino | assert(error >= 0);
|
3098 | 28a7282a | Luiz Capitulino | bs->iostatus = error == ENOSPC ? BDRV_IOS_ENOSPC : BDRV_IOS_FAILED; |
3099 | 28a7282a | Luiz Capitulino | } |
3100 | 28a7282a | Luiz Capitulino | } |
3101 | 28a7282a | Luiz Capitulino | |
3102 | a597e79c | Christoph Hellwig | void
|
3103 | a597e79c | Christoph Hellwig | bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, |
3104 | a597e79c | Christoph Hellwig | enum BlockAcctType type)
|
3105 | a597e79c | Christoph Hellwig | { |
3106 | a597e79c | Christoph Hellwig | assert(type < BDRV_MAX_IOTYPE); |
3107 | a597e79c | Christoph Hellwig | |
3108 | a597e79c | Christoph Hellwig | cookie->bytes = bytes; |
3109 | c488c7f6 | Christoph Hellwig | cookie->start_time_ns = get_clock(); |
3110 | a597e79c | Christoph Hellwig | cookie->type = type; |
3111 | a597e79c | Christoph Hellwig | } |
3112 | a597e79c | Christoph Hellwig | |
3113 | a597e79c | Christoph Hellwig | void
|
3114 | a597e79c | Christoph Hellwig | bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) |
3115 | a597e79c | Christoph Hellwig | { |
3116 | a597e79c | Christoph Hellwig | assert(cookie->type < BDRV_MAX_IOTYPE); |
3117 | a597e79c | Christoph Hellwig | |
3118 | a597e79c | Christoph Hellwig | bs->nr_bytes[cookie->type] += cookie->bytes; |
3119 | a597e79c | Christoph Hellwig | bs->nr_ops[cookie->type]++; |
3120 | c488c7f6 | Christoph Hellwig | bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; |
3121 | a597e79c | Christoph Hellwig | } |
3122 | a597e79c | Christoph Hellwig | |
3123 | f88e1a42 | Jes Sorensen | int bdrv_img_create(const char *filename, const char *fmt, |
3124 | f88e1a42 | Jes Sorensen | const char *base_filename, const char *base_fmt, |
3125 | f88e1a42 | Jes Sorensen | char *options, uint64_t img_size, int flags) |
3126 | f88e1a42 | Jes Sorensen | { |
3127 | f88e1a42 | Jes Sorensen | QEMUOptionParameter *param = NULL, *create_options = NULL; |
3128 | d220894e | Kevin Wolf | QEMUOptionParameter *backing_fmt, *backing_file, *size; |
3129 | f88e1a42 | Jes Sorensen | BlockDriverState *bs = NULL;
|
3130 | f88e1a42 | Jes Sorensen | BlockDriver *drv, *proto_drv; |
3131 | 96df67d1 | Stefan Hajnoczi | BlockDriver *backing_drv = NULL;
|
3132 | f88e1a42 | Jes Sorensen | int ret = 0; |
3133 | f88e1a42 | Jes Sorensen | |
3134 | f88e1a42 | Jes Sorensen | /* Find driver and parse its options */
|
3135 | f88e1a42 | Jes Sorensen | drv = bdrv_find_format(fmt); |
3136 | f88e1a42 | Jes Sorensen | if (!drv) {
|
3137 | f88e1a42 | Jes Sorensen | error_report("Unknown file format '%s'", fmt);
|
3138 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3139 | f88e1a42 | Jes Sorensen | goto out;
|
3140 | f88e1a42 | Jes Sorensen | } |
3141 | f88e1a42 | Jes Sorensen | |
3142 | f88e1a42 | Jes Sorensen | proto_drv = bdrv_find_protocol(filename); |
3143 | f88e1a42 | Jes Sorensen | if (!proto_drv) {
|
3144 | f88e1a42 | Jes Sorensen | error_report("Unknown protocol '%s'", filename);
|
3145 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3146 | f88e1a42 | Jes Sorensen | goto out;
|
3147 | f88e1a42 | Jes Sorensen | } |
3148 | f88e1a42 | Jes Sorensen | |
3149 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
3150 | f88e1a42 | Jes Sorensen | drv->create_options); |
3151 | f88e1a42 | Jes Sorensen | create_options = append_option_parameters(create_options, |
3152 | f88e1a42 | Jes Sorensen | proto_drv->create_options); |
3153 | f88e1a42 | Jes Sorensen | |
3154 | f88e1a42 | Jes Sorensen | /* Create parameter list with default values */
|
3155 | f88e1a42 | Jes Sorensen | param = parse_option_parameters("", create_options, param);
|
3156 | f88e1a42 | Jes Sorensen | |
3157 | f88e1a42 | Jes Sorensen | set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); |
3158 | f88e1a42 | Jes Sorensen | |
3159 | f88e1a42 | Jes Sorensen | /* Parse -o options */
|
3160 | f88e1a42 | Jes Sorensen | if (options) {
|
3161 | f88e1a42 | Jes Sorensen | param = parse_option_parameters(options, create_options, param); |
3162 | f88e1a42 | Jes Sorensen | if (param == NULL) { |
3163 | f88e1a42 | Jes Sorensen | error_report("Invalid options for file format '%s'.", fmt);
|
3164 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3165 | f88e1a42 | Jes Sorensen | goto out;
|
3166 | f88e1a42 | Jes Sorensen | } |
3167 | f88e1a42 | Jes Sorensen | } |
3168 | f88e1a42 | Jes Sorensen | |
3169 | f88e1a42 | Jes Sorensen | if (base_filename) {
|
3170 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
|
3171 | f88e1a42 | Jes Sorensen | base_filename)) { |
3172 | f88e1a42 | Jes Sorensen | error_report("Backing file not supported for file format '%s'",
|
3173 | f88e1a42 | Jes Sorensen | fmt); |
3174 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3175 | f88e1a42 | Jes Sorensen | goto out;
|
3176 | f88e1a42 | Jes Sorensen | } |
3177 | f88e1a42 | Jes Sorensen | } |
3178 | f88e1a42 | Jes Sorensen | |
3179 | f88e1a42 | Jes Sorensen | if (base_fmt) {
|
3180 | f88e1a42 | Jes Sorensen | if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
|
3181 | f88e1a42 | Jes Sorensen | error_report("Backing file format not supported for file "
|
3182 | f88e1a42 | Jes Sorensen | "format '%s'", fmt);
|
3183 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3184 | f88e1a42 | Jes Sorensen | goto out;
|
3185 | f88e1a42 | Jes Sorensen | } |
3186 | f88e1a42 | Jes Sorensen | } |
3187 | f88e1a42 | Jes Sorensen | |
3188 | 792da93a | Jes Sorensen | backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
3189 | 792da93a | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
3190 | 792da93a | Jes Sorensen | if (!strcmp(filename, backing_file->value.s)) {
|
3191 | 792da93a | Jes Sorensen | error_report("Error: Trying to create an image with the "
|
3192 | 792da93a | Jes Sorensen | "same filename as the backing file");
|
3193 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3194 | 792da93a | Jes Sorensen | goto out;
|
3195 | 792da93a | Jes Sorensen | } |
3196 | 792da93a | Jes Sorensen | } |
3197 | 792da93a | Jes Sorensen | |
3198 | f88e1a42 | Jes Sorensen | backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); |
3199 | f88e1a42 | Jes Sorensen | if (backing_fmt && backing_fmt->value.s) {
|
3200 | 96df67d1 | Stefan Hajnoczi | backing_drv = bdrv_find_format(backing_fmt->value.s); |
3201 | 96df67d1 | Stefan Hajnoczi | if (!backing_drv) {
|
3202 | f88e1a42 | Jes Sorensen | error_report("Unknown backing file format '%s'",
|
3203 | f88e1a42 | Jes Sorensen | backing_fmt->value.s); |
3204 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3205 | f88e1a42 | Jes Sorensen | goto out;
|
3206 | f88e1a42 | Jes Sorensen | } |
3207 | f88e1a42 | Jes Sorensen | } |
3208 | f88e1a42 | Jes Sorensen | |
3209 | f88e1a42 | Jes Sorensen | // The size for the image must always be specified, with one exception:
|
3210 | f88e1a42 | Jes Sorensen | // If we are using a backing file, we can obtain the size from there
|
3211 | d220894e | Kevin Wolf | size = get_option_parameter(param, BLOCK_OPT_SIZE); |
3212 | d220894e | Kevin Wolf | if (size && size->value.n == -1) { |
3213 | f88e1a42 | Jes Sorensen | if (backing_file && backing_file->value.s) {
|
3214 | f88e1a42 | Jes Sorensen | uint64_t size; |
3215 | f88e1a42 | Jes Sorensen | char buf[32]; |
3216 | f88e1a42 | Jes Sorensen | |
3217 | f88e1a42 | Jes Sorensen | bs = bdrv_new("");
|
3218 | f88e1a42 | Jes Sorensen | |
3219 | 96df67d1 | Stefan Hajnoczi | ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); |
3220 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
3221 | 96df67d1 | Stefan Hajnoczi | error_report("Could not open '%s'", backing_file->value.s);
|
3222 | f88e1a42 | Jes Sorensen | goto out;
|
3223 | f88e1a42 | Jes Sorensen | } |
3224 | f88e1a42 | Jes Sorensen | bdrv_get_geometry(bs, &size); |
3225 | f88e1a42 | Jes Sorensen | size *= 512;
|
3226 | f88e1a42 | Jes Sorensen | |
3227 | f88e1a42 | Jes Sorensen | snprintf(buf, sizeof(buf), "%" PRId64, size); |
3228 | f88e1a42 | Jes Sorensen | set_option_parameter(param, BLOCK_OPT_SIZE, buf); |
3229 | f88e1a42 | Jes Sorensen | } else {
|
3230 | f88e1a42 | Jes Sorensen | error_report("Image creation needs a size parameter");
|
3231 | 4f70f249 | Jes Sorensen | ret = -EINVAL; |
3232 | f88e1a42 | Jes Sorensen | goto out;
|
3233 | f88e1a42 | Jes Sorensen | } |
3234 | f88e1a42 | Jes Sorensen | } |
3235 | f88e1a42 | Jes Sorensen | |
3236 | f88e1a42 | Jes Sorensen | printf("Formatting '%s', fmt=%s ", filename, fmt);
|
3237 | f88e1a42 | Jes Sorensen | print_option_parameters(param); |
3238 | f88e1a42 | Jes Sorensen | puts("");
|
3239 | f88e1a42 | Jes Sorensen | |
3240 | f88e1a42 | Jes Sorensen | ret = bdrv_create(drv, filename, param); |
3241 | f88e1a42 | Jes Sorensen | |
3242 | f88e1a42 | Jes Sorensen | if (ret < 0) { |
3243 | f88e1a42 | Jes Sorensen | if (ret == -ENOTSUP) {
|
3244 | f88e1a42 | Jes Sorensen | error_report("Formatting or formatting option not supported for "
|
3245 | f88e1a42 | Jes Sorensen | "file format '%s'", fmt);
|
3246 | f88e1a42 | Jes Sorensen | } else if (ret == -EFBIG) { |
3247 | f88e1a42 | Jes Sorensen | error_report("The image size is too large for file format '%s'",
|
3248 | f88e1a42 | Jes Sorensen | fmt); |
3249 | f88e1a42 | Jes Sorensen | } else {
|
3250 | f88e1a42 | Jes Sorensen | error_report("%s: error while creating %s: %s", filename, fmt,
|
3251 | f88e1a42 | Jes Sorensen | strerror(-ret)); |
3252 | f88e1a42 | Jes Sorensen | } |
3253 | f88e1a42 | Jes Sorensen | } |
3254 | f88e1a42 | Jes Sorensen | |
3255 | f88e1a42 | Jes Sorensen | out:
|
3256 | f88e1a42 | Jes Sorensen | free_option_parameters(create_options); |
3257 | f88e1a42 | Jes Sorensen | free_option_parameters(param); |
3258 | f88e1a42 | Jes Sorensen | |
3259 | f88e1a42 | Jes Sorensen | if (bs) {
|
3260 | f88e1a42 | Jes Sorensen | bdrv_delete(bs); |
3261 | f88e1a42 | Jes Sorensen | } |
3262 | 4f70f249 | Jes Sorensen | |
3263 | 4f70f249 | Jes Sorensen | return ret;
|
3264 | f88e1a42 | Jes Sorensen | } |