]> git.proxmox.com Git - qemu.git/blame - block.c
qcow2: Fix corruption after error in update_refcount
[qemu.git] / block.c
CommitLineData
fc01f7e7
FB
1/*
2 * QEMU System Emulator block driver
5fafdf24 3 *
fc01f7e7 4 * Copyright (c) 2003 Fabrice Bellard
5fafdf24 5 *
fc01f7e7
FB
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
3990d09a 24#include "config-host.h"
faf07963 25#include "qemu-common.h"
376253ec 26#include "monitor.h"
ea2384d3 27#include "block_int.h"
5efa9d5a 28#include "module.h"
d15e5465 29#include "qemu-objects.h"
fc01f7e7 30
71e72a19 31#ifdef CONFIG_BSD
7674e7bf
FB
32#include <sys/types.h>
33#include <sys/stat.h>
34#include <sys/ioctl.h>
72cf2d4f 35#include <sys/queue.h>
c5e97233 36#ifndef __DragonFly__
7674e7bf
FB
37#include <sys/disk.h>
38#endif
c5e97233 39#endif
7674e7bf 40
49dc768d
AL
41#ifdef _WIN32
42#include <windows.h>
43#endif
44
f141eafe
AL
45static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
c87c0672 47 BlockDriverCompletionFunc *cb, void *opaque);
f141eafe
AL
48static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 50 BlockDriverCompletionFunc *cb, void *opaque);
b2e12bc6
CH
51static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52 BlockDriverCompletionFunc *cb, void *opaque);
016f5cf6
AG
53static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
54 BlockDriverCompletionFunc *cb, void *opaque);
5fafdf24 55static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091
FB
56 uint8_t *buf, int nb_sectors);
57static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58 const uint8_t *buf, int nb_sectors);
ec530c81 59
1b7bdbc1
SH
60static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
61 QTAILQ_HEAD_INITIALIZER(bdrv_states);
7ee930d0 62
8a22f02a
SH
63static QLIST_HEAD(, BlockDriver) bdrv_drivers =
64 QLIST_HEAD_INITIALIZER(bdrv_drivers);
ea2384d3 65
eb852011
MA
66/* If non-zero, use only whitelisted block drivers */
67static int use_bdrv_whitelist;
68
83f64091 69int path_is_absolute(const char *path)
3b0d4f61 70{
83f64091 71 const char *p;
21664424
FB
72#ifdef _WIN32
73 /* specific case for names like: "\\.\d:" */
74 if (*path == '/' || *path == '\\')
75 return 1;
76#endif
83f64091
FB
77 p = strchr(path, ':');
78 if (p)
79 p++;
80 else
81 p = path;
3b9f94e1
FB
82#ifdef _WIN32
83 return (*p == '/' || *p == '\\');
84#else
85 return (*p == '/');
86#endif
3b0d4f61
FB
87}
88
83f64091
FB
89/* if filename is absolute, just copy it to dest. Otherwise, build a
90 path to it by considering it is relative to base_path. URL are
91 supported. */
92void path_combine(char *dest, int dest_size,
93 const char *base_path,
94 const char *filename)
3b0d4f61 95{
83f64091
FB
96 const char *p, *p1;
97 int len;
98
99 if (dest_size <= 0)
100 return;
101 if (path_is_absolute(filename)) {
102 pstrcpy(dest, dest_size, filename);
103 } else {
104 p = strchr(base_path, ':');
105 if (p)
106 p++;
107 else
108 p = base_path;
3b9f94e1
FB
109 p1 = strrchr(base_path, '/');
110#ifdef _WIN32
111 {
112 const char *p2;
113 p2 = strrchr(base_path, '\\');
114 if (!p1 || p2 > p1)
115 p1 = p2;
116 }
117#endif
83f64091
FB
118 if (p1)
119 p1++;
120 else
121 p1 = base_path;
122 if (p1 > p)
123 p = p1;
124 len = p - base_path;
125 if (len > dest_size - 1)
126 len = dest_size - 1;
127 memcpy(dest, base_path, len);
128 dest[len] = '\0';
129 pstrcat(dest, dest_size, filename);
3b0d4f61 130 }
3b0d4f61
FB
131}
132
5efa9d5a 133void bdrv_register(BlockDriver *bdrv)
ea2384d3 134{
f141eafe 135 if (!bdrv->bdrv_aio_readv) {
83f64091 136 /* add AIO emulation layer */
f141eafe
AL
137 bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
138 bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
eda578e5 139 } else if (!bdrv->bdrv_read) {
83f64091
FB
140 /* add synchronous IO emulation layer */
141 bdrv->bdrv_read = bdrv_read_em;
142 bdrv->bdrv_write = bdrv_write_em;
143 }
b2e12bc6
CH
144
145 if (!bdrv->bdrv_aio_flush)
146 bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
147
8a22f02a 148 QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
ea2384d3 149}
b338082b
FB
150
151/* create a new block device (by default it is empty) */
152BlockDriverState *bdrv_new(const char *device_name)
153{
1b7bdbc1 154 BlockDriverState *bs;
b338082b
FB
155
156 bs = qemu_mallocz(sizeof(BlockDriverState));
b338082b 157 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3 158 if (device_name[0] != '\0') {
1b7bdbc1 159 QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
ea2384d3 160 }
b338082b
FB
161 return bs;
162}
163
ea2384d3
FB
164BlockDriver *bdrv_find_format(const char *format_name)
165{
166 BlockDriver *drv1;
8a22f02a
SH
167 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
168 if (!strcmp(drv1->format_name, format_name)) {
ea2384d3 169 return drv1;
8a22f02a 170 }
ea2384d3
FB
171 }
172 return NULL;
173}
174
eb852011
MA
175static int bdrv_is_whitelisted(BlockDriver *drv)
176{
177 static const char *whitelist[] = {
178 CONFIG_BDRV_WHITELIST
179 };
180 const char **p;
181
182 if (!whitelist[0])
183 return 1; /* no whitelist, anything goes */
184
185 for (p = whitelist; *p; p++) {
186 if (!strcmp(drv->format_name, *p)) {
187 return 1;
188 }
189 }
190 return 0;
191}
192
193BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
194{
195 BlockDriver *drv = bdrv_find_format(format_name);
196 return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
197}
198
0e7e1989
KW
199int bdrv_create(BlockDriver *drv, const char* filename,
200 QEMUOptionParameter *options)
ea2384d3
FB
201{
202 if (!drv->bdrv_create)
203 return -ENOTSUP;
0e7e1989
KW
204
205 return drv->bdrv_create(filename, options);
ea2384d3
FB
206}
207
84a12e66
CH
208int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
209{
210 BlockDriver *drv;
211
b50cbabc 212 drv = bdrv_find_protocol(filename);
84a12e66
CH
213 if (drv == NULL) {
214 drv = bdrv_find_format("file");
215 }
216
217 return bdrv_create(drv, filename, options);
218}
219
d5249393 220#ifdef _WIN32
95389c86 221void get_tmp_filename(char *filename, int size)
d5249393 222{
3b9f94e1 223 char temp_dir[MAX_PATH];
3b46e624 224
3b9f94e1
FB
225 GetTempPath(MAX_PATH, temp_dir);
226 GetTempFileName(temp_dir, "qem", 0, filename);
d5249393
FB
227}
228#else
95389c86 229void get_tmp_filename(char *filename, int size)
fc01f7e7 230{
67b915a5 231 int fd;
7ccfb2eb 232 const char *tmpdir;
d5249393 233 /* XXX: race condition possible */
0badc1ee
AJ
234 tmpdir = getenv("TMPDIR");
235 if (!tmpdir)
236 tmpdir = "/tmp";
237 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
ea2384d3
FB
238 fd = mkstemp(filename);
239 close(fd);
240}
d5249393 241#endif
fc01f7e7 242
19cb3738 243#ifdef _WIN32
f45512fe
FB
244static int is_windows_drive_prefix(const char *filename)
245{
246 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
247 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
248 filename[1] == ':');
249}
3b46e624 250
508c7cb3 251int is_windows_drive(const char *filename)
19cb3738 252{
5fafdf24 253 if (is_windows_drive_prefix(filename) &&
f45512fe 254 filename[2] == '\0')
19cb3738
FB
255 return 1;
256 if (strstart(filename, "\\\\.\\", NULL) ||
257 strstart(filename, "//./", NULL))
258 return 1;
259 return 0;
260}
261#endif
262
84a12e66
CH
263/*
264 * Detect host devices. By convention, /dev/cdrom[N] is always
265 * recognized as a host CDROM.
266 */
267static BlockDriver *find_hdev_driver(const char *filename)
268{
269 int score_max = 0, score;
270 BlockDriver *drv = NULL, *d;
271
272 QLIST_FOREACH(d, &bdrv_drivers, list) {
273 if (d->bdrv_probe_device) {
274 score = d->bdrv_probe_device(filename);
275 if (score > score_max) {
276 score_max = score;
277 drv = d;
278 }
279 }
280 }
281
282 return drv;
283}
284
b50cbabc 285BlockDriver *bdrv_find_protocol(const char *filename)
83f64091
FB
286{
287 BlockDriver *drv1;
288 char protocol[128];
1cec71e3 289 int len;
83f64091 290 const char *p;
20993081 291 int is_drive;
19cb3738 292
66f82cee
KW
293 /* TODO Drivers without bdrv_file_open must be specified explicitly */
294
19cb3738 295#ifdef _WIN32
20993081
KW
296 is_drive = is_windows_drive(filename) ||
297 is_windows_drive_prefix(filename);
298#else
299 is_drive = 0;
19cb3738 300#endif
1cec71e3 301 p = strchr(filename, ':');
20993081 302 if (!p || is_drive) {
84a12e66
CH
303 drv1 = find_hdev_driver(filename);
304 if (!drv1) {
305 drv1 = bdrv_find_format("file");
306 }
307 return drv1;
308 }
1cec71e3
AL
309 len = p - filename;
310 if (len > sizeof(protocol) - 1)
311 len = sizeof(protocol) - 1;
312 memcpy(protocol, filename, len);
313 protocol[len] = '\0';
8a22f02a 314 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
5fafdf24 315 if (drv1->protocol_name &&
8a22f02a 316 !strcmp(drv1->protocol_name, protocol)) {
83f64091 317 return drv1;
8a22f02a 318 }
83f64091
FB
319 }
320 return NULL;
321}
322
f3a5d3f8
CH
323static BlockDriver *find_image_format(const char *filename)
324{
325 int ret, score, score_max;
326 BlockDriver *drv1, *drv;
327 uint8_t buf[2048];
328 BlockDriverState *bs;
329
f5edb014 330 ret = bdrv_file_open(&bs, filename, 0);
83f64091
FB
331 if (ret < 0)
332 return NULL;
f8ea0b00
NB
333
334 /* Return the raw BlockDriver * to scsi-generic devices */
335 if (bs->sg)
336 return bdrv_find_format("raw");
337
83f64091
FB
338 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
339 bdrv_delete(bs);
340 if (ret < 0) {
341 return NULL;
342 }
343
ea2384d3 344 score_max = 0;
84a12e66 345 drv = NULL;
8a22f02a 346 QLIST_FOREACH(drv1, &bdrv_drivers, list) {
83f64091
FB
347 if (drv1->bdrv_probe) {
348 score = drv1->bdrv_probe(buf, ret, filename);
349 if (score > score_max) {
350 score_max = score;
351 drv = drv1;
352 }
0849bf08 353 }
fc01f7e7 354 }
ea2384d3
FB
355 return drv;
356}
357
51762288
SH
358/**
359 * Set the current 'total_sectors' value
360 */
361static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
362{
363 BlockDriver *drv = bs->drv;
364
396759ad
NB
365 /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
366 if (bs->sg)
367 return 0;
368
51762288
SH
369 /* query actual device if possible, otherwise just trust the hint */
370 if (drv->bdrv_getlength) {
371 int64_t length = drv->bdrv_getlength(bs);
372 if (length < 0) {
373 return length;
374 }
375 hint = length >> BDRV_SECTOR_BITS;
376 }
377
378 bs->total_sectors = hint;
379 return 0;
380}
381
57915332
KW
382/*
383 * Common part for opening disk images and files
384 */
385static int bdrv_open_common(BlockDriverState *bs, const char *filename,
386 int flags, BlockDriver *drv)
387{
388 int ret, open_flags;
389
390 assert(drv != NULL);
391
66f82cee 392 bs->file = NULL;
51762288 393 bs->total_sectors = 0;
57915332
KW
394 bs->is_temporary = 0;
395 bs->encrypted = 0;
396 bs->valid_key = 0;
397 bs->open_flags = flags;
398 /* buffer_alignment defaulted to 512, drivers can change this value */
399 bs->buffer_alignment = 512;
400
401 pstrcpy(bs->filename, sizeof(bs->filename), filename);
402
403 if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
404 return -ENOTSUP;
405 }
406
407 bs->drv = drv;
408 bs->opaque = qemu_mallocz(drv->instance_size);
409
410 /*
411 * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
412 * write cache to the guest. We do need the fdatasync to flush
413 * out transactions for block allocations, and we maybe have a
414 * volatile write cache in our backing device to deal with.
415 */
416 if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
417 bs->enable_write_cache = 1;
418
419 /*
420 * Clear flags that are internal to the block layer before opening the
421 * image.
422 */
423 open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
424
425 /*
426 * Snapshots should be writeable.
427 */
428 if (bs->is_temporary) {
429 open_flags |= BDRV_O_RDWR;
430 }
431
66f82cee
KW
432 /* Open the image, either directly or using a protocol */
433 if (drv->bdrv_file_open) {
434 ret = drv->bdrv_file_open(bs, filename, open_flags);
435 } else {
436 ret = bdrv_file_open(&bs->file, filename, open_flags);
437 if (ret >= 0) {
438 ret = drv->bdrv_open(bs, open_flags);
439 }
440 }
441
57915332
KW
442 if (ret < 0) {
443 goto free_and_fail;
444 }
445
446 bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
51762288
SH
447
448 ret = refresh_total_sectors(bs, bs->total_sectors);
449 if (ret < 0) {
450 goto free_and_fail;
57915332 451 }
51762288 452
57915332
KW
453#ifndef _WIN32
454 if (bs->is_temporary) {
455 unlink(filename);
456 }
457#endif
458 return 0;
459
460free_and_fail:
66f82cee
KW
461 if (bs->file) {
462 bdrv_delete(bs->file);
463 bs->file = NULL;
464 }
57915332
KW
465 qemu_free(bs->opaque);
466 bs->opaque = NULL;
467 bs->drv = NULL;
468 return ret;
469}
470
b6ce07aa
KW
471/*
472 * Opens a file using a protocol (file, host_device, nbd, ...)
473 */
83f64091 474int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
ea2384d3 475{
83f64091 476 BlockDriverState *bs;
6db95603 477 BlockDriver *drv;
83f64091
FB
478 int ret;
479
b50cbabc 480 drv = bdrv_find_protocol(filename);
6db95603
CH
481 if (!drv) {
482 return -ENOENT;
483 }
484
83f64091 485 bs = bdrv_new("");
b6ce07aa 486 ret = bdrv_open_common(bs, filename, flags, drv);
83f64091
FB
487 if (ret < 0) {
488 bdrv_delete(bs);
489 return ret;
3b0d4f61 490 }
71d0770c 491 bs->growable = 1;
83f64091
FB
492 *pbs = bs;
493 return 0;
494}
495
b6ce07aa
KW
496/*
497 * Opens a disk image (raw, qcow2, vmdk, ...)
498 */
d6e9098e
KW
499int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
500 BlockDriver *drv)
ea2384d3 501{
b6ce07aa 502 int ret;
712e7874 503
83f64091 504 if (flags & BDRV_O_SNAPSHOT) {
ea2384d3
FB
505 BlockDriverState *bs1;
506 int64_t total_size;
7c96d46e 507 int is_protocol = 0;
91a073a9
KW
508 BlockDriver *bdrv_qcow2;
509 QEMUOptionParameter *options;
b6ce07aa
KW
510 char tmp_filename[PATH_MAX];
511 char backing_filename[PATH_MAX];
3b46e624 512
ea2384d3
FB
513 /* if snapshot, we create a temporary backing file and open it
514 instead of opening 'filename' directly */
33e3963e 515
ea2384d3
FB
516 /* if there is a backing file, use it */
517 bs1 = bdrv_new("");
d6e9098e 518 ret = bdrv_open(bs1, filename, 0, drv);
51d7c00c 519 if (ret < 0) {
ea2384d3 520 bdrv_delete(bs1);
51d7c00c 521 return ret;
ea2384d3 522 }
6ea44308 523 total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS;
7c96d46e
AL
524
525 if (bs1->drv && bs1->drv->protocol_name)
526 is_protocol = 1;
527
ea2384d3 528 bdrv_delete(bs1);
3b46e624 529
ea2384d3 530 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
7c96d46e
AL
531
532 /* Real path is meaningless for protocols */
533 if (is_protocol)
534 snprintf(backing_filename, sizeof(backing_filename),
535 "%s", filename);
114cdfa9
KS
536 else if (!realpath(filename, backing_filename))
537 return -errno;
7c96d46e 538
91a073a9
KW
539 bdrv_qcow2 = bdrv_find_format("qcow2");
540 options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
541
542 set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
543 set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
544 if (drv) {
545 set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
546 drv->format_name);
547 }
548
549 ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
d748768c 550 free_option_parameters(options);
51d7c00c
AL
551 if (ret < 0) {
552 return ret;
ea2384d3 553 }
91a073a9 554
ea2384d3 555 filename = tmp_filename;
91a073a9 556 drv = bdrv_qcow2;
ea2384d3
FB
557 bs->is_temporary = 1;
558 }
712e7874 559
b6ce07aa 560 /* Find the right image format driver */
6db95603 561 if (!drv) {
84a12e66 562 drv = find_image_format(filename);
51d7c00c 563 }
6987307c 564
51d7c00c
AL
565 if (!drv) {
566 ret = -ENOENT;
567 goto unlink_and_fail;
ea2384d3 568 }
b6ce07aa
KW
569
570 /* Open the image */
571 ret = bdrv_open_common(bs, filename, flags, drv);
572 if (ret < 0) {
6987307c
CH
573 goto unlink_and_fail;
574 }
575
b6ce07aa
KW
576 /* If there is a backing file, use it */
577 if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
578 char backing_filename[PATH_MAX];
579 int back_flags;
580 BlockDriver *back_drv = NULL;
581
582 bs->backing_hd = bdrv_new("");
583 path_combine(backing_filename, sizeof(backing_filename),
584 filename, bs->backing_file);
585 if (bs->backing_format[0] != '\0')
586 back_drv = bdrv_find_format(bs->backing_format);
587
588 /* backing files always opened read-only */
589 back_flags =
590 flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
591
592 ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
593 if (ret < 0) {
594 bdrv_close(bs);
595 return ret;
596 }
597 if (bs->is_temporary) {
598 bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
599 } else {
600 /* base image inherits from "parent" */
601 bs->backing_hd->keep_read_only = bs->keep_read_only;
602 }
603 }
604
605 if (!bdrv_key_required(bs)) {
606 /* call the change callback */
607 bs->media_changed = 1;
608 if (bs->change_cb)
609 bs->change_cb(bs->change_opaque);
610 }
611
612 return 0;
613
614unlink_and_fail:
615 if (bs->is_temporary) {
616 unlink(filename);
617 }
618 return ret;
619}
620
fc01f7e7
FB
621void bdrv_close(BlockDriverState *bs)
622{
19cb3738 623 if (bs->drv) {
557df6ac 624 if (bs->backing_hd) {
ea2384d3 625 bdrv_delete(bs->backing_hd);
557df6ac
SH
626 bs->backing_hd = NULL;
627 }
ea2384d3
FB
628 bs->drv->bdrv_close(bs);
629 qemu_free(bs->opaque);
630#ifdef _WIN32
631 if (bs->is_temporary) {
632 unlink(bs->filename);
633 }
67b915a5 634#endif
ea2384d3
FB
635 bs->opaque = NULL;
636 bs->drv = NULL;
b338082b 637
66f82cee
KW
638 if (bs->file != NULL) {
639 bdrv_close(bs->file);
640 }
641
b338082b 642 /* call the change callback */
19cb3738 643 bs->media_changed = 1;
b338082b
FB
644 if (bs->change_cb)
645 bs->change_cb(bs->change_opaque);
646 }
647}
648
649void bdrv_delete(BlockDriverState *bs)
650{
1b7bdbc1
SH
651 /* remove from list, if necessary */
652 if (bs->device_name[0] != '\0') {
653 QTAILQ_REMOVE(&bdrv_states, bs, list);
654 }
34c6f050 655
b338082b 656 bdrv_close(bs);
66f82cee
KW
657 if (bs->file != NULL) {
658 bdrv_delete(bs->file);
659 }
660
b338082b 661 qemu_free(bs);
fc01f7e7
FB
662}
663
e97fc193
AL
664/*
665 * Run consistency checks on an image
666 *
667 * Returns the number of errors or -errno when an internal error occurs
668 */
669int bdrv_check(BlockDriverState *bs)
670{
671 if (bs->drv->bdrv_check == NULL) {
672 return -ENOTSUP;
673 }
674
675 return bs->drv->bdrv_check(bs);
676}
677
33e3963e
FB
678/* commit COW file into the raw image */
679int bdrv_commit(BlockDriverState *bs)
680{
19cb3738 681 BlockDriver *drv = bs->drv;
83f64091 682 int64_t i, total_sectors;
4dca4b63
NS
683 int n, j, ro, open_flags;
684 int ret = 0, rw_ret = 0;
ea2384d3 685 unsigned char sector[512];
4dca4b63
NS
686 char filename[1024];
687 BlockDriverState *bs_rw, *bs_ro;
33e3963e 688
19cb3738
FB
689 if (!drv)
690 return -ENOMEDIUM;
4dca4b63
NS
691
692 if (!bs->backing_hd) {
693 return -ENOTSUP;
33e3963e
FB
694 }
695
4dca4b63
NS
696 if (bs->backing_hd->keep_read_only) {
697 return -EACCES;
698 }
699
700 ro = bs->backing_hd->read_only;
701 strncpy(filename, bs->backing_hd->filename, sizeof(filename));
702 open_flags = bs->backing_hd->open_flags;
703
704 if (ro) {
705 /* re-open as RW */
706 bdrv_delete(bs->backing_hd);
707 bs->backing_hd = NULL;
708 bs_rw = bdrv_new("");
c3349197 709 rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
4dca4b63
NS
710 if (rw_ret < 0) {
711 bdrv_delete(bs_rw);
712 /* try to re-open read-only */
713 bs_ro = bdrv_new("");
c3349197 714 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
4dca4b63
NS
715 if (ret < 0) {
716 bdrv_delete(bs_ro);
717 /* drive not functional anymore */
718 bs->drv = NULL;
719 return ret;
720 }
721 bs->backing_hd = bs_ro;
722 return rw_ret;
723 }
724 bs->backing_hd = bs_rw;
ea2384d3 725 }
33e3963e 726
6ea44308 727 total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
83f64091 728 for (i = 0; i < total_sectors;) {
19cb3738 729 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
ea2384d3
FB
730 for(j = 0; j < n; j++) {
731 if (bdrv_read(bs, i, sector, 1) != 0) {
4dca4b63
NS
732 ret = -EIO;
733 goto ro_cleanup;
ea2384d3
FB
734 }
735
736 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
4dca4b63
NS
737 ret = -EIO;
738 goto ro_cleanup;
ea2384d3
FB
739 }
740 i++;
33e3963e 741 }
ea2384d3
FB
742 } else {
743 i += n;
744 }
33e3963e 745 }
95389c86 746
1d44952f
CH
747 if (drv->bdrv_make_empty) {
748 ret = drv->bdrv_make_empty(bs);
749 bdrv_flush(bs);
750 }
95389c86 751
3f5075ae
CH
752 /*
753 * Make sure all data we wrote to the backing device is actually
754 * stable on disk.
755 */
756 if (bs->backing_hd)
757 bdrv_flush(bs->backing_hd);
4dca4b63
NS
758
759ro_cleanup:
760
761 if (ro) {
762 /* re-open as RO */
763 bdrv_delete(bs->backing_hd);
764 bs->backing_hd = NULL;
765 bs_ro = bdrv_new("");
c3349197 766 ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
4dca4b63
NS
767 if (ret < 0) {
768 bdrv_delete(bs_ro);
769 /* drive not functional anymore */
770 bs->drv = NULL;
771 return ret;
772 }
773 bs->backing_hd = bs_ro;
774 bs->backing_hd->keep_read_only = 0;
775 }
776
1d44952f 777 return ret;
33e3963e
FB
778}
779
756e6736
KW
780/*
781 * Return values:
782 * 0 - success
783 * -EINVAL - backing format specified, but no file
784 * -ENOSPC - can't update the backing file because no space is left in the
785 * image file header
786 * -ENOTSUP - format driver doesn't support changing the backing file
787 */
788int bdrv_change_backing_file(BlockDriverState *bs,
789 const char *backing_file, const char *backing_fmt)
790{
791 BlockDriver *drv = bs->drv;
792
793 if (drv->bdrv_change_backing_file != NULL) {
794 return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
795 } else {
796 return -ENOTSUP;
797 }
798}
799
71d0770c
AL
800static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
801 size_t size)
802{
803 int64_t len;
804
805 if (!bdrv_is_inserted(bs))
806 return -ENOMEDIUM;
807
808 if (bs->growable)
809 return 0;
810
811 len = bdrv_getlength(bs);
812
fbb7b4e0
KW
813 if (offset < 0)
814 return -EIO;
815
816 if ((offset > len) || (len - offset < size))
71d0770c
AL
817 return -EIO;
818
819 return 0;
820}
821
822static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
823 int nb_sectors)
824{
999dec57 825 return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
71d0770c
AL
826}
827
19cb3738 828/* return < 0 if error. See bdrv_write() for the return codes */
5fafdf24 829int bdrv_read(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
830 uint8_t *buf, int nb_sectors)
831{
ea2384d3
FB
832 BlockDriver *drv = bs->drv;
833
19cb3738
FB
834 if (!drv)
835 return -ENOMEDIUM;
71d0770c
AL
836 if (bdrv_check_request(bs, sector_num, nb_sectors))
837 return -EIO;
b338082b 838
eda578e5 839 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
fc01f7e7
FB
840}
841
7cd1e32a 842static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
a55eb92c 843 int nb_sectors, int dirty)
7cd1e32a 844{
845 int64_t start, end;
c6d22830 846 unsigned long val, idx, bit;
a55eb92c 847
6ea44308 848 start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
c6d22830 849 end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
a55eb92c
JK
850
851 for (; start <= end; start++) {
c6d22830
JK
852 idx = start / (sizeof(unsigned long) * 8);
853 bit = start % (sizeof(unsigned long) * 8);
854 val = bs->dirty_bitmap[idx];
855 if (dirty) {
aaa0eb75
LS
856 if (!(val & (1 << bit))) {
857 bs->dirty_count++;
858 val |= 1 << bit;
859 }
c6d22830 860 } else {
aaa0eb75
LS
861 if (val & (1 << bit)) {
862 bs->dirty_count--;
863 val &= ~(1 << bit);
864 }
c6d22830
JK
865 }
866 bs->dirty_bitmap[idx] = val;
7cd1e32a 867 }
868}
869
5fafdf24 870/* Return < 0 if error. Important errors are:
19cb3738
FB
871 -EIO generic I/O error (may happen for all errors)
872 -ENOMEDIUM No media inserted.
873 -EINVAL Invalid sector number or nb_sectors
874 -EACCES Trying to write a read-only device
875*/
5fafdf24 876int bdrv_write(BlockDriverState *bs, int64_t sector_num,
fc01f7e7
FB
877 const uint8_t *buf, int nb_sectors)
878{
83f64091 879 BlockDriver *drv = bs->drv;
19cb3738
FB
880 if (!bs->drv)
881 return -ENOMEDIUM;
0849bf08 882 if (bs->read_only)
19cb3738 883 return -EACCES;
71d0770c
AL
884 if (bdrv_check_request(bs, sector_num, nb_sectors))
885 return -EIO;
a55eb92c 886
c6d22830 887 if (bs->dirty_bitmap) {
7cd1e32a 888 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
889 }
a55eb92c 890
294cc35f
KW
891 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
892 bs->wr_highest_sector = sector_num + nb_sectors - 1;
893 }
894
42fb2807 895 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
83f64091
FB
896}
897
eda578e5
AL
898int bdrv_pread(BlockDriverState *bs, int64_t offset,
899 void *buf, int count1)
83f64091 900{
6ea44308 901 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
83f64091
FB
902 int len, nb_sectors, count;
903 int64_t sector_num;
9a8c4cce 904 int ret;
83f64091
FB
905
906 count = count1;
907 /* first read to align to sector start */
6ea44308 908 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
83f64091
FB
909 if (len > count)
910 len = count;
6ea44308 911 sector_num = offset >> BDRV_SECTOR_BITS;
83f64091 912 if (len > 0) {
9a8c4cce
KW
913 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
914 return ret;
6ea44308 915 memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
83f64091
FB
916 count -= len;
917 if (count == 0)
918 return count1;
919 sector_num++;
920 buf += len;
921 }
922
923 /* read the sectors "in place" */
6ea44308 924 nb_sectors = count >> BDRV_SECTOR_BITS;
83f64091 925 if (nb_sectors > 0) {
9a8c4cce
KW
926 if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
927 return ret;
83f64091 928 sector_num += nb_sectors;
6ea44308 929 len = nb_sectors << BDRV_SECTOR_BITS;
83f64091
FB
930 buf += len;
931 count -= len;
932 }
933
934 /* add data from the last sector */
935 if (count > 0) {
9a8c4cce
KW
936 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
937 return ret;
83f64091
FB
938 memcpy(buf, tmp_buf, count);
939 }
940 return count1;
941}
942
eda578e5
AL
943int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
944 const void *buf, int count1)
83f64091 945{
6ea44308 946 uint8_t tmp_buf[BDRV_SECTOR_SIZE];
83f64091
FB
947 int len, nb_sectors, count;
948 int64_t sector_num;
9a8c4cce 949 int ret;
83f64091
FB
950
951 count = count1;
952 /* first write to align to sector start */
6ea44308 953 len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
83f64091
FB
954 if (len > count)
955 len = count;
6ea44308 956 sector_num = offset >> BDRV_SECTOR_BITS;
83f64091 957 if (len > 0) {
9a8c4cce
KW
958 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
959 return ret;
6ea44308 960 memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
9a8c4cce
KW
961 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
962 return ret;
83f64091
FB
963 count -= len;
964 if (count == 0)
965 return count1;
966 sector_num++;
967 buf += len;
968 }
969
970 /* write the sectors "in place" */
6ea44308 971 nb_sectors = count >> BDRV_SECTOR_BITS;
83f64091 972 if (nb_sectors > 0) {
9a8c4cce
KW
973 if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
974 return ret;
83f64091 975 sector_num += nb_sectors;
6ea44308 976 len = nb_sectors << BDRV_SECTOR_BITS;
83f64091
FB
977 buf += len;
978 count -= len;
979 }
980
981 /* add data from the last sector */
982 if (count > 0) {
9a8c4cce
KW
983 if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
984 return ret;
83f64091 985 memcpy(tmp_buf, buf, count);
9a8c4cce
KW
986 if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
987 return ret;
83f64091
FB
988 }
989 return count1;
990}
83f64091 991
83f64091
FB
992/**
993 * Truncate file to 'offset' bytes (needed only for file protocols)
994 */
995int bdrv_truncate(BlockDriverState *bs, int64_t offset)
996{
997 BlockDriver *drv = bs->drv;
51762288 998 int ret;
83f64091 999 if (!drv)
19cb3738 1000 return -ENOMEDIUM;
83f64091
FB
1001 if (!drv->bdrv_truncate)
1002 return -ENOTSUP;
59f2689d
NS
1003 if (bs->read_only)
1004 return -EACCES;
51762288
SH
1005 ret = drv->bdrv_truncate(bs, offset);
1006 if (ret == 0) {
1007 ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1008 }
1009 return ret;
83f64091
FB
1010}
1011
1012/**
1013 * Length of a file in bytes. Return < 0 if error or unknown.
1014 */
1015int64_t bdrv_getlength(BlockDriverState *bs)
1016{
1017 BlockDriver *drv = bs->drv;
1018 if (!drv)
19cb3738 1019 return -ENOMEDIUM;
51762288
SH
1020
1021 /* Fixed size devices use the total_sectors value for speed instead of
1022 issuing a length query (like lseek) on each call. Also, legacy block
1023 drivers don't provide a bdrv_getlength function and must use
1024 total_sectors. */
1025 if (!bs->growable || !drv->bdrv_getlength) {
6ea44308 1026 return bs->total_sectors * BDRV_SECTOR_SIZE;
83f64091
FB
1027 }
1028 return drv->bdrv_getlength(bs);
fc01f7e7
FB
1029}
1030
19cb3738 1031/* return 0 as number of sectors if no device present or error */
96b8f136 1032void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
fc01f7e7 1033{
19cb3738
FB
1034 int64_t length;
1035 length = bdrv_getlength(bs);
1036 if (length < 0)
1037 length = 0;
1038 else
6ea44308 1039 length = length >> BDRV_SECTOR_BITS;
19cb3738 1040 *nb_sectors_ptr = length;
fc01f7e7 1041}
cf98951b 1042
f3d54fc4
AL
1043struct partition {
1044 uint8_t boot_ind; /* 0x80 - active */
1045 uint8_t head; /* starting head */
1046 uint8_t sector; /* starting sector */
1047 uint8_t cyl; /* starting cylinder */
1048 uint8_t sys_ind; /* What partition type */
1049 uint8_t end_head; /* end head */
1050 uint8_t end_sector; /* end sector */
1051 uint8_t end_cyl; /* end cylinder */
1052 uint32_t start_sect; /* starting sector counting from 0 */
1053 uint32_t nr_sects; /* nr of sectors in partition */
1054} __attribute__((packed));
1055
1056/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1057static int guess_disk_lchs(BlockDriverState *bs,
1058 int *pcylinders, int *pheads, int *psectors)
1059{
1060 uint8_t buf[512];
1061 int ret, i, heads, sectors, cylinders;
1062 struct partition *p;
1063 uint32_t nr_sects;
a38131b6 1064 uint64_t nb_sectors;
f3d54fc4
AL
1065
1066 bdrv_get_geometry(bs, &nb_sectors);
1067
1068 ret = bdrv_read(bs, 0, buf, 1);
1069 if (ret < 0)
1070 return -1;
1071 /* test msdos magic */
1072 if (buf[510] != 0x55 || buf[511] != 0xaa)
1073 return -1;
1074 for(i = 0; i < 4; i++) {
1075 p = ((struct partition *)(buf + 0x1be)) + i;
1076 nr_sects = le32_to_cpu(p->nr_sects);
1077 if (nr_sects && p->end_head) {
1078 /* We make the assumption that the partition terminates on
1079 a cylinder boundary */
1080 heads = p->end_head + 1;
1081 sectors = p->end_sector & 63;
1082 if (sectors == 0)
1083 continue;
1084 cylinders = nb_sectors / (heads * sectors);
1085 if (cylinders < 1 || cylinders > 16383)
1086 continue;
1087 *pheads = heads;
1088 *psectors = sectors;
1089 *pcylinders = cylinders;
1090#if 0
1091 printf("guessed geometry: LCHS=%d %d %d\n",
1092 cylinders, heads, sectors);
1093#endif
1094 return 0;
1095 }
1096 }
1097 return -1;
1098}
1099
1100void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1101{
1102 int translation, lba_detected = 0;
1103 int cylinders, heads, secs;
a38131b6 1104 uint64_t nb_sectors;
f3d54fc4
AL
1105
1106 /* if a geometry hint is available, use it */
1107 bdrv_get_geometry(bs, &nb_sectors);
1108 bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1109 translation = bdrv_get_translation_hint(bs);
1110 if (cylinders != 0) {
1111 *pcyls = cylinders;
1112 *pheads = heads;
1113 *psecs = secs;
1114 } else {
1115 if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1116 if (heads > 16) {
1117 /* if heads > 16, it means that a BIOS LBA
1118 translation was active, so the default
1119 hardware geometry is OK */
1120 lba_detected = 1;
1121 goto default_geometry;
1122 } else {
1123 *pcyls = cylinders;
1124 *pheads = heads;
1125 *psecs = secs;
1126 /* disable any translation to be in sync with
1127 the logical geometry */
1128 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1129 bdrv_set_translation_hint(bs,
1130 BIOS_ATA_TRANSLATION_NONE);
1131 }
1132 }
1133 } else {
1134 default_geometry:
1135 /* if no geometry, use a standard physical disk geometry */
1136 cylinders = nb_sectors / (16 * 63);
1137
1138 if (cylinders > 16383)
1139 cylinders = 16383;
1140 else if (cylinders < 2)
1141 cylinders = 2;
1142 *pcyls = cylinders;
1143 *pheads = 16;
1144 *psecs = 63;
1145 if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1146 if ((*pcyls * *pheads) <= 131072) {
1147 bdrv_set_translation_hint(bs,
1148 BIOS_ATA_TRANSLATION_LARGE);
1149 } else {
1150 bdrv_set_translation_hint(bs,
1151 BIOS_ATA_TRANSLATION_LBA);
1152 }
1153 }
1154 }
1155 bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1156 }
1157}
1158
5fafdf24 1159void bdrv_set_geometry_hint(BlockDriverState *bs,
b338082b
FB
1160 int cyls, int heads, int secs)
1161{
1162 bs->cyls = cyls;
1163 bs->heads = heads;
1164 bs->secs = secs;
1165}
1166
1167void bdrv_set_type_hint(BlockDriverState *bs, int type)
1168{
1169 bs->type = type;
1170 bs->removable = ((type == BDRV_TYPE_CDROM ||
1171 type == BDRV_TYPE_FLOPPY));
1172}
1173
46d4767d
FB
1174void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1175{
1176 bs->translation = translation;
1177}
1178
5fafdf24 1179void bdrv_get_geometry_hint(BlockDriverState *bs,
b338082b
FB
1180 int *pcyls, int *pheads, int *psecs)
1181{
1182 *pcyls = bs->cyls;
1183 *pheads = bs->heads;
1184 *psecs = bs->secs;
1185}
1186
1187int bdrv_get_type_hint(BlockDriverState *bs)
1188{
1189 return bs->type;
1190}
1191
46d4767d
FB
1192int bdrv_get_translation_hint(BlockDriverState *bs)
1193{
1194 return bs->translation;
1195}
1196
b338082b
FB
1197int bdrv_is_removable(BlockDriverState *bs)
1198{
1199 return bs->removable;
1200}
1201
1202int bdrv_is_read_only(BlockDriverState *bs)
1203{
1204 return bs->read_only;
1205}
1206
985a03b0
TS
1207int bdrv_is_sg(BlockDriverState *bs)
1208{
1209 return bs->sg;
1210}
1211
e900a7b7
CH
1212int bdrv_enable_write_cache(BlockDriverState *bs)
1213{
1214 return bs->enable_write_cache;
1215}
1216
19cb3738 1217/* XXX: no longer used */
5fafdf24 1218void bdrv_set_change_cb(BlockDriverState *bs,
b338082b
FB
1219 void (*change_cb)(void *opaque), void *opaque)
1220{
1221 bs->change_cb = change_cb;
1222 bs->change_opaque = opaque;
1223}
1224
ea2384d3
FB
1225int bdrv_is_encrypted(BlockDriverState *bs)
1226{
1227 if (bs->backing_hd && bs->backing_hd->encrypted)
1228 return 1;
1229 return bs->encrypted;
1230}
1231
c0f4ce77
AL
1232int bdrv_key_required(BlockDriverState *bs)
1233{
1234 BlockDriverState *backing_hd = bs->backing_hd;
1235
1236 if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1237 return 1;
1238 return (bs->encrypted && !bs->valid_key);
1239}
1240
ea2384d3
FB
1241int bdrv_set_key(BlockDriverState *bs, const char *key)
1242{
1243 int ret;
1244 if (bs->backing_hd && bs->backing_hd->encrypted) {
1245 ret = bdrv_set_key(bs->backing_hd, key);
1246 if (ret < 0)
1247 return ret;
1248 if (!bs->encrypted)
1249 return 0;
1250 }
fd04a2ae
SH
1251 if (!bs->encrypted) {
1252 return -EINVAL;
1253 } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1254 return -ENOMEDIUM;
1255 }
c0f4ce77 1256 ret = bs->drv->bdrv_set_key(bs, key);
bb5fc20f
AL
1257 if (ret < 0) {
1258 bs->valid_key = 0;
1259 } else if (!bs->valid_key) {
1260 bs->valid_key = 1;
1261 /* call the change callback now, we skipped it on open */
1262 bs->media_changed = 1;
1263 if (bs->change_cb)
1264 bs->change_cb(bs->change_opaque);
1265 }
c0f4ce77 1266 return ret;
ea2384d3
FB
1267}
1268
1269void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1270{
19cb3738 1271 if (!bs->drv) {
ea2384d3
FB
1272 buf[0] = '\0';
1273 } else {
1274 pstrcpy(buf, buf_size, bs->drv->format_name);
1275 }
1276}
1277
5fafdf24 1278void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
ea2384d3
FB
1279 void *opaque)
1280{
1281 BlockDriver *drv;
1282
8a22f02a 1283 QLIST_FOREACH(drv, &bdrv_drivers, list) {
ea2384d3
FB
1284 it(opaque, drv->format_name);
1285 }
1286}
1287
b338082b
FB
1288BlockDriverState *bdrv_find(const char *name)
1289{
1290 BlockDriverState *bs;
1291
1b7bdbc1
SH
1292 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1293 if (!strcmp(name, bs->device_name)) {
b338082b 1294 return bs;
1b7bdbc1 1295 }
b338082b
FB
1296 }
1297 return NULL;
1298}
1299
51de9760 1300void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
81d0912d
FB
1301{
1302 BlockDriverState *bs;
1303
1b7bdbc1 1304 QTAILQ_FOREACH(bs, &bdrv_states, list) {
51de9760 1305 it(opaque, bs);
81d0912d
FB
1306 }
1307}
1308
ea2384d3
FB
1309const char *bdrv_get_device_name(BlockDriverState *bs)
1310{
1311 return bs->device_name;
1312}
1313
7a6cba61
PB
1314void bdrv_flush(BlockDriverState *bs)
1315{
016f5cf6
AG
1316 if (bs->open_flags & BDRV_O_NO_FLUSH) {
1317 return;
1318 }
1319
3f5075ae 1320 if (bs->drv && bs->drv->bdrv_flush)
7a6cba61 1321 bs->drv->bdrv_flush(bs);
7a6cba61
PB
1322}
1323
c6ca28d6
AL
1324void bdrv_flush_all(void)
1325{
1326 BlockDriverState *bs;
1327
1b7bdbc1
SH
1328 QTAILQ_FOREACH(bs, &bdrv_states, list) {
1329 if (bs->drv && !bdrv_is_read_only(bs) &&
1330 (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
c6ca28d6 1331 bdrv_flush(bs);
1b7bdbc1
SH
1332 }
1333 }
c6ca28d6
AL
1334}
1335
f2feebbd
KW
1336int bdrv_has_zero_init(BlockDriverState *bs)
1337{
1338 assert(bs->drv);
1339
1340 if (bs->drv->no_zero_init) {
1341 return 0;
1342 } else if (bs->file) {
1343 return bdrv_has_zero_init(bs->file);
1344 }
1345
1346 return 1;
1347}
1348
f58c7b35
TS
1349/*
1350 * Returns true iff the specified sector is present in the disk image. Drivers
1351 * not implementing the functionality are assumed to not support backing files,
1352 * hence all their sectors are reported as allocated.
1353 *
1354 * 'pnum' is set to the number of sectors (including and immediately following
1355 * the specified sector) that are known to be in the same
1356 * allocated/unallocated state.
1357 *
1358 * 'nb_sectors' is the max value 'pnum' should be set to.
1359 */
1360int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1361 int *pnum)
1362{
1363 int64_t n;
1364 if (!bs->drv->bdrv_is_allocated) {
1365 if (sector_num >= bs->total_sectors) {
1366 *pnum = 0;
1367 return 0;
1368 }
1369 n = bs->total_sectors - sector_num;
1370 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1371 return 1;
1372 }
1373 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1374}
1375
2582bfed
LC
1376void bdrv_mon_event(const BlockDriverState *bdrv,
1377 BlockMonEventAction action, int is_read)
1378{
1379 QObject *data;
1380 const char *action_str;
1381
1382 switch (action) {
1383 case BDRV_ACTION_REPORT:
1384 action_str = "report";
1385 break;
1386 case BDRV_ACTION_IGNORE:
1387 action_str = "ignore";
1388 break;
1389 case BDRV_ACTION_STOP:
1390 action_str = "stop";
1391 break;
1392 default:
1393 abort();
1394 }
1395
1396 data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1397 bdrv->device_name,
1398 action_str,
1399 is_read ? "read" : "write");
1400 monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1401
1402 qobject_decref(data);
1403}
1404
d15e5465 1405static void bdrv_print_dict(QObject *obj, void *opaque)
b338082b 1406{
d15e5465
LC
1407 QDict *bs_dict;
1408 Monitor *mon = opaque;
1409
1410 bs_dict = qobject_to_qdict(obj);
1411
1412 monitor_printf(mon, "%s: type=%s removable=%d",
1413 qdict_get_str(bs_dict, "device"),
1414 qdict_get_str(bs_dict, "type"),
1415 qdict_get_bool(bs_dict, "removable"));
1416
1417 if (qdict_get_bool(bs_dict, "removable")) {
1418 monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1419 }
1420
1421 if (qdict_haskey(bs_dict, "inserted")) {
1422 QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1423
1424 monitor_printf(mon, " file=");
1425 monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1426 if (qdict_haskey(qdict, "backing_file")) {
1427 monitor_printf(mon, " backing_file=");
1428 monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1429 }
1430 monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1431 qdict_get_bool(qdict, "ro"),
1432 qdict_get_str(qdict, "drv"),
1433 qdict_get_bool(qdict, "encrypted"));
1434 } else {
1435 monitor_printf(mon, " [not inserted]");
1436 }
1437
1438 monitor_printf(mon, "\n");
1439}
1440
1441void bdrv_info_print(Monitor *mon, const QObject *data)
1442{
1443 qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1444}
1445
1446/**
1447 * bdrv_info(): Block devices information
1448 *
1449 * Each block device information is stored in a QDict and the
1450 * returned QObject is a QList of all devices.
1451 *
1452 * The QDict contains the following:
1453 *
1454 * - "device": device name
1455 * - "type": device type
1456 * - "removable": true if the device is removable, false otherwise
1457 * - "locked": true if the device is locked, false otherwise
1458 * - "inserted": only present if the device is inserted, it is a QDict
1459 * containing the following:
1460 * - "file": device file name
1461 * - "ro": true if read-only, false otherwise
1462 * - "drv": driver format name
1463 * - "backing_file": backing file name if one is used
1464 * - "encrypted": true if encrypted, false otherwise
1465 *
1466 * Example:
1467 *
1468 * [ { "device": "ide0-hd0", "type": "hd", "removable": false, "locked": false,
1469 * "inserted": { "file": "/tmp/foobar", "ro": false, "drv": "qcow2" } },
1470 * { "device": "floppy0", "type": "floppy", "removable": true,
1471 * "locked": false } ]
1472 */
1473void bdrv_info(Monitor *mon, QObject **ret_data)
1474{
1475 QList *bs_list;
b338082b
FB
1476 BlockDriverState *bs;
1477
d15e5465
LC
1478 bs_list = qlist_new();
1479
1b7bdbc1 1480 QTAILQ_FOREACH(bs, &bdrv_states, list) {
d15e5465
LC
1481 QObject *bs_obj;
1482 const char *type = "unknown";
1483
b338082b
FB
1484 switch(bs->type) {
1485 case BDRV_TYPE_HD:
d15e5465 1486 type = "hd";
b338082b
FB
1487 break;
1488 case BDRV_TYPE_CDROM:
d15e5465 1489 type = "cdrom";
b338082b
FB
1490 break;
1491 case BDRV_TYPE_FLOPPY:
d15e5465 1492 type = "floppy";
b338082b
FB
1493 break;
1494 }
d15e5465
LC
1495
1496 bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1497 "'removable': %i, 'locked': %i }",
1498 bs->device_name, type, bs->removable,
1499 bs->locked);
d15e5465 1500
19cb3738 1501 if (bs->drv) {
d15e5465
LC
1502 QObject *obj;
1503 QDict *bs_dict = qobject_to_qdict(bs_obj);
1504
1505 obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1506 "'encrypted': %i }",
1507 bs->filename, bs->read_only,
1508 bs->drv->format_name,
1509 bdrv_is_encrypted(bs));
fef30743 1510 if (bs->backing_file[0] != '\0') {
d15e5465
LC
1511 QDict *qdict = qobject_to_qdict(obj);
1512 qdict_put(qdict, "backing_file",
1513 qstring_from_str(bs->backing_file));
376253ec 1514 }
d15e5465
LC
1515
1516 qdict_put_obj(bs_dict, "inserted", obj);
b338082b 1517 }
d15e5465 1518 qlist_append_obj(bs_list, bs_obj);
b338082b 1519 }
d15e5465
LC
1520
1521 *ret_data = QOBJECT(bs_list);
b338082b 1522}
a36e69dd 1523
218a536a 1524static void bdrv_stats_iter(QObject *data, void *opaque)
a36e69dd 1525{
218a536a
LC
1526 QDict *qdict;
1527 Monitor *mon = opaque;
1528
1529 qdict = qobject_to_qdict(data);
1530 monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1531
1532 qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1533 monitor_printf(mon, " rd_bytes=%" PRId64
1534 " wr_bytes=%" PRId64
1535 " rd_operations=%" PRId64
1536 " wr_operations=%" PRId64
1537 "\n",
1538 qdict_get_int(qdict, "rd_bytes"),
1539 qdict_get_int(qdict, "wr_bytes"),
1540 qdict_get_int(qdict, "rd_operations"),
1541 qdict_get_int(qdict, "wr_operations"));
1542}
1543
1544void bdrv_stats_print(Monitor *mon, const QObject *data)
1545{
1546 qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1547}
1548
294cc35f
KW
1549static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1550{
1551 QObject *res;
1552 QDict *dict;
1553
1554 res = qobject_from_jsonf("{ 'stats': {"
1555 "'rd_bytes': %" PRId64 ","
1556 "'wr_bytes': %" PRId64 ","
1557 "'rd_operations': %" PRId64 ","
1558 "'wr_operations': %" PRId64 ","
1559 "'wr_highest_offset': %" PRId64
1560 "} }",
1561 bs->rd_bytes, bs->wr_bytes,
1562 bs->rd_ops, bs->wr_ops,
1563 bs->wr_highest_sector * 512);
1564 dict = qobject_to_qdict(res);
1565
1566 if (*bs->device_name) {
1567 qdict_put(dict, "device", qstring_from_str(bs->device_name));
1568 }
1569
1570 if (bs->file) {
1571 QObject *parent = bdrv_info_stats_bs(bs->file);
1572 qdict_put_obj(dict, "parent", parent);
1573 }
1574
1575 return res;
1576}
1577
218a536a
LC
1578/**
1579 * bdrv_info_stats(): show block device statistics
1580 *
1581 * Each device statistic information is stored in a QDict and
1582 * the returned QObject is a QList of all devices.
1583 *
1584 * The QDict contains the following:
1585 *
1586 * - "device": device name
1587 * - "stats": A QDict with the statistics information, it contains:
1588 * - "rd_bytes": bytes read
1589 * - "wr_bytes": bytes written
1590 * - "rd_operations": read operations
1591 * - "wr_operations": write operations
294cc35f
KW
1592 * - "wr_highest_offset": Highest offset of a sector written since the
1593 * BlockDriverState has been opened
21955137
DB
1594 * - "parent": A QDict recursively holding the statistics of the underlying
1595 * protocol (e.g. the host file for a qcow2 image). If there is no
1596 * underlying protocol, this field is omitted.
294cc35f 1597 *
218a536a
LC
1598 * Example:
1599 *
1600 * [ { "device": "ide0-hd0",
1601 * "stats": { "rd_bytes": 512,
1602 * "wr_bytes": 0,
1603 * "rd_operations": 1,
294cc35f 1604 * "wr_operations": 0,
21955137
DB
1605 * "wr_highest_offset": 0 },
1606 * "parent": {
1607 * "stats": { "rd_bytes": 1024,
1608 * "wr_bytes": 0,
1609 * "rd_operations": 2,
1610 * "wr_operations": 0,
1611 * "wr_highest_offset": 0,
1612 * } } },
218a536a
LC
1613 * { "device": "ide1-cd0",
1614 * "stats": { "rd_bytes": 0,
1615 * "wr_bytes": 0,
1616 * "rd_operations": 0,
294cc35f
KW
1617 * "wr_operations": 0,
1618 * "wr_highest_offset": 0 } },
218a536a
LC
1619 */
1620void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1621{
1622 QObject *obj;
1623 QList *devices;
a36e69dd
TS
1624 BlockDriverState *bs;
1625
218a536a
LC
1626 devices = qlist_new();
1627
1b7bdbc1 1628 QTAILQ_FOREACH(bs, &bdrv_states, list) {
294cc35f 1629 obj = bdrv_info_stats_bs(bs);
218a536a 1630 qlist_append_obj(devices, obj);
a36e69dd 1631 }
218a536a
LC
1632
1633 *ret_data = QOBJECT(devices);
a36e69dd 1634}
ea2384d3 1635
045df330
AL
1636const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1637{
1638 if (bs->backing_hd && bs->backing_hd->encrypted)
1639 return bs->backing_file;
1640 else if (bs->encrypted)
1641 return bs->filename;
1642 else
1643 return NULL;
1644}
1645
5fafdf24 1646void bdrv_get_backing_filename(BlockDriverState *bs,
83f64091
FB
1647 char *filename, int filename_size)
1648{
b783e409 1649 if (!bs->backing_file) {
83f64091
FB
1650 pstrcpy(filename, filename_size, "");
1651 } else {
1652 pstrcpy(filename, filename_size, bs->backing_file);
1653 }
1654}
1655
5fafdf24 1656int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
faea38e7
FB
1657 const uint8_t *buf, int nb_sectors)
1658{
1659 BlockDriver *drv = bs->drv;
1660 if (!drv)
19cb3738 1661 return -ENOMEDIUM;
faea38e7
FB
1662 if (!drv->bdrv_write_compressed)
1663 return -ENOTSUP;
fbb7b4e0
KW
1664 if (bdrv_check_request(bs, sector_num, nb_sectors))
1665 return -EIO;
a55eb92c 1666
c6d22830 1667 if (bs->dirty_bitmap) {
7cd1e32a 1668 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1669 }
a55eb92c 1670
faea38e7
FB
1671 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1672}
3b46e624 1673
faea38e7
FB
1674int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1675{
1676 BlockDriver *drv = bs->drv;
1677 if (!drv)
19cb3738 1678 return -ENOMEDIUM;
faea38e7
FB
1679 if (!drv->bdrv_get_info)
1680 return -ENOTSUP;
1681 memset(bdi, 0, sizeof(*bdi));
1682 return drv->bdrv_get_info(bs, bdi);
1683}
1684
45566e9c
CH
1685int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1686 int64_t pos, int size)
178e08a5
AL
1687{
1688 BlockDriver *drv = bs->drv;
1689 if (!drv)
1690 return -ENOMEDIUM;
45566e9c 1691 if (!drv->bdrv_save_vmstate)
178e08a5 1692 return -ENOTSUP;
45566e9c 1693 return drv->bdrv_save_vmstate(bs, buf, pos, size);
178e08a5
AL
1694}
1695
45566e9c
CH
1696int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1697 int64_t pos, int size)
178e08a5
AL
1698{
1699 BlockDriver *drv = bs->drv;
1700 if (!drv)
1701 return -ENOMEDIUM;
45566e9c 1702 if (!drv->bdrv_load_vmstate)
178e08a5 1703 return -ENOTSUP;
45566e9c 1704 return drv->bdrv_load_vmstate(bs, buf, pos, size);
178e08a5
AL
1705}
1706
8b9b0cc2
KW
1707void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1708{
1709 BlockDriver *drv = bs->drv;
1710
1711 if (!drv || !drv->bdrv_debug_event) {
1712 return;
1713 }
1714
1715 return drv->bdrv_debug_event(bs, event);
1716
1717}
1718
faea38e7
FB
1719/**************************************************************/
1720/* handling of snapshots */
1721
5fafdf24 1722int bdrv_snapshot_create(BlockDriverState *bs,
faea38e7
FB
1723 QEMUSnapshotInfo *sn_info)
1724{
1725 BlockDriver *drv = bs->drv;
1726 if (!drv)
19cb3738 1727 return -ENOMEDIUM;
faea38e7
FB
1728 if (!drv->bdrv_snapshot_create)
1729 return -ENOTSUP;
1730 return drv->bdrv_snapshot_create(bs, sn_info);
1731}
1732
5fafdf24 1733int bdrv_snapshot_goto(BlockDriverState *bs,
faea38e7
FB
1734 const char *snapshot_id)
1735{
1736 BlockDriver *drv = bs->drv;
1737 if (!drv)
19cb3738 1738 return -ENOMEDIUM;
faea38e7
FB
1739 if (!drv->bdrv_snapshot_goto)
1740 return -ENOTSUP;
1741 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1742}
1743
1744int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1745{
1746 BlockDriver *drv = bs->drv;
1747 if (!drv)
19cb3738 1748 return -ENOMEDIUM;
faea38e7
FB
1749 if (!drv->bdrv_snapshot_delete)
1750 return -ENOTSUP;
1751 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1752}
1753
5fafdf24 1754int bdrv_snapshot_list(BlockDriverState *bs,
faea38e7
FB
1755 QEMUSnapshotInfo **psn_info)
1756{
1757 BlockDriver *drv = bs->drv;
1758 if (!drv)
19cb3738 1759 return -ENOMEDIUM;
faea38e7
FB
1760 if (!drv->bdrv_snapshot_list)
1761 return -ENOTSUP;
1762 return drv->bdrv_snapshot_list(bs, psn_info);
1763}
1764
1765#define NB_SUFFIXES 4
1766
1767char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1768{
1769 static const char suffixes[NB_SUFFIXES] = "KMGT";
1770 int64_t base;
1771 int i;
1772
1773 if (size <= 999) {
1774 snprintf(buf, buf_size, "%" PRId64, size);
1775 } else {
1776 base = 1024;
1777 for(i = 0; i < NB_SUFFIXES; i++) {
1778 if (size < (10 * base)) {
5fafdf24 1779 snprintf(buf, buf_size, "%0.1f%c",
faea38e7
FB
1780 (double)size / base,
1781 suffixes[i]);
1782 break;
1783 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
5fafdf24 1784 snprintf(buf, buf_size, "%" PRId64 "%c",
faea38e7
FB
1785 ((size + (base >> 1)) / base),
1786 suffixes[i]);
1787 break;
1788 }
1789 base = base * 1024;
1790 }
1791 }
1792 return buf;
1793}
1794
1795char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1796{
1797 char buf1[128], date_buf[128], clock_buf[128];
3b9f94e1
FB
1798#ifdef _WIN32
1799 struct tm *ptm;
1800#else
faea38e7 1801 struct tm tm;
3b9f94e1 1802#endif
faea38e7
FB
1803 time_t ti;
1804 int64_t secs;
1805
1806 if (!sn) {
5fafdf24
TS
1807 snprintf(buf, buf_size,
1808 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1809 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1810 } else {
1811 ti = sn->date_sec;
3b9f94e1
FB
1812#ifdef _WIN32
1813 ptm = localtime(&ti);
1814 strftime(date_buf, sizeof(date_buf),
1815 "%Y-%m-%d %H:%M:%S", ptm);
1816#else
faea38e7
FB
1817 localtime_r(&ti, &tm);
1818 strftime(date_buf, sizeof(date_buf),
1819 "%Y-%m-%d %H:%M:%S", &tm);
3b9f94e1 1820#endif
faea38e7
FB
1821 secs = sn->vm_clock_nsec / 1000000000;
1822 snprintf(clock_buf, sizeof(clock_buf),
1823 "%02d:%02d:%02d.%03d",
1824 (int)(secs / 3600),
1825 (int)((secs / 60) % 60),
5fafdf24 1826 (int)(secs % 60),
faea38e7
FB
1827 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1828 snprintf(buf, buf_size,
5fafdf24 1829 "%-10s%-20s%7s%20s%15s",
faea38e7
FB
1830 sn->id_str, sn->name,
1831 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1832 date_buf,
1833 clock_buf);
1834 }
1835 return buf;
1836}
1837
83f64091 1838
ea2384d3 1839/**************************************************************/
83f64091 1840/* async I/Os */
ea2384d3 1841
3b69e4b9 1842BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
f141eafe 1843 QEMUIOVector *qiov, int nb_sectors,
3b69e4b9 1844 BlockDriverCompletionFunc *cb, void *opaque)
83f64091
FB
1845{
1846 BlockDriver *drv = bs->drv;
a36e69dd 1847 BlockDriverAIOCB *ret;
83f64091 1848
19cb3738 1849 if (!drv)
ce1a14dc 1850 return NULL;
71d0770c
AL
1851 if (bdrv_check_request(bs, sector_num, nb_sectors))
1852 return NULL;
3b46e624 1853
f141eafe
AL
1854 ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1855 cb, opaque);
a36e69dd
TS
1856
1857 if (ret) {
1858 /* Update stats even though technically transfer has not happened. */
6ea44308 1859 bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
a36e69dd
TS
1860 bs->rd_ops ++;
1861 }
1862
1863 return ret;
ea2384d3
FB
1864}
1865
f141eafe
AL
1866BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1867 QEMUIOVector *qiov, int nb_sectors,
1868 BlockDriverCompletionFunc *cb, void *opaque)
ea2384d3 1869{
83f64091 1870 BlockDriver *drv = bs->drv;
a36e69dd 1871 BlockDriverAIOCB *ret;
ea2384d3 1872
19cb3738 1873 if (!drv)
ce1a14dc 1874 return NULL;
83f64091 1875 if (bs->read_only)
ce1a14dc 1876 return NULL;
71d0770c
AL
1877 if (bdrv_check_request(bs, sector_num, nb_sectors))
1878 return NULL;
83f64091 1879
c6d22830 1880 if (bs->dirty_bitmap) {
7cd1e32a 1881 set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1882 }
a55eb92c 1883
f141eafe
AL
1884 ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1885 cb, opaque);
a36e69dd
TS
1886
1887 if (ret) {
294cc35f
KW
1888 /* Update stats even though technically transfer has not happened. */
1889 bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1890 bs->wr_ops ++;
1891 if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1892 bs->wr_highest_sector = sector_num + nb_sectors - 1;
1893 }
a36e69dd
TS
1894 }
1895
1896 return ret;
83f64091
FB
1897}
1898
40b4f539
KW
1899
1900typedef struct MultiwriteCB {
1901 int error;
1902 int num_requests;
1903 int num_callbacks;
1904 struct {
1905 BlockDriverCompletionFunc *cb;
1906 void *opaque;
1907 QEMUIOVector *free_qiov;
1908 void *free_buf;
1909 } callbacks[];
1910} MultiwriteCB;
1911
1912static void multiwrite_user_cb(MultiwriteCB *mcb)
1913{
1914 int i;
1915
1916 for (i = 0; i < mcb->num_callbacks; i++) {
1917 mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1e1ea48d
SH
1918 if (mcb->callbacks[i].free_qiov) {
1919 qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1920 }
40b4f539 1921 qemu_free(mcb->callbacks[i].free_qiov);
f8a83245 1922 qemu_vfree(mcb->callbacks[i].free_buf);
40b4f539
KW
1923 }
1924}
1925
1926static void multiwrite_cb(void *opaque, int ret)
1927{
1928 MultiwriteCB *mcb = opaque;
1929
cb6d3ca0 1930 if (ret < 0 && !mcb->error) {
40b4f539
KW
1931 mcb->error = ret;
1932 multiwrite_user_cb(mcb);
1933 }
1934
1935 mcb->num_requests--;
1936 if (mcb->num_requests == 0) {
1937 if (mcb->error == 0) {
1938 multiwrite_user_cb(mcb);
1939 }
1940 qemu_free(mcb);
1941 }
1942}
1943
1944static int multiwrite_req_compare(const void *a, const void *b)
1945{
77be4366
CH
1946 const BlockRequest *req1 = a, *req2 = b;
1947
1948 /*
1949 * Note that we can't simply subtract req2->sector from req1->sector
1950 * here as that could overflow the return value.
1951 */
1952 if (req1->sector > req2->sector) {
1953 return 1;
1954 } else if (req1->sector < req2->sector) {
1955 return -1;
1956 } else {
1957 return 0;
1958 }
40b4f539
KW
1959}
1960
1961/*
1962 * Takes a bunch of requests and tries to merge them. Returns the number of
1963 * requests that remain after merging.
1964 */
1965static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1966 int num_reqs, MultiwriteCB *mcb)
1967{
1968 int i, outidx;
1969
1970 // Sort requests by start sector
1971 qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1972
1973 // Check if adjacent requests touch the same clusters. If so, combine them,
1974 // filling up gaps with zero sectors.
1975 outidx = 0;
1976 for (i = 1; i < num_reqs; i++) {
1977 int merge = 0;
1978 int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1979
1980 // This handles the cases that are valid for all block drivers, namely
1981 // exactly sequential writes and overlapping writes.
1982 if (reqs[i].sector <= oldreq_last) {
1983 merge = 1;
1984 }
1985
1986 // The block driver may decide that it makes sense to combine requests
1987 // even if there is a gap of some sectors between them. In this case,
1988 // the gap is filled with zeros (therefore only applicable for yet
1989 // unused space in format like qcow2).
1990 if (!merge && bs->drv->bdrv_merge_requests) {
1991 merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1992 }
1993
e2a305fb
CH
1994 if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
1995 merge = 0;
1996 }
1997
40b4f539
KW
1998 if (merge) {
1999 size_t size;
2000 QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
2001 qemu_iovec_init(qiov,
2002 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2003
2004 // Add the first request to the merged one. If the requests are
2005 // overlapping, drop the last sectors of the first request.
2006 size = (reqs[i].sector - reqs[outidx].sector) << 9;
2007 qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2008
2009 // We might need to add some zeros between the two requests
2010 if (reqs[i].sector > oldreq_last) {
2011 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
2012 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
2013 memset(buf, 0, zero_bytes);
2014 qemu_iovec_add(qiov, buf, zero_bytes);
2015 mcb->callbacks[i].free_buf = buf;
2016 }
2017
2018 // Add the second request
2019 qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2020
cbf1dff2 2021 reqs[outidx].nb_sectors = qiov->size >> 9;
40b4f539
KW
2022 reqs[outidx].qiov = qiov;
2023
2024 mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2025 } else {
2026 outidx++;
2027 reqs[outidx].sector = reqs[i].sector;
2028 reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2029 reqs[outidx].qiov = reqs[i].qiov;
2030 }
2031 }
2032
2033 return outidx + 1;
2034}
2035
2036/*
2037 * Submit multiple AIO write requests at once.
2038 *
2039 * On success, the function returns 0 and all requests in the reqs array have
2040 * been submitted. In error case this function returns -1, and any of the
2041 * requests may or may not be submitted yet. In particular, this means that the
2042 * callback will be called for some of the requests, for others it won't. The
2043 * caller must check the error field of the BlockRequest to wait for the right
2044 * callbacks (if error != 0, no callback will be called).
2045 *
2046 * The implementation may modify the contents of the reqs array, e.g. to merge
2047 * requests. However, the fields opaque and error are left unmodified as they
2048 * are used to signal failure for a single request to the caller.
2049 */
2050int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2051{
2052 BlockDriverAIOCB *acb;
2053 MultiwriteCB *mcb;
2054 int i;
2055
2056 if (num_reqs == 0) {
2057 return 0;
2058 }
2059
2060 // Create MultiwriteCB structure
2061 mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2062 mcb->num_requests = 0;
2063 mcb->num_callbacks = num_reqs;
2064
2065 for (i = 0; i < num_reqs; i++) {
2066 mcb->callbacks[i].cb = reqs[i].cb;
2067 mcb->callbacks[i].opaque = reqs[i].opaque;
2068 }
2069
2070 // Check for mergable requests
2071 num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2072
2073 // Run the aio requests
2074 for (i = 0; i < num_reqs; i++) {
2075 acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2076 reqs[i].nb_sectors, multiwrite_cb, mcb);
2077
2078 if (acb == NULL) {
2079 // We can only fail the whole thing if no request has been
2080 // submitted yet. Otherwise we'll wait for the submitted AIOs to
2081 // complete and report the error in the callback.
2082 if (mcb->num_requests == 0) {
0f0b604b 2083 reqs[i].error = -EIO;
40b4f539
KW
2084 goto fail;
2085 } else {
7eb58a6c
KW
2086 mcb->num_requests++;
2087 multiwrite_cb(mcb, -EIO);
40b4f539
KW
2088 break;
2089 }
2090 } else {
2091 mcb->num_requests++;
2092 }
2093 }
2094
2095 return 0;
2096
2097fail:
af474591 2098 qemu_free(mcb);
40b4f539
KW
2099 return -1;
2100}
2101
b2e12bc6
CH
2102BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2103 BlockDriverCompletionFunc *cb, void *opaque)
2104{
2105 BlockDriver *drv = bs->drv;
2106
016f5cf6
AG
2107 if (bs->open_flags & BDRV_O_NO_FLUSH) {
2108 return bdrv_aio_noop_em(bs, cb, opaque);
2109 }
2110
b2e12bc6
CH
2111 if (!drv)
2112 return NULL;
b2e12bc6
CH
2113 return drv->bdrv_aio_flush(bs, cb, opaque);
2114}
2115
83f64091 2116void bdrv_aio_cancel(BlockDriverAIOCB *acb)
83f64091 2117{
6bbff9a0 2118 acb->pool->cancel(acb);
83f64091
FB
2119}
2120
ce1a14dc 2121
83f64091
FB
2122/**************************************************************/
2123/* async block device emulation */
2124
c16b5a2c
CH
2125typedef struct BlockDriverAIOCBSync {
2126 BlockDriverAIOCB common;
2127 QEMUBH *bh;
2128 int ret;
2129 /* vector translation state */
2130 QEMUIOVector *qiov;
2131 uint8_t *bounce;
2132 int is_write;
2133} BlockDriverAIOCBSync;
2134
2135static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2136{
b666d239
KW
2137 BlockDriverAIOCBSync *acb =
2138 container_of(blockacb, BlockDriverAIOCBSync, common);
6a7ad299 2139 qemu_bh_delete(acb->bh);
36afc451 2140 acb->bh = NULL;
c16b5a2c
CH
2141 qemu_aio_release(acb);
2142}
2143
2144static AIOPool bdrv_em_aio_pool = {
2145 .aiocb_size = sizeof(BlockDriverAIOCBSync),
2146 .cancel = bdrv_aio_cancel_em,
2147};
2148
ce1a14dc 2149static void bdrv_aio_bh_cb(void *opaque)
83f64091 2150{
ce1a14dc 2151 BlockDriverAIOCBSync *acb = opaque;
f141eafe 2152
f141eafe
AL
2153 if (!acb->is_write)
2154 qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
ceb42de8 2155 qemu_vfree(acb->bounce);
ce1a14dc 2156 acb->common.cb(acb->common.opaque, acb->ret);
6a7ad299 2157 qemu_bh_delete(acb->bh);
36afc451 2158 acb->bh = NULL;
ce1a14dc 2159 qemu_aio_release(acb);
83f64091 2160}
beac80cd 2161
f141eafe
AL
2162static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2163 int64_t sector_num,
2164 QEMUIOVector *qiov,
2165 int nb_sectors,
2166 BlockDriverCompletionFunc *cb,
2167 void *opaque,
2168 int is_write)
2169
83f64091 2170{
ce1a14dc 2171 BlockDriverAIOCBSync *acb;
ce1a14dc 2172
c16b5a2c 2173 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
f141eafe
AL
2174 acb->is_write = is_write;
2175 acb->qiov = qiov;
e268ca52 2176 acb->bounce = qemu_blockalign(bs, qiov->size);
f141eafe 2177
ce1a14dc
PB
2178 if (!acb->bh)
2179 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
f141eafe
AL
2180
2181 if (is_write) {
2182 qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2183 acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2184 } else {
2185 acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2186 }
2187
ce1a14dc 2188 qemu_bh_schedule(acb->bh);
f141eafe 2189
ce1a14dc 2190 return &acb->common;
beac80cd
FB
2191}
2192
f141eafe
AL
2193static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2194 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
ce1a14dc 2195 BlockDriverCompletionFunc *cb, void *opaque)
beac80cd 2196{
f141eafe
AL
2197 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2198}
83f64091 2199
f141eafe
AL
2200static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2201 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2202 BlockDriverCompletionFunc *cb, void *opaque)
2203{
2204 return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
beac80cd 2205}
beac80cd 2206
b2e12bc6
CH
2207static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2208 BlockDriverCompletionFunc *cb, void *opaque)
2209{
2210 BlockDriverAIOCBSync *acb;
2211
2212 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2213 acb->is_write = 1; /* don't bounce in the completion hadler */
2214 acb->qiov = NULL;
2215 acb->bounce = NULL;
2216 acb->ret = 0;
2217
2218 if (!acb->bh)
2219 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2220
2221 bdrv_flush(bs);
2222 qemu_bh_schedule(acb->bh);
2223 return &acb->common;
2224}
2225
016f5cf6
AG
2226static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2227 BlockDriverCompletionFunc *cb, void *opaque)
2228{
2229 BlockDriverAIOCBSync *acb;
2230
2231 acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2232 acb->is_write = 1; /* don't bounce in the completion handler */
2233 acb->qiov = NULL;
2234 acb->bounce = NULL;
2235 acb->ret = 0;
2236
2237 if (!acb->bh) {
2238 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2239 }
2240
2241 qemu_bh_schedule(acb->bh);
2242 return &acb->common;
2243}
2244
83f64091
FB
2245/**************************************************************/
2246/* sync block device emulation */
ea2384d3 2247
83f64091
FB
2248static void bdrv_rw_em_cb(void *opaque, int ret)
2249{
2250 *(int *)opaque = ret;
ea2384d3
FB
2251}
2252
83f64091
FB
2253#define NOT_DONE 0x7fffffff
2254
5fafdf24 2255static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
83f64091 2256 uint8_t *buf, int nb_sectors)
7a6cba61 2257{
ce1a14dc
PB
2258 int async_ret;
2259 BlockDriverAIOCB *acb;
f141eafe
AL
2260 struct iovec iov;
2261 QEMUIOVector qiov;
83f64091 2262
65d6b3d8
KW
2263 async_context_push();
2264
83f64091 2265 async_ret = NOT_DONE;
3f4cb3d3 2266 iov.iov_base = (void *)buf;
f141eafe
AL
2267 iov.iov_len = nb_sectors * 512;
2268 qemu_iovec_init_external(&qiov, &iov, 1);
2269 acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2270 bdrv_rw_em_cb, &async_ret);
65d6b3d8
KW
2271 if (acb == NULL) {
2272 async_ret = -1;
2273 goto fail;
2274 }
baf35cb9 2275
83f64091
FB
2276 while (async_ret == NOT_DONE) {
2277 qemu_aio_wait();
2278 }
baf35cb9 2279
65d6b3d8
KW
2280
2281fail:
2282 async_context_pop();
83f64091 2283 return async_ret;
7a6cba61
PB
2284}
2285
83f64091
FB
2286static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2287 const uint8_t *buf, int nb_sectors)
2288{
ce1a14dc
PB
2289 int async_ret;
2290 BlockDriverAIOCB *acb;
f141eafe
AL
2291 struct iovec iov;
2292 QEMUIOVector qiov;
83f64091 2293
65d6b3d8
KW
2294 async_context_push();
2295
83f64091 2296 async_ret = NOT_DONE;
f141eafe
AL
2297 iov.iov_base = (void *)buf;
2298 iov.iov_len = nb_sectors * 512;
2299 qemu_iovec_init_external(&qiov, &iov, 1);
2300 acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2301 bdrv_rw_em_cb, &async_ret);
65d6b3d8
KW
2302 if (acb == NULL) {
2303 async_ret = -1;
2304 goto fail;
2305 }
83f64091
FB
2306 while (async_ret == NOT_DONE) {
2307 qemu_aio_wait();
2308 }
65d6b3d8
KW
2309
2310fail:
2311 async_context_pop();
83f64091
FB
2312 return async_ret;
2313}
ea2384d3
FB
2314
2315void bdrv_init(void)
2316{
5efa9d5a 2317 module_call_init(MODULE_INIT_BLOCK);
ea2384d3 2318}
ce1a14dc 2319
eb852011
MA
2320void bdrv_init_with_whitelist(void)
2321{
2322 use_bdrv_whitelist = 1;
2323 bdrv_init();
2324}
2325
c16b5a2c
CH
2326void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2327 BlockDriverCompletionFunc *cb, void *opaque)
ce1a14dc 2328{
ce1a14dc
PB
2329 BlockDriverAIOCB *acb;
2330
6bbff9a0
AL
2331 if (pool->free_aiocb) {
2332 acb = pool->free_aiocb;
2333 pool->free_aiocb = acb->next;
ce1a14dc 2334 } else {
6bbff9a0
AL
2335 acb = qemu_mallocz(pool->aiocb_size);
2336 acb->pool = pool;
ce1a14dc
PB
2337 }
2338 acb->bs = bs;
2339 acb->cb = cb;
2340 acb->opaque = opaque;
2341 return acb;
2342}
2343
2344void qemu_aio_release(void *p)
2345{
6bbff9a0
AL
2346 BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2347 AIOPool *pool = acb->pool;
2348 acb->next = pool->free_aiocb;
2349 pool->free_aiocb = acb;
ce1a14dc 2350}
19cb3738
FB
2351
2352/**************************************************************/
2353/* removable device support */
2354
2355/**
2356 * Return TRUE if the media is present
2357 */
2358int bdrv_is_inserted(BlockDriverState *bs)
2359{
2360 BlockDriver *drv = bs->drv;
2361 int ret;
2362 if (!drv)
2363 return 0;
2364 if (!drv->bdrv_is_inserted)
2365 return 1;
2366 ret = drv->bdrv_is_inserted(bs);
2367 return ret;
2368}
2369
2370/**
2371 * Return TRUE if the media changed since the last call to this
5fafdf24 2372 * function. It is currently only used for floppy disks
19cb3738
FB
2373 */
2374int bdrv_media_changed(BlockDriverState *bs)
2375{
2376 BlockDriver *drv = bs->drv;
2377 int ret;
2378
2379 if (!drv || !drv->bdrv_media_changed)
2380 ret = -ENOTSUP;
2381 else
2382 ret = drv->bdrv_media_changed(bs);
2383 if (ret == -ENOTSUP)
2384 ret = bs->media_changed;
2385 bs->media_changed = 0;
2386 return ret;
2387}
2388
2389/**
2390 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2391 */
aea2a33c 2392int bdrv_eject(BlockDriverState *bs, int eject_flag)
19cb3738
FB
2393{
2394 BlockDriver *drv = bs->drv;
2395 int ret;
2396
aea2a33c
MM
2397 if (bs->locked) {
2398 return -EBUSY;
2399 }
2400
19cb3738
FB
2401 if (!drv || !drv->bdrv_eject) {
2402 ret = -ENOTSUP;
2403 } else {
2404 ret = drv->bdrv_eject(bs, eject_flag);
2405 }
2406 if (ret == -ENOTSUP) {
2407 if (eject_flag)
2408 bdrv_close(bs);
aea2a33c 2409 ret = 0;
19cb3738 2410 }
aea2a33c
MM
2411
2412 return ret;
19cb3738
FB
2413}
2414
2415int bdrv_is_locked(BlockDriverState *bs)
2416{
2417 return bs->locked;
2418}
2419
2420/**
2421 * Lock or unlock the media (if it is locked, the user won't be able
2422 * to eject it manually).
2423 */
2424void bdrv_set_locked(BlockDriverState *bs, int locked)
2425{
2426 BlockDriver *drv = bs->drv;
2427
2428 bs->locked = locked;
2429 if (drv && drv->bdrv_set_locked) {
2430 drv->bdrv_set_locked(bs, locked);
2431 }
2432}
985a03b0
TS
2433
2434/* needed for generic scsi interface */
2435
2436int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2437{
2438 BlockDriver *drv = bs->drv;
2439
2440 if (drv && drv->bdrv_ioctl)
2441 return drv->bdrv_ioctl(bs, req, buf);
2442 return -ENOTSUP;
2443}
7d780669 2444
221f715d
AL
2445BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2446 unsigned long int req, void *buf,
2447 BlockDriverCompletionFunc *cb, void *opaque)
7d780669 2448{
221f715d 2449 BlockDriver *drv = bs->drv;
7d780669 2450
221f715d
AL
2451 if (drv && drv->bdrv_aio_ioctl)
2452 return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2453 return NULL;
7d780669 2454}
e268ca52 2455
7cd1e32a 2456
2457
e268ca52
AL
2458void *qemu_blockalign(BlockDriverState *bs, size_t size)
2459{
2460 return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2461}
7cd1e32a 2462
2463void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2464{
2465 int64_t bitmap_size;
a55eb92c 2466
aaa0eb75 2467 bs->dirty_count = 0;
a55eb92c 2468 if (enable) {
c6d22830
JK
2469 if (!bs->dirty_bitmap) {
2470 bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2471 BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2472 bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
a55eb92c 2473
7cd1e32a 2474 bs->dirty_bitmap = qemu_mallocz(bitmap_size);
a55eb92c 2475 }
7cd1e32a 2476 } else {
c6d22830 2477 if (bs->dirty_bitmap) {
7cd1e32a 2478 qemu_free(bs->dirty_bitmap);
c6d22830 2479 bs->dirty_bitmap = NULL;
a55eb92c 2480 }
7cd1e32a 2481 }
2482}
2483
2484int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2485{
6ea44308 2486 int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
a55eb92c 2487
c6d22830
JK
2488 if (bs->dirty_bitmap &&
2489 (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2490 return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2491 (1 << (chunk % (sizeof(unsigned long) * 8)));
7cd1e32a 2492 } else {
2493 return 0;
2494 }
2495}
2496
a55eb92c
JK
2497void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2498 int nr_sectors)
7cd1e32a 2499{
2500 set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2501}
aaa0eb75
LS
2502
2503int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2504{
2505 return bs->dirty_count;
2506}