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