]>
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" |
179a2c19 | 25 | #ifdef HOST_BSD |
3990d09a BS |
26 | /* include native header before sys-queue.h */ |
27 | #include <sys/queue.h> | |
28 | #endif | |
29 | ||
faf07963 | 30 | #include "qemu-common.h" |
376253ec | 31 | #include "monitor.h" |
ea2384d3 | 32 | #include "block_int.h" |
fc01f7e7 | 33 | |
179a2c19 | 34 | #ifdef HOST_BSD |
7674e7bf FB |
35 | #include <sys/types.h> |
36 | #include <sys/stat.h> | |
37 | #include <sys/ioctl.h> | |
c5e97233 | 38 | #ifndef __DragonFly__ |
7674e7bf FB |
39 | #include <sys/disk.h> |
40 | #endif | |
c5e97233 | 41 | #endif |
7674e7bf | 42 | |
49dc768d AL |
43 | #ifdef _WIN32 |
44 | #include <windows.h> | |
45 | #endif | |
46 | ||
83f64091 FB |
47 | #define SECTOR_BITS 9 |
48 | #define SECTOR_SIZE (1 << SECTOR_BITS) | |
49 | ||
c07a9008 AL |
50 | static AIOPool vectored_aio_pool; |
51 | ||
90765429 FB |
52 | typedef struct BlockDriverAIOCBSync { |
53 | BlockDriverAIOCB common; | |
54 | QEMUBH *bh; | |
55 | int ret; | |
56 | } BlockDriverAIOCBSync; | |
57 | ||
c87c0672 AL |
58 | static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, |
59 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
60 | BlockDriverCompletionFunc *cb, void *opaque); | |
61 | static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, | |
62 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
63 | BlockDriverCompletionFunc *cb, void *opaque); | |
ce1a14dc PB |
64 | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, |
65 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
66 | BlockDriverCompletionFunc *cb, void *opaque); | |
67 | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, | |
68 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
69 | BlockDriverCompletionFunc *cb, void *opaque); | |
83f64091 | 70 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); |
5fafdf24 | 71 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
83f64091 FB |
72 | uint8_t *buf, int nb_sectors); |
73 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, | |
74 | const uint8_t *buf, int nb_sectors); | |
ec530c81 | 75 | |
7ee930d0 BS |
76 | BlockDriverState *bdrv_first; |
77 | ||
ea2384d3 FB |
78 | static BlockDriver *first_drv; |
79 | ||
83f64091 | 80 | int path_is_absolute(const char *path) |
3b0d4f61 | 81 | { |
83f64091 | 82 | const char *p; |
21664424 FB |
83 | #ifdef _WIN32 |
84 | /* specific case for names like: "\\.\d:" */ | |
85 | if (*path == '/' || *path == '\\') | |
86 | return 1; | |
87 | #endif | |
83f64091 FB |
88 | p = strchr(path, ':'); |
89 | if (p) | |
90 | p++; | |
91 | else | |
92 | p = path; | |
3b9f94e1 FB |
93 | #ifdef _WIN32 |
94 | return (*p == '/' || *p == '\\'); | |
95 | #else | |
96 | return (*p == '/'); | |
97 | #endif | |
3b0d4f61 FB |
98 | } |
99 | ||
83f64091 FB |
100 | /* if filename is absolute, just copy it to dest. Otherwise, build a |
101 | path to it by considering it is relative to base_path. URL are | |
102 | supported. */ | |
103 | void path_combine(char *dest, int dest_size, | |
104 | const char *base_path, | |
105 | const char *filename) | |
3b0d4f61 | 106 | { |
83f64091 FB |
107 | const char *p, *p1; |
108 | int len; | |
109 | ||
110 | if (dest_size <= 0) | |
111 | return; | |
112 | if (path_is_absolute(filename)) { | |
113 | pstrcpy(dest, dest_size, filename); | |
114 | } else { | |
115 | p = strchr(base_path, ':'); | |
116 | if (p) | |
117 | p++; | |
118 | else | |
119 | p = base_path; | |
3b9f94e1 FB |
120 | p1 = strrchr(base_path, '/'); |
121 | #ifdef _WIN32 | |
122 | { | |
123 | const char *p2; | |
124 | p2 = strrchr(base_path, '\\'); | |
125 | if (!p1 || p2 > p1) | |
126 | p1 = p2; | |
127 | } | |
128 | #endif | |
83f64091 FB |
129 | if (p1) |
130 | p1++; | |
131 | else | |
132 | p1 = base_path; | |
133 | if (p1 > p) | |
134 | p = p1; | |
135 | len = p - base_path; | |
136 | if (len > dest_size - 1) | |
137 | len = dest_size - 1; | |
138 | memcpy(dest, base_path, len); | |
139 | dest[len] = '\0'; | |
140 | pstrcat(dest, dest_size, filename); | |
3b0d4f61 | 141 | } |
3b0d4f61 FB |
142 | } |
143 | ||
3b0d4f61 | 144 | |
9596ebb7 | 145 | static void bdrv_register(BlockDriver *bdrv) |
ea2384d3 | 146 | { |
ce1a14dc | 147 | if (!bdrv->bdrv_aio_read) { |
83f64091 | 148 | /* add AIO emulation layer */ |
83f64091 FB |
149 | bdrv->bdrv_aio_read = bdrv_aio_read_em; |
150 | bdrv->bdrv_aio_write = bdrv_aio_write_em; | |
151 | bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; | |
90765429 | 152 | bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync); |
eda578e5 | 153 | } else if (!bdrv->bdrv_read) { |
83f64091 FB |
154 | /* add synchronous IO emulation layer */ |
155 | bdrv->bdrv_read = bdrv_read_em; | |
156 | bdrv->bdrv_write = bdrv_write_em; | |
157 | } | |
6bbff9a0 | 158 | aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel); |
ea2384d3 FB |
159 | bdrv->next = first_drv; |
160 | first_drv = bdrv; | |
161 | } | |
b338082b FB |
162 | |
163 | /* create a new block device (by default it is empty) */ | |
164 | BlockDriverState *bdrv_new(const char *device_name) | |
165 | { | |
166 | BlockDriverState **pbs, *bs; | |
167 | ||
168 | bs = qemu_mallocz(sizeof(BlockDriverState)); | |
b338082b | 169 | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); |
ea2384d3 FB |
170 | if (device_name[0] != '\0') { |
171 | /* insert at the end */ | |
172 | pbs = &bdrv_first; | |
173 | while (*pbs != NULL) | |
174 | pbs = &(*pbs)->next; | |
175 | *pbs = bs; | |
176 | } | |
b338082b FB |
177 | return bs; |
178 | } | |
179 | ||
ea2384d3 FB |
180 | BlockDriver *bdrv_find_format(const char *format_name) |
181 | { | |
182 | BlockDriver *drv1; | |
183 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
184 | if (!strcmp(drv1->format_name, format_name)) | |
185 | return drv1; | |
186 | } | |
187 | return NULL; | |
188 | } | |
189 | ||
5eb45639 AL |
190 | int bdrv_create2(BlockDriver *drv, |
191 | const char *filename, int64_t size_in_sectors, | |
192 | const char *backing_file, const char *backing_format, | |
193 | int flags) | |
194 | { | |
195 | if (drv->bdrv_create2) | |
196 | return drv->bdrv_create2(filename, size_in_sectors, backing_file, | |
197 | backing_format, flags); | |
198 | if (drv->bdrv_create) | |
199 | return drv->bdrv_create(filename, size_in_sectors, backing_file, | |
200 | flags); | |
201 | return -ENOTSUP; | |
202 | } | |
203 | ||
5fafdf24 | 204 | int bdrv_create(BlockDriver *drv, |
ea2384d3 FB |
205 | const char *filename, int64_t size_in_sectors, |
206 | const char *backing_file, int flags) | |
207 | { | |
208 | if (!drv->bdrv_create) | |
209 | return -ENOTSUP; | |
210 | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); | |
211 | } | |
212 | ||
d5249393 | 213 | #ifdef _WIN32 |
95389c86 | 214 | void get_tmp_filename(char *filename, int size) |
d5249393 | 215 | { |
3b9f94e1 | 216 | char temp_dir[MAX_PATH]; |
3b46e624 | 217 | |
3b9f94e1 FB |
218 | GetTempPath(MAX_PATH, temp_dir); |
219 | GetTempFileName(temp_dir, "qem", 0, filename); | |
d5249393 FB |
220 | } |
221 | #else | |
95389c86 | 222 | void get_tmp_filename(char *filename, int size) |
fc01f7e7 | 223 | { |
67b915a5 | 224 | int fd; |
7ccfb2eb | 225 | const char *tmpdir; |
d5249393 | 226 | /* XXX: race condition possible */ |
0badc1ee AJ |
227 | tmpdir = getenv("TMPDIR"); |
228 | if (!tmpdir) | |
229 | tmpdir = "/tmp"; | |
230 | snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); | |
ea2384d3 FB |
231 | fd = mkstemp(filename); |
232 | close(fd); | |
233 | } | |
d5249393 | 234 | #endif |
fc01f7e7 | 235 | |
19cb3738 | 236 | #ifdef _WIN32 |
f45512fe FB |
237 | static int is_windows_drive_prefix(const char *filename) |
238 | { | |
239 | return (((filename[0] >= 'a' && filename[0] <= 'z') || | |
240 | (filename[0] >= 'A' && filename[0] <= 'Z')) && | |
241 | filename[1] == ':'); | |
242 | } | |
3b46e624 | 243 | |
19cb3738 FB |
244 | static int is_windows_drive(const char *filename) |
245 | { | |
5fafdf24 | 246 | if (is_windows_drive_prefix(filename) && |
f45512fe | 247 | filename[2] == '\0') |
19cb3738 FB |
248 | return 1; |
249 | if (strstart(filename, "\\\\.\\", NULL) || | |
250 | strstart(filename, "//./", NULL)) | |
251 | return 1; | |
252 | return 0; | |
253 | } | |
254 | #endif | |
255 | ||
83f64091 FB |
256 | static BlockDriver *find_protocol(const char *filename) |
257 | { | |
258 | BlockDriver *drv1; | |
259 | char protocol[128]; | |
260 | int len; | |
261 | const char *p; | |
19cb3738 FB |
262 | |
263 | #ifdef _WIN32 | |
f45512fe FB |
264 | if (is_windows_drive(filename) || |
265 | is_windows_drive_prefix(filename)) | |
19cb3738 FB |
266 | return &bdrv_raw; |
267 | #endif | |
83f64091 FB |
268 | p = strchr(filename, ':'); |
269 | if (!p) | |
270 | return &bdrv_raw; | |
271 | len = p - filename; | |
272 | if (len > sizeof(protocol) - 1) | |
273 | len = sizeof(protocol) - 1; | |
83f64091 FB |
274 | memcpy(protocol, filename, len); |
275 | protocol[len] = '\0'; | |
276 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
5fafdf24 | 277 | if (drv1->protocol_name && |
83f64091 FB |
278 | !strcmp(drv1->protocol_name, protocol)) |
279 | return drv1; | |
280 | } | |
281 | return NULL; | |
282 | } | |
283 | ||
7674e7bf FB |
284 | /* XXX: force raw format if block or character device ? It would |
285 | simplify the BSD case */ | |
ea2384d3 FB |
286 | static BlockDriver *find_image_format(const char *filename) |
287 | { | |
83f64091 | 288 | int ret, score, score_max; |
ea2384d3 | 289 | BlockDriver *drv1, *drv; |
83f64091 FB |
290 | uint8_t buf[2048]; |
291 | BlockDriverState *bs; | |
3b46e624 | 292 | |
19cb3738 FB |
293 | /* detect host devices. By convention, /dev/cdrom[N] is always |
294 | recognized as a host CDROM */ | |
295 | if (strstart(filename, "/dev/cdrom", NULL)) | |
296 | return &bdrv_host_device; | |
297 | #ifdef _WIN32 | |
298 | if (is_windows_drive(filename)) | |
299 | return &bdrv_host_device; | |
300 | #else | |
301 | { | |
302 | struct stat st; | |
5fafdf24 | 303 | if (stat(filename, &st) >= 0 && |
19cb3738 FB |
304 | (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { |
305 | return &bdrv_host_device; | |
306 | } | |
307 | } | |
308 | #endif | |
3b46e624 | 309 | |
83f64091 | 310 | drv = find_protocol(filename); |
19cb3738 | 311 | /* no need to test disk image formats for vvfat */ |
83f64091 FB |
312 | if (drv == &bdrv_vvfat) |
313 | return drv; | |
19cb3738 | 314 | |
83f64091 FB |
315 | ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); |
316 | if (ret < 0) | |
317 | return NULL; | |
318 | ret = bdrv_pread(bs, 0, buf, sizeof(buf)); | |
319 | bdrv_delete(bs); | |
320 | if (ret < 0) { | |
321 | return NULL; | |
322 | } | |
323 | ||
ea2384d3 FB |
324 | score_max = 0; |
325 | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { | |
83f64091 FB |
326 | if (drv1->bdrv_probe) { |
327 | score = drv1->bdrv_probe(buf, ret, filename); | |
328 | if (score > score_max) { | |
329 | score_max = score; | |
330 | drv = drv1; | |
331 | } | |
0849bf08 | 332 | } |
fc01f7e7 | 333 | } |
ea2384d3 FB |
334 | return drv; |
335 | } | |
336 | ||
83f64091 | 337 | int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) |
ea2384d3 | 338 | { |
83f64091 FB |
339 | BlockDriverState *bs; |
340 | int ret; | |
341 | ||
342 | bs = bdrv_new(""); | |
83f64091 FB |
343 | ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); |
344 | if (ret < 0) { | |
345 | bdrv_delete(bs); | |
346 | return ret; | |
3b0d4f61 | 347 | } |
71d0770c | 348 | bs->growable = 1; |
83f64091 FB |
349 | *pbs = bs; |
350 | return 0; | |
351 | } | |
352 | ||
353 | int bdrv_open(BlockDriverState *bs, const char *filename, int flags) | |
354 | { | |
355 | return bdrv_open2(bs, filename, flags, NULL); | |
ea2384d3 FB |
356 | } |
357 | ||
83f64091 | 358 | int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, |
ea2384d3 FB |
359 | BlockDriver *drv) |
360 | { | |
83f64091 | 361 | int ret, open_flags; |
eb5c851f TS |
362 | char tmp_filename[PATH_MAX]; |
363 | char backing_filename[PATH_MAX]; | |
3b46e624 | 364 | |
ea2384d3 FB |
365 | bs->read_only = 0; |
366 | bs->is_temporary = 0; | |
367 | bs->encrypted = 0; | |
c0f4ce77 | 368 | bs->valid_key = 0; |
712e7874 | 369 | |
83f64091 | 370 | if (flags & BDRV_O_SNAPSHOT) { |
ea2384d3 FB |
371 | BlockDriverState *bs1; |
372 | int64_t total_size; | |
7c96d46e | 373 | int is_protocol = 0; |
3b46e624 | 374 | |
ea2384d3 FB |
375 | /* if snapshot, we create a temporary backing file and open it |
376 | instead of opening 'filename' directly */ | |
33e3963e | 377 | |
ea2384d3 FB |
378 | /* if there is a backing file, use it */ |
379 | bs1 = bdrv_new(""); | |
5eb45639 | 380 | ret = bdrv_open2(bs1, filename, 0, drv); |
51d7c00c | 381 | if (ret < 0) { |
ea2384d3 | 382 | bdrv_delete(bs1); |
51d7c00c | 383 | return ret; |
ea2384d3 | 384 | } |
83f64091 | 385 | total_size = bdrv_getlength(bs1) >> SECTOR_BITS; |
7c96d46e AL |
386 | |
387 | if (bs1->drv && bs1->drv->protocol_name) | |
388 | is_protocol = 1; | |
389 | ||
ea2384d3 | 390 | bdrv_delete(bs1); |
3b46e624 | 391 | |
ea2384d3 | 392 | get_tmp_filename(tmp_filename, sizeof(tmp_filename)); |
7c96d46e AL |
393 | |
394 | /* Real path is meaningless for protocols */ | |
395 | if (is_protocol) | |
396 | snprintf(backing_filename, sizeof(backing_filename), | |
397 | "%s", filename); | |
398 | else | |
399 | realpath(filename, backing_filename); | |
400 | ||
5eb45639 AL |
401 | ret = bdrv_create2(&bdrv_qcow2, tmp_filename, |
402 | total_size, backing_filename, | |
403 | (drv ? drv->format_name : NULL), 0); | |
51d7c00c AL |
404 | if (ret < 0) { |
405 | return ret; | |
ea2384d3 FB |
406 | } |
407 | filename = tmp_filename; | |
5eb45639 | 408 | drv = &bdrv_qcow2; |
ea2384d3 FB |
409 | bs->is_temporary = 1; |
410 | } | |
712e7874 | 411 | |
ea2384d3 | 412 | pstrcpy(bs->filename, sizeof(bs->filename), filename); |
83f64091 FB |
413 | if (flags & BDRV_O_FILE) { |
414 | drv = find_protocol(filename); | |
51d7c00c AL |
415 | } else if (!drv) { |
416 | drv = find_image_format(filename); | |
417 | } | |
418 | if (!drv) { | |
419 | ret = -ENOENT; | |
420 | goto unlink_and_fail; | |
ea2384d3 FB |
421 | } |
422 | bs->drv = drv; | |
423 | bs->opaque = qemu_mallocz(drv->instance_size); | |
83f64091 FB |
424 | /* Note: for compatibility, we open disk image files as RDWR, and |
425 | RDONLY as fallback */ | |
426 | if (!(flags & BDRV_O_FILE)) | |
9f7965c7 | 427 | open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); |
83f64091 FB |
428 | else |
429 | open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); | |
430 | ret = drv->bdrv_open(bs, filename, open_flags); | |
a0a83536 | 431 | if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { |
9f7965c7 | 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(""); |
83f64091 FB |
456 | path_combine(backing_filename, sizeof(backing_filename), |
457 | filename, bs->backing_file); | |
5eb45639 AL |
458 | if (bs->backing_format[0] != '\0') |
459 | back_drv = bdrv_find_format(bs->backing_format); | |
460 | ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, | |
461 | back_drv); | |
51d7c00c AL |
462 | if (ret < 0) { |
463 | bdrv_close(bs); | |
464 | return ret; | |
465 | } | |
33e3963e FB |
466 | } |
467 | ||
bb5fc20f AL |
468 | if (!bdrv_key_required(bs)) { |
469 | /* call the change callback */ | |
470 | bs->media_changed = 1; | |
471 | if (bs->change_cb) | |
472 | bs->change_cb(bs->change_opaque); | |
473 | } | |
b338082b | 474 | return 0; |
fc01f7e7 FB |
475 | } |
476 | ||
477 | void bdrv_close(BlockDriverState *bs) | |
478 | { | |
19cb3738 | 479 | if (bs->drv) { |
ea2384d3 FB |
480 | if (bs->backing_hd) |
481 | bdrv_delete(bs->backing_hd); | |
482 | bs->drv->bdrv_close(bs); | |
483 | qemu_free(bs->opaque); | |
484 | #ifdef _WIN32 | |
485 | if (bs->is_temporary) { | |
486 | unlink(bs->filename); | |
487 | } | |
67b915a5 | 488 | #endif |
ea2384d3 FB |
489 | bs->opaque = NULL; |
490 | bs->drv = NULL; | |
b338082b FB |
491 | |
492 | /* call the change callback */ | |
19cb3738 | 493 | bs->media_changed = 1; |
b338082b FB |
494 | if (bs->change_cb) |
495 | bs->change_cb(bs->change_opaque); | |
496 | } | |
497 | } | |
498 | ||
499 | void bdrv_delete(BlockDriverState *bs) | |
500 | { | |
34c6f050 AJ |
501 | BlockDriverState **pbs; |
502 | ||
503 | pbs = &bdrv_first; | |
504 | while (*pbs != bs && *pbs != NULL) | |
505 | pbs = &(*pbs)->next; | |
506 | if (*pbs == bs) | |
507 | *pbs = bs->next; | |
508 | ||
b338082b FB |
509 | bdrv_close(bs); |
510 | qemu_free(bs); | |
fc01f7e7 FB |
511 | } |
512 | ||
33e3963e FB |
513 | /* commit COW file into the raw image */ |
514 | int bdrv_commit(BlockDriverState *bs) | |
515 | { | |
19cb3738 | 516 | BlockDriver *drv = bs->drv; |
83f64091 | 517 | int64_t i, total_sectors; |
ea2384d3 FB |
518 | int n, j; |
519 | unsigned char sector[512]; | |
33e3963e | 520 | |
19cb3738 FB |
521 | if (!drv) |
522 | return -ENOMEDIUM; | |
33e3963e FB |
523 | |
524 | if (bs->read_only) { | |
ea2384d3 | 525 | return -EACCES; |
33e3963e FB |
526 | } |
527 | ||
ea2384d3 FB |
528 | if (!bs->backing_hd) { |
529 | return -ENOTSUP; | |
530 | } | |
33e3963e | 531 | |
83f64091 FB |
532 | total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; |
533 | for (i = 0; i < total_sectors;) { | |
19cb3738 | 534 | if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
ea2384d3 FB |
535 | for(j = 0; j < n; j++) { |
536 | if (bdrv_read(bs, i, sector, 1) != 0) { | |
537 | return -EIO; | |
538 | } | |
539 | ||
540 | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { | |
541 | return -EIO; | |
542 | } | |
543 | i++; | |
33e3963e | 544 | } |
ea2384d3 FB |
545 | } else { |
546 | i += n; | |
547 | } | |
33e3963e | 548 | } |
95389c86 | 549 | |
19cb3738 FB |
550 | if (drv->bdrv_make_empty) |
551 | return drv->bdrv_make_empty(bs); | |
95389c86 | 552 | |
33e3963e FB |
553 | return 0; |
554 | } | |
555 | ||
71d0770c AL |
556 | static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, |
557 | size_t size) | |
558 | { | |
559 | int64_t len; | |
560 | ||
561 | if (!bdrv_is_inserted(bs)) | |
562 | return -ENOMEDIUM; | |
563 | ||
564 | if (bs->growable) | |
565 | return 0; | |
566 | ||
567 | len = bdrv_getlength(bs); | |
568 | ||
569 | if ((offset + size) > len) | |
570 | return -EIO; | |
571 | ||
572 | return 0; | |
573 | } | |
574 | ||
575 | static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, | |
576 | int nb_sectors) | |
577 | { | |
999dec57 | 578 | return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); |
71d0770c AL |
579 | } |
580 | ||
19cb3738 | 581 | /* return < 0 if error. See bdrv_write() for the return codes */ |
5fafdf24 | 582 | int bdrv_read(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
583 | uint8_t *buf, int nb_sectors) |
584 | { | |
ea2384d3 FB |
585 | BlockDriver *drv = bs->drv; |
586 | ||
19cb3738 FB |
587 | if (!drv) |
588 | return -ENOMEDIUM; | |
71d0770c AL |
589 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
590 | return -EIO; | |
b338082b | 591 | |
eda578e5 | 592 | return drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
fc01f7e7 FB |
593 | } |
594 | ||
5fafdf24 | 595 | /* Return < 0 if error. Important errors are: |
19cb3738 FB |
596 | -EIO generic I/O error (may happen for all errors) |
597 | -ENOMEDIUM No media inserted. | |
598 | -EINVAL Invalid sector number or nb_sectors | |
599 | -EACCES Trying to write a read-only device | |
600 | */ | |
5fafdf24 | 601 | int bdrv_write(BlockDriverState *bs, int64_t sector_num, |
fc01f7e7 FB |
602 | const uint8_t *buf, int nb_sectors) |
603 | { | |
83f64091 | 604 | BlockDriver *drv = bs->drv; |
19cb3738 FB |
605 | if (!bs->drv) |
606 | return -ENOMEDIUM; | |
0849bf08 | 607 | if (bs->read_only) |
19cb3738 | 608 | return -EACCES; |
71d0770c AL |
609 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
610 | return -EIO; | |
611 | ||
42fb2807 | 612 | return drv->bdrv_write(bs, sector_num, buf, nb_sectors); |
83f64091 FB |
613 | } |
614 | ||
eda578e5 AL |
615 | int bdrv_pread(BlockDriverState *bs, int64_t offset, |
616 | void *buf, int count1) | |
83f64091 | 617 | { |
83f64091 FB |
618 | uint8_t tmp_buf[SECTOR_SIZE]; |
619 | int len, nb_sectors, count; | |
620 | int64_t sector_num; | |
621 | ||
622 | count = count1; | |
623 | /* first read to align to sector start */ | |
624 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); | |
625 | if (len > count) | |
626 | len = count; | |
627 | sector_num = offset >> SECTOR_BITS; | |
628 | if (len > 0) { | |
629 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
630 | return -EIO; | |
631 | memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); | |
632 | count -= len; | |
633 | if (count == 0) | |
634 | return count1; | |
635 | sector_num++; | |
636 | buf += len; | |
637 | } | |
638 | ||
639 | /* read the sectors "in place" */ | |
640 | nb_sectors = count >> SECTOR_BITS; | |
641 | if (nb_sectors > 0) { | |
642 | if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) | |
643 | return -EIO; | |
644 | sector_num += nb_sectors; | |
645 | len = nb_sectors << SECTOR_BITS; | |
646 | buf += len; | |
647 | count -= len; | |
648 | } | |
649 | ||
650 | /* add data from the last sector */ | |
651 | if (count > 0) { | |
652 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
653 | return -EIO; | |
654 | memcpy(buf, tmp_buf, count); | |
655 | } | |
656 | return count1; | |
657 | } | |
658 | ||
eda578e5 AL |
659 | int bdrv_pwrite(BlockDriverState *bs, int64_t offset, |
660 | const void *buf, int count1) | |
83f64091 | 661 | { |
83f64091 FB |
662 | uint8_t tmp_buf[SECTOR_SIZE]; |
663 | int len, nb_sectors, count; | |
664 | int64_t sector_num; | |
665 | ||
666 | count = count1; | |
667 | /* first write to align to sector start */ | |
668 | len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); | |
669 | if (len > count) | |
670 | len = count; | |
671 | sector_num = offset >> SECTOR_BITS; | |
672 | if (len > 0) { | |
673 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
674 | return -EIO; | |
675 | memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); | |
676 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) | |
677 | return -EIO; | |
678 | count -= len; | |
679 | if (count == 0) | |
680 | return count1; | |
681 | sector_num++; | |
682 | buf += len; | |
683 | } | |
684 | ||
685 | /* write the sectors "in place" */ | |
686 | nb_sectors = count >> SECTOR_BITS; | |
687 | if (nb_sectors > 0) { | |
688 | if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) | |
689 | return -EIO; | |
690 | sector_num += nb_sectors; | |
691 | len = nb_sectors << SECTOR_BITS; | |
692 | buf += len; | |
693 | count -= len; | |
694 | } | |
695 | ||
696 | /* add data from the last sector */ | |
697 | if (count > 0) { | |
698 | if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) | |
699 | return -EIO; | |
700 | memcpy(tmp_buf, buf, count); | |
701 | if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) | |
702 | return -EIO; | |
703 | } | |
704 | return count1; | |
705 | } | |
83f64091 | 706 | |
83f64091 FB |
707 | /** |
708 | * Truncate file to 'offset' bytes (needed only for file protocols) | |
709 | */ | |
710 | int bdrv_truncate(BlockDriverState *bs, int64_t offset) | |
711 | { | |
712 | BlockDriver *drv = bs->drv; | |
713 | if (!drv) | |
19cb3738 | 714 | return -ENOMEDIUM; |
83f64091 FB |
715 | if (!drv->bdrv_truncate) |
716 | return -ENOTSUP; | |
717 | return drv->bdrv_truncate(bs, offset); | |
718 | } | |
719 | ||
720 | /** | |
721 | * Length of a file in bytes. Return < 0 if error or unknown. | |
722 | */ | |
723 | int64_t bdrv_getlength(BlockDriverState *bs) | |
724 | { | |
725 | BlockDriver *drv = bs->drv; | |
726 | if (!drv) | |
19cb3738 | 727 | return -ENOMEDIUM; |
83f64091 FB |
728 | if (!drv->bdrv_getlength) { |
729 | /* legacy mode */ | |
730 | return bs->total_sectors * SECTOR_SIZE; | |
731 | } | |
732 | return drv->bdrv_getlength(bs); | |
fc01f7e7 FB |
733 | } |
734 | ||
19cb3738 | 735 | /* return 0 as number of sectors if no device present or error */ |
96b8f136 | 736 | void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) |
fc01f7e7 | 737 | { |
19cb3738 FB |
738 | int64_t length; |
739 | length = bdrv_getlength(bs); | |
740 | if (length < 0) | |
741 | length = 0; | |
742 | else | |
743 | length = length >> SECTOR_BITS; | |
744 | *nb_sectors_ptr = length; | |
fc01f7e7 | 745 | } |
cf98951b | 746 | |
f3d54fc4 AL |
747 | struct partition { |
748 | uint8_t boot_ind; /* 0x80 - active */ | |
749 | uint8_t head; /* starting head */ | |
750 | uint8_t sector; /* starting sector */ | |
751 | uint8_t cyl; /* starting cylinder */ | |
752 | uint8_t sys_ind; /* What partition type */ | |
753 | uint8_t end_head; /* end head */ | |
754 | uint8_t end_sector; /* end sector */ | |
755 | uint8_t end_cyl; /* end cylinder */ | |
756 | uint32_t start_sect; /* starting sector counting from 0 */ | |
757 | uint32_t nr_sects; /* nr of sectors in partition */ | |
758 | } __attribute__((packed)); | |
759 | ||
760 | /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ | |
761 | static int guess_disk_lchs(BlockDriverState *bs, | |
762 | int *pcylinders, int *pheads, int *psectors) | |
763 | { | |
764 | uint8_t buf[512]; | |
765 | int ret, i, heads, sectors, cylinders; | |
766 | struct partition *p; | |
767 | uint32_t nr_sects; | |
a38131b6 | 768 | uint64_t nb_sectors; |
f3d54fc4 AL |
769 | |
770 | bdrv_get_geometry(bs, &nb_sectors); | |
771 | ||
772 | ret = bdrv_read(bs, 0, buf, 1); | |
773 | if (ret < 0) | |
774 | return -1; | |
775 | /* test msdos magic */ | |
776 | if (buf[510] != 0x55 || buf[511] != 0xaa) | |
777 | return -1; | |
778 | for(i = 0; i < 4; i++) { | |
779 | p = ((struct partition *)(buf + 0x1be)) + i; | |
780 | nr_sects = le32_to_cpu(p->nr_sects); | |
781 | if (nr_sects && p->end_head) { | |
782 | /* We make the assumption that the partition terminates on | |
783 | a cylinder boundary */ | |
784 | heads = p->end_head + 1; | |
785 | sectors = p->end_sector & 63; | |
786 | if (sectors == 0) | |
787 | continue; | |
788 | cylinders = nb_sectors / (heads * sectors); | |
789 | if (cylinders < 1 || cylinders > 16383) | |
790 | continue; | |
791 | *pheads = heads; | |
792 | *psectors = sectors; | |
793 | *pcylinders = cylinders; | |
794 | #if 0 | |
795 | printf("guessed geometry: LCHS=%d %d %d\n", | |
796 | cylinders, heads, sectors); | |
797 | #endif | |
798 | return 0; | |
799 | } | |
800 | } | |
801 | return -1; | |
802 | } | |
803 | ||
804 | void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) | |
805 | { | |
806 | int translation, lba_detected = 0; | |
807 | int cylinders, heads, secs; | |
a38131b6 | 808 | uint64_t nb_sectors; |
f3d54fc4 AL |
809 | |
810 | /* if a geometry hint is available, use it */ | |
811 | bdrv_get_geometry(bs, &nb_sectors); | |
812 | bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); | |
813 | translation = bdrv_get_translation_hint(bs); | |
814 | if (cylinders != 0) { | |
815 | *pcyls = cylinders; | |
816 | *pheads = heads; | |
817 | *psecs = secs; | |
818 | } else { | |
819 | if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { | |
820 | if (heads > 16) { | |
821 | /* if heads > 16, it means that a BIOS LBA | |
822 | translation was active, so the default | |
823 | hardware geometry is OK */ | |
824 | lba_detected = 1; | |
825 | goto default_geometry; | |
826 | } else { | |
827 | *pcyls = cylinders; | |
828 | *pheads = heads; | |
829 | *psecs = secs; | |
830 | /* disable any translation to be in sync with | |
831 | the logical geometry */ | |
832 | if (translation == BIOS_ATA_TRANSLATION_AUTO) { | |
833 | bdrv_set_translation_hint(bs, | |
834 | BIOS_ATA_TRANSLATION_NONE); | |
835 | } | |
836 | } | |
837 | } else { | |
838 | default_geometry: | |
839 | /* if no geometry, use a standard physical disk geometry */ | |
840 | cylinders = nb_sectors / (16 * 63); | |
841 | ||
842 | if (cylinders > 16383) | |
843 | cylinders = 16383; | |
844 | else if (cylinders < 2) | |
845 | cylinders = 2; | |
846 | *pcyls = cylinders; | |
847 | *pheads = 16; | |
848 | *psecs = 63; | |
849 | if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { | |
850 | if ((*pcyls * *pheads) <= 131072) { | |
851 | bdrv_set_translation_hint(bs, | |
852 | BIOS_ATA_TRANSLATION_LARGE); | |
853 | } else { | |
854 | bdrv_set_translation_hint(bs, | |
855 | BIOS_ATA_TRANSLATION_LBA); | |
856 | } | |
857 | } | |
858 | } | |
859 | bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); | |
860 | } | |
861 | } | |
862 | ||
5fafdf24 | 863 | void bdrv_set_geometry_hint(BlockDriverState *bs, |
b338082b FB |
864 | int cyls, int heads, int secs) |
865 | { | |
866 | bs->cyls = cyls; | |
867 | bs->heads = heads; | |
868 | bs->secs = secs; | |
869 | } | |
870 | ||
871 | void bdrv_set_type_hint(BlockDriverState *bs, int type) | |
872 | { | |
873 | bs->type = type; | |
874 | bs->removable = ((type == BDRV_TYPE_CDROM || | |
875 | type == BDRV_TYPE_FLOPPY)); | |
876 | } | |
877 | ||
46d4767d FB |
878 | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
879 | { | |
880 | bs->translation = translation; | |
881 | } | |
882 | ||
5fafdf24 | 883 | void bdrv_get_geometry_hint(BlockDriverState *bs, |
b338082b FB |
884 | int *pcyls, int *pheads, int *psecs) |
885 | { | |
886 | *pcyls = bs->cyls; | |
887 | *pheads = bs->heads; | |
888 | *psecs = bs->secs; | |
889 | } | |
890 | ||
891 | int bdrv_get_type_hint(BlockDriverState *bs) | |
892 | { | |
893 | return bs->type; | |
894 | } | |
895 | ||
46d4767d FB |
896 | int bdrv_get_translation_hint(BlockDriverState *bs) |
897 | { | |
898 | return bs->translation; | |
899 | } | |
900 | ||
b338082b FB |
901 | int bdrv_is_removable(BlockDriverState *bs) |
902 | { | |
903 | return bs->removable; | |
904 | } | |
905 | ||
906 | int bdrv_is_read_only(BlockDriverState *bs) | |
907 | { | |
908 | return bs->read_only; | |
909 | } | |
910 | ||
985a03b0 TS |
911 | int bdrv_is_sg(BlockDriverState *bs) |
912 | { | |
913 | return bs->sg; | |
914 | } | |
915 | ||
19cb3738 | 916 | /* XXX: no longer used */ |
5fafdf24 | 917 | void bdrv_set_change_cb(BlockDriverState *bs, |
b338082b FB |
918 | void (*change_cb)(void *opaque), void *opaque) |
919 | { | |
920 | bs->change_cb = change_cb; | |
921 | bs->change_opaque = opaque; | |
922 | } | |
923 | ||
ea2384d3 FB |
924 | int bdrv_is_encrypted(BlockDriverState *bs) |
925 | { | |
926 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
927 | return 1; | |
928 | return bs->encrypted; | |
929 | } | |
930 | ||
c0f4ce77 AL |
931 | int bdrv_key_required(BlockDriverState *bs) |
932 | { | |
933 | BlockDriverState *backing_hd = bs->backing_hd; | |
934 | ||
935 | if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) | |
936 | return 1; | |
937 | return (bs->encrypted && !bs->valid_key); | |
938 | } | |
939 | ||
ea2384d3 FB |
940 | int bdrv_set_key(BlockDriverState *bs, const char *key) |
941 | { | |
942 | int ret; | |
943 | if (bs->backing_hd && bs->backing_hd->encrypted) { | |
944 | ret = bdrv_set_key(bs->backing_hd, key); | |
945 | if (ret < 0) | |
946 | return ret; | |
947 | if (!bs->encrypted) | |
948 | return 0; | |
949 | } | |
950 | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) | |
951 | return -1; | |
c0f4ce77 | 952 | ret = bs->drv->bdrv_set_key(bs, key); |
bb5fc20f AL |
953 | if (ret < 0) { |
954 | bs->valid_key = 0; | |
955 | } else if (!bs->valid_key) { | |
956 | bs->valid_key = 1; | |
957 | /* call the change callback now, we skipped it on open */ | |
958 | bs->media_changed = 1; | |
959 | if (bs->change_cb) | |
960 | bs->change_cb(bs->change_opaque); | |
961 | } | |
c0f4ce77 | 962 | return ret; |
ea2384d3 FB |
963 | } |
964 | ||
965 | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) | |
966 | { | |
19cb3738 | 967 | if (!bs->drv) { |
ea2384d3 FB |
968 | buf[0] = '\0'; |
969 | } else { | |
970 | pstrcpy(buf, buf_size, bs->drv->format_name); | |
971 | } | |
972 | } | |
973 | ||
5fafdf24 | 974 | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
ea2384d3 FB |
975 | void *opaque) |
976 | { | |
977 | BlockDriver *drv; | |
978 | ||
979 | for (drv = first_drv; drv != NULL; drv = drv->next) { | |
980 | it(opaque, drv->format_name); | |
981 | } | |
982 | } | |
983 | ||
b338082b FB |
984 | BlockDriverState *bdrv_find(const char *name) |
985 | { | |
986 | BlockDriverState *bs; | |
987 | ||
988 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
989 | if (!strcmp(name, bs->device_name)) | |
990 | return bs; | |
991 | } | |
992 | return NULL; | |
993 | } | |
994 | ||
51de9760 | 995 | void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) |
81d0912d FB |
996 | { |
997 | BlockDriverState *bs; | |
998 | ||
999 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
51de9760 | 1000 | it(opaque, bs); |
81d0912d FB |
1001 | } |
1002 | } | |
1003 | ||
ea2384d3 FB |
1004 | const char *bdrv_get_device_name(BlockDriverState *bs) |
1005 | { | |
1006 | return bs->device_name; | |
1007 | } | |
1008 | ||
7a6cba61 PB |
1009 | void bdrv_flush(BlockDriverState *bs) |
1010 | { | |
081501da AL |
1011 | if (!bs->drv) |
1012 | return; | |
7a6cba61 PB |
1013 | if (bs->drv->bdrv_flush) |
1014 | bs->drv->bdrv_flush(bs); | |
1015 | if (bs->backing_hd) | |
1016 | bdrv_flush(bs->backing_hd); | |
1017 | } | |
1018 | ||
c6ca28d6 AL |
1019 | void bdrv_flush_all(void) |
1020 | { | |
1021 | BlockDriverState *bs; | |
1022 | ||
1023 | for (bs = bdrv_first; bs != NULL; bs = bs->next) | |
1024 | if (bs->drv && !bdrv_is_read_only(bs) && | |
1025 | (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) | |
1026 | bdrv_flush(bs); | |
1027 | } | |
1028 | ||
f58c7b35 TS |
1029 | /* |
1030 | * Returns true iff the specified sector is present in the disk image. Drivers | |
1031 | * not implementing the functionality are assumed to not support backing files, | |
1032 | * hence all their sectors are reported as allocated. | |
1033 | * | |
1034 | * 'pnum' is set to the number of sectors (including and immediately following | |
1035 | * the specified sector) that are known to be in the same | |
1036 | * allocated/unallocated state. | |
1037 | * | |
1038 | * 'nb_sectors' is the max value 'pnum' should be set to. | |
1039 | */ | |
1040 | int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, | |
1041 | int *pnum) | |
1042 | { | |
1043 | int64_t n; | |
1044 | if (!bs->drv->bdrv_is_allocated) { | |
1045 | if (sector_num >= bs->total_sectors) { | |
1046 | *pnum = 0; | |
1047 | return 0; | |
1048 | } | |
1049 | n = bs->total_sectors - sector_num; | |
1050 | *pnum = (n < nb_sectors) ? (n) : (nb_sectors); | |
1051 | return 1; | |
1052 | } | |
1053 | return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); | |
1054 | } | |
1055 | ||
376253ec | 1056 | void bdrv_info(Monitor *mon) |
b338082b FB |
1057 | { |
1058 | BlockDriverState *bs; | |
1059 | ||
1060 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
376253ec AL |
1061 | monitor_printf(mon, "%s:", bs->device_name); |
1062 | monitor_printf(mon, " type="); | |
b338082b FB |
1063 | switch(bs->type) { |
1064 | case BDRV_TYPE_HD: | |
376253ec | 1065 | monitor_printf(mon, "hd"); |
b338082b FB |
1066 | break; |
1067 | case BDRV_TYPE_CDROM: | |
376253ec | 1068 | monitor_printf(mon, "cdrom"); |
b338082b FB |
1069 | break; |
1070 | case BDRV_TYPE_FLOPPY: | |
376253ec | 1071 | monitor_printf(mon, "floppy"); |
b338082b FB |
1072 | break; |
1073 | } | |
376253ec | 1074 | monitor_printf(mon, " removable=%d", bs->removable); |
b338082b | 1075 | if (bs->removable) { |
376253ec | 1076 | monitor_printf(mon, " locked=%d", bs->locked); |
b338082b | 1077 | } |
19cb3738 | 1078 | if (bs->drv) { |
376253ec AL |
1079 | monitor_printf(mon, " file="); |
1080 | monitor_print_filename(mon, bs->filename); | |
fef30743 | 1081 | if (bs->backing_file[0] != '\0') { |
376253ec AL |
1082 | monitor_printf(mon, " backing_file="); |
1083 | monitor_print_filename(mon, bs->backing_file); | |
1084 | } | |
1085 | monitor_printf(mon, " ro=%d", bs->read_only); | |
1086 | monitor_printf(mon, " drv=%s", bs->drv->format_name); | |
1087 | monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs)); | |
b338082b | 1088 | } else { |
376253ec | 1089 | monitor_printf(mon, " [not inserted]"); |
b338082b | 1090 | } |
376253ec | 1091 | monitor_printf(mon, "\n"); |
b338082b FB |
1092 | } |
1093 | } | |
a36e69dd TS |
1094 | |
1095 | /* The "info blockstats" command. */ | |
376253ec | 1096 | void bdrv_info_stats(Monitor *mon) |
a36e69dd TS |
1097 | { |
1098 | BlockDriverState *bs; | |
1099 | ||
1100 | for (bs = bdrv_first; bs != NULL; bs = bs->next) { | |
376253ec AL |
1101 | monitor_printf(mon, "%s:" |
1102 | " rd_bytes=%" PRIu64 | |
1103 | " wr_bytes=%" PRIu64 | |
1104 | " rd_operations=%" PRIu64 | |
1105 | " wr_operations=%" PRIu64 | |
ebf53fcd | 1106 | "\n", |
376253ec AL |
1107 | bs->device_name, |
1108 | bs->rd_bytes, bs->wr_bytes, | |
1109 | bs->rd_ops, bs->wr_ops); | |
a36e69dd TS |
1110 | } |
1111 | } | |
ea2384d3 | 1112 | |
045df330 AL |
1113 | const char *bdrv_get_encrypted_filename(BlockDriverState *bs) |
1114 | { | |
1115 | if (bs->backing_hd && bs->backing_hd->encrypted) | |
1116 | return bs->backing_file; | |
1117 | else if (bs->encrypted) | |
1118 | return bs->filename; | |
1119 | else | |
1120 | return NULL; | |
1121 | } | |
1122 | ||
5fafdf24 | 1123 | void bdrv_get_backing_filename(BlockDriverState *bs, |
83f64091 FB |
1124 | char *filename, int filename_size) |
1125 | { | |
1126 | if (!bs->backing_hd) { | |
1127 | pstrcpy(filename, filename_size, ""); | |
1128 | } else { | |
1129 | pstrcpy(filename, filename_size, bs->backing_file); | |
1130 | } | |
1131 | } | |
1132 | ||
5fafdf24 | 1133 | int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, |
faea38e7 FB |
1134 | const uint8_t *buf, int nb_sectors) |
1135 | { | |
1136 | BlockDriver *drv = bs->drv; | |
1137 | if (!drv) | |
19cb3738 | 1138 | return -ENOMEDIUM; |
faea38e7 FB |
1139 | if (!drv->bdrv_write_compressed) |
1140 | return -ENOTSUP; | |
1141 | return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); | |
1142 | } | |
3b46e624 | 1143 | |
faea38e7 FB |
1144 | int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
1145 | { | |
1146 | BlockDriver *drv = bs->drv; | |
1147 | if (!drv) | |
19cb3738 | 1148 | return -ENOMEDIUM; |
faea38e7 FB |
1149 | if (!drv->bdrv_get_info) |
1150 | return -ENOTSUP; | |
1151 | memset(bdi, 0, sizeof(*bdi)); | |
1152 | return drv->bdrv_get_info(bs, bdi); | |
1153 | } | |
1154 | ||
178e08a5 AL |
1155 | int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) |
1156 | { | |
1157 | BlockDriver *drv = bs->drv; | |
1158 | if (!drv) | |
1159 | return -ENOMEDIUM; | |
1160 | if (!drv->bdrv_put_buffer) | |
1161 | return -ENOTSUP; | |
1162 | return drv->bdrv_put_buffer(bs, buf, pos, size); | |
1163 | } | |
1164 | ||
1165 | int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size) | |
1166 | { | |
1167 | BlockDriver *drv = bs->drv; | |
1168 | if (!drv) | |
1169 | return -ENOMEDIUM; | |
1170 | if (!drv->bdrv_get_buffer) | |
1171 | return -ENOTSUP; | |
1172 | return drv->bdrv_get_buffer(bs, buf, pos, size); | |
1173 | } | |
1174 | ||
faea38e7 FB |
1175 | /**************************************************************/ |
1176 | /* handling of snapshots */ | |
1177 | ||
5fafdf24 | 1178 | int bdrv_snapshot_create(BlockDriverState *bs, |
faea38e7 FB |
1179 | QEMUSnapshotInfo *sn_info) |
1180 | { | |
1181 | BlockDriver *drv = bs->drv; | |
1182 | if (!drv) | |
19cb3738 | 1183 | return -ENOMEDIUM; |
faea38e7 FB |
1184 | if (!drv->bdrv_snapshot_create) |
1185 | return -ENOTSUP; | |
1186 | return drv->bdrv_snapshot_create(bs, sn_info); | |
1187 | } | |
1188 | ||
5fafdf24 | 1189 | int bdrv_snapshot_goto(BlockDriverState *bs, |
faea38e7 FB |
1190 | const char *snapshot_id) |
1191 | { | |
1192 | BlockDriver *drv = bs->drv; | |
1193 | if (!drv) | |
19cb3738 | 1194 | return -ENOMEDIUM; |
faea38e7 FB |
1195 | if (!drv->bdrv_snapshot_goto) |
1196 | return -ENOTSUP; | |
1197 | return drv->bdrv_snapshot_goto(bs, snapshot_id); | |
1198 | } | |
1199 | ||
1200 | int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) | |
1201 | { | |
1202 | BlockDriver *drv = bs->drv; | |
1203 | if (!drv) | |
19cb3738 | 1204 | return -ENOMEDIUM; |
faea38e7 FB |
1205 | if (!drv->bdrv_snapshot_delete) |
1206 | return -ENOTSUP; | |
1207 | return drv->bdrv_snapshot_delete(bs, snapshot_id); | |
1208 | } | |
1209 | ||
5fafdf24 | 1210 | int bdrv_snapshot_list(BlockDriverState *bs, |
faea38e7 FB |
1211 | QEMUSnapshotInfo **psn_info) |
1212 | { | |
1213 | BlockDriver *drv = bs->drv; | |
1214 | if (!drv) | |
19cb3738 | 1215 | return -ENOMEDIUM; |
faea38e7 FB |
1216 | if (!drv->bdrv_snapshot_list) |
1217 | return -ENOTSUP; | |
1218 | return drv->bdrv_snapshot_list(bs, psn_info); | |
1219 | } | |
1220 | ||
1221 | #define NB_SUFFIXES 4 | |
1222 | ||
1223 | char *get_human_readable_size(char *buf, int buf_size, int64_t size) | |
1224 | { | |
1225 | static const char suffixes[NB_SUFFIXES] = "KMGT"; | |
1226 | int64_t base; | |
1227 | int i; | |
1228 | ||
1229 | if (size <= 999) { | |
1230 | snprintf(buf, buf_size, "%" PRId64, size); | |
1231 | } else { | |
1232 | base = 1024; | |
1233 | for(i = 0; i < NB_SUFFIXES; i++) { | |
1234 | if (size < (10 * base)) { | |
5fafdf24 | 1235 | snprintf(buf, buf_size, "%0.1f%c", |
faea38e7 FB |
1236 | (double)size / base, |
1237 | suffixes[i]); | |
1238 | break; | |
1239 | } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { | |
5fafdf24 | 1240 | snprintf(buf, buf_size, "%" PRId64 "%c", |
faea38e7 FB |
1241 | ((size + (base >> 1)) / base), |
1242 | suffixes[i]); | |
1243 | break; | |
1244 | } | |
1245 | base = base * 1024; | |
1246 | } | |
1247 | } | |
1248 | return buf; | |
1249 | } | |
1250 | ||
1251 | char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) | |
1252 | { | |
1253 | char buf1[128], date_buf[128], clock_buf[128]; | |
3b9f94e1 FB |
1254 | #ifdef _WIN32 |
1255 | struct tm *ptm; | |
1256 | #else | |
faea38e7 | 1257 | struct tm tm; |
3b9f94e1 | 1258 | #endif |
faea38e7 FB |
1259 | time_t ti; |
1260 | int64_t secs; | |
1261 | ||
1262 | if (!sn) { | |
5fafdf24 TS |
1263 | snprintf(buf, buf_size, |
1264 | "%-10s%-20s%7s%20s%15s", | |
faea38e7 FB |
1265 | "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); |
1266 | } else { | |
1267 | ti = sn->date_sec; | |
3b9f94e1 FB |
1268 | #ifdef _WIN32 |
1269 | ptm = localtime(&ti); | |
1270 | strftime(date_buf, sizeof(date_buf), | |
1271 | "%Y-%m-%d %H:%M:%S", ptm); | |
1272 | #else | |
faea38e7 FB |
1273 | localtime_r(&ti, &tm); |
1274 | strftime(date_buf, sizeof(date_buf), | |
1275 | "%Y-%m-%d %H:%M:%S", &tm); | |
3b9f94e1 | 1276 | #endif |
faea38e7 FB |
1277 | secs = sn->vm_clock_nsec / 1000000000; |
1278 | snprintf(clock_buf, sizeof(clock_buf), | |
1279 | "%02d:%02d:%02d.%03d", | |
1280 | (int)(secs / 3600), | |
1281 | (int)((secs / 60) % 60), | |
5fafdf24 | 1282 | (int)(secs % 60), |
faea38e7 FB |
1283 | (int)((sn->vm_clock_nsec / 1000000) % 1000)); |
1284 | snprintf(buf, buf_size, | |
5fafdf24 | 1285 | "%-10s%-20s%7s%20s%15s", |
faea38e7 FB |
1286 | sn->id_str, sn->name, |
1287 | get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), | |
1288 | date_buf, | |
1289 | clock_buf); | |
1290 | } | |
1291 | return buf; | |
1292 | } | |
1293 | ||
83f64091 | 1294 | |
ea2384d3 | 1295 | /**************************************************************/ |
83f64091 | 1296 | /* async I/Os */ |
ea2384d3 | 1297 | |
3fb94d56 AL |
1298 | typedef struct VectorTranslationAIOCB { |
1299 | BlockDriverAIOCB common; | |
3b69e4b9 AL |
1300 | QEMUIOVector *iov; |
1301 | uint8_t *bounce; | |
1302 | int is_write; | |
1303 | BlockDriverAIOCB *aiocb; | |
3fb94d56 | 1304 | } VectorTranslationAIOCB; |
3b69e4b9 | 1305 | |
3fb94d56 | 1306 | static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb) |
c07a9008 | 1307 | { |
3fb94d56 AL |
1308 | VectorTranslationAIOCB *acb |
1309 | = container_of(_acb, VectorTranslationAIOCB, common); | |
c07a9008 | 1310 | |
3fb94d56 | 1311 | bdrv_aio_cancel(acb->aiocb); |
c07a9008 AL |
1312 | } |
1313 | ||
3b69e4b9 AL |
1314 | static void bdrv_aio_rw_vector_cb(void *opaque, int ret) |
1315 | { | |
3fb94d56 | 1316 | VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque; |
3b69e4b9 AL |
1317 | |
1318 | if (!s->is_write) { | |
249aa745 | 1319 | qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size); |
3b69e4b9 | 1320 | } |
d905dba4 | 1321 | qemu_vfree(s->bounce); |
3fb94d56 AL |
1322 | s->common.cb(s->common.opaque, ret); |
1323 | qemu_aio_release(s); | |
3b69e4b9 AL |
1324 | } |
1325 | ||
1326 | static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, | |
1327 | int64_t sector_num, | |
1328 | QEMUIOVector *iov, | |
1329 | int nb_sectors, | |
1330 | BlockDriverCompletionFunc *cb, | |
1331 | void *opaque, | |
1332 | int is_write) | |
1333 | ||
1334 | { | |
3fb94d56 AL |
1335 | VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs, |
1336 | cb, opaque); | |
3b69e4b9 | 1337 | |
3b69e4b9 AL |
1338 | s->iov = iov; |
1339 | s->bounce = qemu_memalign(512, nb_sectors * 512); | |
1340 | s->is_write = is_write; | |
1341 | if (is_write) { | |
1342 | qemu_iovec_to_buffer(s->iov, s->bounce); | |
1343 | s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors, | |
1344 | bdrv_aio_rw_vector_cb, s); | |
1345 | } else { | |
1346 | s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors, | |
1347 | bdrv_aio_rw_vector_cb, s); | |
1348 | } | |
c240b9af AL |
1349 | if (!s->aiocb) { |
1350 | qemu_vfree(s->bounce); | |
1351 | qemu_aio_release(s); | |
1352 | return NULL; | |
1353 | } | |
3fb94d56 | 1354 | return &s->common; |
3b69e4b9 AL |
1355 | } |
1356 | ||
1357 | BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, | |
1358 | QEMUIOVector *iov, int nb_sectors, | |
1359 | BlockDriverCompletionFunc *cb, void *opaque) | |
1360 | { | |
71d0770c AL |
1361 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1362 | return NULL; | |
1363 | ||
3b69e4b9 AL |
1364 | return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, |
1365 | cb, opaque, 0); | |
1366 | } | |
1367 | ||
1368 | BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, | |
1369 | QEMUIOVector *iov, int nb_sectors, | |
1370 | BlockDriverCompletionFunc *cb, void *opaque) | |
1371 | { | |
71d0770c AL |
1372 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1373 | return NULL; | |
1374 | ||
3b69e4b9 AL |
1375 | return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, |
1376 | cb, opaque, 1); | |
1377 | } | |
1378 | ||
c87c0672 | 1379 | static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, |
ce1a14dc PB |
1380 | uint8_t *buf, int nb_sectors, |
1381 | BlockDriverCompletionFunc *cb, void *opaque) | |
83f64091 FB |
1382 | { |
1383 | BlockDriver *drv = bs->drv; | |
a36e69dd | 1384 | BlockDriverAIOCB *ret; |
83f64091 | 1385 | |
19cb3738 | 1386 | if (!drv) |
ce1a14dc | 1387 | return NULL; |
71d0770c AL |
1388 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1389 | return NULL; | |
3b46e624 | 1390 | |
a36e69dd TS |
1391 | ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); |
1392 | ||
1393 | if (ret) { | |
1394 | /* Update stats even though technically transfer has not happened. */ | |
1395 | bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; | |
1396 | bs->rd_ops ++; | |
1397 | } | |
1398 | ||
1399 | return ret; | |
ea2384d3 FB |
1400 | } |
1401 | ||
c87c0672 | 1402 | static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, |
ce1a14dc PB |
1403 | const uint8_t *buf, int nb_sectors, |
1404 | BlockDriverCompletionFunc *cb, void *opaque) | |
ea2384d3 | 1405 | { |
83f64091 | 1406 | BlockDriver *drv = bs->drv; |
a36e69dd | 1407 | BlockDriverAIOCB *ret; |
ea2384d3 | 1408 | |
19cb3738 | 1409 | if (!drv) |
ce1a14dc | 1410 | return NULL; |
83f64091 | 1411 | if (bs->read_only) |
ce1a14dc | 1412 | return NULL; |
71d0770c AL |
1413 | if (bdrv_check_request(bs, sector_num, nb_sectors)) |
1414 | return NULL; | |
83f64091 | 1415 | |
a36e69dd TS |
1416 | ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); |
1417 | ||
1418 | if (ret) { | |
1419 | /* Update stats even though technically transfer has not happened. */ | |
1420 | bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; | |
1421 | bs->wr_ops ++; | |
1422 | } | |
1423 | ||
1424 | return ret; | |
83f64091 FB |
1425 | } |
1426 | ||
1427 | void bdrv_aio_cancel(BlockDriverAIOCB *acb) | |
83f64091 | 1428 | { |
6bbff9a0 | 1429 | acb->pool->cancel(acb); |
83f64091 FB |
1430 | } |
1431 | ||
ce1a14dc | 1432 | |
83f64091 FB |
1433 | /**************************************************************/ |
1434 | /* async block device emulation */ | |
1435 | ||
ce1a14dc | 1436 | static void bdrv_aio_bh_cb(void *opaque) |
83f64091 | 1437 | { |
ce1a14dc PB |
1438 | BlockDriverAIOCBSync *acb = opaque; |
1439 | acb->common.cb(acb->common.opaque, acb->ret); | |
1440 | qemu_aio_release(acb); | |
83f64091 | 1441 | } |
beac80cd | 1442 | |
ce1a14dc PB |
1443 | static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, |
1444 | int64_t sector_num, uint8_t *buf, int nb_sectors, | |
1445 | BlockDriverCompletionFunc *cb, void *opaque) | |
83f64091 | 1446 | { |
ce1a14dc | 1447 | BlockDriverAIOCBSync *acb; |
83f64091 | 1448 | int ret; |
ce1a14dc PB |
1449 | |
1450 | acb = qemu_aio_get(bs, cb, opaque); | |
1451 | if (!acb->bh) | |
1452 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
1453 | ret = bdrv_read(bs, sector_num, buf, nb_sectors); | |
1454 | acb->ret = ret; | |
1455 | qemu_bh_schedule(acb->bh); | |
1456 | return &acb->common; | |
beac80cd FB |
1457 | } |
1458 | ||
ce1a14dc PB |
1459 | static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, |
1460 | int64_t sector_num, const uint8_t *buf, int nb_sectors, | |
1461 | BlockDriverCompletionFunc *cb, void *opaque) | |
beac80cd | 1462 | { |
ce1a14dc | 1463 | BlockDriverAIOCBSync *acb; |
83f64091 | 1464 | int ret; |
83f64091 | 1465 | |
ce1a14dc PB |
1466 | acb = qemu_aio_get(bs, cb, opaque); |
1467 | if (!acb->bh) | |
1468 | acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); | |
1469 | ret = bdrv_write(bs, sector_num, buf, nb_sectors); | |
1470 | acb->ret = ret; | |
1471 | qemu_bh_schedule(acb->bh); | |
1472 | return &acb->common; | |
beac80cd | 1473 | } |
beac80cd | 1474 | |
ce1a14dc | 1475 | static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) |
ea2384d3 | 1476 | { |
ce1a14dc PB |
1477 | BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; |
1478 | qemu_bh_cancel(acb->bh); | |
1479 | qemu_aio_release(acb); | |
83f64091 | 1480 | } |
ea2384d3 | 1481 | |
83f64091 FB |
1482 | /**************************************************************/ |
1483 | /* sync block device emulation */ | |
ea2384d3 | 1484 | |
83f64091 FB |
1485 | static void bdrv_rw_em_cb(void *opaque, int ret) |
1486 | { | |
1487 | *(int *)opaque = ret; | |
ea2384d3 FB |
1488 | } |
1489 | ||
83f64091 FB |
1490 | #define NOT_DONE 0x7fffffff |
1491 | ||
5fafdf24 | 1492 | static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, |
83f64091 | 1493 | uint8_t *buf, int nb_sectors) |
7a6cba61 | 1494 | { |
ce1a14dc PB |
1495 | int async_ret; |
1496 | BlockDriverAIOCB *acb; | |
83f64091 | 1497 | |
83f64091 | 1498 | async_ret = NOT_DONE; |
5fafdf24 | 1499 | acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, |
83f64091 | 1500 | bdrv_rw_em_cb, &async_ret); |
baf35cb9 | 1501 | if (acb == NULL) |
ce1a14dc | 1502 | return -1; |
baf35cb9 | 1503 | |
83f64091 FB |
1504 | while (async_ret == NOT_DONE) { |
1505 | qemu_aio_wait(); | |
1506 | } | |
baf35cb9 | 1507 | |
83f64091 | 1508 | return async_ret; |
7a6cba61 PB |
1509 | } |
1510 | ||
83f64091 FB |
1511 | static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, |
1512 | const uint8_t *buf, int nb_sectors) | |
1513 | { | |
ce1a14dc PB |
1514 | int async_ret; |
1515 | BlockDriverAIOCB *acb; | |
83f64091 | 1516 | |
83f64091 | 1517 | async_ret = NOT_DONE; |
5fafdf24 | 1518 | acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, |
83f64091 | 1519 | bdrv_rw_em_cb, &async_ret); |
baf35cb9 | 1520 | if (acb == NULL) |
ce1a14dc | 1521 | return -1; |
83f64091 FB |
1522 | while (async_ret == NOT_DONE) { |
1523 | qemu_aio_wait(); | |
1524 | } | |
83f64091 FB |
1525 | return async_ret; |
1526 | } | |
ea2384d3 FB |
1527 | |
1528 | void bdrv_init(void) | |
1529 | { | |
3fb94d56 | 1530 | aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB), |
c07a9008 AL |
1531 | bdrv_aio_cancel_vector); |
1532 | ||
ea2384d3 | 1533 | bdrv_register(&bdrv_raw); |
19cb3738 | 1534 | bdrv_register(&bdrv_host_device); |
ea2384d3 FB |
1535 | #ifndef _WIN32 |
1536 | bdrv_register(&bdrv_cow); | |
1537 | #endif | |
1538 | bdrv_register(&bdrv_qcow); | |
1539 | bdrv_register(&bdrv_vmdk); | |
3c56521b | 1540 | bdrv_register(&bdrv_cloop); |
585d0ed9 | 1541 | bdrv_register(&bdrv_dmg); |
a8753c34 | 1542 | bdrv_register(&bdrv_bochs); |
6a0f9e82 | 1543 | bdrv_register(&bdrv_vpc); |
712e7874 | 1544 | bdrv_register(&bdrv_vvfat); |
faea38e7 | 1545 | bdrv_register(&bdrv_qcow2); |
6ada7453 | 1546 | bdrv_register(&bdrv_parallels); |
75818250 | 1547 | bdrv_register(&bdrv_nbd); |
ea2384d3 | 1548 | } |
ce1a14dc | 1549 | |
6bbff9a0 AL |
1550 | void aio_pool_init(AIOPool *pool, int aiocb_size, |
1551 | void (*cancel)(BlockDriverAIOCB *acb)) | |
1552 | { | |
1553 | pool->aiocb_size = aiocb_size; | |
1554 | pool->cancel = cancel; | |
1555 | pool->free_aiocb = NULL; | |
1556 | } | |
1557 | ||
1558 | void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs, | |
1559 | BlockDriverCompletionFunc *cb, void *opaque) | |
ce1a14dc | 1560 | { |
ce1a14dc PB |
1561 | BlockDriverAIOCB *acb; |
1562 | ||
6bbff9a0 AL |
1563 | if (pool->free_aiocb) { |
1564 | acb = pool->free_aiocb; | |
1565 | pool->free_aiocb = acb->next; | |
ce1a14dc | 1566 | } else { |
6bbff9a0 AL |
1567 | acb = qemu_mallocz(pool->aiocb_size); |
1568 | acb->pool = pool; | |
ce1a14dc PB |
1569 | } |
1570 | acb->bs = bs; | |
1571 | acb->cb = cb; | |
1572 | acb->opaque = opaque; | |
1573 | return acb; | |
1574 | } | |
1575 | ||
6bbff9a0 AL |
1576 | void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, |
1577 | void *opaque) | |
1578 | { | |
1579 | return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque); | |
1580 | } | |
1581 | ||
ce1a14dc PB |
1582 | void qemu_aio_release(void *p) |
1583 | { | |
6bbff9a0 AL |
1584 | BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; |
1585 | AIOPool *pool = acb->pool; | |
1586 | acb->next = pool->free_aiocb; | |
1587 | pool->free_aiocb = acb; | |
ce1a14dc | 1588 | } |
19cb3738 FB |
1589 | |
1590 | /**************************************************************/ | |
1591 | /* removable device support */ | |
1592 | ||
1593 | /** | |
1594 | * Return TRUE if the media is present | |
1595 | */ | |
1596 | int bdrv_is_inserted(BlockDriverState *bs) | |
1597 | { | |
1598 | BlockDriver *drv = bs->drv; | |
1599 | int ret; | |
1600 | if (!drv) | |
1601 | return 0; | |
1602 | if (!drv->bdrv_is_inserted) | |
1603 | return 1; | |
1604 | ret = drv->bdrv_is_inserted(bs); | |
1605 | return ret; | |
1606 | } | |
1607 | ||
1608 | /** | |
1609 | * Return TRUE if the media changed since the last call to this | |
5fafdf24 | 1610 | * function. It is currently only used for floppy disks |
19cb3738 FB |
1611 | */ |
1612 | int bdrv_media_changed(BlockDriverState *bs) | |
1613 | { | |
1614 | BlockDriver *drv = bs->drv; | |
1615 | int ret; | |
1616 | ||
1617 | if (!drv || !drv->bdrv_media_changed) | |
1618 | ret = -ENOTSUP; | |
1619 | else | |
1620 | ret = drv->bdrv_media_changed(bs); | |
1621 | if (ret == -ENOTSUP) | |
1622 | ret = bs->media_changed; | |
1623 | bs->media_changed = 0; | |
1624 | return ret; | |
1625 | } | |
1626 | ||
1627 | /** | |
1628 | * If eject_flag is TRUE, eject the media. Otherwise, close the tray | |
1629 | */ | |
1630 | void bdrv_eject(BlockDriverState *bs, int eject_flag) | |
1631 | { | |
1632 | BlockDriver *drv = bs->drv; | |
1633 | int ret; | |
1634 | ||
1635 | if (!drv || !drv->bdrv_eject) { | |
1636 | ret = -ENOTSUP; | |
1637 | } else { | |
1638 | ret = drv->bdrv_eject(bs, eject_flag); | |
1639 | } | |
1640 | if (ret == -ENOTSUP) { | |
1641 | if (eject_flag) | |
1642 | bdrv_close(bs); | |
1643 | } | |
1644 | } | |
1645 | ||
1646 | int bdrv_is_locked(BlockDriverState *bs) | |
1647 | { | |
1648 | return bs->locked; | |
1649 | } | |
1650 | ||
1651 | /** | |
1652 | * Lock or unlock the media (if it is locked, the user won't be able | |
1653 | * to eject it manually). | |
1654 | */ | |
1655 | void bdrv_set_locked(BlockDriverState *bs, int locked) | |
1656 | { | |
1657 | BlockDriver *drv = bs->drv; | |
1658 | ||
1659 | bs->locked = locked; | |
1660 | if (drv && drv->bdrv_set_locked) { | |
1661 | drv->bdrv_set_locked(bs, locked); | |
1662 | } | |
1663 | } | |
985a03b0 TS |
1664 | |
1665 | /* needed for generic scsi interface */ | |
1666 | ||
1667 | int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) | |
1668 | { | |
1669 | BlockDriver *drv = bs->drv; | |
1670 | ||
1671 | if (drv && drv->bdrv_ioctl) | |
1672 | return drv->bdrv_ioctl(bs, req, buf); | |
1673 | return -ENOTSUP; | |
1674 | } | |
7d780669 | 1675 | |
221f715d AL |
1676 | BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, |
1677 | unsigned long int req, void *buf, | |
1678 | BlockDriverCompletionFunc *cb, void *opaque) | |
7d780669 | 1679 | { |
221f715d | 1680 | BlockDriver *drv = bs->drv; |
7d780669 | 1681 | |
221f715d AL |
1682 | if (drv && drv->bdrv_aio_ioctl) |
1683 | return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); | |
1684 | return NULL; | |
7d780669 | 1685 | } |