root / block.c @ 7b88e48b
History | View | Annotate | Download (58.7 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 | 376253ec | aliguori | #include "monitor.h" |
27 | ea2384d3 | bellard | #include "block_int.h" |
28 | 5efa9d5a | Anthony Liguori | #include "module.h" |
29 | d15e5465 | Luiz Capitulino | #include "qemu-objects.h" |
30 | fc01f7e7 | bellard | |
31 | 71e72a19 | Juan Quintela | #ifdef CONFIG_BSD
|
32 | 7674e7bf | bellard | #include <sys/types.h> |
33 | 7674e7bf | bellard | #include <sys/stat.h> |
34 | 7674e7bf | bellard | #include <sys/ioctl.h> |
35 | 72cf2d4f | Blue Swirl | #include <sys/queue.h> |
36 | c5e97233 | blueswir1 | #ifndef __DragonFly__
|
37 | 7674e7bf | bellard | #include <sys/disk.h> |
38 | 7674e7bf | bellard | #endif
|
39 | c5e97233 | blueswir1 | #endif
|
40 | 7674e7bf | bellard | |
41 | 49dc768d | aliguori | #ifdef _WIN32
|
42 | 49dc768d | aliguori | #include <windows.h> |
43 | 49dc768d | aliguori | #endif
|
44 | 49dc768d | aliguori | |
45 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
46 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
47 | c87c0672 | aliguori | BlockDriverCompletionFunc *cb, void *opaque);
|
48 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
49 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
50 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque);
|
51 | b2e12bc6 | Christoph Hellwig | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
|
52 | b2e12bc6 | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque);
|
53 | 5fafdf24 | ths | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
54 | 83f64091 | bellard | uint8_t *buf, int nb_sectors);
|
55 | 83f64091 | bellard | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
56 | 83f64091 | bellard | const uint8_t *buf, int nb_sectors); |
57 | ec530c81 | bellard | |
58 | 7ee930d0 | blueswir1 | BlockDriverState *bdrv_first; |
59 | 7ee930d0 | blueswir1 | |
60 | ea2384d3 | bellard | static BlockDriver *first_drv;
|
61 | ea2384d3 | bellard | |
62 | eb852011 | Markus Armbruster | /* If non-zero, use only whitelisted block drivers */
|
63 | eb852011 | Markus Armbruster | static int use_bdrv_whitelist; |
64 | eb852011 | Markus Armbruster | |
65 | 83f64091 | bellard | int path_is_absolute(const char *path) |
66 | 3b0d4f61 | bellard | { |
67 | 83f64091 | bellard | const char *p; |
68 | 21664424 | bellard | #ifdef _WIN32
|
69 | 21664424 | bellard | /* specific case for names like: "\\.\d:" */
|
70 | 21664424 | bellard | if (*path == '/' || *path == '\\') |
71 | 21664424 | bellard | return 1; |
72 | 21664424 | bellard | #endif
|
73 | 83f64091 | bellard | p = strchr(path, ':');
|
74 | 83f64091 | bellard | if (p)
|
75 | 83f64091 | bellard | p++; |
76 | 83f64091 | bellard | else
|
77 | 83f64091 | bellard | p = path; |
78 | 3b9f94e1 | bellard | #ifdef _WIN32
|
79 | 3b9f94e1 | bellard | return (*p == '/' || *p == '\\'); |
80 | 3b9f94e1 | bellard | #else
|
81 | 3b9f94e1 | bellard | return (*p == '/'); |
82 | 3b9f94e1 | bellard | #endif
|
83 | 3b0d4f61 | bellard | } |
84 | 3b0d4f61 | bellard | |
85 | 83f64091 | bellard | /* if filename is absolute, just copy it to dest. Otherwise, build a
|
86 | 83f64091 | bellard | path to it by considering it is relative to base_path. URL are
|
87 | 83f64091 | bellard | supported. */
|
88 | 83f64091 | bellard | void path_combine(char *dest, int dest_size, |
89 | 83f64091 | bellard | const char *base_path, |
90 | 83f64091 | bellard | const char *filename) |
91 | 3b0d4f61 | bellard | { |
92 | 83f64091 | bellard | const char *p, *p1; |
93 | 83f64091 | bellard | int len;
|
94 | 83f64091 | bellard | |
95 | 83f64091 | bellard | if (dest_size <= 0) |
96 | 83f64091 | bellard | return;
|
97 | 83f64091 | bellard | if (path_is_absolute(filename)) {
|
98 | 83f64091 | bellard | pstrcpy(dest, dest_size, filename); |
99 | 83f64091 | bellard | } else {
|
100 | 83f64091 | bellard | p = strchr(base_path, ':');
|
101 | 83f64091 | bellard | if (p)
|
102 | 83f64091 | bellard | p++; |
103 | 83f64091 | bellard | else
|
104 | 83f64091 | bellard | p = base_path; |
105 | 3b9f94e1 | bellard | p1 = strrchr(base_path, '/');
|
106 | 3b9f94e1 | bellard | #ifdef _WIN32
|
107 | 3b9f94e1 | bellard | { |
108 | 3b9f94e1 | bellard | const char *p2; |
109 | 3b9f94e1 | bellard | p2 = strrchr(base_path, '\\');
|
110 | 3b9f94e1 | bellard | if (!p1 || p2 > p1)
|
111 | 3b9f94e1 | bellard | p1 = p2; |
112 | 3b9f94e1 | bellard | } |
113 | 3b9f94e1 | bellard | #endif
|
114 | 83f64091 | bellard | if (p1)
|
115 | 83f64091 | bellard | p1++; |
116 | 83f64091 | bellard | else
|
117 | 83f64091 | bellard | p1 = base_path; |
118 | 83f64091 | bellard | if (p1 > p)
|
119 | 83f64091 | bellard | p = p1; |
120 | 83f64091 | bellard | len = p - base_path; |
121 | 83f64091 | bellard | if (len > dest_size - 1) |
122 | 83f64091 | bellard | len = dest_size - 1;
|
123 | 83f64091 | bellard | memcpy(dest, base_path, len); |
124 | 83f64091 | bellard | dest[len] = '\0';
|
125 | 83f64091 | bellard | pstrcat(dest, dest_size, filename); |
126 | 3b0d4f61 | bellard | } |
127 | 3b0d4f61 | bellard | } |
128 | 3b0d4f61 | bellard | |
129 | 5efa9d5a | Anthony Liguori | void bdrv_register(BlockDriver *bdrv)
|
130 | ea2384d3 | bellard | { |
131 | f141eafe | aliguori | if (!bdrv->bdrv_aio_readv) {
|
132 | 83f64091 | bellard | /* add AIO emulation layer */
|
133 | f141eafe | aliguori | bdrv->bdrv_aio_readv = bdrv_aio_readv_em; |
134 | f141eafe | aliguori | bdrv->bdrv_aio_writev = bdrv_aio_writev_em; |
135 | eda578e5 | aliguori | } else if (!bdrv->bdrv_read) { |
136 | 83f64091 | bellard | /* add synchronous IO emulation layer */
|
137 | 83f64091 | bellard | bdrv->bdrv_read = bdrv_read_em; |
138 | 83f64091 | bellard | bdrv->bdrv_write = bdrv_write_em; |
139 | 83f64091 | bellard | } |
140 | b2e12bc6 | Christoph Hellwig | |
141 | b2e12bc6 | Christoph Hellwig | if (!bdrv->bdrv_aio_flush)
|
142 | b2e12bc6 | Christoph Hellwig | bdrv->bdrv_aio_flush = bdrv_aio_flush_em; |
143 | b2e12bc6 | Christoph Hellwig | |
144 | ea2384d3 | bellard | bdrv->next = first_drv; |
145 | ea2384d3 | bellard | first_drv = bdrv; |
146 | ea2384d3 | bellard | } |
147 | b338082b | bellard | |
148 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
149 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
150 | b338082b | bellard | { |
151 | b338082b | bellard | BlockDriverState **pbs, *bs; |
152 | b338082b | bellard | |
153 | b338082b | bellard | bs = qemu_mallocz(sizeof(BlockDriverState));
|
154 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
155 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
156 | ea2384d3 | bellard | /* insert at the end */
|
157 | ea2384d3 | bellard | pbs = &bdrv_first; |
158 | ea2384d3 | bellard | while (*pbs != NULL) |
159 | ea2384d3 | bellard | pbs = &(*pbs)->next; |
160 | ea2384d3 | bellard | *pbs = bs; |
161 | ea2384d3 | bellard | } |
162 | b338082b | bellard | return bs;
|
163 | b338082b | bellard | } |
164 | b338082b | bellard | |
165 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
166 | ea2384d3 | bellard | { |
167 | ea2384d3 | bellard | BlockDriver *drv1; |
168 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
169 | ea2384d3 | bellard | if (!strcmp(drv1->format_name, format_name))
|
170 | ea2384d3 | bellard | return drv1;
|
171 | ea2384d3 | bellard | } |
172 | ea2384d3 | bellard | return NULL; |
173 | ea2384d3 | bellard | } |
174 | ea2384d3 | bellard | |
175 | eb852011 | Markus Armbruster | static int bdrv_is_whitelisted(BlockDriver *drv) |
176 | eb852011 | Markus Armbruster | { |
177 | eb852011 | Markus Armbruster | static const char *whitelist[] = { |
178 | eb852011 | Markus Armbruster | CONFIG_BDRV_WHITELIST |
179 | eb852011 | Markus Armbruster | }; |
180 | eb852011 | Markus Armbruster | const char **p; |
181 | eb852011 | Markus Armbruster | |
182 | eb852011 | Markus Armbruster | if (!whitelist[0]) |
183 | eb852011 | Markus Armbruster | return 1; /* no whitelist, anything goes */ |
184 | eb852011 | Markus Armbruster | |
185 | eb852011 | Markus Armbruster | for (p = whitelist; *p; p++) {
|
186 | eb852011 | Markus Armbruster | if (!strcmp(drv->format_name, *p)) {
|
187 | eb852011 | Markus Armbruster | return 1; |
188 | eb852011 | Markus Armbruster | } |
189 | eb852011 | Markus Armbruster | } |
190 | eb852011 | Markus Armbruster | return 0; |
191 | eb852011 | Markus Armbruster | } |
192 | eb852011 | Markus Armbruster | |
193 | eb852011 | Markus Armbruster | BlockDriver *bdrv_find_whitelisted_format(const char *format_name) |
194 | eb852011 | Markus Armbruster | { |
195 | eb852011 | Markus Armbruster | BlockDriver *drv = bdrv_find_format(format_name); |
196 | eb852011 | Markus Armbruster | return drv && bdrv_is_whitelisted(drv) ? drv : NULL; |
197 | eb852011 | Markus Armbruster | } |
198 | eb852011 | Markus Armbruster | |
199 | 0e7e1989 | Kevin Wolf | int bdrv_create(BlockDriver *drv, const char* filename, |
200 | 0e7e1989 | Kevin Wolf | QEMUOptionParameter *options) |
201 | ea2384d3 | bellard | { |
202 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
203 | ea2384d3 | bellard | return -ENOTSUP;
|
204 | 0e7e1989 | Kevin Wolf | |
205 | 0e7e1989 | Kevin Wolf | return drv->bdrv_create(filename, options);
|
206 | ea2384d3 | bellard | } |
207 | ea2384d3 | bellard | |
208 | d5249393 | bellard | #ifdef _WIN32
|
209 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
210 | d5249393 | bellard | { |
211 | 3b9f94e1 | bellard | char temp_dir[MAX_PATH];
|
212 | 3b46e624 | ths | |
213 | 3b9f94e1 | bellard | GetTempPath(MAX_PATH, temp_dir); |
214 | 3b9f94e1 | bellard | GetTempFileName(temp_dir, "qem", 0, filename); |
215 | d5249393 | bellard | } |
216 | d5249393 | bellard | #else
|
217 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
218 | fc01f7e7 | bellard | { |
219 | 67b915a5 | bellard | int fd;
|
220 | 7ccfb2eb | blueswir1 | const char *tmpdir; |
221 | d5249393 | bellard | /* XXX: race condition possible */
|
222 | 0badc1ee | aurel32 | tmpdir = getenv("TMPDIR");
|
223 | 0badc1ee | aurel32 | if (!tmpdir)
|
224 | 0badc1ee | aurel32 | tmpdir = "/tmp";
|
225 | 0badc1ee | aurel32 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
|
226 | ea2384d3 | bellard | fd = mkstemp(filename); |
227 | ea2384d3 | bellard | close(fd); |
228 | ea2384d3 | bellard | } |
229 | d5249393 | bellard | #endif
|
230 | fc01f7e7 | bellard | |
231 | 19cb3738 | bellard | #ifdef _WIN32
|
232 | f45512fe | bellard | static int is_windows_drive_prefix(const char *filename) |
233 | f45512fe | bellard | { |
234 | f45512fe | bellard | return (((filename[0] >= 'a' && filename[0] <= 'z') || |
235 | f45512fe | bellard | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
236 | f45512fe | bellard | filename[1] == ':'); |
237 | f45512fe | bellard | } |
238 | 3b46e624 | ths | |
239 | 508c7cb3 | Christoph Hellwig | int is_windows_drive(const char *filename) |
240 | 19cb3738 | bellard | { |
241 | 5fafdf24 | ths | if (is_windows_drive_prefix(filename) &&
|
242 | f45512fe | bellard | filename[2] == '\0') |
243 | 19cb3738 | bellard | return 1; |
244 | 19cb3738 | bellard | if (strstart(filename, "\\\\.\\", NULL) || |
245 | 19cb3738 | bellard | strstart(filename, "//./", NULL)) |
246 | 19cb3738 | bellard | return 1; |
247 | 19cb3738 | bellard | return 0; |
248 | 19cb3738 | bellard | } |
249 | 19cb3738 | bellard | #endif
|
250 | 19cb3738 | bellard | |
251 | 83f64091 | bellard | static BlockDriver *find_protocol(const char *filename) |
252 | 83f64091 | bellard | { |
253 | 83f64091 | bellard | BlockDriver *drv1; |
254 | 83f64091 | bellard | char protocol[128]; |
255 | 1cec71e3 | Anthony Liguori | int len;
|
256 | 83f64091 | bellard | const char *p; |
257 | 19cb3738 | bellard | |
258 | 19cb3738 | bellard | #ifdef _WIN32
|
259 | f45512fe | bellard | if (is_windows_drive(filename) ||
|
260 | f45512fe | bellard | is_windows_drive_prefix(filename)) |
261 | 5efa9d5a | Anthony Liguori | return bdrv_find_format("raw"); |
262 | 19cb3738 | bellard | #endif
|
263 | 1cec71e3 | Anthony Liguori | p = strchr(filename, ':');
|
264 | 1cec71e3 | Anthony Liguori | if (!p)
|
265 | 5efa9d5a | Anthony Liguori | return bdrv_find_format("raw"); |
266 | 1cec71e3 | Anthony Liguori | len = p - filename; |
267 | 1cec71e3 | Anthony Liguori | if (len > sizeof(protocol) - 1) |
268 | 1cec71e3 | Anthony Liguori | len = sizeof(protocol) - 1; |
269 | 1cec71e3 | Anthony Liguori | memcpy(protocol, filename, len); |
270 | 1cec71e3 | Anthony Liguori | protocol[len] = '\0';
|
271 | 83f64091 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
272 | 5fafdf24 | ths | if (drv1->protocol_name &&
|
273 | 83f64091 | bellard | !strcmp(drv1->protocol_name, protocol)) |
274 | 83f64091 | bellard | return drv1;
|
275 | 83f64091 | bellard | } |
276 | 83f64091 | bellard | return NULL; |
277 | 83f64091 | bellard | } |
278 | 83f64091 | bellard | |
279 | f3a5d3f8 | Christoph Hellwig | /*
|
280 | f3a5d3f8 | Christoph Hellwig | * Detect host devices. By convention, /dev/cdrom[N] is always
|
281 | f3a5d3f8 | Christoph Hellwig | * recognized as a host CDROM.
|
282 | f3a5d3f8 | Christoph Hellwig | */
|
283 | f3a5d3f8 | Christoph Hellwig | static BlockDriver *find_hdev_driver(const char *filename) |
284 | f3a5d3f8 | Christoph Hellwig | { |
285 | 508c7cb3 | Christoph Hellwig | int score_max = 0, score; |
286 | 508c7cb3 | Christoph Hellwig | BlockDriver *drv = NULL, *d;
|
287 | f3a5d3f8 | Christoph Hellwig | |
288 | 508c7cb3 | Christoph Hellwig | for (d = first_drv; d; d = d->next) {
|
289 | 508c7cb3 | Christoph Hellwig | if (d->bdrv_probe_device) {
|
290 | 508c7cb3 | Christoph Hellwig | score = d->bdrv_probe_device(filename); |
291 | 508c7cb3 | Christoph Hellwig | if (score > score_max) {
|
292 | 508c7cb3 | Christoph Hellwig | score_max = score; |
293 | 508c7cb3 | Christoph Hellwig | drv = d; |
294 | 508c7cb3 | Christoph Hellwig | } |
295 | 508c7cb3 | Christoph Hellwig | } |
296 | 19cb3738 | bellard | } |
297 | f3a5d3f8 | Christoph Hellwig | |
298 | 508c7cb3 | Christoph Hellwig | return drv;
|
299 | f3a5d3f8 | Christoph Hellwig | } |
300 | 3b46e624 | ths | |
301 | f3a5d3f8 | Christoph Hellwig | static BlockDriver *find_image_format(const char *filename) |
302 | f3a5d3f8 | Christoph Hellwig | { |
303 | f3a5d3f8 | Christoph Hellwig | int ret, score, score_max;
|
304 | f3a5d3f8 | Christoph Hellwig | BlockDriver *drv1, *drv; |
305 | f3a5d3f8 | Christoph Hellwig | uint8_t buf[2048];
|
306 | f3a5d3f8 | Christoph Hellwig | BlockDriverState *bs; |
307 | f3a5d3f8 | Christoph Hellwig | |
308 | 83f64091 | bellard | drv = find_protocol(filename); |
309 | 19cb3738 | bellard | /* no need to test disk image formats for vvfat */
|
310 | c833ab73 | Anthony Liguori | if (drv && strcmp(drv->format_name, "vvfat") == 0) |
311 | 83f64091 | bellard | return drv;
|
312 | 19cb3738 | bellard | |
313 | f5edb014 | Naphtali Sprei | ret = bdrv_file_open(&bs, filename, 0);
|
314 | 83f64091 | bellard | if (ret < 0) |
315 | 83f64091 | bellard | return NULL; |
316 | 83f64091 | bellard | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); |
317 | 83f64091 | bellard | bdrv_delete(bs); |
318 | 83f64091 | bellard | if (ret < 0) { |
319 | 83f64091 | bellard | return NULL; |
320 | 83f64091 | bellard | } |
321 | 83f64091 | bellard | |
322 | ea2384d3 | bellard | score_max = 0;
|
323 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
324 | 83f64091 | bellard | if (drv1->bdrv_probe) {
|
325 | 83f64091 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
326 | 83f64091 | bellard | if (score > score_max) {
|
327 | 83f64091 | bellard | score_max = score; |
328 | 83f64091 | bellard | drv = drv1; |
329 | 83f64091 | bellard | } |
330 | 0849bf08 | bellard | } |
331 | fc01f7e7 | bellard | } |
332 | ea2384d3 | bellard | return drv;
|
333 | ea2384d3 | bellard | } |
334 | ea2384d3 | bellard | |
335 | 83f64091 | bellard | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
336 | ea2384d3 | bellard | { |
337 | 83f64091 | bellard | BlockDriverState *bs; |
338 | 83f64091 | bellard | int ret;
|
339 | 83f64091 | bellard | |
340 | 83f64091 | bellard | bs = bdrv_new("");
|
341 | 83f64091 | bellard | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
|
342 | 83f64091 | bellard | if (ret < 0) { |
343 | 83f64091 | bellard | bdrv_delete(bs); |
344 | 83f64091 | bellard | return ret;
|
345 | 3b0d4f61 | bellard | } |
346 | 71d0770c | aliguori | bs->growable = 1;
|
347 | 83f64091 | bellard | *pbs = bs; |
348 | 83f64091 | bellard | return 0; |
349 | 83f64091 | bellard | } |
350 | 83f64091 | bellard | |
351 | 83f64091 | bellard | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) |
352 | 83f64091 | bellard | { |
353 | 83f64091 | bellard | return bdrv_open2(bs, filename, flags, NULL); |
354 | ea2384d3 | bellard | } |
355 | ea2384d3 | bellard | |
356 | 83f64091 | bellard | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
357 | ea2384d3 | bellard | BlockDriver *drv) |
358 | ea2384d3 | bellard | { |
359 | f5edb014 | Naphtali Sprei | int ret, open_flags;
|
360 | eb5c851f | ths | char tmp_filename[PATH_MAX];
|
361 | eb5c851f | ths | char backing_filename[PATH_MAX];
|
362 | 3b46e624 | ths | |
363 | ea2384d3 | bellard | bs->is_temporary = 0;
|
364 | ea2384d3 | bellard | bs->encrypted = 0;
|
365 | c0f4ce77 | aliguori | bs->valid_key = 0;
|
366 | e268ca52 | aliguori | /* buffer_alignment defaulted to 512, drivers can change this value */
|
367 | e268ca52 | aliguori | bs->buffer_alignment = 512;
|
368 | 712e7874 | bellard | |
369 | 83f64091 | bellard | if (flags & BDRV_O_SNAPSHOT) {
|
370 | ea2384d3 | bellard | BlockDriverState *bs1; |
371 | ea2384d3 | bellard | int64_t total_size; |
372 | 7c96d46e | aliguori | int is_protocol = 0; |
373 | 91a073a9 | Kevin Wolf | BlockDriver *bdrv_qcow2; |
374 | 91a073a9 | Kevin Wolf | QEMUOptionParameter *options; |
375 | 3b46e624 | ths | |
376 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
377 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
378 | 33e3963e | bellard | |
379 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
380 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
381 | 5eb45639 | aliguori | ret = bdrv_open2(bs1, filename, 0, drv);
|
382 | 51d7c00c | aliguori | if (ret < 0) { |
383 | ea2384d3 | bellard | bdrv_delete(bs1); |
384 | 51d7c00c | aliguori | return ret;
|
385 | ea2384d3 | bellard | } |
386 | 6ea44308 | Jan Kiszka | total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS; |
387 | 7c96d46e | aliguori | |
388 | 7c96d46e | aliguori | if (bs1->drv && bs1->drv->protocol_name)
|
389 | 7c96d46e | aliguori | is_protocol = 1;
|
390 | 7c96d46e | aliguori | |
391 | ea2384d3 | bellard | bdrv_delete(bs1); |
392 | 3b46e624 | ths | |
393 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
394 | 7c96d46e | aliguori | |
395 | 7c96d46e | aliguori | /* Real path is meaningless for protocols */
|
396 | 7c96d46e | aliguori | if (is_protocol)
|
397 | 7c96d46e | aliguori | snprintf(backing_filename, sizeof(backing_filename),
|
398 | 7c96d46e | aliguori | "%s", filename);
|
399 | 114cdfa9 | Kirill A. Shutemov | else if (!realpath(filename, backing_filename)) |
400 | 114cdfa9 | Kirill A. Shutemov | return -errno;
|
401 | 7c96d46e | aliguori | |
402 | 91a073a9 | Kevin Wolf | bdrv_qcow2 = bdrv_find_format("qcow2");
|
403 | 91a073a9 | Kevin Wolf | options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); |
404 | 91a073a9 | Kevin Wolf | |
405 | 91a073a9 | Kevin Wolf | set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
|
406 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); |
407 | 91a073a9 | Kevin Wolf | if (drv) {
|
408 | 91a073a9 | Kevin Wolf | set_option_parameter(options, BLOCK_OPT_BACKING_FMT, |
409 | 91a073a9 | Kevin Wolf | drv->format_name); |
410 | 91a073a9 | Kevin Wolf | } |
411 | 91a073a9 | Kevin Wolf | |
412 | 91a073a9 | Kevin Wolf | ret = bdrv_create(bdrv_qcow2, tmp_filename, options); |
413 | 51d7c00c | aliguori | if (ret < 0) { |
414 | 51d7c00c | aliguori | return ret;
|
415 | ea2384d3 | bellard | } |
416 | 91a073a9 | Kevin Wolf | |
417 | ea2384d3 | bellard | filename = tmp_filename; |
418 | 91a073a9 | Kevin Wolf | drv = bdrv_qcow2; |
419 | ea2384d3 | bellard | bs->is_temporary = 1;
|
420 | ea2384d3 | bellard | } |
421 | 712e7874 | bellard | |
422 | ea2384d3 | bellard | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
423 | 83f64091 | bellard | if (flags & BDRV_O_FILE) {
|
424 | 83f64091 | bellard | drv = find_protocol(filename); |
425 | 51d7c00c | aliguori | } else if (!drv) { |
426 | f3a5d3f8 | Christoph Hellwig | drv = find_hdev_driver(filename); |
427 | f3a5d3f8 | Christoph Hellwig | if (!drv) {
|
428 | f3a5d3f8 | Christoph Hellwig | drv = find_image_format(filename); |
429 | f3a5d3f8 | Christoph Hellwig | } |
430 | 51d7c00c | aliguori | } |
431 | 6987307c | Christoph Hellwig | |
432 | 51d7c00c | aliguori | if (!drv) {
|
433 | 51d7c00c | aliguori | ret = -ENOENT; |
434 | 51d7c00c | aliguori | goto unlink_and_fail;
|
435 | ea2384d3 | bellard | } |
436 | 6987307c | Christoph Hellwig | if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
|
437 | 6987307c | Christoph Hellwig | ret = -ENOTSUP; |
438 | 6987307c | Christoph Hellwig | goto unlink_and_fail;
|
439 | 6987307c | Christoph Hellwig | } |
440 | 6987307c | Christoph Hellwig | |
441 | ea2384d3 | bellard | bs->drv = drv; |
442 | ea2384d3 | bellard | bs->opaque = qemu_mallocz(drv->instance_size); |
443 | e900a7b7 | Christoph Hellwig | |
444 | e900a7b7 | Christoph Hellwig | /*
|
445 | e900a7b7 | Christoph Hellwig | * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
|
446 | e900a7b7 | Christoph Hellwig | * write cache to the guest. We do need the fdatasync to flush
|
447 | e900a7b7 | Christoph Hellwig | * out transactions for block allocations, and we maybe have a
|
448 | e900a7b7 | Christoph Hellwig | * volatile write cache in our backing device to deal with.
|
449 | e900a7b7 | Christoph Hellwig | */
|
450 | e900a7b7 | Christoph Hellwig | if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
|
451 | e900a7b7 | Christoph Hellwig | bs->enable_write_cache = 1;
|
452 | e900a7b7 | Christoph Hellwig | |
453 | f5edb014 | Naphtali Sprei | bs->read_only = (flags & BDRV_O_RDWR) == 0;
|
454 | f5edb014 | Naphtali Sprei | if (!(flags & BDRV_O_FILE)) {
|
455 | f5edb014 | Naphtali Sprei | open_flags = (flags & (BDRV_O_RDWR | BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO)); |
456 | f5edb014 | Naphtali Sprei | if (bs->is_temporary) { /* snapshot should be writeable */ |
457 | f5edb014 | Naphtali Sprei | open_flags |= BDRV_O_RDWR; |
458 | f5edb014 | Naphtali Sprei | } |
459 | f5edb014 | Naphtali Sprei | } else {
|
460 | 83f64091 | bellard | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); |
461 | f5edb014 | Naphtali Sprei | } |
462 | 6987307c | Christoph Hellwig | |
463 | 6987307c | Christoph Hellwig | ret = drv->bdrv_open(bs, filename, open_flags); |
464 | ea2384d3 | bellard | if (ret < 0) { |
465 | 6987307c | Christoph Hellwig | goto free_and_fail;
|
466 | 33e3963e | bellard | } |
467 | 6987307c | Christoph Hellwig | |
468 | d15a771d | bellard | if (drv->bdrv_getlength) {
|
469 | 6ea44308 | Jan Kiszka | bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
470 | d15a771d | bellard | } |
471 | 67b915a5 | bellard | #ifndef _WIN32
|
472 | ea2384d3 | bellard | if (bs->is_temporary) {
|
473 | ea2384d3 | bellard | unlink(filename); |
474 | ea2384d3 | bellard | } |
475 | ea2384d3 | bellard | #endif
|
476 | b783e409 | Kevin Wolf | if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { |
477 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
478 | 5eb45639 | aliguori | BlockDriver *back_drv = NULL;
|
479 | ea2384d3 | bellard | bs->backing_hd = bdrv_new("");
|
480 | 83f64091 | bellard | path_combine(backing_filename, sizeof(backing_filename),
|
481 | 83f64091 | bellard | filename, bs->backing_file); |
482 | 5eb45639 | aliguori | if (bs->backing_format[0] != '\0') |
483 | 5eb45639 | aliguori | back_drv = bdrv_find_format(bs->backing_format); |
484 | 5eb45639 | aliguori | ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, |
485 | 5eb45639 | aliguori | back_drv); |
486 | f5edb014 | Naphtali Sprei | bs->backing_hd->read_only = (open_flags & BDRV_O_RDWR) == 0;
|
487 | 51d7c00c | aliguori | if (ret < 0) { |
488 | 51d7c00c | aliguori | bdrv_close(bs); |
489 | 51d7c00c | aliguori | return ret;
|
490 | 51d7c00c | aliguori | } |
491 | 33e3963e | bellard | } |
492 | 33e3963e | bellard | |
493 | bb5fc20f | aliguori | if (!bdrv_key_required(bs)) {
|
494 | bb5fc20f | aliguori | /* call the change callback */
|
495 | bb5fc20f | aliguori | bs->media_changed = 1;
|
496 | bb5fc20f | aliguori | if (bs->change_cb)
|
497 | bb5fc20f | aliguori | bs->change_cb(bs->change_opaque); |
498 | bb5fc20f | aliguori | } |
499 | b338082b | bellard | return 0; |
500 | 6987307c | Christoph Hellwig | |
501 | 6987307c | Christoph Hellwig | free_and_fail:
|
502 | 6987307c | Christoph Hellwig | qemu_free(bs->opaque); |
503 | 6987307c | Christoph Hellwig | bs->opaque = NULL;
|
504 | 6987307c | Christoph Hellwig | bs->drv = NULL;
|
505 | 6987307c | Christoph Hellwig | unlink_and_fail:
|
506 | 6987307c | Christoph Hellwig | if (bs->is_temporary)
|
507 | 6987307c | Christoph Hellwig | unlink(filename); |
508 | 6987307c | Christoph Hellwig | return ret;
|
509 | fc01f7e7 | bellard | } |
510 | fc01f7e7 | bellard | |
511 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
512 | fc01f7e7 | bellard | { |
513 | 19cb3738 | bellard | if (bs->drv) {
|
514 | ea2384d3 | bellard | if (bs->backing_hd)
|
515 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
516 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
517 | ea2384d3 | bellard | qemu_free(bs->opaque); |
518 | ea2384d3 | bellard | #ifdef _WIN32
|
519 | ea2384d3 | bellard | if (bs->is_temporary) {
|
520 | ea2384d3 | bellard | unlink(bs->filename); |
521 | ea2384d3 | bellard | } |
522 | 67b915a5 | bellard | #endif
|
523 | ea2384d3 | bellard | bs->opaque = NULL;
|
524 | ea2384d3 | bellard | bs->drv = NULL;
|
525 | b338082b | bellard | |
526 | b338082b | bellard | /* call the change callback */
|
527 | 19cb3738 | bellard | bs->media_changed = 1;
|
528 | b338082b | bellard | if (bs->change_cb)
|
529 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
530 | b338082b | bellard | } |
531 | b338082b | bellard | } |
532 | b338082b | bellard | |
533 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
534 | b338082b | bellard | { |
535 | 34c6f050 | aurel32 | BlockDriverState **pbs; |
536 | 34c6f050 | aurel32 | |
537 | 34c6f050 | aurel32 | pbs = &bdrv_first; |
538 | 34c6f050 | aurel32 | while (*pbs != bs && *pbs != NULL) |
539 | 34c6f050 | aurel32 | pbs = &(*pbs)->next; |
540 | 34c6f050 | aurel32 | if (*pbs == bs)
|
541 | 34c6f050 | aurel32 | *pbs = bs->next; |
542 | 34c6f050 | aurel32 | |
543 | b338082b | bellard | bdrv_close(bs); |
544 | b338082b | bellard | qemu_free(bs); |
545 | fc01f7e7 | bellard | } |
546 | fc01f7e7 | bellard | |
547 | e97fc193 | aliguori | /*
|
548 | e97fc193 | aliguori | * Run consistency checks on an image
|
549 | e97fc193 | aliguori | *
|
550 | e97fc193 | aliguori | * Returns the number of errors or -errno when an internal error occurs
|
551 | e97fc193 | aliguori | */
|
552 | e97fc193 | aliguori | int bdrv_check(BlockDriverState *bs)
|
553 | e97fc193 | aliguori | { |
554 | e97fc193 | aliguori | if (bs->drv->bdrv_check == NULL) { |
555 | e97fc193 | aliguori | return -ENOTSUP;
|
556 | e97fc193 | aliguori | } |
557 | e97fc193 | aliguori | |
558 | e97fc193 | aliguori | return bs->drv->bdrv_check(bs);
|
559 | e97fc193 | aliguori | } |
560 | e97fc193 | aliguori | |
561 | 33e3963e | bellard | /* commit COW file into the raw image */
|
562 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
563 | 33e3963e | bellard | { |
564 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
565 | 83f64091 | bellard | int64_t i, total_sectors; |
566 | ea2384d3 | bellard | int n, j;
|
567 | 1d44952f | Christoph Hellwig | int ret = 0; |
568 | ea2384d3 | bellard | unsigned char sector[512]; |
569 | 33e3963e | bellard | |
570 | 19cb3738 | bellard | if (!drv)
|
571 | 19cb3738 | bellard | return -ENOMEDIUM;
|
572 | 33e3963e | bellard | |
573 | 33e3963e | bellard | if (bs->read_only) {
|
574 | ea2384d3 | bellard | return -EACCES;
|
575 | 33e3963e | bellard | } |
576 | 33e3963e | bellard | |
577 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
578 | ea2384d3 | bellard | return -ENOTSUP;
|
579 | ea2384d3 | bellard | } |
580 | 33e3963e | bellard | |
581 | 6ea44308 | Jan Kiszka | total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; |
582 | 83f64091 | bellard | for (i = 0; i < total_sectors;) { |
583 | 19cb3738 | bellard | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
584 | ea2384d3 | bellard | for(j = 0; j < n; j++) { |
585 | ea2384d3 | bellard | if (bdrv_read(bs, i, sector, 1) != 0) { |
586 | ea2384d3 | bellard | return -EIO;
|
587 | ea2384d3 | bellard | } |
588 | ea2384d3 | bellard | |
589 | ea2384d3 | bellard | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
590 | ea2384d3 | bellard | return -EIO;
|
591 | ea2384d3 | bellard | } |
592 | ea2384d3 | bellard | i++; |
593 | 33e3963e | bellard | } |
594 | ea2384d3 | bellard | } else {
|
595 | ea2384d3 | bellard | i += n; |
596 | ea2384d3 | bellard | } |
597 | 33e3963e | bellard | } |
598 | 95389c86 | bellard | |
599 | 1d44952f | Christoph Hellwig | if (drv->bdrv_make_empty) {
|
600 | 1d44952f | Christoph Hellwig | ret = drv->bdrv_make_empty(bs); |
601 | 1d44952f | Christoph Hellwig | bdrv_flush(bs); |
602 | 1d44952f | Christoph Hellwig | } |
603 | 95389c86 | bellard | |
604 | 3f5075ae | Christoph Hellwig | /*
|
605 | 3f5075ae | Christoph Hellwig | * Make sure all data we wrote to the backing device is actually
|
606 | 3f5075ae | Christoph Hellwig | * stable on disk.
|
607 | 3f5075ae | Christoph Hellwig | */
|
608 | 3f5075ae | Christoph Hellwig | if (bs->backing_hd)
|
609 | 3f5075ae | Christoph Hellwig | bdrv_flush(bs->backing_hd); |
610 | 1d44952f | Christoph Hellwig | return ret;
|
611 | 33e3963e | bellard | } |
612 | 33e3963e | bellard | |
613 | 756e6736 | Kevin Wolf | /*
|
614 | 756e6736 | Kevin Wolf | * Return values:
|
615 | 756e6736 | Kevin Wolf | * 0 - success
|
616 | 756e6736 | Kevin Wolf | * -EINVAL - backing format specified, but no file
|
617 | 756e6736 | Kevin Wolf | * -ENOSPC - can't update the backing file because no space is left in the
|
618 | 756e6736 | Kevin Wolf | * image file header
|
619 | 756e6736 | Kevin Wolf | * -ENOTSUP - format driver doesn't support changing the backing file
|
620 | 756e6736 | Kevin Wolf | */
|
621 | 756e6736 | Kevin Wolf | int bdrv_change_backing_file(BlockDriverState *bs,
|
622 | 756e6736 | Kevin Wolf | const char *backing_file, const char *backing_fmt) |
623 | 756e6736 | Kevin Wolf | { |
624 | 756e6736 | Kevin Wolf | BlockDriver *drv = bs->drv; |
625 | 756e6736 | Kevin Wolf | |
626 | 756e6736 | Kevin Wolf | if (drv->bdrv_change_backing_file != NULL) { |
627 | 756e6736 | Kevin Wolf | return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
|
628 | 756e6736 | Kevin Wolf | } else {
|
629 | 756e6736 | Kevin Wolf | return -ENOTSUP;
|
630 | 756e6736 | Kevin Wolf | } |
631 | 756e6736 | Kevin Wolf | } |
632 | 756e6736 | Kevin Wolf | |
633 | 71d0770c | aliguori | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
634 | 71d0770c | aliguori | size_t size) |
635 | 71d0770c | aliguori | { |
636 | 71d0770c | aliguori | int64_t len; |
637 | 71d0770c | aliguori | |
638 | 71d0770c | aliguori | if (!bdrv_is_inserted(bs))
|
639 | 71d0770c | aliguori | return -ENOMEDIUM;
|
640 | 71d0770c | aliguori | |
641 | 71d0770c | aliguori | if (bs->growable)
|
642 | 71d0770c | aliguori | return 0; |
643 | 71d0770c | aliguori | |
644 | 71d0770c | aliguori | len = bdrv_getlength(bs); |
645 | 71d0770c | aliguori | |
646 | fbb7b4e0 | Kevin Wolf | if (offset < 0) |
647 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
648 | fbb7b4e0 | Kevin Wolf | |
649 | fbb7b4e0 | Kevin Wolf | if ((offset > len) || (len - offset < size))
|
650 | 71d0770c | aliguori | return -EIO;
|
651 | 71d0770c | aliguori | |
652 | 71d0770c | aliguori | return 0; |
653 | 71d0770c | aliguori | } |
654 | 71d0770c | aliguori | |
655 | 71d0770c | aliguori | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, |
656 | 71d0770c | aliguori | int nb_sectors)
|
657 | 71d0770c | aliguori | { |
658 | 999dec57 | aliguori | return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); |
659 | 71d0770c | aliguori | } |
660 | 71d0770c | aliguori | |
661 | 19cb3738 | bellard | /* return < 0 if error. See bdrv_write() for the return codes */
|
662 | 5fafdf24 | ths | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
663 | fc01f7e7 | bellard | uint8_t *buf, int nb_sectors)
|
664 | fc01f7e7 | bellard | { |
665 | ea2384d3 | bellard | BlockDriver *drv = bs->drv; |
666 | ea2384d3 | bellard | |
667 | 19cb3738 | bellard | if (!drv)
|
668 | 19cb3738 | bellard | return -ENOMEDIUM;
|
669 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
670 | 71d0770c | aliguori | return -EIO;
|
671 | b338082b | bellard | |
672 | eda578e5 | aliguori | return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
|
673 | fc01f7e7 | bellard | } |
674 | fc01f7e7 | bellard | |
675 | 7cd1e32a | lirans@il.ibm.com | static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, |
676 | a55eb92c | Jan Kiszka | int nb_sectors, int dirty) |
677 | 7cd1e32a | lirans@il.ibm.com | { |
678 | 7cd1e32a | lirans@il.ibm.com | int64_t start, end; |
679 | c6d22830 | Jan Kiszka | unsigned long val, idx, bit; |
680 | a55eb92c | Jan Kiszka | |
681 | 6ea44308 | Jan Kiszka | start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; |
682 | c6d22830 | Jan Kiszka | end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
|
683 | a55eb92c | Jan Kiszka | |
684 | a55eb92c | Jan Kiszka | for (; start <= end; start++) {
|
685 | c6d22830 | Jan Kiszka | idx = start / (sizeof(unsigned long) * 8); |
686 | c6d22830 | Jan Kiszka | bit = start % (sizeof(unsigned long) * 8); |
687 | c6d22830 | Jan Kiszka | val = bs->dirty_bitmap[idx]; |
688 | c6d22830 | Jan Kiszka | if (dirty) {
|
689 | c6d22830 | Jan Kiszka | val |= 1 << bit;
|
690 | c6d22830 | Jan Kiszka | } else {
|
691 | c6d22830 | Jan Kiszka | val &= ~(1 << bit);
|
692 | c6d22830 | Jan Kiszka | } |
693 | c6d22830 | Jan Kiszka | bs->dirty_bitmap[idx] = val; |
694 | 7cd1e32a | lirans@il.ibm.com | } |
695 | 7cd1e32a | lirans@il.ibm.com | } |
696 | 7cd1e32a | lirans@il.ibm.com | |
697 | 5fafdf24 | ths | /* Return < 0 if error. Important errors are:
|
698 | 19cb3738 | bellard | -EIO generic I/O error (may happen for all errors)
|
699 | 19cb3738 | bellard | -ENOMEDIUM No media inserted.
|
700 | 19cb3738 | bellard | -EINVAL Invalid sector number or nb_sectors
|
701 | 19cb3738 | bellard | -EACCES Trying to write a read-only device
|
702 | 19cb3738 | bellard | */
|
703 | 5fafdf24 | ths | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
704 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
705 | fc01f7e7 | bellard | { |
706 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
707 | 19cb3738 | bellard | if (!bs->drv)
|
708 | 19cb3738 | bellard | return -ENOMEDIUM;
|
709 | 0849bf08 | bellard | if (bs->read_only)
|
710 | 19cb3738 | bellard | return -EACCES;
|
711 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
712 | 71d0770c | aliguori | return -EIO;
|
713 | a55eb92c | Jan Kiszka | |
714 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
715 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
716 | 7cd1e32a | lirans@il.ibm.com | } |
717 | a55eb92c | Jan Kiszka | |
718 | 42fb2807 | aliguori | return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
|
719 | 83f64091 | bellard | } |
720 | 83f64091 | bellard | |
721 | eda578e5 | aliguori | int bdrv_pread(BlockDriverState *bs, int64_t offset,
|
722 | eda578e5 | aliguori | void *buf, int count1) |
723 | 83f64091 | bellard | { |
724 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
725 | 83f64091 | bellard | int len, nb_sectors, count;
|
726 | 83f64091 | bellard | int64_t sector_num; |
727 | 9a8c4cce | Kevin Wolf | int ret;
|
728 | 83f64091 | bellard | |
729 | 83f64091 | bellard | count = count1; |
730 | 83f64091 | bellard | /* first read to align to sector start */
|
731 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
732 | 83f64091 | bellard | if (len > count)
|
733 | 83f64091 | bellard | len = count; |
734 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
735 | 83f64091 | bellard | if (len > 0) { |
736 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
737 | 9a8c4cce | Kevin Wolf | return ret;
|
738 | 6ea44308 | Jan Kiszka | memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
|
739 | 83f64091 | bellard | count -= len; |
740 | 83f64091 | bellard | if (count == 0) |
741 | 83f64091 | bellard | return count1;
|
742 | 83f64091 | bellard | sector_num++; |
743 | 83f64091 | bellard | buf += len; |
744 | 83f64091 | bellard | } |
745 | 83f64091 | bellard | |
746 | 83f64091 | bellard | /* read the sectors "in place" */
|
747 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
748 | 83f64091 | bellard | if (nb_sectors > 0) { |
749 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) |
750 | 9a8c4cce | Kevin Wolf | return ret;
|
751 | 83f64091 | bellard | sector_num += nb_sectors; |
752 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
753 | 83f64091 | bellard | buf += len; |
754 | 83f64091 | bellard | count -= len; |
755 | 83f64091 | bellard | } |
756 | 83f64091 | bellard | |
757 | 83f64091 | bellard | /* add data from the last sector */
|
758 | 83f64091 | bellard | if (count > 0) { |
759 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
760 | 9a8c4cce | Kevin Wolf | return ret;
|
761 | 83f64091 | bellard | memcpy(buf, tmp_buf, count); |
762 | 83f64091 | bellard | } |
763 | 83f64091 | bellard | return count1;
|
764 | 83f64091 | bellard | } |
765 | 83f64091 | bellard | |
766 | eda578e5 | aliguori | int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
|
767 | eda578e5 | aliguori | const void *buf, int count1) |
768 | 83f64091 | bellard | { |
769 | 6ea44308 | Jan Kiszka | uint8_t tmp_buf[BDRV_SECTOR_SIZE]; |
770 | 83f64091 | bellard | int len, nb_sectors, count;
|
771 | 83f64091 | bellard | int64_t sector_num; |
772 | 9a8c4cce | Kevin Wolf | int ret;
|
773 | 83f64091 | bellard | |
774 | 83f64091 | bellard | count = count1; |
775 | 83f64091 | bellard | /* first write to align to sector start */
|
776 | 6ea44308 | Jan Kiszka | len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
|
777 | 83f64091 | bellard | if (len > count)
|
778 | 83f64091 | bellard | len = count; |
779 | 6ea44308 | Jan Kiszka | sector_num = offset >> BDRV_SECTOR_BITS; |
780 | 83f64091 | bellard | if (len > 0) { |
781 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
782 | 9a8c4cce | Kevin Wolf | return ret;
|
783 | 6ea44308 | Jan Kiszka | memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
|
784 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
785 | 9a8c4cce | Kevin Wolf | return ret;
|
786 | 83f64091 | bellard | count -= len; |
787 | 83f64091 | bellard | if (count == 0) |
788 | 83f64091 | bellard | return count1;
|
789 | 83f64091 | bellard | sector_num++; |
790 | 83f64091 | bellard | buf += len; |
791 | 83f64091 | bellard | } |
792 | 83f64091 | bellard | |
793 | 83f64091 | bellard | /* write the sectors "in place" */
|
794 | 6ea44308 | Jan Kiszka | nb_sectors = count >> BDRV_SECTOR_BITS; |
795 | 83f64091 | bellard | if (nb_sectors > 0) { |
796 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) |
797 | 9a8c4cce | Kevin Wolf | return ret;
|
798 | 83f64091 | bellard | sector_num += nb_sectors; |
799 | 6ea44308 | Jan Kiszka | len = nb_sectors << BDRV_SECTOR_BITS; |
800 | 83f64091 | bellard | buf += len; |
801 | 83f64091 | bellard | count -= len; |
802 | 83f64091 | bellard | } |
803 | 83f64091 | bellard | |
804 | 83f64091 | bellard | /* add data from the last sector */
|
805 | 83f64091 | bellard | if (count > 0) { |
806 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) |
807 | 9a8c4cce | Kevin Wolf | return ret;
|
808 | 83f64091 | bellard | memcpy(tmp_buf, buf, count); |
809 | 9a8c4cce | Kevin Wolf | if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) |
810 | 9a8c4cce | Kevin Wolf | return ret;
|
811 | 83f64091 | bellard | } |
812 | 83f64091 | bellard | return count1;
|
813 | 83f64091 | bellard | } |
814 | 83f64091 | bellard | |
815 | 83f64091 | bellard | /**
|
816 | 83f64091 | bellard | * Truncate file to 'offset' bytes (needed only for file protocols)
|
817 | 83f64091 | bellard | */
|
818 | 83f64091 | bellard | int bdrv_truncate(BlockDriverState *bs, int64_t offset)
|
819 | 83f64091 | bellard | { |
820 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
821 | 83f64091 | bellard | if (!drv)
|
822 | 19cb3738 | bellard | return -ENOMEDIUM;
|
823 | 83f64091 | bellard | if (!drv->bdrv_truncate)
|
824 | 83f64091 | bellard | return -ENOTSUP;
|
825 | 59f2689d | Naphtali Sprei | if (bs->read_only)
|
826 | 59f2689d | Naphtali Sprei | return -EACCES;
|
827 | 83f64091 | bellard | return drv->bdrv_truncate(bs, offset);
|
828 | 83f64091 | bellard | } |
829 | 83f64091 | bellard | |
830 | 83f64091 | bellard | /**
|
831 | 83f64091 | bellard | * Length of a file in bytes. Return < 0 if error or unknown.
|
832 | 83f64091 | bellard | */
|
833 | 83f64091 | bellard | int64_t bdrv_getlength(BlockDriverState *bs) |
834 | 83f64091 | bellard | { |
835 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
836 | 83f64091 | bellard | if (!drv)
|
837 | 19cb3738 | bellard | return -ENOMEDIUM;
|
838 | 83f64091 | bellard | if (!drv->bdrv_getlength) {
|
839 | 83f64091 | bellard | /* legacy mode */
|
840 | 6ea44308 | Jan Kiszka | return bs->total_sectors * BDRV_SECTOR_SIZE;
|
841 | 83f64091 | bellard | } |
842 | 83f64091 | bellard | return drv->bdrv_getlength(bs);
|
843 | fc01f7e7 | bellard | } |
844 | fc01f7e7 | bellard | |
845 | 19cb3738 | bellard | /* return 0 as number of sectors if no device present or error */
|
846 | 96b8f136 | ths | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
|
847 | fc01f7e7 | bellard | { |
848 | 19cb3738 | bellard | int64_t length; |
849 | 19cb3738 | bellard | length = bdrv_getlength(bs); |
850 | 19cb3738 | bellard | if (length < 0) |
851 | 19cb3738 | bellard | length = 0;
|
852 | 19cb3738 | bellard | else
|
853 | 6ea44308 | Jan Kiszka | length = length >> BDRV_SECTOR_BITS; |
854 | 19cb3738 | bellard | *nb_sectors_ptr = length; |
855 | fc01f7e7 | bellard | } |
856 | cf98951b | bellard | |
857 | f3d54fc4 | aliguori | struct partition {
|
858 | f3d54fc4 | aliguori | uint8_t boot_ind; /* 0x80 - active */
|
859 | f3d54fc4 | aliguori | uint8_t head; /* starting head */
|
860 | f3d54fc4 | aliguori | uint8_t sector; /* starting sector */
|
861 | f3d54fc4 | aliguori | uint8_t cyl; /* starting cylinder */
|
862 | f3d54fc4 | aliguori | uint8_t sys_ind; /* What partition type */
|
863 | f3d54fc4 | aliguori | uint8_t end_head; /* end head */
|
864 | f3d54fc4 | aliguori | uint8_t end_sector; /* end sector */
|
865 | f3d54fc4 | aliguori | uint8_t end_cyl; /* end cylinder */
|
866 | f3d54fc4 | aliguori | uint32_t start_sect; /* starting sector counting from 0 */
|
867 | f3d54fc4 | aliguori | uint32_t nr_sects; /* nr of sectors in partition */
|
868 | f3d54fc4 | aliguori | } __attribute__((packed)); |
869 | f3d54fc4 | aliguori | |
870 | f3d54fc4 | aliguori | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
|
871 | f3d54fc4 | aliguori | static int guess_disk_lchs(BlockDriverState *bs, |
872 | f3d54fc4 | aliguori | int *pcylinders, int *pheads, int *psectors) |
873 | f3d54fc4 | aliguori | { |
874 | f3d54fc4 | aliguori | uint8_t buf[512];
|
875 | f3d54fc4 | aliguori | int ret, i, heads, sectors, cylinders;
|
876 | f3d54fc4 | aliguori | struct partition *p;
|
877 | f3d54fc4 | aliguori | uint32_t nr_sects; |
878 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
879 | f3d54fc4 | aliguori | |
880 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
881 | f3d54fc4 | aliguori | |
882 | f3d54fc4 | aliguori | ret = bdrv_read(bs, 0, buf, 1); |
883 | f3d54fc4 | aliguori | if (ret < 0) |
884 | f3d54fc4 | aliguori | return -1; |
885 | f3d54fc4 | aliguori | /* test msdos magic */
|
886 | f3d54fc4 | aliguori | if (buf[510] != 0x55 || buf[511] != 0xaa) |
887 | f3d54fc4 | aliguori | return -1; |
888 | f3d54fc4 | aliguori | for(i = 0; i < 4; i++) { |
889 | f3d54fc4 | aliguori | p = ((struct partition *)(buf + 0x1be)) + i; |
890 | f3d54fc4 | aliguori | nr_sects = le32_to_cpu(p->nr_sects); |
891 | f3d54fc4 | aliguori | if (nr_sects && p->end_head) {
|
892 | f3d54fc4 | aliguori | /* We make the assumption that the partition terminates on
|
893 | f3d54fc4 | aliguori | a cylinder boundary */
|
894 | f3d54fc4 | aliguori | heads = p->end_head + 1;
|
895 | f3d54fc4 | aliguori | sectors = p->end_sector & 63;
|
896 | f3d54fc4 | aliguori | if (sectors == 0) |
897 | f3d54fc4 | aliguori | continue;
|
898 | f3d54fc4 | aliguori | cylinders = nb_sectors / (heads * sectors); |
899 | f3d54fc4 | aliguori | if (cylinders < 1 || cylinders > 16383) |
900 | f3d54fc4 | aliguori | continue;
|
901 | f3d54fc4 | aliguori | *pheads = heads; |
902 | f3d54fc4 | aliguori | *psectors = sectors; |
903 | f3d54fc4 | aliguori | *pcylinders = cylinders; |
904 | f3d54fc4 | aliguori | #if 0
|
905 | f3d54fc4 | aliguori | printf("guessed geometry: LCHS=%d %d %d\n",
|
906 | f3d54fc4 | aliguori | cylinders, heads, sectors);
|
907 | f3d54fc4 | aliguori | #endif
|
908 | f3d54fc4 | aliguori | return 0; |
909 | f3d54fc4 | aliguori | } |
910 | f3d54fc4 | aliguori | } |
911 | f3d54fc4 | aliguori | return -1; |
912 | f3d54fc4 | aliguori | } |
913 | f3d54fc4 | aliguori | |
914 | f3d54fc4 | aliguori | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) |
915 | f3d54fc4 | aliguori | { |
916 | f3d54fc4 | aliguori | int translation, lba_detected = 0; |
917 | f3d54fc4 | aliguori | int cylinders, heads, secs;
|
918 | a38131b6 | blueswir1 | uint64_t nb_sectors; |
919 | f3d54fc4 | aliguori | |
920 | f3d54fc4 | aliguori | /* if a geometry hint is available, use it */
|
921 | f3d54fc4 | aliguori | bdrv_get_geometry(bs, &nb_sectors); |
922 | f3d54fc4 | aliguori | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); |
923 | f3d54fc4 | aliguori | translation = bdrv_get_translation_hint(bs); |
924 | f3d54fc4 | aliguori | if (cylinders != 0) { |
925 | f3d54fc4 | aliguori | *pcyls = cylinders; |
926 | f3d54fc4 | aliguori | *pheads = heads; |
927 | f3d54fc4 | aliguori | *psecs = secs; |
928 | f3d54fc4 | aliguori | } else {
|
929 | f3d54fc4 | aliguori | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { |
930 | f3d54fc4 | aliguori | if (heads > 16) { |
931 | f3d54fc4 | aliguori | /* if heads > 16, it means that a BIOS LBA
|
932 | f3d54fc4 | aliguori | translation was active, so the default
|
933 | f3d54fc4 | aliguori | hardware geometry is OK */
|
934 | f3d54fc4 | aliguori | lba_detected = 1;
|
935 | f3d54fc4 | aliguori | goto default_geometry;
|
936 | f3d54fc4 | aliguori | } else {
|
937 | f3d54fc4 | aliguori | *pcyls = cylinders; |
938 | f3d54fc4 | aliguori | *pheads = heads; |
939 | f3d54fc4 | aliguori | *psecs = secs; |
940 | f3d54fc4 | aliguori | /* disable any translation to be in sync with
|
941 | f3d54fc4 | aliguori | the logical geometry */
|
942 | f3d54fc4 | aliguori | if (translation == BIOS_ATA_TRANSLATION_AUTO) {
|
943 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
944 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_NONE); |
945 | f3d54fc4 | aliguori | } |
946 | f3d54fc4 | aliguori | } |
947 | f3d54fc4 | aliguori | } else {
|
948 | f3d54fc4 | aliguori | default_geometry:
|
949 | f3d54fc4 | aliguori | /* if no geometry, use a standard physical disk geometry */
|
950 | f3d54fc4 | aliguori | cylinders = nb_sectors / (16 * 63); |
951 | f3d54fc4 | aliguori | |
952 | f3d54fc4 | aliguori | if (cylinders > 16383) |
953 | f3d54fc4 | aliguori | cylinders = 16383;
|
954 | f3d54fc4 | aliguori | else if (cylinders < 2) |
955 | f3d54fc4 | aliguori | cylinders = 2;
|
956 | f3d54fc4 | aliguori | *pcyls = cylinders; |
957 | f3d54fc4 | aliguori | *pheads = 16;
|
958 | f3d54fc4 | aliguori | *psecs = 63;
|
959 | f3d54fc4 | aliguori | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { |
960 | f3d54fc4 | aliguori | if ((*pcyls * *pheads) <= 131072) { |
961 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
962 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LARGE); |
963 | f3d54fc4 | aliguori | } else {
|
964 | f3d54fc4 | aliguori | bdrv_set_translation_hint(bs, |
965 | f3d54fc4 | aliguori | BIOS_ATA_TRANSLATION_LBA); |
966 | f3d54fc4 | aliguori | } |
967 | f3d54fc4 | aliguori | } |
968 | f3d54fc4 | aliguori | } |
969 | f3d54fc4 | aliguori | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); |
970 | f3d54fc4 | aliguori | } |
971 | f3d54fc4 | aliguori | } |
972 | f3d54fc4 | aliguori | |
973 | 5fafdf24 | ths | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
974 | b338082b | bellard | int cyls, int heads, int secs) |
975 | b338082b | bellard | { |
976 | b338082b | bellard | bs->cyls = cyls; |
977 | b338082b | bellard | bs->heads = heads; |
978 | b338082b | bellard | bs->secs = secs; |
979 | b338082b | bellard | } |
980 | b338082b | bellard | |
981 | b338082b | bellard | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
982 | b338082b | bellard | { |
983 | b338082b | bellard | bs->type = type; |
984 | b338082b | bellard | bs->removable = ((type == BDRV_TYPE_CDROM || |
985 | b338082b | bellard | type == BDRV_TYPE_FLOPPY)); |
986 | b338082b | bellard | } |
987 | b338082b | bellard | |
988 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
989 | 46d4767d | bellard | { |
990 | 46d4767d | bellard | bs->translation = translation; |
991 | 46d4767d | bellard | } |
992 | 46d4767d | bellard | |
993 | 5fafdf24 | ths | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
994 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
995 | b338082b | bellard | { |
996 | b338082b | bellard | *pcyls = bs->cyls; |
997 | b338082b | bellard | *pheads = bs->heads; |
998 | b338082b | bellard | *psecs = bs->secs; |
999 | b338082b | bellard | } |
1000 | b338082b | bellard | |
1001 | b338082b | bellard | int bdrv_get_type_hint(BlockDriverState *bs)
|
1002 | b338082b | bellard | { |
1003 | b338082b | bellard | return bs->type;
|
1004 | b338082b | bellard | } |
1005 | b338082b | bellard | |
1006 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
1007 | 46d4767d | bellard | { |
1008 | 46d4767d | bellard | return bs->translation;
|
1009 | 46d4767d | bellard | } |
1010 | 46d4767d | bellard | |
1011 | b338082b | bellard | int bdrv_is_removable(BlockDriverState *bs)
|
1012 | b338082b | bellard | { |
1013 | b338082b | bellard | return bs->removable;
|
1014 | b338082b | bellard | } |
1015 | b338082b | bellard | |
1016 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
1017 | b338082b | bellard | { |
1018 | b338082b | bellard | return bs->read_only;
|
1019 | b338082b | bellard | } |
1020 | b338082b | bellard | |
1021 | 985a03b0 | ths | int bdrv_is_sg(BlockDriverState *bs)
|
1022 | 985a03b0 | ths | { |
1023 | 985a03b0 | ths | return bs->sg;
|
1024 | 985a03b0 | ths | } |
1025 | 985a03b0 | ths | |
1026 | e900a7b7 | Christoph Hellwig | int bdrv_enable_write_cache(BlockDriverState *bs)
|
1027 | e900a7b7 | Christoph Hellwig | { |
1028 | e900a7b7 | Christoph Hellwig | return bs->enable_write_cache;
|
1029 | e900a7b7 | Christoph Hellwig | } |
1030 | e900a7b7 | Christoph Hellwig | |
1031 | 19cb3738 | bellard | /* XXX: no longer used */
|
1032 | 5fafdf24 | ths | void bdrv_set_change_cb(BlockDriverState *bs,
|
1033 | b338082b | bellard | void (*change_cb)(void *opaque), void *opaque) |
1034 | b338082b | bellard | { |
1035 | b338082b | bellard | bs->change_cb = change_cb; |
1036 | b338082b | bellard | bs->change_opaque = opaque; |
1037 | b338082b | bellard | } |
1038 | b338082b | bellard | |
1039 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
1040 | ea2384d3 | bellard | { |
1041 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
1042 | ea2384d3 | bellard | return 1; |
1043 | ea2384d3 | bellard | return bs->encrypted;
|
1044 | ea2384d3 | bellard | } |
1045 | ea2384d3 | bellard | |
1046 | c0f4ce77 | aliguori | int bdrv_key_required(BlockDriverState *bs)
|
1047 | c0f4ce77 | aliguori | { |
1048 | c0f4ce77 | aliguori | BlockDriverState *backing_hd = bs->backing_hd; |
1049 | c0f4ce77 | aliguori | |
1050 | c0f4ce77 | aliguori | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
|
1051 | c0f4ce77 | aliguori | return 1; |
1052 | c0f4ce77 | aliguori | return (bs->encrypted && !bs->valid_key);
|
1053 | c0f4ce77 | aliguori | } |
1054 | c0f4ce77 | aliguori | |
1055 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
1056 | ea2384d3 | bellard | { |
1057 | ea2384d3 | bellard | int ret;
|
1058 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
1059 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
1060 | ea2384d3 | bellard | if (ret < 0) |
1061 | ea2384d3 | bellard | return ret;
|
1062 | ea2384d3 | bellard | if (!bs->encrypted)
|
1063 | ea2384d3 | bellard | return 0; |
1064 | ea2384d3 | bellard | } |
1065 | ea2384d3 | bellard | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
|
1066 | ea2384d3 | bellard | return -1; |
1067 | c0f4ce77 | aliguori | ret = bs->drv->bdrv_set_key(bs, key); |
1068 | bb5fc20f | aliguori | if (ret < 0) { |
1069 | bb5fc20f | aliguori | bs->valid_key = 0;
|
1070 | bb5fc20f | aliguori | } else if (!bs->valid_key) { |
1071 | bb5fc20f | aliguori | bs->valid_key = 1;
|
1072 | bb5fc20f | aliguori | /* call the change callback now, we skipped it on open */
|
1073 | bb5fc20f | aliguori | bs->media_changed = 1;
|
1074 | bb5fc20f | aliguori | if (bs->change_cb)
|
1075 | bb5fc20f | aliguori | bs->change_cb(bs->change_opaque); |
1076 | bb5fc20f | aliguori | } |
1077 | c0f4ce77 | aliguori | return ret;
|
1078 | ea2384d3 | bellard | } |
1079 | ea2384d3 | bellard | |
1080 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
1081 | ea2384d3 | bellard | { |
1082 | 19cb3738 | bellard | if (!bs->drv) {
|
1083 | ea2384d3 | bellard | buf[0] = '\0'; |
1084 | ea2384d3 | bellard | } else {
|
1085 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
1086 | ea2384d3 | bellard | } |
1087 | ea2384d3 | bellard | } |
1088 | ea2384d3 | bellard | |
1089 | 5fafdf24 | ths | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
1090 | ea2384d3 | bellard | void *opaque)
|
1091 | ea2384d3 | bellard | { |
1092 | ea2384d3 | bellard | BlockDriver *drv; |
1093 | ea2384d3 | bellard | |
1094 | ea2384d3 | bellard | for (drv = first_drv; drv != NULL; drv = drv->next) { |
1095 | ea2384d3 | bellard | it(opaque, drv->format_name); |
1096 | ea2384d3 | bellard | } |
1097 | ea2384d3 | bellard | } |
1098 | ea2384d3 | bellard | |
1099 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
1100 | b338082b | bellard | { |
1101 | b338082b | bellard | BlockDriverState *bs; |
1102 | b338082b | bellard | |
1103 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1104 | b338082b | bellard | if (!strcmp(name, bs->device_name))
|
1105 | b338082b | bellard | return bs;
|
1106 | b338082b | bellard | } |
1107 | b338082b | bellard | return NULL; |
1108 | b338082b | bellard | } |
1109 | b338082b | bellard | |
1110 | 51de9760 | aliguori | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
1111 | 81d0912d | bellard | { |
1112 | 81d0912d | bellard | BlockDriverState *bs; |
1113 | 81d0912d | bellard | |
1114 | 81d0912d | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1115 | 51de9760 | aliguori | it(opaque, bs); |
1116 | 81d0912d | bellard | } |
1117 | 81d0912d | bellard | } |
1118 | 81d0912d | bellard | |
1119 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
1120 | ea2384d3 | bellard | { |
1121 | ea2384d3 | bellard | return bs->device_name;
|
1122 | ea2384d3 | bellard | } |
1123 | ea2384d3 | bellard | |
1124 | 7a6cba61 | pbrook | void bdrv_flush(BlockDriverState *bs)
|
1125 | 7a6cba61 | pbrook | { |
1126 | 3f5075ae | Christoph Hellwig | if (bs->drv && bs->drv->bdrv_flush)
|
1127 | 7a6cba61 | pbrook | bs->drv->bdrv_flush(bs); |
1128 | 7a6cba61 | pbrook | } |
1129 | 7a6cba61 | pbrook | |
1130 | c6ca28d6 | aliguori | void bdrv_flush_all(void) |
1131 | c6ca28d6 | aliguori | { |
1132 | c6ca28d6 | aliguori | BlockDriverState *bs; |
1133 | c6ca28d6 | aliguori | |
1134 | c6ca28d6 | aliguori | for (bs = bdrv_first; bs != NULL; bs = bs->next) |
1135 | c6ca28d6 | aliguori | if (bs->drv && !bdrv_is_read_only(bs) &&
|
1136 | c6ca28d6 | aliguori | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) |
1137 | c6ca28d6 | aliguori | bdrv_flush(bs); |
1138 | c6ca28d6 | aliguori | } |
1139 | c6ca28d6 | aliguori | |
1140 | f58c7b35 | ths | /*
|
1141 | f58c7b35 | ths | * Returns true iff the specified sector is present in the disk image. Drivers
|
1142 | f58c7b35 | ths | * not implementing the functionality are assumed to not support backing files,
|
1143 | f58c7b35 | ths | * hence all their sectors are reported as allocated.
|
1144 | f58c7b35 | ths | *
|
1145 | f58c7b35 | ths | * 'pnum' is set to the number of sectors (including and immediately following
|
1146 | f58c7b35 | ths | * the specified sector) that are known to be in the same
|
1147 | f58c7b35 | ths | * allocated/unallocated state.
|
1148 | f58c7b35 | ths | *
|
1149 | f58c7b35 | ths | * 'nb_sectors' is the max value 'pnum' should be set to.
|
1150 | f58c7b35 | ths | */
|
1151 | f58c7b35 | ths | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, |
1152 | f58c7b35 | ths | int *pnum)
|
1153 | f58c7b35 | ths | { |
1154 | f58c7b35 | ths | int64_t n; |
1155 | f58c7b35 | ths | if (!bs->drv->bdrv_is_allocated) {
|
1156 | f58c7b35 | ths | if (sector_num >= bs->total_sectors) {
|
1157 | f58c7b35 | ths | *pnum = 0;
|
1158 | f58c7b35 | ths | return 0; |
1159 | f58c7b35 | ths | } |
1160 | f58c7b35 | ths | n = bs->total_sectors - sector_num; |
1161 | f58c7b35 | ths | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); |
1162 | f58c7b35 | ths | return 1; |
1163 | f58c7b35 | ths | } |
1164 | f58c7b35 | ths | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
|
1165 | f58c7b35 | ths | } |
1166 | f58c7b35 | ths | |
1167 | d15e5465 | Luiz Capitulino | static void bdrv_print_dict(QObject *obj, void *opaque) |
1168 | b338082b | bellard | { |
1169 | d15e5465 | Luiz Capitulino | QDict *bs_dict; |
1170 | d15e5465 | Luiz Capitulino | Monitor *mon = opaque; |
1171 | d15e5465 | Luiz Capitulino | |
1172 | d15e5465 | Luiz Capitulino | bs_dict = qobject_to_qdict(obj); |
1173 | d15e5465 | Luiz Capitulino | |
1174 | d15e5465 | Luiz Capitulino | monitor_printf(mon, "%s: type=%s removable=%d",
|
1175 | d15e5465 | Luiz Capitulino | qdict_get_str(bs_dict, "device"),
|
1176 | d15e5465 | Luiz Capitulino | qdict_get_str(bs_dict, "type"),
|
1177 | d15e5465 | Luiz Capitulino | qdict_get_bool(bs_dict, "removable"));
|
1178 | d15e5465 | Luiz Capitulino | |
1179 | d15e5465 | Luiz Capitulino | if (qdict_get_bool(bs_dict, "removable")) { |
1180 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); |
1181 | d15e5465 | Luiz Capitulino | } |
1182 | d15e5465 | Luiz Capitulino | |
1183 | d15e5465 | Luiz Capitulino | if (qdict_haskey(bs_dict, "inserted")) { |
1184 | d15e5465 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
|
1185 | d15e5465 | Luiz Capitulino | |
1186 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " file=");
|
1187 | d15e5465 | Luiz Capitulino | monitor_print_filename(mon, qdict_get_str(qdict, "file"));
|
1188 | d15e5465 | Luiz Capitulino | if (qdict_haskey(qdict, "backing_file")) { |
1189 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " backing_file=");
|
1190 | d15e5465 | Luiz Capitulino | monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
|
1191 | d15e5465 | Luiz Capitulino | } |
1192 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
|
1193 | d15e5465 | Luiz Capitulino | qdict_get_bool(qdict, "ro"),
|
1194 | d15e5465 | Luiz Capitulino | qdict_get_str(qdict, "drv"),
|
1195 | d15e5465 | Luiz Capitulino | qdict_get_bool(qdict, "encrypted"));
|
1196 | d15e5465 | Luiz Capitulino | } else {
|
1197 | d15e5465 | Luiz Capitulino | monitor_printf(mon, " [not inserted]");
|
1198 | d15e5465 | Luiz Capitulino | } |
1199 | d15e5465 | Luiz Capitulino | |
1200 | d15e5465 | Luiz Capitulino | monitor_printf(mon, "\n");
|
1201 | d15e5465 | Luiz Capitulino | } |
1202 | d15e5465 | Luiz Capitulino | |
1203 | d15e5465 | Luiz Capitulino | void bdrv_info_print(Monitor *mon, const QObject *data) |
1204 | d15e5465 | Luiz Capitulino | { |
1205 | d15e5465 | Luiz Capitulino | qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); |
1206 | d15e5465 | Luiz Capitulino | } |
1207 | d15e5465 | Luiz Capitulino | |
1208 | d15e5465 | Luiz Capitulino | /**
|
1209 | d15e5465 | Luiz Capitulino | * bdrv_info(): Block devices information
|
1210 | d15e5465 | Luiz Capitulino | *
|
1211 | d15e5465 | Luiz Capitulino | * Each block device information is stored in a QDict and the
|
1212 | d15e5465 | Luiz Capitulino | * returned QObject is a QList of all devices.
|
1213 | d15e5465 | Luiz Capitulino | *
|
1214 | d15e5465 | Luiz Capitulino | * The QDict contains the following:
|
1215 | d15e5465 | Luiz Capitulino | *
|
1216 | d15e5465 | Luiz Capitulino | * - "device": device name
|
1217 | d15e5465 | Luiz Capitulino | * - "type": device type
|
1218 | d15e5465 | Luiz Capitulino | * - "removable": true if the device is removable, false otherwise
|
1219 | d15e5465 | Luiz Capitulino | * - "locked": true if the device is locked, false otherwise
|
1220 | d15e5465 | Luiz Capitulino | * - "inserted": only present if the device is inserted, it is a QDict
|
1221 | d15e5465 | Luiz Capitulino | * containing the following:
|
1222 | d15e5465 | Luiz Capitulino | * - "file": device file name
|
1223 | d15e5465 | Luiz Capitulino | * - "ro": true if read-only, false otherwise
|
1224 | d15e5465 | Luiz Capitulino | * - "drv": driver format name
|
1225 | d15e5465 | Luiz Capitulino | * - "backing_file": backing file name if one is used
|
1226 | d15e5465 | Luiz Capitulino | * - "encrypted": true if encrypted, false otherwise
|
1227 | d15e5465 | Luiz Capitulino | *
|
1228 | d15e5465 | Luiz Capitulino | * Example:
|
1229 | d15e5465 | Luiz Capitulino | *
|
1230 | d15e5465 | Luiz Capitulino | * [ { "device": "ide0-hd0", "type": "hd", "removable": false, "locked": false,
|
1231 | d15e5465 | Luiz Capitulino | * "inserted": { "file": "/tmp/foobar", "ro": false, "drv": "qcow2" } },
|
1232 | d15e5465 | Luiz Capitulino | * { "device": "floppy0", "type": "floppy", "removable": true,
|
1233 | d15e5465 | Luiz Capitulino | * "locked": false } ]
|
1234 | d15e5465 | Luiz Capitulino | */
|
1235 | d15e5465 | Luiz Capitulino | void bdrv_info(Monitor *mon, QObject **ret_data)
|
1236 | d15e5465 | Luiz Capitulino | { |
1237 | d15e5465 | Luiz Capitulino | QList *bs_list; |
1238 | b338082b | bellard | BlockDriverState *bs; |
1239 | b338082b | bellard | |
1240 | d15e5465 | Luiz Capitulino | bs_list = qlist_new(); |
1241 | d15e5465 | Luiz Capitulino | |
1242 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1243 | d15e5465 | Luiz Capitulino | QObject *bs_obj; |
1244 | d15e5465 | Luiz Capitulino | const char *type = "unknown"; |
1245 | d15e5465 | Luiz Capitulino | |
1246 | b338082b | bellard | switch(bs->type) {
|
1247 | b338082b | bellard | case BDRV_TYPE_HD:
|
1248 | d15e5465 | Luiz Capitulino | type = "hd";
|
1249 | b338082b | bellard | break;
|
1250 | b338082b | bellard | case BDRV_TYPE_CDROM:
|
1251 | d15e5465 | Luiz Capitulino | type = "cdrom";
|
1252 | b338082b | bellard | break;
|
1253 | b338082b | bellard | case BDRV_TYPE_FLOPPY:
|
1254 | d15e5465 | Luiz Capitulino | type = "floppy";
|
1255 | b338082b | bellard | break;
|
1256 | b338082b | bellard | } |
1257 | d15e5465 | Luiz Capitulino | |
1258 | d15e5465 | Luiz Capitulino | bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
|
1259 | d15e5465 | Luiz Capitulino | "'removable': %i, 'locked': %i }",
|
1260 | d15e5465 | Luiz Capitulino | bs->device_name, type, bs->removable, |
1261 | d15e5465 | Luiz Capitulino | bs->locked); |
1262 | d15e5465 | Luiz Capitulino | assert(bs_obj != NULL);
|
1263 | d15e5465 | Luiz Capitulino | |
1264 | 19cb3738 | bellard | if (bs->drv) {
|
1265 | d15e5465 | Luiz Capitulino | QObject *obj; |
1266 | d15e5465 | Luiz Capitulino | QDict *bs_dict = qobject_to_qdict(bs_obj); |
1267 | d15e5465 | Luiz Capitulino | |
1268 | d15e5465 | Luiz Capitulino | obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
|
1269 | d15e5465 | Luiz Capitulino | "'encrypted': %i }",
|
1270 | d15e5465 | Luiz Capitulino | bs->filename, bs->read_only, |
1271 | d15e5465 | Luiz Capitulino | bs->drv->format_name, |
1272 | d15e5465 | Luiz Capitulino | bdrv_is_encrypted(bs)); |
1273 | d15e5465 | Luiz Capitulino | assert(obj != NULL);
|
1274 | fef30743 | ths | if (bs->backing_file[0] != '\0') { |
1275 | d15e5465 | Luiz Capitulino | QDict *qdict = qobject_to_qdict(obj); |
1276 | d15e5465 | Luiz Capitulino | qdict_put(qdict, "backing_file",
|
1277 | d15e5465 | Luiz Capitulino | qstring_from_str(bs->backing_file)); |
1278 | 376253ec | aliguori | } |
1279 | d15e5465 | Luiz Capitulino | |
1280 | d15e5465 | Luiz Capitulino | qdict_put_obj(bs_dict, "inserted", obj);
|
1281 | b338082b | bellard | } |
1282 | d15e5465 | Luiz Capitulino | qlist_append_obj(bs_list, bs_obj); |
1283 | b338082b | bellard | } |
1284 | d15e5465 | Luiz Capitulino | |
1285 | d15e5465 | Luiz Capitulino | *ret_data = QOBJECT(bs_list); |
1286 | b338082b | bellard | } |
1287 | a36e69dd | ths | |
1288 | 218a536a | Luiz Capitulino | static void bdrv_stats_iter(QObject *data, void *opaque) |
1289 | a36e69dd | ths | { |
1290 | 218a536a | Luiz Capitulino | QDict *qdict; |
1291 | 218a536a | Luiz Capitulino | Monitor *mon = opaque; |
1292 | 218a536a | Luiz Capitulino | |
1293 | 218a536a | Luiz Capitulino | qdict = qobject_to_qdict(data); |
1294 | 218a536a | Luiz Capitulino | monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); |
1295 | 218a536a | Luiz Capitulino | |
1296 | 218a536a | Luiz Capitulino | qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
|
1297 | 218a536a | Luiz Capitulino | monitor_printf(mon, " rd_bytes=%" PRId64
|
1298 | 218a536a | Luiz Capitulino | " wr_bytes=%" PRId64
|
1299 | 218a536a | Luiz Capitulino | " rd_operations=%" PRId64
|
1300 | 218a536a | Luiz Capitulino | " wr_operations=%" PRId64
|
1301 | 218a536a | Luiz Capitulino | "\n",
|
1302 | 218a536a | Luiz Capitulino | qdict_get_int(qdict, "rd_bytes"),
|
1303 | 218a536a | Luiz Capitulino | qdict_get_int(qdict, "wr_bytes"),
|
1304 | 218a536a | Luiz Capitulino | qdict_get_int(qdict, "rd_operations"),
|
1305 | 218a536a | Luiz Capitulino | qdict_get_int(qdict, "wr_operations"));
|
1306 | 218a536a | Luiz Capitulino | } |
1307 | 218a536a | Luiz Capitulino | |
1308 | 218a536a | Luiz Capitulino | void bdrv_stats_print(Monitor *mon, const QObject *data) |
1309 | 218a536a | Luiz Capitulino | { |
1310 | 218a536a | Luiz Capitulino | qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); |
1311 | 218a536a | Luiz Capitulino | } |
1312 | 218a536a | Luiz Capitulino | |
1313 | 218a536a | Luiz Capitulino | /**
|
1314 | 218a536a | Luiz Capitulino | * bdrv_info_stats(): show block device statistics
|
1315 | 218a536a | Luiz Capitulino | *
|
1316 | 218a536a | Luiz Capitulino | * Each device statistic information is stored in a QDict and
|
1317 | 218a536a | Luiz Capitulino | * the returned QObject is a QList of all devices.
|
1318 | 218a536a | Luiz Capitulino | *
|
1319 | 218a536a | Luiz Capitulino | * The QDict contains the following:
|
1320 | 218a536a | Luiz Capitulino | *
|
1321 | 218a536a | Luiz Capitulino | * - "device": device name
|
1322 | 218a536a | Luiz Capitulino | * - "stats": A QDict with the statistics information, it contains:
|
1323 | 218a536a | Luiz Capitulino | * - "rd_bytes": bytes read
|
1324 | 218a536a | Luiz Capitulino | * - "wr_bytes": bytes written
|
1325 | 218a536a | Luiz Capitulino | * - "rd_operations": read operations
|
1326 | 218a536a | Luiz Capitulino | * - "wr_operations": write operations
|
1327 | 218a536a | Luiz Capitulino | *
|
1328 | 218a536a | Luiz Capitulino | * Example:
|
1329 | 218a536a | Luiz Capitulino | *
|
1330 | 218a536a | Luiz Capitulino | * [ { "device": "ide0-hd0",
|
1331 | 218a536a | Luiz Capitulino | * "stats": { "rd_bytes": 512,
|
1332 | 218a536a | Luiz Capitulino | * "wr_bytes": 0,
|
1333 | 218a536a | Luiz Capitulino | * "rd_operations": 1,
|
1334 | 218a536a | Luiz Capitulino | * "wr_operations": 0 } },
|
1335 | 218a536a | Luiz Capitulino | * { "device": "ide1-cd0",
|
1336 | 218a536a | Luiz Capitulino | * "stats": { "rd_bytes": 0,
|
1337 | 218a536a | Luiz Capitulino | * "wr_bytes": 0,
|
1338 | 218a536a | Luiz Capitulino | * "rd_operations": 0,
|
1339 | 218a536a | Luiz Capitulino | * "wr_operations": 0 } } ]
|
1340 | 218a536a | Luiz Capitulino | */
|
1341 | 218a536a | Luiz Capitulino | void bdrv_info_stats(Monitor *mon, QObject **ret_data)
|
1342 | 218a536a | Luiz Capitulino | { |
1343 | 218a536a | Luiz Capitulino | QObject *obj; |
1344 | 218a536a | Luiz Capitulino | QList *devices; |
1345 | a36e69dd | ths | BlockDriverState *bs; |
1346 | a36e69dd | ths | |
1347 | 218a536a | Luiz Capitulino | devices = qlist_new(); |
1348 | 218a536a | Luiz Capitulino | |
1349 | a36e69dd | ths | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
1350 | 218a536a | Luiz Capitulino | obj = qobject_from_jsonf("{ 'device': %s, 'stats': {"
|
1351 | 218a536a | Luiz Capitulino | "'rd_bytes': %" PRId64 "," |
1352 | 218a536a | Luiz Capitulino | "'wr_bytes': %" PRId64 "," |
1353 | 218a536a | Luiz Capitulino | "'rd_operations': %" PRId64 "," |
1354 | 218a536a | Luiz Capitulino | "'wr_operations': %" PRId64
|
1355 | 218a536a | Luiz Capitulino | "} }",
|
1356 | 218a536a | Luiz Capitulino | bs->device_name, |
1357 | 218a536a | Luiz Capitulino | bs->rd_bytes, bs->wr_bytes, |
1358 | 218a536a | Luiz Capitulino | bs->rd_ops, bs->wr_ops); |
1359 | 218a536a | Luiz Capitulino | assert(obj != NULL);
|
1360 | 218a536a | Luiz Capitulino | qlist_append_obj(devices, obj); |
1361 | a36e69dd | ths | } |
1362 | 218a536a | Luiz Capitulino | |
1363 | 218a536a | Luiz Capitulino | *ret_data = QOBJECT(devices); |
1364 | a36e69dd | ths | } |
1365 | ea2384d3 | bellard | |
1366 | 045df330 | aliguori | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
1367 | 045df330 | aliguori | { |
1368 | 045df330 | aliguori | if (bs->backing_hd && bs->backing_hd->encrypted)
|
1369 | 045df330 | aliguori | return bs->backing_file;
|
1370 | 045df330 | aliguori | else if (bs->encrypted) |
1371 | 045df330 | aliguori | return bs->filename;
|
1372 | 045df330 | aliguori | else
|
1373 | 045df330 | aliguori | return NULL; |
1374 | 045df330 | aliguori | } |
1375 | 045df330 | aliguori | |
1376 | 5fafdf24 | ths | void bdrv_get_backing_filename(BlockDriverState *bs,
|
1377 | 83f64091 | bellard | char *filename, int filename_size) |
1378 | 83f64091 | bellard | { |
1379 | b783e409 | Kevin Wolf | if (!bs->backing_file) {
|
1380 | 83f64091 | bellard | pstrcpy(filename, filename_size, "");
|
1381 | 83f64091 | bellard | } else {
|
1382 | 83f64091 | bellard | pstrcpy(filename, filename_size, bs->backing_file); |
1383 | 83f64091 | bellard | } |
1384 | 83f64091 | bellard | } |
1385 | 83f64091 | bellard | |
1386 | 5fafdf24 | ths | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
|
1387 | faea38e7 | bellard | const uint8_t *buf, int nb_sectors) |
1388 | faea38e7 | bellard | { |
1389 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1390 | faea38e7 | bellard | if (!drv)
|
1391 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1392 | faea38e7 | bellard | if (!drv->bdrv_write_compressed)
|
1393 | faea38e7 | bellard | return -ENOTSUP;
|
1394 | fbb7b4e0 | Kevin Wolf | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
1395 | fbb7b4e0 | Kevin Wolf | return -EIO;
|
1396 | a55eb92c | Jan Kiszka | |
1397 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
1398 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
1399 | 7cd1e32a | lirans@il.ibm.com | } |
1400 | a55eb92c | Jan Kiszka | |
1401 | faea38e7 | bellard | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
|
1402 | faea38e7 | bellard | } |
1403 | 3b46e624 | ths | |
1404 | faea38e7 | bellard | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
|
1405 | faea38e7 | bellard | { |
1406 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1407 | faea38e7 | bellard | if (!drv)
|
1408 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1409 | faea38e7 | bellard | if (!drv->bdrv_get_info)
|
1410 | faea38e7 | bellard | return -ENOTSUP;
|
1411 | faea38e7 | bellard | memset(bdi, 0, sizeof(*bdi)); |
1412 | faea38e7 | bellard | return drv->bdrv_get_info(bs, bdi);
|
1413 | faea38e7 | bellard | } |
1414 | faea38e7 | bellard | |
1415 | 45566e9c | Christoph Hellwig | int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, |
1416 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
1417 | 178e08a5 | aliguori | { |
1418 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
1419 | 178e08a5 | aliguori | if (!drv)
|
1420 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
1421 | 45566e9c | Christoph Hellwig | if (!drv->bdrv_save_vmstate)
|
1422 | 178e08a5 | aliguori | return -ENOTSUP;
|
1423 | 45566e9c | Christoph Hellwig | return drv->bdrv_save_vmstate(bs, buf, pos, size);
|
1424 | 178e08a5 | aliguori | } |
1425 | 178e08a5 | aliguori | |
1426 | 45566e9c | Christoph Hellwig | int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
|
1427 | 45566e9c | Christoph Hellwig | int64_t pos, int size)
|
1428 | 178e08a5 | aliguori | { |
1429 | 178e08a5 | aliguori | BlockDriver *drv = bs->drv; |
1430 | 178e08a5 | aliguori | if (!drv)
|
1431 | 178e08a5 | aliguori | return -ENOMEDIUM;
|
1432 | 45566e9c | Christoph Hellwig | if (!drv->bdrv_load_vmstate)
|
1433 | 178e08a5 | aliguori | return -ENOTSUP;
|
1434 | 45566e9c | Christoph Hellwig | return drv->bdrv_load_vmstate(bs, buf, pos, size);
|
1435 | 178e08a5 | aliguori | } |
1436 | 178e08a5 | aliguori | |
1437 | faea38e7 | bellard | /**************************************************************/
|
1438 | faea38e7 | bellard | /* handling of snapshots */
|
1439 | faea38e7 | bellard | |
1440 | 5fafdf24 | ths | int bdrv_snapshot_create(BlockDriverState *bs,
|
1441 | faea38e7 | bellard | QEMUSnapshotInfo *sn_info) |
1442 | faea38e7 | bellard | { |
1443 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1444 | faea38e7 | bellard | if (!drv)
|
1445 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1446 | faea38e7 | bellard | if (!drv->bdrv_snapshot_create)
|
1447 | faea38e7 | bellard | return -ENOTSUP;
|
1448 | faea38e7 | bellard | return drv->bdrv_snapshot_create(bs, sn_info);
|
1449 | faea38e7 | bellard | } |
1450 | faea38e7 | bellard | |
1451 | 5fafdf24 | ths | int bdrv_snapshot_goto(BlockDriverState *bs,
|
1452 | faea38e7 | bellard | const char *snapshot_id) |
1453 | faea38e7 | bellard | { |
1454 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1455 | faea38e7 | bellard | if (!drv)
|
1456 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1457 | faea38e7 | bellard | if (!drv->bdrv_snapshot_goto)
|
1458 | faea38e7 | bellard | return -ENOTSUP;
|
1459 | faea38e7 | bellard | return drv->bdrv_snapshot_goto(bs, snapshot_id);
|
1460 | faea38e7 | bellard | } |
1461 | faea38e7 | bellard | |
1462 | faea38e7 | bellard | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) |
1463 | faea38e7 | bellard | { |
1464 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1465 | faea38e7 | bellard | if (!drv)
|
1466 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1467 | faea38e7 | bellard | if (!drv->bdrv_snapshot_delete)
|
1468 | faea38e7 | bellard | return -ENOTSUP;
|
1469 | faea38e7 | bellard | return drv->bdrv_snapshot_delete(bs, snapshot_id);
|
1470 | faea38e7 | bellard | } |
1471 | faea38e7 | bellard | |
1472 | 5fafdf24 | ths | int bdrv_snapshot_list(BlockDriverState *bs,
|
1473 | faea38e7 | bellard | QEMUSnapshotInfo **psn_info) |
1474 | faea38e7 | bellard | { |
1475 | faea38e7 | bellard | BlockDriver *drv = bs->drv; |
1476 | faea38e7 | bellard | if (!drv)
|
1477 | 19cb3738 | bellard | return -ENOMEDIUM;
|
1478 | faea38e7 | bellard | if (!drv->bdrv_snapshot_list)
|
1479 | faea38e7 | bellard | return -ENOTSUP;
|
1480 | faea38e7 | bellard | return drv->bdrv_snapshot_list(bs, psn_info);
|
1481 | faea38e7 | bellard | } |
1482 | faea38e7 | bellard | |
1483 | faea38e7 | bellard | #define NB_SUFFIXES 4 |
1484 | faea38e7 | bellard | |
1485 | faea38e7 | bellard | char *get_human_readable_size(char *buf, int buf_size, int64_t size) |
1486 | faea38e7 | bellard | { |
1487 | faea38e7 | bellard | static const char suffixes[NB_SUFFIXES] = "KMGT"; |
1488 | faea38e7 | bellard | int64_t base; |
1489 | faea38e7 | bellard | int i;
|
1490 | faea38e7 | bellard | |
1491 | faea38e7 | bellard | if (size <= 999) { |
1492 | faea38e7 | bellard | snprintf(buf, buf_size, "%" PRId64, size);
|
1493 | faea38e7 | bellard | } else {
|
1494 | faea38e7 | bellard | base = 1024;
|
1495 | faea38e7 | bellard | for(i = 0; i < NB_SUFFIXES; i++) { |
1496 | faea38e7 | bellard | if (size < (10 * base)) { |
1497 | 5fafdf24 | ths | snprintf(buf, buf_size, "%0.1f%c",
|
1498 | faea38e7 | bellard | (double)size / base,
|
1499 | faea38e7 | bellard | suffixes[i]); |
1500 | faea38e7 | bellard | break;
|
1501 | faea38e7 | bellard | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { |
1502 | 5fafdf24 | ths | snprintf(buf, buf_size, "%" PRId64 "%c", |
1503 | faea38e7 | bellard | ((size + (base >> 1)) / base),
|
1504 | faea38e7 | bellard | suffixes[i]); |
1505 | faea38e7 | bellard | break;
|
1506 | faea38e7 | bellard | } |
1507 | faea38e7 | bellard | base = base * 1024;
|
1508 | faea38e7 | bellard | } |
1509 | faea38e7 | bellard | } |
1510 | faea38e7 | bellard | return buf;
|
1511 | faea38e7 | bellard | } |
1512 | faea38e7 | bellard | |
1513 | faea38e7 | bellard | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) |
1514 | faea38e7 | bellard | { |
1515 | faea38e7 | bellard | char buf1[128], date_buf[128], clock_buf[128]; |
1516 | 3b9f94e1 | bellard | #ifdef _WIN32
|
1517 | 3b9f94e1 | bellard | struct tm *ptm;
|
1518 | 3b9f94e1 | bellard | #else
|
1519 | faea38e7 | bellard | struct tm tm;
|
1520 | 3b9f94e1 | bellard | #endif
|
1521 | faea38e7 | bellard | time_t ti; |
1522 | faea38e7 | bellard | int64_t secs; |
1523 | faea38e7 | bellard | |
1524 | faea38e7 | bellard | if (!sn) {
|
1525 | 5fafdf24 | ths | snprintf(buf, buf_size, |
1526 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
1527 | faea38e7 | bellard | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
1528 | faea38e7 | bellard | } else {
|
1529 | faea38e7 | bellard | ti = sn->date_sec; |
1530 | 3b9f94e1 | bellard | #ifdef _WIN32
|
1531 | 3b9f94e1 | bellard | ptm = localtime(&ti); |
1532 | 3b9f94e1 | bellard | strftime(date_buf, sizeof(date_buf),
|
1533 | 3b9f94e1 | bellard | "%Y-%m-%d %H:%M:%S", ptm);
|
1534 | 3b9f94e1 | bellard | #else
|
1535 | faea38e7 | bellard | localtime_r(&ti, &tm); |
1536 | faea38e7 | bellard | strftime(date_buf, sizeof(date_buf),
|
1537 | faea38e7 | bellard | "%Y-%m-%d %H:%M:%S", &tm);
|
1538 | 3b9f94e1 | bellard | #endif
|
1539 | faea38e7 | bellard | secs = sn->vm_clock_nsec / 1000000000;
|
1540 | faea38e7 | bellard | snprintf(clock_buf, sizeof(clock_buf),
|
1541 | faea38e7 | bellard | "%02d:%02d:%02d.%03d",
|
1542 | faea38e7 | bellard | (int)(secs / 3600), |
1543 | faea38e7 | bellard | (int)((secs / 60) % 60), |
1544 | 5fafdf24 | ths | (int)(secs % 60), |
1545 | faea38e7 | bellard | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
1546 | faea38e7 | bellard | snprintf(buf, buf_size, |
1547 | 5fafdf24 | ths | "%-10s%-20s%7s%20s%15s",
|
1548 | faea38e7 | bellard | sn->id_str, sn->name, |
1549 | faea38e7 | bellard | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
|
1550 | faea38e7 | bellard | date_buf, |
1551 | faea38e7 | bellard | clock_buf); |
1552 | faea38e7 | bellard | } |
1553 | faea38e7 | bellard | return buf;
|
1554 | faea38e7 | bellard | } |
1555 | faea38e7 | bellard | |
1556 | 83f64091 | bellard | |
1557 | ea2384d3 | bellard | /**************************************************************/
|
1558 | 83f64091 | bellard | /* async I/Os */
|
1559 | ea2384d3 | bellard | |
1560 | 3b69e4b9 | aliguori | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, |
1561 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
1562 | 3b69e4b9 | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1563 | 3b69e4b9 | aliguori | { |
1564 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1565 | a36e69dd | ths | BlockDriverAIOCB *ret; |
1566 | 83f64091 | bellard | |
1567 | 19cb3738 | bellard | if (!drv)
|
1568 | ce1a14dc | pbrook | return NULL; |
1569 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
1570 | 71d0770c | aliguori | return NULL; |
1571 | 3b46e624 | ths | |
1572 | f141eafe | aliguori | ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, |
1573 | f141eafe | aliguori | cb, opaque); |
1574 | a36e69dd | ths | |
1575 | a36e69dd | ths | if (ret) {
|
1576 | a36e69dd | ths | /* Update stats even though technically transfer has not happened. */
|
1577 | 6ea44308 | Jan Kiszka | bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
|
1578 | a36e69dd | ths | bs->rd_ops ++; |
1579 | a36e69dd | ths | } |
1580 | a36e69dd | ths | |
1581 | a36e69dd | ths | return ret;
|
1582 | ea2384d3 | bellard | } |
1583 | ea2384d3 | bellard | |
1584 | f141eafe | aliguori | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, |
1585 | f141eafe | aliguori | QEMUIOVector *qiov, int nb_sectors,
|
1586 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1587 | ea2384d3 | bellard | { |
1588 | 83f64091 | bellard | BlockDriver *drv = bs->drv; |
1589 | a36e69dd | ths | BlockDriverAIOCB *ret; |
1590 | ea2384d3 | bellard | |
1591 | 19cb3738 | bellard | if (!drv)
|
1592 | ce1a14dc | pbrook | return NULL; |
1593 | 83f64091 | bellard | if (bs->read_only)
|
1594 | ce1a14dc | pbrook | return NULL; |
1595 | 71d0770c | aliguori | if (bdrv_check_request(bs, sector_num, nb_sectors))
|
1596 | 71d0770c | aliguori | return NULL; |
1597 | 83f64091 | bellard | |
1598 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
1599 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
|
1600 | 7cd1e32a | lirans@il.ibm.com | } |
1601 | a55eb92c | Jan Kiszka | |
1602 | f141eafe | aliguori | ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, |
1603 | f141eafe | aliguori | cb, opaque); |
1604 | a36e69dd | ths | |
1605 | a36e69dd | ths | if (ret) {
|
1606 | a36e69dd | ths | /* Update stats even though technically transfer has not happened. */
|
1607 | 6ea44308 | Jan Kiszka | bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
|
1608 | a36e69dd | ths | bs->wr_ops ++; |
1609 | a36e69dd | ths | } |
1610 | a36e69dd | ths | |
1611 | a36e69dd | ths | return ret;
|
1612 | 83f64091 | bellard | } |
1613 | 83f64091 | bellard | |
1614 | 40b4f539 | Kevin Wolf | |
1615 | 40b4f539 | Kevin Wolf | typedef struct MultiwriteCB { |
1616 | 40b4f539 | Kevin Wolf | int error;
|
1617 | 40b4f539 | Kevin Wolf | int num_requests;
|
1618 | 40b4f539 | Kevin Wolf | int num_callbacks;
|
1619 | 40b4f539 | Kevin Wolf | struct {
|
1620 | 40b4f539 | Kevin Wolf | BlockDriverCompletionFunc *cb; |
1621 | 40b4f539 | Kevin Wolf | void *opaque;
|
1622 | 40b4f539 | Kevin Wolf | QEMUIOVector *free_qiov; |
1623 | 40b4f539 | Kevin Wolf | void *free_buf;
|
1624 | 40b4f539 | Kevin Wolf | } callbacks[]; |
1625 | 40b4f539 | Kevin Wolf | } MultiwriteCB; |
1626 | 40b4f539 | Kevin Wolf | |
1627 | 40b4f539 | Kevin Wolf | static void multiwrite_user_cb(MultiwriteCB *mcb) |
1628 | 40b4f539 | Kevin Wolf | { |
1629 | 40b4f539 | Kevin Wolf | int i;
|
1630 | 40b4f539 | Kevin Wolf | |
1631 | 40b4f539 | Kevin Wolf | for (i = 0; i < mcb->num_callbacks; i++) { |
1632 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); |
1633 | 40b4f539 | Kevin Wolf | qemu_free(mcb->callbacks[i].free_qiov); |
1634 | 40b4f539 | Kevin Wolf | qemu_free(mcb->callbacks[i].free_buf); |
1635 | 40b4f539 | Kevin Wolf | } |
1636 | 40b4f539 | Kevin Wolf | } |
1637 | 40b4f539 | Kevin Wolf | |
1638 | 40b4f539 | Kevin Wolf | static void multiwrite_cb(void *opaque, int ret) |
1639 | 40b4f539 | Kevin Wolf | { |
1640 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb = opaque; |
1641 | 40b4f539 | Kevin Wolf | |
1642 | 40b4f539 | Kevin Wolf | if (ret < 0) { |
1643 | 40b4f539 | Kevin Wolf | mcb->error = ret; |
1644 | 40b4f539 | Kevin Wolf | multiwrite_user_cb(mcb); |
1645 | 40b4f539 | Kevin Wolf | } |
1646 | 40b4f539 | Kevin Wolf | |
1647 | 40b4f539 | Kevin Wolf | mcb->num_requests--; |
1648 | 40b4f539 | Kevin Wolf | if (mcb->num_requests == 0) { |
1649 | 40b4f539 | Kevin Wolf | if (mcb->error == 0) { |
1650 | 40b4f539 | Kevin Wolf | multiwrite_user_cb(mcb); |
1651 | 40b4f539 | Kevin Wolf | } |
1652 | 40b4f539 | Kevin Wolf | qemu_free(mcb); |
1653 | 40b4f539 | Kevin Wolf | } |
1654 | 40b4f539 | Kevin Wolf | } |
1655 | 40b4f539 | Kevin Wolf | |
1656 | 40b4f539 | Kevin Wolf | static int multiwrite_req_compare(const void *a, const void *b) |
1657 | 40b4f539 | Kevin Wolf | { |
1658 | 40b4f539 | Kevin Wolf | return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
|
1659 | 40b4f539 | Kevin Wolf | } |
1660 | 40b4f539 | Kevin Wolf | |
1661 | 40b4f539 | Kevin Wolf | /*
|
1662 | 40b4f539 | Kevin Wolf | * Takes a bunch of requests and tries to merge them. Returns the number of
|
1663 | 40b4f539 | Kevin Wolf | * requests that remain after merging.
|
1664 | 40b4f539 | Kevin Wolf | */
|
1665 | 40b4f539 | Kevin Wolf | static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, |
1666 | 40b4f539 | Kevin Wolf | int num_reqs, MultiwriteCB *mcb)
|
1667 | 40b4f539 | Kevin Wolf | { |
1668 | 40b4f539 | Kevin Wolf | int i, outidx;
|
1669 | 40b4f539 | Kevin Wolf | |
1670 | 40b4f539 | Kevin Wolf | // Sort requests by start sector
|
1671 | 40b4f539 | Kevin Wolf | qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
|
1672 | 40b4f539 | Kevin Wolf | |
1673 | 40b4f539 | Kevin Wolf | // Check if adjacent requests touch the same clusters. If so, combine them,
|
1674 | 40b4f539 | Kevin Wolf | // filling up gaps with zero sectors.
|
1675 | 40b4f539 | Kevin Wolf | outidx = 0;
|
1676 | 40b4f539 | Kevin Wolf | for (i = 1; i < num_reqs; i++) { |
1677 | 40b4f539 | Kevin Wolf | int merge = 0; |
1678 | 40b4f539 | Kevin Wolf | int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; |
1679 | 40b4f539 | Kevin Wolf | |
1680 | 40b4f539 | Kevin Wolf | // This handles the cases that are valid for all block drivers, namely
|
1681 | 40b4f539 | Kevin Wolf | // exactly sequential writes and overlapping writes.
|
1682 | 40b4f539 | Kevin Wolf | if (reqs[i].sector <= oldreq_last) {
|
1683 | 40b4f539 | Kevin Wolf | merge = 1;
|
1684 | 40b4f539 | Kevin Wolf | } |
1685 | 40b4f539 | Kevin Wolf | |
1686 | 40b4f539 | Kevin Wolf | // The block driver may decide that it makes sense to combine requests
|
1687 | 40b4f539 | Kevin Wolf | // even if there is a gap of some sectors between them. In this case,
|
1688 | 40b4f539 | Kevin Wolf | // the gap is filled with zeros (therefore only applicable for yet
|
1689 | 40b4f539 | Kevin Wolf | // unused space in format like qcow2).
|
1690 | 40b4f539 | Kevin Wolf | if (!merge && bs->drv->bdrv_merge_requests) {
|
1691 | 40b4f539 | Kevin Wolf | merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); |
1692 | 40b4f539 | Kevin Wolf | } |
1693 | 40b4f539 | Kevin Wolf | |
1694 | 40b4f539 | Kevin Wolf | if (merge) {
|
1695 | 40b4f539 | Kevin Wolf | size_t size; |
1696 | 40b4f539 | Kevin Wolf | QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
|
1697 | 40b4f539 | Kevin Wolf | qemu_iovec_init(qiov, |
1698 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
|
1699 | 40b4f539 | Kevin Wolf | |
1700 | 40b4f539 | Kevin Wolf | // Add the first request to the merged one. If the requests are
|
1701 | 40b4f539 | Kevin Wolf | // overlapping, drop the last sectors of the first request.
|
1702 | 40b4f539 | Kevin Wolf | size = (reqs[i].sector - reqs[outidx].sector) << 9;
|
1703 | 40b4f539 | Kevin Wolf | qemu_iovec_concat(qiov, reqs[outidx].qiov, size); |
1704 | 40b4f539 | Kevin Wolf | |
1705 | 40b4f539 | Kevin Wolf | // We might need to add some zeros between the two requests
|
1706 | 40b4f539 | Kevin Wolf | if (reqs[i].sector > oldreq_last) {
|
1707 | 40b4f539 | Kevin Wolf | size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
|
1708 | 40b4f539 | Kevin Wolf | uint8_t *buf = qemu_blockalign(bs, zero_bytes); |
1709 | 40b4f539 | Kevin Wolf | memset(buf, 0, zero_bytes);
|
1710 | 40b4f539 | Kevin Wolf | qemu_iovec_add(qiov, buf, zero_bytes); |
1711 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_buf = buf; |
1712 | 40b4f539 | Kevin Wolf | } |
1713 | 40b4f539 | Kevin Wolf | |
1714 | 40b4f539 | Kevin Wolf | // Add the second request
|
1715 | 40b4f539 | Kevin Wolf | qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); |
1716 | 40b4f539 | Kevin Wolf | |
1717 | 40b4f539 | Kevin Wolf | reqs[outidx].nb_sectors += reqs[i].nb_sectors; |
1718 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = qiov; |
1719 | 40b4f539 | Kevin Wolf | |
1720 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].free_qiov = reqs[outidx].qiov; |
1721 | 40b4f539 | Kevin Wolf | } else {
|
1722 | 40b4f539 | Kevin Wolf | outidx++; |
1723 | 40b4f539 | Kevin Wolf | reqs[outidx].sector = reqs[i].sector; |
1724 | 40b4f539 | Kevin Wolf | reqs[outidx].nb_sectors = reqs[i].nb_sectors; |
1725 | 40b4f539 | Kevin Wolf | reqs[outidx].qiov = reqs[i].qiov; |
1726 | 40b4f539 | Kevin Wolf | } |
1727 | 40b4f539 | Kevin Wolf | } |
1728 | 40b4f539 | Kevin Wolf | |
1729 | 40b4f539 | Kevin Wolf | return outidx + 1; |
1730 | 40b4f539 | Kevin Wolf | } |
1731 | 40b4f539 | Kevin Wolf | |
1732 | 40b4f539 | Kevin Wolf | /*
|
1733 | 40b4f539 | Kevin Wolf | * Submit multiple AIO write requests at once.
|
1734 | 40b4f539 | Kevin Wolf | *
|
1735 | 40b4f539 | Kevin Wolf | * On success, the function returns 0 and all requests in the reqs array have
|
1736 | 40b4f539 | Kevin Wolf | * been submitted. In error case this function returns -1, and any of the
|
1737 | 40b4f539 | Kevin Wolf | * requests may or may not be submitted yet. In particular, this means that the
|
1738 | 40b4f539 | Kevin Wolf | * callback will be called for some of the requests, for others it won't. The
|
1739 | 40b4f539 | Kevin Wolf | * caller must check the error field of the BlockRequest to wait for the right
|
1740 | 40b4f539 | Kevin Wolf | * callbacks (if error != 0, no callback will be called).
|
1741 | 40b4f539 | Kevin Wolf | *
|
1742 | 40b4f539 | Kevin Wolf | * The implementation may modify the contents of the reqs array, e.g. to merge
|
1743 | 40b4f539 | Kevin Wolf | * requests. However, the fields opaque and error are left unmodified as they
|
1744 | 40b4f539 | Kevin Wolf | * are used to signal failure for a single request to the caller.
|
1745 | 40b4f539 | Kevin Wolf | */
|
1746 | 40b4f539 | Kevin Wolf | int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) |
1747 | 40b4f539 | Kevin Wolf | { |
1748 | 40b4f539 | Kevin Wolf | BlockDriverAIOCB *acb; |
1749 | 40b4f539 | Kevin Wolf | MultiwriteCB *mcb; |
1750 | 40b4f539 | Kevin Wolf | int i;
|
1751 | 40b4f539 | Kevin Wolf | |
1752 | 40b4f539 | Kevin Wolf | if (num_reqs == 0) { |
1753 | 40b4f539 | Kevin Wolf | return 0; |
1754 | 40b4f539 | Kevin Wolf | } |
1755 | 40b4f539 | Kevin Wolf | |
1756 | 40b4f539 | Kevin Wolf | // Create MultiwriteCB structure
|
1757 | 40b4f539 | Kevin Wolf | mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); |
1758 | 40b4f539 | Kevin Wolf | mcb->num_requests = 0;
|
1759 | 40b4f539 | Kevin Wolf | mcb->num_callbacks = num_reqs; |
1760 | 40b4f539 | Kevin Wolf | |
1761 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
1762 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].cb = reqs[i].cb; |
1763 | 40b4f539 | Kevin Wolf | mcb->callbacks[i].opaque = reqs[i].opaque; |
1764 | 40b4f539 | Kevin Wolf | } |
1765 | 40b4f539 | Kevin Wolf | |
1766 | 40b4f539 | Kevin Wolf | // Check for mergable requests
|
1767 | 40b4f539 | Kevin Wolf | num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); |
1768 | 40b4f539 | Kevin Wolf | |
1769 | 40b4f539 | Kevin Wolf | // Run the aio requests
|
1770 | 40b4f539 | Kevin Wolf | for (i = 0; i < num_reqs; i++) { |
1771 | 40b4f539 | Kevin Wolf | acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, |
1772 | 40b4f539 | Kevin Wolf | reqs[i].nb_sectors, multiwrite_cb, mcb); |
1773 | 40b4f539 | Kevin Wolf | |
1774 | 40b4f539 | Kevin Wolf | if (acb == NULL) { |
1775 | 40b4f539 | Kevin Wolf | // We can only fail the whole thing if no request has been
|
1776 | 40b4f539 | Kevin Wolf | // submitted yet. Otherwise we'll wait for the submitted AIOs to
|
1777 | 40b4f539 | Kevin Wolf | // complete and report the error in the callback.
|
1778 | 40b4f539 | Kevin Wolf | if (mcb->num_requests == 0) { |
1779 | 40b4f539 | Kevin Wolf | reqs[i].error = EIO; |
1780 | 40b4f539 | Kevin Wolf | goto fail;
|
1781 | 40b4f539 | Kevin Wolf | } else {
|
1782 | 40b4f539 | Kevin Wolf | mcb->error = EIO; |
1783 | 40b4f539 | Kevin Wolf | break;
|
1784 | 40b4f539 | Kevin Wolf | } |
1785 | 40b4f539 | Kevin Wolf | } else {
|
1786 | 40b4f539 | Kevin Wolf | mcb->num_requests++; |
1787 | 40b4f539 | Kevin Wolf | } |
1788 | 40b4f539 | Kevin Wolf | } |
1789 | 40b4f539 | Kevin Wolf | |
1790 | 40b4f539 | Kevin Wolf | return 0; |
1791 | 40b4f539 | Kevin Wolf | |
1792 | 40b4f539 | Kevin Wolf | fail:
|
1793 | 40b4f539 | Kevin Wolf | free(mcb); |
1794 | 40b4f539 | Kevin Wolf | return -1; |
1795 | 40b4f539 | Kevin Wolf | } |
1796 | 40b4f539 | Kevin Wolf | |
1797 | b2e12bc6 | Christoph Hellwig | BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, |
1798 | b2e12bc6 | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque)
|
1799 | b2e12bc6 | Christoph Hellwig | { |
1800 | b2e12bc6 | Christoph Hellwig | BlockDriver *drv = bs->drv; |
1801 | b2e12bc6 | Christoph Hellwig | |
1802 | b2e12bc6 | Christoph Hellwig | if (!drv)
|
1803 | b2e12bc6 | Christoph Hellwig | return NULL; |
1804 | b2e12bc6 | Christoph Hellwig | return drv->bdrv_aio_flush(bs, cb, opaque);
|
1805 | b2e12bc6 | Christoph Hellwig | } |
1806 | b2e12bc6 | Christoph Hellwig | |
1807 | 83f64091 | bellard | void bdrv_aio_cancel(BlockDriverAIOCB *acb)
|
1808 | 83f64091 | bellard | { |
1809 | 6bbff9a0 | aliguori | acb->pool->cancel(acb); |
1810 | 83f64091 | bellard | } |
1811 | 83f64091 | bellard | |
1812 | ce1a14dc | pbrook | |
1813 | 83f64091 | bellard | /**************************************************************/
|
1814 | 83f64091 | bellard | /* async block device emulation */
|
1815 | 83f64091 | bellard | |
1816 | c16b5a2c | Christoph Hellwig | typedef struct BlockDriverAIOCBSync { |
1817 | c16b5a2c | Christoph Hellwig | BlockDriverAIOCB common; |
1818 | c16b5a2c | Christoph Hellwig | QEMUBH *bh; |
1819 | c16b5a2c | Christoph Hellwig | int ret;
|
1820 | c16b5a2c | Christoph Hellwig | /* vector translation state */
|
1821 | c16b5a2c | Christoph Hellwig | QEMUIOVector *qiov; |
1822 | c16b5a2c | Christoph Hellwig | uint8_t *bounce; |
1823 | c16b5a2c | Christoph Hellwig | int is_write;
|
1824 | c16b5a2c | Christoph Hellwig | } BlockDriverAIOCBSync; |
1825 | c16b5a2c | Christoph Hellwig | |
1826 | c16b5a2c | Christoph Hellwig | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
1827 | c16b5a2c | Christoph Hellwig | { |
1828 | c16b5a2c | Christoph Hellwig | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
1829 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
1830 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
1831 | c16b5a2c | Christoph Hellwig | qemu_aio_release(acb); |
1832 | c16b5a2c | Christoph Hellwig | } |
1833 | c16b5a2c | Christoph Hellwig | |
1834 | c16b5a2c | Christoph Hellwig | static AIOPool bdrv_em_aio_pool = {
|
1835 | c16b5a2c | Christoph Hellwig | .aiocb_size = sizeof(BlockDriverAIOCBSync),
|
1836 | c16b5a2c | Christoph Hellwig | .cancel = bdrv_aio_cancel_em, |
1837 | c16b5a2c | Christoph Hellwig | }; |
1838 | c16b5a2c | Christoph Hellwig | |
1839 | ce1a14dc | pbrook | static void bdrv_aio_bh_cb(void *opaque) |
1840 | 83f64091 | bellard | { |
1841 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb = opaque; |
1842 | f141eafe | aliguori | |
1843 | f141eafe | aliguori | if (!acb->is_write)
|
1844 | f141eafe | aliguori | qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); |
1845 | ceb42de8 | aliguori | qemu_vfree(acb->bounce); |
1846 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, acb->ret); |
1847 | 6a7ad299 | Dor Laor | qemu_bh_delete(acb->bh); |
1848 | 36afc451 | Avi Kivity | acb->bh = NULL;
|
1849 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1850 | 83f64091 | bellard | } |
1851 | beac80cd | bellard | |
1852 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
|
1853 | f141eafe | aliguori | int64_t sector_num, |
1854 | f141eafe | aliguori | QEMUIOVector *qiov, |
1855 | f141eafe | aliguori | int nb_sectors,
|
1856 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, |
1857 | f141eafe | aliguori | void *opaque,
|
1858 | f141eafe | aliguori | int is_write)
|
1859 | f141eafe | aliguori | |
1860 | 83f64091 | bellard | { |
1861 | ce1a14dc | pbrook | BlockDriverAIOCBSync *acb; |
1862 | ce1a14dc | pbrook | |
1863 | c16b5a2c | Christoph Hellwig | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
1864 | f141eafe | aliguori | acb->is_write = is_write; |
1865 | f141eafe | aliguori | acb->qiov = qiov; |
1866 | e268ca52 | aliguori | acb->bounce = qemu_blockalign(bs, qiov->size); |
1867 | f141eafe | aliguori | |
1868 | ce1a14dc | pbrook | if (!acb->bh)
|
1869 | ce1a14dc | pbrook | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
1870 | f141eafe | aliguori | |
1871 | f141eafe | aliguori | if (is_write) {
|
1872 | f141eafe | aliguori | qemu_iovec_to_buffer(acb->qiov, acb->bounce); |
1873 | f141eafe | aliguori | acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); |
1874 | f141eafe | aliguori | } else {
|
1875 | f141eafe | aliguori | acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); |
1876 | f141eafe | aliguori | } |
1877 | f141eafe | aliguori | |
1878 | ce1a14dc | pbrook | qemu_bh_schedule(acb->bh); |
1879 | f141eafe | aliguori | |
1880 | ce1a14dc | pbrook | return &acb->common;
|
1881 | beac80cd | bellard | } |
1882 | beac80cd | bellard | |
1883 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
|
1884 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
1885 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1886 | beac80cd | bellard | { |
1887 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); |
1888 | f141eafe | aliguori | } |
1889 | 83f64091 | bellard | |
1890 | f141eafe | aliguori | static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
|
1891 | f141eafe | aliguori | int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
|
1892 | f141eafe | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1893 | f141eafe | aliguori | { |
1894 | f141eafe | aliguori | return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); |
1895 | beac80cd | bellard | } |
1896 | beac80cd | bellard | |
1897 | b2e12bc6 | Christoph Hellwig | static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
|
1898 | b2e12bc6 | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque)
|
1899 | b2e12bc6 | Christoph Hellwig | { |
1900 | b2e12bc6 | Christoph Hellwig | BlockDriverAIOCBSync *acb; |
1901 | b2e12bc6 | Christoph Hellwig | |
1902 | b2e12bc6 | Christoph Hellwig | acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); |
1903 | b2e12bc6 | Christoph Hellwig | acb->is_write = 1; /* don't bounce in the completion hadler */ |
1904 | b2e12bc6 | Christoph Hellwig | acb->qiov = NULL;
|
1905 | b2e12bc6 | Christoph Hellwig | acb->bounce = NULL;
|
1906 | b2e12bc6 | Christoph Hellwig | acb->ret = 0;
|
1907 | b2e12bc6 | Christoph Hellwig | |
1908 | b2e12bc6 | Christoph Hellwig | if (!acb->bh)
|
1909 | b2e12bc6 | Christoph Hellwig | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); |
1910 | b2e12bc6 | Christoph Hellwig | |
1911 | b2e12bc6 | Christoph Hellwig | bdrv_flush(bs); |
1912 | b2e12bc6 | Christoph Hellwig | qemu_bh_schedule(acb->bh); |
1913 | b2e12bc6 | Christoph Hellwig | return &acb->common;
|
1914 | b2e12bc6 | Christoph Hellwig | } |
1915 | b2e12bc6 | Christoph Hellwig | |
1916 | 83f64091 | bellard | /**************************************************************/
|
1917 | 83f64091 | bellard | /* sync block device emulation */
|
1918 | ea2384d3 | bellard | |
1919 | 83f64091 | bellard | static void bdrv_rw_em_cb(void *opaque, int ret) |
1920 | 83f64091 | bellard | { |
1921 | 83f64091 | bellard | *(int *)opaque = ret;
|
1922 | ea2384d3 | bellard | } |
1923 | ea2384d3 | bellard | |
1924 | 83f64091 | bellard | #define NOT_DONE 0x7fffffff |
1925 | 83f64091 | bellard | |
1926 | 5fafdf24 | ths | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
1927 | 83f64091 | bellard | uint8_t *buf, int nb_sectors)
|
1928 | 7a6cba61 | pbrook | { |
1929 | ce1a14dc | pbrook | int async_ret;
|
1930 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1931 | f141eafe | aliguori | struct iovec iov;
|
1932 | f141eafe | aliguori | QEMUIOVector qiov; |
1933 | 83f64091 | bellard | |
1934 | 65d6b3d8 | Kevin Wolf | async_context_push(); |
1935 | 65d6b3d8 | Kevin Wolf | |
1936 | 83f64091 | bellard | async_ret = NOT_DONE; |
1937 | 3f4cb3d3 | blueswir1 | iov.iov_base = (void *)buf;
|
1938 | f141eafe | aliguori | iov.iov_len = nb_sectors * 512;
|
1939 | f141eafe | aliguori | qemu_iovec_init_external(&qiov, &iov, 1);
|
1940 | f141eafe | aliguori | acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, |
1941 | f141eafe | aliguori | bdrv_rw_em_cb, &async_ret); |
1942 | 65d6b3d8 | Kevin Wolf | if (acb == NULL) { |
1943 | 65d6b3d8 | Kevin Wolf | async_ret = -1;
|
1944 | 65d6b3d8 | Kevin Wolf | goto fail;
|
1945 | 65d6b3d8 | Kevin Wolf | } |
1946 | baf35cb9 | aliguori | |
1947 | 83f64091 | bellard | while (async_ret == NOT_DONE) {
|
1948 | 83f64091 | bellard | qemu_aio_wait(); |
1949 | 83f64091 | bellard | } |
1950 | baf35cb9 | aliguori | |
1951 | 65d6b3d8 | Kevin Wolf | |
1952 | 65d6b3d8 | Kevin Wolf | fail:
|
1953 | 65d6b3d8 | Kevin Wolf | async_context_pop(); |
1954 | 83f64091 | bellard | return async_ret;
|
1955 | 7a6cba61 | pbrook | } |
1956 | 7a6cba61 | pbrook | |
1957 | 83f64091 | bellard | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
1958 | 83f64091 | bellard | const uint8_t *buf, int nb_sectors) |
1959 | 83f64091 | bellard | { |
1960 | ce1a14dc | pbrook | int async_ret;
|
1961 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
1962 | f141eafe | aliguori | struct iovec iov;
|
1963 | f141eafe | aliguori | QEMUIOVector qiov; |
1964 | 83f64091 | bellard | |
1965 | 65d6b3d8 | Kevin Wolf | async_context_push(); |
1966 | 65d6b3d8 | Kevin Wolf | |
1967 | 83f64091 | bellard | async_ret = NOT_DONE; |
1968 | f141eafe | aliguori | iov.iov_base = (void *)buf;
|
1969 | f141eafe | aliguori | iov.iov_len = nb_sectors * 512;
|
1970 | f141eafe | aliguori | qemu_iovec_init_external(&qiov, &iov, 1);
|
1971 | f141eafe | aliguori | acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, |
1972 | f141eafe | aliguori | bdrv_rw_em_cb, &async_ret); |
1973 | 65d6b3d8 | Kevin Wolf | if (acb == NULL) { |
1974 | 65d6b3d8 | Kevin Wolf | async_ret = -1;
|
1975 | 65d6b3d8 | Kevin Wolf | goto fail;
|
1976 | 65d6b3d8 | Kevin Wolf | } |
1977 | 83f64091 | bellard | while (async_ret == NOT_DONE) {
|
1978 | 83f64091 | bellard | qemu_aio_wait(); |
1979 | 83f64091 | bellard | } |
1980 | 65d6b3d8 | Kevin Wolf | |
1981 | 65d6b3d8 | Kevin Wolf | fail:
|
1982 | 65d6b3d8 | Kevin Wolf | async_context_pop(); |
1983 | 83f64091 | bellard | return async_ret;
|
1984 | 83f64091 | bellard | } |
1985 | ea2384d3 | bellard | |
1986 | ea2384d3 | bellard | void bdrv_init(void) |
1987 | ea2384d3 | bellard | { |
1988 | 5efa9d5a | Anthony Liguori | module_call_init(MODULE_INIT_BLOCK); |
1989 | ea2384d3 | bellard | } |
1990 | ce1a14dc | pbrook | |
1991 | eb852011 | Markus Armbruster | void bdrv_init_with_whitelist(void) |
1992 | eb852011 | Markus Armbruster | { |
1993 | eb852011 | Markus Armbruster | use_bdrv_whitelist = 1;
|
1994 | eb852011 | Markus Armbruster | bdrv_init(); |
1995 | eb852011 | Markus Armbruster | } |
1996 | eb852011 | Markus Armbruster | |
1997 | c16b5a2c | Christoph Hellwig | void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
|
1998 | c16b5a2c | Christoph Hellwig | BlockDriverCompletionFunc *cb, void *opaque)
|
1999 | ce1a14dc | pbrook | { |
2000 | ce1a14dc | pbrook | BlockDriverAIOCB *acb; |
2001 | ce1a14dc | pbrook | |
2002 | 6bbff9a0 | aliguori | if (pool->free_aiocb) {
|
2003 | 6bbff9a0 | aliguori | acb = pool->free_aiocb; |
2004 | 6bbff9a0 | aliguori | pool->free_aiocb = acb->next; |
2005 | ce1a14dc | pbrook | } else {
|
2006 | 6bbff9a0 | aliguori | acb = qemu_mallocz(pool->aiocb_size); |
2007 | 6bbff9a0 | aliguori | acb->pool = pool; |
2008 | ce1a14dc | pbrook | } |
2009 | ce1a14dc | pbrook | acb->bs = bs; |
2010 | ce1a14dc | pbrook | acb->cb = cb; |
2011 | ce1a14dc | pbrook | acb->opaque = opaque; |
2012 | ce1a14dc | pbrook | return acb;
|
2013 | ce1a14dc | pbrook | } |
2014 | ce1a14dc | pbrook | |
2015 | ce1a14dc | pbrook | void qemu_aio_release(void *p) |
2016 | ce1a14dc | pbrook | { |
2017 | 6bbff9a0 | aliguori | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
2018 | 6bbff9a0 | aliguori | AIOPool *pool = acb->pool; |
2019 | 6bbff9a0 | aliguori | acb->next = pool->free_aiocb; |
2020 | 6bbff9a0 | aliguori | pool->free_aiocb = acb; |
2021 | ce1a14dc | pbrook | } |
2022 | 19cb3738 | bellard | |
2023 | 19cb3738 | bellard | /**************************************************************/
|
2024 | 19cb3738 | bellard | /* removable device support */
|
2025 | 19cb3738 | bellard | |
2026 | 19cb3738 | bellard | /**
|
2027 | 19cb3738 | bellard | * Return TRUE if the media is present
|
2028 | 19cb3738 | bellard | */
|
2029 | 19cb3738 | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
2030 | 19cb3738 | bellard | { |
2031 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2032 | 19cb3738 | bellard | int ret;
|
2033 | 19cb3738 | bellard | if (!drv)
|
2034 | 19cb3738 | bellard | return 0; |
2035 | 19cb3738 | bellard | if (!drv->bdrv_is_inserted)
|
2036 | 19cb3738 | bellard | return 1; |
2037 | 19cb3738 | bellard | ret = drv->bdrv_is_inserted(bs); |
2038 | 19cb3738 | bellard | return ret;
|
2039 | 19cb3738 | bellard | } |
2040 | 19cb3738 | bellard | |
2041 | 19cb3738 | bellard | /**
|
2042 | 19cb3738 | bellard | * Return TRUE if the media changed since the last call to this
|
2043 | 5fafdf24 | ths | * function. It is currently only used for floppy disks
|
2044 | 19cb3738 | bellard | */
|
2045 | 19cb3738 | bellard | int bdrv_media_changed(BlockDriverState *bs)
|
2046 | 19cb3738 | bellard | { |
2047 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2048 | 19cb3738 | bellard | int ret;
|
2049 | 19cb3738 | bellard | |
2050 | 19cb3738 | bellard | if (!drv || !drv->bdrv_media_changed)
|
2051 | 19cb3738 | bellard | ret = -ENOTSUP; |
2052 | 19cb3738 | bellard | else
|
2053 | 19cb3738 | bellard | ret = drv->bdrv_media_changed(bs); |
2054 | 19cb3738 | bellard | if (ret == -ENOTSUP)
|
2055 | 19cb3738 | bellard | ret = bs->media_changed; |
2056 | 19cb3738 | bellard | bs->media_changed = 0;
|
2057 | 19cb3738 | bellard | return ret;
|
2058 | 19cb3738 | bellard | } |
2059 | 19cb3738 | bellard | |
2060 | 19cb3738 | bellard | /**
|
2061 | 19cb3738 | bellard | * If eject_flag is TRUE, eject the media. Otherwise, close the tray
|
2062 | 19cb3738 | bellard | */
|
2063 | aea2a33c | Mark McLoughlin | int bdrv_eject(BlockDriverState *bs, int eject_flag) |
2064 | 19cb3738 | bellard | { |
2065 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2066 | 19cb3738 | bellard | int ret;
|
2067 | 19cb3738 | bellard | |
2068 | aea2a33c | Mark McLoughlin | if (bs->locked) {
|
2069 | aea2a33c | Mark McLoughlin | return -EBUSY;
|
2070 | aea2a33c | Mark McLoughlin | } |
2071 | aea2a33c | Mark McLoughlin | |
2072 | 19cb3738 | bellard | if (!drv || !drv->bdrv_eject) {
|
2073 | 19cb3738 | bellard | ret = -ENOTSUP; |
2074 | 19cb3738 | bellard | } else {
|
2075 | 19cb3738 | bellard | ret = drv->bdrv_eject(bs, eject_flag); |
2076 | 19cb3738 | bellard | } |
2077 | 19cb3738 | bellard | if (ret == -ENOTSUP) {
|
2078 | 19cb3738 | bellard | if (eject_flag)
|
2079 | 19cb3738 | bellard | bdrv_close(bs); |
2080 | aea2a33c | Mark McLoughlin | ret = 0;
|
2081 | 19cb3738 | bellard | } |
2082 | aea2a33c | Mark McLoughlin | |
2083 | aea2a33c | Mark McLoughlin | return ret;
|
2084 | 19cb3738 | bellard | } |
2085 | 19cb3738 | bellard | |
2086 | 19cb3738 | bellard | int bdrv_is_locked(BlockDriverState *bs)
|
2087 | 19cb3738 | bellard | { |
2088 | 19cb3738 | bellard | return bs->locked;
|
2089 | 19cb3738 | bellard | } |
2090 | 19cb3738 | bellard | |
2091 | 19cb3738 | bellard | /**
|
2092 | 19cb3738 | bellard | * Lock or unlock the media (if it is locked, the user won't be able
|
2093 | 19cb3738 | bellard | * to eject it manually).
|
2094 | 19cb3738 | bellard | */
|
2095 | 19cb3738 | bellard | void bdrv_set_locked(BlockDriverState *bs, int locked) |
2096 | 19cb3738 | bellard | { |
2097 | 19cb3738 | bellard | BlockDriver *drv = bs->drv; |
2098 | 19cb3738 | bellard | |
2099 | 19cb3738 | bellard | bs->locked = locked; |
2100 | 19cb3738 | bellard | if (drv && drv->bdrv_set_locked) {
|
2101 | 19cb3738 | bellard | drv->bdrv_set_locked(bs, locked); |
2102 | 19cb3738 | bellard | } |
2103 | 19cb3738 | bellard | } |
2104 | 985a03b0 | ths | |
2105 | 985a03b0 | ths | /* needed for generic scsi interface */
|
2106 | 985a03b0 | ths | |
2107 | 985a03b0 | ths | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
2108 | 985a03b0 | ths | { |
2109 | 985a03b0 | ths | BlockDriver *drv = bs->drv; |
2110 | 985a03b0 | ths | |
2111 | 985a03b0 | ths | if (drv && drv->bdrv_ioctl)
|
2112 | 985a03b0 | ths | return drv->bdrv_ioctl(bs, req, buf);
|
2113 | 985a03b0 | ths | return -ENOTSUP;
|
2114 | 985a03b0 | ths | } |
2115 | 7d780669 | aliguori | |
2116 | 221f715d | aliguori | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
2117 | 221f715d | aliguori | unsigned long int req, void *buf, |
2118 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
2119 | 7d780669 | aliguori | { |
2120 | 221f715d | aliguori | BlockDriver *drv = bs->drv; |
2121 | 7d780669 | aliguori | |
2122 | 221f715d | aliguori | if (drv && drv->bdrv_aio_ioctl)
|
2123 | 221f715d | aliguori | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
|
2124 | 221f715d | aliguori | return NULL; |
2125 | 7d780669 | aliguori | } |
2126 | e268ca52 | aliguori | |
2127 | 7cd1e32a | lirans@il.ibm.com | |
2128 | 7cd1e32a | lirans@il.ibm.com | |
2129 | e268ca52 | aliguori | void *qemu_blockalign(BlockDriverState *bs, size_t size)
|
2130 | e268ca52 | aliguori | { |
2131 | e268ca52 | aliguori | return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); |
2132 | e268ca52 | aliguori | } |
2133 | 7cd1e32a | lirans@il.ibm.com | |
2134 | 7cd1e32a | lirans@il.ibm.com | void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) |
2135 | 7cd1e32a | lirans@il.ibm.com | { |
2136 | 7cd1e32a | lirans@il.ibm.com | int64_t bitmap_size; |
2137 | a55eb92c | Jan Kiszka | |
2138 | a55eb92c | Jan Kiszka | if (enable) {
|
2139 | c6d22830 | Jan Kiszka | if (!bs->dirty_bitmap) {
|
2140 | c6d22830 | Jan Kiszka | bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + |
2141 | c6d22830 | Jan Kiszka | BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; |
2142 | c6d22830 | Jan Kiszka | bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
|
2143 | a55eb92c | Jan Kiszka | |
2144 | 7cd1e32a | lirans@il.ibm.com | bs->dirty_bitmap = qemu_mallocz(bitmap_size); |
2145 | a55eb92c | Jan Kiszka | } |
2146 | 7cd1e32a | lirans@il.ibm.com | } else {
|
2147 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap) {
|
2148 | 7cd1e32a | lirans@il.ibm.com | qemu_free(bs->dirty_bitmap); |
2149 | c6d22830 | Jan Kiszka | bs->dirty_bitmap = NULL;
|
2150 | a55eb92c | Jan Kiszka | } |
2151 | 7cd1e32a | lirans@il.ibm.com | } |
2152 | 7cd1e32a | lirans@il.ibm.com | } |
2153 | 7cd1e32a | lirans@il.ibm.com | |
2154 | 7cd1e32a | lirans@il.ibm.com | int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
|
2155 | 7cd1e32a | lirans@il.ibm.com | { |
2156 | 6ea44308 | Jan Kiszka | int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; |
2157 | a55eb92c | Jan Kiszka | |
2158 | c6d22830 | Jan Kiszka | if (bs->dirty_bitmap &&
|
2159 | c6d22830 | Jan Kiszka | (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { |
2160 | c6d22830 | Jan Kiszka | return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & |
2161 | c6d22830 | Jan Kiszka | (1 << (chunk % (sizeof(unsigned long) * 8))); |
2162 | 7cd1e32a | lirans@il.ibm.com | } else {
|
2163 | 7cd1e32a | lirans@il.ibm.com | return 0; |
2164 | 7cd1e32a | lirans@il.ibm.com | } |
2165 | 7cd1e32a | lirans@il.ibm.com | } |
2166 | 7cd1e32a | lirans@il.ibm.com | |
2167 | a55eb92c | Jan Kiszka | void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
|
2168 | a55eb92c | Jan Kiszka | int nr_sectors)
|
2169 | 7cd1e32a | lirans@il.ibm.com | { |
2170 | 7cd1e32a | lirans@il.ibm.com | set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
|
2171 | 7cd1e32a | lirans@il.ibm.com | } |