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