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