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