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