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