xref: /openbmc/qemu/block.c (revision 9ac228e02cf16202547e7025ef300369e0db7781)
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"
26376253ecSaliguori #include "monitor.h"
27ea2384d3Sbellard #include "block_int.h"
285efa9d5aSAnthony Liguori #include "module.h"
29d15e5465SLuiz Capitulino #include "qemu-objects.h"
30fc01f7e7Sbellard 
3171e72a19SJuan Quintela #ifdef CONFIG_BSD
327674e7bfSbellard #include <sys/types.h>
337674e7bfSbellard #include <sys/stat.h>
347674e7bfSbellard #include <sys/ioctl.h>
3572cf2d4fSBlue Swirl #include <sys/queue.h>
36c5e97233Sblueswir1 #ifndef __DragonFly__
377674e7bfSbellard #include <sys/disk.h>
387674e7bfSbellard #endif
39c5e97233Sblueswir1 #endif
407674e7bfSbellard 
4149dc768dSaliguori #ifdef _WIN32
4249dc768dSaliguori #include <windows.h>
4349dc768dSaliguori #endif
4449dc768dSaliguori 
45f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
47c87c0672Saliguori         BlockDriverCompletionFunc *cb, void *opaque);
48f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
50ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
51b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque);
53016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
54016f5cf6SAlexander Graf         BlockDriverCompletionFunc *cb, void *opaque);
5583f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
5683f64091Sbellard                         uint8_t *buf, int nb_sectors);
5783f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
5883f64091Sbellard                          const uint8_t *buf, int nb_sectors);
59ec530c81Sbellard 
601b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
611b7bdbc1SStefan Hajnoczi     QTAILQ_HEAD_INITIALIZER(bdrv_states);
627ee930d0Sblueswir1 
638a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers =
648a22f02aSStefan Hajnoczi     QLIST_HEAD_INITIALIZER(bdrv_drivers);
65ea2384d3Sbellard 
66f9092b10SMarkus Armbruster /* The device to use for VM snapshots */
67f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots;
68f9092b10SMarkus Armbruster 
69eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */
70eb852011SMarkus Armbruster static int use_bdrv_whitelist;
71eb852011SMarkus Armbruster 
7283f64091Sbellard int path_is_absolute(const char *path)
7383f64091Sbellard {
7483f64091Sbellard     const char *p;
7521664424Sbellard #ifdef _WIN32
7621664424Sbellard     /* specific case for names like: "\\.\d:" */
7721664424Sbellard     if (*path == '/' || *path == '\\')
7821664424Sbellard         return 1;
7921664424Sbellard #endif
8083f64091Sbellard     p = strchr(path, ':');
8183f64091Sbellard     if (p)
8283f64091Sbellard         p++;
8383f64091Sbellard     else
8483f64091Sbellard         p = path;
853b9f94e1Sbellard #ifdef _WIN32
863b9f94e1Sbellard     return (*p == '/' || *p == '\\');
873b9f94e1Sbellard #else
883b9f94e1Sbellard     return (*p == '/');
893b9f94e1Sbellard #endif
9083f64091Sbellard }
9183f64091Sbellard 
9283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
9383f64091Sbellard    path to it by considering it is relative to base_path. URL are
9483f64091Sbellard    supported. */
9583f64091Sbellard void path_combine(char *dest, int dest_size,
9683f64091Sbellard                   const char *base_path,
9783f64091Sbellard                   const char *filename)
9883f64091Sbellard {
9983f64091Sbellard     const char *p, *p1;
10083f64091Sbellard     int len;
10183f64091Sbellard 
10283f64091Sbellard     if (dest_size <= 0)
10383f64091Sbellard         return;
10483f64091Sbellard     if (path_is_absolute(filename)) {
10583f64091Sbellard         pstrcpy(dest, dest_size, filename);
10683f64091Sbellard     } else {
10783f64091Sbellard         p = strchr(base_path, ':');
10883f64091Sbellard         if (p)
10983f64091Sbellard             p++;
11083f64091Sbellard         else
11183f64091Sbellard             p = base_path;
1123b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1133b9f94e1Sbellard #ifdef _WIN32
1143b9f94e1Sbellard         {
1153b9f94e1Sbellard             const char *p2;
1163b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1173b9f94e1Sbellard             if (!p1 || p2 > p1)
1183b9f94e1Sbellard                 p1 = p2;
1193b9f94e1Sbellard         }
1203b9f94e1Sbellard #endif
12183f64091Sbellard         if (p1)
12283f64091Sbellard             p1++;
12383f64091Sbellard         else
12483f64091Sbellard             p1 = base_path;
12583f64091Sbellard         if (p1 > p)
12683f64091Sbellard             p = p1;
12783f64091Sbellard         len = p - base_path;
12883f64091Sbellard         if (len > dest_size - 1)
12983f64091Sbellard             len = dest_size - 1;
13083f64091Sbellard         memcpy(dest, base_path, len);
13183f64091Sbellard         dest[len] = '\0';
13283f64091Sbellard         pstrcat(dest, dest_size, filename);
13383f64091Sbellard     }
13483f64091Sbellard }
13583f64091Sbellard 
1365efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv)
137ea2384d3Sbellard {
138f141eafeSaliguori     if (!bdrv->bdrv_aio_readv) {
13983f64091Sbellard         /* add AIO emulation layer */
140f141eafeSaliguori         bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
141f141eafeSaliguori         bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
142eda578e5Saliguori     } else if (!bdrv->bdrv_read) {
14383f64091Sbellard         /* add synchronous IO emulation layer */
14483f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
14583f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
14683f64091Sbellard     }
147b2e12bc6SChristoph Hellwig 
148b2e12bc6SChristoph Hellwig     if (!bdrv->bdrv_aio_flush)
149b2e12bc6SChristoph Hellwig         bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
150b2e12bc6SChristoph Hellwig 
1518a22f02aSStefan Hajnoczi     QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
152ea2384d3Sbellard }
153b338082bSbellard 
154b338082bSbellard /* create a new block device (by default it is empty) */
155b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
156fc01f7e7Sbellard {
1571b7bdbc1SStefan Hajnoczi     BlockDriverState *bs;
158b338082bSbellard 
159b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
160b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
161ea2384d3Sbellard     if (device_name[0] != '\0') {
1621b7bdbc1SStefan Hajnoczi         QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
163ea2384d3Sbellard     }
164b338082bSbellard     return bs;
165b338082bSbellard }
166b338082bSbellard 
167ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
168ea2384d3Sbellard {
169ea2384d3Sbellard     BlockDriver *drv1;
1708a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
1718a22f02aSStefan Hajnoczi         if (!strcmp(drv1->format_name, format_name)) {
172ea2384d3Sbellard             return drv1;
173ea2384d3Sbellard         }
1748a22f02aSStefan Hajnoczi     }
175ea2384d3Sbellard     return NULL;
176ea2384d3Sbellard }
177ea2384d3Sbellard 
178eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv)
179eb852011SMarkus Armbruster {
180eb852011SMarkus Armbruster     static const char *whitelist[] = {
181eb852011SMarkus Armbruster         CONFIG_BDRV_WHITELIST
182eb852011SMarkus Armbruster     };
183eb852011SMarkus Armbruster     const char **p;
184eb852011SMarkus Armbruster 
185eb852011SMarkus Armbruster     if (!whitelist[0])
186eb852011SMarkus Armbruster         return 1;               /* no whitelist, anything goes */
187eb852011SMarkus Armbruster 
188eb852011SMarkus Armbruster     for (p = whitelist; *p; p++) {
189eb852011SMarkus Armbruster         if (!strcmp(drv->format_name, *p)) {
190eb852011SMarkus Armbruster             return 1;
191eb852011SMarkus Armbruster         }
192eb852011SMarkus Armbruster     }
193eb852011SMarkus Armbruster     return 0;
194eb852011SMarkus Armbruster }
195eb852011SMarkus Armbruster 
196eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
197eb852011SMarkus Armbruster {
198eb852011SMarkus Armbruster     BlockDriver *drv = bdrv_find_format(format_name);
199eb852011SMarkus Armbruster     return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
200eb852011SMarkus Armbruster }
201eb852011SMarkus Armbruster 
2020e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename,
2030e7e1989SKevin Wolf     QEMUOptionParameter *options)
204ea2384d3Sbellard {
205ea2384d3Sbellard     if (!drv->bdrv_create)
206ea2384d3Sbellard         return -ENOTSUP;
2070e7e1989SKevin Wolf 
2080e7e1989SKevin Wolf     return drv->bdrv_create(filename, options);
209ea2384d3Sbellard }
210ea2384d3Sbellard 
21184a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
21284a12e66SChristoph Hellwig {
21384a12e66SChristoph Hellwig     BlockDriver *drv;
21484a12e66SChristoph Hellwig 
215b50cbabcSMORITA Kazutaka     drv = bdrv_find_protocol(filename);
21684a12e66SChristoph Hellwig     if (drv == NULL) {
21784a12e66SChristoph Hellwig         drv = bdrv_find_format("file");
21884a12e66SChristoph Hellwig     }
21984a12e66SChristoph Hellwig 
22084a12e66SChristoph Hellwig     return bdrv_create(drv, filename, options);
22184a12e66SChristoph Hellwig }
22284a12e66SChristoph Hellwig 
223d5249393Sbellard #ifdef _WIN32
22495389c86Sbellard void get_tmp_filename(char *filename, int size)
225d5249393Sbellard {
2263b9f94e1Sbellard     char temp_dir[MAX_PATH];
2273b9f94e1Sbellard 
2283b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
2293b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
230d5249393Sbellard }
231d5249393Sbellard #else
23295389c86Sbellard void get_tmp_filename(char *filename, int size)
233ea2384d3Sbellard {
234ea2384d3Sbellard     int fd;
2357ccfb2ebSblueswir1     const char *tmpdir;
236d5249393Sbellard     /* XXX: race condition possible */
2370badc1eeSaurel32     tmpdir = getenv("TMPDIR");
2380badc1eeSaurel32     if (!tmpdir)
2390badc1eeSaurel32         tmpdir = "/tmp";
2400badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
241ea2384d3Sbellard     fd = mkstemp(filename);
242ea2384d3Sbellard     close(fd);
243ea2384d3Sbellard }
244d5249393Sbellard #endif
245ea2384d3Sbellard 
24619cb3738Sbellard #ifdef _WIN32
247f45512feSbellard static int is_windows_drive_prefix(const char *filename)
248f45512feSbellard {
249f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
250f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
251f45512feSbellard             filename[1] == ':');
252f45512feSbellard }
253f45512feSbellard 
254508c7cb3SChristoph Hellwig int is_windows_drive(const char *filename)
25519cb3738Sbellard {
256f45512feSbellard     if (is_windows_drive_prefix(filename) &&
257f45512feSbellard         filename[2] == '\0')
25819cb3738Sbellard         return 1;
25919cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
26019cb3738Sbellard         strstart(filename, "//./", NULL))
26119cb3738Sbellard         return 1;
26219cb3738Sbellard     return 0;
26319cb3738Sbellard }
26419cb3738Sbellard #endif
26519cb3738Sbellard 
266f3a5d3f8SChristoph Hellwig /*
267f3a5d3f8SChristoph Hellwig  * Detect host devices. By convention, /dev/cdrom[N] is always
268f3a5d3f8SChristoph Hellwig  * recognized as a host CDROM.
269f3a5d3f8SChristoph Hellwig  */
270f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename)
271f3a5d3f8SChristoph Hellwig {
272508c7cb3SChristoph Hellwig     int score_max = 0, score;
273508c7cb3SChristoph Hellwig     BlockDriver *drv = NULL, *d;
274f3a5d3f8SChristoph Hellwig 
2758a22f02aSStefan Hajnoczi     QLIST_FOREACH(d, &bdrv_drivers, list) {
276508c7cb3SChristoph Hellwig         if (d->bdrv_probe_device) {
277508c7cb3SChristoph Hellwig             score = d->bdrv_probe_device(filename);
278508c7cb3SChristoph Hellwig             if (score > score_max) {
279508c7cb3SChristoph Hellwig                 score_max = score;
280508c7cb3SChristoph Hellwig                 drv = d;
281f3a5d3f8SChristoph Hellwig             }
282508c7cb3SChristoph Hellwig         }
283f3a5d3f8SChristoph Hellwig     }
284f3a5d3f8SChristoph Hellwig 
285508c7cb3SChristoph Hellwig     return drv;
286f3a5d3f8SChristoph Hellwig }
287f3a5d3f8SChristoph Hellwig 
288b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename)
28984a12e66SChristoph Hellwig {
29084a12e66SChristoph Hellwig     BlockDriver *drv1;
29184a12e66SChristoph Hellwig     char protocol[128];
29284a12e66SChristoph Hellwig     int len;
29384a12e66SChristoph Hellwig     const char *p;
29484a12e66SChristoph Hellwig 
29566f82ceeSKevin Wolf     /* TODO Drivers without bdrv_file_open must be specified explicitly */
29666f82ceeSKevin Wolf 
29739508e7aSChristoph Hellwig     /*
29839508e7aSChristoph Hellwig      * XXX(hch): we really should not let host device detection
29939508e7aSChristoph Hellwig      * override an explicit protocol specification, but moving this
30039508e7aSChristoph Hellwig      * later breaks access to device names with colons in them.
30139508e7aSChristoph Hellwig      * Thanks to the brain-dead persistent naming schemes on udev-
30239508e7aSChristoph Hellwig      * based Linux systems those actually are quite common.
30339508e7aSChristoph Hellwig      */
30484a12e66SChristoph Hellwig     drv1 = find_hdev_driver(filename);
30539508e7aSChristoph Hellwig     if (drv1) {
30684a12e66SChristoph Hellwig         return drv1;
30784a12e66SChristoph Hellwig     }
30839508e7aSChristoph Hellwig 
30939508e7aSChristoph Hellwig #ifdef _WIN32
31039508e7aSChristoph Hellwig      if (is_windows_drive(filename) ||
31139508e7aSChristoph Hellwig          is_windows_drive_prefix(filename))
31239508e7aSChristoph Hellwig          return bdrv_find_format("file");
31339508e7aSChristoph Hellwig #endif
31439508e7aSChristoph Hellwig 
31539508e7aSChristoph Hellwig     p = strchr(filename, ':');
31639508e7aSChristoph Hellwig     if (!p) {
31739508e7aSChristoph Hellwig         return bdrv_find_format("file");
31839508e7aSChristoph Hellwig     }
31984a12e66SChristoph Hellwig     len = p - filename;
32084a12e66SChristoph Hellwig     if (len > sizeof(protocol) - 1)
32184a12e66SChristoph Hellwig         len = sizeof(protocol) - 1;
32284a12e66SChristoph Hellwig     memcpy(protocol, filename, len);
32384a12e66SChristoph Hellwig     protocol[len] = '\0';
32484a12e66SChristoph Hellwig     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
32584a12e66SChristoph Hellwig         if (drv1->protocol_name &&
32684a12e66SChristoph Hellwig             !strcmp(drv1->protocol_name, protocol)) {
32784a12e66SChristoph Hellwig             return drv1;
32884a12e66SChristoph Hellwig         }
32984a12e66SChristoph Hellwig     }
33084a12e66SChristoph Hellwig     return NULL;
33184a12e66SChristoph Hellwig }
33284a12e66SChristoph Hellwig 
333ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
334ea2384d3Sbellard {
33583f64091Sbellard     int ret, score, score_max;
336ea2384d3Sbellard     BlockDriver *drv1, *drv;
33783f64091Sbellard     uint8_t buf[2048];
33883f64091Sbellard     BlockDriverState *bs;
339ea2384d3Sbellard 
340f5edb014SNaphtali Sprei     ret = bdrv_file_open(&bs, filename, 0);
34183f64091Sbellard     if (ret < 0)
3427674e7bfSbellard         return NULL;
343f8ea0b00SNicholas Bellinger 
34408a00559SKevin Wolf     /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
34508a00559SKevin Wolf     if (bs->sg || !bdrv_is_inserted(bs)) {
3461a396859SNicholas A. Bellinger         bdrv_delete(bs);
347f8ea0b00SNicholas Bellinger         return bdrv_find_format("raw");
3481a396859SNicholas A. Bellinger     }
349f8ea0b00SNicholas Bellinger 
35083f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
35183f64091Sbellard     bdrv_delete(bs);
352ea2384d3Sbellard     if (ret < 0) {
353ea2384d3Sbellard         return NULL;
354ea2384d3Sbellard     }
355ea2384d3Sbellard 
356ea2384d3Sbellard     score_max = 0;
35784a12e66SChristoph Hellwig     drv = NULL;
3588a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
35983f64091Sbellard         if (drv1->bdrv_probe) {
360ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
361ea2384d3Sbellard             if (score > score_max) {
362ea2384d3Sbellard                 score_max = score;
363ea2384d3Sbellard                 drv = drv1;
364ea2384d3Sbellard             }
365ea2384d3Sbellard         }
36683f64091Sbellard     }
367ea2384d3Sbellard     return drv;
368ea2384d3Sbellard }
369ea2384d3Sbellard 
37051762288SStefan Hajnoczi /**
37151762288SStefan Hajnoczi  * Set the current 'total_sectors' value
37251762288SStefan Hajnoczi  */
37351762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
37451762288SStefan Hajnoczi {
37551762288SStefan Hajnoczi     BlockDriver *drv = bs->drv;
37651762288SStefan Hajnoczi 
377396759adSNicholas Bellinger     /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
378396759adSNicholas Bellinger     if (bs->sg)
379396759adSNicholas Bellinger         return 0;
380396759adSNicholas Bellinger 
38151762288SStefan Hajnoczi     /* query actual device if possible, otherwise just trust the hint */
38251762288SStefan Hajnoczi     if (drv->bdrv_getlength) {
38351762288SStefan Hajnoczi         int64_t length = drv->bdrv_getlength(bs);
38451762288SStefan Hajnoczi         if (length < 0) {
38551762288SStefan Hajnoczi             return length;
38651762288SStefan Hajnoczi         }
38751762288SStefan Hajnoczi         hint = length >> BDRV_SECTOR_BITS;
38851762288SStefan Hajnoczi     }
38951762288SStefan Hajnoczi 
39051762288SStefan Hajnoczi     bs->total_sectors = hint;
39151762288SStefan Hajnoczi     return 0;
39251762288SStefan Hajnoczi }
39351762288SStefan Hajnoczi 
394b6ce07aaSKevin Wolf /*
39557915332SKevin Wolf  * Common part for opening disk images and files
39657915332SKevin Wolf  */
39757915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename,
39857915332SKevin Wolf     int flags, BlockDriver *drv)
39957915332SKevin Wolf {
40057915332SKevin Wolf     int ret, open_flags;
40157915332SKevin Wolf 
40257915332SKevin Wolf     assert(drv != NULL);
40357915332SKevin Wolf 
40466f82ceeSKevin Wolf     bs->file = NULL;
40551762288SStefan Hajnoczi     bs->total_sectors = 0;
40657915332SKevin Wolf     bs->encrypted = 0;
40757915332SKevin Wolf     bs->valid_key = 0;
40857915332SKevin Wolf     bs->open_flags = flags;
40957915332SKevin Wolf     /* buffer_alignment defaulted to 512, drivers can change this value */
41057915332SKevin Wolf     bs->buffer_alignment = 512;
41157915332SKevin Wolf 
41257915332SKevin Wolf     pstrcpy(bs->filename, sizeof(bs->filename), filename);
41357915332SKevin Wolf 
41457915332SKevin Wolf     if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
41557915332SKevin Wolf         return -ENOTSUP;
41657915332SKevin Wolf     }
41757915332SKevin Wolf 
41857915332SKevin Wolf     bs->drv = drv;
41957915332SKevin Wolf     bs->opaque = qemu_mallocz(drv->instance_size);
42057915332SKevin Wolf 
42157915332SKevin Wolf     /*
42257915332SKevin Wolf      * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
42357915332SKevin Wolf      * write cache to the guest.  We do need the fdatasync to flush
42457915332SKevin Wolf      * out transactions for block allocations, and we maybe have a
42557915332SKevin Wolf      * volatile write cache in our backing device to deal with.
42657915332SKevin Wolf      */
42757915332SKevin Wolf     if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
42857915332SKevin Wolf         bs->enable_write_cache = 1;
42957915332SKevin Wolf 
43057915332SKevin Wolf     /*
43157915332SKevin Wolf      * Clear flags that are internal to the block layer before opening the
43257915332SKevin Wolf      * image.
43357915332SKevin Wolf      */
43457915332SKevin Wolf     open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
43557915332SKevin Wolf 
43657915332SKevin Wolf     /*
43757915332SKevin Wolf      * Snapshots should be writeable.
43857915332SKevin Wolf      */
43957915332SKevin Wolf     if (bs->is_temporary) {
44057915332SKevin Wolf         open_flags |= BDRV_O_RDWR;
44157915332SKevin Wolf     }
44257915332SKevin Wolf 
44366f82ceeSKevin Wolf     /* Open the image, either directly or using a protocol */
44466f82ceeSKevin Wolf     if (drv->bdrv_file_open) {
44566f82ceeSKevin Wolf         ret = drv->bdrv_file_open(bs, filename, open_flags);
44666f82ceeSKevin Wolf     } else {
44766f82ceeSKevin Wolf         ret = bdrv_file_open(&bs->file, filename, open_flags);
44866f82ceeSKevin Wolf         if (ret >= 0) {
44966f82ceeSKevin Wolf             ret = drv->bdrv_open(bs, open_flags);
45066f82ceeSKevin Wolf         }
45166f82ceeSKevin Wolf     }
45266f82ceeSKevin Wolf 
45357915332SKevin Wolf     if (ret < 0) {
45457915332SKevin Wolf         goto free_and_fail;
45557915332SKevin Wolf     }
45657915332SKevin Wolf 
45757915332SKevin Wolf     bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
45851762288SStefan Hajnoczi 
45951762288SStefan Hajnoczi     ret = refresh_total_sectors(bs, bs->total_sectors);
46051762288SStefan Hajnoczi     if (ret < 0) {
46151762288SStefan Hajnoczi         goto free_and_fail;
46257915332SKevin Wolf     }
46351762288SStefan Hajnoczi 
46457915332SKevin Wolf #ifndef _WIN32
46557915332SKevin Wolf     if (bs->is_temporary) {
46657915332SKevin Wolf         unlink(filename);
46757915332SKevin Wolf     }
46857915332SKevin Wolf #endif
46957915332SKevin Wolf     return 0;
47057915332SKevin Wolf 
47157915332SKevin Wolf free_and_fail:
47266f82ceeSKevin Wolf     if (bs->file) {
47366f82ceeSKevin Wolf         bdrv_delete(bs->file);
47466f82ceeSKevin Wolf         bs->file = NULL;
47566f82ceeSKevin Wolf     }
47657915332SKevin Wolf     qemu_free(bs->opaque);
47757915332SKevin Wolf     bs->opaque = NULL;
47857915332SKevin Wolf     bs->drv = NULL;
47957915332SKevin Wolf     return ret;
48057915332SKevin Wolf }
48157915332SKevin Wolf 
48257915332SKevin Wolf /*
483b6ce07aaSKevin Wolf  * Opens a file using a protocol (file, host_device, nbd, ...)
484b6ce07aaSKevin Wolf  */
48583f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
486b338082bSbellard {
48783f64091Sbellard     BlockDriverState *bs;
4886db95603SChristoph Hellwig     BlockDriver *drv;
48983f64091Sbellard     int ret;
4903b0d4f61Sbellard 
491b50cbabcSMORITA Kazutaka     drv = bdrv_find_protocol(filename);
4926db95603SChristoph Hellwig     if (!drv) {
4936db95603SChristoph Hellwig         return -ENOENT;
4946db95603SChristoph Hellwig     }
4956db95603SChristoph Hellwig 
49683f64091Sbellard     bs = bdrv_new("");
497b6ce07aaSKevin Wolf     ret = bdrv_open_common(bs, filename, flags, drv);
49883f64091Sbellard     if (ret < 0) {
49983f64091Sbellard         bdrv_delete(bs);
50083f64091Sbellard         return ret;
5013b0d4f61Sbellard     }
50271d0770cSaliguori     bs->growable = 1;
50383f64091Sbellard     *pbs = bs;
50483f64091Sbellard     return 0;
5053b0d4f61Sbellard }
5063b0d4f61Sbellard 
507b6ce07aaSKevin Wolf /*
508b6ce07aaSKevin Wolf  * Opens a disk image (raw, qcow2, vmdk, ...)
509b6ce07aaSKevin Wolf  */
510d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
511ea2384d3Sbellard               BlockDriver *drv)
512ea2384d3Sbellard {
513b6ce07aaSKevin Wolf     int ret;
51433e3963eSbellard 
51583f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
516ea2384d3Sbellard         BlockDriverState *bs1;
517ea2384d3Sbellard         int64_t total_size;
5187c96d46eSaliguori         int is_protocol = 0;
51991a073a9SKevin Wolf         BlockDriver *bdrv_qcow2;
52091a073a9SKevin Wolf         QEMUOptionParameter *options;
521b6ce07aaSKevin Wolf         char tmp_filename[PATH_MAX];
522b6ce07aaSKevin Wolf         char backing_filename[PATH_MAX];
52333e3963eSbellard 
524ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
525ea2384d3Sbellard            instead of opening 'filename' directly */
526ea2384d3Sbellard 
527ea2384d3Sbellard         /* if there is a backing file, use it */
528ea2384d3Sbellard         bs1 = bdrv_new("");
529d6e9098eSKevin Wolf         ret = bdrv_open(bs1, filename, 0, drv);
53051d7c00cSaliguori         if (ret < 0) {
531ea2384d3Sbellard             bdrv_delete(bs1);
53251d7c00cSaliguori             return ret;
533ea2384d3Sbellard         }
5343e82990bSJes Sorensen         total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
5357c96d46eSaliguori 
5367c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
5377c96d46eSaliguori             is_protocol = 1;
5387c96d46eSaliguori 
539ea2384d3Sbellard         bdrv_delete(bs1);
540ea2384d3Sbellard 
541ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
5427c96d46eSaliguori 
5437c96d46eSaliguori         /* Real path is meaningless for protocols */
5447c96d46eSaliguori         if (is_protocol)
5457c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
5467c96d46eSaliguori                      "%s", filename);
547114cdfa9SKirill A. Shutemov         else if (!realpath(filename, backing_filename))
548114cdfa9SKirill A. Shutemov             return -errno;
5497c96d46eSaliguori 
55091a073a9SKevin Wolf         bdrv_qcow2 = bdrv_find_format("qcow2");
55191a073a9SKevin Wolf         options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
55291a073a9SKevin Wolf 
5533e82990bSJes Sorensen         set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
55491a073a9SKevin Wolf         set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
55591a073a9SKevin Wolf         if (drv) {
55691a073a9SKevin Wolf             set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
55791a073a9SKevin Wolf                 drv->format_name);
55891a073a9SKevin Wolf         }
55991a073a9SKevin Wolf 
56091a073a9SKevin Wolf         ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
561d748768cSJan Kiszka         free_option_parameters(options);
56251d7c00cSaliguori         if (ret < 0) {
56351d7c00cSaliguori             return ret;
564ea2384d3Sbellard         }
56591a073a9SKevin Wolf 
566ea2384d3Sbellard         filename = tmp_filename;
56791a073a9SKevin Wolf         drv = bdrv_qcow2;
568ea2384d3Sbellard         bs->is_temporary = 1;
569ea2384d3Sbellard     }
570ea2384d3Sbellard 
571b6ce07aaSKevin Wolf     /* Find the right image format driver */
5726db95603SChristoph Hellwig     if (!drv) {
573ea2384d3Sbellard         drv = find_image_format(filename);
574ea2384d3Sbellard     }
5756987307cSChristoph Hellwig 
57651d7c00cSaliguori     if (!drv) {
57751d7c00cSaliguori         ret = -ENOENT;
57851d7c00cSaliguori         goto unlink_and_fail;
57983f64091Sbellard     }
580b6ce07aaSKevin Wolf 
581b6ce07aaSKevin Wolf     /* Open the image */
582b6ce07aaSKevin Wolf     ret = bdrv_open_common(bs, filename, flags, drv);
583b6ce07aaSKevin Wolf     if (ret < 0) {
5846987307cSChristoph Hellwig         goto unlink_and_fail;
5856987307cSChristoph Hellwig     }
5866987307cSChristoph Hellwig 
587b6ce07aaSKevin Wolf     /* If there is a backing file, use it */
588b6ce07aaSKevin Wolf     if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
589b6ce07aaSKevin Wolf         char backing_filename[PATH_MAX];
590b6ce07aaSKevin Wolf         int back_flags;
591b6ce07aaSKevin Wolf         BlockDriver *back_drv = NULL;
592b6ce07aaSKevin Wolf 
593b6ce07aaSKevin Wolf         bs->backing_hd = bdrv_new("");
594b6ce07aaSKevin Wolf         path_combine(backing_filename, sizeof(backing_filename),
595b6ce07aaSKevin Wolf                      filename, bs->backing_file);
596b6ce07aaSKevin Wolf         if (bs->backing_format[0] != '\0')
597b6ce07aaSKevin Wolf             back_drv = bdrv_find_format(bs->backing_format);
598b6ce07aaSKevin Wolf 
599b6ce07aaSKevin Wolf         /* backing files always opened read-only */
600b6ce07aaSKevin Wolf         back_flags =
601b6ce07aaSKevin Wolf             flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
602b6ce07aaSKevin Wolf 
603b6ce07aaSKevin Wolf         ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
604b6ce07aaSKevin Wolf         if (ret < 0) {
605b6ce07aaSKevin Wolf             bdrv_close(bs);
606b6ce07aaSKevin Wolf             return ret;
607b6ce07aaSKevin Wolf         }
608b6ce07aaSKevin Wolf         if (bs->is_temporary) {
609b6ce07aaSKevin Wolf             bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
610b6ce07aaSKevin Wolf         } else {
611b6ce07aaSKevin Wolf             /* base image inherits from "parent" */
612b6ce07aaSKevin Wolf             bs->backing_hd->keep_read_only = bs->keep_read_only;
613b6ce07aaSKevin Wolf         }
614b6ce07aaSKevin Wolf     }
615b6ce07aaSKevin Wolf 
616b6ce07aaSKevin Wolf     if (!bdrv_key_required(bs)) {
617b6ce07aaSKevin Wolf         /* call the change callback */
618b6ce07aaSKevin Wolf         bs->media_changed = 1;
619b6ce07aaSKevin Wolf         if (bs->change_cb)
620b6ce07aaSKevin Wolf             bs->change_cb(bs->change_opaque);
621b6ce07aaSKevin Wolf     }
622b6ce07aaSKevin Wolf 
623b6ce07aaSKevin Wolf     return 0;
624b6ce07aaSKevin Wolf 
625b6ce07aaSKevin Wolf unlink_and_fail:
626b6ce07aaSKevin Wolf     if (bs->is_temporary) {
627b6ce07aaSKevin Wolf         unlink(filename);
628b6ce07aaSKevin Wolf     }
629b6ce07aaSKevin Wolf     return ret;
630b6ce07aaSKevin Wolf }
631b6ce07aaSKevin Wolf 
632fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
633fc01f7e7Sbellard {
63419cb3738Sbellard     if (bs->drv) {
635f9092b10SMarkus Armbruster         if (bs == bs_snapshots) {
636f9092b10SMarkus Armbruster             bs_snapshots = NULL;
637f9092b10SMarkus Armbruster         }
638557df6acSStefan Hajnoczi         if (bs->backing_hd) {
639ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
640557df6acSStefan Hajnoczi             bs->backing_hd = NULL;
641557df6acSStefan Hajnoczi         }
642ea2384d3Sbellard         bs->drv->bdrv_close(bs);
643ea2384d3Sbellard         qemu_free(bs->opaque);
644ea2384d3Sbellard #ifdef _WIN32
645ea2384d3Sbellard         if (bs->is_temporary) {
646ea2384d3Sbellard             unlink(bs->filename);
647ea2384d3Sbellard         }
64867b915a5Sbellard #endif
649ea2384d3Sbellard         bs->opaque = NULL;
650ea2384d3Sbellard         bs->drv = NULL;
651b338082bSbellard 
65266f82ceeSKevin Wolf         if (bs->file != NULL) {
65366f82ceeSKevin Wolf             bdrv_close(bs->file);
65466f82ceeSKevin Wolf         }
65566f82ceeSKevin Wolf 
656b338082bSbellard         /* call the change callback */
65719cb3738Sbellard         bs->media_changed = 1;
658b338082bSbellard         if (bs->change_cb)
659b338082bSbellard             bs->change_cb(bs->change_opaque);
660b338082bSbellard     }
661b338082bSbellard }
662b338082bSbellard 
6632bc93fedSMORITA Kazutaka void bdrv_close_all(void)
6642bc93fedSMORITA Kazutaka {
6652bc93fedSMORITA Kazutaka     BlockDriverState *bs;
6662bc93fedSMORITA Kazutaka 
6672bc93fedSMORITA Kazutaka     QTAILQ_FOREACH(bs, &bdrv_states, list) {
6682bc93fedSMORITA Kazutaka         bdrv_close(bs);
6692bc93fedSMORITA Kazutaka     }
6702bc93fedSMORITA Kazutaka }
6712bc93fedSMORITA Kazutaka 
672b338082bSbellard void bdrv_delete(BlockDriverState *bs)
673b338082bSbellard {
67418846deeSMarkus Armbruster     assert(!bs->peer);
67518846deeSMarkus Armbruster 
6761b7bdbc1SStefan Hajnoczi     /* remove from list, if necessary */
6771b7bdbc1SStefan Hajnoczi     if (bs->device_name[0] != '\0') {
6781b7bdbc1SStefan Hajnoczi         QTAILQ_REMOVE(&bdrv_states, bs, list);
6791b7bdbc1SStefan Hajnoczi     }
68034c6f050Saurel32 
681b338082bSbellard     bdrv_close(bs);
68266f82ceeSKevin Wolf     if (bs->file != NULL) {
68366f82ceeSKevin Wolf         bdrv_delete(bs->file);
68466f82ceeSKevin Wolf     }
68566f82ceeSKevin Wolf 
686f9092b10SMarkus Armbruster     assert(bs != bs_snapshots);
687b338082bSbellard     qemu_free(bs);
688fc01f7e7Sbellard }
689fc01f7e7Sbellard 
69018846deeSMarkus Armbruster int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
69118846deeSMarkus Armbruster {
69218846deeSMarkus Armbruster     if (bs->peer) {
69318846deeSMarkus Armbruster         return -EBUSY;
69418846deeSMarkus Armbruster     }
69518846deeSMarkus Armbruster     bs->peer = qdev;
69618846deeSMarkus Armbruster     return 0;
69718846deeSMarkus Armbruster }
69818846deeSMarkus Armbruster 
69918846deeSMarkus Armbruster void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
70018846deeSMarkus Armbruster {
70118846deeSMarkus Armbruster     assert(bs->peer == qdev);
70218846deeSMarkus Armbruster     bs->peer = NULL;
70318846deeSMarkus Armbruster }
70418846deeSMarkus Armbruster 
70518846deeSMarkus Armbruster DeviceState *bdrv_get_attached(BlockDriverState *bs)
70618846deeSMarkus Armbruster {
70718846deeSMarkus Armbruster     return bs->peer;
70818846deeSMarkus Armbruster }
70918846deeSMarkus Armbruster 
710e97fc193Saliguori /*
711e97fc193Saliguori  * Run consistency checks on an image
712e97fc193Saliguori  *
713e076f338SKevin Wolf  * Returns 0 if the check could be completed (it doesn't mean that the image is
714e076f338SKevin Wolf  * free of errors) or -errno when an internal error occured. The results of the
715e076f338SKevin Wolf  * check are stored in res.
716e97fc193Saliguori  */
717e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
718e97fc193Saliguori {
719e97fc193Saliguori     if (bs->drv->bdrv_check == NULL) {
720e97fc193Saliguori         return -ENOTSUP;
721e97fc193Saliguori     }
722e97fc193Saliguori 
723e076f338SKevin Wolf     memset(res, 0, sizeof(*res));
724*9ac228e0SKevin Wolf     return bs->drv->bdrv_check(bs, res);
725e97fc193Saliguori }
726e97fc193Saliguori 
72733e3963eSbellard /* commit COW file into the raw image */
72833e3963eSbellard int bdrv_commit(BlockDriverState *bs)
72933e3963eSbellard {
73019cb3738Sbellard     BlockDriver *drv = bs->drv;
73183f64091Sbellard     int64_t i, total_sectors;
7324dca4b63SNaphtali Sprei     int n, j, ro, open_flags;
7334dca4b63SNaphtali Sprei     int ret = 0, rw_ret = 0;
734eb5a3165SJes Sorensen     unsigned char sector[BDRV_SECTOR_SIZE];
7354dca4b63SNaphtali Sprei     char filename[1024];
7364dca4b63SNaphtali Sprei     BlockDriverState *bs_rw, *bs_ro;
73733e3963eSbellard 
73819cb3738Sbellard     if (!drv)
73919cb3738Sbellard         return -ENOMEDIUM;
74033e3963eSbellard 
7414dca4b63SNaphtali Sprei     if (!bs->backing_hd) {
7424dca4b63SNaphtali Sprei         return -ENOTSUP;
7434dca4b63SNaphtali Sprei     }
7444dca4b63SNaphtali Sprei 
7454dca4b63SNaphtali Sprei     if (bs->backing_hd->keep_read_only) {
746ea2384d3Sbellard         return -EACCES;
74733e3963eSbellard     }
74833e3963eSbellard 
7494dca4b63SNaphtali Sprei     ro = bs->backing_hd->read_only;
7504dca4b63SNaphtali Sprei     strncpy(filename, bs->backing_hd->filename, sizeof(filename));
7514dca4b63SNaphtali Sprei     open_flags =  bs->backing_hd->open_flags;
7524dca4b63SNaphtali Sprei 
7534dca4b63SNaphtali Sprei     if (ro) {
7544dca4b63SNaphtali Sprei         /* re-open as RW */
7554dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
7564dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
7574dca4b63SNaphtali Sprei         bs_rw = bdrv_new("");
758c3349197SKevin Wolf         rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
7594dca4b63SNaphtali Sprei         if (rw_ret < 0) {
7604dca4b63SNaphtali Sprei             bdrv_delete(bs_rw);
7614dca4b63SNaphtali Sprei             /* try to re-open read-only */
7624dca4b63SNaphtali Sprei             bs_ro = bdrv_new("");
763c3349197SKevin Wolf             ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
7644dca4b63SNaphtali Sprei             if (ret < 0) {
7654dca4b63SNaphtali Sprei                 bdrv_delete(bs_ro);
7664dca4b63SNaphtali Sprei                 /* drive not functional anymore */
7674dca4b63SNaphtali Sprei                 bs->drv = NULL;
7684dca4b63SNaphtali Sprei                 return ret;
7694dca4b63SNaphtali Sprei             }
7704dca4b63SNaphtali Sprei             bs->backing_hd = bs_ro;
7714dca4b63SNaphtali Sprei             return rw_ret;
7724dca4b63SNaphtali Sprei         }
7734dca4b63SNaphtali Sprei         bs->backing_hd = bs_rw;
774ea2384d3Sbellard     }
775ea2384d3Sbellard 
7766ea44308SJan Kiszka     total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
77783f64091Sbellard     for (i = 0; i < total_sectors;) {
77819cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
779ea2384d3Sbellard             for(j = 0; j < n; j++) {
78033e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
7814dca4b63SNaphtali Sprei                     ret = -EIO;
7824dca4b63SNaphtali Sprei                     goto ro_cleanup;
78333e3963eSbellard                 }
78433e3963eSbellard 
785ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
7864dca4b63SNaphtali Sprei                     ret = -EIO;
7874dca4b63SNaphtali Sprei                     goto ro_cleanup;
78833e3963eSbellard                 }
789ea2384d3Sbellard                 i++;
790ea2384d3Sbellard 	    }
791ea2384d3Sbellard 	} else {
792ea2384d3Sbellard             i += n;
79333e3963eSbellard         }
79433e3963eSbellard     }
79595389c86Sbellard 
7961d44952fSChristoph Hellwig     if (drv->bdrv_make_empty) {
7971d44952fSChristoph Hellwig         ret = drv->bdrv_make_empty(bs);
7981d44952fSChristoph Hellwig         bdrv_flush(bs);
7991d44952fSChristoph Hellwig     }
80095389c86Sbellard 
8013f5075aeSChristoph Hellwig     /*
8023f5075aeSChristoph Hellwig      * Make sure all data we wrote to the backing device is actually
8033f5075aeSChristoph Hellwig      * stable on disk.
8043f5075aeSChristoph Hellwig      */
8053f5075aeSChristoph Hellwig     if (bs->backing_hd)
8063f5075aeSChristoph Hellwig         bdrv_flush(bs->backing_hd);
8074dca4b63SNaphtali Sprei 
8084dca4b63SNaphtali Sprei ro_cleanup:
8094dca4b63SNaphtali Sprei 
8104dca4b63SNaphtali Sprei     if (ro) {
8114dca4b63SNaphtali Sprei         /* re-open as RO */
8124dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
8134dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
8144dca4b63SNaphtali Sprei         bs_ro = bdrv_new("");
815c3349197SKevin Wolf         ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
8164dca4b63SNaphtali Sprei         if (ret < 0) {
8174dca4b63SNaphtali Sprei             bdrv_delete(bs_ro);
8184dca4b63SNaphtali Sprei             /* drive not functional anymore */
8194dca4b63SNaphtali Sprei             bs->drv = NULL;
8204dca4b63SNaphtali Sprei             return ret;
8214dca4b63SNaphtali Sprei         }
8224dca4b63SNaphtali Sprei         bs->backing_hd = bs_ro;
8234dca4b63SNaphtali Sprei         bs->backing_hd->keep_read_only = 0;
8244dca4b63SNaphtali Sprei     }
8254dca4b63SNaphtali Sprei 
8261d44952fSChristoph Hellwig     return ret;
82733e3963eSbellard }
82833e3963eSbellard 
8296ab4b5abSMarkus Armbruster void bdrv_commit_all(void)
8306ab4b5abSMarkus Armbruster {
8316ab4b5abSMarkus Armbruster     BlockDriverState *bs;
8326ab4b5abSMarkus Armbruster 
8336ab4b5abSMarkus Armbruster     QTAILQ_FOREACH(bs, &bdrv_states, list) {
8346ab4b5abSMarkus Armbruster         bdrv_commit(bs);
8356ab4b5abSMarkus Armbruster     }
8366ab4b5abSMarkus Armbruster }
8376ab4b5abSMarkus Armbruster 
838756e6736SKevin Wolf /*
839756e6736SKevin Wolf  * Return values:
840756e6736SKevin Wolf  * 0        - success
841756e6736SKevin Wolf  * -EINVAL  - backing format specified, but no file
842756e6736SKevin Wolf  * -ENOSPC  - can't update the backing file because no space is left in the
843756e6736SKevin Wolf  *            image file header
844756e6736SKevin Wolf  * -ENOTSUP - format driver doesn't support changing the backing file
845756e6736SKevin Wolf  */
846756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs,
847756e6736SKevin Wolf     const char *backing_file, const char *backing_fmt)
848756e6736SKevin Wolf {
849756e6736SKevin Wolf     BlockDriver *drv = bs->drv;
850756e6736SKevin Wolf 
851756e6736SKevin Wolf     if (drv->bdrv_change_backing_file != NULL) {
852756e6736SKevin Wolf         return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
853756e6736SKevin Wolf     } else {
854756e6736SKevin Wolf         return -ENOTSUP;
855756e6736SKevin Wolf     }
856756e6736SKevin Wolf }
857756e6736SKevin Wolf 
85871d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
85971d0770cSaliguori                                    size_t size)
86071d0770cSaliguori {
86171d0770cSaliguori     int64_t len;
86271d0770cSaliguori 
86371d0770cSaliguori     if (!bdrv_is_inserted(bs))
86471d0770cSaliguori         return -ENOMEDIUM;
86571d0770cSaliguori 
86671d0770cSaliguori     if (bs->growable)
86771d0770cSaliguori         return 0;
86871d0770cSaliguori 
86971d0770cSaliguori     len = bdrv_getlength(bs);
87071d0770cSaliguori 
871fbb7b4e0SKevin Wolf     if (offset < 0)
872fbb7b4e0SKevin Wolf         return -EIO;
873fbb7b4e0SKevin Wolf 
874fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
87571d0770cSaliguori         return -EIO;
87671d0770cSaliguori 
87771d0770cSaliguori     return 0;
87871d0770cSaliguori }
87971d0770cSaliguori 
88071d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
88171d0770cSaliguori                               int nb_sectors)
88271d0770cSaliguori {
883eb5a3165SJes Sorensen     return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
884eb5a3165SJes Sorensen                                    nb_sectors * BDRV_SECTOR_SIZE);
88571d0770cSaliguori }
88671d0770cSaliguori 
88719cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
888fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
889fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
890fc01f7e7Sbellard {
891ea2384d3Sbellard     BlockDriver *drv = bs->drv;
892fc01f7e7Sbellard 
89319cb3738Sbellard     if (!drv)
89419cb3738Sbellard         return -ENOMEDIUM;
89571d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
89671d0770cSaliguori         return -EIO;
897b338082bSbellard 
89883f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
89983f64091Sbellard }
900fc01f7e7Sbellard 
9017cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
9027cd1e32aSlirans@il.ibm.com                              int nb_sectors, int dirty)
9037cd1e32aSlirans@il.ibm.com {
9047cd1e32aSlirans@il.ibm.com     int64_t start, end;
905c6d22830SJan Kiszka     unsigned long val, idx, bit;
906a55eb92cSJan Kiszka 
9076ea44308SJan Kiszka     start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
908c6d22830SJan Kiszka     end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
9097cd1e32aSlirans@il.ibm.com 
9107cd1e32aSlirans@il.ibm.com     for (; start <= end; start++) {
911c6d22830SJan Kiszka         idx = start / (sizeof(unsigned long) * 8);
912c6d22830SJan Kiszka         bit = start % (sizeof(unsigned long) * 8);
913c6d22830SJan Kiszka         val = bs->dirty_bitmap[idx];
914c6d22830SJan Kiszka         if (dirty) {
915aaa0eb75SLiran Schour             if (!(val & (1 << bit))) {
916aaa0eb75SLiran Schour                 bs->dirty_count++;
917c6d22830SJan Kiszka                 val |= 1 << bit;
918aaa0eb75SLiran Schour             }
919c6d22830SJan Kiszka         } else {
920aaa0eb75SLiran Schour             if (val & (1 << bit)) {
921aaa0eb75SLiran Schour                 bs->dirty_count--;
922c6d22830SJan Kiszka                 val &= ~(1 << bit);
923c6d22830SJan Kiszka             }
924aaa0eb75SLiran Schour         }
925c6d22830SJan Kiszka         bs->dirty_bitmap[idx] = val;
9267cd1e32aSlirans@il.ibm.com     }
9277cd1e32aSlirans@il.ibm.com }
9287cd1e32aSlirans@il.ibm.com 
92919cb3738Sbellard /* Return < 0 if error. Important errors are:
93019cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
93119cb3738Sbellard   -ENOMEDIUM   No media inserted.
93219cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
93319cb3738Sbellard   -EACCES      Trying to write a read-only device
93419cb3738Sbellard */
935fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
936fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
937fc01f7e7Sbellard {
93883f64091Sbellard     BlockDriver *drv = bs->drv;
93919cb3738Sbellard     if (!bs->drv)
94019cb3738Sbellard         return -ENOMEDIUM;
9410849bf08Sbellard     if (bs->read_only)
94219cb3738Sbellard         return -EACCES;
94371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
94471d0770cSaliguori         return -EIO;
94571d0770cSaliguori 
946c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
9477cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
9487cd1e32aSlirans@il.ibm.com     }
9497cd1e32aSlirans@il.ibm.com 
950294cc35fSKevin Wolf     if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
951294cc35fSKevin Wolf         bs->wr_highest_sector = sector_num + nb_sectors - 1;
952294cc35fSKevin Wolf     }
953294cc35fSKevin Wolf 
95483f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
95583f64091Sbellard }
95683f64091Sbellard 
957eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
958eda578e5Saliguori                void *buf, int count1)
95983f64091Sbellard {
9606ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
96183f64091Sbellard     int len, nb_sectors, count;
96283f64091Sbellard     int64_t sector_num;
9639a8c4cceSKevin Wolf     int ret;
96483f64091Sbellard 
96583f64091Sbellard     count = count1;
96683f64091Sbellard     /* first read to align to sector start */
9676ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
96883f64091Sbellard     if (len > count)
96983f64091Sbellard         len = count;
9706ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
97183f64091Sbellard     if (len > 0) {
9729a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
9739a8c4cceSKevin Wolf             return ret;
9746ea44308SJan Kiszka         memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
97583f64091Sbellard         count -= len;
97683f64091Sbellard         if (count == 0)
97783f64091Sbellard             return count1;
97883f64091Sbellard         sector_num++;
97983f64091Sbellard         buf += len;
98083f64091Sbellard     }
98183f64091Sbellard 
98283f64091Sbellard     /* read the sectors "in place" */
9836ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
98483f64091Sbellard     if (nb_sectors > 0) {
9859a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
9869a8c4cceSKevin Wolf             return ret;
98783f64091Sbellard         sector_num += nb_sectors;
9886ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
98983f64091Sbellard         buf += len;
99083f64091Sbellard         count -= len;
99183f64091Sbellard     }
99283f64091Sbellard 
99383f64091Sbellard     /* add data from the last sector */
99483f64091Sbellard     if (count > 0) {
9959a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
9969a8c4cceSKevin Wolf             return ret;
99783f64091Sbellard         memcpy(buf, tmp_buf, count);
99883f64091Sbellard     }
99983f64091Sbellard     return count1;
100083f64091Sbellard }
100183f64091Sbellard 
1002eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1003eda578e5Saliguori                 const void *buf, int count1)
100483f64091Sbellard {
10056ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
100683f64091Sbellard     int len, nb_sectors, count;
100783f64091Sbellard     int64_t sector_num;
10089a8c4cceSKevin Wolf     int ret;
100983f64091Sbellard 
101083f64091Sbellard     count = count1;
101183f64091Sbellard     /* first write to align to sector start */
10126ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
101383f64091Sbellard     if (len > count)
101483f64091Sbellard         len = count;
10156ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
101683f64091Sbellard     if (len > 0) {
10179a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10189a8c4cceSKevin Wolf             return ret;
10196ea44308SJan Kiszka         memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
10209a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10219a8c4cceSKevin Wolf             return ret;
102283f64091Sbellard         count -= len;
102383f64091Sbellard         if (count == 0)
102483f64091Sbellard             return count1;
102583f64091Sbellard         sector_num++;
102683f64091Sbellard         buf += len;
102783f64091Sbellard     }
102883f64091Sbellard 
102983f64091Sbellard     /* write the sectors "in place" */
10306ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
103183f64091Sbellard     if (nb_sectors > 0) {
10329a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
10339a8c4cceSKevin Wolf             return ret;
103483f64091Sbellard         sector_num += nb_sectors;
10356ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
103683f64091Sbellard         buf += len;
103783f64091Sbellard         count -= len;
103883f64091Sbellard     }
103983f64091Sbellard 
104083f64091Sbellard     /* add data from the last sector */
104183f64091Sbellard     if (count > 0) {
10429a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10439a8c4cceSKevin Wolf             return ret;
104483f64091Sbellard         memcpy(tmp_buf, buf, count);
10459a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10469a8c4cceSKevin Wolf             return ret;
104783f64091Sbellard     }
104883f64091Sbellard     return count1;
104983f64091Sbellard }
105083f64091Sbellard 
1051f08145feSKevin Wolf /*
1052f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1053f08145feSKevin Wolf  * request (acts as a barrier)
1054f08145feSKevin Wolf  *
1055f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1056f08145feSKevin Wolf  */
1057f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1058f08145feSKevin Wolf     const void *buf, int count)
1059f08145feSKevin Wolf {
1060f08145feSKevin Wolf     int ret;
1061f08145feSKevin Wolf 
1062f08145feSKevin Wolf     ret = bdrv_pwrite(bs, offset, buf, count);
1063f08145feSKevin Wolf     if (ret < 0) {
1064f08145feSKevin Wolf         return ret;
1065f08145feSKevin Wolf     }
1066f08145feSKevin Wolf 
1067f08145feSKevin Wolf     /* No flush needed for cache=writethrough, it uses O_DSYNC */
1068f08145feSKevin Wolf     if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1069f08145feSKevin Wolf         bdrv_flush(bs);
1070f08145feSKevin Wolf     }
1071f08145feSKevin Wolf 
1072f08145feSKevin Wolf     return 0;
1073f08145feSKevin Wolf }
1074f08145feSKevin Wolf 
1075f08145feSKevin Wolf /*
1076f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1077f08145feSKevin Wolf  * request (acts as a barrier)
1078f08145feSKevin Wolf  *
1079f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1080f08145feSKevin Wolf  */
1081f08145feSKevin Wolf int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1082f08145feSKevin Wolf     const uint8_t *buf, int nb_sectors)
1083f08145feSKevin Wolf {
1084f08145feSKevin Wolf     return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1085f08145feSKevin Wolf         buf, BDRV_SECTOR_SIZE * nb_sectors);
1086f08145feSKevin Wolf }
1087f08145feSKevin Wolf 
108883f64091Sbellard /**
108983f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
109083f64091Sbellard  */
109183f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
109283f64091Sbellard {
109383f64091Sbellard     BlockDriver *drv = bs->drv;
109451762288SStefan Hajnoczi     int ret;
109583f64091Sbellard     if (!drv)
109619cb3738Sbellard         return -ENOMEDIUM;
109783f64091Sbellard     if (!drv->bdrv_truncate)
109883f64091Sbellard         return -ENOTSUP;
109959f2689dSNaphtali Sprei     if (bs->read_only)
110059f2689dSNaphtali Sprei         return -EACCES;
110151762288SStefan Hajnoczi     ret = drv->bdrv_truncate(bs, offset);
110251762288SStefan Hajnoczi     if (ret == 0) {
110351762288SStefan Hajnoczi         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
110451762288SStefan Hajnoczi     }
110551762288SStefan Hajnoczi     return ret;
110683f64091Sbellard }
110783f64091Sbellard 
110883f64091Sbellard /**
110983f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
111083f64091Sbellard  */
111183f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
111283f64091Sbellard {
111383f64091Sbellard     BlockDriver *drv = bs->drv;
111483f64091Sbellard     if (!drv)
111519cb3738Sbellard         return -ENOMEDIUM;
111651762288SStefan Hajnoczi 
111751762288SStefan Hajnoczi     /* Fixed size devices use the total_sectors value for speed instead of
111851762288SStefan Hajnoczi        issuing a length query (like lseek) on each call.  Also, legacy block
111951762288SStefan Hajnoczi        drivers don't provide a bdrv_getlength function and must use
112051762288SStefan Hajnoczi        total_sectors. */
112151762288SStefan Hajnoczi     if (!bs->growable || !drv->bdrv_getlength) {
11226ea44308SJan Kiszka         return bs->total_sectors * BDRV_SECTOR_SIZE;
112383f64091Sbellard     }
112483f64091Sbellard     return drv->bdrv_getlength(bs);
1125fc01f7e7Sbellard }
1126fc01f7e7Sbellard 
112719cb3738Sbellard /* return 0 as number of sectors if no device present or error */
112896b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1129fc01f7e7Sbellard {
113019cb3738Sbellard     int64_t length;
113119cb3738Sbellard     length = bdrv_getlength(bs);
113219cb3738Sbellard     if (length < 0)
113319cb3738Sbellard         length = 0;
113419cb3738Sbellard     else
11356ea44308SJan Kiszka         length = length >> BDRV_SECTOR_BITS;
113619cb3738Sbellard     *nb_sectors_ptr = length;
1137fc01f7e7Sbellard }
1138cf98951bSbellard 
1139f3d54fc4Saliguori struct partition {
1140f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
1141f3d54fc4Saliguori         uint8_t head;               /* starting head */
1142f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
1143f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
1144f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
1145f3d54fc4Saliguori         uint8_t end_head;           /* end head */
1146f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
1147f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
1148f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
1149f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
1150f3d54fc4Saliguori } __attribute__((packed));
1151f3d54fc4Saliguori 
1152f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1153f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
1154f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
1155f3d54fc4Saliguori {
1156eb5a3165SJes Sorensen     uint8_t buf[BDRV_SECTOR_SIZE];
1157f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
1158f3d54fc4Saliguori     struct partition *p;
1159f3d54fc4Saliguori     uint32_t nr_sects;
1160a38131b6Sblueswir1     uint64_t nb_sectors;
1161f3d54fc4Saliguori 
1162f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1163f3d54fc4Saliguori 
1164f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
1165f3d54fc4Saliguori     if (ret < 0)
1166f3d54fc4Saliguori         return -1;
1167f3d54fc4Saliguori     /* test msdos magic */
1168f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
1169f3d54fc4Saliguori         return -1;
1170f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
1171f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
1172f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
1173f3d54fc4Saliguori         if (nr_sects && p->end_head) {
1174f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
1175f3d54fc4Saliguori                a cylinder boundary */
1176f3d54fc4Saliguori             heads = p->end_head + 1;
1177f3d54fc4Saliguori             sectors = p->end_sector & 63;
1178f3d54fc4Saliguori             if (sectors == 0)
1179f3d54fc4Saliguori                 continue;
1180f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
1181f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
1182f3d54fc4Saliguori                 continue;
1183f3d54fc4Saliguori             *pheads = heads;
1184f3d54fc4Saliguori             *psectors = sectors;
1185f3d54fc4Saliguori             *pcylinders = cylinders;
1186f3d54fc4Saliguori #if 0
1187f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
1188f3d54fc4Saliguori                    cylinders, heads, sectors);
1189f3d54fc4Saliguori #endif
1190f3d54fc4Saliguori             return 0;
1191f3d54fc4Saliguori         }
1192f3d54fc4Saliguori     }
1193f3d54fc4Saliguori     return -1;
1194f3d54fc4Saliguori }
1195f3d54fc4Saliguori 
1196f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1197f3d54fc4Saliguori {
1198f3d54fc4Saliguori     int translation, lba_detected = 0;
1199f3d54fc4Saliguori     int cylinders, heads, secs;
1200a38131b6Sblueswir1     uint64_t nb_sectors;
1201f3d54fc4Saliguori 
1202f3d54fc4Saliguori     /* if a geometry hint is available, use it */
1203f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1204f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1205f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
1206f3d54fc4Saliguori     if (cylinders != 0) {
1207f3d54fc4Saliguori         *pcyls = cylinders;
1208f3d54fc4Saliguori         *pheads = heads;
1209f3d54fc4Saliguori         *psecs = secs;
1210f3d54fc4Saliguori     } else {
1211f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1212f3d54fc4Saliguori             if (heads > 16) {
1213f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
1214f3d54fc4Saliguori                    translation was active, so the default
1215f3d54fc4Saliguori                    hardware geometry is OK */
1216f3d54fc4Saliguori                 lba_detected = 1;
1217f3d54fc4Saliguori                 goto default_geometry;
1218f3d54fc4Saliguori             } else {
1219f3d54fc4Saliguori                 *pcyls = cylinders;
1220f3d54fc4Saliguori                 *pheads = heads;
1221f3d54fc4Saliguori                 *psecs = secs;
1222f3d54fc4Saliguori                 /* disable any translation to be in sync with
1223f3d54fc4Saliguori                    the logical geometry */
1224f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1225f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1226f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
1227f3d54fc4Saliguori                 }
1228f3d54fc4Saliguori             }
1229f3d54fc4Saliguori         } else {
1230f3d54fc4Saliguori         default_geometry:
1231f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
1232f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
1233f3d54fc4Saliguori 
1234f3d54fc4Saliguori             if (cylinders > 16383)
1235f3d54fc4Saliguori                 cylinders = 16383;
1236f3d54fc4Saliguori             else if (cylinders < 2)
1237f3d54fc4Saliguori                 cylinders = 2;
1238f3d54fc4Saliguori             *pcyls = cylinders;
1239f3d54fc4Saliguori             *pheads = 16;
1240f3d54fc4Saliguori             *psecs = 63;
1241f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1242f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
1243f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1244f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
1245f3d54fc4Saliguori                 } else {
1246f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1247f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
1248f3d54fc4Saliguori                 }
1249f3d54fc4Saliguori             }
1250f3d54fc4Saliguori         }
1251f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1252f3d54fc4Saliguori     }
1253f3d54fc4Saliguori }
1254f3d54fc4Saliguori 
1255b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
1256b338082bSbellard                             int cyls, int heads, int secs)
1257b338082bSbellard {
1258b338082bSbellard     bs->cyls = cyls;
1259b338082bSbellard     bs->heads = heads;
1260b338082bSbellard     bs->secs = secs;
1261b338082bSbellard }
1262b338082bSbellard 
1263b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
1264b338082bSbellard {
1265b338082bSbellard     bs->type = type;
1266b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
1267b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
1268b338082bSbellard }
1269b338082bSbellard 
127046d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
127146d4767dSbellard {
127246d4767dSbellard     bs->translation = translation;
127346d4767dSbellard }
127446d4767dSbellard 
1275b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
1276b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
1277b338082bSbellard {
1278b338082bSbellard     *pcyls = bs->cyls;
1279b338082bSbellard     *pheads = bs->heads;
1280b338082bSbellard     *psecs = bs->secs;
1281b338082bSbellard }
1282b338082bSbellard 
1283b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
1284b338082bSbellard {
1285b338082bSbellard     return bs->type;
1286b338082bSbellard }
1287b338082bSbellard 
128846d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
128946d4767dSbellard {
129046d4767dSbellard     return bs->translation;
129146d4767dSbellard }
129246d4767dSbellard 
1293abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1294abd7f68dSMarkus Armbruster                        BlockErrorAction on_write_error)
1295abd7f68dSMarkus Armbruster {
1296abd7f68dSMarkus Armbruster     bs->on_read_error = on_read_error;
1297abd7f68dSMarkus Armbruster     bs->on_write_error = on_write_error;
1298abd7f68dSMarkus Armbruster }
1299abd7f68dSMarkus Armbruster 
1300abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1301abd7f68dSMarkus Armbruster {
1302abd7f68dSMarkus Armbruster     return is_read ? bs->on_read_error : bs->on_write_error;
1303abd7f68dSMarkus Armbruster }
1304abd7f68dSMarkus Armbruster 
13057d0d6950SMarkus Armbruster void bdrv_set_removable(BlockDriverState *bs, int removable)
13067d0d6950SMarkus Armbruster {
13077d0d6950SMarkus Armbruster     bs->removable = removable;
13087d0d6950SMarkus Armbruster     if (removable && bs == bs_snapshots) {
13097d0d6950SMarkus Armbruster         bs_snapshots = NULL;
13107d0d6950SMarkus Armbruster     }
13117d0d6950SMarkus Armbruster }
13127d0d6950SMarkus Armbruster 
1313b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
1314b338082bSbellard {
1315b338082bSbellard     return bs->removable;
1316b338082bSbellard }
1317b338082bSbellard 
1318b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
1319b338082bSbellard {
1320b338082bSbellard     return bs->read_only;
1321b338082bSbellard }
1322b338082bSbellard 
1323985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
1324985a03b0Sths {
1325985a03b0Sths     return bs->sg;
1326985a03b0Sths }
1327985a03b0Sths 
1328e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs)
1329e900a7b7SChristoph Hellwig {
1330e900a7b7SChristoph Hellwig     return bs->enable_write_cache;
1331e900a7b7SChristoph Hellwig }
1332e900a7b7SChristoph Hellwig 
133319cb3738Sbellard /* XXX: no longer used */
1334b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
1335b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
1336b338082bSbellard {
1337b338082bSbellard     bs->change_cb = change_cb;
1338b338082bSbellard     bs->change_opaque = opaque;
1339b338082bSbellard }
1340b338082bSbellard 
1341ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
1342ea2384d3Sbellard {
1343ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
1344ea2384d3Sbellard         return 1;
1345ea2384d3Sbellard     return bs->encrypted;
1346ea2384d3Sbellard }
1347ea2384d3Sbellard 
1348c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
1349c0f4ce77Saliguori {
1350c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
1351c0f4ce77Saliguori 
1352c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1353c0f4ce77Saliguori         return 1;
1354c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
1355c0f4ce77Saliguori }
1356c0f4ce77Saliguori 
1357ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
1358ea2384d3Sbellard {
1359ea2384d3Sbellard     int ret;
1360ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
1361ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
1362ea2384d3Sbellard         if (ret < 0)
1363ea2384d3Sbellard             return ret;
1364ea2384d3Sbellard         if (!bs->encrypted)
1365ea2384d3Sbellard             return 0;
1366ea2384d3Sbellard     }
1367fd04a2aeSShahar Havivi     if (!bs->encrypted) {
1368fd04a2aeSShahar Havivi         return -EINVAL;
1369fd04a2aeSShahar Havivi     } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1370fd04a2aeSShahar Havivi         return -ENOMEDIUM;
1371fd04a2aeSShahar Havivi     }
1372c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
1373bb5fc20fSaliguori     if (ret < 0) {
1374bb5fc20fSaliguori         bs->valid_key = 0;
1375bb5fc20fSaliguori     } else if (!bs->valid_key) {
1376bb5fc20fSaliguori         bs->valid_key = 1;
1377bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
1378bb5fc20fSaliguori         bs->media_changed = 1;
1379bb5fc20fSaliguori         if (bs->change_cb)
1380bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
1381bb5fc20fSaliguori     }
1382c0f4ce77Saliguori     return ret;
1383ea2384d3Sbellard }
1384ea2384d3Sbellard 
1385ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1386ea2384d3Sbellard {
138719cb3738Sbellard     if (!bs->drv) {
1388ea2384d3Sbellard         buf[0] = '\0';
1389ea2384d3Sbellard     } else {
1390ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
1391ea2384d3Sbellard     }
1392ea2384d3Sbellard }
1393ea2384d3Sbellard 
1394ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1395ea2384d3Sbellard                          void *opaque)
1396ea2384d3Sbellard {
1397ea2384d3Sbellard     BlockDriver *drv;
1398ea2384d3Sbellard 
13998a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv, &bdrv_drivers, list) {
1400ea2384d3Sbellard         it(opaque, drv->format_name);
1401ea2384d3Sbellard     }
1402ea2384d3Sbellard }
1403ea2384d3Sbellard 
1404b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1405b338082bSbellard {
1406b338082bSbellard     BlockDriverState *bs;
1407b338082bSbellard 
14081b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
14091b7bdbc1SStefan Hajnoczi         if (!strcmp(name, bs->device_name)) {
1410b338082bSbellard             return bs;
1411b338082bSbellard         }
14121b7bdbc1SStefan Hajnoczi     }
1413b338082bSbellard     return NULL;
1414b338082bSbellard }
1415b338082bSbellard 
14162f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs)
14172f399b0aSMarkus Armbruster {
14182f399b0aSMarkus Armbruster     if (!bs) {
14192f399b0aSMarkus Armbruster         return QTAILQ_FIRST(&bdrv_states);
14202f399b0aSMarkus Armbruster     }
14212f399b0aSMarkus Armbruster     return QTAILQ_NEXT(bs, list);
14222f399b0aSMarkus Armbruster }
14232f399b0aSMarkus Armbruster 
142451de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
142581d0912dSbellard {
142681d0912dSbellard     BlockDriverState *bs;
142781d0912dSbellard 
14281b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
142951de9760Saliguori         it(opaque, bs);
143081d0912dSbellard     }
143181d0912dSbellard }
143281d0912dSbellard 
1433ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1434ea2384d3Sbellard {
1435ea2384d3Sbellard     return bs->device_name;
1436ea2384d3Sbellard }
1437ea2384d3Sbellard 
14387a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
14397a6cba61Spbrook {
1440016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
1441016f5cf6SAlexander Graf         return;
1442016f5cf6SAlexander Graf     }
1443016f5cf6SAlexander Graf 
14443f5075aeSChristoph Hellwig     if (bs->drv && bs->drv->bdrv_flush)
14457a6cba61Spbrook         bs->drv->bdrv_flush(bs);
14467a6cba61Spbrook }
14477a6cba61Spbrook 
1448c6ca28d6Saliguori void bdrv_flush_all(void)
1449c6ca28d6Saliguori {
1450c6ca28d6Saliguori     BlockDriverState *bs;
1451c6ca28d6Saliguori 
14521b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1453c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
14541b7bdbc1SStefan Hajnoczi             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1455c6ca28d6Saliguori             bdrv_flush(bs);
1456c6ca28d6Saliguori         }
14571b7bdbc1SStefan Hajnoczi     }
14581b7bdbc1SStefan Hajnoczi }
1459c6ca28d6Saliguori 
1460f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs)
1461f2feebbdSKevin Wolf {
1462f2feebbdSKevin Wolf     assert(bs->drv);
1463f2feebbdSKevin Wolf 
1464f2feebbdSKevin Wolf     if (bs->drv->no_zero_init) {
1465f2feebbdSKevin Wolf         return 0;
1466f2feebbdSKevin Wolf     } else if (bs->file) {
1467f2feebbdSKevin Wolf         return bdrv_has_zero_init(bs->file);
1468f2feebbdSKevin Wolf     }
1469f2feebbdSKevin Wolf 
1470f2feebbdSKevin Wolf     return 1;
1471f2feebbdSKevin Wolf }
1472f2feebbdSKevin Wolf 
1473f58c7b35Sths /*
1474f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1475f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1476f58c7b35Sths  * hence all their sectors are reported as allocated.
1477f58c7b35Sths  *
1478f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1479f58c7b35Sths  * the specified sector) that are known to be in the same
1480f58c7b35Sths  * allocated/unallocated state.
1481f58c7b35Sths  *
1482f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1483f58c7b35Sths  */
1484f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1485f58c7b35Sths 	int *pnum)
1486f58c7b35Sths {
1487f58c7b35Sths     int64_t n;
1488f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1489f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1490f58c7b35Sths             *pnum = 0;
1491f58c7b35Sths             return 0;
1492f58c7b35Sths         }
1493f58c7b35Sths         n = bs->total_sectors - sector_num;
1494f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1495f58c7b35Sths         return 1;
1496f58c7b35Sths     }
1497f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1498f58c7b35Sths }
1499f58c7b35Sths 
15002582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv,
15012582bfedSLuiz Capitulino                     BlockMonEventAction action, int is_read)
15022582bfedSLuiz Capitulino {
15032582bfedSLuiz Capitulino     QObject *data;
15042582bfedSLuiz Capitulino     const char *action_str;
15052582bfedSLuiz Capitulino 
15062582bfedSLuiz Capitulino     switch (action) {
15072582bfedSLuiz Capitulino     case BDRV_ACTION_REPORT:
15082582bfedSLuiz Capitulino         action_str = "report";
15092582bfedSLuiz Capitulino         break;
15102582bfedSLuiz Capitulino     case BDRV_ACTION_IGNORE:
15112582bfedSLuiz Capitulino         action_str = "ignore";
15122582bfedSLuiz Capitulino         break;
15132582bfedSLuiz Capitulino     case BDRV_ACTION_STOP:
15142582bfedSLuiz Capitulino         action_str = "stop";
15152582bfedSLuiz Capitulino         break;
15162582bfedSLuiz Capitulino     default:
15172582bfedSLuiz Capitulino         abort();
15182582bfedSLuiz Capitulino     }
15192582bfedSLuiz Capitulino 
15202582bfedSLuiz Capitulino     data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
15212582bfedSLuiz Capitulino                               bdrv->device_name,
15222582bfedSLuiz Capitulino                               action_str,
15232582bfedSLuiz Capitulino                               is_read ? "read" : "write");
15242582bfedSLuiz Capitulino     monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
15252582bfedSLuiz Capitulino 
15262582bfedSLuiz Capitulino     qobject_decref(data);
15272582bfedSLuiz Capitulino }
15282582bfedSLuiz Capitulino 
1529d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque)
1530b338082bSbellard {
1531d15e5465SLuiz Capitulino     QDict *bs_dict;
1532d15e5465SLuiz Capitulino     Monitor *mon = opaque;
1533b338082bSbellard 
1534d15e5465SLuiz Capitulino     bs_dict = qobject_to_qdict(obj);
1535d15e5465SLuiz Capitulino 
1536d15e5465SLuiz Capitulino     monitor_printf(mon, "%s: type=%s removable=%d",
1537d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "device"),
1538d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "type"),
1539d15e5465SLuiz Capitulino                         qdict_get_bool(bs_dict, "removable"));
1540d15e5465SLuiz Capitulino 
1541d15e5465SLuiz Capitulino     if (qdict_get_bool(bs_dict, "removable")) {
1542d15e5465SLuiz Capitulino         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1543b338082bSbellard     }
1544d15e5465SLuiz Capitulino 
1545d15e5465SLuiz Capitulino     if (qdict_haskey(bs_dict, "inserted")) {
1546d15e5465SLuiz Capitulino         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1547d15e5465SLuiz Capitulino 
1548376253ecSaliguori         monitor_printf(mon, " file=");
1549d15e5465SLuiz Capitulino         monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1550d15e5465SLuiz Capitulino         if (qdict_haskey(qdict, "backing_file")) {
1551376253ecSaliguori             monitor_printf(mon, " backing_file=");
1552d15e5465SLuiz Capitulino             monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1553fef30743Sths         }
1554d15e5465SLuiz Capitulino         monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1555d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "ro"),
1556d15e5465SLuiz Capitulino                             qdict_get_str(qdict, "drv"),
1557d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "encrypted"));
1558b338082bSbellard     } else {
1559376253ecSaliguori         monitor_printf(mon, " [not inserted]");
1560b338082bSbellard     }
1561d15e5465SLuiz Capitulino 
1562376253ecSaliguori     monitor_printf(mon, "\n");
1563b338082bSbellard }
1564d15e5465SLuiz Capitulino 
1565d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data)
1566d15e5465SLuiz Capitulino {
1567d15e5465SLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1568d15e5465SLuiz Capitulino }
1569d15e5465SLuiz Capitulino 
1570d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data)
1571d15e5465SLuiz Capitulino {
1572d15e5465SLuiz Capitulino     QList *bs_list;
1573d15e5465SLuiz Capitulino     BlockDriverState *bs;
1574d15e5465SLuiz Capitulino 
1575d15e5465SLuiz Capitulino     bs_list = qlist_new();
1576d15e5465SLuiz Capitulino 
15771b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1578d15e5465SLuiz Capitulino         QObject *bs_obj;
1579d15e5465SLuiz Capitulino         const char *type = "unknown";
1580d15e5465SLuiz Capitulino 
1581d15e5465SLuiz Capitulino         switch(bs->type) {
1582d15e5465SLuiz Capitulino         case BDRV_TYPE_HD:
1583d15e5465SLuiz Capitulino             type = "hd";
1584d15e5465SLuiz Capitulino             break;
1585d15e5465SLuiz Capitulino         case BDRV_TYPE_CDROM:
1586d15e5465SLuiz Capitulino             type = "cdrom";
1587d15e5465SLuiz Capitulino             break;
1588d15e5465SLuiz Capitulino         case BDRV_TYPE_FLOPPY:
1589d15e5465SLuiz Capitulino             type = "floppy";
1590d15e5465SLuiz Capitulino             break;
1591d15e5465SLuiz Capitulino         }
1592d15e5465SLuiz Capitulino 
1593d15e5465SLuiz Capitulino         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1594d15e5465SLuiz Capitulino                                     "'removable': %i, 'locked': %i }",
1595d15e5465SLuiz Capitulino                                     bs->device_name, type, bs->removable,
1596d15e5465SLuiz Capitulino                                     bs->locked);
1597d15e5465SLuiz Capitulino 
1598d15e5465SLuiz Capitulino         if (bs->drv) {
1599d15e5465SLuiz Capitulino             QObject *obj;
1600d15e5465SLuiz Capitulino             QDict *bs_dict = qobject_to_qdict(bs_obj);
1601d15e5465SLuiz Capitulino 
1602d15e5465SLuiz Capitulino             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1603d15e5465SLuiz Capitulino                                      "'encrypted': %i }",
1604d15e5465SLuiz Capitulino                                      bs->filename, bs->read_only,
1605d15e5465SLuiz Capitulino                                      bs->drv->format_name,
1606d15e5465SLuiz Capitulino                                      bdrv_is_encrypted(bs));
1607d15e5465SLuiz Capitulino             if (bs->backing_file[0] != '\0') {
1608d15e5465SLuiz Capitulino                 QDict *qdict = qobject_to_qdict(obj);
1609d15e5465SLuiz Capitulino                 qdict_put(qdict, "backing_file",
1610d15e5465SLuiz Capitulino                           qstring_from_str(bs->backing_file));
1611d15e5465SLuiz Capitulino             }
1612d15e5465SLuiz Capitulino 
1613d15e5465SLuiz Capitulino             qdict_put_obj(bs_dict, "inserted", obj);
1614d15e5465SLuiz Capitulino         }
1615d15e5465SLuiz Capitulino         qlist_append_obj(bs_list, bs_obj);
1616d15e5465SLuiz Capitulino     }
1617d15e5465SLuiz Capitulino 
1618d15e5465SLuiz Capitulino     *ret_data = QOBJECT(bs_list);
1619b338082bSbellard }
1620a36e69ddSths 
1621218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque)
1622a36e69ddSths {
1623218a536aSLuiz Capitulino     QDict *qdict;
1624218a536aSLuiz Capitulino     Monitor *mon = opaque;
1625218a536aSLuiz Capitulino 
1626218a536aSLuiz Capitulino     qdict = qobject_to_qdict(data);
1627218a536aSLuiz Capitulino     monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1628218a536aSLuiz Capitulino 
1629218a536aSLuiz Capitulino     qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1630218a536aSLuiz Capitulino     monitor_printf(mon, " rd_bytes=%" PRId64
1631218a536aSLuiz Capitulino                         " wr_bytes=%" PRId64
1632218a536aSLuiz Capitulino                         " rd_operations=%" PRId64
1633218a536aSLuiz Capitulino                         " wr_operations=%" PRId64
1634218a536aSLuiz Capitulino                         "\n",
1635218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_bytes"),
1636218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_bytes"),
1637218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_operations"),
1638218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_operations"));
1639218a536aSLuiz Capitulino }
1640218a536aSLuiz Capitulino 
1641218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data)
1642218a536aSLuiz Capitulino {
1643218a536aSLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1644218a536aSLuiz Capitulino }
1645218a536aSLuiz Capitulino 
1646294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1647294cc35fSKevin Wolf {
1648294cc35fSKevin Wolf     QObject *res;
1649294cc35fSKevin Wolf     QDict *dict;
1650294cc35fSKevin Wolf 
1651294cc35fSKevin Wolf     res = qobject_from_jsonf("{ 'stats': {"
1652294cc35fSKevin Wolf                              "'rd_bytes': %" PRId64 ","
1653294cc35fSKevin Wolf                              "'wr_bytes': %" PRId64 ","
1654294cc35fSKevin Wolf                              "'rd_operations': %" PRId64 ","
1655294cc35fSKevin Wolf                              "'wr_operations': %" PRId64 ","
1656294cc35fSKevin Wolf                              "'wr_highest_offset': %" PRId64
1657294cc35fSKevin Wolf                              "} }",
1658294cc35fSKevin Wolf                              bs->rd_bytes, bs->wr_bytes,
1659294cc35fSKevin Wolf                              bs->rd_ops, bs->wr_ops,
16605ffbbc67SBlue Swirl                              bs->wr_highest_sector *
16615ffbbc67SBlue Swirl                              (uint64_t)BDRV_SECTOR_SIZE);
1662294cc35fSKevin Wolf     dict  = qobject_to_qdict(res);
1663294cc35fSKevin Wolf 
1664294cc35fSKevin Wolf     if (*bs->device_name) {
1665294cc35fSKevin Wolf         qdict_put(dict, "device", qstring_from_str(bs->device_name));
1666294cc35fSKevin Wolf     }
1667294cc35fSKevin Wolf 
1668294cc35fSKevin Wolf     if (bs->file) {
1669294cc35fSKevin Wolf         QObject *parent = bdrv_info_stats_bs(bs->file);
1670294cc35fSKevin Wolf         qdict_put_obj(dict, "parent", parent);
1671294cc35fSKevin Wolf     }
1672294cc35fSKevin Wolf 
1673294cc35fSKevin Wolf     return res;
1674294cc35fSKevin Wolf }
1675294cc35fSKevin Wolf 
1676218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1677218a536aSLuiz Capitulino {
1678218a536aSLuiz Capitulino     QObject *obj;
1679218a536aSLuiz Capitulino     QList *devices;
1680a36e69ddSths     BlockDriverState *bs;
1681a36e69ddSths 
1682218a536aSLuiz Capitulino     devices = qlist_new();
1683218a536aSLuiz Capitulino 
16841b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1685294cc35fSKevin Wolf         obj = bdrv_info_stats_bs(bs);
1686218a536aSLuiz Capitulino         qlist_append_obj(devices, obj);
1687a36e69ddSths     }
1688218a536aSLuiz Capitulino 
1689218a536aSLuiz Capitulino     *ret_data = QOBJECT(devices);
1690a36e69ddSths }
1691ea2384d3Sbellard 
1692045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1693045df330Saliguori {
1694045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1695045df330Saliguori         return bs->backing_file;
1696045df330Saliguori     else if (bs->encrypted)
1697045df330Saliguori         return bs->filename;
1698045df330Saliguori     else
1699045df330Saliguori         return NULL;
1700045df330Saliguori }
1701045df330Saliguori 
170283f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
170383f64091Sbellard                                char *filename, int filename_size)
170483f64091Sbellard {
1705b783e409SKevin Wolf     if (!bs->backing_file) {
170683f64091Sbellard         pstrcpy(filename, filename_size, "");
170783f64091Sbellard     } else {
170883f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
170983f64091Sbellard     }
171083f64091Sbellard }
171183f64091Sbellard 
1712faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1713faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1714faea38e7Sbellard {
1715faea38e7Sbellard     BlockDriver *drv = bs->drv;
1716faea38e7Sbellard     if (!drv)
171719cb3738Sbellard         return -ENOMEDIUM;
1718faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1719faea38e7Sbellard         return -ENOTSUP;
1720fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1721fbb7b4e0SKevin Wolf         return -EIO;
17227cd1e32aSlirans@il.ibm.com 
1723c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
17247cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
17257cd1e32aSlirans@il.ibm.com     }
17267cd1e32aSlirans@il.ibm.com 
1727faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1728faea38e7Sbellard }
1729faea38e7Sbellard 
1730faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1731faea38e7Sbellard {
1732faea38e7Sbellard     BlockDriver *drv = bs->drv;
1733faea38e7Sbellard     if (!drv)
173419cb3738Sbellard         return -ENOMEDIUM;
1735faea38e7Sbellard     if (!drv->bdrv_get_info)
1736faea38e7Sbellard         return -ENOTSUP;
1737faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1738faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1739faea38e7Sbellard }
1740faea38e7Sbellard 
174145566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
174245566e9cSChristoph Hellwig                       int64_t pos, int size)
1743178e08a5Saliguori {
1744178e08a5Saliguori     BlockDriver *drv = bs->drv;
1745178e08a5Saliguori     if (!drv)
1746178e08a5Saliguori         return -ENOMEDIUM;
17477cdb1f6dSMORITA Kazutaka     if (drv->bdrv_save_vmstate)
174845566e9cSChristoph Hellwig         return drv->bdrv_save_vmstate(bs, buf, pos, size);
17497cdb1f6dSMORITA Kazutaka     if (bs->file)
17507cdb1f6dSMORITA Kazutaka         return bdrv_save_vmstate(bs->file, buf, pos, size);
17517cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1752178e08a5Saliguori }
1753178e08a5Saliguori 
175445566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
175545566e9cSChristoph Hellwig                       int64_t pos, int size)
1756178e08a5Saliguori {
1757178e08a5Saliguori     BlockDriver *drv = bs->drv;
1758178e08a5Saliguori     if (!drv)
1759178e08a5Saliguori         return -ENOMEDIUM;
17607cdb1f6dSMORITA Kazutaka     if (drv->bdrv_load_vmstate)
176145566e9cSChristoph Hellwig         return drv->bdrv_load_vmstate(bs, buf, pos, size);
17627cdb1f6dSMORITA Kazutaka     if (bs->file)
17637cdb1f6dSMORITA Kazutaka         return bdrv_load_vmstate(bs->file, buf, pos, size);
17647cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1765178e08a5Saliguori }
1766178e08a5Saliguori 
17678b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
17688b9b0cc2SKevin Wolf {
17698b9b0cc2SKevin Wolf     BlockDriver *drv = bs->drv;
17708b9b0cc2SKevin Wolf 
17718b9b0cc2SKevin Wolf     if (!drv || !drv->bdrv_debug_event) {
17728b9b0cc2SKevin Wolf         return;
17738b9b0cc2SKevin Wolf     }
17748b9b0cc2SKevin Wolf 
17758b9b0cc2SKevin Wolf     return drv->bdrv_debug_event(bs, event);
17768b9b0cc2SKevin Wolf 
17778b9b0cc2SKevin Wolf }
17788b9b0cc2SKevin Wolf 
1779faea38e7Sbellard /**************************************************************/
1780faea38e7Sbellard /* handling of snapshots */
1781faea38e7Sbellard 
1782feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs)
1783feeee5acSMiguel Di Ciurcio Filho {
1784feeee5acSMiguel Di Ciurcio Filho     BlockDriver *drv = bs->drv;
1785feeee5acSMiguel Di Ciurcio Filho     if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1786feeee5acSMiguel Di Ciurcio Filho         return 0;
1787feeee5acSMiguel Di Ciurcio Filho     }
1788feeee5acSMiguel Di Ciurcio Filho 
1789feeee5acSMiguel Di Ciurcio Filho     if (!drv->bdrv_snapshot_create) {
1790feeee5acSMiguel Di Ciurcio Filho         if (bs->file != NULL) {
1791feeee5acSMiguel Di Ciurcio Filho             return bdrv_can_snapshot(bs->file);
1792feeee5acSMiguel Di Ciurcio Filho         }
1793feeee5acSMiguel Di Ciurcio Filho         return 0;
1794feeee5acSMiguel Di Ciurcio Filho     }
1795feeee5acSMiguel Di Ciurcio Filho 
1796feeee5acSMiguel Di Ciurcio Filho     return 1;
1797feeee5acSMiguel Di Ciurcio Filho }
1798feeee5acSMiguel Di Ciurcio Filho 
1799f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void)
1800f9092b10SMarkus Armbruster {
1801f9092b10SMarkus Armbruster     BlockDriverState *bs;
1802f9092b10SMarkus Armbruster 
18033ac906f7SMarkus Armbruster     if (bs_snapshots) {
1804f9092b10SMarkus Armbruster         return bs_snapshots;
18053ac906f7SMarkus Armbruster     }
1806f9092b10SMarkus Armbruster 
1807f9092b10SMarkus Armbruster     bs = NULL;
1808f9092b10SMarkus Armbruster     while ((bs = bdrv_next(bs))) {
1809f9092b10SMarkus Armbruster         if (bdrv_can_snapshot(bs)) {
18103ac906f7SMarkus Armbruster             bs_snapshots = bs;
18113ac906f7SMarkus Armbruster             return bs;
1812f9092b10SMarkus Armbruster         }
1813f9092b10SMarkus Armbruster     }
1814f9092b10SMarkus Armbruster     return NULL;
1815f9092b10SMarkus Armbruster }
1816f9092b10SMarkus Armbruster 
1817faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1818faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1819faea38e7Sbellard {
1820faea38e7Sbellard     BlockDriver *drv = bs->drv;
1821faea38e7Sbellard     if (!drv)
182219cb3738Sbellard         return -ENOMEDIUM;
18237cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_create)
1824faea38e7Sbellard         return drv->bdrv_snapshot_create(bs, sn_info);
18257cdb1f6dSMORITA Kazutaka     if (bs->file)
18267cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_create(bs->file, sn_info);
18277cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1828faea38e7Sbellard }
1829faea38e7Sbellard 
1830faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1831faea38e7Sbellard                        const char *snapshot_id)
1832faea38e7Sbellard {
1833faea38e7Sbellard     BlockDriver *drv = bs->drv;
18347cdb1f6dSMORITA Kazutaka     int ret, open_ret;
18357cdb1f6dSMORITA Kazutaka 
1836faea38e7Sbellard     if (!drv)
183719cb3738Sbellard         return -ENOMEDIUM;
18387cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_goto)
1839faea38e7Sbellard         return drv->bdrv_snapshot_goto(bs, snapshot_id);
18407cdb1f6dSMORITA Kazutaka 
18417cdb1f6dSMORITA Kazutaka     if (bs->file) {
18427cdb1f6dSMORITA Kazutaka         drv->bdrv_close(bs);
18437cdb1f6dSMORITA Kazutaka         ret = bdrv_snapshot_goto(bs->file, snapshot_id);
18447cdb1f6dSMORITA Kazutaka         open_ret = drv->bdrv_open(bs, bs->open_flags);
18457cdb1f6dSMORITA Kazutaka         if (open_ret < 0) {
18467cdb1f6dSMORITA Kazutaka             bdrv_delete(bs->file);
18477cdb1f6dSMORITA Kazutaka             bs->drv = NULL;
18487cdb1f6dSMORITA Kazutaka             return open_ret;
18497cdb1f6dSMORITA Kazutaka         }
18507cdb1f6dSMORITA Kazutaka         return ret;
18517cdb1f6dSMORITA Kazutaka     }
18527cdb1f6dSMORITA Kazutaka 
18537cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1854faea38e7Sbellard }
1855faea38e7Sbellard 
1856faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1857faea38e7Sbellard {
1858faea38e7Sbellard     BlockDriver *drv = bs->drv;
1859faea38e7Sbellard     if (!drv)
186019cb3738Sbellard         return -ENOMEDIUM;
18617cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_delete)
1862faea38e7Sbellard         return drv->bdrv_snapshot_delete(bs, snapshot_id);
18637cdb1f6dSMORITA Kazutaka     if (bs->file)
18647cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_delete(bs->file, snapshot_id);
18657cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1866faea38e7Sbellard }
1867faea38e7Sbellard 
1868faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1869faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1870faea38e7Sbellard {
1871faea38e7Sbellard     BlockDriver *drv = bs->drv;
1872faea38e7Sbellard     if (!drv)
187319cb3738Sbellard         return -ENOMEDIUM;
18747cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_list)
1875faea38e7Sbellard         return drv->bdrv_snapshot_list(bs, psn_info);
18767cdb1f6dSMORITA Kazutaka     if (bs->file)
18777cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_list(bs->file, psn_info);
18787cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1879faea38e7Sbellard }
1880faea38e7Sbellard 
1881faea38e7Sbellard #define NB_SUFFIXES 4
1882faea38e7Sbellard 
1883faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1884faea38e7Sbellard {
1885faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1886faea38e7Sbellard     int64_t base;
1887faea38e7Sbellard     int i;
1888faea38e7Sbellard 
1889faea38e7Sbellard     if (size <= 999) {
1890faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1891faea38e7Sbellard     } else {
1892faea38e7Sbellard         base = 1024;
1893faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1894faea38e7Sbellard             if (size < (10 * base)) {
1895faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1896faea38e7Sbellard                          (double)size / base,
1897faea38e7Sbellard                          suffixes[i]);
1898faea38e7Sbellard                 break;
1899faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1900faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1901faea38e7Sbellard                          ((size + (base >> 1)) / base),
1902faea38e7Sbellard                          suffixes[i]);
1903faea38e7Sbellard                 break;
1904faea38e7Sbellard             }
1905faea38e7Sbellard             base = base * 1024;
1906faea38e7Sbellard         }
1907faea38e7Sbellard     }
1908faea38e7Sbellard     return buf;
1909faea38e7Sbellard }
1910faea38e7Sbellard 
1911faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1912faea38e7Sbellard {
1913faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
19143b9f94e1Sbellard #ifdef _WIN32
19153b9f94e1Sbellard     struct tm *ptm;
19163b9f94e1Sbellard #else
1917faea38e7Sbellard     struct tm tm;
19183b9f94e1Sbellard #endif
1919faea38e7Sbellard     time_t ti;
1920faea38e7Sbellard     int64_t secs;
1921faea38e7Sbellard 
1922faea38e7Sbellard     if (!sn) {
1923faea38e7Sbellard         snprintf(buf, buf_size,
1924faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1925faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1926faea38e7Sbellard     } else {
1927faea38e7Sbellard         ti = sn->date_sec;
19283b9f94e1Sbellard #ifdef _WIN32
19293b9f94e1Sbellard         ptm = localtime(&ti);
19303b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
19313b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
19323b9f94e1Sbellard #else
1933faea38e7Sbellard         localtime_r(&ti, &tm);
1934faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1935faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
19363b9f94e1Sbellard #endif
1937faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1938faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1939faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1940faea38e7Sbellard                  (int)(secs / 3600),
1941faea38e7Sbellard                  (int)((secs / 60) % 60),
1942faea38e7Sbellard                  (int)(secs % 60),
1943faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1944faea38e7Sbellard         snprintf(buf, buf_size,
1945faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1946faea38e7Sbellard                  sn->id_str, sn->name,
1947faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1948faea38e7Sbellard                  date_buf,
1949faea38e7Sbellard                  clock_buf);
1950faea38e7Sbellard     }
1951faea38e7Sbellard     return buf;
1952faea38e7Sbellard }
1953faea38e7Sbellard 
195483f64091Sbellard 
1955ea2384d3Sbellard /**************************************************************/
195683f64091Sbellard /* async I/Os */
1957ea2384d3Sbellard 
19583b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1959f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
196083f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
1961ea2384d3Sbellard {
196283f64091Sbellard     BlockDriver *drv = bs->drv;
1963a36e69ddSths     BlockDriverAIOCB *ret;
1964ea2384d3Sbellard 
196519cb3738Sbellard     if (!drv)
1966ce1a14dcSpbrook         return NULL;
196771d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
196871d0770cSaliguori         return NULL;
196983f64091Sbellard 
1970f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1971f141eafeSaliguori                               cb, opaque);
1972a36e69ddSths 
1973a36e69ddSths     if (ret) {
1974a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
19756ea44308SJan Kiszka 	bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1976a36e69ddSths 	bs->rd_ops ++;
1977a36e69ddSths     }
1978a36e69ddSths 
1979a36e69ddSths     return ret;
198083f64091Sbellard }
198183f64091Sbellard 
1982f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1983f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
198483f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
19857674e7bfSbellard {
198683f64091Sbellard     BlockDriver *drv = bs->drv;
1987a36e69ddSths     BlockDriverAIOCB *ret;
198883f64091Sbellard 
198919cb3738Sbellard     if (!drv)
1990ce1a14dcSpbrook         return NULL;
199183f64091Sbellard     if (bs->read_only)
1992ce1a14dcSpbrook         return NULL;
199371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
199471d0770cSaliguori         return NULL;
199583f64091Sbellard 
1996c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
19977cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
19987cd1e32aSlirans@il.ibm.com     }
19997cd1e32aSlirans@il.ibm.com 
2000f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2001f141eafeSaliguori                                cb, opaque);
2002a36e69ddSths 
2003a36e69ddSths     if (ret) {
2004a36e69ddSths         /* Update stats even though technically transfer has not happened. */
20056ea44308SJan Kiszka         bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2006a36e69ddSths         bs->wr_ops ++;
2007294cc35fSKevin Wolf         if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2008294cc35fSKevin Wolf             bs->wr_highest_sector = sector_num + nb_sectors - 1;
2009294cc35fSKevin Wolf         }
2010a36e69ddSths     }
2011a36e69ddSths 
2012a36e69ddSths     return ret;
201383f64091Sbellard }
201483f64091Sbellard 
201540b4f539SKevin Wolf 
201640b4f539SKevin Wolf typedef struct MultiwriteCB {
201740b4f539SKevin Wolf     int error;
201840b4f539SKevin Wolf     int num_requests;
201940b4f539SKevin Wolf     int num_callbacks;
202040b4f539SKevin Wolf     struct {
202140b4f539SKevin Wolf         BlockDriverCompletionFunc *cb;
202240b4f539SKevin Wolf         void *opaque;
202340b4f539SKevin Wolf         QEMUIOVector *free_qiov;
202440b4f539SKevin Wolf         void *free_buf;
202540b4f539SKevin Wolf     } callbacks[];
202640b4f539SKevin Wolf } MultiwriteCB;
202740b4f539SKevin Wolf 
202840b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb)
202940b4f539SKevin Wolf {
203040b4f539SKevin Wolf     int i;
203140b4f539SKevin Wolf 
203240b4f539SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
203340b4f539SKevin Wolf         mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
20341e1ea48dSStefan Hajnoczi         if (mcb->callbacks[i].free_qiov) {
20351e1ea48dSStefan Hajnoczi             qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
20361e1ea48dSStefan Hajnoczi         }
203740b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_qiov);
2038f8a83245SHerve Poussineau         qemu_vfree(mcb->callbacks[i].free_buf);
203940b4f539SKevin Wolf     }
204040b4f539SKevin Wolf }
204140b4f539SKevin Wolf 
204240b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret)
204340b4f539SKevin Wolf {
204440b4f539SKevin Wolf     MultiwriteCB *mcb = opaque;
204540b4f539SKevin Wolf 
2046cb6d3ca0SKevin Wolf     if (ret < 0 && !mcb->error) {
204740b4f539SKevin Wolf         mcb->error = ret;
204840b4f539SKevin Wolf     }
204940b4f539SKevin Wolf 
205040b4f539SKevin Wolf     mcb->num_requests--;
205140b4f539SKevin Wolf     if (mcb->num_requests == 0) {
205240b4f539SKevin Wolf         multiwrite_user_cb(mcb);
205340b4f539SKevin Wolf         qemu_free(mcb);
205440b4f539SKevin Wolf     }
205540b4f539SKevin Wolf }
205640b4f539SKevin Wolf 
205740b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b)
205840b4f539SKevin Wolf {
205977be4366SChristoph Hellwig     const BlockRequest *req1 = a, *req2 = b;
206077be4366SChristoph Hellwig 
206177be4366SChristoph Hellwig     /*
206277be4366SChristoph Hellwig      * Note that we can't simply subtract req2->sector from req1->sector
206377be4366SChristoph Hellwig      * here as that could overflow the return value.
206477be4366SChristoph Hellwig      */
206577be4366SChristoph Hellwig     if (req1->sector > req2->sector) {
206677be4366SChristoph Hellwig         return 1;
206777be4366SChristoph Hellwig     } else if (req1->sector < req2->sector) {
206877be4366SChristoph Hellwig         return -1;
206977be4366SChristoph Hellwig     } else {
207077be4366SChristoph Hellwig         return 0;
207177be4366SChristoph Hellwig     }
207240b4f539SKevin Wolf }
207340b4f539SKevin Wolf 
207440b4f539SKevin Wolf /*
207540b4f539SKevin Wolf  * Takes a bunch of requests and tries to merge them. Returns the number of
207640b4f539SKevin Wolf  * requests that remain after merging.
207740b4f539SKevin Wolf  */
207840b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
207940b4f539SKevin Wolf     int num_reqs, MultiwriteCB *mcb)
208040b4f539SKevin Wolf {
208140b4f539SKevin Wolf     int i, outidx;
208240b4f539SKevin Wolf 
208340b4f539SKevin Wolf     // Sort requests by start sector
208440b4f539SKevin Wolf     qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
208540b4f539SKevin Wolf 
208640b4f539SKevin Wolf     // Check if adjacent requests touch the same clusters. If so, combine them,
208740b4f539SKevin Wolf     // filling up gaps with zero sectors.
208840b4f539SKevin Wolf     outidx = 0;
208940b4f539SKevin Wolf     for (i = 1; i < num_reqs; i++) {
209040b4f539SKevin Wolf         int merge = 0;
209140b4f539SKevin Wolf         int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
209240b4f539SKevin Wolf 
209340b4f539SKevin Wolf         // This handles the cases that are valid for all block drivers, namely
209440b4f539SKevin Wolf         // exactly sequential writes and overlapping writes.
209540b4f539SKevin Wolf         if (reqs[i].sector <= oldreq_last) {
209640b4f539SKevin Wolf             merge = 1;
209740b4f539SKevin Wolf         }
209840b4f539SKevin Wolf 
209940b4f539SKevin Wolf         // The block driver may decide that it makes sense to combine requests
210040b4f539SKevin Wolf         // even if there is a gap of some sectors between them. In this case,
210140b4f539SKevin Wolf         // the gap is filled with zeros (therefore only applicable for yet
210240b4f539SKevin Wolf         // unused space in format like qcow2).
210340b4f539SKevin Wolf         if (!merge && bs->drv->bdrv_merge_requests) {
210440b4f539SKevin Wolf             merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
210540b4f539SKevin Wolf         }
210640b4f539SKevin Wolf 
2107e2a305fbSChristoph Hellwig         if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2108e2a305fbSChristoph Hellwig             merge = 0;
2109e2a305fbSChristoph Hellwig         }
2110e2a305fbSChristoph Hellwig 
211140b4f539SKevin Wolf         if (merge) {
211240b4f539SKevin Wolf             size_t size;
211340b4f539SKevin Wolf             QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
211440b4f539SKevin Wolf             qemu_iovec_init(qiov,
211540b4f539SKevin Wolf                 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
211640b4f539SKevin Wolf 
211740b4f539SKevin Wolf             // Add the first request to the merged one. If the requests are
211840b4f539SKevin Wolf             // overlapping, drop the last sectors of the first request.
211940b4f539SKevin Wolf             size = (reqs[i].sector - reqs[outidx].sector) << 9;
212040b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
212140b4f539SKevin Wolf 
212240b4f539SKevin Wolf             // We might need to add some zeros between the two requests
212340b4f539SKevin Wolf             if (reqs[i].sector > oldreq_last) {
212440b4f539SKevin Wolf                 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
212540b4f539SKevin Wolf                 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
212640b4f539SKevin Wolf                 memset(buf, 0, zero_bytes);
212740b4f539SKevin Wolf                 qemu_iovec_add(qiov, buf, zero_bytes);
212840b4f539SKevin Wolf                 mcb->callbacks[i].free_buf = buf;
212940b4f539SKevin Wolf             }
213040b4f539SKevin Wolf 
213140b4f539SKevin Wolf             // Add the second request
213240b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
213340b4f539SKevin Wolf 
2134cbf1dff2SKevin Wolf             reqs[outidx].nb_sectors = qiov->size >> 9;
213540b4f539SKevin Wolf             reqs[outidx].qiov = qiov;
213640b4f539SKevin Wolf 
213740b4f539SKevin Wolf             mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
213840b4f539SKevin Wolf         } else {
213940b4f539SKevin Wolf             outidx++;
214040b4f539SKevin Wolf             reqs[outidx].sector     = reqs[i].sector;
214140b4f539SKevin Wolf             reqs[outidx].nb_sectors = reqs[i].nb_sectors;
214240b4f539SKevin Wolf             reqs[outidx].qiov       = reqs[i].qiov;
214340b4f539SKevin Wolf         }
214440b4f539SKevin Wolf     }
214540b4f539SKevin Wolf 
214640b4f539SKevin Wolf     return outidx + 1;
214740b4f539SKevin Wolf }
214840b4f539SKevin Wolf 
214940b4f539SKevin Wolf /*
215040b4f539SKevin Wolf  * Submit multiple AIO write requests at once.
215140b4f539SKevin Wolf  *
215240b4f539SKevin Wolf  * On success, the function returns 0 and all requests in the reqs array have
215340b4f539SKevin Wolf  * been submitted. In error case this function returns -1, and any of the
215440b4f539SKevin Wolf  * requests may or may not be submitted yet. In particular, this means that the
215540b4f539SKevin Wolf  * callback will be called for some of the requests, for others it won't. The
215640b4f539SKevin Wolf  * caller must check the error field of the BlockRequest to wait for the right
215740b4f539SKevin Wolf  * callbacks (if error != 0, no callback will be called).
215840b4f539SKevin Wolf  *
215940b4f539SKevin Wolf  * The implementation may modify the contents of the reqs array, e.g. to merge
216040b4f539SKevin Wolf  * requests. However, the fields opaque and error are left unmodified as they
216140b4f539SKevin Wolf  * are used to signal failure for a single request to the caller.
216240b4f539SKevin Wolf  */
216340b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
216440b4f539SKevin Wolf {
216540b4f539SKevin Wolf     BlockDriverAIOCB *acb;
216640b4f539SKevin Wolf     MultiwriteCB *mcb;
216740b4f539SKevin Wolf     int i;
216840b4f539SKevin Wolf 
216940b4f539SKevin Wolf     if (num_reqs == 0) {
217040b4f539SKevin Wolf         return 0;
217140b4f539SKevin Wolf     }
217240b4f539SKevin Wolf 
217340b4f539SKevin Wolf     // Create MultiwriteCB structure
217440b4f539SKevin Wolf     mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
217540b4f539SKevin Wolf     mcb->num_requests = 0;
217640b4f539SKevin Wolf     mcb->num_callbacks = num_reqs;
217740b4f539SKevin Wolf 
217840b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
217940b4f539SKevin Wolf         mcb->callbacks[i].cb = reqs[i].cb;
218040b4f539SKevin Wolf         mcb->callbacks[i].opaque = reqs[i].opaque;
218140b4f539SKevin Wolf     }
218240b4f539SKevin Wolf 
218340b4f539SKevin Wolf     // Check for mergable requests
218440b4f539SKevin Wolf     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
218540b4f539SKevin Wolf 
2186453f9a16SKevin Wolf     /*
2187453f9a16SKevin Wolf      * Run the aio requests. As soon as one request can't be submitted
2188453f9a16SKevin Wolf      * successfully, fail all requests that are not yet submitted (we must
2189453f9a16SKevin Wolf      * return failure for all requests anyway)
2190453f9a16SKevin Wolf      *
2191453f9a16SKevin Wolf      * num_requests cannot be set to the right value immediately: If
2192453f9a16SKevin Wolf      * bdrv_aio_writev fails for some request, num_requests would be too high
2193453f9a16SKevin Wolf      * and therefore multiwrite_cb() would never recognize the multiwrite
2194453f9a16SKevin Wolf      * request as completed. We also cannot use the loop variable i to set it
2195453f9a16SKevin Wolf      * when the first request fails because the callback may already have been
2196453f9a16SKevin Wolf      * called for previously submitted requests. Thus, num_requests must be
2197453f9a16SKevin Wolf      * incremented for each request that is submitted.
2198453f9a16SKevin Wolf      *
2199453f9a16SKevin Wolf      * The problem that callbacks may be called early also means that we need
2200453f9a16SKevin Wolf      * to take care that num_requests doesn't become 0 before all requests are
2201453f9a16SKevin Wolf      * submitted - multiwrite_cb() would consider the multiwrite request
2202453f9a16SKevin Wolf      * completed. A dummy request that is "completed" by a manual call to
2203453f9a16SKevin Wolf      * multiwrite_cb() takes care of this.
2204453f9a16SKevin Wolf      */
2205453f9a16SKevin Wolf     mcb->num_requests = 1;
2206453f9a16SKevin Wolf 
220740b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
2208453f9a16SKevin Wolf         mcb->num_requests++;
220940b4f539SKevin Wolf         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
221040b4f539SKevin Wolf             reqs[i].nb_sectors, multiwrite_cb, mcb);
221140b4f539SKevin Wolf 
221240b4f539SKevin Wolf         if (acb == NULL) {
221340b4f539SKevin Wolf             // We can only fail the whole thing if no request has been
221440b4f539SKevin Wolf             // submitted yet. Otherwise we'll wait for the submitted AIOs to
221540b4f539SKevin Wolf             // complete and report the error in the callback.
2216453f9a16SKevin Wolf             if (i == 0) {
221740b4f539SKevin Wolf                 goto fail;
221840b4f539SKevin Wolf             } else {
22197eb58a6cSKevin Wolf                 multiwrite_cb(mcb, -EIO);
222040b4f539SKevin Wolf                 break;
222140b4f539SKevin Wolf             }
222240b4f539SKevin Wolf         }
222340b4f539SKevin Wolf     }
222440b4f539SKevin Wolf 
2225453f9a16SKevin Wolf     /* Complete the dummy request */
2226453f9a16SKevin Wolf     multiwrite_cb(mcb, 0);
2227453f9a16SKevin Wolf 
222840b4f539SKevin Wolf     return 0;
222940b4f539SKevin Wolf 
223040b4f539SKevin Wolf fail:
2231453f9a16SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
2232453f9a16SKevin Wolf         reqs[i].error = -EIO;
2233453f9a16SKevin Wolf     }
2234af474591SBruce Rogers     qemu_free(mcb);
223540b4f539SKevin Wolf     return -1;
223640b4f539SKevin Wolf }
223740b4f539SKevin Wolf 
2238b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2239b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2240b2e12bc6SChristoph Hellwig {
2241b2e12bc6SChristoph Hellwig     BlockDriver *drv = bs->drv;
2242b2e12bc6SChristoph Hellwig 
2243016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
2244016f5cf6SAlexander Graf         return bdrv_aio_noop_em(bs, cb, opaque);
2245016f5cf6SAlexander Graf     }
2246016f5cf6SAlexander Graf 
2247b2e12bc6SChristoph Hellwig     if (!drv)
2248b2e12bc6SChristoph Hellwig         return NULL;
2249b2e12bc6SChristoph Hellwig     return drv->bdrv_aio_flush(bs, cb, opaque);
2250b2e12bc6SChristoph Hellwig }
2251b2e12bc6SChristoph Hellwig 
225283f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
225383f64091Sbellard {
22546bbff9a0Saliguori     acb->pool->cancel(acb);
225583f64091Sbellard }
225683f64091Sbellard 
225783f64091Sbellard 
225883f64091Sbellard /**************************************************************/
225983f64091Sbellard /* async block device emulation */
226083f64091Sbellard 
2261c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync {
2262c16b5a2cSChristoph Hellwig     BlockDriverAIOCB common;
2263c16b5a2cSChristoph Hellwig     QEMUBH *bh;
2264c16b5a2cSChristoph Hellwig     int ret;
2265c16b5a2cSChristoph Hellwig     /* vector translation state */
2266c16b5a2cSChristoph Hellwig     QEMUIOVector *qiov;
2267c16b5a2cSChristoph Hellwig     uint8_t *bounce;
2268c16b5a2cSChristoph Hellwig     int is_write;
2269c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync;
2270c16b5a2cSChristoph Hellwig 
2271c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2272c16b5a2cSChristoph Hellwig {
2273b666d239SKevin Wolf     BlockDriverAIOCBSync *acb =
2274b666d239SKevin Wolf         container_of(blockacb, BlockDriverAIOCBSync, common);
22756a7ad299SDor Laor     qemu_bh_delete(acb->bh);
227636afc451SAvi Kivity     acb->bh = NULL;
2277c16b5a2cSChristoph Hellwig     qemu_aio_release(acb);
2278c16b5a2cSChristoph Hellwig }
2279c16b5a2cSChristoph Hellwig 
2280c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = {
2281c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2282c16b5a2cSChristoph Hellwig     .cancel             = bdrv_aio_cancel_em,
2283c16b5a2cSChristoph Hellwig };
2284c16b5a2cSChristoph Hellwig 
228583f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
2286beac80cdSbellard {
2287ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
2288f141eafeSaliguori 
2289f141eafeSaliguori     if (!acb->is_write)
2290f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2291ceb42de8Saliguori     qemu_vfree(acb->bounce);
2292ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
22936a7ad299SDor Laor     qemu_bh_delete(acb->bh);
229436afc451SAvi Kivity     acb->bh = NULL;
2295ce1a14dcSpbrook     qemu_aio_release(acb);
2296beac80cdSbellard }
2297beac80cdSbellard 
2298f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2299f141eafeSaliguori                                             int64_t sector_num,
2300f141eafeSaliguori                                             QEMUIOVector *qiov,
2301f141eafeSaliguori                                             int nb_sectors,
2302f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
2303f141eafeSaliguori                                             void *opaque,
2304f141eafeSaliguori                                             int is_write)
2305f141eafeSaliguori 
2306ea2384d3Sbellard {
2307ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
230883f64091Sbellard 
2309c16b5a2cSChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2310f141eafeSaliguori     acb->is_write = is_write;
2311f141eafeSaliguori     acb->qiov = qiov;
2312e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
2313f141eafeSaliguori 
2314ce1a14dcSpbrook     if (!acb->bh)
2315ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2316f141eafeSaliguori 
2317f141eafeSaliguori     if (is_write) {
2318f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2319f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2320f141eafeSaliguori     } else {
2321f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2322f141eafeSaliguori     }
2323f141eafeSaliguori 
2324ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
2325f141eafeSaliguori 
2326ce1a14dcSpbrook     return &acb->common;
23277a6cba61Spbrook }
23287a6cba61Spbrook 
2329f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2330f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2331ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
233283f64091Sbellard {
2333f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
233483f64091Sbellard }
233583f64091Sbellard 
2336f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2337f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2338f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
2339f141eafeSaliguori {
2340f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2341f141eafeSaliguori }
2342f141eafeSaliguori 
2343b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2344b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2345b2e12bc6SChristoph Hellwig {
2346b2e12bc6SChristoph Hellwig     BlockDriverAIOCBSync *acb;
2347b2e12bc6SChristoph Hellwig 
2348b2e12bc6SChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2349b2e12bc6SChristoph Hellwig     acb->is_write = 1; /* don't bounce in the completion hadler */
2350b2e12bc6SChristoph Hellwig     acb->qiov = NULL;
2351b2e12bc6SChristoph Hellwig     acb->bounce = NULL;
2352b2e12bc6SChristoph Hellwig     acb->ret = 0;
2353b2e12bc6SChristoph Hellwig 
2354b2e12bc6SChristoph Hellwig     if (!acb->bh)
2355b2e12bc6SChristoph Hellwig         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2356b2e12bc6SChristoph Hellwig 
2357b2e12bc6SChristoph Hellwig     bdrv_flush(bs);
2358b2e12bc6SChristoph Hellwig     qemu_bh_schedule(acb->bh);
2359b2e12bc6SChristoph Hellwig     return &acb->common;
2360b2e12bc6SChristoph Hellwig }
2361b2e12bc6SChristoph Hellwig 
2362016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2363016f5cf6SAlexander Graf         BlockDriverCompletionFunc *cb, void *opaque)
2364016f5cf6SAlexander Graf {
2365016f5cf6SAlexander Graf     BlockDriverAIOCBSync *acb;
2366016f5cf6SAlexander Graf 
2367016f5cf6SAlexander Graf     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2368016f5cf6SAlexander Graf     acb->is_write = 1; /* don't bounce in the completion handler */
2369016f5cf6SAlexander Graf     acb->qiov = NULL;
2370016f5cf6SAlexander Graf     acb->bounce = NULL;
2371016f5cf6SAlexander Graf     acb->ret = 0;
2372016f5cf6SAlexander Graf 
2373016f5cf6SAlexander Graf     if (!acb->bh) {
2374016f5cf6SAlexander Graf         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2375016f5cf6SAlexander Graf     }
2376016f5cf6SAlexander Graf 
2377016f5cf6SAlexander Graf     qemu_bh_schedule(acb->bh);
2378016f5cf6SAlexander Graf     return &acb->common;
2379016f5cf6SAlexander Graf }
2380016f5cf6SAlexander Graf 
238183f64091Sbellard /**************************************************************/
238283f64091Sbellard /* sync block device emulation */
238383f64091Sbellard 
238483f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
238583f64091Sbellard {
238683f64091Sbellard     *(int *)opaque = ret;
238783f64091Sbellard }
238883f64091Sbellard 
238983f64091Sbellard #define NOT_DONE 0x7fffffff
239083f64091Sbellard 
239183f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
239283f64091Sbellard                         uint8_t *buf, int nb_sectors)
239383f64091Sbellard {
2394ce1a14dcSpbrook     int async_ret;
2395ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2396f141eafeSaliguori     struct iovec iov;
2397f141eafeSaliguori     QEMUIOVector qiov;
239883f64091Sbellard 
239965d6b3d8SKevin Wolf     async_context_push();
240065d6b3d8SKevin Wolf 
240183f64091Sbellard     async_ret = NOT_DONE;
24023f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
2403eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2404f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2405f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
240683f64091Sbellard         bdrv_rw_em_cb, &async_ret);
240765d6b3d8SKevin Wolf     if (acb == NULL) {
240865d6b3d8SKevin Wolf         async_ret = -1;
240965d6b3d8SKevin Wolf         goto fail;
241065d6b3d8SKevin Wolf     }
2411baf35cb9Saliguori 
241283f64091Sbellard     while (async_ret == NOT_DONE) {
241383f64091Sbellard         qemu_aio_wait();
241483f64091Sbellard     }
2415baf35cb9Saliguori 
241665d6b3d8SKevin Wolf 
241765d6b3d8SKevin Wolf fail:
241865d6b3d8SKevin Wolf     async_context_pop();
241983f64091Sbellard     return async_ret;
242083f64091Sbellard }
242183f64091Sbellard 
242283f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
242383f64091Sbellard                          const uint8_t *buf, int nb_sectors)
242483f64091Sbellard {
2425ce1a14dcSpbrook     int async_ret;
2426ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2427f141eafeSaliguori     struct iovec iov;
2428f141eafeSaliguori     QEMUIOVector qiov;
242983f64091Sbellard 
243065d6b3d8SKevin Wolf     async_context_push();
243165d6b3d8SKevin Wolf 
243283f64091Sbellard     async_ret = NOT_DONE;
2433f141eafeSaliguori     iov.iov_base = (void *)buf;
2434eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2435f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2436f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
243783f64091Sbellard         bdrv_rw_em_cb, &async_ret);
243865d6b3d8SKevin Wolf     if (acb == NULL) {
243965d6b3d8SKevin Wolf         async_ret = -1;
244065d6b3d8SKevin Wolf         goto fail;
244165d6b3d8SKevin Wolf     }
244283f64091Sbellard     while (async_ret == NOT_DONE) {
244383f64091Sbellard         qemu_aio_wait();
244483f64091Sbellard     }
244565d6b3d8SKevin Wolf 
244665d6b3d8SKevin Wolf fail:
244765d6b3d8SKevin Wolf     async_context_pop();
244883f64091Sbellard     return async_ret;
244983f64091Sbellard }
2450ea2384d3Sbellard 
2451ea2384d3Sbellard void bdrv_init(void)
2452ea2384d3Sbellard {
24535efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
2454ea2384d3Sbellard }
2455ce1a14dcSpbrook 
2456eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void)
2457eb852011SMarkus Armbruster {
2458eb852011SMarkus Armbruster     use_bdrv_whitelist = 1;
2459eb852011SMarkus Armbruster     bdrv_init();
2460eb852011SMarkus Armbruster }
2461eb852011SMarkus Armbruster 
2462c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
24636bbff9a0Saliguori                    BlockDriverCompletionFunc *cb, void *opaque)
24646bbff9a0Saliguori {
2465ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2466ce1a14dcSpbrook 
24676bbff9a0Saliguori     if (pool->free_aiocb) {
24686bbff9a0Saliguori         acb = pool->free_aiocb;
24696bbff9a0Saliguori         pool->free_aiocb = acb->next;
2470ce1a14dcSpbrook     } else {
24716bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
24726bbff9a0Saliguori         acb->pool = pool;
2473ce1a14dcSpbrook     }
2474ce1a14dcSpbrook     acb->bs = bs;
2475ce1a14dcSpbrook     acb->cb = cb;
2476ce1a14dcSpbrook     acb->opaque = opaque;
2477ce1a14dcSpbrook     return acb;
2478ce1a14dcSpbrook }
2479ce1a14dcSpbrook 
2480ce1a14dcSpbrook void qemu_aio_release(void *p)
2481ce1a14dcSpbrook {
24826bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
24836bbff9a0Saliguori     AIOPool *pool = acb->pool;
24846bbff9a0Saliguori     acb->next = pool->free_aiocb;
24856bbff9a0Saliguori     pool->free_aiocb = acb;
2486ce1a14dcSpbrook }
248719cb3738Sbellard 
248819cb3738Sbellard /**************************************************************/
248919cb3738Sbellard /* removable device support */
249019cb3738Sbellard 
249119cb3738Sbellard /**
249219cb3738Sbellard  * Return TRUE if the media is present
249319cb3738Sbellard  */
249419cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
249519cb3738Sbellard {
249619cb3738Sbellard     BlockDriver *drv = bs->drv;
249719cb3738Sbellard     int ret;
249819cb3738Sbellard     if (!drv)
249919cb3738Sbellard         return 0;
250019cb3738Sbellard     if (!drv->bdrv_is_inserted)
250119cb3738Sbellard         return 1;
250219cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
250319cb3738Sbellard     return ret;
250419cb3738Sbellard }
250519cb3738Sbellard 
250619cb3738Sbellard /**
250719cb3738Sbellard  * Return TRUE if the media changed since the last call to this
250819cb3738Sbellard  * function. It is currently only used for floppy disks
250919cb3738Sbellard  */
251019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
251119cb3738Sbellard {
251219cb3738Sbellard     BlockDriver *drv = bs->drv;
251319cb3738Sbellard     int ret;
251419cb3738Sbellard 
251519cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
251619cb3738Sbellard         ret = -ENOTSUP;
251719cb3738Sbellard     else
251819cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
251919cb3738Sbellard     if (ret == -ENOTSUP)
252019cb3738Sbellard         ret = bs->media_changed;
252119cb3738Sbellard     bs->media_changed = 0;
252219cb3738Sbellard     return ret;
252319cb3738Sbellard }
252419cb3738Sbellard 
252519cb3738Sbellard /**
252619cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
252719cb3738Sbellard  */
2528aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag)
252919cb3738Sbellard {
253019cb3738Sbellard     BlockDriver *drv = bs->drv;
253119cb3738Sbellard     int ret;
253219cb3738Sbellard 
2533aea2a33cSMark McLoughlin     if (bs->locked) {
2534aea2a33cSMark McLoughlin         return -EBUSY;
2535aea2a33cSMark McLoughlin     }
2536aea2a33cSMark McLoughlin 
253719cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
253819cb3738Sbellard         ret = -ENOTSUP;
253919cb3738Sbellard     } else {
254019cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
254119cb3738Sbellard     }
254219cb3738Sbellard     if (ret == -ENOTSUP) {
254319cb3738Sbellard         if (eject_flag)
254419cb3738Sbellard             bdrv_close(bs);
2545aea2a33cSMark McLoughlin         ret = 0;
254619cb3738Sbellard     }
2547aea2a33cSMark McLoughlin 
2548aea2a33cSMark McLoughlin     return ret;
254919cb3738Sbellard }
255019cb3738Sbellard 
255119cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
255219cb3738Sbellard {
255319cb3738Sbellard     return bs->locked;
255419cb3738Sbellard }
255519cb3738Sbellard 
255619cb3738Sbellard /**
255719cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
255819cb3738Sbellard  * to eject it manually).
255919cb3738Sbellard  */
256019cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
256119cb3738Sbellard {
256219cb3738Sbellard     BlockDriver *drv = bs->drv;
256319cb3738Sbellard 
256419cb3738Sbellard     bs->locked = locked;
256519cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
256619cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
256719cb3738Sbellard     }
256819cb3738Sbellard }
2569985a03b0Sths 
2570985a03b0Sths /* needed for generic scsi interface */
2571985a03b0Sths 
2572985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2573985a03b0Sths {
2574985a03b0Sths     BlockDriver *drv = bs->drv;
2575985a03b0Sths 
2576985a03b0Sths     if (drv && drv->bdrv_ioctl)
2577985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
2578985a03b0Sths     return -ENOTSUP;
2579985a03b0Sths }
25807d780669Saliguori 
2581221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2582221f715dSaliguori         unsigned long int req, void *buf,
25837d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
25847d780669Saliguori {
2585221f715dSaliguori     BlockDriver *drv = bs->drv;
25867d780669Saliguori 
2587221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
2588221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2589221f715dSaliguori     return NULL;
25907d780669Saliguori }
2591e268ca52Saliguori 
25927cd1e32aSlirans@il.ibm.com 
25937cd1e32aSlirans@il.ibm.com 
2594e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
2595e268ca52Saliguori {
2596e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2597e268ca52Saliguori }
25987cd1e32aSlirans@il.ibm.com 
25997cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
26007cd1e32aSlirans@il.ibm.com {
26017cd1e32aSlirans@il.ibm.com     int64_t bitmap_size;
2602a55eb92cSJan Kiszka 
2603aaa0eb75SLiran Schour     bs->dirty_count = 0;
26047cd1e32aSlirans@il.ibm.com     if (enable) {
2605c6d22830SJan Kiszka         if (!bs->dirty_bitmap) {
2606c6d22830SJan Kiszka             bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2607c6d22830SJan Kiszka                     BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2608c6d22830SJan Kiszka             bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
26097cd1e32aSlirans@il.ibm.com 
26107cd1e32aSlirans@il.ibm.com             bs->dirty_bitmap = qemu_mallocz(bitmap_size);
26117cd1e32aSlirans@il.ibm.com         }
26127cd1e32aSlirans@il.ibm.com     } else {
2613c6d22830SJan Kiszka         if (bs->dirty_bitmap) {
26147cd1e32aSlirans@il.ibm.com             qemu_free(bs->dirty_bitmap);
2615c6d22830SJan Kiszka             bs->dirty_bitmap = NULL;
26167cd1e32aSlirans@il.ibm.com         }
26177cd1e32aSlirans@il.ibm.com     }
26187cd1e32aSlirans@il.ibm.com }
26197cd1e32aSlirans@il.ibm.com 
26207cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
26217cd1e32aSlirans@il.ibm.com {
26226ea44308SJan Kiszka     int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
26237cd1e32aSlirans@il.ibm.com 
2624c6d22830SJan Kiszka     if (bs->dirty_bitmap &&
2625c6d22830SJan Kiszka         (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2626c6d22830SJan Kiszka         return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2627c6d22830SJan Kiszka             (1 << (chunk % (sizeof(unsigned long) * 8)));
26287cd1e32aSlirans@il.ibm.com     } else {
26297cd1e32aSlirans@il.ibm.com         return 0;
26307cd1e32aSlirans@il.ibm.com     }
26317cd1e32aSlirans@il.ibm.com }
26327cd1e32aSlirans@il.ibm.com 
26337cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
26347cd1e32aSlirans@il.ibm.com                       int nr_sectors)
26357cd1e32aSlirans@il.ibm.com {
26367cd1e32aSlirans@il.ibm.com     set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
26377cd1e32aSlirans@il.ibm.com }
2638aaa0eb75SLiran Schour 
2639aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2640aaa0eb75SLiran Schour {
2641aaa0eb75SLiran Schour     return bs->dirty_count;
2642aaa0eb75SLiran Schour }
2643