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