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