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