xref: /openbmc/qemu/block.c (revision 4be9762adb0947a353e6efef2fed354f69218bfb)
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 
333c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv)
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);
341c98ac35dSStefan Weil     if (ret < 0) {
342c98ac35dSStefan Weil         *pdrv = NULL;
343c98ac35dSStefan Weil         return ret;
344c98ac35dSStefan Weil     }
345f8ea0b00SNicholas Bellinger 
34608a00559SKevin Wolf     /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
34708a00559SKevin Wolf     if (bs->sg || !bdrv_is_inserted(bs)) {
3481a396859SNicholas A. Bellinger         bdrv_delete(bs);
349c98ac35dSStefan Weil         drv = bdrv_find_format("raw");
350c98ac35dSStefan Weil         if (!drv) {
351c98ac35dSStefan Weil             ret = -ENOENT;
352c98ac35dSStefan Weil         }
353c98ac35dSStefan Weil         *pdrv = drv;
354c98ac35dSStefan Weil         return ret;
3551a396859SNicholas A. Bellinger     }
356f8ea0b00SNicholas Bellinger 
35783f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
35883f64091Sbellard     bdrv_delete(bs);
359ea2384d3Sbellard     if (ret < 0) {
360c98ac35dSStefan Weil         *pdrv = NULL;
361c98ac35dSStefan Weil         return ret;
362ea2384d3Sbellard     }
363ea2384d3Sbellard 
364ea2384d3Sbellard     score_max = 0;
36584a12e66SChristoph Hellwig     drv = NULL;
3668a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv1, &bdrv_drivers, list) {
36783f64091Sbellard         if (drv1->bdrv_probe) {
368ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
369ea2384d3Sbellard             if (score > score_max) {
370ea2384d3Sbellard                 score_max = score;
371ea2384d3Sbellard                 drv = drv1;
372ea2384d3Sbellard             }
373ea2384d3Sbellard         }
37483f64091Sbellard     }
375c98ac35dSStefan Weil     if (!drv) {
376c98ac35dSStefan Weil         ret = -ENOENT;
377c98ac35dSStefan Weil     }
378c98ac35dSStefan Weil     *pdrv = drv;
379c98ac35dSStefan Weil     return ret;
380ea2384d3Sbellard }
381ea2384d3Sbellard 
38251762288SStefan Hajnoczi /**
38351762288SStefan Hajnoczi  * Set the current 'total_sectors' value
38451762288SStefan Hajnoczi  */
38551762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
38651762288SStefan Hajnoczi {
38751762288SStefan Hajnoczi     BlockDriver *drv = bs->drv;
38851762288SStefan Hajnoczi 
389396759adSNicholas Bellinger     /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
390396759adSNicholas Bellinger     if (bs->sg)
391396759adSNicholas Bellinger         return 0;
392396759adSNicholas Bellinger 
39351762288SStefan Hajnoczi     /* query actual device if possible, otherwise just trust the hint */
39451762288SStefan Hajnoczi     if (drv->bdrv_getlength) {
39551762288SStefan Hajnoczi         int64_t length = drv->bdrv_getlength(bs);
39651762288SStefan Hajnoczi         if (length < 0) {
39751762288SStefan Hajnoczi             return length;
39851762288SStefan Hajnoczi         }
39951762288SStefan Hajnoczi         hint = length >> BDRV_SECTOR_BITS;
40051762288SStefan Hajnoczi     }
40151762288SStefan Hajnoczi 
40251762288SStefan Hajnoczi     bs->total_sectors = hint;
40351762288SStefan Hajnoczi     return 0;
40451762288SStefan Hajnoczi }
40551762288SStefan Hajnoczi 
406b6ce07aaSKevin Wolf /*
40757915332SKevin Wolf  * Common part for opening disk images and files
40857915332SKevin Wolf  */
40957915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename,
41057915332SKevin Wolf     int flags, BlockDriver *drv)
41157915332SKevin Wolf {
41257915332SKevin Wolf     int ret, open_flags;
41357915332SKevin Wolf 
41457915332SKevin Wolf     assert(drv != NULL);
41557915332SKevin Wolf 
41666f82ceeSKevin Wolf     bs->file = NULL;
41751762288SStefan Hajnoczi     bs->total_sectors = 0;
41857915332SKevin Wolf     bs->encrypted = 0;
41957915332SKevin Wolf     bs->valid_key = 0;
42057915332SKevin Wolf     bs->open_flags = flags;
42157915332SKevin Wolf     /* buffer_alignment defaulted to 512, drivers can change this value */
42257915332SKevin Wolf     bs->buffer_alignment = 512;
42357915332SKevin Wolf 
42457915332SKevin Wolf     pstrcpy(bs->filename, sizeof(bs->filename), filename);
42557915332SKevin Wolf 
42657915332SKevin Wolf     if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
42757915332SKevin Wolf         return -ENOTSUP;
42857915332SKevin Wolf     }
42957915332SKevin Wolf 
43057915332SKevin Wolf     bs->drv = drv;
43157915332SKevin Wolf     bs->opaque = qemu_mallocz(drv->instance_size);
43257915332SKevin Wolf 
43357915332SKevin Wolf     /*
43457915332SKevin Wolf      * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
43557915332SKevin Wolf      * write cache to the guest.  We do need the fdatasync to flush
43657915332SKevin Wolf      * out transactions for block allocations, and we maybe have a
43757915332SKevin Wolf      * volatile write cache in our backing device to deal with.
43857915332SKevin Wolf      */
43957915332SKevin Wolf     if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
44057915332SKevin Wolf         bs->enable_write_cache = 1;
44157915332SKevin Wolf 
44257915332SKevin Wolf     /*
44357915332SKevin Wolf      * Clear flags that are internal to the block layer before opening the
44457915332SKevin Wolf      * image.
44557915332SKevin Wolf      */
44657915332SKevin Wolf     open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
44757915332SKevin Wolf 
44857915332SKevin Wolf     /*
44957915332SKevin Wolf      * Snapshots should be writeable.
45057915332SKevin Wolf      */
45157915332SKevin Wolf     if (bs->is_temporary) {
45257915332SKevin Wolf         open_flags |= BDRV_O_RDWR;
45357915332SKevin Wolf     }
45457915332SKevin Wolf 
45566f82ceeSKevin Wolf     /* Open the image, either directly or using a protocol */
45666f82ceeSKevin Wolf     if (drv->bdrv_file_open) {
45766f82ceeSKevin Wolf         ret = drv->bdrv_file_open(bs, filename, open_flags);
45866f82ceeSKevin Wolf     } else {
45966f82ceeSKevin Wolf         ret = bdrv_file_open(&bs->file, filename, open_flags);
46066f82ceeSKevin Wolf         if (ret >= 0) {
46166f82ceeSKevin Wolf             ret = drv->bdrv_open(bs, open_flags);
46266f82ceeSKevin Wolf         }
46366f82ceeSKevin Wolf     }
46466f82ceeSKevin Wolf 
46557915332SKevin Wolf     if (ret < 0) {
46657915332SKevin Wolf         goto free_and_fail;
46757915332SKevin Wolf     }
46857915332SKevin Wolf 
46957915332SKevin Wolf     bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
47051762288SStefan Hajnoczi 
47151762288SStefan Hajnoczi     ret = refresh_total_sectors(bs, bs->total_sectors);
47251762288SStefan Hajnoczi     if (ret < 0) {
47351762288SStefan Hajnoczi         goto free_and_fail;
47457915332SKevin Wolf     }
47551762288SStefan Hajnoczi 
47657915332SKevin Wolf #ifndef _WIN32
47757915332SKevin Wolf     if (bs->is_temporary) {
47857915332SKevin Wolf         unlink(filename);
47957915332SKevin Wolf     }
48057915332SKevin Wolf #endif
48157915332SKevin Wolf     return 0;
48257915332SKevin Wolf 
48357915332SKevin Wolf free_and_fail:
48466f82ceeSKevin Wolf     if (bs->file) {
48566f82ceeSKevin Wolf         bdrv_delete(bs->file);
48666f82ceeSKevin Wolf         bs->file = NULL;
48766f82ceeSKevin Wolf     }
48857915332SKevin Wolf     qemu_free(bs->opaque);
48957915332SKevin Wolf     bs->opaque = NULL;
49057915332SKevin Wolf     bs->drv = NULL;
49157915332SKevin Wolf     return ret;
49257915332SKevin Wolf }
49357915332SKevin Wolf 
49457915332SKevin Wolf /*
495b6ce07aaSKevin Wolf  * Opens a file using a protocol (file, host_device, nbd, ...)
496b6ce07aaSKevin Wolf  */
49783f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
498b338082bSbellard {
49983f64091Sbellard     BlockDriverState *bs;
5006db95603SChristoph Hellwig     BlockDriver *drv;
50183f64091Sbellard     int ret;
5023b0d4f61Sbellard 
503b50cbabcSMORITA Kazutaka     drv = bdrv_find_protocol(filename);
5046db95603SChristoph Hellwig     if (!drv) {
5056db95603SChristoph Hellwig         return -ENOENT;
5066db95603SChristoph Hellwig     }
5076db95603SChristoph Hellwig 
50883f64091Sbellard     bs = bdrv_new("");
509b6ce07aaSKevin Wolf     ret = bdrv_open_common(bs, filename, flags, drv);
51083f64091Sbellard     if (ret < 0) {
51183f64091Sbellard         bdrv_delete(bs);
51283f64091Sbellard         return ret;
5133b0d4f61Sbellard     }
51471d0770cSaliguori     bs->growable = 1;
51583f64091Sbellard     *pbs = bs;
51683f64091Sbellard     return 0;
5173b0d4f61Sbellard }
5183b0d4f61Sbellard 
519b6ce07aaSKevin Wolf /*
520b6ce07aaSKevin Wolf  * Opens a disk image (raw, qcow2, vmdk, ...)
521b6ce07aaSKevin Wolf  */
522d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
523ea2384d3Sbellard               BlockDriver *drv)
524ea2384d3Sbellard {
525b6ce07aaSKevin Wolf     int ret;
52679368c81SAnthony Liguori     int probed = 0;
52733e3963eSbellard 
52883f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
529ea2384d3Sbellard         BlockDriverState *bs1;
530ea2384d3Sbellard         int64_t total_size;
5317c96d46eSaliguori         int is_protocol = 0;
53291a073a9SKevin Wolf         BlockDriver *bdrv_qcow2;
53391a073a9SKevin Wolf         QEMUOptionParameter *options;
534b6ce07aaSKevin Wolf         char tmp_filename[PATH_MAX];
535b6ce07aaSKevin Wolf         char backing_filename[PATH_MAX];
53633e3963eSbellard 
537ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
538ea2384d3Sbellard            instead of opening 'filename' directly */
539ea2384d3Sbellard 
540ea2384d3Sbellard         /* if there is a backing file, use it */
541ea2384d3Sbellard         bs1 = bdrv_new("");
542d6e9098eSKevin Wolf         ret = bdrv_open(bs1, filename, 0, drv);
54351d7c00cSaliguori         if (ret < 0) {
544ea2384d3Sbellard             bdrv_delete(bs1);
54551d7c00cSaliguori             return ret;
546ea2384d3Sbellard         }
5473e82990bSJes Sorensen         total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
5487c96d46eSaliguori 
5497c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
5507c96d46eSaliguori             is_protocol = 1;
5517c96d46eSaliguori 
552ea2384d3Sbellard         bdrv_delete(bs1);
553ea2384d3Sbellard 
554ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
5557c96d46eSaliguori 
5567c96d46eSaliguori         /* Real path is meaningless for protocols */
5577c96d46eSaliguori         if (is_protocol)
5587c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
5597c96d46eSaliguori                      "%s", filename);
560114cdfa9SKirill A. Shutemov         else if (!realpath(filename, backing_filename))
561114cdfa9SKirill A. Shutemov             return -errno;
5627c96d46eSaliguori 
56391a073a9SKevin Wolf         bdrv_qcow2 = bdrv_find_format("qcow2");
56491a073a9SKevin Wolf         options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
56591a073a9SKevin Wolf 
5663e82990bSJes Sorensen         set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
56791a073a9SKevin Wolf         set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
56891a073a9SKevin Wolf         if (drv) {
56991a073a9SKevin Wolf             set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
57091a073a9SKevin Wolf                 drv->format_name);
57191a073a9SKevin Wolf         }
57291a073a9SKevin Wolf 
57391a073a9SKevin Wolf         ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
574d748768cSJan Kiszka         free_option_parameters(options);
57551d7c00cSaliguori         if (ret < 0) {
57651d7c00cSaliguori             return ret;
577ea2384d3Sbellard         }
57891a073a9SKevin Wolf 
579ea2384d3Sbellard         filename = tmp_filename;
58091a073a9SKevin Wolf         drv = bdrv_qcow2;
581ea2384d3Sbellard         bs->is_temporary = 1;
582ea2384d3Sbellard     }
583ea2384d3Sbellard 
584b6ce07aaSKevin Wolf     /* Find the right image format driver */
5856db95603SChristoph Hellwig     if (!drv) {
586c98ac35dSStefan Weil         ret = find_image_format(filename, &drv);
58779368c81SAnthony Liguori         probed = 1;
588ea2384d3Sbellard     }
5896987307cSChristoph Hellwig 
59051d7c00cSaliguori     if (!drv) {
59151d7c00cSaliguori         goto unlink_and_fail;
59283f64091Sbellard     }
593b6ce07aaSKevin Wolf 
594b6ce07aaSKevin Wolf     /* Open the image */
595b6ce07aaSKevin Wolf     ret = bdrv_open_common(bs, filename, flags, drv);
596b6ce07aaSKevin Wolf     if (ret < 0) {
5976987307cSChristoph Hellwig         goto unlink_and_fail;
5986987307cSChristoph Hellwig     }
5996987307cSChristoph Hellwig 
60079368c81SAnthony Liguori     bs->probed = probed;
60179368c81SAnthony Liguori 
602b6ce07aaSKevin Wolf     /* If there is a backing file, use it */
603b6ce07aaSKevin Wolf     if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
604b6ce07aaSKevin Wolf         char backing_filename[PATH_MAX];
605b6ce07aaSKevin Wolf         int back_flags;
606b6ce07aaSKevin Wolf         BlockDriver *back_drv = NULL;
607b6ce07aaSKevin Wolf 
608b6ce07aaSKevin Wolf         bs->backing_hd = bdrv_new("");
609b6ce07aaSKevin Wolf         path_combine(backing_filename, sizeof(backing_filename),
610b6ce07aaSKevin Wolf                      filename, bs->backing_file);
611b6ce07aaSKevin Wolf         if (bs->backing_format[0] != '\0')
612b6ce07aaSKevin Wolf             back_drv = bdrv_find_format(bs->backing_format);
613b6ce07aaSKevin Wolf 
614b6ce07aaSKevin Wolf         /* backing files always opened read-only */
615b6ce07aaSKevin Wolf         back_flags =
616b6ce07aaSKevin Wolf             flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
617b6ce07aaSKevin Wolf 
618b6ce07aaSKevin Wolf         ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
619b6ce07aaSKevin Wolf         if (ret < 0) {
620b6ce07aaSKevin Wolf             bdrv_close(bs);
621b6ce07aaSKevin Wolf             return ret;
622b6ce07aaSKevin Wolf         }
623b6ce07aaSKevin Wolf         if (bs->is_temporary) {
624b6ce07aaSKevin Wolf             bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
625b6ce07aaSKevin Wolf         } else {
626b6ce07aaSKevin Wolf             /* base image inherits from "parent" */
627b6ce07aaSKevin Wolf             bs->backing_hd->keep_read_only = bs->keep_read_only;
628b6ce07aaSKevin Wolf         }
629b6ce07aaSKevin Wolf     }
630b6ce07aaSKevin Wolf 
631b6ce07aaSKevin Wolf     if (!bdrv_key_required(bs)) {
632b6ce07aaSKevin Wolf         /* call the change callback */
633b6ce07aaSKevin Wolf         bs->media_changed = 1;
634b6ce07aaSKevin Wolf         if (bs->change_cb)
635b6ce07aaSKevin Wolf             bs->change_cb(bs->change_opaque);
636b6ce07aaSKevin Wolf     }
637b6ce07aaSKevin Wolf 
638b6ce07aaSKevin Wolf     return 0;
639b6ce07aaSKevin Wolf 
640b6ce07aaSKevin Wolf unlink_and_fail:
641b6ce07aaSKevin Wolf     if (bs->is_temporary) {
642b6ce07aaSKevin Wolf         unlink(filename);
643b6ce07aaSKevin Wolf     }
644b6ce07aaSKevin Wolf     return ret;
645b6ce07aaSKevin Wolf }
646b6ce07aaSKevin Wolf 
647fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
648fc01f7e7Sbellard {
64919cb3738Sbellard     if (bs->drv) {
650f9092b10SMarkus Armbruster         if (bs == bs_snapshots) {
651f9092b10SMarkus Armbruster             bs_snapshots = NULL;
652f9092b10SMarkus Armbruster         }
653557df6acSStefan Hajnoczi         if (bs->backing_hd) {
654ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
655557df6acSStefan Hajnoczi             bs->backing_hd = NULL;
656557df6acSStefan Hajnoczi         }
657ea2384d3Sbellard         bs->drv->bdrv_close(bs);
658ea2384d3Sbellard         qemu_free(bs->opaque);
659ea2384d3Sbellard #ifdef _WIN32
660ea2384d3Sbellard         if (bs->is_temporary) {
661ea2384d3Sbellard             unlink(bs->filename);
662ea2384d3Sbellard         }
66367b915a5Sbellard #endif
664ea2384d3Sbellard         bs->opaque = NULL;
665ea2384d3Sbellard         bs->drv = NULL;
666b338082bSbellard 
66766f82ceeSKevin Wolf         if (bs->file != NULL) {
66866f82ceeSKevin Wolf             bdrv_close(bs->file);
66966f82ceeSKevin Wolf         }
67066f82ceeSKevin Wolf 
671b338082bSbellard         /* call the change callback */
67219cb3738Sbellard         bs->media_changed = 1;
673b338082bSbellard         if (bs->change_cb)
674b338082bSbellard             bs->change_cb(bs->change_opaque);
675b338082bSbellard     }
676b338082bSbellard }
677b338082bSbellard 
6782bc93fedSMORITA Kazutaka void bdrv_close_all(void)
6792bc93fedSMORITA Kazutaka {
6802bc93fedSMORITA Kazutaka     BlockDriverState *bs;
6812bc93fedSMORITA Kazutaka 
6822bc93fedSMORITA Kazutaka     QTAILQ_FOREACH(bs, &bdrv_states, list) {
6832bc93fedSMORITA Kazutaka         bdrv_close(bs);
6842bc93fedSMORITA Kazutaka     }
6852bc93fedSMORITA Kazutaka }
6862bc93fedSMORITA Kazutaka 
687b338082bSbellard void bdrv_delete(BlockDriverState *bs)
688b338082bSbellard {
68918846deeSMarkus Armbruster     assert(!bs->peer);
69018846deeSMarkus Armbruster 
6911b7bdbc1SStefan Hajnoczi     /* remove from list, if necessary */
6921b7bdbc1SStefan Hajnoczi     if (bs->device_name[0] != '\0') {
6931b7bdbc1SStefan Hajnoczi         QTAILQ_REMOVE(&bdrv_states, bs, list);
6941b7bdbc1SStefan Hajnoczi     }
69534c6f050Saurel32 
696b338082bSbellard     bdrv_close(bs);
69766f82ceeSKevin Wolf     if (bs->file != NULL) {
69866f82ceeSKevin Wolf         bdrv_delete(bs->file);
69966f82ceeSKevin Wolf     }
70066f82ceeSKevin Wolf 
701f9092b10SMarkus Armbruster     assert(bs != bs_snapshots);
702b338082bSbellard     qemu_free(bs);
703fc01f7e7Sbellard }
704fc01f7e7Sbellard 
70518846deeSMarkus Armbruster int bdrv_attach(BlockDriverState *bs, DeviceState *qdev)
70618846deeSMarkus Armbruster {
70718846deeSMarkus Armbruster     if (bs->peer) {
70818846deeSMarkus Armbruster         return -EBUSY;
70918846deeSMarkus Armbruster     }
71018846deeSMarkus Armbruster     bs->peer = qdev;
71118846deeSMarkus Armbruster     return 0;
71218846deeSMarkus Armbruster }
71318846deeSMarkus Armbruster 
71418846deeSMarkus Armbruster void bdrv_detach(BlockDriverState *bs, DeviceState *qdev)
71518846deeSMarkus Armbruster {
71618846deeSMarkus Armbruster     assert(bs->peer == qdev);
71718846deeSMarkus Armbruster     bs->peer = NULL;
71818846deeSMarkus Armbruster }
71918846deeSMarkus Armbruster 
72018846deeSMarkus Armbruster DeviceState *bdrv_get_attached(BlockDriverState *bs)
72118846deeSMarkus Armbruster {
72218846deeSMarkus Armbruster     return bs->peer;
72318846deeSMarkus Armbruster }
72418846deeSMarkus Armbruster 
725e97fc193Saliguori /*
726e97fc193Saliguori  * Run consistency checks on an image
727e97fc193Saliguori  *
728e076f338SKevin Wolf  * Returns 0 if the check could be completed (it doesn't mean that the image is
729e076f338SKevin Wolf  * free of errors) or -errno when an internal error occured. The results of the
730e076f338SKevin Wolf  * check are stored in res.
731e97fc193Saliguori  */
732e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
733e97fc193Saliguori {
734e97fc193Saliguori     if (bs->drv->bdrv_check == NULL) {
735e97fc193Saliguori         return -ENOTSUP;
736e97fc193Saliguori     }
737e97fc193Saliguori 
738e076f338SKevin Wolf     memset(res, 0, sizeof(*res));
7399ac228e0SKevin Wolf     return bs->drv->bdrv_check(bs, res);
740e97fc193Saliguori }
741e97fc193Saliguori 
7428a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048
7438a426614SKevin Wolf 
74433e3963eSbellard /* commit COW file into the raw image */
74533e3963eSbellard int bdrv_commit(BlockDriverState *bs)
74633e3963eSbellard {
74719cb3738Sbellard     BlockDriver *drv = bs->drv;
7488a426614SKevin Wolf     int64_t sector, total_sectors;
7498a426614SKevin Wolf     int n, ro, open_flags;
7504dca4b63SNaphtali Sprei     int ret = 0, rw_ret = 0;
7518a426614SKevin Wolf     uint8_t *buf;
7524dca4b63SNaphtali Sprei     char filename[1024];
7534dca4b63SNaphtali Sprei     BlockDriverState *bs_rw, *bs_ro;
75433e3963eSbellard 
75519cb3738Sbellard     if (!drv)
75619cb3738Sbellard         return -ENOMEDIUM;
75733e3963eSbellard 
7584dca4b63SNaphtali Sprei     if (!bs->backing_hd) {
7594dca4b63SNaphtali Sprei         return -ENOTSUP;
7604dca4b63SNaphtali Sprei     }
7614dca4b63SNaphtali Sprei 
7624dca4b63SNaphtali Sprei     if (bs->backing_hd->keep_read_only) {
763ea2384d3Sbellard         return -EACCES;
76433e3963eSbellard     }
76533e3963eSbellard 
7664dca4b63SNaphtali Sprei     ro = bs->backing_hd->read_only;
7674dca4b63SNaphtali Sprei     strncpy(filename, bs->backing_hd->filename, sizeof(filename));
7684dca4b63SNaphtali Sprei     open_flags =  bs->backing_hd->open_flags;
7694dca4b63SNaphtali Sprei 
7704dca4b63SNaphtali Sprei     if (ro) {
7714dca4b63SNaphtali Sprei         /* re-open as RW */
7724dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
7734dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
7744dca4b63SNaphtali Sprei         bs_rw = bdrv_new("");
775c3349197SKevin Wolf         rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
7764dca4b63SNaphtali Sprei         if (rw_ret < 0) {
7774dca4b63SNaphtali Sprei             bdrv_delete(bs_rw);
7784dca4b63SNaphtali Sprei             /* try to re-open read-only */
7794dca4b63SNaphtali Sprei             bs_ro = bdrv_new("");
780c3349197SKevin Wolf             ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
7814dca4b63SNaphtali Sprei             if (ret < 0) {
7824dca4b63SNaphtali Sprei                 bdrv_delete(bs_ro);
7834dca4b63SNaphtali Sprei                 /* drive not functional anymore */
7844dca4b63SNaphtali Sprei                 bs->drv = NULL;
7854dca4b63SNaphtali Sprei                 return ret;
7864dca4b63SNaphtali Sprei             }
7874dca4b63SNaphtali Sprei             bs->backing_hd = bs_ro;
7884dca4b63SNaphtali Sprei             return rw_ret;
7894dca4b63SNaphtali Sprei         }
7904dca4b63SNaphtali Sprei         bs->backing_hd = bs_rw;
791ea2384d3Sbellard     }
792ea2384d3Sbellard 
7936ea44308SJan Kiszka     total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
7948a426614SKevin Wolf     buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
7958a426614SKevin Wolf 
7968a426614SKevin Wolf     for (sector = 0; sector < total_sectors; sector += n) {
7978a426614SKevin Wolf         if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
7988a426614SKevin Wolf 
7998a426614SKevin Wolf             if (bdrv_read(bs, sector, buf, n) != 0) {
8004dca4b63SNaphtali Sprei                 ret = -EIO;
8014dca4b63SNaphtali Sprei                 goto ro_cleanup;
80233e3963eSbellard             }
80333e3963eSbellard 
8048a426614SKevin Wolf             if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
8054dca4b63SNaphtali Sprei                 ret = -EIO;
8064dca4b63SNaphtali Sprei                 goto ro_cleanup;
80733e3963eSbellard             }
80833e3963eSbellard         }
80933e3963eSbellard     }
81095389c86Sbellard 
8111d44952fSChristoph Hellwig     if (drv->bdrv_make_empty) {
8121d44952fSChristoph Hellwig         ret = drv->bdrv_make_empty(bs);
8131d44952fSChristoph Hellwig         bdrv_flush(bs);
8141d44952fSChristoph Hellwig     }
81595389c86Sbellard 
8163f5075aeSChristoph Hellwig     /*
8173f5075aeSChristoph Hellwig      * Make sure all data we wrote to the backing device is actually
8183f5075aeSChristoph Hellwig      * stable on disk.
8193f5075aeSChristoph Hellwig      */
8203f5075aeSChristoph Hellwig     if (bs->backing_hd)
8213f5075aeSChristoph Hellwig         bdrv_flush(bs->backing_hd);
8224dca4b63SNaphtali Sprei 
8234dca4b63SNaphtali Sprei ro_cleanup:
8248a426614SKevin Wolf     qemu_free(buf);
8254dca4b63SNaphtali Sprei 
8264dca4b63SNaphtali Sprei     if (ro) {
8274dca4b63SNaphtali Sprei         /* re-open as RO */
8284dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
8294dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
8304dca4b63SNaphtali Sprei         bs_ro = bdrv_new("");
831c3349197SKevin Wolf         ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
8324dca4b63SNaphtali Sprei         if (ret < 0) {
8334dca4b63SNaphtali Sprei             bdrv_delete(bs_ro);
8344dca4b63SNaphtali Sprei             /* drive not functional anymore */
8354dca4b63SNaphtali Sprei             bs->drv = NULL;
8364dca4b63SNaphtali Sprei             return ret;
8374dca4b63SNaphtali Sprei         }
8384dca4b63SNaphtali Sprei         bs->backing_hd = bs_ro;
8394dca4b63SNaphtali Sprei         bs->backing_hd->keep_read_only = 0;
8404dca4b63SNaphtali Sprei     }
8414dca4b63SNaphtali Sprei 
8421d44952fSChristoph Hellwig     return ret;
84333e3963eSbellard }
84433e3963eSbellard 
8456ab4b5abSMarkus Armbruster void bdrv_commit_all(void)
8466ab4b5abSMarkus Armbruster {
8476ab4b5abSMarkus Armbruster     BlockDriverState *bs;
8486ab4b5abSMarkus Armbruster 
8496ab4b5abSMarkus Armbruster     QTAILQ_FOREACH(bs, &bdrv_states, list) {
8506ab4b5abSMarkus Armbruster         bdrv_commit(bs);
8516ab4b5abSMarkus Armbruster     }
8526ab4b5abSMarkus Armbruster }
8536ab4b5abSMarkus Armbruster 
854756e6736SKevin Wolf /*
855756e6736SKevin Wolf  * Return values:
856756e6736SKevin Wolf  * 0        - success
857756e6736SKevin Wolf  * -EINVAL  - backing format specified, but no file
858756e6736SKevin Wolf  * -ENOSPC  - can't update the backing file because no space is left in the
859756e6736SKevin Wolf  *            image file header
860756e6736SKevin Wolf  * -ENOTSUP - format driver doesn't support changing the backing file
861756e6736SKevin Wolf  */
862756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs,
863756e6736SKevin Wolf     const char *backing_file, const char *backing_fmt)
864756e6736SKevin Wolf {
865756e6736SKevin Wolf     BlockDriver *drv = bs->drv;
866756e6736SKevin Wolf 
867756e6736SKevin Wolf     if (drv->bdrv_change_backing_file != NULL) {
868756e6736SKevin Wolf         return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
869756e6736SKevin Wolf     } else {
870756e6736SKevin Wolf         return -ENOTSUP;
871756e6736SKevin Wolf     }
872756e6736SKevin Wolf }
873756e6736SKevin Wolf 
87471d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
87571d0770cSaliguori                                    size_t size)
87671d0770cSaliguori {
87771d0770cSaliguori     int64_t len;
87871d0770cSaliguori 
87971d0770cSaliguori     if (!bdrv_is_inserted(bs))
88071d0770cSaliguori         return -ENOMEDIUM;
88171d0770cSaliguori 
88271d0770cSaliguori     if (bs->growable)
88371d0770cSaliguori         return 0;
88471d0770cSaliguori 
88571d0770cSaliguori     len = bdrv_getlength(bs);
88671d0770cSaliguori 
887fbb7b4e0SKevin Wolf     if (offset < 0)
888fbb7b4e0SKevin Wolf         return -EIO;
889fbb7b4e0SKevin Wolf 
890fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
89171d0770cSaliguori         return -EIO;
89271d0770cSaliguori 
89371d0770cSaliguori     return 0;
89471d0770cSaliguori }
89571d0770cSaliguori 
89671d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
89771d0770cSaliguori                               int nb_sectors)
89871d0770cSaliguori {
899eb5a3165SJes Sorensen     return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
900eb5a3165SJes Sorensen                                    nb_sectors * BDRV_SECTOR_SIZE);
90171d0770cSaliguori }
90271d0770cSaliguori 
90319cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
904fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
905fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
906fc01f7e7Sbellard {
907ea2384d3Sbellard     BlockDriver *drv = bs->drv;
908fc01f7e7Sbellard 
90919cb3738Sbellard     if (!drv)
91019cb3738Sbellard         return -ENOMEDIUM;
91171d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
91271d0770cSaliguori         return -EIO;
913b338082bSbellard 
91483f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
91583f64091Sbellard }
916fc01f7e7Sbellard 
9177cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
9187cd1e32aSlirans@il.ibm.com                              int nb_sectors, int dirty)
9197cd1e32aSlirans@il.ibm.com {
9207cd1e32aSlirans@il.ibm.com     int64_t start, end;
921c6d22830SJan Kiszka     unsigned long val, idx, bit;
922a55eb92cSJan Kiszka 
9236ea44308SJan Kiszka     start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
924c6d22830SJan Kiszka     end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
9257cd1e32aSlirans@il.ibm.com 
9267cd1e32aSlirans@il.ibm.com     for (; start <= end; start++) {
927c6d22830SJan Kiszka         idx = start / (sizeof(unsigned long) * 8);
928c6d22830SJan Kiszka         bit = start % (sizeof(unsigned long) * 8);
929c6d22830SJan Kiszka         val = bs->dirty_bitmap[idx];
930c6d22830SJan Kiszka         if (dirty) {
931aaa0eb75SLiran Schour             if (!(val & (1 << bit))) {
932aaa0eb75SLiran Schour                 bs->dirty_count++;
933c6d22830SJan Kiszka                 val |= 1 << bit;
934aaa0eb75SLiran Schour             }
935c6d22830SJan Kiszka         } else {
936aaa0eb75SLiran Schour             if (val & (1 << bit)) {
937aaa0eb75SLiran Schour                 bs->dirty_count--;
938c6d22830SJan Kiszka                 val &= ~(1 << bit);
939c6d22830SJan Kiszka             }
940aaa0eb75SLiran Schour         }
941c6d22830SJan Kiszka         bs->dirty_bitmap[idx] = val;
9427cd1e32aSlirans@il.ibm.com     }
9437cd1e32aSlirans@il.ibm.com }
9447cd1e32aSlirans@il.ibm.com 
94519cb3738Sbellard /* Return < 0 if error. Important errors are:
94619cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
94719cb3738Sbellard   -ENOMEDIUM   No media inserted.
94819cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
94919cb3738Sbellard   -EACCES      Trying to write a read-only device
95019cb3738Sbellard */
951fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
952fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
953fc01f7e7Sbellard {
95483f64091Sbellard     BlockDriver *drv = bs->drv;
95519cb3738Sbellard     if (!bs->drv)
95619cb3738Sbellard         return -ENOMEDIUM;
9570849bf08Sbellard     if (bs->read_only)
95819cb3738Sbellard         return -EACCES;
95971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
96071d0770cSaliguori         return -EIO;
96171d0770cSaliguori 
962c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
9637cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
9647cd1e32aSlirans@il.ibm.com     }
9657cd1e32aSlirans@il.ibm.com 
966294cc35fSKevin Wolf     if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
967294cc35fSKevin Wolf         bs->wr_highest_sector = sector_num + nb_sectors - 1;
968294cc35fSKevin Wolf     }
969294cc35fSKevin Wolf 
97083f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
97183f64091Sbellard }
97283f64091Sbellard 
973eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
974eda578e5Saliguori                void *buf, int count1)
97583f64091Sbellard {
9766ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
97783f64091Sbellard     int len, nb_sectors, count;
97883f64091Sbellard     int64_t sector_num;
9799a8c4cceSKevin Wolf     int ret;
98083f64091Sbellard 
98183f64091Sbellard     count = count1;
98283f64091Sbellard     /* first read to align to sector start */
9836ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
98483f64091Sbellard     if (len > count)
98583f64091Sbellard         len = count;
9866ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
98783f64091Sbellard     if (len > 0) {
9889a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
9899a8c4cceSKevin Wolf             return ret;
9906ea44308SJan Kiszka         memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
99183f64091Sbellard         count -= len;
99283f64091Sbellard         if (count == 0)
99383f64091Sbellard             return count1;
99483f64091Sbellard         sector_num++;
99583f64091Sbellard         buf += len;
99683f64091Sbellard     }
99783f64091Sbellard 
99883f64091Sbellard     /* read the sectors "in place" */
9996ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
100083f64091Sbellard     if (nb_sectors > 0) {
10019a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
10029a8c4cceSKevin Wolf             return ret;
100383f64091Sbellard         sector_num += nb_sectors;
10046ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
100583f64091Sbellard         buf += len;
100683f64091Sbellard         count -= len;
100783f64091Sbellard     }
100883f64091Sbellard 
100983f64091Sbellard     /* add data from the last sector */
101083f64091Sbellard     if (count > 0) {
10119a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10129a8c4cceSKevin Wolf             return ret;
101383f64091Sbellard         memcpy(buf, tmp_buf, count);
101483f64091Sbellard     }
101583f64091Sbellard     return count1;
101683f64091Sbellard }
101783f64091Sbellard 
1018eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1019eda578e5Saliguori                 const void *buf, int count1)
102083f64091Sbellard {
10216ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
102283f64091Sbellard     int len, nb_sectors, count;
102383f64091Sbellard     int64_t sector_num;
10249a8c4cceSKevin Wolf     int ret;
102583f64091Sbellard 
102683f64091Sbellard     count = count1;
102783f64091Sbellard     /* first write to align to sector start */
10286ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
102983f64091Sbellard     if (len > count)
103083f64091Sbellard         len = count;
10316ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
103283f64091Sbellard     if (len > 0) {
10339a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10349a8c4cceSKevin Wolf             return ret;
10356ea44308SJan Kiszka         memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
10369a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10379a8c4cceSKevin Wolf             return ret;
103883f64091Sbellard         count -= len;
103983f64091Sbellard         if (count == 0)
104083f64091Sbellard             return count1;
104183f64091Sbellard         sector_num++;
104283f64091Sbellard         buf += len;
104383f64091Sbellard     }
104483f64091Sbellard 
104583f64091Sbellard     /* write the sectors "in place" */
10466ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
104783f64091Sbellard     if (nb_sectors > 0) {
10489a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
10499a8c4cceSKevin Wolf             return ret;
105083f64091Sbellard         sector_num += nb_sectors;
10516ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
105283f64091Sbellard         buf += len;
105383f64091Sbellard         count -= len;
105483f64091Sbellard     }
105583f64091Sbellard 
105683f64091Sbellard     /* add data from the last sector */
105783f64091Sbellard     if (count > 0) {
10589a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10599a8c4cceSKevin Wolf             return ret;
106083f64091Sbellard         memcpy(tmp_buf, buf, count);
10619a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10629a8c4cceSKevin Wolf             return ret;
106383f64091Sbellard     }
106483f64091Sbellard     return count1;
106583f64091Sbellard }
106683f64091Sbellard 
1067f08145feSKevin Wolf /*
1068f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1069f08145feSKevin Wolf  * request (acts as a barrier)
1070f08145feSKevin Wolf  *
1071f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1072f08145feSKevin Wolf  */
1073f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1074f08145feSKevin Wolf     const void *buf, int count)
1075f08145feSKevin Wolf {
1076f08145feSKevin Wolf     int ret;
1077f08145feSKevin Wolf 
1078f08145feSKevin Wolf     ret = bdrv_pwrite(bs, offset, buf, count);
1079f08145feSKevin Wolf     if (ret < 0) {
1080f08145feSKevin Wolf         return ret;
1081f08145feSKevin Wolf     }
1082f08145feSKevin Wolf 
1083f08145feSKevin Wolf     /* No flush needed for cache=writethrough, it uses O_DSYNC */
1084f08145feSKevin Wolf     if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1085f08145feSKevin Wolf         bdrv_flush(bs);
1086f08145feSKevin Wolf     }
1087f08145feSKevin Wolf 
1088f08145feSKevin Wolf     return 0;
1089f08145feSKevin Wolf }
1090f08145feSKevin Wolf 
1091f08145feSKevin Wolf /*
1092f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1093f08145feSKevin Wolf  * request (acts as a barrier)
1094f08145feSKevin Wolf  *
1095f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1096f08145feSKevin Wolf  */
1097f08145feSKevin Wolf int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1098f08145feSKevin Wolf     const uint8_t *buf, int nb_sectors)
1099f08145feSKevin Wolf {
1100f08145feSKevin Wolf     return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1101f08145feSKevin Wolf         buf, BDRV_SECTOR_SIZE * nb_sectors);
1102f08145feSKevin Wolf }
1103f08145feSKevin Wolf 
110483f64091Sbellard /**
110583f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
110683f64091Sbellard  */
110783f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
110883f64091Sbellard {
110983f64091Sbellard     BlockDriver *drv = bs->drv;
111051762288SStefan Hajnoczi     int ret;
111183f64091Sbellard     if (!drv)
111219cb3738Sbellard         return -ENOMEDIUM;
111383f64091Sbellard     if (!drv->bdrv_truncate)
111483f64091Sbellard         return -ENOTSUP;
111559f2689dSNaphtali Sprei     if (bs->read_only)
111659f2689dSNaphtali Sprei         return -EACCES;
111751762288SStefan Hajnoczi     ret = drv->bdrv_truncate(bs, offset);
111851762288SStefan Hajnoczi     if (ret == 0) {
111951762288SStefan Hajnoczi         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
112051762288SStefan Hajnoczi     }
112151762288SStefan Hajnoczi     return ret;
112283f64091Sbellard }
112383f64091Sbellard 
112483f64091Sbellard /**
112583f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
112683f64091Sbellard  */
112783f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
112883f64091Sbellard {
112983f64091Sbellard     BlockDriver *drv = bs->drv;
113083f64091Sbellard     if (!drv)
113119cb3738Sbellard         return -ENOMEDIUM;
113251762288SStefan Hajnoczi 
113351762288SStefan Hajnoczi     /* Fixed size devices use the total_sectors value for speed instead of
113451762288SStefan Hajnoczi        issuing a length query (like lseek) on each call.  Also, legacy block
113551762288SStefan Hajnoczi        drivers don't provide a bdrv_getlength function and must use
113651762288SStefan Hajnoczi        total_sectors. */
113751762288SStefan Hajnoczi     if (!bs->growable || !drv->bdrv_getlength) {
11386ea44308SJan Kiszka         return bs->total_sectors * BDRV_SECTOR_SIZE;
113983f64091Sbellard     }
114083f64091Sbellard     return drv->bdrv_getlength(bs);
1141fc01f7e7Sbellard }
1142fc01f7e7Sbellard 
114319cb3738Sbellard /* return 0 as number of sectors if no device present or error */
114496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1145fc01f7e7Sbellard {
114619cb3738Sbellard     int64_t length;
114719cb3738Sbellard     length = bdrv_getlength(bs);
114819cb3738Sbellard     if (length < 0)
114919cb3738Sbellard         length = 0;
115019cb3738Sbellard     else
11516ea44308SJan Kiszka         length = length >> BDRV_SECTOR_BITS;
115219cb3738Sbellard     *nb_sectors_ptr = length;
1153fc01f7e7Sbellard }
1154cf98951bSbellard 
1155f3d54fc4Saliguori struct partition {
1156f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
1157f3d54fc4Saliguori         uint8_t head;               /* starting head */
1158f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
1159f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
1160f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
1161f3d54fc4Saliguori         uint8_t end_head;           /* end head */
1162f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
1163f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
1164f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
1165f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
1166f3d54fc4Saliguori } __attribute__((packed));
1167f3d54fc4Saliguori 
1168f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1169f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
1170f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
1171f3d54fc4Saliguori {
1172eb5a3165SJes Sorensen     uint8_t buf[BDRV_SECTOR_SIZE];
1173f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
1174f3d54fc4Saliguori     struct partition *p;
1175f3d54fc4Saliguori     uint32_t nr_sects;
1176a38131b6Sblueswir1     uint64_t nb_sectors;
1177f3d54fc4Saliguori 
1178f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1179f3d54fc4Saliguori 
1180f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
1181f3d54fc4Saliguori     if (ret < 0)
1182f3d54fc4Saliguori         return -1;
1183f3d54fc4Saliguori     /* test msdos magic */
1184f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
1185f3d54fc4Saliguori         return -1;
1186f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
1187f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
1188f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
1189f3d54fc4Saliguori         if (nr_sects && p->end_head) {
1190f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
1191f3d54fc4Saliguori                a cylinder boundary */
1192f3d54fc4Saliguori             heads = p->end_head + 1;
1193f3d54fc4Saliguori             sectors = p->end_sector & 63;
1194f3d54fc4Saliguori             if (sectors == 0)
1195f3d54fc4Saliguori                 continue;
1196f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
1197f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
1198f3d54fc4Saliguori                 continue;
1199f3d54fc4Saliguori             *pheads = heads;
1200f3d54fc4Saliguori             *psectors = sectors;
1201f3d54fc4Saliguori             *pcylinders = cylinders;
1202f3d54fc4Saliguori #if 0
1203f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
1204f3d54fc4Saliguori                    cylinders, heads, sectors);
1205f3d54fc4Saliguori #endif
1206f3d54fc4Saliguori             return 0;
1207f3d54fc4Saliguori         }
1208f3d54fc4Saliguori     }
1209f3d54fc4Saliguori     return -1;
1210f3d54fc4Saliguori }
1211f3d54fc4Saliguori 
1212f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1213f3d54fc4Saliguori {
1214f3d54fc4Saliguori     int translation, lba_detected = 0;
1215f3d54fc4Saliguori     int cylinders, heads, secs;
1216a38131b6Sblueswir1     uint64_t nb_sectors;
1217f3d54fc4Saliguori 
1218f3d54fc4Saliguori     /* if a geometry hint is available, use it */
1219f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1220f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1221f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
1222f3d54fc4Saliguori     if (cylinders != 0) {
1223f3d54fc4Saliguori         *pcyls = cylinders;
1224f3d54fc4Saliguori         *pheads = heads;
1225f3d54fc4Saliguori         *psecs = secs;
1226f3d54fc4Saliguori     } else {
1227f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1228f3d54fc4Saliguori             if (heads > 16) {
1229f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
1230f3d54fc4Saliguori                    translation was active, so the default
1231f3d54fc4Saliguori                    hardware geometry is OK */
1232f3d54fc4Saliguori                 lba_detected = 1;
1233f3d54fc4Saliguori                 goto default_geometry;
1234f3d54fc4Saliguori             } else {
1235f3d54fc4Saliguori                 *pcyls = cylinders;
1236f3d54fc4Saliguori                 *pheads = heads;
1237f3d54fc4Saliguori                 *psecs = secs;
1238f3d54fc4Saliguori                 /* disable any translation to be in sync with
1239f3d54fc4Saliguori                    the logical geometry */
1240f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1241f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1242f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
1243f3d54fc4Saliguori                 }
1244f3d54fc4Saliguori             }
1245f3d54fc4Saliguori         } else {
1246f3d54fc4Saliguori         default_geometry:
1247f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
1248f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
1249f3d54fc4Saliguori 
1250f3d54fc4Saliguori             if (cylinders > 16383)
1251f3d54fc4Saliguori                 cylinders = 16383;
1252f3d54fc4Saliguori             else if (cylinders < 2)
1253f3d54fc4Saliguori                 cylinders = 2;
1254f3d54fc4Saliguori             *pcyls = cylinders;
1255f3d54fc4Saliguori             *pheads = 16;
1256f3d54fc4Saliguori             *psecs = 63;
1257f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1258f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
1259f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1260f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
1261f3d54fc4Saliguori                 } else {
1262f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1263f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
1264f3d54fc4Saliguori                 }
1265f3d54fc4Saliguori             }
1266f3d54fc4Saliguori         }
1267f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1268f3d54fc4Saliguori     }
1269f3d54fc4Saliguori }
1270f3d54fc4Saliguori 
1271b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
1272b338082bSbellard                             int cyls, int heads, int secs)
1273b338082bSbellard {
1274b338082bSbellard     bs->cyls = cyls;
1275b338082bSbellard     bs->heads = heads;
1276b338082bSbellard     bs->secs = secs;
1277b338082bSbellard }
1278b338082bSbellard 
1279b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
1280b338082bSbellard {
1281b338082bSbellard     bs->type = type;
1282b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
1283b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
1284b338082bSbellard }
1285b338082bSbellard 
128646d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
128746d4767dSbellard {
128846d4767dSbellard     bs->translation = translation;
128946d4767dSbellard }
129046d4767dSbellard 
1291b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
1292b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
1293b338082bSbellard {
1294b338082bSbellard     *pcyls = bs->cyls;
1295b338082bSbellard     *pheads = bs->heads;
1296b338082bSbellard     *psecs = bs->secs;
1297b338082bSbellard }
1298b338082bSbellard 
1299b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
1300b338082bSbellard {
1301b338082bSbellard     return bs->type;
1302b338082bSbellard }
1303b338082bSbellard 
130446d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
130546d4767dSbellard {
130646d4767dSbellard     return bs->translation;
130746d4767dSbellard }
130846d4767dSbellard 
1309abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1310abd7f68dSMarkus Armbruster                        BlockErrorAction on_write_error)
1311abd7f68dSMarkus Armbruster {
1312abd7f68dSMarkus Armbruster     bs->on_read_error = on_read_error;
1313abd7f68dSMarkus Armbruster     bs->on_write_error = on_write_error;
1314abd7f68dSMarkus Armbruster }
1315abd7f68dSMarkus Armbruster 
1316abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1317abd7f68dSMarkus Armbruster {
1318abd7f68dSMarkus Armbruster     return is_read ? bs->on_read_error : bs->on_write_error;
1319abd7f68dSMarkus Armbruster }
1320abd7f68dSMarkus Armbruster 
13217d0d6950SMarkus Armbruster void bdrv_set_removable(BlockDriverState *bs, int removable)
13227d0d6950SMarkus Armbruster {
13237d0d6950SMarkus Armbruster     bs->removable = removable;
13247d0d6950SMarkus Armbruster     if (removable && bs == bs_snapshots) {
13257d0d6950SMarkus Armbruster         bs_snapshots = NULL;
13267d0d6950SMarkus Armbruster     }
13277d0d6950SMarkus Armbruster }
13287d0d6950SMarkus Armbruster 
1329b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
1330b338082bSbellard {
1331b338082bSbellard     return bs->removable;
1332b338082bSbellard }
1333b338082bSbellard 
1334b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
1335b338082bSbellard {
1336b338082bSbellard     return bs->read_only;
1337b338082bSbellard }
1338b338082bSbellard 
1339985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
1340985a03b0Sths {
1341985a03b0Sths     return bs->sg;
1342985a03b0Sths }
1343985a03b0Sths 
1344e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs)
1345e900a7b7SChristoph Hellwig {
1346e900a7b7SChristoph Hellwig     return bs->enable_write_cache;
1347e900a7b7SChristoph Hellwig }
1348e900a7b7SChristoph Hellwig 
134919cb3738Sbellard /* XXX: no longer used */
1350b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
1351b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
1352b338082bSbellard {
1353b338082bSbellard     bs->change_cb = change_cb;
1354b338082bSbellard     bs->change_opaque = opaque;
1355b338082bSbellard }
1356b338082bSbellard 
1357ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
1358ea2384d3Sbellard {
1359ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
1360ea2384d3Sbellard         return 1;
1361ea2384d3Sbellard     return bs->encrypted;
1362ea2384d3Sbellard }
1363ea2384d3Sbellard 
1364c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
1365c0f4ce77Saliguori {
1366c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
1367c0f4ce77Saliguori 
1368c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1369c0f4ce77Saliguori         return 1;
1370c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
1371c0f4ce77Saliguori }
1372c0f4ce77Saliguori 
1373ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
1374ea2384d3Sbellard {
1375ea2384d3Sbellard     int ret;
1376ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
1377ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
1378ea2384d3Sbellard         if (ret < 0)
1379ea2384d3Sbellard             return ret;
1380ea2384d3Sbellard         if (!bs->encrypted)
1381ea2384d3Sbellard             return 0;
1382ea2384d3Sbellard     }
1383fd04a2aeSShahar Havivi     if (!bs->encrypted) {
1384fd04a2aeSShahar Havivi         return -EINVAL;
1385fd04a2aeSShahar Havivi     } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1386fd04a2aeSShahar Havivi         return -ENOMEDIUM;
1387fd04a2aeSShahar Havivi     }
1388c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
1389bb5fc20fSaliguori     if (ret < 0) {
1390bb5fc20fSaliguori         bs->valid_key = 0;
1391bb5fc20fSaliguori     } else if (!bs->valid_key) {
1392bb5fc20fSaliguori         bs->valid_key = 1;
1393bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
1394bb5fc20fSaliguori         bs->media_changed = 1;
1395bb5fc20fSaliguori         if (bs->change_cb)
1396bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
1397bb5fc20fSaliguori     }
1398c0f4ce77Saliguori     return ret;
1399ea2384d3Sbellard }
1400ea2384d3Sbellard 
1401ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1402ea2384d3Sbellard {
140319cb3738Sbellard     if (!bs->drv) {
1404ea2384d3Sbellard         buf[0] = '\0';
1405ea2384d3Sbellard     } else {
1406ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
1407ea2384d3Sbellard     }
1408ea2384d3Sbellard }
1409ea2384d3Sbellard 
1410ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1411ea2384d3Sbellard                          void *opaque)
1412ea2384d3Sbellard {
1413ea2384d3Sbellard     BlockDriver *drv;
1414ea2384d3Sbellard 
14158a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv, &bdrv_drivers, list) {
1416ea2384d3Sbellard         it(opaque, drv->format_name);
1417ea2384d3Sbellard     }
1418ea2384d3Sbellard }
1419ea2384d3Sbellard 
1420b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1421b338082bSbellard {
1422b338082bSbellard     BlockDriverState *bs;
1423b338082bSbellard 
14241b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
14251b7bdbc1SStefan Hajnoczi         if (!strcmp(name, bs->device_name)) {
1426b338082bSbellard             return bs;
1427b338082bSbellard         }
14281b7bdbc1SStefan Hajnoczi     }
1429b338082bSbellard     return NULL;
1430b338082bSbellard }
1431b338082bSbellard 
14322f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs)
14332f399b0aSMarkus Armbruster {
14342f399b0aSMarkus Armbruster     if (!bs) {
14352f399b0aSMarkus Armbruster         return QTAILQ_FIRST(&bdrv_states);
14362f399b0aSMarkus Armbruster     }
14372f399b0aSMarkus Armbruster     return QTAILQ_NEXT(bs, list);
14382f399b0aSMarkus Armbruster }
14392f399b0aSMarkus Armbruster 
144051de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
144181d0912dSbellard {
144281d0912dSbellard     BlockDriverState *bs;
144381d0912dSbellard 
14441b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
144551de9760Saliguori         it(opaque, bs);
144681d0912dSbellard     }
144781d0912dSbellard }
144881d0912dSbellard 
1449ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1450ea2384d3Sbellard {
1451ea2384d3Sbellard     return bs->device_name;
1452ea2384d3Sbellard }
1453ea2384d3Sbellard 
14547a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
14557a6cba61Spbrook {
1456016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
1457016f5cf6SAlexander Graf         return;
1458016f5cf6SAlexander Graf     }
1459016f5cf6SAlexander Graf 
14603f5075aeSChristoph Hellwig     if (bs->drv && bs->drv->bdrv_flush)
14617a6cba61Spbrook         bs->drv->bdrv_flush(bs);
14627a6cba61Spbrook }
14637a6cba61Spbrook 
1464c6ca28d6Saliguori void bdrv_flush_all(void)
1465c6ca28d6Saliguori {
1466c6ca28d6Saliguori     BlockDriverState *bs;
1467c6ca28d6Saliguori 
14681b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1469c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
14701b7bdbc1SStefan Hajnoczi             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1471c6ca28d6Saliguori             bdrv_flush(bs);
1472c6ca28d6Saliguori         }
14731b7bdbc1SStefan Hajnoczi     }
14741b7bdbc1SStefan Hajnoczi }
1475c6ca28d6Saliguori 
1476f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs)
1477f2feebbdSKevin Wolf {
1478f2feebbdSKevin Wolf     assert(bs->drv);
1479f2feebbdSKevin Wolf 
1480336c1c12SKevin Wolf     if (bs->drv->bdrv_has_zero_init) {
1481336c1c12SKevin Wolf         return bs->drv->bdrv_has_zero_init(bs);
1482f2feebbdSKevin Wolf     }
1483f2feebbdSKevin Wolf 
1484f2feebbdSKevin Wolf     return 1;
1485f2feebbdSKevin Wolf }
1486f2feebbdSKevin Wolf 
1487f58c7b35Sths /*
1488f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1489f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1490f58c7b35Sths  * hence all their sectors are reported as allocated.
1491f58c7b35Sths  *
1492f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1493f58c7b35Sths  * the specified sector) that are known to be in the same
1494f58c7b35Sths  * allocated/unallocated state.
1495f58c7b35Sths  *
1496f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1497f58c7b35Sths  */
1498f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1499f58c7b35Sths 	int *pnum)
1500f58c7b35Sths {
1501f58c7b35Sths     int64_t n;
1502f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1503f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1504f58c7b35Sths             *pnum = 0;
1505f58c7b35Sths             return 0;
1506f58c7b35Sths         }
1507f58c7b35Sths         n = bs->total_sectors - sector_num;
1508f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1509f58c7b35Sths         return 1;
1510f58c7b35Sths     }
1511f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1512f58c7b35Sths }
1513f58c7b35Sths 
15142582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv,
15152582bfedSLuiz Capitulino                     BlockMonEventAction action, int is_read)
15162582bfedSLuiz Capitulino {
15172582bfedSLuiz Capitulino     QObject *data;
15182582bfedSLuiz Capitulino     const char *action_str;
15192582bfedSLuiz Capitulino 
15202582bfedSLuiz Capitulino     switch (action) {
15212582bfedSLuiz Capitulino     case BDRV_ACTION_REPORT:
15222582bfedSLuiz Capitulino         action_str = "report";
15232582bfedSLuiz Capitulino         break;
15242582bfedSLuiz Capitulino     case BDRV_ACTION_IGNORE:
15252582bfedSLuiz Capitulino         action_str = "ignore";
15262582bfedSLuiz Capitulino         break;
15272582bfedSLuiz Capitulino     case BDRV_ACTION_STOP:
15282582bfedSLuiz Capitulino         action_str = "stop";
15292582bfedSLuiz Capitulino         break;
15302582bfedSLuiz Capitulino     default:
15312582bfedSLuiz Capitulino         abort();
15322582bfedSLuiz Capitulino     }
15332582bfedSLuiz Capitulino 
15342582bfedSLuiz Capitulino     data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
15352582bfedSLuiz Capitulino                               bdrv->device_name,
15362582bfedSLuiz Capitulino                               action_str,
15372582bfedSLuiz Capitulino                               is_read ? "read" : "write");
15382582bfedSLuiz Capitulino     monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
15392582bfedSLuiz Capitulino 
15402582bfedSLuiz Capitulino     qobject_decref(data);
15412582bfedSLuiz Capitulino }
15422582bfedSLuiz Capitulino 
1543d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque)
1544b338082bSbellard {
1545d15e5465SLuiz Capitulino     QDict *bs_dict;
1546d15e5465SLuiz Capitulino     Monitor *mon = opaque;
1547b338082bSbellard 
1548d15e5465SLuiz Capitulino     bs_dict = qobject_to_qdict(obj);
1549d15e5465SLuiz Capitulino 
1550d15e5465SLuiz Capitulino     monitor_printf(mon, "%s: type=%s removable=%d",
1551d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "device"),
1552d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "type"),
1553d15e5465SLuiz Capitulino                         qdict_get_bool(bs_dict, "removable"));
1554d15e5465SLuiz Capitulino 
1555d15e5465SLuiz Capitulino     if (qdict_get_bool(bs_dict, "removable")) {
1556d15e5465SLuiz Capitulino         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1557b338082bSbellard     }
1558d15e5465SLuiz Capitulino 
1559d15e5465SLuiz Capitulino     if (qdict_haskey(bs_dict, "inserted")) {
1560d15e5465SLuiz Capitulino         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1561d15e5465SLuiz Capitulino 
1562376253ecSaliguori         monitor_printf(mon, " file=");
1563d15e5465SLuiz Capitulino         monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1564d15e5465SLuiz Capitulino         if (qdict_haskey(qdict, "backing_file")) {
1565376253ecSaliguori             monitor_printf(mon, " backing_file=");
1566d15e5465SLuiz Capitulino             monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1567fef30743Sths         }
1568d15e5465SLuiz Capitulino         monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1569d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "ro"),
1570d15e5465SLuiz Capitulino                             qdict_get_str(qdict, "drv"),
1571d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "encrypted"));
1572b338082bSbellard     } else {
1573376253ecSaliguori         monitor_printf(mon, " [not inserted]");
1574b338082bSbellard     }
1575d15e5465SLuiz Capitulino 
1576376253ecSaliguori     monitor_printf(mon, "\n");
1577b338082bSbellard }
1578d15e5465SLuiz Capitulino 
1579d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data)
1580d15e5465SLuiz Capitulino {
1581d15e5465SLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1582d15e5465SLuiz Capitulino }
1583d15e5465SLuiz Capitulino 
1584d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data)
1585d15e5465SLuiz Capitulino {
1586d15e5465SLuiz Capitulino     QList *bs_list;
1587d15e5465SLuiz Capitulino     BlockDriverState *bs;
1588d15e5465SLuiz Capitulino 
1589d15e5465SLuiz Capitulino     bs_list = qlist_new();
1590d15e5465SLuiz Capitulino 
15911b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1592d15e5465SLuiz Capitulino         QObject *bs_obj;
1593d15e5465SLuiz Capitulino         const char *type = "unknown";
1594d15e5465SLuiz Capitulino 
1595d15e5465SLuiz Capitulino         switch(bs->type) {
1596d15e5465SLuiz Capitulino         case BDRV_TYPE_HD:
1597d15e5465SLuiz Capitulino             type = "hd";
1598d15e5465SLuiz Capitulino             break;
1599d15e5465SLuiz Capitulino         case BDRV_TYPE_CDROM:
1600d15e5465SLuiz Capitulino             type = "cdrom";
1601d15e5465SLuiz Capitulino             break;
1602d15e5465SLuiz Capitulino         case BDRV_TYPE_FLOPPY:
1603d15e5465SLuiz Capitulino             type = "floppy";
1604d15e5465SLuiz Capitulino             break;
1605d15e5465SLuiz Capitulino         }
1606d15e5465SLuiz Capitulino 
1607d15e5465SLuiz Capitulino         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1608d15e5465SLuiz Capitulino                                     "'removable': %i, 'locked': %i }",
1609d15e5465SLuiz Capitulino                                     bs->device_name, type, bs->removable,
1610d15e5465SLuiz Capitulino                                     bs->locked);
1611d15e5465SLuiz Capitulino 
1612d15e5465SLuiz Capitulino         if (bs->drv) {
1613d15e5465SLuiz Capitulino             QObject *obj;
1614d15e5465SLuiz Capitulino             QDict *bs_dict = qobject_to_qdict(bs_obj);
1615d15e5465SLuiz Capitulino 
1616d15e5465SLuiz Capitulino             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1617d15e5465SLuiz Capitulino                                      "'encrypted': %i }",
1618d15e5465SLuiz Capitulino                                      bs->filename, bs->read_only,
1619d15e5465SLuiz Capitulino                                      bs->drv->format_name,
1620d15e5465SLuiz Capitulino                                      bdrv_is_encrypted(bs));
1621d15e5465SLuiz Capitulino             if (bs->backing_file[0] != '\0') {
1622d15e5465SLuiz Capitulino                 QDict *qdict = qobject_to_qdict(obj);
1623d15e5465SLuiz Capitulino                 qdict_put(qdict, "backing_file",
1624d15e5465SLuiz Capitulino                           qstring_from_str(bs->backing_file));
1625d15e5465SLuiz Capitulino             }
1626d15e5465SLuiz Capitulino 
1627d15e5465SLuiz Capitulino             qdict_put_obj(bs_dict, "inserted", obj);
1628d15e5465SLuiz Capitulino         }
1629d15e5465SLuiz Capitulino         qlist_append_obj(bs_list, bs_obj);
1630d15e5465SLuiz Capitulino     }
1631d15e5465SLuiz Capitulino 
1632d15e5465SLuiz Capitulino     *ret_data = QOBJECT(bs_list);
1633b338082bSbellard }
1634a36e69ddSths 
1635218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque)
1636a36e69ddSths {
1637218a536aSLuiz Capitulino     QDict *qdict;
1638218a536aSLuiz Capitulino     Monitor *mon = opaque;
1639218a536aSLuiz Capitulino 
1640218a536aSLuiz Capitulino     qdict = qobject_to_qdict(data);
1641218a536aSLuiz Capitulino     monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1642218a536aSLuiz Capitulino 
1643218a536aSLuiz Capitulino     qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1644218a536aSLuiz Capitulino     monitor_printf(mon, " rd_bytes=%" PRId64
1645218a536aSLuiz Capitulino                         " wr_bytes=%" PRId64
1646218a536aSLuiz Capitulino                         " rd_operations=%" PRId64
1647218a536aSLuiz Capitulino                         " wr_operations=%" PRId64
1648218a536aSLuiz Capitulino                         "\n",
1649218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_bytes"),
1650218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_bytes"),
1651218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_operations"),
1652218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_operations"));
1653218a536aSLuiz Capitulino }
1654218a536aSLuiz Capitulino 
1655218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data)
1656218a536aSLuiz Capitulino {
1657218a536aSLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1658218a536aSLuiz Capitulino }
1659218a536aSLuiz Capitulino 
1660294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1661294cc35fSKevin Wolf {
1662294cc35fSKevin Wolf     QObject *res;
1663294cc35fSKevin Wolf     QDict *dict;
1664294cc35fSKevin Wolf 
1665294cc35fSKevin Wolf     res = qobject_from_jsonf("{ 'stats': {"
1666294cc35fSKevin Wolf                              "'rd_bytes': %" PRId64 ","
1667294cc35fSKevin Wolf                              "'wr_bytes': %" PRId64 ","
1668294cc35fSKevin Wolf                              "'rd_operations': %" PRId64 ","
1669294cc35fSKevin Wolf                              "'wr_operations': %" PRId64 ","
1670294cc35fSKevin Wolf                              "'wr_highest_offset': %" PRId64
1671294cc35fSKevin Wolf                              "} }",
1672294cc35fSKevin Wolf                              bs->rd_bytes, bs->wr_bytes,
1673294cc35fSKevin Wolf                              bs->rd_ops, bs->wr_ops,
16745ffbbc67SBlue Swirl                              bs->wr_highest_sector *
16755ffbbc67SBlue Swirl                              (uint64_t)BDRV_SECTOR_SIZE);
1676294cc35fSKevin Wolf     dict  = qobject_to_qdict(res);
1677294cc35fSKevin Wolf 
1678294cc35fSKevin Wolf     if (*bs->device_name) {
1679294cc35fSKevin Wolf         qdict_put(dict, "device", qstring_from_str(bs->device_name));
1680294cc35fSKevin Wolf     }
1681294cc35fSKevin Wolf 
1682294cc35fSKevin Wolf     if (bs->file) {
1683294cc35fSKevin Wolf         QObject *parent = bdrv_info_stats_bs(bs->file);
1684294cc35fSKevin Wolf         qdict_put_obj(dict, "parent", parent);
1685294cc35fSKevin Wolf     }
1686294cc35fSKevin Wolf 
1687294cc35fSKevin Wolf     return res;
1688294cc35fSKevin Wolf }
1689294cc35fSKevin Wolf 
1690218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1691218a536aSLuiz Capitulino {
1692218a536aSLuiz Capitulino     QObject *obj;
1693218a536aSLuiz Capitulino     QList *devices;
1694a36e69ddSths     BlockDriverState *bs;
1695a36e69ddSths 
1696218a536aSLuiz Capitulino     devices = qlist_new();
1697218a536aSLuiz Capitulino 
16981b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1699294cc35fSKevin Wolf         obj = bdrv_info_stats_bs(bs);
1700218a536aSLuiz Capitulino         qlist_append_obj(devices, obj);
1701a36e69ddSths     }
1702218a536aSLuiz Capitulino 
1703218a536aSLuiz Capitulino     *ret_data = QOBJECT(devices);
1704a36e69ddSths }
1705ea2384d3Sbellard 
1706045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1707045df330Saliguori {
1708045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1709045df330Saliguori         return bs->backing_file;
1710045df330Saliguori     else if (bs->encrypted)
1711045df330Saliguori         return bs->filename;
1712045df330Saliguori     else
1713045df330Saliguori         return NULL;
1714045df330Saliguori }
1715045df330Saliguori 
171683f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
171783f64091Sbellard                                char *filename, int filename_size)
171883f64091Sbellard {
1719b783e409SKevin Wolf     if (!bs->backing_file) {
172083f64091Sbellard         pstrcpy(filename, filename_size, "");
172183f64091Sbellard     } else {
172283f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
172383f64091Sbellard     }
172483f64091Sbellard }
172583f64091Sbellard 
1726faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1727faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1728faea38e7Sbellard {
1729faea38e7Sbellard     BlockDriver *drv = bs->drv;
1730faea38e7Sbellard     if (!drv)
173119cb3738Sbellard         return -ENOMEDIUM;
1732faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1733faea38e7Sbellard         return -ENOTSUP;
1734fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1735fbb7b4e0SKevin Wolf         return -EIO;
17367cd1e32aSlirans@il.ibm.com 
1737c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
17387cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
17397cd1e32aSlirans@il.ibm.com     }
17407cd1e32aSlirans@il.ibm.com 
1741faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1742faea38e7Sbellard }
1743faea38e7Sbellard 
1744faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1745faea38e7Sbellard {
1746faea38e7Sbellard     BlockDriver *drv = bs->drv;
1747faea38e7Sbellard     if (!drv)
174819cb3738Sbellard         return -ENOMEDIUM;
1749faea38e7Sbellard     if (!drv->bdrv_get_info)
1750faea38e7Sbellard         return -ENOTSUP;
1751faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1752faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1753faea38e7Sbellard }
1754faea38e7Sbellard 
175545566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
175645566e9cSChristoph Hellwig                       int64_t pos, int size)
1757178e08a5Saliguori {
1758178e08a5Saliguori     BlockDriver *drv = bs->drv;
1759178e08a5Saliguori     if (!drv)
1760178e08a5Saliguori         return -ENOMEDIUM;
17617cdb1f6dSMORITA Kazutaka     if (drv->bdrv_save_vmstate)
176245566e9cSChristoph Hellwig         return drv->bdrv_save_vmstate(bs, buf, pos, size);
17637cdb1f6dSMORITA Kazutaka     if (bs->file)
17647cdb1f6dSMORITA Kazutaka         return bdrv_save_vmstate(bs->file, buf, pos, size);
17657cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1766178e08a5Saliguori }
1767178e08a5Saliguori 
176845566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
176945566e9cSChristoph Hellwig                       int64_t pos, int size)
1770178e08a5Saliguori {
1771178e08a5Saliguori     BlockDriver *drv = bs->drv;
1772178e08a5Saliguori     if (!drv)
1773178e08a5Saliguori         return -ENOMEDIUM;
17747cdb1f6dSMORITA Kazutaka     if (drv->bdrv_load_vmstate)
177545566e9cSChristoph Hellwig         return drv->bdrv_load_vmstate(bs, buf, pos, size);
17767cdb1f6dSMORITA Kazutaka     if (bs->file)
17777cdb1f6dSMORITA Kazutaka         return bdrv_load_vmstate(bs->file, buf, pos, size);
17787cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1779178e08a5Saliguori }
1780178e08a5Saliguori 
17818b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
17828b9b0cc2SKevin Wolf {
17838b9b0cc2SKevin Wolf     BlockDriver *drv = bs->drv;
17848b9b0cc2SKevin Wolf 
17858b9b0cc2SKevin Wolf     if (!drv || !drv->bdrv_debug_event) {
17868b9b0cc2SKevin Wolf         return;
17878b9b0cc2SKevin Wolf     }
17888b9b0cc2SKevin Wolf 
17898b9b0cc2SKevin Wolf     return drv->bdrv_debug_event(bs, event);
17908b9b0cc2SKevin Wolf 
17918b9b0cc2SKevin Wolf }
17928b9b0cc2SKevin Wolf 
1793faea38e7Sbellard /**************************************************************/
1794faea38e7Sbellard /* handling of snapshots */
1795faea38e7Sbellard 
1796feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs)
1797feeee5acSMiguel Di Ciurcio Filho {
1798feeee5acSMiguel Di Ciurcio Filho     BlockDriver *drv = bs->drv;
1799feeee5acSMiguel Di Ciurcio Filho     if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1800feeee5acSMiguel Di Ciurcio Filho         return 0;
1801feeee5acSMiguel Di Ciurcio Filho     }
1802feeee5acSMiguel Di Ciurcio Filho 
1803feeee5acSMiguel Di Ciurcio Filho     if (!drv->bdrv_snapshot_create) {
1804feeee5acSMiguel Di Ciurcio Filho         if (bs->file != NULL) {
1805feeee5acSMiguel Di Ciurcio Filho             return bdrv_can_snapshot(bs->file);
1806feeee5acSMiguel Di Ciurcio Filho         }
1807feeee5acSMiguel Di Ciurcio Filho         return 0;
1808feeee5acSMiguel Di Ciurcio Filho     }
1809feeee5acSMiguel Di Ciurcio Filho 
1810feeee5acSMiguel Di Ciurcio Filho     return 1;
1811feeee5acSMiguel Di Ciurcio Filho }
1812feeee5acSMiguel Di Ciurcio Filho 
1813199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs)
1814199630b6SBlue Swirl {
1815199630b6SBlue Swirl     return !!(bs->open_flags & BDRV_O_SNAPSHOT);
1816199630b6SBlue Swirl }
1817199630b6SBlue Swirl 
1818f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void)
1819f9092b10SMarkus Armbruster {
1820f9092b10SMarkus Armbruster     BlockDriverState *bs;
1821f9092b10SMarkus Armbruster 
18223ac906f7SMarkus Armbruster     if (bs_snapshots) {
1823f9092b10SMarkus Armbruster         return bs_snapshots;
18243ac906f7SMarkus Armbruster     }
1825f9092b10SMarkus Armbruster 
1826f9092b10SMarkus Armbruster     bs = NULL;
1827f9092b10SMarkus Armbruster     while ((bs = bdrv_next(bs))) {
1828f9092b10SMarkus Armbruster         if (bdrv_can_snapshot(bs)) {
18293ac906f7SMarkus Armbruster             bs_snapshots = bs;
18303ac906f7SMarkus Armbruster             return bs;
1831f9092b10SMarkus Armbruster         }
1832f9092b10SMarkus Armbruster     }
1833f9092b10SMarkus Armbruster     return NULL;
1834f9092b10SMarkus Armbruster }
1835f9092b10SMarkus Armbruster 
1836faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1837faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1838faea38e7Sbellard {
1839faea38e7Sbellard     BlockDriver *drv = bs->drv;
1840faea38e7Sbellard     if (!drv)
184119cb3738Sbellard         return -ENOMEDIUM;
18427cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_create)
1843faea38e7Sbellard         return drv->bdrv_snapshot_create(bs, sn_info);
18447cdb1f6dSMORITA Kazutaka     if (bs->file)
18457cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_create(bs->file, sn_info);
18467cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1847faea38e7Sbellard }
1848faea38e7Sbellard 
1849faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1850faea38e7Sbellard                        const char *snapshot_id)
1851faea38e7Sbellard {
1852faea38e7Sbellard     BlockDriver *drv = bs->drv;
18537cdb1f6dSMORITA Kazutaka     int ret, open_ret;
18547cdb1f6dSMORITA Kazutaka 
1855faea38e7Sbellard     if (!drv)
185619cb3738Sbellard         return -ENOMEDIUM;
18577cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_goto)
1858faea38e7Sbellard         return drv->bdrv_snapshot_goto(bs, snapshot_id);
18597cdb1f6dSMORITA Kazutaka 
18607cdb1f6dSMORITA Kazutaka     if (bs->file) {
18617cdb1f6dSMORITA Kazutaka         drv->bdrv_close(bs);
18627cdb1f6dSMORITA Kazutaka         ret = bdrv_snapshot_goto(bs->file, snapshot_id);
18637cdb1f6dSMORITA Kazutaka         open_ret = drv->bdrv_open(bs, bs->open_flags);
18647cdb1f6dSMORITA Kazutaka         if (open_ret < 0) {
18657cdb1f6dSMORITA Kazutaka             bdrv_delete(bs->file);
18667cdb1f6dSMORITA Kazutaka             bs->drv = NULL;
18677cdb1f6dSMORITA Kazutaka             return open_ret;
18687cdb1f6dSMORITA Kazutaka         }
18697cdb1f6dSMORITA Kazutaka         return ret;
18707cdb1f6dSMORITA Kazutaka     }
18717cdb1f6dSMORITA Kazutaka 
18727cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1873faea38e7Sbellard }
1874faea38e7Sbellard 
1875faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1876faea38e7Sbellard {
1877faea38e7Sbellard     BlockDriver *drv = bs->drv;
1878faea38e7Sbellard     if (!drv)
187919cb3738Sbellard         return -ENOMEDIUM;
18807cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_delete)
1881faea38e7Sbellard         return drv->bdrv_snapshot_delete(bs, snapshot_id);
18827cdb1f6dSMORITA Kazutaka     if (bs->file)
18837cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_delete(bs->file, snapshot_id);
18847cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1885faea38e7Sbellard }
1886faea38e7Sbellard 
1887faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1888faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1889faea38e7Sbellard {
1890faea38e7Sbellard     BlockDriver *drv = bs->drv;
1891faea38e7Sbellard     if (!drv)
189219cb3738Sbellard         return -ENOMEDIUM;
18937cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_list)
1894faea38e7Sbellard         return drv->bdrv_snapshot_list(bs, psn_info);
18957cdb1f6dSMORITA Kazutaka     if (bs->file)
18967cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_list(bs->file, psn_info);
18977cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1898faea38e7Sbellard }
1899faea38e7Sbellard 
1900faea38e7Sbellard #define NB_SUFFIXES 4
1901faea38e7Sbellard 
1902faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1903faea38e7Sbellard {
1904faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1905faea38e7Sbellard     int64_t base;
1906faea38e7Sbellard     int i;
1907faea38e7Sbellard 
1908faea38e7Sbellard     if (size <= 999) {
1909faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1910faea38e7Sbellard     } else {
1911faea38e7Sbellard         base = 1024;
1912faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1913faea38e7Sbellard             if (size < (10 * base)) {
1914faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1915faea38e7Sbellard                          (double)size / base,
1916faea38e7Sbellard                          suffixes[i]);
1917faea38e7Sbellard                 break;
1918faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1919faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1920faea38e7Sbellard                          ((size + (base >> 1)) / base),
1921faea38e7Sbellard                          suffixes[i]);
1922faea38e7Sbellard                 break;
1923faea38e7Sbellard             }
1924faea38e7Sbellard             base = base * 1024;
1925faea38e7Sbellard         }
1926faea38e7Sbellard     }
1927faea38e7Sbellard     return buf;
1928faea38e7Sbellard }
1929faea38e7Sbellard 
1930faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1931faea38e7Sbellard {
1932faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
19333b9f94e1Sbellard #ifdef _WIN32
19343b9f94e1Sbellard     struct tm *ptm;
19353b9f94e1Sbellard #else
1936faea38e7Sbellard     struct tm tm;
19373b9f94e1Sbellard #endif
1938faea38e7Sbellard     time_t ti;
1939faea38e7Sbellard     int64_t secs;
1940faea38e7Sbellard 
1941faea38e7Sbellard     if (!sn) {
1942faea38e7Sbellard         snprintf(buf, buf_size,
1943faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1944faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1945faea38e7Sbellard     } else {
1946faea38e7Sbellard         ti = sn->date_sec;
19473b9f94e1Sbellard #ifdef _WIN32
19483b9f94e1Sbellard         ptm = localtime(&ti);
19493b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
19503b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
19513b9f94e1Sbellard #else
1952faea38e7Sbellard         localtime_r(&ti, &tm);
1953faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1954faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
19553b9f94e1Sbellard #endif
1956faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1957faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1958faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1959faea38e7Sbellard                  (int)(secs / 3600),
1960faea38e7Sbellard                  (int)((secs / 60) % 60),
1961faea38e7Sbellard                  (int)(secs % 60),
1962faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1963faea38e7Sbellard         snprintf(buf, buf_size,
1964faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1965faea38e7Sbellard                  sn->id_str, sn->name,
1966faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1967faea38e7Sbellard                  date_buf,
1968faea38e7Sbellard                  clock_buf);
1969faea38e7Sbellard     }
1970faea38e7Sbellard     return buf;
1971faea38e7Sbellard }
1972faea38e7Sbellard 
197383f64091Sbellard 
1974ea2384d3Sbellard /**************************************************************/
197583f64091Sbellard /* async I/Os */
1976ea2384d3Sbellard 
19773b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1978f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
197983f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
1980ea2384d3Sbellard {
198183f64091Sbellard     BlockDriver *drv = bs->drv;
1982a36e69ddSths     BlockDriverAIOCB *ret;
1983ea2384d3Sbellard 
198419cb3738Sbellard     if (!drv)
1985ce1a14dcSpbrook         return NULL;
198671d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
198771d0770cSaliguori         return NULL;
198883f64091Sbellard 
1989f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1990f141eafeSaliguori                               cb, opaque);
1991a36e69ddSths 
1992a36e69ddSths     if (ret) {
1993a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
19946ea44308SJan Kiszka 	bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1995a36e69ddSths 	bs->rd_ops ++;
1996a36e69ddSths     }
1997a36e69ddSths 
1998a36e69ddSths     return ret;
199983f64091Sbellard }
200083f64091Sbellard 
2001f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2002f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
200383f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
20047674e7bfSbellard {
200583f64091Sbellard     BlockDriver *drv = bs->drv;
2006a36e69ddSths     BlockDriverAIOCB *ret;
200783f64091Sbellard 
200819cb3738Sbellard     if (!drv)
2009ce1a14dcSpbrook         return NULL;
201083f64091Sbellard     if (bs->read_only)
2011ce1a14dcSpbrook         return NULL;
201271d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
201371d0770cSaliguori         return NULL;
201483f64091Sbellard 
2015c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
20167cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
20177cd1e32aSlirans@il.ibm.com     }
20187cd1e32aSlirans@il.ibm.com 
2019f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2020f141eafeSaliguori                                cb, opaque);
2021a36e69ddSths 
2022a36e69ddSths     if (ret) {
2023a36e69ddSths         /* Update stats even though technically transfer has not happened. */
20246ea44308SJan Kiszka         bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2025a36e69ddSths         bs->wr_ops ++;
2026294cc35fSKevin Wolf         if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2027294cc35fSKevin Wolf             bs->wr_highest_sector = sector_num + nb_sectors - 1;
2028294cc35fSKevin Wolf         }
2029a36e69ddSths     }
2030a36e69ddSths 
2031a36e69ddSths     return ret;
203283f64091Sbellard }
203383f64091Sbellard 
203440b4f539SKevin Wolf 
203540b4f539SKevin Wolf typedef struct MultiwriteCB {
203640b4f539SKevin Wolf     int error;
203740b4f539SKevin Wolf     int num_requests;
203840b4f539SKevin Wolf     int num_callbacks;
203940b4f539SKevin Wolf     struct {
204040b4f539SKevin Wolf         BlockDriverCompletionFunc *cb;
204140b4f539SKevin Wolf         void *opaque;
204240b4f539SKevin Wolf         QEMUIOVector *free_qiov;
204340b4f539SKevin Wolf         void *free_buf;
204440b4f539SKevin Wolf     } callbacks[];
204540b4f539SKevin Wolf } MultiwriteCB;
204640b4f539SKevin Wolf 
204740b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb)
204840b4f539SKevin Wolf {
204940b4f539SKevin Wolf     int i;
205040b4f539SKevin Wolf 
205140b4f539SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
205240b4f539SKevin Wolf         mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
20531e1ea48dSStefan Hajnoczi         if (mcb->callbacks[i].free_qiov) {
20541e1ea48dSStefan Hajnoczi             qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
20551e1ea48dSStefan Hajnoczi         }
205640b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_qiov);
2057f8a83245SHerve Poussineau         qemu_vfree(mcb->callbacks[i].free_buf);
205840b4f539SKevin Wolf     }
205940b4f539SKevin Wolf }
206040b4f539SKevin Wolf 
206140b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret)
206240b4f539SKevin Wolf {
206340b4f539SKevin Wolf     MultiwriteCB *mcb = opaque;
206440b4f539SKevin Wolf 
2065cb6d3ca0SKevin Wolf     if (ret < 0 && !mcb->error) {
206640b4f539SKevin Wolf         mcb->error = ret;
206740b4f539SKevin Wolf     }
206840b4f539SKevin Wolf 
206940b4f539SKevin Wolf     mcb->num_requests--;
207040b4f539SKevin Wolf     if (mcb->num_requests == 0) {
207140b4f539SKevin Wolf         multiwrite_user_cb(mcb);
207240b4f539SKevin Wolf         qemu_free(mcb);
207340b4f539SKevin Wolf     }
207440b4f539SKevin Wolf }
207540b4f539SKevin Wolf 
207640b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b)
207740b4f539SKevin Wolf {
207877be4366SChristoph Hellwig     const BlockRequest *req1 = a, *req2 = b;
207977be4366SChristoph Hellwig 
208077be4366SChristoph Hellwig     /*
208177be4366SChristoph Hellwig      * Note that we can't simply subtract req2->sector from req1->sector
208277be4366SChristoph Hellwig      * here as that could overflow the return value.
208377be4366SChristoph Hellwig      */
208477be4366SChristoph Hellwig     if (req1->sector > req2->sector) {
208577be4366SChristoph Hellwig         return 1;
208677be4366SChristoph Hellwig     } else if (req1->sector < req2->sector) {
208777be4366SChristoph Hellwig         return -1;
208877be4366SChristoph Hellwig     } else {
208977be4366SChristoph Hellwig         return 0;
209077be4366SChristoph Hellwig     }
209140b4f539SKevin Wolf }
209240b4f539SKevin Wolf 
209340b4f539SKevin Wolf /*
209440b4f539SKevin Wolf  * Takes a bunch of requests and tries to merge them. Returns the number of
209540b4f539SKevin Wolf  * requests that remain after merging.
209640b4f539SKevin Wolf  */
209740b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
209840b4f539SKevin Wolf     int num_reqs, MultiwriteCB *mcb)
209940b4f539SKevin Wolf {
210040b4f539SKevin Wolf     int i, outidx;
210140b4f539SKevin Wolf 
210240b4f539SKevin Wolf     // Sort requests by start sector
210340b4f539SKevin Wolf     qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
210440b4f539SKevin Wolf 
210540b4f539SKevin Wolf     // Check if adjacent requests touch the same clusters. If so, combine them,
210640b4f539SKevin Wolf     // filling up gaps with zero sectors.
210740b4f539SKevin Wolf     outidx = 0;
210840b4f539SKevin Wolf     for (i = 1; i < num_reqs; i++) {
210940b4f539SKevin Wolf         int merge = 0;
211040b4f539SKevin Wolf         int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
211140b4f539SKevin Wolf 
211240b4f539SKevin Wolf         // This handles the cases that are valid for all block drivers, namely
211340b4f539SKevin Wolf         // exactly sequential writes and overlapping writes.
211440b4f539SKevin Wolf         if (reqs[i].sector <= oldreq_last) {
211540b4f539SKevin Wolf             merge = 1;
211640b4f539SKevin Wolf         }
211740b4f539SKevin Wolf 
211840b4f539SKevin Wolf         // The block driver may decide that it makes sense to combine requests
211940b4f539SKevin Wolf         // even if there is a gap of some sectors between them. In this case,
212040b4f539SKevin Wolf         // the gap is filled with zeros (therefore only applicable for yet
212140b4f539SKevin Wolf         // unused space in format like qcow2).
212240b4f539SKevin Wolf         if (!merge && bs->drv->bdrv_merge_requests) {
212340b4f539SKevin Wolf             merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
212440b4f539SKevin Wolf         }
212540b4f539SKevin Wolf 
2126e2a305fbSChristoph Hellwig         if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2127e2a305fbSChristoph Hellwig             merge = 0;
2128e2a305fbSChristoph Hellwig         }
2129e2a305fbSChristoph Hellwig 
213040b4f539SKevin Wolf         if (merge) {
213140b4f539SKevin Wolf             size_t size;
213240b4f539SKevin Wolf             QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
213340b4f539SKevin Wolf             qemu_iovec_init(qiov,
213440b4f539SKevin Wolf                 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
213540b4f539SKevin Wolf 
213640b4f539SKevin Wolf             // Add the first request to the merged one. If the requests are
213740b4f539SKevin Wolf             // overlapping, drop the last sectors of the first request.
213840b4f539SKevin Wolf             size = (reqs[i].sector - reqs[outidx].sector) << 9;
213940b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
214040b4f539SKevin Wolf 
214140b4f539SKevin Wolf             // We might need to add some zeros between the two requests
214240b4f539SKevin Wolf             if (reqs[i].sector > oldreq_last) {
214340b4f539SKevin Wolf                 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
214440b4f539SKevin Wolf                 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
214540b4f539SKevin Wolf                 memset(buf, 0, zero_bytes);
214640b4f539SKevin Wolf                 qemu_iovec_add(qiov, buf, zero_bytes);
214740b4f539SKevin Wolf                 mcb->callbacks[i].free_buf = buf;
214840b4f539SKevin Wolf             }
214940b4f539SKevin Wolf 
215040b4f539SKevin Wolf             // Add the second request
215140b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
215240b4f539SKevin Wolf 
2153cbf1dff2SKevin Wolf             reqs[outidx].nb_sectors = qiov->size >> 9;
215440b4f539SKevin Wolf             reqs[outidx].qiov = qiov;
215540b4f539SKevin Wolf 
215640b4f539SKevin Wolf             mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
215740b4f539SKevin Wolf         } else {
215840b4f539SKevin Wolf             outidx++;
215940b4f539SKevin Wolf             reqs[outidx].sector     = reqs[i].sector;
216040b4f539SKevin Wolf             reqs[outidx].nb_sectors = reqs[i].nb_sectors;
216140b4f539SKevin Wolf             reqs[outidx].qiov       = reqs[i].qiov;
216240b4f539SKevin Wolf         }
216340b4f539SKevin Wolf     }
216440b4f539SKevin Wolf 
216540b4f539SKevin Wolf     return outidx + 1;
216640b4f539SKevin Wolf }
216740b4f539SKevin Wolf 
216840b4f539SKevin Wolf /*
216940b4f539SKevin Wolf  * Submit multiple AIO write requests at once.
217040b4f539SKevin Wolf  *
217140b4f539SKevin Wolf  * On success, the function returns 0 and all requests in the reqs array have
217240b4f539SKevin Wolf  * been submitted. In error case this function returns -1, and any of the
217340b4f539SKevin Wolf  * requests may or may not be submitted yet. In particular, this means that the
217440b4f539SKevin Wolf  * callback will be called for some of the requests, for others it won't. The
217540b4f539SKevin Wolf  * caller must check the error field of the BlockRequest to wait for the right
217640b4f539SKevin Wolf  * callbacks (if error != 0, no callback will be called).
217740b4f539SKevin Wolf  *
217840b4f539SKevin Wolf  * The implementation may modify the contents of the reqs array, e.g. to merge
217940b4f539SKevin Wolf  * requests. However, the fields opaque and error are left unmodified as they
218040b4f539SKevin Wolf  * are used to signal failure for a single request to the caller.
218140b4f539SKevin Wolf  */
218240b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
218340b4f539SKevin Wolf {
218440b4f539SKevin Wolf     BlockDriverAIOCB *acb;
218540b4f539SKevin Wolf     MultiwriteCB *mcb;
218640b4f539SKevin Wolf     int i;
218740b4f539SKevin Wolf 
218840b4f539SKevin Wolf     if (num_reqs == 0) {
218940b4f539SKevin Wolf         return 0;
219040b4f539SKevin Wolf     }
219140b4f539SKevin Wolf 
219240b4f539SKevin Wolf     // Create MultiwriteCB structure
219340b4f539SKevin Wolf     mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
219440b4f539SKevin Wolf     mcb->num_requests = 0;
219540b4f539SKevin Wolf     mcb->num_callbacks = num_reqs;
219640b4f539SKevin Wolf 
219740b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
219840b4f539SKevin Wolf         mcb->callbacks[i].cb = reqs[i].cb;
219940b4f539SKevin Wolf         mcb->callbacks[i].opaque = reqs[i].opaque;
220040b4f539SKevin Wolf     }
220140b4f539SKevin Wolf 
220240b4f539SKevin Wolf     // Check for mergable requests
220340b4f539SKevin Wolf     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
220440b4f539SKevin Wolf 
2205453f9a16SKevin Wolf     /*
2206453f9a16SKevin Wolf      * Run the aio requests. As soon as one request can't be submitted
2207453f9a16SKevin Wolf      * successfully, fail all requests that are not yet submitted (we must
2208453f9a16SKevin Wolf      * return failure for all requests anyway)
2209453f9a16SKevin Wolf      *
2210453f9a16SKevin Wolf      * num_requests cannot be set to the right value immediately: If
2211453f9a16SKevin Wolf      * bdrv_aio_writev fails for some request, num_requests would be too high
2212453f9a16SKevin Wolf      * and therefore multiwrite_cb() would never recognize the multiwrite
2213453f9a16SKevin Wolf      * request as completed. We also cannot use the loop variable i to set it
2214453f9a16SKevin Wolf      * when the first request fails because the callback may already have been
2215453f9a16SKevin Wolf      * called for previously submitted requests. Thus, num_requests must be
2216453f9a16SKevin Wolf      * incremented for each request that is submitted.
2217453f9a16SKevin Wolf      *
2218453f9a16SKevin Wolf      * The problem that callbacks may be called early also means that we need
2219453f9a16SKevin Wolf      * to take care that num_requests doesn't become 0 before all requests are
2220453f9a16SKevin Wolf      * submitted - multiwrite_cb() would consider the multiwrite request
2221453f9a16SKevin Wolf      * completed. A dummy request that is "completed" by a manual call to
2222453f9a16SKevin Wolf      * multiwrite_cb() takes care of this.
2223453f9a16SKevin Wolf      */
2224453f9a16SKevin Wolf     mcb->num_requests = 1;
2225453f9a16SKevin Wolf 
222640b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
2227453f9a16SKevin Wolf         mcb->num_requests++;
222840b4f539SKevin Wolf         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
222940b4f539SKevin Wolf             reqs[i].nb_sectors, multiwrite_cb, mcb);
223040b4f539SKevin Wolf 
223140b4f539SKevin Wolf         if (acb == NULL) {
223240b4f539SKevin Wolf             // We can only fail the whole thing if no request has been
223340b4f539SKevin Wolf             // submitted yet. Otherwise we'll wait for the submitted AIOs to
223440b4f539SKevin Wolf             // complete and report the error in the callback.
2235453f9a16SKevin Wolf             if (i == 0) {
223640b4f539SKevin Wolf                 goto fail;
223740b4f539SKevin Wolf             } else {
22387eb58a6cSKevin Wolf                 multiwrite_cb(mcb, -EIO);
223940b4f539SKevin Wolf                 break;
224040b4f539SKevin Wolf             }
224140b4f539SKevin Wolf         }
224240b4f539SKevin Wolf     }
224340b4f539SKevin Wolf 
2244453f9a16SKevin Wolf     /* Complete the dummy request */
2245453f9a16SKevin Wolf     multiwrite_cb(mcb, 0);
2246453f9a16SKevin Wolf 
224740b4f539SKevin Wolf     return 0;
224840b4f539SKevin Wolf 
224940b4f539SKevin Wolf fail:
2250453f9a16SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
2251453f9a16SKevin Wolf         reqs[i].error = -EIO;
2252453f9a16SKevin Wolf     }
2253af474591SBruce Rogers     qemu_free(mcb);
225440b4f539SKevin Wolf     return -1;
225540b4f539SKevin Wolf }
225640b4f539SKevin Wolf 
2257b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2258b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2259b2e12bc6SChristoph Hellwig {
2260b2e12bc6SChristoph Hellwig     BlockDriver *drv = bs->drv;
2261b2e12bc6SChristoph Hellwig 
2262016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
2263016f5cf6SAlexander Graf         return bdrv_aio_noop_em(bs, cb, opaque);
2264016f5cf6SAlexander Graf     }
2265016f5cf6SAlexander Graf 
2266b2e12bc6SChristoph Hellwig     if (!drv)
2267b2e12bc6SChristoph Hellwig         return NULL;
2268b2e12bc6SChristoph Hellwig     return drv->bdrv_aio_flush(bs, cb, opaque);
2269b2e12bc6SChristoph Hellwig }
2270b2e12bc6SChristoph Hellwig 
227183f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
227283f64091Sbellard {
22736bbff9a0Saliguori     acb->pool->cancel(acb);
227483f64091Sbellard }
227583f64091Sbellard 
227683f64091Sbellard 
227783f64091Sbellard /**************************************************************/
227883f64091Sbellard /* async block device emulation */
227983f64091Sbellard 
2280c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync {
2281c16b5a2cSChristoph Hellwig     BlockDriverAIOCB common;
2282c16b5a2cSChristoph Hellwig     QEMUBH *bh;
2283c16b5a2cSChristoph Hellwig     int ret;
2284c16b5a2cSChristoph Hellwig     /* vector translation state */
2285c16b5a2cSChristoph Hellwig     QEMUIOVector *qiov;
2286c16b5a2cSChristoph Hellwig     uint8_t *bounce;
2287c16b5a2cSChristoph Hellwig     int is_write;
2288c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync;
2289c16b5a2cSChristoph Hellwig 
2290c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2291c16b5a2cSChristoph Hellwig {
2292b666d239SKevin Wolf     BlockDriverAIOCBSync *acb =
2293b666d239SKevin Wolf         container_of(blockacb, BlockDriverAIOCBSync, common);
22946a7ad299SDor Laor     qemu_bh_delete(acb->bh);
229536afc451SAvi Kivity     acb->bh = NULL;
2296c16b5a2cSChristoph Hellwig     qemu_aio_release(acb);
2297c16b5a2cSChristoph Hellwig }
2298c16b5a2cSChristoph Hellwig 
2299c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = {
2300c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2301c16b5a2cSChristoph Hellwig     .cancel             = bdrv_aio_cancel_em,
2302c16b5a2cSChristoph Hellwig };
2303c16b5a2cSChristoph Hellwig 
230483f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
2305beac80cdSbellard {
2306ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
2307f141eafeSaliguori 
2308f141eafeSaliguori     if (!acb->is_write)
2309f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2310ceb42de8Saliguori     qemu_vfree(acb->bounce);
2311ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
23126a7ad299SDor Laor     qemu_bh_delete(acb->bh);
231336afc451SAvi Kivity     acb->bh = NULL;
2314ce1a14dcSpbrook     qemu_aio_release(acb);
2315beac80cdSbellard }
2316beac80cdSbellard 
2317f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2318f141eafeSaliguori                                             int64_t sector_num,
2319f141eafeSaliguori                                             QEMUIOVector *qiov,
2320f141eafeSaliguori                                             int nb_sectors,
2321f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
2322f141eafeSaliguori                                             void *opaque,
2323f141eafeSaliguori                                             int is_write)
2324f141eafeSaliguori 
2325ea2384d3Sbellard {
2326ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
232783f64091Sbellard 
2328c16b5a2cSChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2329f141eafeSaliguori     acb->is_write = is_write;
2330f141eafeSaliguori     acb->qiov = qiov;
2331e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
2332f141eafeSaliguori 
2333ce1a14dcSpbrook     if (!acb->bh)
2334ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2335f141eafeSaliguori 
2336f141eafeSaliguori     if (is_write) {
2337f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2338f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2339f141eafeSaliguori     } else {
2340f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2341f141eafeSaliguori     }
2342f141eafeSaliguori 
2343ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
2344f141eafeSaliguori 
2345ce1a14dcSpbrook     return &acb->common;
23467a6cba61Spbrook }
23477a6cba61Spbrook 
2348f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2349f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2350ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
235183f64091Sbellard {
2352f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
235383f64091Sbellard }
235483f64091Sbellard 
2355f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2356f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2357f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
2358f141eafeSaliguori {
2359f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2360f141eafeSaliguori }
2361f141eafeSaliguori 
2362b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2363b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2364b2e12bc6SChristoph Hellwig {
2365b2e12bc6SChristoph Hellwig     BlockDriverAIOCBSync *acb;
2366b2e12bc6SChristoph Hellwig 
2367b2e12bc6SChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2368b2e12bc6SChristoph Hellwig     acb->is_write = 1; /* don't bounce in the completion hadler */
2369b2e12bc6SChristoph Hellwig     acb->qiov = NULL;
2370b2e12bc6SChristoph Hellwig     acb->bounce = NULL;
2371b2e12bc6SChristoph Hellwig     acb->ret = 0;
2372b2e12bc6SChristoph Hellwig 
2373b2e12bc6SChristoph Hellwig     if (!acb->bh)
2374b2e12bc6SChristoph Hellwig         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2375b2e12bc6SChristoph Hellwig 
2376b2e12bc6SChristoph Hellwig     bdrv_flush(bs);
2377b2e12bc6SChristoph Hellwig     qemu_bh_schedule(acb->bh);
2378b2e12bc6SChristoph Hellwig     return &acb->common;
2379b2e12bc6SChristoph Hellwig }
2380b2e12bc6SChristoph Hellwig 
2381016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2382016f5cf6SAlexander Graf         BlockDriverCompletionFunc *cb, void *opaque)
2383016f5cf6SAlexander Graf {
2384016f5cf6SAlexander Graf     BlockDriverAIOCBSync *acb;
2385016f5cf6SAlexander Graf 
2386016f5cf6SAlexander Graf     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2387016f5cf6SAlexander Graf     acb->is_write = 1; /* don't bounce in the completion handler */
2388016f5cf6SAlexander Graf     acb->qiov = NULL;
2389016f5cf6SAlexander Graf     acb->bounce = NULL;
2390016f5cf6SAlexander Graf     acb->ret = 0;
2391016f5cf6SAlexander Graf 
2392016f5cf6SAlexander Graf     if (!acb->bh) {
2393016f5cf6SAlexander Graf         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2394016f5cf6SAlexander Graf     }
2395016f5cf6SAlexander Graf 
2396016f5cf6SAlexander Graf     qemu_bh_schedule(acb->bh);
2397016f5cf6SAlexander Graf     return &acb->common;
2398016f5cf6SAlexander Graf }
2399016f5cf6SAlexander Graf 
240083f64091Sbellard /**************************************************************/
240183f64091Sbellard /* sync block device emulation */
240283f64091Sbellard 
240383f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
240483f64091Sbellard {
240583f64091Sbellard     *(int *)opaque = ret;
240683f64091Sbellard }
240783f64091Sbellard 
240883f64091Sbellard #define NOT_DONE 0x7fffffff
240983f64091Sbellard 
241083f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
241183f64091Sbellard                         uint8_t *buf, int nb_sectors)
241283f64091Sbellard {
2413ce1a14dcSpbrook     int async_ret;
2414ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2415f141eafeSaliguori     struct iovec iov;
2416f141eafeSaliguori     QEMUIOVector qiov;
241783f64091Sbellard 
241865d6b3d8SKevin Wolf     async_context_push();
241965d6b3d8SKevin Wolf 
242083f64091Sbellard     async_ret = NOT_DONE;
24213f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
2422eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2423f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2424f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
242583f64091Sbellard         bdrv_rw_em_cb, &async_ret);
242665d6b3d8SKevin Wolf     if (acb == NULL) {
242765d6b3d8SKevin Wolf         async_ret = -1;
242865d6b3d8SKevin Wolf         goto fail;
242965d6b3d8SKevin Wolf     }
2430baf35cb9Saliguori 
243183f64091Sbellard     while (async_ret == NOT_DONE) {
243283f64091Sbellard         qemu_aio_wait();
243383f64091Sbellard     }
2434baf35cb9Saliguori 
243565d6b3d8SKevin Wolf 
243665d6b3d8SKevin Wolf fail:
243765d6b3d8SKevin Wolf     async_context_pop();
243883f64091Sbellard     return async_ret;
243983f64091Sbellard }
244083f64091Sbellard 
244183f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
244283f64091Sbellard                          const uint8_t *buf, int nb_sectors)
244383f64091Sbellard {
2444ce1a14dcSpbrook     int async_ret;
2445ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2446f141eafeSaliguori     struct iovec iov;
2447f141eafeSaliguori     QEMUIOVector qiov;
244883f64091Sbellard 
244965d6b3d8SKevin Wolf     async_context_push();
245065d6b3d8SKevin Wolf 
245183f64091Sbellard     async_ret = NOT_DONE;
2452f141eafeSaliguori     iov.iov_base = (void *)buf;
2453eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2454f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2455f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
245683f64091Sbellard         bdrv_rw_em_cb, &async_ret);
245765d6b3d8SKevin Wolf     if (acb == NULL) {
245865d6b3d8SKevin Wolf         async_ret = -1;
245965d6b3d8SKevin Wolf         goto fail;
246065d6b3d8SKevin Wolf     }
246183f64091Sbellard     while (async_ret == NOT_DONE) {
246283f64091Sbellard         qemu_aio_wait();
246383f64091Sbellard     }
246465d6b3d8SKevin Wolf 
246565d6b3d8SKevin Wolf fail:
246665d6b3d8SKevin Wolf     async_context_pop();
246783f64091Sbellard     return async_ret;
246883f64091Sbellard }
2469ea2384d3Sbellard 
2470ea2384d3Sbellard void bdrv_init(void)
2471ea2384d3Sbellard {
24725efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
2473ea2384d3Sbellard }
2474ce1a14dcSpbrook 
2475eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void)
2476eb852011SMarkus Armbruster {
2477eb852011SMarkus Armbruster     use_bdrv_whitelist = 1;
2478eb852011SMarkus Armbruster     bdrv_init();
2479eb852011SMarkus Armbruster }
2480eb852011SMarkus Armbruster 
2481c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
24826bbff9a0Saliguori                    BlockDriverCompletionFunc *cb, void *opaque)
24836bbff9a0Saliguori {
2484ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2485ce1a14dcSpbrook 
24866bbff9a0Saliguori     if (pool->free_aiocb) {
24876bbff9a0Saliguori         acb = pool->free_aiocb;
24886bbff9a0Saliguori         pool->free_aiocb = acb->next;
2489ce1a14dcSpbrook     } else {
24906bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
24916bbff9a0Saliguori         acb->pool = pool;
2492ce1a14dcSpbrook     }
2493ce1a14dcSpbrook     acb->bs = bs;
2494ce1a14dcSpbrook     acb->cb = cb;
2495ce1a14dcSpbrook     acb->opaque = opaque;
2496ce1a14dcSpbrook     return acb;
2497ce1a14dcSpbrook }
2498ce1a14dcSpbrook 
2499ce1a14dcSpbrook void qemu_aio_release(void *p)
2500ce1a14dcSpbrook {
25016bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
25026bbff9a0Saliguori     AIOPool *pool = acb->pool;
25036bbff9a0Saliguori     acb->next = pool->free_aiocb;
25046bbff9a0Saliguori     pool->free_aiocb = acb;
2505ce1a14dcSpbrook }
250619cb3738Sbellard 
250719cb3738Sbellard /**************************************************************/
250819cb3738Sbellard /* removable device support */
250919cb3738Sbellard 
251019cb3738Sbellard /**
251119cb3738Sbellard  * Return TRUE if the media is present
251219cb3738Sbellard  */
251319cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
251419cb3738Sbellard {
251519cb3738Sbellard     BlockDriver *drv = bs->drv;
251619cb3738Sbellard     int ret;
251719cb3738Sbellard     if (!drv)
251819cb3738Sbellard         return 0;
251919cb3738Sbellard     if (!drv->bdrv_is_inserted)
2520*4be9762aSMarkus Armbruster         return !bs->tray_open;
252119cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
252219cb3738Sbellard     return ret;
252319cb3738Sbellard }
252419cb3738Sbellard 
252519cb3738Sbellard /**
252619cb3738Sbellard  * Return TRUE if the media changed since the last call to this
252719cb3738Sbellard  * function. It is currently only used for floppy disks
252819cb3738Sbellard  */
252919cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
253019cb3738Sbellard {
253119cb3738Sbellard     BlockDriver *drv = bs->drv;
253219cb3738Sbellard     int ret;
253319cb3738Sbellard 
253419cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
253519cb3738Sbellard         ret = -ENOTSUP;
253619cb3738Sbellard     else
253719cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
253819cb3738Sbellard     if (ret == -ENOTSUP)
253919cb3738Sbellard         ret = bs->media_changed;
254019cb3738Sbellard     bs->media_changed = 0;
254119cb3738Sbellard     return ret;
254219cb3738Sbellard }
254319cb3738Sbellard 
254419cb3738Sbellard /**
254519cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
254619cb3738Sbellard  */
2547aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag)
254819cb3738Sbellard {
254919cb3738Sbellard     BlockDriver *drv = bs->drv;
255019cb3738Sbellard     int ret;
255119cb3738Sbellard 
2552aea2a33cSMark McLoughlin     if (bs->locked) {
2553aea2a33cSMark McLoughlin         return -EBUSY;
2554aea2a33cSMark McLoughlin     }
2555aea2a33cSMark McLoughlin 
255619cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
255719cb3738Sbellard         ret = -ENOTSUP;
255819cb3738Sbellard     } else {
255919cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
256019cb3738Sbellard     }
256119cb3738Sbellard     if (ret == -ENOTSUP) {
2562aea2a33cSMark McLoughlin         ret = 0;
256319cb3738Sbellard     }
2564*4be9762aSMarkus Armbruster     if (ret >= 0) {
2565*4be9762aSMarkus Armbruster         bs->tray_open = eject_flag;
2566*4be9762aSMarkus Armbruster     }
2567aea2a33cSMark McLoughlin 
2568aea2a33cSMark McLoughlin     return ret;
256919cb3738Sbellard }
257019cb3738Sbellard 
257119cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
257219cb3738Sbellard {
257319cb3738Sbellard     return bs->locked;
257419cb3738Sbellard }
257519cb3738Sbellard 
257619cb3738Sbellard /**
257719cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
257819cb3738Sbellard  * to eject it manually).
257919cb3738Sbellard  */
258019cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
258119cb3738Sbellard {
258219cb3738Sbellard     BlockDriver *drv = bs->drv;
258319cb3738Sbellard 
258419cb3738Sbellard     bs->locked = locked;
258519cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
258619cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
258719cb3738Sbellard     }
258819cb3738Sbellard }
2589985a03b0Sths 
2590985a03b0Sths /* needed for generic scsi interface */
2591985a03b0Sths 
2592985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2593985a03b0Sths {
2594985a03b0Sths     BlockDriver *drv = bs->drv;
2595985a03b0Sths 
2596985a03b0Sths     if (drv && drv->bdrv_ioctl)
2597985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
2598985a03b0Sths     return -ENOTSUP;
2599985a03b0Sths }
26007d780669Saliguori 
2601221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2602221f715dSaliguori         unsigned long int req, void *buf,
26037d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
26047d780669Saliguori {
2605221f715dSaliguori     BlockDriver *drv = bs->drv;
26067d780669Saliguori 
2607221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
2608221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2609221f715dSaliguori     return NULL;
26107d780669Saliguori }
2611e268ca52Saliguori 
26127cd1e32aSlirans@il.ibm.com 
26137cd1e32aSlirans@il.ibm.com 
2614e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
2615e268ca52Saliguori {
2616e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2617e268ca52Saliguori }
26187cd1e32aSlirans@il.ibm.com 
26197cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
26207cd1e32aSlirans@il.ibm.com {
26217cd1e32aSlirans@il.ibm.com     int64_t bitmap_size;
2622a55eb92cSJan Kiszka 
2623aaa0eb75SLiran Schour     bs->dirty_count = 0;
26247cd1e32aSlirans@il.ibm.com     if (enable) {
2625c6d22830SJan Kiszka         if (!bs->dirty_bitmap) {
2626c6d22830SJan Kiszka             bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2627c6d22830SJan Kiszka                     BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2628c6d22830SJan Kiszka             bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
26297cd1e32aSlirans@il.ibm.com 
26307cd1e32aSlirans@il.ibm.com             bs->dirty_bitmap = qemu_mallocz(bitmap_size);
26317cd1e32aSlirans@il.ibm.com         }
26327cd1e32aSlirans@il.ibm.com     } else {
2633c6d22830SJan Kiszka         if (bs->dirty_bitmap) {
26347cd1e32aSlirans@il.ibm.com             qemu_free(bs->dirty_bitmap);
2635c6d22830SJan Kiszka             bs->dirty_bitmap = NULL;
26367cd1e32aSlirans@il.ibm.com         }
26377cd1e32aSlirans@il.ibm.com     }
26387cd1e32aSlirans@il.ibm.com }
26397cd1e32aSlirans@il.ibm.com 
26407cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
26417cd1e32aSlirans@il.ibm.com {
26426ea44308SJan Kiszka     int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
26437cd1e32aSlirans@il.ibm.com 
2644c6d22830SJan Kiszka     if (bs->dirty_bitmap &&
2645c6d22830SJan Kiszka         (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2646c6d22830SJan Kiszka         return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2647c6d22830SJan Kiszka             (1 << (chunk % (sizeof(unsigned long) * 8)));
26487cd1e32aSlirans@il.ibm.com     } else {
26497cd1e32aSlirans@il.ibm.com         return 0;
26507cd1e32aSlirans@il.ibm.com     }
26517cd1e32aSlirans@il.ibm.com }
26527cd1e32aSlirans@il.ibm.com 
26537cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
26547cd1e32aSlirans@il.ibm.com                       int nr_sectors)
26557cd1e32aSlirans@il.ibm.com {
26567cd1e32aSlirans@il.ibm.com     set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
26577cd1e32aSlirans@il.ibm.com }
2658aaa0eb75SLiran Schour 
2659aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2660aaa0eb75SLiran Schour {
2661aaa0eb75SLiran Schour     return bs->dirty_count;
2662aaa0eb75SLiran Schour }
2663