xref: /openbmc/qemu/block.c (revision 16905d717507d3daffa714c7f0fd5403873807b2)
1fc01f7e7Sbellard /*
2fc01f7e7Sbellard  * QEMU System Emulator block driver
3fc01f7e7Sbellard  *
4fc01f7e7Sbellard  * Copyright (c) 2003 Fabrice Bellard
5fc01f7e7Sbellard  *
6fc01f7e7Sbellard  * Permission is hereby granted, free of charge, to any person obtaining a copy
7fc01f7e7Sbellard  * of this software and associated documentation files (the "Software"), to deal
8fc01f7e7Sbellard  * in the Software without restriction, including without limitation the rights
9fc01f7e7Sbellard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10fc01f7e7Sbellard  * copies of the Software, and to permit persons to whom the Software is
11fc01f7e7Sbellard  * furnished to do so, subject to the following conditions:
12fc01f7e7Sbellard  *
13fc01f7e7Sbellard  * The above copyright notice and this permission notice shall be included in
14fc01f7e7Sbellard  * all copies or substantial portions of the Software.
15fc01f7e7Sbellard  *
16fc01f7e7Sbellard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17fc01f7e7Sbellard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18fc01f7e7Sbellard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19fc01f7e7Sbellard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20fc01f7e7Sbellard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21fc01f7e7Sbellard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22fc01f7e7Sbellard  * THE SOFTWARE.
23fc01f7e7Sbellard  */
243990d09aSblueswir1 #include "config-host.h"
25faf07963Spbrook #include "qemu-common.h"
266d519a5fSStefan Hajnoczi #include "trace.h"
27376253ecSaliguori #include "monitor.h"
28ea2384d3Sbellard #include "block_int.h"
295efa9d5aSAnthony Liguori #include "module.h"
30d15e5465SLuiz Capitulino #include "qemu-objects.h"
31fc01f7e7Sbellard 
3271e72a19SJuan Quintela #ifdef CONFIG_BSD
337674e7bfSbellard #include <sys/types.h>
347674e7bfSbellard #include <sys/stat.h>
357674e7bfSbellard #include <sys/ioctl.h>
3672cf2d4fSBlue Swirl #include <sys/queue.h>
37c5e97233Sblueswir1 #ifndef __DragonFly__
387674e7bfSbellard #include <sys/disk.h>
397674e7bfSbellard #endif
40c5e97233Sblueswir1 #endif
417674e7bfSbellard 
4249dc768dSaliguori #ifdef _WIN32
4349dc768dSaliguori #include <windows.h>
4449dc768dSaliguori #endif
4549dc768dSaliguori 
46f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
47f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
48c87c0672Saliguori         BlockDriverCompletionFunc *cb, void *opaque);
49f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
50f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
51ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
52b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
53b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque);
54016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
55016f5cf6SAlexander Graf         BlockDriverCompletionFunc *cb, void *opaque);
5683f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
5783f64091Sbellard                         uint8_t *buf, int nb_sectors);
5883f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
5983f64091Sbellard                          const uint8_t *buf, int nb_sectors);
60ec530c81Sbellard 
611b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
621b7bdbc1SStefan Hajnoczi     QTAILQ_HEAD_INITIALIZER(bdrv_states);
637ee930d0Sblueswir1 
648a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers =
658a22f02aSStefan Hajnoczi     QLIST_HEAD_INITIALIZER(bdrv_drivers);
66ea2384d3Sbellard 
67f9092b10SMarkus Armbruster /* The device to use for VM snapshots */
68f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots;
69f9092b10SMarkus Armbruster 
70eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */
71eb852011SMarkus Armbruster static int use_bdrv_whitelist;
72eb852011SMarkus Armbruster 
7383f64091Sbellard int path_is_absolute(const char *path)
7483f64091Sbellard {
7583f64091Sbellard     const char *p;
7621664424Sbellard #ifdef _WIN32
7721664424Sbellard     /* specific case for names like: "\\.\d:" */
7821664424Sbellard     if (*path == '/' || *path == '\\')
7921664424Sbellard         return 1;
8021664424Sbellard #endif
8183f64091Sbellard     p = strchr(path, ':');
8283f64091Sbellard     if (p)
8383f64091Sbellard         p++;
8483f64091Sbellard     else
8583f64091Sbellard         p = path;
863b9f94e1Sbellard #ifdef _WIN32
873b9f94e1Sbellard     return (*p == '/' || *p == '\\');
883b9f94e1Sbellard #else
893b9f94e1Sbellard     return (*p == '/');
903b9f94e1Sbellard #endif
9183f64091Sbellard }
9283f64091Sbellard 
9383f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
9483f64091Sbellard    path to it by considering it is relative to base_path. URL are
9583f64091Sbellard    supported. */
9683f64091Sbellard void path_combine(char *dest, int dest_size,
9783f64091Sbellard                   const char *base_path,
9883f64091Sbellard                   const char *filename)
9983f64091Sbellard {
10083f64091Sbellard     const char *p, *p1;
10183f64091Sbellard     int len;
10283f64091Sbellard 
10383f64091Sbellard     if (dest_size <= 0)
10483f64091Sbellard         return;
10583f64091Sbellard     if (path_is_absolute(filename)) {
10683f64091Sbellard         pstrcpy(dest, dest_size, filename);
10783f64091Sbellard     } else {
10883f64091Sbellard         p = strchr(base_path, ':');
10983f64091Sbellard         if (p)
11083f64091Sbellard             p++;
11183f64091Sbellard         else
11283f64091Sbellard             p = base_path;
1133b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1143b9f94e1Sbellard #ifdef _WIN32
1153b9f94e1Sbellard         {
1163b9f94e1Sbellard             const char *p2;
1173b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1183b9f94e1Sbellard             if (!p1 || p2 > p1)
1193b9f94e1Sbellard                 p1 = p2;
1203b9f94e1Sbellard         }
1213b9f94e1Sbellard #endif
12283f64091Sbellard         if (p1)
12383f64091Sbellard             p1++;
12483f64091Sbellard         else
12583f64091Sbellard             p1 = base_path;
12683f64091Sbellard         if (p1 > p)
12783f64091Sbellard             p = p1;
12883f64091Sbellard         len = p - base_path;
12983f64091Sbellard         if (len > dest_size - 1)
13083f64091Sbellard             len = dest_size - 1;
13183f64091Sbellard         memcpy(dest, base_path, len);
13283f64091Sbellard         dest[len] = '\0';
13383f64091Sbellard         pstrcat(dest, dest_size, filename);
13483f64091Sbellard     }
13583f64091Sbellard }
13683f64091Sbellard 
1375efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv)
138ea2384d3Sbellard {
139f141eafeSaliguori     if (!bdrv->bdrv_aio_readv) {
14083f64091Sbellard         /* add AIO emulation layer */
141f141eafeSaliguori         bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
142f141eafeSaliguori         bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
143eda578e5Saliguori     } else if (!bdrv->bdrv_read) {
14483f64091Sbellard         /* add synchronous IO emulation layer */
14583f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
14683f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
14783f64091Sbellard     }
148b2e12bc6SChristoph Hellwig 
149b2e12bc6SChristoph Hellwig     if (!bdrv->bdrv_aio_flush)
150b2e12bc6SChristoph Hellwig         bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
151b2e12bc6SChristoph Hellwig 
1528a22f02aSStefan Hajnoczi     QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
153ea2384d3Sbellard }
154b338082bSbellard 
155b338082bSbellard /* create a new block device (by default it is empty) */
156b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
157fc01f7e7Sbellard {
1581b7bdbc1SStefan Hajnoczi     BlockDriverState *bs;
159b338082bSbellard 
160b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
161b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
162ea2384d3Sbellard     if (device_name[0] != '\0') {
1631b7bdbc1SStefan Hajnoczi         QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
164ea2384d3Sbellard     }
165b338082bSbellard     return bs;
166b338082bSbellard }
167b338082bSbellard 
168ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
169ea2384d3Sbellard {
170ea2384d3Sbellard     BlockDriver *drv1;
1718a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
1728a22f02aSStefan Hajnoczi         if (!strcmp(drv1->format_name, format_name)) {
173ea2384d3Sbellard             return drv1;
174ea2384d3Sbellard         }
1758a22f02aSStefan Hajnoczi     }
176ea2384d3Sbellard     return NULL;
177ea2384d3Sbellard }
178ea2384d3Sbellard 
179eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv)
180eb852011SMarkus Armbruster {
181eb852011SMarkus Armbruster     static const char *whitelist[] = {
182eb852011SMarkus Armbruster         CONFIG_BDRV_WHITELIST
183eb852011SMarkus Armbruster     };
184eb852011SMarkus Armbruster     const char **p;
185eb852011SMarkus Armbruster 
186eb852011SMarkus Armbruster     if (!whitelist[0])
187eb852011SMarkus Armbruster         return 1;               /* no whitelist, anything goes */
188eb852011SMarkus Armbruster 
189eb852011SMarkus Armbruster     for (p = whitelist; *p; p++) {
190eb852011SMarkus Armbruster         if (!strcmp(drv->format_name, *p)) {
191eb852011SMarkus Armbruster             return 1;
192eb852011SMarkus Armbruster         }
193eb852011SMarkus Armbruster     }
194eb852011SMarkus Armbruster     return 0;
195eb852011SMarkus Armbruster }
196eb852011SMarkus Armbruster 
197eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
198eb852011SMarkus Armbruster {
199eb852011SMarkus Armbruster     BlockDriver *drv = bdrv_find_format(format_name);
200eb852011SMarkus Armbruster     return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
201eb852011SMarkus Armbruster }
202eb852011SMarkus Armbruster 
2030e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename,
2040e7e1989SKevin Wolf     QEMUOptionParameter *options)
205ea2384d3Sbellard {
206ea2384d3Sbellard     if (!drv->bdrv_create)
207ea2384d3Sbellard         return -ENOTSUP;
2080e7e1989SKevin Wolf 
2090e7e1989SKevin Wolf     return drv->bdrv_create(filename, options);
210ea2384d3Sbellard }
211ea2384d3Sbellard 
21284a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
21384a12e66SChristoph Hellwig {
21484a12e66SChristoph Hellwig     BlockDriver *drv;
21584a12e66SChristoph Hellwig 
216b50cbabcSMORITA Kazutaka     drv = bdrv_find_protocol(filename);
21784a12e66SChristoph Hellwig     if (drv == NULL) {
218*16905d71SStefan Hajnoczi         return -ENOENT;
21984a12e66SChristoph Hellwig     }
22084a12e66SChristoph Hellwig 
22184a12e66SChristoph Hellwig     return bdrv_create(drv, filename, options);
22284a12e66SChristoph Hellwig }
22384a12e66SChristoph Hellwig 
224d5249393Sbellard #ifdef _WIN32
22595389c86Sbellard void get_tmp_filename(char *filename, int size)
226d5249393Sbellard {
2273b9f94e1Sbellard     char temp_dir[MAX_PATH];
2283b9f94e1Sbellard 
2293b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
2303b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
231d5249393Sbellard }
232d5249393Sbellard #else
23395389c86Sbellard void get_tmp_filename(char *filename, int size)
234ea2384d3Sbellard {
235ea2384d3Sbellard     int fd;
2367ccfb2ebSblueswir1     const char *tmpdir;
237d5249393Sbellard     /* XXX: race condition possible */
2380badc1eeSaurel32     tmpdir = getenv("TMPDIR");
2390badc1eeSaurel32     if (!tmpdir)
2400badc1eeSaurel32         tmpdir = "/tmp";
2410badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
242ea2384d3Sbellard     fd = mkstemp(filename);
243ea2384d3Sbellard     close(fd);
244ea2384d3Sbellard }
245d5249393Sbellard #endif
246ea2384d3Sbellard 
24719cb3738Sbellard #ifdef _WIN32
248f45512feSbellard static int is_windows_drive_prefix(const char *filename)
249f45512feSbellard {
250f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
251f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
252f45512feSbellard             filename[1] == ':');
253f45512feSbellard }
254f45512feSbellard 
255508c7cb3SChristoph Hellwig int is_windows_drive(const char *filename)
25619cb3738Sbellard {
257f45512feSbellard     if (is_windows_drive_prefix(filename) &&
258f45512feSbellard         filename[2] == '\0')
25919cb3738Sbellard         return 1;
26019cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
26119cb3738Sbellard         strstart(filename, "//./", NULL))
26219cb3738Sbellard         return 1;
26319cb3738Sbellard     return 0;
26419cb3738Sbellard }
26519cb3738Sbellard #endif
26619cb3738Sbellard 
267f3a5d3f8SChristoph Hellwig /*
268f3a5d3f8SChristoph Hellwig  * Detect host devices. By convention, /dev/cdrom[N] is always
269f3a5d3f8SChristoph Hellwig  * recognized as a host CDROM.
270f3a5d3f8SChristoph Hellwig  */
271f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename)
272f3a5d3f8SChristoph Hellwig {
273508c7cb3SChristoph Hellwig     int score_max = 0, score;
274508c7cb3SChristoph Hellwig     BlockDriver *drv = NULL, *d;
275f3a5d3f8SChristoph Hellwig 
2768a22f02aSStefan Hajnoczi     QLIST_FOREACH(d, &bdrv_drivers, list) {
277508c7cb3SChristoph Hellwig         if (d->bdrv_probe_device) {
278508c7cb3SChristoph Hellwig             score = d->bdrv_probe_device(filename);
279508c7cb3SChristoph Hellwig             if (score > score_max) {
280508c7cb3SChristoph Hellwig                 score_max = score;
281508c7cb3SChristoph Hellwig                 drv = d;
282f3a5d3f8SChristoph Hellwig             }
283508c7cb3SChristoph Hellwig         }
284f3a5d3f8SChristoph Hellwig     }
285f3a5d3f8SChristoph Hellwig 
286508c7cb3SChristoph Hellwig     return drv;
287f3a5d3f8SChristoph Hellwig }
288f3a5d3f8SChristoph Hellwig 
289b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename)
29084a12e66SChristoph Hellwig {
29184a12e66SChristoph Hellwig     BlockDriver *drv1;
29284a12e66SChristoph Hellwig     char protocol[128];
29384a12e66SChristoph Hellwig     int len;
29484a12e66SChristoph Hellwig     const char *p;
29584a12e66SChristoph Hellwig 
29666f82ceeSKevin Wolf     /* TODO Drivers without bdrv_file_open must be specified explicitly */
29766f82ceeSKevin Wolf 
29839508e7aSChristoph Hellwig     /*
29939508e7aSChristoph Hellwig      * XXX(hch): we really should not let host device detection
30039508e7aSChristoph Hellwig      * override an explicit protocol specification, but moving this
30139508e7aSChristoph Hellwig      * later breaks access to device names with colons in them.
30239508e7aSChristoph Hellwig      * Thanks to the brain-dead persistent naming schemes on udev-
30339508e7aSChristoph Hellwig      * based Linux systems those actually are quite common.
30439508e7aSChristoph Hellwig      */
30584a12e66SChristoph Hellwig     drv1 = find_hdev_driver(filename);
30639508e7aSChristoph Hellwig     if (drv1) {
30784a12e66SChristoph Hellwig         return drv1;
30884a12e66SChristoph Hellwig     }
30939508e7aSChristoph Hellwig 
31039508e7aSChristoph Hellwig #ifdef _WIN32
31139508e7aSChristoph Hellwig      if (is_windows_drive(filename) ||
31239508e7aSChristoph Hellwig          is_windows_drive_prefix(filename))
31339508e7aSChristoph Hellwig          return bdrv_find_format("file");
31439508e7aSChristoph Hellwig #endif
31539508e7aSChristoph Hellwig 
31639508e7aSChristoph Hellwig     p = strchr(filename, ':');
31739508e7aSChristoph Hellwig     if (!p) {
31839508e7aSChristoph Hellwig         return bdrv_find_format("file");
31939508e7aSChristoph Hellwig     }
32084a12e66SChristoph Hellwig     len = p - filename;
32184a12e66SChristoph Hellwig     if (len > sizeof(protocol) - 1)
32284a12e66SChristoph Hellwig         len = sizeof(protocol) - 1;
32384a12e66SChristoph Hellwig     memcpy(protocol, filename, len);
32484a12e66SChristoph Hellwig     protocol[len] = '\0';
32584a12e66SChristoph Hellwig     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
32684a12e66SChristoph Hellwig         if (drv1->protocol_name &&
32784a12e66SChristoph Hellwig             !strcmp(drv1->protocol_name, protocol)) {
32884a12e66SChristoph Hellwig             return drv1;
32984a12e66SChristoph Hellwig         }
33084a12e66SChristoph Hellwig     }
33184a12e66SChristoph Hellwig     return NULL;
33284a12e66SChristoph Hellwig }
33384a12e66SChristoph Hellwig 
334c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv)
335ea2384d3Sbellard {
33683f64091Sbellard     int ret, score, score_max;
337ea2384d3Sbellard     BlockDriver *drv1, *drv;
33883f64091Sbellard     uint8_t buf[2048];
33983f64091Sbellard     BlockDriverState *bs;
340ea2384d3Sbellard 
341f5edb014SNaphtali Sprei     ret = bdrv_file_open(&bs, filename, 0);
342c98ac35dSStefan Weil     if (ret < 0) {
343c98ac35dSStefan Weil         *pdrv = NULL;
344c98ac35dSStefan Weil         return ret;
345c98ac35dSStefan Weil     }
346f8ea0b00SNicholas Bellinger 
34708a00559SKevin Wolf     /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
34808a00559SKevin Wolf     if (bs->sg || !bdrv_is_inserted(bs)) {
3491a396859SNicholas A. Bellinger         bdrv_delete(bs);
350c98ac35dSStefan Weil         drv = bdrv_find_format("raw");
351c98ac35dSStefan Weil         if (!drv) {
352c98ac35dSStefan Weil             ret = -ENOENT;
353c98ac35dSStefan Weil         }
354c98ac35dSStefan Weil         *pdrv = drv;
355c98ac35dSStefan Weil         return ret;
3561a396859SNicholas A. Bellinger     }
357f8ea0b00SNicholas Bellinger 
35883f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
35983f64091Sbellard     bdrv_delete(bs);
360ea2384d3Sbellard     if (ret < 0) {
361c98ac35dSStefan Weil         *pdrv = NULL;
362c98ac35dSStefan Weil         return ret;
363ea2384d3Sbellard     }
364ea2384d3Sbellard 
365ea2384d3Sbellard     score_max = 0;
36684a12e66SChristoph Hellwig     drv = NULL;
3678a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
36883f64091Sbellard         if (drv1->bdrv_probe) {
369ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
370ea2384d3Sbellard             if (score > score_max) {
371ea2384d3Sbellard                 score_max = score;
372ea2384d3Sbellard                 drv = drv1;
373ea2384d3Sbellard             }
374ea2384d3Sbellard         }
37583f64091Sbellard     }
376c98ac35dSStefan Weil     if (!drv) {
377c98ac35dSStefan Weil         ret = -ENOENT;
378c98ac35dSStefan Weil     }
379c98ac35dSStefan Weil     *pdrv = drv;
380c98ac35dSStefan Weil     return ret;
381ea2384d3Sbellard }
382ea2384d3Sbellard 
38351762288SStefan Hajnoczi /**
38451762288SStefan Hajnoczi  * Set the current 'total_sectors' value
38551762288SStefan Hajnoczi  */
38651762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
38751762288SStefan Hajnoczi {
38851762288SStefan Hajnoczi     BlockDriver *drv = bs->drv;
38951762288SStefan Hajnoczi 
390396759adSNicholas Bellinger     /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
391396759adSNicholas Bellinger     if (bs->sg)
392396759adSNicholas Bellinger         return 0;
393396759adSNicholas Bellinger 
39451762288SStefan Hajnoczi     /* query actual device if possible, otherwise just trust the hint */
39551762288SStefan Hajnoczi     if (drv->bdrv_getlength) {
39651762288SStefan Hajnoczi         int64_t length = drv->bdrv_getlength(bs);
39751762288SStefan Hajnoczi         if (length < 0) {
39851762288SStefan Hajnoczi             return length;
39951762288SStefan Hajnoczi         }
40051762288SStefan Hajnoczi         hint = length >> BDRV_SECTOR_BITS;
40151762288SStefan Hajnoczi     }
40251762288SStefan Hajnoczi 
40351762288SStefan Hajnoczi     bs->total_sectors = hint;
40451762288SStefan Hajnoczi     return 0;
40551762288SStefan Hajnoczi }
40651762288SStefan Hajnoczi 
407b6ce07aaSKevin Wolf /*
40857915332SKevin Wolf  * Common part for opening disk images and files
40957915332SKevin Wolf  */
41057915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename,
41157915332SKevin Wolf     int flags, BlockDriver *drv)
41257915332SKevin Wolf {
41357915332SKevin Wolf     int ret, open_flags;
41457915332SKevin Wolf 
41557915332SKevin Wolf     assert(drv != NULL);
41657915332SKevin Wolf 
41766f82ceeSKevin Wolf     bs->file = NULL;
41851762288SStefan Hajnoczi     bs->total_sectors = 0;
41957915332SKevin Wolf     bs->encrypted = 0;
42057915332SKevin Wolf     bs->valid_key = 0;
42157915332SKevin Wolf     bs->open_flags = flags;
42257915332SKevin Wolf     /* buffer_alignment defaulted to 512, drivers can change this value */
42357915332SKevin Wolf     bs->buffer_alignment = 512;
42457915332SKevin Wolf 
42557915332SKevin Wolf     pstrcpy(bs->filename, sizeof(bs->filename), filename);
42657915332SKevin Wolf 
42757915332SKevin Wolf     if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
42857915332SKevin Wolf         return -ENOTSUP;
42957915332SKevin Wolf     }
43057915332SKevin Wolf 
43157915332SKevin Wolf     bs->drv = drv;
43257915332SKevin Wolf     bs->opaque = qemu_mallocz(drv->instance_size);
43357915332SKevin Wolf 
43457915332SKevin Wolf     /*
43557915332SKevin Wolf      * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
43657915332SKevin Wolf      * write cache to the guest.  We do need the fdatasync to flush
43757915332SKevin Wolf      * out transactions for block allocations, and we maybe have a
43857915332SKevin Wolf      * volatile write cache in our backing device to deal with.
43957915332SKevin Wolf      */
44057915332SKevin Wolf     if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
44157915332SKevin Wolf         bs->enable_write_cache = 1;
44257915332SKevin Wolf 
44357915332SKevin Wolf     /*
44457915332SKevin Wolf      * Clear flags that are internal to the block layer before opening the
44557915332SKevin Wolf      * image.
44657915332SKevin Wolf      */
44757915332SKevin Wolf     open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
44857915332SKevin Wolf 
44957915332SKevin Wolf     /*
45057915332SKevin Wolf      * Snapshots should be writeable.
45157915332SKevin Wolf      */
45257915332SKevin Wolf     if (bs->is_temporary) {
45357915332SKevin Wolf         open_flags |= BDRV_O_RDWR;
45457915332SKevin Wolf     }
45557915332SKevin Wolf 
45666f82ceeSKevin Wolf     /* Open the image, either directly or using a protocol */
45766f82ceeSKevin Wolf     if (drv->bdrv_file_open) {
45866f82ceeSKevin Wolf         ret = drv->bdrv_file_open(bs, filename, open_flags);
45966f82ceeSKevin Wolf     } else {
46066f82ceeSKevin Wolf         ret = bdrv_file_open(&bs->file, filename, open_flags);
46166f82ceeSKevin Wolf         if (ret >= 0) {
46266f82ceeSKevin Wolf             ret = drv->bdrv_open(bs, open_flags);
46366f82ceeSKevin Wolf         }
46466f82ceeSKevin Wolf     }
46566f82ceeSKevin Wolf 
46657915332SKevin Wolf     if (ret < 0) {
46757915332SKevin Wolf         goto free_and_fail;
46857915332SKevin Wolf     }
46957915332SKevin Wolf 
47057915332SKevin Wolf     bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
47151762288SStefan Hajnoczi 
47251762288SStefan Hajnoczi     ret = refresh_total_sectors(bs, bs->total_sectors);
47351762288SStefan Hajnoczi     if (ret < 0) {
47451762288SStefan Hajnoczi         goto free_and_fail;
47557915332SKevin Wolf     }
47651762288SStefan Hajnoczi 
47757915332SKevin Wolf #ifndef _WIN32
47857915332SKevin Wolf     if (bs->is_temporary) {
47957915332SKevin Wolf         unlink(filename);
48057915332SKevin Wolf     }
48157915332SKevin Wolf #endif
48257915332SKevin Wolf     return 0;
48357915332SKevin Wolf 
48457915332SKevin Wolf free_and_fail:
48566f82ceeSKevin Wolf     if (bs->file) {
48666f82ceeSKevin Wolf         bdrv_delete(bs->file);
48766f82ceeSKevin Wolf         bs->file = NULL;
48866f82ceeSKevin Wolf     }
48957915332SKevin Wolf     qemu_free(bs->opaque);
49057915332SKevin Wolf     bs->opaque = NULL;
49157915332SKevin Wolf     bs->drv = NULL;
49257915332SKevin Wolf     return ret;
49357915332SKevin Wolf }
49457915332SKevin Wolf 
49557915332SKevin Wolf /*
496b6ce07aaSKevin Wolf  * Opens a file using a protocol (file, host_device, nbd, ...)
497b6ce07aaSKevin Wolf  */
49883f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
499b338082bSbellard {
50083f64091Sbellard     BlockDriverState *bs;
5016db95603SChristoph Hellwig     BlockDriver *drv;
50283f64091Sbellard     int ret;
5033b0d4f61Sbellard 
504b50cbabcSMORITA Kazutaka     drv = bdrv_find_protocol(filename);
5056db95603SChristoph Hellwig     if (!drv) {
5066db95603SChristoph Hellwig         return -ENOENT;
5076db95603SChristoph Hellwig     }
5086db95603SChristoph Hellwig 
50983f64091Sbellard     bs = bdrv_new("");
510b6ce07aaSKevin Wolf     ret = bdrv_open_common(bs, filename, flags, drv);
51183f64091Sbellard     if (ret < 0) {
51283f64091Sbellard         bdrv_delete(bs);
51383f64091Sbellard         return ret;
5143b0d4f61Sbellard     }
51571d0770cSaliguori     bs->growable = 1;
51683f64091Sbellard     *pbs = bs;
51783f64091Sbellard     return 0;
5183b0d4f61Sbellard }
5193b0d4f61Sbellard 
520b6ce07aaSKevin Wolf /*
521b6ce07aaSKevin Wolf  * Opens a disk image (raw, qcow2, vmdk, ...)
522b6ce07aaSKevin Wolf  */
523d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
524ea2384d3Sbellard               BlockDriver *drv)
525ea2384d3Sbellard {
526b6ce07aaSKevin Wolf     int ret;
52733e3963eSbellard 
52883f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
529ea2384d3Sbellard         BlockDriverState *bs1;
530ea2384d3Sbellard         int64_t total_size;
5317c96d46eSaliguori         int is_protocol = 0;
53291a073a9SKevin Wolf         BlockDriver *bdrv_qcow2;
53391a073a9SKevin Wolf         QEMUOptionParameter *options;
534b6ce07aaSKevin Wolf         char tmp_filename[PATH_MAX];
535b6ce07aaSKevin Wolf         char backing_filename[PATH_MAX];
53633e3963eSbellard 
537ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
538ea2384d3Sbellard            instead of opening 'filename' directly */
539ea2384d3Sbellard 
540ea2384d3Sbellard         /* if there is a backing file, use it */
541ea2384d3Sbellard         bs1 = bdrv_new("");
542d6e9098eSKevin Wolf         ret = bdrv_open(bs1, filename, 0, drv);
54351d7c00cSaliguori         if (ret < 0) {
544ea2384d3Sbellard             bdrv_delete(bs1);
54551d7c00cSaliguori             return ret;
546ea2384d3Sbellard         }
5473e82990bSJes Sorensen         total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
5487c96d46eSaliguori 
5497c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
5507c96d46eSaliguori             is_protocol = 1;
5517c96d46eSaliguori 
552ea2384d3Sbellard         bdrv_delete(bs1);
553ea2384d3Sbellard 
554ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
5557c96d46eSaliguori 
5567c96d46eSaliguori         /* Real path is meaningless for protocols */
5577c96d46eSaliguori         if (is_protocol)
5587c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
5597c96d46eSaliguori                      "%s", filename);
560114cdfa9SKirill A. Shutemov         else if (!realpath(filename, backing_filename))
561114cdfa9SKirill A. Shutemov             return -errno;
5627c96d46eSaliguori 
56391a073a9SKevin Wolf         bdrv_qcow2 = bdrv_find_format("qcow2");
56491a073a9SKevin Wolf         options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
56591a073a9SKevin Wolf 
5663e82990bSJes Sorensen         set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
56791a073a9SKevin Wolf         set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
56891a073a9SKevin Wolf         if (drv) {
56991a073a9SKevin Wolf             set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
57091a073a9SKevin Wolf                 drv->format_name);
57191a073a9SKevin Wolf         }
57291a073a9SKevin Wolf 
57391a073a9SKevin Wolf         ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
574d748768cSJan Kiszka         free_option_parameters(options);
57551d7c00cSaliguori         if (ret < 0) {
57651d7c00cSaliguori             return ret;
577ea2384d3Sbellard         }
57891a073a9SKevin Wolf 
579ea2384d3Sbellard         filename = tmp_filename;
58091a073a9SKevin Wolf         drv = bdrv_qcow2;
581ea2384d3Sbellard         bs->is_temporary = 1;
582ea2384d3Sbellard     }
583ea2384d3Sbellard 
584b6ce07aaSKevin Wolf     /* Find the right image format driver */
5856db95603SChristoph Hellwig     if (!drv) {
586c98ac35dSStefan Weil         ret = find_image_format(filename, &drv);
587ea2384d3Sbellard     }
5886987307cSChristoph Hellwig 
58951d7c00cSaliguori     if (!drv) {
59051d7c00cSaliguori         goto unlink_and_fail;
59183f64091Sbellard     }
592b6ce07aaSKevin Wolf 
593b6ce07aaSKevin Wolf     /* Open the image */
594b6ce07aaSKevin Wolf     ret = bdrv_open_common(bs, filename, flags, drv);
595b6ce07aaSKevin Wolf     if (ret < 0) {
5966987307cSChristoph Hellwig         goto unlink_and_fail;
5976987307cSChristoph Hellwig     }
5986987307cSChristoph Hellwig 
599b6ce07aaSKevin Wolf     /* If there is a backing file, use it */
600b6ce07aaSKevin Wolf     if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
601b6ce07aaSKevin Wolf         char backing_filename[PATH_MAX];
602b6ce07aaSKevin Wolf         int back_flags;
603b6ce07aaSKevin Wolf         BlockDriver *back_drv = NULL;
604b6ce07aaSKevin Wolf 
605b6ce07aaSKevin Wolf         bs->backing_hd = bdrv_new("");
606b6ce07aaSKevin Wolf         path_combine(backing_filename, sizeof(backing_filename),
607b6ce07aaSKevin Wolf                      filename, bs->backing_file);
608b6ce07aaSKevin Wolf         if (bs->backing_format[0] != '\0')
609b6ce07aaSKevin Wolf             back_drv = bdrv_find_format(bs->backing_format);
610b6ce07aaSKevin Wolf 
611b6ce07aaSKevin Wolf         /* backing files always opened read-only */
612b6ce07aaSKevin Wolf         back_flags =
613b6ce07aaSKevin Wolf             flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
614b6ce07aaSKevin Wolf 
615b6ce07aaSKevin Wolf         ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
616b6ce07aaSKevin Wolf         if (ret < 0) {
617b6ce07aaSKevin Wolf             bdrv_close(bs);
618b6ce07aaSKevin Wolf             return ret;
619b6ce07aaSKevin Wolf         }
620b6ce07aaSKevin Wolf         if (bs->is_temporary) {
621b6ce07aaSKevin Wolf             bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
622b6ce07aaSKevin Wolf         } else {
623b6ce07aaSKevin Wolf             /* base image inherits from "parent" */
624b6ce07aaSKevin Wolf             bs->backing_hd->keep_read_only = bs->keep_read_only;
625b6ce07aaSKevin Wolf         }
626b6ce07aaSKevin Wolf     }
627b6ce07aaSKevin Wolf 
628b6ce07aaSKevin Wolf     if (!bdrv_key_required(bs)) {
629b6ce07aaSKevin Wolf         /* call the change callback */
630b6ce07aaSKevin Wolf         bs->media_changed = 1;
631b6ce07aaSKevin Wolf         if (bs->change_cb)
632b6ce07aaSKevin Wolf             bs->change_cb(bs->change_opaque);
633b6ce07aaSKevin Wolf     }
634b6ce07aaSKevin Wolf 
635b6ce07aaSKevin Wolf     return 0;
636b6ce07aaSKevin Wolf 
637b6ce07aaSKevin Wolf unlink_and_fail:
638b6ce07aaSKevin Wolf     if (bs->is_temporary) {
639b6ce07aaSKevin Wolf         unlink(filename);
640b6ce07aaSKevin Wolf     }
641b6ce07aaSKevin Wolf     return ret;
642b6ce07aaSKevin Wolf }
643b6ce07aaSKevin Wolf 
644fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
645fc01f7e7Sbellard {
64619cb3738Sbellard     if (bs->drv) {
647f9092b10SMarkus Armbruster         if (bs == bs_snapshots) {
648f9092b10SMarkus Armbruster             bs_snapshots = NULL;
649f9092b10SMarkus Armbruster         }
650557df6acSStefan Hajnoczi         if (bs->backing_hd) {
651ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
652557df6acSStefan Hajnoczi             bs->backing_hd = NULL;
653557df6acSStefan Hajnoczi         }
654ea2384d3Sbellard         bs->drv->bdrv_close(bs);
655ea2384d3Sbellard         qemu_free(bs->opaque);
656ea2384d3Sbellard #ifdef _WIN32
657ea2384d3Sbellard         if (bs->is_temporary) {
658ea2384d3Sbellard             unlink(bs->filename);
659ea2384d3Sbellard         }
66067b915a5Sbellard #endif
661ea2384d3Sbellard         bs->opaque = NULL;
662ea2384d3Sbellard         bs->drv = NULL;
663b338082bSbellard 
66466f82ceeSKevin Wolf         if (bs->file != NULL) {
66566f82ceeSKevin Wolf             bdrv_close(bs->file);
66666f82ceeSKevin Wolf         }
66766f82ceeSKevin Wolf 
668b338082bSbellard         /* call the change callback */
66919cb3738Sbellard         bs->media_changed = 1;
670b338082bSbellard         if (bs->change_cb)
671b338082bSbellard             bs->change_cb(bs->change_opaque);
672b338082bSbellard     }
673b338082bSbellard }
674b338082bSbellard 
6752bc93fedSMORITA Kazutaka void bdrv_close_all(void)
6762bc93fedSMORITA Kazutaka {
6772bc93fedSMORITA Kazutaka     BlockDriverState *bs;
6782bc93fedSMORITA Kazutaka 
6792bc93fedSMORITA Kazutaka     QTAILQ_FOREACH(bs, &bdrv_states, list) {
6802bc93fedSMORITA Kazutaka         bdrv_close(bs);
6812bc93fedSMORITA Kazutaka     }
6822bc93fedSMORITA Kazutaka }
6832bc93fedSMORITA Kazutaka 
684b338082bSbellard void bdrv_delete(BlockDriverState *bs)
685b338082bSbellard {
68618846deeSMarkus Armbruster     assert(!bs->peer);
68718846deeSMarkus Armbruster 
6881b7bdbc1SStefan Hajnoczi     /* remove from list, if necessary */
6891b7bdbc1SStefan Hajnoczi     if (bs->device_name[0] != '\0') {
6901b7bdbc1SStefan Hajnoczi         QTAILQ_REMOVE(&bdrv_states, bs, list);
6911b7bdbc1SStefan Hajnoczi     }
69234c6f050Saurel32 
693b338082bSbellard     bdrv_close(bs);
69466f82ceeSKevin Wolf     if (bs->file != NULL) {
69566f82ceeSKevin Wolf         bdrv_delete(bs->file);
69666f82ceeSKevin Wolf     }
69766f82ceeSKevin Wolf 
698f9092b10SMarkus Armbruster     assert(bs != bs_snapshots);
699b338082bSbellard     qemu_free(bs);
700fc01f7e7Sbellard }
701fc01f7e7Sbellard 
70218846deeSMarkus Armbruster int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
70318846deeSMarkus Armbruster {
70418846deeSMarkus Armbruster     if (bs->peer) {
70518846deeSMarkus Armbruster         return -EBUSY;
70618846deeSMarkus Armbruster     }
70718846deeSMarkus Armbruster     bs->peer = qdev;
70818846deeSMarkus Armbruster     return 0;
70918846deeSMarkus Armbruster }
71018846deeSMarkus Armbruster 
71118846deeSMarkus Armbruster void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
71218846deeSMarkus Armbruster {
71318846deeSMarkus Armbruster     assert(bs->peer == qdev);
71418846deeSMarkus Armbruster     bs->peer = NULL;
71518846deeSMarkus Armbruster }
71618846deeSMarkus Armbruster 
71718846deeSMarkus Armbruster DeviceState *bdrv_get_attached(BlockDriverState *bs)
71818846deeSMarkus Armbruster {
71918846deeSMarkus Armbruster     return bs->peer;
72018846deeSMarkus Armbruster }
72118846deeSMarkus Armbruster 
722e97fc193Saliguori /*
723e97fc193Saliguori  * Run consistency checks on an image
724e97fc193Saliguori  *
725e076f338SKevin Wolf  * Returns 0 if the check could be completed (it doesn't mean that the image is
726e076f338SKevin Wolf  * free of errors) or -errno when an internal error occured. The results of the
727e076f338SKevin Wolf  * check are stored in res.
728e97fc193Saliguori  */
729e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
730e97fc193Saliguori {
731e97fc193Saliguori     if (bs->drv->bdrv_check == NULL) {
732e97fc193Saliguori         return -ENOTSUP;
733e97fc193Saliguori     }
734e97fc193Saliguori 
735e076f338SKevin Wolf     memset(res, 0, sizeof(*res));
7369ac228e0SKevin Wolf     return bs->drv->bdrv_check(bs, res);
737e97fc193Saliguori }
738e97fc193Saliguori 
7398a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048
7408a426614SKevin Wolf 
74133e3963eSbellard /* commit COW file into the raw image */
74233e3963eSbellard int bdrv_commit(BlockDriverState *bs)
74333e3963eSbellard {
74419cb3738Sbellard     BlockDriver *drv = bs->drv;
745ee181196SKevin Wolf     BlockDriver *backing_drv;
7468a426614SKevin Wolf     int64_t sector, total_sectors;
7478a426614SKevin Wolf     int n, ro, open_flags;
7484dca4b63SNaphtali Sprei     int ret = 0, rw_ret = 0;
7498a426614SKevin Wolf     uint8_t *buf;
7504dca4b63SNaphtali Sprei     char filename[1024];
7514dca4b63SNaphtali Sprei     BlockDriverState *bs_rw, *bs_ro;
75233e3963eSbellard 
75319cb3738Sbellard     if (!drv)
75419cb3738Sbellard         return -ENOMEDIUM;
75533e3963eSbellard 
7564dca4b63SNaphtali Sprei     if (!bs->backing_hd) {
7574dca4b63SNaphtali Sprei         return -ENOTSUP;
7584dca4b63SNaphtali Sprei     }
7594dca4b63SNaphtali Sprei 
7604dca4b63SNaphtali Sprei     if (bs->backing_hd->keep_read_only) {
761ea2384d3Sbellard         return -EACCES;
76233e3963eSbellard     }
76333e3963eSbellard 
764ee181196SKevin Wolf     backing_drv = bs->backing_hd->drv;
7654dca4b63SNaphtali Sprei     ro = bs->backing_hd->read_only;
7664dca4b63SNaphtali Sprei     strncpy(filename, bs->backing_hd->filename, sizeof(filename));
7674dca4b63SNaphtali Sprei     open_flags =  bs->backing_hd->open_flags;
7684dca4b63SNaphtali Sprei 
7694dca4b63SNaphtali Sprei     if (ro) {
7704dca4b63SNaphtali Sprei         /* re-open as RW */
7714dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
7724dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
7734dca4b63SNaphtali Sprei         bs_rw = bdrv_new("");
774ee181196SKevin Wolf         rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
775ee181196SKevin Wolf             backing_drv);
7764dca4b63SNaphtali Sprei         if (rw_ret < 0) {
7774dca4b63SNaphtali Sprei             bdrv_delete(bs_rw);
7784dca4b63SNaphtali Sprei             /* try to re-open read-only */
7794dca4b63SNaphtali Sprei             bs_ro = bdrv_new("");
780ee181196SKevin Wolf             ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
781ee181196SKevin Wolf                 backing_drv);
7824dca4b63SNaphtali Sprei             if (ret < 0) {
7834dca4b63SNaphtali Sprei                 bdrv_delete(bs_ro);
7844dca4b63SNaphtali Sprei                 /* drive not functional anymore */
7854dca4b63SNaphtali Sprei                 bs->drv = NULL;
7864dca4b63SNaphtali Sprei                 return ret;
7874dca4b63SNaphtali Sprei             }
7884dca4b63SNaphtali Sprei             bs->backing_hd = bs_ro;
7894dca4b63SNaphtali Sprei             return rw_ret;
7904dca4b63SNaphtali Sprei         }
7914dca4b63SNaphtali Sprei         bs->backing_hd = bs_rw;
792ea2384d3Sbellard     }
793ea2384d3Sbellard 
7946ea44308SJan Kiszka     total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
7958a426614SKevin Wolf     buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
7968a426614SKevin Wolf 
7978a426614SKevin Wolf     for (sector = 0; sector < total_sectors; sector += n) {
7988a426614SKevin Wolf         if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
7998a426614SKevin Wolf 
8008a426614SKevin Wolf             if (bdrv_read(bs, sector, buf, n) != 0) {
8014dca4b63SNaphtali Sprei                 ret = -EIO;
8024dca4b63SNaphtali Sprei                 goto ro_cleanup;
80333e3963eSbellard             }
80433e3963eSbellard 
8058a426614SKevin Wolf             if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
8064dca4b63SNaphtali Sprei                 ret = -EIO;
8074dca4b63SNaphtali Sprei                 goto ro_cleanup;
80833e3963eSbellard             }
80933e3963eSbellard         }
81033e3963eSbellard     }
81195389c86Sbellard 
8121d44952fSChristoph Hellwig     if (drv->bdrv_make_empty) {
8131d44952fSChristoph Hellwig         ret = drv->bdrv_make_empty(bs);
8141d44952fSChristoph Hellwig         bdrv_flush(bs);
8151d44952fSChristoph Hellwig     }
81695389c86Sbellard 
8173f5075aeSChristoph Hellwig     /*
8183f5075aeSChristoph Hellwig      * Make sure all data we wrote to the backing device is actually
8193f5075aeSChristoph Hellwig      * stable on disk.
8203f5075aeSChristoph Hellwig      */
8213f5075aeSChristoph Hellwig     if (bs->backing_hd)
8223f5075aeSChristoph Hellwig         bdrv_flush(bs->backing_hd);
8234dca4b63SNaphtali Sprei 
8244dca4b63SNaphtali Sprei ro_cleanup:
8258a426614SKevin Wolf     qemu_free(buf);
8264dca4b63SNaphtali Sprei 
8274dca4b63SNaphtali Sprei     if (ro) {
8284dca4b63SNaphtali Sprei         /* re-open as RO */
8294dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
8304dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
8314dca4b63SNaphtali Sprei         bs_ro = bdrv_new("");
832ee181196SKevin Wolf         ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
833ee181196SKevin Wolf             backing_drv);
8344dca4b63SNaphtali Sprei         if (ret < 0) {
8354dca4b63SNaphtali Sprei             bdrv_delete(bs_ro);
8364dca4b63SNaphtali Sprei             /* drive not functional anymore */
8374dca4b63SNaphtali Sprei             bs->drv = NULL;
8384dca4b63SNaphtali Sprei             return ret;
8394dca4b63SNaphtali Sprei         }
8404dca4b63SNaphtali Sprei         bs->backing_hd = bs_ro;
8414dca4b63SNaphtali Sprei         bs->backing_hd->keep_read_only = 0;
8424dca4b63SNaphtali Sprei     }
8434dca4b63SNaphtali Sprei 
8441d44952fSChristoph Hellwig     return ret;
84533e3963eSbellard }
84633e3963eSbellard 
8476ab4b5abSMarkus Armbruster void bdrv_commit_all(void)
8486ab4b5abSMarkus Armbruster {
8496ab4b5abSMarkus Armbruster     BlockDriverState *bs;
8506ab4b5abSMarkus Armbruster 
8516ab4b5abSMarkus Armbruster     QTAILQ_FOREACH(bs, &bdrv_states, list) {
8526ab4b5abSMarkus Armbruster         bdrv_commit(bs);
8536ab4b5abSMarkus Armbruster     }
8546ab4b5abSMarkus Armbruster }
8556ab4b5abSMarkus Armbruster 
856756e6736SKevin Wolf /*
857756e6736SKevin Wolf  * Return values:
858756e6736SKevin Wolf  * 0        - success
859756e6736SKevin Wolf  * -EINVAL  - backing format specified, but no file
860756e6736SKevin Wolf  * -ENOSPC  - can't update the backing file because no space is left in the
861756e6736SKevin Wolf  *            image file header
862756e6736SKevin Wolf  * -ENOTSUP - format driver doesn't support changing the backing file
863756e6736SKevin Wolf  */
864756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs,
865756e6736SKevin Wolf     const char *backing_file, const char *backing_fmt)
866756e6736SKevin Wolf {
867756e6736SKevin Wolf     BlockDriver *drv = bs->drv;
868756e6736SKevin Wolf 
869756e6736SKevin Wolf     if (drv->bdrv_change_backing_file != NULL) {
870756e6736SKevin Wolf         return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
871756e6736SKevin Wolf     } else {
872756e6736SKevin Wolf         return -ENOTSUP;
873756e6736SKevin Wolf     }
874756e6736SKevin Wolf }
875756e6736SKevin Wolf 
87671d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
87771d0770cSaliguori                                    size_t size)
87871d0770cSaliguori {
87971d0770cSaliguori     int64_t len;
88071d0770cSaliguori 
88171d0770cSaliguori     if (!bdrv_is_inserted(bs))
88271d0770cSaliguori         return -ENOMEDIUM;
88371d0770cSaliguori 
88471d0770cSaliguori     if (bs->growable)
88571d0770cSaliguori         return 0;
88671d0770cSaliguori 
88771d0770cSaliguori     len = bdrv_getlength(bs);
88871d0770cSaliguori 
889fbb7b4e0SKevin Wolf     if (offset < 0)
890fbb7b4e0SKevin Wolf         return -EIO;
891fbb7b4e0SKevin Wolf 
892fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
89371d0770cSaliguori         return -EIO;
89471d0770cSaliguori 
89571d0770cSaliguori     return 0;
89671d0770cSaliguori }
89771d0770cSaliguori 
89871d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
89971d0770cSaliguori                               int nb_sectors)
90071d0770cSaliguori {
901eb5a3165SJes Sorensen     return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
902eb5a3165SJes Sorensen                                    nb_sectors * BDRV_SECTOR_SIZE);
90371d0770cSaliguori }
90471d0770cSaliguori 
90519cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
906fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
907fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
908fc01f7e7Sbellard {
909ea2384d3Sbellard     BlockDriver *drv = bs->drv;
910fc01f7e7Sbellard 
91119cb3738Sbellard     if (!drv)
91219cb3738Sbellard         return -ENOMEDIUM;
91371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
91471d0770cSaliguori         return -EIO;
915b338082bSbellard 
91683f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
91783f64091Sbellard }
918fc01f7e7Sbellard 
9197cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
9207cd1e32aSlirans@il.ibm.com                              int nb_sectors, int dirty)
9217cd1e32aSlirans@il.ibm.com {
9227cd1e32aSlirans@il.ibm.com     int64_t start, end;
923c6d22830SJan Kiszka     unsigned long val, idx, bit;
924a55eb92cSJan Kiszka 
9256ea44308SJan Kiszka     start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
926c6d22830SJan Kiszka     end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
9277cd1e32aSlirans@il.ibm.com 
9287cd1e32aSlirans@il.ibm.com     for (; start <= end; start++) {
929c6d22830SJan Kiszka         idx = start / (sizeof(unsigned long) * 8);
930c6d22830SJan Kiszka         bit = start % (sizeof(unsigned long) * 8);
931c6d22830SJan Kiszka         val = bs->dirty_bitmap[idx];
932c6d22830SJan Kiszka         if (dirty) {
9336d59fec1SMarcelo Tosatti             if (!(val & (1UL << bit))) {
934aaa0eb75SLiran Schour                 bs->dirty_count++;
9356d59fec1SMarcelo Tosatti                 val |= 1UL << bit;
936aaa0eb75SLiran Schour             }
937c6d22830SJan Kiszka         } else {
9386d59fec1SMarcelo Tosatti             if (val & (1UL << bit)) {
939aaa0eb75SLiran Schour                 bs->dirty_count--;
9406d59fec1SMarcelo Tosatti                 val &= ~(1UL << bit);
941c6d22830SJan Kiszka             }
942aaa0eb75SLiran Schour         }
943c6d22830SJan Kiszka         bs->dirty_bitmap[idx] = val;
9447cd1e32aSlirans@il.ibm.com     }
9457cd1e32aSlirans@il.ibm.com }
9467cd1e32aSlirans@il.ibm.com 
94719cb3738Sbellard /* Return < 0 if error. Important errors are:
94819cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
94919cb3738Sbellard   -ENOMEDIUM   No media inserted.
95019cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
95119cb3738Sbellard   -EACCES      Trying to write a read-only device
95219cb3738Sbellard */
953fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
954fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
955fc01f7e7Sbellard {
95683f64091Sbellard     BlockDriver *drv = bs->drv;
95719cb3738Sbellard     if (!bs->drv)
95819cb3738Sbellard         return -ENOMEDIUM;
9590849bf08Sbellard     if (bs->read_only)
96019cb3738Sbellard         return -EACCES;
96171d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
96271d0770cSaliguori         return -EIO;
96371d0770cSaliguori 
964c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
9657cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
9667cd1e32aSlirans@il.ibm.com     }
9677cd1e32aSlirans@il.ibm.com 
968294cc35fSKevin Wolf     if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
969294cc35fSKevin Wolf         bs->wr_highest_sector = sector_num + nb_sectors - 1;
970294cc35fSKevin Wolf     }
971294cc35fSKevin Wolf 
97283f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
97383f64091Sbellard }
97483f64091Sbellard 
975eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
976eda578e5Saliguori                void *buf, int count1)
97783f64091Sbellard {
9786ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
97983f64091Sbellard     int len, nb_sectors, count;
98083f64091Sbellard     int64_t sector_num;
9819a8c4cceSKevin Wolf     int ret;
98283f64091Sbellard 
98383f64091Sbellard     count = count1;
98483f64091Sbellard     /* first read to align to sector start */
9856ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
98683f64091Sbellard     if (len > count)
98783f64091Sbellard         len = count;
9886ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
98983f64091Sbellard     if (len > 0) {
9909a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
9919a8c4cceSKevin Wolf             return ret;
9926ea44308SJan Kiszka         memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
99383f64091Sbellard         count -= len;
99483f64091Sbellard         if (count == 0)
99583f64091Sbellard             return count1;
99683f64091Sbellard         sector_num++;
99783f64091Sbellard         buf += len;
99883f64091Sbellard     }
99983f64091Sbellard 
100083f64091Sbellard     /* read the sectors "in place" */
10016ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
100283f64091Sbellard     if (nb_sectors > 0) {
10039a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
10049a8c4cceSKevin Wolf             return ret;
100583f64091Sbellard         sector_num += nb_sectors;
10066ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
100783f64091Sbellard         buf += len;
100883f64091Sbellard         count -= len;
100983f64091Sbellard     }
101083f64091Sbellard 
101183f64091Sbellard     /* add data from the last sector */
101283f64091Sbellard     if (count > 0) {
10139a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10149a8c4cceSKevin Wolf             return ret;
101583f64091Sbellard         memcpy(buf, tmp_buf, count);
101683f64091Sbellard     }
101783f64091Sbellard     return count1;
101883f64091Sbellard }
101983f64091Sbellard 
1020eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1021eda578e5Saliguori                 const void *buf, int count1)
102283f64091Sbellard {
10236ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
102483f64091Sbellard     int len, nb_sectors, count;
102583f64091Sbellard     int64_t sector_num;
10269a8c4cceSKevin Wolf     int ret;
102783f64091Sbellard 
102883f64091Sbellard     count = count1;
102983f64091Sbellard     /* first write to align to sector start */
10306ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
103183f64091Sbellard     if (len > count)
103283f64091Sbellard         len = count;
10336ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
103483f64091Sbellard     if (len > 0) {
10359a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10369a8c4cceSKevin Wolf             return ret;
10376ea44308SJan Kiszka         memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
10389a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10399a8c4cceSKevin Wolf             return ret;
104083f64091Sbellard         count -= len;
104183f64091Sbellard         if (count == 0)
104283f64091Sbellard             return count1;
104383f64091Sbellard         sector_num++;
104483f64091Sbellard         buf += len;
104583f64091Sbellard     }
104683f64091Sbellard 
104783f64091Sbellard     /* write the sectors "in place" */
10486ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
104983f64091Sbellard     if (nb_sectors > 0) {
10509a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
10519a8c4cceSKevin Wolf             return ret;
105283f64091Sbellard         sector_num += nb_sectors;
10536ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
105483f64091Sbellard         buf += len;
105583f64091Sbellard         count -= len;
105683f64091Sbellard     }
105783f64091Sbellard 
105883f64091Sbellard     /* add data from the last sector */
105983f64091Sbellard     if (count > 0) {
10609a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10619a8c4cceSKevin Wolf             return ret;
106283f64091Sbellard         memcpy(tmp_buf, buf, count);
10639a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10649a8c4cceSKevin Wolf             return ret;
106583f64091Sbellard     }
106683f64091Sbellard     return count1;
106783f64091Sbellard }
106883f64091Sbellard 
1069f08145feSKevin Wolf /*
1070f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1071f08145feSKevin Wolf  * request (acts as a barrier)
1072f08145feSKevin Wolf  *
1073f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1074f08145feSKevin Wolf  */
1075f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1076f08145feSKevin Wolf     const void *buf, int count)
1077f08145feSKevin Wolf {
1078f08145feSKevin Wolf     int ret;
1079f08145feSKevin Wolf 
1080f08145feSKevin Wolf     ret = bdrv_pwrite(bs, offset, buf, count);
1081f08145feSKevin Wolf     if (ret < 0) {
1082f08145feSKevin Wolf         return ret;
1083f08145feSKevin Wolf     }
1084f08145feSKevin Wolf 
1085f08145feSKevin Wolf     /* No flush needed for cache=writethrough, it uses O_DSYNC */
1086f08145feSKevin Wolf     if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1087f08145feSKevin Wolf         bdrv_flush(bs);
1088f08145feSKevin Wolf     }
1089f08145feSKevin Wolf 
1090f08145feSKevin Wolf     return 0;
1091f08145feSKevin Wolf }
1092f08145feSKevin Wolf 
1093f08145feSKevin Wolf /*
1094f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1095f08145feSKevin Wolf  * request (acts as a barrier)
1096f08145feSKevin Wolf  *
1097f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1098f08145feSKevin Wolf  */
1099f08145feSKevin Wolf int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1100f08145feSKevin Wolf     const uint8_t *buf, int nb_sectors)
1101f08145feSKevin Wolf {
1102f08145feSKevin Wolf     return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1103f08145feSKevin Wolf         buf, BDRV_SECTOR_SIZE * nb_sectors);
1104f08145feSKevin Wolf }
1105f08145feSKevin Wolf 
110683f64091Sbellard /**
110783f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
110883f64091Sbellard  */
110983f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
111083f64091Sbellard {
111183f64091Sbellard     BlockDriver *drv = bs->drv;
111251762288SStefan Hajnoczi     int ret;
111383f64091Sbellard     if (!drv)
111419cb3738Sbellard         return -ENOMEDIUM;
111583f64091Sbellard     if (!drv->bdrv_truncate)
111683f64091Sbellard         return -ENOTSUP;
111759f2689dSNaphtali Sprei     if (bs->read_only)
111859f2689dSNaphtali Sprei         return -EACCES;
111951762288SStefan Hajnoczi     ret = drv->bdrv_truncate(bs, offset);
112051762288SStefan Hajnoczi     if (ret == 0) {
112151762288SStefan Hajnoczi         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
112251762288SStefan Hajnoczi     }
112351762288SStefan Hajnoczi     return ret;
112483f64091Sbellard }
112583f64091Sbellard 
112683f64091Sbellard /**
112783f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
112883f64091Sbellard  */
112983f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
113083f64091Sbellard {
113183f64091Sbellard     BlockDriver *drv = bs->drv;
113283f64091Sbellard     if (!drv)
113319cb3738Sbellard         return -ENOMEDIUM;
113451762288SStefan Hajnoczi 
113551762288SStefan Hajnoczi     /* Fixed size devices use the total_sectors value for speed instead of
113651762288SStefan Hajnoczi        issuing a length query (like lseek) on each call.  Also, legacy block
113751762288SStefan Hajnoczi        drivers don't provide a bdrv_getlength function and must use
113851762288SStefan Hajnoczi        total_sectors. */
113951762288SStefan Hajnoczi     if (!bs->growable || !drv->bdrv_getlength) {
11406ea44308SJan Kiszka         return bs->total_sectors * BDRV_SECTOR_SIZE;
114183f64091Sbellard     }
114283f64091Sbellard     return drv->bdrv_getlength(bs);
1143fc01f7e7Sbellard }
1144fc01f7e7Sbellard 
114519cb3738Sbellard /* return 0 as number of sectors if no device present or error */
114696b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1147fc01f7e7Sbellard {
114819cb3738Sbellard     int64_t length;
114919cb3738Sbellard     length = bdrv_getlength(bs);
115019cb3738Sbellard     if (length < 0)
115119cb3738Sbellard         length = 0;
115219cb3738Sbellard     else
11536ea44308SJan Kiszka         length = length >> BDRV_SECTOR_BITS;
115419cb3738Sbellard     *nb_sectors_ptr = length;
1155fc01f7e7Sbellard }
1156cf98951bSbellard 
1157f3d54fc4Saliguori struct partition {
1158f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
1159f3d54fc4Saliguori         uint8_t head;               /* starting head */
1160f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
1161f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
1162f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
1163f3d54fc4Saliguori         uint8_t end_head;           /* end head */
1164f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
1165f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
1166f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
1167f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
1168f3d54fc4Saliguori } __attribute__((packed));
1169f3d54fc4Saliguori 
1170f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1171f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
1172f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
1173f3d54fc4Saliguori {
1174eb5a3165SJes Sorensen     uint8_t buf[BDRV_SECTOR_SIZE];
1175f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
1176f3d54fc4Saliguori     struct partition *p;
1177f3d54fc4Saliguori     uint32_t nr_sects;
1178a38131b6Sblueswir1     uint64_t nb_sectors;
1179f3d54fc4Saliguori 
1180f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1181f3d54fc4Saliguori 
1182f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
1183f3d54fc4Saliguori     if (ret < 0)
1184f3d54fc4Saliguori         return -1;
1185f3d54fc4Saliguori     /* test msdos magic */
1186f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
1187f3d54fc4Saliguori         return -1;
1188f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
1189f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
1190f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
1191f3d54fc4Saliguori         if (nr_sects && p->end_head) {
1192f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
1193f3d54fc4Saliguori                a cylinder boundary */
1194f3d54fc4Saliguori             heads = p->end_head + 1;
1195f3d54fc4Saliguori             sectors = p->end_sector & 63;
1196f3d54fc4Saliguori             if (sectors == 0)
1197f3d54fc4Saliguori                 continue;
1198f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
1199f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
1200f3d54fc4Saliguori                 continue;
1201f3d54fc4Saliguori             *pheads = heads;
1202f3d54fc4Saliguori             *psectors = sectors;
1203f3d54fc4Saliguori             *pcylinders = cylinders;
1204f3d54fc4Saliguori #if 0
1205f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
1206f3d54fc4Saliguori                    cylinders, heads, sectors);
1207f3d54fc4Saliguori #endif
1208f3d54fc4Saliguori             return 0;
1209f3d54fc4Saliguori         }
1210f3d54fc4Saliguori     }
1211f3d54fc4Saliguori     return -1;
1212f3d54fc4Saliguori }
1213f3d54fc4Saliguori 
1214f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1215f3d54fc4Saliguori {
1216f3d54fc4Saliguori     int translation, lba_detected = 0;
1217f3d54fc4Saliguori     int cylinders, heads, secs;
1218a38131b6Sblueswir1     uint64_t nb_sectors;
1219f3d54fc4Saliguori 
1220f3d54fc4Saliguori     /* if a geometry hint is available, use it */
1221f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1222f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1223f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
1224f3d54fc4Saliguori     if (cylinders != 0) {
1225f3d54fc4Saliguori         *pcyls = cylinders;
1226f3d54fc4Saliguori         *pheads = heads;
1227f3d54fc4Saliguori         *psecs = secs;
1228f3d54fc4Saliguori     } else {
1229f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1230f3d54fc4Saliguori             if (heads > 16) {
1231f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
1232f3d54fc4Saliguori                    translation was active, so the default
1233f3d54fc4Saliguori                    hardware geometry is OK */
1234f3d54fc4Saliguori                 lba_detected = 1;
1235f3d54fc4Saliguori                 goto default_geometry;
1236f3d54fc4Saliguori             } else {
1237f3d54fc4Saliguori                 *pcyls = cylinders;
1238f3d54fc4Saliguori                 *pheads = heads;
1239f3d54fc4Saliguori                 *psecs = secs;
1240f3d54fc4Saliguori                 /* disable any translation to be in sync with
1241f3d54fc4Saliguori                    the logical geometry */
1242f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1243f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1244f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
1245f3d54fc4Saliguori                 }
1246f3d54fc4Saliguori             }
1247f3d54fc4Saliguori         } else {
1248f3d54fc4Saliguori         default_geometry:
1249f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
1250f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
1251f3d54fc4Saliguori 
1252f3d54fc4Saliguori             if (cylinders > 16383)
1253f3d54fc4Saliguori                 cylinders = 16383;
1254f3d54fc4Saliguori             else if (cylinders < 2)
1255f3d54fc4Saliguori                 cylinders = 2;
1256f3d54fc4Saliguori             *pcyls = cylinders;
1257f3d54fc4Saliguori             *pheads = 16;
1258f3d54fc4Saliguori             *psecs = 63;
1259f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1260f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
1261f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1262f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
1263f3d54fc4Saliguori                 } else {
1264f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1265f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
1266f3d54fc4Saliguori                 }
1267f3d54fc4Saliguori             }
1268f3d54fc4Saliguori         }
1269f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1270f3d54fc4Saliguori     }
1271f3d54fc4Saliguori }
1272f3d54fc4Saliguori 
1273b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
1274b338082bSbellard                             int cyls, int heads, int secs)
1275b338082bSbellard {
1276b338082bSbellard     bs->cyls = cyls;
1277b338082bSbellard     bs->heads = heads;
1278b338082bSbellard     bs->secs = secs;
1279b338082bSbellard }
1280b338082bSbellard 
1281b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
1282b338082bSbellard {
1283b338082bSbellard     bs->type = type;
1284b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
1285b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
1286b338082bSbellard }
1287b338082bSbellard 
128846d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
128946d4767dSbellard {
129046d4767dSbellard     bs->translation = translation;
129146d4767dSbellard }
129246d4767dSbellard 
1293b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
1294b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
1295b338082bSbellard {
1296b338082bSbellard     *pcyls = bs->cyls;
1297b338082bSbellard     *pheads = bs->heads;
1298b338082bSbellard     *psecs = bs->secs;
1299b338082bSbellard }
1300b338082bSbellard 
1301b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
1302b338082bSbellard {
1303b338082bSbellard     return bs->type;
1304b338082bSbellard }
1305b338082bSbellard 
130646d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
130746d4767dSbellard {
130846d4767dSbellard     return bs->translation;
130946d4767dSbellard }
131046d4767dSbellard 
1311abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1312abd7f68dSMarkus Armbruster                        BlockErrorAction on_write_error)
1313abd7f68dSMarkus Armbruster {
1314abd7f68dSMarkus Armbruster     bs->on_read_error = on_read_error;
1315abd7f68dSMarkus Armbruster     bs->on_write_error = on_write_error;
1316abd7f68dSMarkus Armbruster }
1317abd7f68dSMarkus Armbruster 
1318abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1319abd7f68dSMarkus Armbruster {
1320abd7f68dSMarkus Armbruster     return is_read ? bs->on_read_error : bs->on_write_error;
1321abd7f68dSMarkus Armbruster }
1322abd7f68dSMarkus Armbruster 
13237d0d6950SMarkus Armbruster void bdrv_set_removable(BlockDriverState *bs, int removable)
13247d0d6950SMarkus Armbruster {
13257d0d6950SMarkus Armbruster     bs->removable = removable;
13267d0d6950SMarkus Armbruster     if (removable && bs == bs_snapshots) {
13277d0d6950SMarkus Armbruster         bs_snapshots = NULL;
13287d0d6950SMarkus Armbruster     }
13297d0d6950SMarkus Armbruster }
13307d0d6950SMarkus Armbruster 
1331b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
1332b338082bSbellard {
1333b338082bSbellard     return bs->removable;
1334b338082bSbellard }
1335b338082bSbellard 
1336b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
1337b338082bSbellard {
1338b338082bSbellard     return bs->read_only;
1339b338082bSbellard }
1340b338082bSbellard 
1341985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
1342985a03b0Sths {
1343985a03b0Sths     return bs->sg;
1344985a03b0Sths }
1345985a03b0Sths 
1346e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs)
1347e900a7b7SChristoph Hellwig {
1348e900a7b7SChristoph Hellwig     return bs->enable_write_cache;
1349e900a7b7SChristoph Hellwig }
1350e900a7b7SChristoph Hellwig 
135119cb3738Sbellard /* XXX: no longer used */
1352b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
1353b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
1354b338082bSbellard {
1355b338082bSbellard     bs->change_cb = change_cb;
1356b338082bSbellard     bs->change_opaque = opaque;
1357b338082bSbellard }
1358b338082bSbellard 
1359ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
1360ea2384d3Sbellard {
1361ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
1362ea2384d3Sbellard         return 1;
1363ea2384d3Sbellard     return bs->encrypted;
1364ea2384d3Sbellard }
1365ea2384d3Sbellard 
1366c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
1367c0f4ce77Saliguori {
1368c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
1369c0f4ce77Saliguori 
1370c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1371c0f4ce77Saliguori         return 1;
1372c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
1373c0f4ce77Saliguori }
1374c0f4ce77Saliguori 
1375ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
1376ea2384d3Sbellard {
1377ea2384d3Sbellard     int ret;
1378ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
1379ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
1380ea2384d3Sbellard         if (ret < 0)
1381ea2384d3Sbellard             return ret;
1382ea2384d3Sbellard         if (!bs->encrypted)
1383ea2384d3Sbellard             return 0;
1384ea2384d3Sbellard     }
1385fd04a2aeSShahar Havivi     if (!bs->encrypted) {
1386fd04a2aeSShahar Havivi         return -EINVAL;
1387fd04a2aeSShahar Havivi     } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1388fd04a2aeSShahar Havivi         return -ENOMEDIUM;
1389fd04a2aeSShahar Havivi     }
1390c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
1391bb5fc20fSaliguori     if (ret < 0) {
1392bb5fc20fSaliguori         bs->valid_key = 0;
1393bb5fc20fSaliguori     } else if (!bs->valid_key) {
1394bb5fc20fSaliguori         bs->valid_key = 1;
1395bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
1396bb5fc20fSaliguori         bs->media_changed = 1;
1397bb5fc20fSaliguori         if (bs->change_cb)
1398bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
1399bb5fc20fSaliguori     }
1400c0f4ce77Saliguori     return ret;
1401ea2384d3Sbellard }
1402ea2384d3Sbellard 
1403ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1404ea2384d3Sbellard {
140519cb3738Sbellard     if (!bs->drv) {
1406ea2384d3Sbellard         buf[0] = '\0';
1407ea2384d3Sbellard     } else {
1408ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
1409ea2384d3Sbellard     }
1410ea2384d3Sbellard }
1411ea2384d3Sbellard 
1412ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1413ea2384d3Sbellard                          void *opaque)
1414ea2384d3Sbellard {
1415ea2384d3Sbellard     BlockDriver *drv;
1416ea2384d3Sbellard 
14178a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv, &bdrv_drivers, list) {
1418ea2384d3Sbellard         it(opaque, drv->format_name);
1419ea2384d3Sbellard     }
1420ea2384d3Sbellard }
1421ea2384d3Sbellard 
1422b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1423b338082bSbellard {
1424b338082bSbellard     BlockDriverState *bs;
1425b338082bSbellard 
14261b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
14271b7bdbc1SStefan Hajnoczi         if (!strcmp(name, bs->device_name)) {
1428b338082bSbellard             return bs;
1429b338082bSbellard         }
14301b7bdbc1SStefan Hajnoczi     }
1431b338082bSbellard     return NULL;
1432b338082bSbellard }
1433b338082bSbellard 
14342f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs)
14352f399b0aSMarkus Armbruster {
14362f399b0aSMarkus Armbruster     if (!bs) {
14372f399b0aSMarkus Armbruster         return QTAILQ_FIRST(&bdrv_states);
14382f399b0aSMarkus Armbruster     }
14392f399b0aSMarkus Armbruster     return QTAILQ_NEXT(bs, list);
14402f399b0aSMarkus Armbruster }
14412f399b0aSMarkus Armbruster 
144251de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
144381d0912dSbellard {
144481d0912dSbellard     BlockDriverState *bs;
144581d0912dSbellard 
14461b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
144751de9760Saliguori         it(opaque, bs);
144881d0912dSbellard     }
144981d0912dSbellard }
145081d0912dSbellard 
1451ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1452ea2384d3Sbellard {
1453ea2384d3Sbellard     return bs->device_name;
1454ea2384d3Sbellard }
1455ea2384d3Sbellard 
1456205ef796SKevin Wolf int bdrv_flush(BlockDriverState *bs)
14577a6cba61Spbrook {
1458016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
1459205ef796SKevin Wolf         return 0;
1460016f5cf6SAlexander Graf     }
1461016f5cf6SAlexander Graf 
1462205ef796SKevin Wolf     if (bs->drv && bs->drv->bdrv_flush) {
1463205ef796SKevin Wolf         return bs->drv->bdrv_flush(bs);
1464205ef796SKevin Wolf     }
1465205ef796SKevin Wolf 
1466205ef796SKevin Wolf     /*
1467205ef796SKevin Wolf      * Some block drivers always operate in either writethrough or unsafe mode
1468205ef796SKevin Wolf      * and don't support bdrv_flush therefore. Usually qemu doesn't know how
1469205ef796SKevin Wolf      * the server works (because the behaviour is hardcoded or depends on
1470205ef796SKevin Wolf      * server-side configuration), so we can't ensure that everything is safe
1471205ef796SKevin Wolf      * on disk. Returning an error doesn't work because that would break guests
1472205ef796SKevin Wolf      * even if the server operates in writethrough mode.
1473205ef796SKevin Wolf      *
1474205ef796SKevin Wolf      * Let's hope the user knows what he's doing.
1475205ef796SKevin Wolf      */
1476205ef796SKevin Wolf     return 0;
14777a6cba61Spbrook }
14787a6cba61Spbrook 
1479c6ca28d6Saliguori void bdrv_flush_all(void)
1480c6ca28d6Saliguori {
1481c6ca28d6Saliguori     BlockDriverState *bs;
1482c6ca28d6Saliguori 
14831b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1484c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
14851b7bdbc1SStefan Hajnoczi             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1486c6ca28d6Saliguori             bdrv_flush(bs);
1487c6ca28d6Saliguori         }
14881b7bdbc1SStefan Hajnoczi     }
14891b7bdbc1SStefan Hajnoczi }
1490c6ca28d6Saliguori 
1491f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs)
1492f2feebbdSKevin Wolf {
1493f2feebbdSKevin Wolf     assert(bs->drv);
1494f2feebbdSKevin Wolf 
1495336c1c12SKevin Wolf     if (bs->drv->bdrv_has_zero_init) {
1496336c1c12SKevin Wolf         return bs->drv->bdrv_has_zero_init(bs);
1497f2feebbdSKevin Wolf     }
1498f2feebbdSKevin Wolf 
1499f2feebbdSKevin Wolf     return 1;
1500f2feebbdSKevin Wolf }
1501f2feebbdSKevin Wolf 
1502f58c7b35Sths /*
1503f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1504f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1505f58c7b35Sths  * hence all their sectors are reported as allocated.
1506f58c7b35Sths  *
1507f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1508f58c7b35Sths  * the specified sector) that are known to be in the same
1509f58c7b35Sths  * allocated/unallocated state.
1510f58c7b35Sths  *
1511f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1512f58c7b35Sths  */
1513f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1514f58c7b35Sths 	int *pnum)
1515f58c7b35Sths {
1516f58c7b35Sths     int64_t n;
1517f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1518f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1519f58c7b35Sths             *pnum = 0;
1520f58c7b35Sths             return 0;
1521f58c7b35Sths         }
1522f58c7b35Sths         n = bs->total_sectors - sector_num;
1523f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1524f58c7b35Sths         return 1;
1525f58c7b35Sths     }
1526f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1527f58c7b35Sths }
1528f58c7b35Sths 
15292582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv,
15302582bfedSLuiz Capitulino                     BlockMonEventAction action, int is_read)
15312582bfedSLuiz Capitulino {
15322582bfedSLuiz Capitulino     QObject *data;
15332582bfedSLuiz Capitulino     const char *action_str;
15342582bfedSLuiz Capitulino 
15352582bfedSLuiz Capitulino     switch (action) {
15362582bfedSLuiz Capitulino     case BDRV_ACTION_REPORT:
15372582bfedSLuiz Capitulino         action_str = "report";
15382582bfedSLuiz Capitulino         break;
15392582bfedSLuiz Capitulino     case BDRV_ACTION_IGNORE:
15402582bfedSLuiz Capitulino         action_str = "ignore";
15412582bfedSLuiz Capitulino         break;
15422582bfedSLuiz Capitulino     case BDRV_ACTION_STOP:
15432582bfedSLuiz Capitulino         action_str = "stop";
15442582bfedSLuiz Capitulino         break;
15452582bfedSLuiz Capitulino     default:
15462582bfedSLuiz Capitulino         abort();
15472582bfedSLuiz Capitulino     }
15482582bfedSLuiz Capitulino 
15492582bfedSLuiz Capitulino     data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
15502582bfedSLuiz Capitulino                               bdrv->device_name,
15512582bfedSLuiz Capitulino                               action_str,
15522582bfedSLuiz Capitulino                               is_read ? "read" : "write");
15532582bfedSLuiz Capitulino     monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
15542582bfedSLuiz Capitulino 
15552582bfedSLuiz Capitulino     qobject_decref(data);
15562582bfedSLuiz Capitulino }
15572582bfedSLuiz Capitulino 
1558d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque)
1559b338082bSbellard {
1560d15e5465SLuiz Capitulino     QDict *bs_dict;
1561d15e5465SLuiz Capitulino     Monitor *mon = opaque;
1562b338082bSbellard 
1563d15e5465SLuiz Capitulino     bs_dict = qobject_to_qdict(obj);
1564d15e5465SLuiz Capitulino 
1565d15e5465SLuiz Capitulino     monitor_printf(mon, "%s: type=%s removable=%d",
1566d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "device"),
1567d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "type"),
1568d15e5465SLuiz Capitulino                         qdict_get_bool(bs_dict, "removable"));
1569d15e5465SLuiz Capitulino 
1570d15e5465SLuiz Capitulino     if (qdict_get_bool(bs_dict, "removable")) {
1571d15e5465SLuiz Capitulino         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1572b338082bSbellard     }
1573d15e5465SLuiz Capitulino 
1574d15e5465SLuiz Capitulino     if (qdict_haskey(bs_dict, "inserted")) {
1575d15e5465SLuiz Capitulino         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1576d15e5465SLuiz Capitulino 
1577376253ecSaliguori         monitor_printf(mon, " file=");
1578d15e5465SLuiz Capitulino         monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1579d15e5465SLuiz Capitulino         if (qdict_haskey(qdict, "backing_file")) {
1580376253ecSaliguori             monitor_printf(mon, " backing_file=");
1581d15e5465SLuiz Capitulino             monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1582fef30743Sths         }
1583d15e5465SLuiz Capitulino         monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1584d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "ro"),
1585d15e5465SLuiz Capitulino                             qdict_get_str(qdict, "drv"),
1586d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "encrypted"));
1587b338082bSbellard     } else {
1588376253ecSaliguori         monitor_printf(mon, " [not inserted]");
1589b338082bSbellard     }
1590d15e5465SLuiz Capitulino 
1591376253ecSaliguori     monitor_printf(mon, "\n");
1592b338082bSbellard }
1593d15e5465SLuiz Capitulino 
1594d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data)
1595d15e5465SLuiz Capitulino {
1596d15e5465SLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1597d15e5465SLuiz Capitulino }
1598d15e5465SLuiz Capitulino 
1599d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data)
1600d15e5465SLuiz Capitulino {
1601d15e5465SLuiz Capitulino     QList *bs_list;
1602d15e5465SLuiz Capitulino     BlockDriverState *bs;
1603d15e5465SLuiz Capitulino 
1604d15e5465SLuiz Capitulino     bs_list = qlist_new();
1605d15e5465SLuiz Capitulino 
16061b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1607d15e5465SLuiz Capitulino         QObject *bs_obj;
1608d15e5465SLuiz Capitulino         const char *type = "unknown";
1609d15e5465SLuiz Capitulino 
1610d15e5465SLuiz Capitulino         switch(bs->type) {
1611d15e5465SLuiz Capitulino         case BDRV_TYPE_HD:
1612d15e5465SLuiz Capitulino             type = "hd";
1613d15e5465SLuiz Capitulino             break;
1614d15e5465SLuiz Capitulino         case BDRV_TYPE_CDROM:
1615d15e5465SLuiz Capitulino             type = "cdrom";
1616d15e5465SLuiz Capitulino             break;
1617d15e5465SLuiz Capitulino         case BDRV_TYPE_FLOPPY:
1618d15e5465SLuiz Capitulino             type = "floppy";
1619d15e5465SLuiz Capitulino             break;
1620d15e5465SLuiz Capitulino         }
1621d15e5465SLuiz Capitulino 
1622d15e5465SLuiz Capitulino         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1623d15e5465SLuiz Capitulino                                     "'removable': %i, 'locked': %i }",
1624d15e5465SLuiz Capitulino                                     bs->device_name, type, bs->removable,
1625d15e5465SLuiz Capitulino                                     bs->locked);
1626d15e5465SLuiz Capitulino 
1627d15e5465SLuiz Capitulino         if (bs->drv) {
1628d15e5465SLuiz Capitulino             QObject *obj;
1629d15e5465SLuiz Capitulino             QDict *bs_dict = qobject_to_qdict(bs_obj);
1630d15e5465SLuiz Capitulino 
1631d15e5465SLuiz Capitulino             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1632d15e5465SLuiz Capitulino                                      "'encrypted': %i }",
1633d15e5465SLuiz Capitulino                                      bs->filename, bs->read_only,
1634d15e5465SLuiz Capitulino                                      bs->drv->format_name,
1635d15e5465SLuiz Capitulino                                      bdrv_is_encrypted(bs));
1636d15e5465SLuiz Capitulino             if (bs->backing_file[0] != '\0') {
1637d15e5465SLuiz Capitulino                 QDict *qdict = qobject_to_qdict(obj);
1638d15e5465SLuiz Capitulino                 qdict_put(qdict, "backing_file",
1639d15e5465SLuiz Capitulino                           qstring_from_str(bs->backing_file));
1640d15e5465SLuiz Capitulino             }
1641d15e5465SLuiz Capitulino 
1642d15e5465SLuiz Capitulino             qdict_put_obj(bs_dict, "inserted", obj);
1643d15e5465SLuiz Capitulino         }
1644d15e5465SLuiz Capitulino         qlist_append_obj(bs_list, bs_obj);
1645d15e5465SLuiz Capitulino     }
1646d15e5465SLuiz Capitulino 
1647d15e5465SLuiz Capitulino     *ret_data = QOBJECT(bs_list);
1648b338082bSbellard }
1649a36e69ddSths 
1650218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque)
1651a36e69ddSths {
1652218a536aSLuiz Capitulino     QDict *qdict;
1653218a536aSLuiz Capitulino     Monitor *mon = opaque;
1654218a536aSLuiz Capitulino 
1655218a536aSLuiz Capitulino     qdict = qobject_to_qdict(data);
1656218a536aSLuiz Capitulino     monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1657218a536aSLuiz Capitulino 
1658218a536aSLuiz Capitulino     qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1659218a536aSLuiz Capitulino     monitor_printf(mon, " rd_bytes=%" PRId64
1660218a536aSLuiz Capitulino                         " wr_bytes=%" PRId64
1661218a536aSLuiz Capitulino                         " rd_operations=%" PRId64
1662218a536aSLuiz Capitulino                         " wr_operations=%" PRId64
1663218a536aSLuiz Capitulino                         "\n",
1664218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_bytes"),
1665218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_bytes"),
1666218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_operations"),
1667218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_operations"));
1668218a536aSLuiz Capitulino }
1669218a536aSLuiz Capitulino 
1670218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data)
1671218a536aSLuiz Capitulino {
1672218a536aSLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1673218a536aSLuiz Capitulino }
1674218a536aSLuiz Capitulino 
1675294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1676294cc35fSKevin Wolf {
1677294cc35fSKevin Wolf     QObject *res;
1678294cc35fSKevin Wolf     QDict *dict;
1679294cc35fSKevin Wolf 
1680294cc35fSKevin Wolf     res = qobject_from_jsonf("{ 'stats': {"
1681294cc35fSKevin Wolf                              "'rd_bytes': %" PRId64 ","
1682294cc35fSKevin Wolf                              "'wr_bytes': %" PRId64 ","
1683294cc35fSKevin Wolf                              "'rd_operations': %" PRId64 ","
1684294cc35fSKevin Wolf                              "'wr_operations': %" PRId64 ","
1685294cc35fSKevin Wolf                              "'wr_highest_offset': %" PRId64
1686294cc35fSKevin Wolf                              "} }",
1687294cc35fSKevin Wolf                              bs->rd_bytes, bs->wr_bytes,
1688294cc35fSKevin Wolf                              bs->rd_ops, bs->wr_ops,
16895ffbbc67SBlue Swirl                              bs->wr_highest_sector *
16905ffbbc67SBlue Swirl                              (uint64_t)BDRV_SECTOR_SIZE);
1691294cc35fSKevin Wolf     dict  = qobject_to_qdict(res);
1692294cc35fSKevin Wolf 
1693294cc35fSKevin Wolf     if (*bs->device_name) {
1694294cc35fSKevin Wolf         qdict_put(dict, "device", qstring_from_str(bs->device_name));
1695294cc35fSKevin Wolf     }
1696294cc35fSKevin Wolf 
1697294cc35fSKevin Wolf     if (bs->file) {
1698294cc35fSKevin Wolf         QObject *parent = bdrv_info_stats_bs(bs->file);
1699294cc35fSKevin Wolf         qdict_put_obj(dict, "parent", parent);
1700294cc35fSKevin Wolf     }
1701294cc35fSKevin Wolf 
1702294cc35fSKevin Wolf     return res;
1703294cc35fSKevin Wolf }
1704294cc35fSKevin Wolf 
1705218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1706218a536aSLuiz Capitulino {
1707218a536aSLuiz Capitulino     QObject *obj;
1708218a536aSLuiz Capitulino     QList *devices;
1709a36e69ddSths     BlockDriverState *bs;
1710a36e69ddSths 
1711218a536aSLuiz Capitulino     devices = qlist_new();
1712218a536aSLuiz Capitulino 
17131b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1714294cc35fSKevin Wolf         obj = bdrv_info_stats_bs(bs);
1715218a536aSLuiz Capitulino         qlist_append_obj(devices, obj);
1716a36e69ddSths     }
1717218a536aSLuiz Capitulino 
1718218a536aSLuiz Capitulino     *ret_data = QOBJECT(devices);
1719a36e69ddSths }
1720ea2384d3Sbellard 
1721045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1722045df330Saliguori {
1723045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1724045df330Saliguori         return bs->backing_file;
1725045df330Saliguori     else if (bs->encrypted)
1726045df330Saliguori         return bs->filename;
1727045df330Saliguori     else
1728045df330Saliguori         return NULL;
1729045df330Saliguori }
1730045df330Saliguori 
173183f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
173283f64091Sbellard                                char *filename, int filename_size)
173383f64091Sbellard {
1734b783e409SKevin Wolf     if (!bs->backing_file) {
173583f64091Sbellard         pstrcpy(filename, filename_size, "");
173683f64091Sbellard     } else {
173783f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
173883f64091Sbellard     }
173983f64091Sbellard }
174083f64091Sbellard 
1741faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1742faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1743faea38e7Sbellard {
1744faea38e7Sbellard     BlockDriver *drv = bs->drv;
1745faea38e7Sbellard     if (!drv)
174619cb3738Sbellard         return -ENOMEDIUM;
1747faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1748faea38e7Sbellard         return -ENOTSUP;
1749fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1750fbb7b4e0SKevin Wolf         return -EIO;
17517cd1e32aSlirans@il.ibm.com 
1752c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
17537cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
17547cd1e32aSlirans@il.ibm.com     }
17557cd1e32aSlirans@il.ibm.com 
1756faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1757faea38e7Sbellard }
1758faea38e7Sbellard 
1759faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1760faea38e7Sbellard {
1761faea38e7Sbellard     BlockDriver *drv = bs->drv;
1762faea38e7Sbellard     if (!drv)
176319cb3738Sbellard         return -ENOMEDIUM;
1764faea38e7Sbellard     if (!drv->bdrv_get_info)
1765faea38e7Sbellard         return -ENOTSUP;
1766faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1767faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1768faea38e7Sbellard }
1769faea38e7Sbellard 
177045566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
177145566e9cSChristoph Hellwig                       int64_t pos, int size)
1772178e08a5Saliguori {
1773178e08a5Saliguori     BlockDriver *drv = bs->drv;
1774178e08a5Saliguori     if (!drv)
1775178e08a5Saliguori         return -ENOMEDIUM;
17767cdb1f6dSMORITA Kazutaka     if (drv->bdrv_save_vmstate)
177745566e9cSChristoph Hellwig         return drv->bdrv_save_vmstate(bs, buf, pos, size);
17787cdb1f6dSMORITA Kazutaka     if (bs->file)
17797cdb1f6dSMORITA Kazutaka         return bdrv_save_vmstate(bs->file, buf, pos, size);
17807cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1781178e08a5Saliguori }
1782178e08a5Saliguori 
178345566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
178445566e9cSChristoph Hellwig                       int64_t pos, int size)
1785178e08a5Saliguori {
1786178e08a5Saliguori     BlockDriver *drv = bs->drv;
1787178e08a5Saliguori     if (!drv)
1788178e08a5Saliguori         return -ENOMEDIUM;
17897cdb1f6dSMORITA Kazutaka     if (drv->bdrv_load_vmstate)
179045566e9cSChristoph Hellwig         return drv->bdrv_load_vmstate(bs, buf, pos, size);
17917cdb1f6dSMORITA Kazutaka     if (bs->file)
17927cdb1f6dSMORITA Kazutaka         return bdrv_load_vmstate(bs->file, buf, pos, size);
17937cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1794178e08a5Saliguori }
1795178e08a5Saliguori 
17968b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
17978b9b0cc2SKevin Wolf {
17988b9b0cc2SKevin Wolf     BlockDriver *drv = bs->drv;
17998b9b0cc2SKevin Wolf 
18008b9b0cc2SKevin Wolf     if (!drv || !drv->bdrv_debug_event) {
18018b9b0cc2SKevin Wolf         return;
18028b9b0cc2SKevin Wolf     }
18038b9b0cc2SKevin Wolf 
18048b9b0cc2SKevin Wolf     return drv->bdrv_debug_event(bs, event);
18058b9b0cc2SKevin Wolf 
18068b9b0cc2SKevin Wolf }
18078b9b0cc2SKevin Wolf 
1808faea38e7Sbellard /**************************************************************/
1809faea38e7Sbellard /* handling of snapshots */
1810faea38e7Sbellard 
1811feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs)
1812feeee5acSMiguel Di Ciurcio Filho {
1813feeee5acSMiguel Di Ciurcio Filho     BlockDriver *drv = bs->drv;
1814feeee5acSMiguel Di Ciurcio Filho     if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1815feeee5acSMiguel Di Ciurcio Filho         return 0;
1816feeee5acSMiguel Di Ciurcio Filho     }
1817feeee5acSMiguel Di Ciurcio Filho 
1818feeee5acSMiguel Di Ciurcio Filho     if (!drv->bdrv_snapshot_create) {
1819feeee5acSMiguel Di Ciurcio Filho         if (bs->file != NULL) {
1820feeee5acSMiguel Di Ciurcio Filho             return bdrv_can_snapshot(bs->file);
1821feeee5acSMiguel Di Ciurcio Filho         }
1822feeee5acSMiguel Di Ciurcio Filho         return 0;
1823feeee5acSMiguel Di Ciurcio Filho     }
1824feeee5acSMiguel Di Ciurcio Filho 
1825feeee5acSMiguel Di Ciurcio Filho     return 1;
1826feeee5acSMiguel Di Ciurcio Filho }
1827feeee5acSMiguel Di Ciurcio Filho 
1828199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs)
1829199630b6SBlue Swirl {
1830199630b6SBlue Swirl     return !!(bs->open_flags & BDRV_O_SNAPSHOT);
1831199630b6SBlue Swirl }
1832199630b6SBlue Swirl 
1833f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void)
1834f9092b10SMarkus Armbruster {
1835f9092b10SMarkus Armbruster     BlockDriverState *bs;
1836f9092b10SMarkus Armbruster 
18373ac906f7SMarkus Armbruster     if (bs_snapshots) {
1838f9092b10SMarkus Armbruster         return bs_snapshots;
18393ac906f7SMarkus Armbruster     }
1840f9092b10SMarkus Armbruster 
1841f9092b10SMarkus Armbruster     bs = NULL;
1842f9092b10SMarkus Armbruster     while ((bs = bdrv_next(bs))) {
1843f9092b10SMarkus Armbruster         if (bdrv_can_snapshot(bs)) {
18443ac906f7SMarkus Armbruster             bs_snapshots = bs;
18453ac906f7SMarkus Armbruster             return bs;
1846f9092b10SMarkus Armbruster         }
1847f9092b10SMarkus Armbruster     }
1848f9092b10SMarkus Armbruster     return NULL;
1849f9092b10SMarkus Armbruster }
1850f9092b10SMarkus Armbruster 
1851faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1852faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1853faea38e7Sbellard {
1854faea38e7Sbellard     BlockDriver *drv = bs->drv;
1855faea38e7Sbellard     if (!drv)
185619cb3738Sbellard         return -ENOMEDIUM;
18577cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_create)
1858faea38e7Sbellard         return drv->bdrv_snapshot_create(bs, sn_info);
18597cdb1f6dSMORITA Kazutaka     if (bs->file)
18607cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_create(bs->file, sn_info);
18617cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1862faea38e7Sbellard }
1863faea38e7Sbellard 
1864faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1865faea38e7Sbellard                        const char *snapshot_id)
1866faea38e7Sbellard {
1867faea38e7Sbellard     BlockDriver *drv = bs->drv;
18687cdb1f6dSMORITA Kazutaka     int ret, open_ret;
18697cdb1f6dSMORITA Kazutaka 
1870faea38e7Sbellard     if (!drv)
187119cb3738Sbellard         return -ENOMEDIUM;
18727cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_goto)
1873faea38e7Sbellard         return drv->bdrv_snapshot_goto(bs, snapshot_id);
18747cdb1f6dSMORITA Kazutaka 
18757cdb1f6dSMORITA Kazutaka     if (bs->file) {
18767cdb1f6dSMORITA Kazutaka         drv->bdrv_close(bs);
18777cdb1f6dSMORITA Kazutaka         ret = bdrv_snapshot_goto(bs->file, snapshot_id);
18787cdb1f6dSMORITA Kazutaka         open_ret = drv->bdrv_open(bs, bs->open_flags);
18797cdb1f6dSMORITA Kazutaka         if (open_ret < 0) {
18807cdb1f6dSMORITA Kazutaka             bdrv_delete(bs->file);
18817cdb1f6dSMORITA Kazutaka             bs->drv = NULL;
18827cdb1f6dSMORITA Kazutaka             return open_ret;
18837cdb1f6dSMORITA Kazutaka         }
18847cdb1f6dSMORITA Kazutaka         return ret;
18857cdb1f6dSMORITA Kazutaka     }
18867cdb1f6dSMORITA Kazutaka 
18877cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1888faea38e7Sbellard }
1889faea38e7Sbellard 
1890faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1891faea38e7Sbellard {
1892faea38e7Sbellard     BlockDriver *drv = bs->drv;
1893faea38e7Sbellard     if (!drv)
189419cb3738Sbellard         return -ENOMEDIUM;
18957cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_delete)
1896faea38e7Sbellard         return drv->bdrv_snapshot_delete(bs, snapshot_id);
18977cdb1f6dSMORITA Kazutaka     if (bs->file)
18987cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_delete(bs->file, snapshot_id);
18997cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1900faea38e7Sbellard }
1901faea38e7Sbellard 
1902faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1903faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1904faea38e7Sbellard {
1905faea38e7Sbellard     BlockDriver *drv = bs->drv;
1906faea38e7Sbellard     if (!drv)
190719cb3738Sbellard         return -ENOMEDIUM;
19087cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_list)
1909faea38e7Sbellard         return drv->bdrv_snapshot_list(bs, psn_info);
19107cdb1f6dSMORITA Kazutaka     if (bs->file)
19117cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_list(bs->file, psn_info);
19127cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1913faea38e7Sbellard }
1914faea38e7Sbellard 
191551ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs,
191651ef6727Sedison         const char *snapshot_name)
191751ef6727Sedison {
191851ef6727Sedison     BlockDriver *drv = bs->drv;
191951ef6727Sedison     if (!drv) {
192051ef6727Sedison         return -ENOMEDIUM;
192151ef6727Sedison     }
192251ef6727Sedison     if (!bs->read_only) {
192351ef6727Sedison         return -EINVAL;
192451ef6727Sedison     }
192551ef6727Sedison     if (drv->bdrv_snapshot_load_tmp) {
192651ef6727Sedison         return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
192751ef6727Sedison     }
192851ef6727Sedison     return -ENOTSUP;
192951ef6727Sedison }
193051ef6727Sedison 
1931faea38e7Sbellard #define NB_SUFFIXES 4
1932faea38e7Sbellard 
1933faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1934faea38e7Sbellard {
1935faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1936faea38e7Sbellard     int64_t base;
1937faea38e7Sbellard     int i;
1938faea38e7Sbellard 
1939faea38e7Sbellard     if (size <= 999) {
1940faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1941faea38e7Sbellard     } else {
1942faea38e7Sbellard         base = 1024;
1943faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1944faea38e7Sbellard             if (size < (10 * base)) {
1945faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1946faea38e7Sbellard                          (double)size / base,
1947faea38e7Sbellard                          suffixes[i]);
1948faea38e7Sbellard                 break;
1949faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1950faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1951faea38e7Sbellard                          ((size + (base >> 1)) / base),
1952faea38e7Sbellard                          suffixes[i]);
1953faea38e7Sbellard                 break;
1954faea38e7Sbellard             }
1955faea38e7Sbellard             base = base * 1024;
1956faea38e7Sbellard         }
1957faea38e7Sbellard     }
1958faea38e7Sbellard     return buf;
1959faea38e7Sbellard }
1960faea38e7Sbellard 
1961faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1962faea38e7Sbellard {
1963faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
19643b9f94e1Sbellard #ifdef _WIN32
19653b9f94e1Sbellard     struct tm *ptm;
19663b9f94e1Sbellard #else
1967faea38e7Sbellard     struct tm tm;
19683b9f94e1Sbellard #endif
1969faea38e7Sbellard     time_t ti;
1970faea38e7Sbellard     int64_t secs;
1971faea38e7Sbellard 
1972faea38e7Sbellard     if (!sn) {
1973faea38e7Sbellard         snprintf(buf, buf_size,
1974faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1975faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1976faea38e7Sbellard     } else {
1977faea38e7Sbellard         ti = sn->date_sec;
19783b9f94e1Sbellard #ifdef _WIN32
19793b9f94e1Sbellard         ptm = localtime(&ti);
19803b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
19813b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
19823b9f94e1Sbellard #else
1983faea38e7Sbellard         localtime_r(&ti, &tm);
1984faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1985faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
19863b9f94e1Sbellard #endif
1987faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1988faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1989faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1990faea38e7Sbellard                  (int)(secs / 3600),
1991faea38e7Sbellard                  (int)((secs / 60) % 60),
1992faea38e7Sbellard                  (int)(secs % 60),
1993faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1994faea38e7Sbellard         snprintf(buf, buf_size,
1995faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1996faea38e7Sbellard                  sn->id_str, sn->name,
1997faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1998faea38e7Sbellard                  date_buf,
1999faea38e7Sbellard                  clock_buf);
2000faea38e7Sbellard     }
2001faea38e7Sbellard     return buf;
2002faea38e7Sbellard }
2003faea38e7Sbellard 
200483f64091Sbellard 
2005ea2384d3Sbellard /**************************************************************/
200683f64091Sbellard /* async I/Os */
2007ea2384d3Sbellard 
20083b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
2009f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
201083f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
2011ea2384d3Sbellard {
201283f64091Sbellard     BlockDriver *drv = bs->drv;
2013a36e69ddSths     BlockDriverAIOCB *ret;
2014ea2384d3Sbellard 
2015bbf0a440SStefan Hajnoczi     trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2016bbf0a440SStefan Hajnoczi 
201719cb3738Sbellard     if (!drv)
2018ce1a14dcSpbrook         return NULL;
201971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
202071d0770cSaliguori         return NULL;
202183f64091Sbellard 
2022f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
2023f141eafeSaliguori                               cb, opaque);
2024a36e69ddSths 
2025a36e69ddSths     if (ret) {
2026a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
20276ea44308SJan Kiszka 	bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2028a36e69ddSths 	bs->rd_ops ++;
2029a36e69ddSths     }
2030a36e69ddSths 
2031a36e69ddSths     return ret;
203283f64091Sbellard }
203383f64091Sbellard 
20344dcafbb1SMarcelo Tosatti typedef struct BlockCompleteData {
20354dcafbb1SMarcelo Tosatti     BlockDriverCompletionFunc *cb;
20364dcafbb1SMarcelo Tosatti     void *opaque;
20374dcafbb1SMarcelo Tosatti     BlockDriverState *bs;
20384dcafbb1SMarcelo Tosatti     int64_t sector_num;
20394dcafbb1SMarcelo Tosatti     int nb_sectors;
20404dcafbb1SMarcelo Tosatti } BlockCompleteData;
20414dcafbb1SMarcelo Tosatti 
20424dcafbb1SMarcelo Tosatti static void block_complete_cb(void *opaque, int ret)
20434dcafbb1SMarcelo Tosatti {
20444dcafbb1SMarcelo Tosatti     BlockCompleteData *b = opaque;
20454dcafbb1SMarcelo Tosatti 
20464dcafbb1SMarcelo Tosatti     if (b->bs->dirty_bitmap) {
20474dcafbb1SMarcelo Tosatti         set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1);
20484dcafbb1SMarcelo Tosatti     }
20494dcafbb1SMarcelo Tosatti     b->cb(b->opaque, ret);
20504dcafbb1SMarcelo Tosatti     qemu_free(b);
20514dcafbb1SMarcelo Tosatti }
20524dcafbb1SMarcelo Tosatti 
20534dcafbb1SMarcelo Tosatti static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs,
20544dcafbb1SMarcelo Tosatti                                              int64_t sector_num,
20554dcafbb1SMarcelo Tosatti                                              int nb_sectors,
20564dcafbb1SMarcelo Tosatti                                              BlockDriverCompletionFunc *cb,
20574dcafbb1SMarcelo Tosatti                                              void *opaque)
20584dcafbb1SMarcelo Tosatti {
20594dcafbb1SMarcelo Tosatti     BlockCompleteData *blkdata = qemu_mallocz(sizeof(BlockCompleteData));
20604dcafbb1SMarcelo Tosatti 
20614dcafbb1SMarcelo Tosatti     blkdata->bs = bs;
20624dcafbb1SMarcelo Tosatti     blkdata->cb = cb;
20634dcafbb1SMarcelo Tosatti     blkdata->opaque = opaque;
20644dcafbb1SMarcelo Tosatti     blkdata->sector_num = sector_num;
20654dcafbb1SMarcelo Tosatti     blkdata->nb_sectors = nb_sectors;
20664dcafbb1SMarcelo Tosatti 
20674dcafbb1SMarcelo Tosatti     return blkdata;
20684dcafbb1SMarcelo Tosatti }
20694dcafbb1SMarcelo Tosatti 
2070f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2071f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
207283f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
20737674e7bfSbellard {
207483f64091Sbellard     BlockDriver *drv = bs->drv;
2075a36e69ddSths     BlockDriverAIOCB *ret;
20764dcafbb1SMarcelo Tosatti     BlockCompleteData *blk_cb_data;
207783f64091Sbellard 
2078bbf0a440SStefan Hajnoczi     trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2079bbf0a440SStefan Hajnoczi 
208019cb3738Sbellard     if (!drv)
2081ce1a14dcSpbrook         return NULL;
208283f64091Sbellard     if (bs->read_only)
2083ce1a14dcSpbrook         return NULL;
208471d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
208571d0770cSaliguori         return NULL;
208683f64091Sbellard 
2087c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
20884dcafbb1SMarcelo Tosatti         blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb,
20894dcafbb1SMarcelo Tosatti                                          opaque);
20904dcafbb1SMarcelo Tosatti         cb = &block_complete_cb;
20914dcafbb1SMarcelo Tosatti         opaque = blk_cb_data;
20927cd1e32aSlirans@il.ibm.com     }
20937cd1e32aSlirans@il.ibm.com 
2094f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2095f141eafeSaliguori                                cb, opaque);
2096a36e69ddSths 
2097a36e69ddSths     if (ret) {
2098a36e69ddSths         /* Update stats even though technically transfer has not happened. */
20996ea44308SJan Kiszka         bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2100a36e69ddSths         bs->wr_ops ++;
2101294cc35fSKevin Wolf         if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2102294cc35fSKevin Wolf             bs->wr_highest_sector = sector_num + nb_sectors - 1;
2103294cc35fSKevin Wolf         }
2104a36e69ddSths     }
2105a36e69ddSths 
2106a36e69ddSths     return ret;
210783f64091Sbellard }
210883f64091Sbellard 
210940b4f539SKevin Wolf 
211040b4f539SKevin Wolf typedef struct MultiwriteCB {
211140b4f539SKevin Wolf     int error;
211240b4f539SKevin Wolf     int num_requests;
211340b4f539SKevin Wolf     int num_callbacks;
211440b4f539SKevin Wolf     struct {
211540b4f539SKevin Wolf         BlockDriverCompletionFunc *cb;
211640b4f539SKevin Wolf         void *opaque;
211740b4f539SKevin Wolf         QEMUIOVector *free_qiov;
211840b4f539SKevin Wolf         void *free_buf;
211940b4f539SKevin Wolf     } callbacks[];
212040b4f539SKevin Wolf } MultiwriteCB;
212140b4f539SKevin Wolf 
212240b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb)
212340b4f539SKevin Wolf {
212440b4f539SKevin Wolf     int i;
212540b4f539SKevin Wolf 
212640b4f539SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
212740b4f539SKevin Wolf         mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
21281e1ea48dSStefan Hajnoczi         if (mcb->callbacks[i].free_qiov) {
21291e1ea48dSStefan Hajnoczi             qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
21301e1ea48dSStefan Hajnoczi         }
213140b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_qiov);
2132f8a83245SHerve Poussineau         qemu_vfree(mcb->callbacks[i].free_buf);
213340b4f539SKevin Wolf     }
213440b4f539SKevin Wolf }
213540b4f539SKevin Wolf 
213640b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret)
213740b4f539SKevin Wolf {
213840b4f539SKevin Wolf     MultiwriteCB *mcb = opaque;
213940b4f539SKevin Wolf 
21406d519a5fSStefan Hajnoczi     trace_multiwrite_cb(mcb, ret);
21416d519a5fSStefan Hajnoczi 
2142cb6d3ca0SKevin Wolf     if (ret < 0 && !mcb->error) {
214340b4f539SKevin Wolf         mcb->error = ret;
214440b4f539SKevin Wolf     }
214540b4f539SKevin Wolf 
214640b4f539SKevin Wolf     mcb->num_requests--;
214740b4f539SKevin Wolf     if (mcb->num_requests == 0) {
214840b4f539SKevin Wolf         multiwrite_user_cb(mcb);
214940b4f539SKevin Wolf         qemu_free(mcb);
215040b4f539SKevin Wolf     }
215140b4f539SKevin Wolf }
215240b4f539SKevin Wolf 
215340b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b)
215440b4f539SKevin Wolf {
215577be4366SChristoph Hellwig     const BlockRequest *req1 = a, *req2 = b;
215677be4366SChristoph Hellwig 
215777be4366SChristoph Hellwig     /*
215877be4366SChristoph Hellwig      * Note that we can't simply subtract req2->sector from req1->sector
215977be4366SChristoph Hellwig      * here as that could overflow the return value.
216077be4366SChristoph Hellwig      */
216177be4366SChristoph Hellwig     if (req1->sector > req2->sector) {
216277be4366SChristoph Hellwig         return 1;
216377be4366SChristoph Hellwig     } else if (req1->sector < req2->sector) {
216477be4366SChristoph Hellwig         return -1;
216577be4366SChristoph Hellwig     } else {
216677be4366SChristoph Hellwig         return 0;
216777be4366SChristoph Hellwig     }
216840b4f539SKevin Wolf }
216940b4f539SKevin Wolf 
217040b4f539SKevin Wolf /*
217140b4f539SKevin Wolf  * Takes a bunch of requests and tries to merge them. Returns the number of
217240b4f539SKevin Wolf  * requests that remain after merging.
217340b4f539SKevin Wolf  */
217440b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
217540b4f539SKevin Wolf     int num_reqs, MultiwriteCB *mcb)
217640b4f539SKevin Wolf {
217740b4f539SKevin Wolf     int i, outidx;
217840b4f539SKevin Wolf 
217940b4f539SKevin Wolf     // Sort requests by start sector
218040b4f539SKevin Wolf     qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
218140b4f539SKevin Wolf 
218240b4f539SKevin Wolf     // Check if adjacent requests touch the same clusters. If so, combine them,
218340b4f539SKevin Wolf     // filling up gaps with zero sectors.
218440b4f539SKevin Wolf     outidx = 0;
218540b4f539SKevin Wolf     for (i = 1; i < num_reqs; i++) {
218640b4f539SKevin Wolf         int merge = 0;
218740b4f539SKevin Wolf         int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
218840b4f539SKevin Wolf 
218940b4f539SKevin Wolf         // This handles the cases that are valid for all block drivers, namely
219040b4f539SKevin Wolf         // exactly sequential writes and overlapping writes.
219140b4f539SKevin Wolf         if (reqs[i].sector <= oldreq_last) {
219240b4f539SKevin Wolf             merge = 1;
219340b4f539SKevin Wolf         }
219440b4f539SKevin Wolf 
219540b4f539SKevin Wolf         // The block driver may decide that it makes sense to combine requests
219640b4f539SKevin Wolf         // even if there is a gap of some sectors between them. In this case,
219740b4f539SKevin Wolf         // the gap is filled with zeros (therefore only applicable for yet
219840b4f539SKevin Wolf         // unused space in format like qcow2).
219940b4f539SKevin Wolf         if (!merge && bs->drv->bdrv_merge_requests) {
220040b4f539SKevin Wolf             merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
220140b4f539SKevin Wolf         }
220240b4f539SKevin Wolf 
2203e2a305fbSChristoph Hellwig         if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2204e2a305fbSChristoph Hellwig             merge = 0;
2205e2a305fbSChristoph Hellwig         }
2206e2a305fbSChristoph Hellwig 
220740b4f539SKevin Wolf         if (merge) {
220840b4f539SKevin Wolf             size_t size;
220940b4f539SKevin Wolf             QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
221040b4f539SKevin Wolf             qemu_iovec_init(qiov,
221140b4f539SKevin Wolf                 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
221240b4f539SKevin Wolf 
221340b4f539SKevin Wolf             // Add the first request to the merged one. If the requests are
221440b4f539SKevin Wolf             // overlapping, drop the last sectors of the first request.
221540b4f539SKevin Wolf             size = (reqs[i].sector - reqs[outidx].sector) << 9;
221640b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
221740b4f539SKevin Wolf 
221840b4f539SKevin Wolf             // We might need to add some zeros between the two requests
221940b4f539SKevin Wolf             if (reqs[i].sector > oldreq_last) {
222040b4f539SKevin Wolf                 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
222140b4f539SKevin Wolf                 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
222240b4f539SKevin Wolf                 memset(buf, 0, zero_bytes);
222340b4f539SKevin Wolf                 qemu_iovec_add(qiov, buf, zero_bytes);
222440b4f539SKevin Wolf                 mcb->callbacks[i].free_buf = buf;
222540b4f539SKevin Wolf             }
222640b4f539SKevin Wolf 
222740b4f539SKevin Wolf             // Add the second request
222840b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
222940b4f539SKevin Wolf 
2230cbf1dff2SKevin Wolf             reqs[outidx].nb_sectors = qiov->size >> 9;
223140b4f539SKevin Wolf             reqs[outidx].qiov = qiov;
223240b4f539SKevin Wolf 
223340b4f539SKevin Wolf             mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
223440b4f539SKevin Wolf         } else {
223540b4f539SKevin Wolf             outidx++;
223640b4f539SKevin Wolf             reqs[outidx].sector     = reqs[i].sector;
223740b4f539SKevin Wolf             reqs[outidx].nb_sectors = reqs[i].nb_sectors;
223840b4f539SKevin Wolf             reqs[outidx].qiov       = reqs[i].qiov;
223940b4f539SKevin Wolf         }
224040b4f539SKevin Wolf     }
224140b4f539SKevin Wolf 
224240b4f539SKevin Wolf     return outidx + 1;
224340b4f539SKevin Wolf }
224440b4f539SKevin Wolf 
224540b4f539SKevin Wolf /*
224640b4f539SKevin Wolf  * Submit multiple AIO write requests at once.
224740b4f539SKevin Wolf  *
224840b4f539SKevin Wolf  * On success, the function returns 0 and all requests in the reqs array have
224940b4f539SKevin Wolf  * been submitted. In error case this function returns -1, and any of the
225040b4f539SKevin Wolf  * requests may or may not be submitted yet. In particular, this means that the
225140b4f539SKevin Wolf  * callback will be called for some of the requests, for others it won't. The
225240b4f539SKevin Wolf  * caller must check the error field of the BlockRequest to wait for the right
225340b4f539SKevin Wolf  * callbacks (if error != 0, no callback will be called).
225440b4f539SKevin Wolf  *
225540b4f539SKevin Wolf  * The implementation may modify the contents of the reqs array, e.g. to merge
225640b4f539SKevin Wolf  * requests. However, the fields opaque and error are left unmodified as they
225740b4f539SKevin Wolf  * are used to signal failure for a single request to the caller.
225840b4f539SKevin Wolf  */
225940b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
226040b4f539SKevin Wolf {
226140b4f539SKevin Wolf     BlockDriverAIOCB *acb;
226240b4f539SKevin Wolf     MultiwriteCB *mcb;
226340b4f539SKevin Wolf     int i;
226440b4f539SKevin Wolf 
226540b4f539SKevin Wolf     if (num_reqs == 0) {
226640b4f539SKevin Wolf         return 0;
226740b4f539SKevin Wolf     }
226840b4f539SKevin Wolf 
226940b4f539SKevin Wolf     // Create MultiwriteCB structure
227040b4f539SKevin Wolf     mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
227140b4f539SKevin Wolf     mcb->num_requests = 0;
227240b4f539SKevin Wolf     mcb->num_callbacks = num_reqs;
227340b4f539SKevin Wolf 
227440b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
227540b4f539SKevin Wolf         mcb->callbacks[i].cb = reqs[i].cb;
227640b4f539SKevin Wolf         mcb->callbacks[i].opaque = reqs[i].opaque;
227740b4f539SKevin Wolf     }
227840b4f539SKevin Wolf 
227940b4f539SKevin Wolf     // Check for mergable requests
228040b4f539SKevin Wolf     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
228140b4f539SKevin Wolf 
22826d519a5fSStefan Hajnoczi     trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
22836d519a5fSStefan Hajnoczi 
2284453f9a16SKevin Wolf     /*
2285453f9a16SKevin Wolf      * Run the aio requests. As soon as one request can't be submitted
2286453f9a16SKevin Wolf      * successfully, fail all requests that are not yet submitted (we must
2287453f9a16SKevin Wolf      * return failure for all requests anyway)
2288453f9a16SKevin Wolf      *
2289453f9a16SKevin Wolf      * num_requests cannot be set to the right value immediately: If
2290453f9a16SKevin Wolf      * bdrv_aio_writev fails for some request, num_requests would be too high
2291453f9a16SKevin Wolf      * and therefore multiwrite_cb() would never recognize the multiwrite
2292453f9a16SKevin Wolf      * request as completed. We also cannot use the loop variable i to set it
2293453f9a16SKevin Wolf      * when the first request fails because the callback may already have been
2294453f9a16SKevin Wolf      * called for previously submitted requests. Thus, num_requests must be
2295453f9a16SKevin Wolf      * incremented for each request that is submitted.
2296453f9a16SKevin Wolf      *
2297453f9a16SKevin Wolf      * The problem that callbacks may be called early also means that we need
2298453f9a16SKevin Wolf      * to take care that num_requests doesn't become 0 before all requests are
2299453f9a16SKevin Wolf      * submitted - multiwrite_cb() would consider the multiwrite request
2300453f9a16SKevin Wolf      * completed. A dummy request that is "completed" by a manual call to
2301453f9a16SKevin Wolf      * multiwrite_cb() takes care of this.
2302453f9a16SKevin Wolf      */
2303453f9a16SKevin Wolf     mcb->num_requests = 1;
2304453f9a16SKevin Wolf 
23056d519a5fSStefan Hajnoczi     // Run the aio requests
230640b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
2307453f9a16SKevin Wolf         mcb->num_requests++;
230840b4f539SKevin Wolf         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
230940b4f539SKevin Wolf             reqs[i].nb_sectors, multiwrite_cb, mcb);
231040b4f539SKevin Wolf 
231140b4f539SKevin Wolf         if (acb == NULL) {
231240b4f539SKevin Wolf             // We can only fail the whole thing if no request has been
231340b4f539SKevin Wolf             // submitted yet. Otherwise we'll wait for the submitted AIOs to
231440b4f539SKevin Wolf             // complete and report the error in the callback.
2315453f9a16SKevin Wolf             if (i == 0) {
23166d519a5fSStefan Hajnoczi                 trace_bdrv_aio_multiwrite_earlyfail(mcb);
231740b4f539SKevin Wolf                 goto fail;
231840b4f539SKevin Wolf             } else {
23196d519a5fSStefan Hajnoczi                 trace_bdrv_aio_multiwrite_latefail(mcb, i);
23207eb58a6cSKevin Wolf                 multiwrite_cb(mcb, -EIO);
232140b4f539SKevin Wolf                 break;
232240b4f539SKevin Wolf             }
232340b4f539SKevin Wolf         }
232440b4f539SKevin Wolf     }
232540b4f539SKevin Wolf 
2326453f9a16SKevin Wolf     /* Complete the dummy request */
2327453f9a16SKevin Wolf     multiwrite_cb(mcb, 0);
2328453f9a16SKevin Wolf 
232940b4f539SKevin Wolf     return 0;
233040b4f539SKevin Wolf 
233140b4f539SKevin Wolf fail:
2332453f9a16SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
2333453f9a16SKevin Wolf         reqs[i].error = -EIO;
2334453f9a16SKevin Wolf     }
2335af474591SBruce Rogers     qemu_free(mcb);
233640b4f539SKevin Wolf     return -1;
233740b4f539SKevin Wolf }
233840b4f539SKevin Wolf 
2339b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2340b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2341b2e12bc6SChristoph Hellwig {
2342b2e12bc6SChristoph Hellwig     BlockDriver *drv = bs->drv;
2343b2e12bc6SChristoph Hellwig 
2344016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
2345016f5cf6SAlexander Graf         return bdrv_aio_noop_em(bs, cb, opaque);
2346016f5cf6SAlexander Graf     }
2347016f5cf6SAlexander Graf 
2348b2e12bc6SChristoph Hellwig     if (!drv)
2349b2e12bc6SChristoph Hellwig         return NULL;
2350b2e12bc6SChristoph Hellwig     return drv->bdrv_aio_flush(bs, cb, opaque);
2351b2e12bc6SChristoph Hellwig }
2352b2e12bc6SChristoph Hellwig 
235383f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
235483f64091Sbellard {
23556bbff9a0Saliguori     acb->pool->cancel(acb);
235683f64091Sbellard }
235783f64091Sbellard 
235883f64091Sbellard 
235983f64091Sbellard /**************************************************************/
236083f64091Sbellard /* async block device emulation */
236183f64091Sbellard 
2362c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync {
2363c16b5a2cSChristoph Hellwig     BlockDriverAIOCB common;
2364c16b5a2cSChristoph Hellwig     QEMUBH *bh;
2365c16b5a2cSChristoph Hellwig     int ret;
2366c16b5a2cSChristoph Hellwig     /* vector translation state */
2367c16b5a2cSChristoph Hellwig     QEMUIOVector *qiov;
2368c16b5a2cSChristoph Hellwig     uint8_t *bounce;
2369c16b5a2cSChristoph Hellwig     int is_write;
2370c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync;
2371c16b5a2cSChristoph Hellwig 
2372c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2373c16b5a2cSChristoph Hellwig {
2374b666d239SKevin Wolf     BlockDriverAIOCBSync *acb =
2375b666d239SKevin Wolf         container_of(blockacb, BlockDriverAIOCBSync, common);
23766a7ad299SDor Laor     qemu_bh_delete(acb->bh);
237736afc451SAvi Kivity     acb->bh = NULL;
2378c16b5a2cSChristoph Hellwig     qemu_aio_release(acb);
2379c16b5a2cSChristoph Hellwig }
2380c16b5a2cSChristoph Hellwig 
2381c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = {
2382c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2383c16b5a2cSChristoph Hellwig     .cancel             = bdrv_aio_cancel_em,
2384c16b5a2cSChristoph Hellwig };
2385c16b5a2cSChristoph Hellwig 
238683f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
2387beac80cdSbellard {
2388ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
2389f141eafeSaliguori 
2390f141eafeSaliguori     if (!acb->is_write)
2391f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2392ceb42de8Saliguori     qemu_vfree(acb->bounce);
2393ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
23946a7ad299SDor Laor     qemu_bh_delete(acb->bh);
239536afc451SAvi Kivity     acb->bh = NULL;
2396ce1a14dcSpbrook     qemu_aio_release(acb);
2397beac80cdSbellard }
2398beac80cdSbellard 
2399f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2400f141eafeSaliguori                                             int64_t sector_num,
2401f141eafeSaliguori                                             QEMUIOVector *qiov,
2402f141eafeSaliguori                                             int nb_sectors,
2403f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
2404f141eafeSaliguori                                             void *opaque,
2405f141eafeSaliguori                                             int is_write)
2406f141eafeSaliguori 
2407ea2384d3Sbellard {
2408ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
240983f64091Sbellard 
2410c16b5a2cSChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2411f141eafeSaliguori     acb->is_write = is_write;
2412f141eafeSaliguori     acb->qiov = qiov;
2413e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
2414f141eafeSaliguori 
2415ce1a14dcSpbrook     if (!acb->bh)
2416ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2417f141eafeSaliguori 
2418f141eafeSaliguori     if (is_write) {
2419f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2420f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2421f141eafeSaliguori     } else {
2422f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2423f141eafeSaliguori     }
2424f141eafeSaliguori 
2425ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
2426f141eafeSaliguori 
2427ce1a14dcSpbrook     return &acb->common;
24287a6cba61Spbrook }
24297a6cba61Spbrook 
2430f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2431f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2432ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
243383f64091Sbellard {
2434f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
243583f64091Sbellard }
243683f64091Sbellard 
2437f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2438f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2439f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
2440f141eafeSaliguori {
2441f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2442f141eafeSaliguori }
2443f141eafeSaliguori 
2444b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2445b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2446b2e12bc6SChristoph Hellwig {
2447b2e12bc6SChristoph Hellwig     BlockDriverAIOCBSync *acb;
2448b2e12bc6SChristoph Hellwig 
2449b2e12bc6SChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2450b2e12bc6SChristoph Hellwig     acb->is_write = 1; /* don't bounce in the completion hadler */
2451b2e12bc6SChristoph Hellwig     acb->qiov = NULL;
2452b2e12bc6SChristoph Hellwig     acb->bounce = NULL;
2453b2e12bc6SChristoph Hellwig     acb->ret = 0;
2454b2e12bc6SChristoph Hellwig 
2455b2e12bc6SChristoph Hellwig     if (!acb->bh)
2456b2e12bc6SChristoph Hellwig         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2457b2e12bc6SChristoph Hellwig 
2458b2e12bc6SChristoph Hellwig     bdrv_flush(bs);
2459b2e12bc6SChristoph Hellwig     qemu_bh_schedule(acb->bh);
2460b2e12bc6SChristoph Hellwig     return &acb->common;
2461b2e12bc6SChristoph Hellwig }
2462b2e12bc6SChristoph Hellwig 
2463016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2464016f5cf6SAlexander Graf         BlockDriverCompletionFunc *cb, void *opaque)
2465016f5cf6SAlexander Graf {
2466016f5cf6SAlexander Graf     BlockDriverAIOCBSync *acb;
2467016f5cf6SAlexander Graf 
2468016f5cf6SAlexander Graf     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2469016f5cf6SAlexander Graf     acb->is_write = 1; /* don't bounce in the completion handler */
2470016f5cf6SAlexander Graf     acb->qiov = NULL;
2471016f5cf6SAlexander Graf     acb->bounce = NULL;
2472016f5cf6SAlexander Graf     acb->ret = 0;
2473016f5cf6SAlexander Graf 
2474016f5cf6SAlexander Graf     if (!acb->bh) {
2475016f5cf6SAlexander Graf         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2476016f5cf6SAlexander Graf     }
2477016f5cf6SAlexander Graf 
2478016f5cf6SAlexander Graf     qemu_bh_schedule(acb->bh);
2479016f5cf6SAlexander Graf     return &acb->common;
2480016f5cf6SAlexander Graf }
2481016f5cf6SAlexander Graf 
248283f64091Sbellard /**************************************************************/
248383f64091Sbellard /* sync block device emulation */
248483f64091Sbellard 
248583f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
248683f64091Sbellard {
248783f64091Sbellard     *(int *)opaque = ret;
248883f64091Sbellard }
248983f64091Sbellard 
249083f64091Sbellard #define NOT_DONE 0x7fffffff
249183f64091Sbellard 
249283f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
249383f64091Sbellard                         uint8_t *buf, int nb_sectors)
249483f64091Sbellard {
2495ce1a14dcSpbrook     int async_ret;
2496ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2497f141eafeSaliguori     struct iovec iov;
2498f141eafeSaliguori     QEMUIOVector qiov;
249983f64091Sbellard 
250065d6b3d8SKevin Wolf     async_context_push();
250165d6b3d8SKevin Wolf 
250283f64091Sbellard     async_ret = NOT_DONE;
25033f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
2504eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2505f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2506f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
250783f64091Sbellard         bdrv_rw_em_cb, &async_ret);
250865d6b3d8SKevin Wolf     if (acb == NULL) {
250965d6b3d8SKevin Wolf         async_ret = -1;
251065d6b3d8SKevin Wolf         goto fail;
251165d6b3d8SKevin Wolf     }
2512baf35cb9Saliguori 
251383f64091Sbellard     while (async_ret == NOT_DONE) {
251483f64091Sbellard         qemu_aio_wait();
251583f64091Sbellard     }
2516baf35cb9Saliguori 
251765d6b3d8SKevin Wolf 
251865d6b3d8SKevin Wolf fail:
251965d6b3d8SKevin Wolf     async_context_pop();
252083f64091Sbellard     return async_ret;
252183f64091Sbellard }
252283f64091Sbellard 
252383f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
252483f64091Sbellard                          const uint8_t *buf, int nb_sectors)
252583f64091Sbellard {
2526ce1a14dcSpbrook     int async_ret;
2527ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2528f141eafeSaliguori     struct iovec iov;
2529f141eafeSaliguori     QEMUIOVector qiov;
253083f64091Sbellard 
253165d6b3d8SKevin Wolf     async_context_push();
253265d6b3d8SKevin Wolf 
253383f64091Sbellard     async_ret = NOT_DONE;
2534f141eafeSaliguori     iov.iov_base = (void *)buf;
2535eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2536f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2537f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
253883f64091Sbellard         bdrv_rw_em_cb, &async_ret);
253965d6b3d8SKevin Wolf     if (acb == NULL) {
254065d6b3d8SKevin Wolf         async_ret = -1;
254165d6b3d8SKevin Wolf         goto fail;
254265d6b3d8SKevin Wolf     }
254383f64091Sbellard     while (async_ret == NOT_DONE) {
254483f64091Sbellard         qemu_aio_wait();
254583f64091Sbellard     }
254665d6b3d8SKevin Wolf 
254765d6b3d8SKevin Wolf fail:
254865d6b3d8SKevin Wolf     async_context_pop();
254983f64091Sbellard     return async_ret;
255083f64091Sbellard }
2551ea2384d3Sbellard 
2552ea2384d3Sbellard void bdrv_init(void)
2553ea2384d3Sbellard {
25545efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
2555ea2384d3Sbellard }
2556ce1a14dcSpbrook 
2557eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void)
2558eb852011SMarkus Armbruster {
2559eb852011SMarkus Armbruster     use_bdrv_whitelist = 1;
2560eb852011SMarkus Armbruster     bdrv_init();
2561eb852011SMarkus Armbruster }
2562eb852011SMarkus Armbruster 
2563c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
25646bbff9a0Saliguori                    BlockDriverCompletionFunc *cb, void *opaque)
25656bbff9a0Saliguori {
2566ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2567ce1a14dcSpbrook 
25686bbff9a0Saliguori     if (pool->free_aiocb) {
25696bbff9a0Saliguori         acb = pool->free_aiocb;
25706bbff9a0Saliguori         pool->free_aiocb = acb->next;
2571ce1a14dcSpbrook     } else {
25726bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
25736bbff9a0Saliguori         acb->pool = pool;
2574ce1a14dcSpbrook     }
2575ce1a14dcSpbrook     acb->bs = bs;
2576ce1a14dcSpbrook     acb->cb = cb;
2577ce1a14dcSpbrook     acb->opaque = opaque;
2578ce1a14dcSpbrook     return acb;
2579ce1a14dcSpbrook }
2580ce1a14dcSpbrook 
2581ce1a14dcSpbrook void qemu_aio_release(void *p)
2582ce1a14dcSpbrook {
25836bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
25846bbff9a0Saliguori     AIOPool *pool = acb->pool;
25856bbff9a0Saliguori     acb->next = pool->free_aiocb;
25866bbff9a0Saliguori     pool->free_aiocb = acb;
2587ce1a14dcSpbrook }
258819cb3738Sbellard 
258919cb3738Sbellard /**************************************************************/
259019cb3738Sbellard /* removable device support */
259119cb3738Sbellard 
259219cb3738Sbellard /**
259319cb3738Sbellard  * Return TRUE if the media is present
259419cb3738Sbellard  */
259519cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
259619cb3738Sbellard {
259719cb3738Sbellard     BlockDriver *drv = bs->drv;
259819cb3738Sbellard     int ret;
259919cb3738Sbellard     if (!drv)
260019cb3738Sbellard         return 0;
260119cb3738Sbellard     if (!drv->bdrv_is_inserted)
26024be9762aSMarkus Armbruster         return !bs->tray_open;
260319cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
260419cb3738Sbellard     return ret;
260519cb3738Sbellard }
260619cb3738Sbellard 
260719cb3738Sbellard /**
260819cb3738Sbellard  * Return TRUE if the media changed since the last call to this
260919cb3738Sbellard  * function. It is currently only used for floppy disks
261019cb3738Sbellard  */
261119cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
261219cb3738Sbellard {
261319cb3738Sbellard     BlockDriver *drv = bs->drv;
261419cb3738Sbellard     int ret;
261519cb3738Sbellard 
261619cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
261719cb3738Sbellard         ret = -ENOTSUP;
261819cb3738Sbellard     else
261919cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
262019cb3738Sbellard     if (ret == -ENOTSUP)
262119cb3738Sbellard         ret = bs->media_changed;
262219cb3738Sbellard     bs->media_changed = 0;
262319cb3738Sbellard     return ret;
262419cb3738Sbellard }
262519cb3738Sbellard 
262619cb3738Sbellard /**
262719cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
262819cb3738Sbellard  */
2629aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag)
263019cb3738Sbellard {
263119cb3738Sbellard     BlockDriver *drv = bs->drv;
263219cb3738Sbellard     int ret;
263319cb3738Sbellard 
2634aea2a33cSMark McLoughlin     if (bs->locked) {
2635aea2a33cSMark McLoughlin         return -EBUSY;
2636aea2a33cSMark McLoughlin     }
2637aea2a33cSMark McLoughlin 
263819cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
263919cb3738Sbellard         ret = -ENOTSUP;
264019cb3738Sbellard     } else {
264119cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
264219cb3738Sbellard     }
264319cb3738Sbellard     if (ret == -ENOTSUP) {
2644aea2a33cSMark McLoughlin         ret = 0;
264519cb3738Sbellard     }
26464be9762aSMarkus Armbruster     if (ret >= 0) {
26474be9762aSMarkus Armbruster         bs->tray_open = eject_flag;
26484be9762aSMarkus Armbruster     }
2649aea2a33cSMark McLoughlin 
2650aea2a33cSMark McLoughlin     return ret;
265119cb3738Sbellard }
265219cb3738Sbellard 
265319cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
265419cb3738Sbellard {
265519cb3738Sbellard     return bs->locked;
265619cb3738Sbellard }
265719cb3738Sbellard 
265819cb3738Sbellard /**
265919cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
266019cb3738Sbellard  * to eject it manually).
266119cb3738Sbellard  */
266219cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
266319cb3738Sbellard {
266419cb3738Sbellard     BlockDriver *drv = bs->drv;
266519cb3738Sbellard 
266619cb3738Sbellard     bs->locked = locked;
266719cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
266819cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
266919cb3738Sbellard     }
267019cb3738Sbellard }
2671985a03b0Sths 
2672985a03b0Sths /* needed for generic scsi interface */
2673985a03b0Sths 
2674985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2675985a03b0Sths {
2676985a03b0Sths     BlockDriver *drv = bs->drv;
2677985a03b0Sths 
2678985a03b0Sths     if (drv && drv->bdrv_ioctl)
2679985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
2680985a03b0Sths     return -ENOTSUP;
2681985a03b0Sths }
26827d780669Saliguori 
2683221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2684221f715dSaliguori         unsigned long int req, void *buf,
26857d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
26867d780669Saliguori {
2687221f715dSaliguori     BlockDriver *drv = bs->drv;
26887d780669Saliguori 
2689221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
2690221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2691221f715dSaliguori     return NULL;
26927d780669Saliguori }
2693e268ca52Saliguori 
26947cd1e32aSlirans@il.ibm.com 
26957cd1e32aSlirans@il.ibm.com 
2696e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
2697e268ca52Saliguori {
2698e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2699e268ca52Saliguori }
27007cd1e32aSlirans@il.ibm.com 
27017cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
27027cd1e32aSlirans@il.ibm.com {
27037cd1e32aSlirans@il.ibm.com     int64_t bitmap_size;
2704a55eb92cSJan Kiszka 
2705aaa0eb75SLiran Schour     bs->dirty_count = 0;
27067cd1e32aSlirans@il.ibm.com     if (enable) {
2707c6d22830SJan Kiszka         if (!bs->dirty_bitmap) {
2708c6d22830SJan Kiszka             bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2709c6d22830SJan Kiszka                     BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2710c6d22830SJan Kiszka             bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
27117cd1e32aSlirans@il.ibm.com 
27127cd1e32aSlirans@il.ibm.com             bs->dirty_bitmap = qemu_mallocz(bitmap_size);
27137cd1e32aSlirans@il.ibm.com         }
27147cd1e32aSlirans@il.ibm.com     } else {
2715c6d22830SJan Kiszka         if (bs->dirty_bitmap) {
27167cd1e32aSlirans@il.ibm.com             qemu_free(bs->dirty_bitmap);
2717c6d22830SJan Kiszka             bs->dirty_bitmap = NULL;
27187cd1e32aSlirans@il.ibm.com         }
27197cd1e32aSlirans@il.ibm.com     }
27207cd1e32aSlirans@il.ibm.com }
27217cd1e32aSlirans@il.ibm.com 
27227cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
27237cd1e32aSlirans@il.ibm.com {
27246ea44308SJan Kiszka     int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
27257cd1e32aSlirans@il.ibm.com 
2726c6d22830SJan Kiszka     if (bs->dirty_bitmap &&
2727c6d22830SJan Kiszka         (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
27286d59fec1SMarcelo Tosatti         return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
27296d59fec1SMarcelo Tosatti             (1UL << (chunk % (sizeof(unsigned long) * 8))));
27307cd1e32aSlirans@il.ibm.com     } else {
27317cd1e32aSlirans@il.ibm.com         return 0;
27327cd1e32aSlirans@il.ibm.com     }
27337cd1e32aSlirans@il.ibm.com }
27347cd1e32aSlirans@il.ibm.com 
27357cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
27367cd1e32aSlirans@il.ibm.com                       int nr_sectors)
27377cd1e32aSlirans@il.ibm.com {
27387cd1e32aSlirans@il.ibm.com     set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
27397cd1e32aSlirans@il.ibm.com }
2740aaa0eb75SLiran Schour 
2741aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2742aaa0eb75SLiran Schour {
2743aaa0eb75SLiran Schour     return bs->dirty_count;
2744aaa0eb75SLiran Schour }
2745