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