xref: /openbmc/qemu/block.c (revision ee1811965fd15e0b41f8d508b951a8ab826ae3a7)
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;
748*ee181196SKevin Wolf     BlockDriver *backing_drv;
7498a426614SKevin Wolf     int64_t sector, total_sectors;
7508a426614SKevin Wolf     int n, ro, open_flags;
7514dca4b63SNaphtali Sprei     int ret = 0, rw_ret = 0;
7528a426614SKevin Wolf     uint8_t *buf;
7534dca4b63SNaphtali Sprei     char filename[1024];
7544dca4b63SNaphtali Sprei     BlockDriverState *bs_rw, *bs_ro;
75533e3963eSbellard 
75619cb3738Sbellard     if (!drv)
75719cb3738Sbellard         return -ENOMEDIUM;
75833e3963eSbellard 
7594dca4b63SNaphtali Sprei     if (!bs->backing_hd) {
7604dca4b63SNaphtali Sprei         return -ENOTSUP;
7614dca4b63SNaphtali Sprei     }
7624dca4b63SNaphtali Sprei 
7634dca4b63SNaphtali Sprei     if (bs->backing_hd->keep_read_only) {
764ea2384d3Sbellard         return -EACCES;
76533e3963eSbellard     }
76633e3963eSbellard 
767*ee181196SKevin Wolf     backing_drv = bs->backing_hd->drv;
7684dca4b63SNaphtali Sprei     ro = bs->backing_hd->read_only;
7694dca4b63SNaphtali Sprei     strncpy(filename, bs->backing_hd->filename, sizeof(filename));
7704dca4b63SNaphtali Sprei     open_flags =  bs->backing_hd->open_flags;
7714dca4b63SNaphtali Sprei 
7724dca4b63SNaphtali Sprei     if (ro) {
7734dca4b63SNaphtali Sprei         /* re-open as RW */
7744dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
7754dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
7764dca4b63SNaphtali Sprei         bs_rw = bdrv_new("");
777*ee181196SKevin Wolf         rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
778*ee181196SKevin Wolf             backing_drv);
7794dca4b63SNaphtali Sprei         if (rw_ret < 0) {
7804dca4b63SNaphtali Sprei             bdrv_delete(bs_rw);
7814dca4b63SNaphtali Sprei             /* try to re-open read-only */
7824dca4b63SNaphtali Sprei             bs_ro = bdrv_new("");
783*ee181196SKevin Wolf             ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
784*ee181196SKevin Wolf                 backing_drv);
7854dca4b63SNaphtali Sprei             if (ret < 0) {
7864dca4b63SNaphtali Sprei                 bdrv_delete(bs_ro);
7874dca4b63SNaphtali Sprei                 /* drive not functional anymore */
7884dca4b63SNaphtali Sprei                 bs->drv = NULL;
7894dca4b63SNaphtali Sprei                 return ret;
7904dca4b63SNaphtali Sprei             }
7914dca4b63SNaphtali Sprei             bs->backing_hd = bs_ro;
7924dca4b63SNaphtali Sprei             return rw_ret;
7934dca4b63SNaphtali Sprei         }
7944dca4b63SNaphtali Sprei         bs->backing_hd = bs_rw;
795ea2384d3Sbellard     }
796ea2384d3Sbellard 
7976ea44308SJan Kiszka     total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
7988a426614SKevin Wolf     buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
7998a426614SKevin Wolf 
8008a426614SKevin Wolf     for (sector = 0; sector < total_sectors; sector += n) {
8018a426614SKevin Wolf         if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
8028a426614SKevin Wolf 
8038a426614SKevin Wolf             if (bdrv_read(bs, sector, buf, n) != 0) {
8044dca4b63SNaphtali Sprei                 ret = -EIO;
8054dca4b63SNaphtali Sprei                 goto ro_cleanup;
80633e3963eSbellard             }
80733e3963eSbellard 
8088a426614SKevin Wolf             if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
8094dca4b63SNaphtali Sprei                 ret = -EIO;
8104dca4b63SNaphtali Sprei                 goto ro_cleanup;
81133e3963eSbellard             }
81233e3963eSbellard         }
81333e3963eSbellard     }
81495389c86Sbellard 
8151d44952fSChristoph Hellwig     if (drv->bdrv_make_empty) {
8161d44952fSChristoph Hellwig         ret = drv->bdrv_make_empty(bs);
8171d44952fSChristoph Hellwig         bdrv_flush(bs);
8181d44952fSChristoph Hellwig     }
81995389c86Sbellard 
8203f5075aeSChristoph Hellwig     /*
8213f5075aeSChristoph Hellwig      * Make sure all data we wrote to the backing device is actually
8223f5075aeSChristoph Hellwig      * stable on disk.
8233f5075aeSChristoph Hellwig      */
8243f5075aeSChristoph Hellwig     if (bs->backing_hd)
8253f5075aeSChristoph Hellwig         bdrv_flush(bs->backing_hd);
8264dca4b63SNaphtali Sprei 
8274dca4b63SNaphtali Sprei ro_cleanup:
8288a426614SKevin Wolf     qemu_free(buf);
8294dca4b63SNaphtali Sprei 
8304dca4b63SNaphtali Sprei     if (ro) {
8314dca4b63SNaphtali Sprei         /* re-open as RO */
8324dca4b63SNaphtali Sprei         bdrv_delete(bs->backing_hd);
8334dca4b63SNaphtali Sprei         bs->backing_hd = NULL;
8344dca4b63SNaphtali Sprei         bs_ro = bdrv_new("");
835*ee181196SKevin Wolf         ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
836*ee181196SKevin Wolf             backing_drv);
8374dca4b63SNaphtali Sprei         if (ret < 0) {
8384dca4b63SNaphtali Sprei             bdrv_delete(bs_ro);
8394dca4b63SNaphtali Sprei             /* drive not functional anymore */
8404dca4b63SNaphtali Sprei             bs->drv = NULL;
8414dca4b63SNaphtali Sprei             return ret;
8424dca4b63SNaphtali Sprei         }
8434dca4b63SNaphtali Sprei         bs->backing_hd = bs_ro;
8444dca4b63SNaphtali Sprei         bs->backing_hd->keep_read_only = 0;
8454dca4b63SNaphtali Sprei     }
8464dca4b63SNaphtali Sprei 
8471d44952fSChristoph Hellwig     return ret;
84833e3963eSbellard }
84933e3963eSbellard 
8506ab4b5abSMarkus Armbruster void bdrv_commit_all(void)
8516ab4b5abSMarkus Armbruster {
8526ab4b5abSMarkus Armbruster     BlockDriverState *bs;
8536ab4b5abSMarkus Armbruster 
8546ab4b5abSMarkus Armbruster     QTAILQ_FOREACH(bs, &bdrv_states, list) {
8556ab4b5abSMarkus Armbruster         bdrv_commit(bs);
8566ab4b5abSMarkus Armbruster     }
8576ab4b5abSMarkus Armbruster }
8586ab4b5abSMarkus Armbruster 
859756e6736SKevin Wolf /*
860756e6736SKevin Wolf  * Return values:
861756e6736SKevin Wolf  * 0        - success
862756e6736SKevin Wolf  * -EINVAL  - backing format specified, but no file
863756e6736SKevin Wolf  * -ENOSPC  - can't update the backing file because no space is left in the
864756e6736SKevin Wolf  *            image file header
865756e6736SKevin Wolf  * -ENOTSUP - format driver doesn't support changing the backing file
866756e6736SKevin Wolf  */
867756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs,
868756e6736SKevin Wolf     const char *backing_file, const char *backing_fmt)
869756e6736SKevin Wolf {
870756e6736SKevin Wolf     BlockDriver *drv = bs->drv;
871756e6736SKevin Wolf 
872756e6736SKevin Wolf     if (drv->bdrv_change_backing_file != NULL) {
873756e6736SKevin Wolf         return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
874756e6736SKevin Wolf     } else {
875756e6736SKevin Wolf         return -ENOTSUP;
876756e6736SKevin Wolf     }
877756e6736SKevin Wolf }
878756e6736SKevin Wolf 
87971d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
88071d0770cSaliguori                                    size_t size)
88171d0770cSaliguori {
88271d0770cSaliguori     int64_t len;
88371d0770cSaliguori 
88471d0770cSaliguori     if (!bdrv_is_inserted(bs))
88571d0770cSaliguori         return -ENOMEDIUM;
88671d0770cSaliguori 
88771d0770cSaliguori     if (bs->growable)
88871d0770cSaliguori         return 0;
88971d0770cSaliguori 
89071d0770cSaliguori     len = bdrv_getlength(bs);
89171d0770cSaliguori 
892fbb7b4e0SKevin Wolf     if (offset < 0)
893fbb7b4e0SKevin Wolf         return -EIO;
894fbb7b4e0SKevin Wolf 
895fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
89671d0770cSaliguori         return -EIO;
89771d0770cSaliguori 
89871d0770cSaliguori     return 0;
89971d0770cSaliguori }
90071d0770cSaliguori 
90171d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
90271d0770cSaliguori                               int nb_sectors)
90371d0770cSaliguori {
904eb5a3165SJes Sorensen     return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
905eb5a3165SJes Sorensen                                    nb_sectors * BDRV_SECTOR_SIZE);
90671d0770cSaliguori }
90771d0770cSaliguori 
90819cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
909fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
910fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
911fc01f7e7Sbellard {
912ea2384d3Sbellard     BlockDriver *drv = bs->drv;
913fc01f7e7Sbellard 
91419cb3738Sbellard     if (!drv)
91519cb3738Sbellard         return -ENOMEDIUM;
91671d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
91771d0770cSaliguori         return -EIO;
918b338082bSbellard 
91983f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
92083f64091Sbellard }
921fc01f7e7Sbellard 
9227cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
9237cd1e32aSlirans@il.ibm.com                              int nb_sectors, int dirty)
9247cd1e32aSlirans@il.ibm.com {
9257cd1e32aSlirans@il.ibm.com     int64_t start, end;
926c6d22830SJan Kiszka     unsigned long val, idx, bit;
927a55eb92cSJan Kiszka 
9286ea44308SJan Kiszka     start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
929c6d22830SJan Kiszka     end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
9307cd1e32aSlirans@il.ibm.com 
9317cd1e32aSlirans@il.ibm.com     for (; start <= end; start++) {
932c6d22830SJan Kiszka         idx = start / (sizeof(unsigned long) * 8);
933c6d22830SJan Kiszka         bit = start % (sizeof(unsigned long) * 8);
934c6d22830SJan Kiszka         val = bs->dirty_bitmap[idx];
935c6d22830SJan Kiszka         if (dirty) {
936aaa0eb75SLiran Schour             if (!(val & (1 << bit))) {
937aaa0eb75SLiran Schour                 bs->dirty_count++;
938c6d22830SJan Kiszka                 val |= 1 << bit;
939aaa0eb75SLiran Schour             }
940c6d22830SJan Kiszka         } else {
941aaa0eb75SLiran Schour             if (val & (1 << bit)) {
942aaa0eb75SLiran Schour                 bs->dirty_count--;
943c6d22830SJan Kiszka                 val &= ~(1 << bit);
944c6d22830SJan Kiszka             }
945aaa0eb75SLiran Schour         }
946c6d22830SJan Kiszka         bs->dirty_bitmap[idx] = val;
9477cd1e32aSlirans@il.ibm.com     }
9487cd1e32aSlirans@il.ibm.com }
9497cd1e32aSlirans@il.ibm.com 
95019cb3738Sbellard /* Return < 0 if error. Important errors are:
95119cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
95219cb3738Sbellard   -ENOMEDIUM   No media inserted.
95319cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
95419cb3738Sbellard   -EACCES      Trying to write a read-only device
95519cb3738Sbellard */
956fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
957fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
958fc01f7e7Sbellard {
95983f64091Sbellard     BlockDriver *drv = bs->drv;
96019cb3738Sbellard     if (!bs->drv)
96119cb3738Sbellard         return -ENOMEDIUM;
9620849bf08Sbellard     if (bs->read_only)
96319cb3738Sbellard         return -EACCES;
96471d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
96571d0770cSaliguori         return -EIO;
96671d0770cSaliguori 
967c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
9687cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
9697cd1e32aSlirans@il.ibm.com     }
9707cd1e32aSlirans@il.ibm.com 
971294cc35fSKevin Wolf     if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
972294cc35fSKevin Wolf         bs->wr_highest_sector = sector_num + nb_sectors - 1;
973294cc35fSKevin Wolf     }
974294cc35fSKevin Wolf 
97583f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
97683f64091Sbellard }
97783f64091Sbellard 
978eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
979eda578e5Saliguori                void *buf, int count1)
98083f64091Sbellard {
9816ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
98283f64091Sbellard     int len, nb_sectors, count;
98383f64091Sbellard     int64_t sector_num;
9849a8c4cceSKevin Wolf     int ret;
98583f64091Sbellard 
98683f64091Sbellard     count = count1;
98783f64091Sbellard     /* first read to align to sector start */
9886ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
98983f64091Sbellard     if (len > count)
99083f64091Sbellard         len = count;
9916ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
99283f64091Sbellard     if (len > 0) {
9939a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
9949a8c4cceSKevin Wolf             return ret;
9956ea44308SJan Kiszka         memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
99683f64091Sbellard         count -= len;
99783f64091Sbellard         if (count == 0)
99883f64091Sbellard             return count1;
99983f64091Sbellard         sector_num++;
100083f64091Sbellard         buf += len;
100183f64091Sbellard     }
100283f64091Sbellard 
100383f64091Sbellard     /* read the sectors "in place" */
10046ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
100583f64091Sbellard     if (nb_sectors > 0) {
10069a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
10079a8c4cceSKevin Wolf             return ret;
100883f64091Sbellard         sector_num += nb_sectors;
10096ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
101083f64091Sbellard         buf += len;
101183f64091Sbellard         count -= len;
101283f64091Sbellard     }
101383f64091Sbellard 
101483f64091Sbellard     /* add data from the last sector */
101583f64091Sbellard     if (count > 0) {
10169a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10179a8c4cceSKevin Wolf             return ret;
101883f64091Sbellard         memcpy(buf, tmp_buf, count);
101983f64091Sbellard     }
102083f64091Sbellard     return count1;
102183f64091Sbellard }
102283f64091Sbellard 
1023eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1024eda578e5Saliguori                 const void *buf, int count1)
102583f64091Sbellard {
10266ea44308SJan Kiszka     uint8_t tmp_buf[BDRV_SECTOR_SIZE];
102783f64091Sbellard     int len, nb_sectors, count;
102883f64091Sbellard     int64_t sector_num;
10299a8c4cceSKevin Wolf     int ret;
103083f64091Sbellard 
103183f64091Sbellard     count = count1;
103283f64091Sbellard     /* first write to align to sector start */
10336ea44308SJan Kiszka     len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
103483f64091Sbellard     if (len > count)
103583f64091Sbellard         len = count;
10366ea44308SJan Kiszka     sector_num = offset >> BDRV_SECTOR_BITS;
103783f64091Sbellard     if (len > 0) {
10389a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10399a8c4cceSKevin Wolf             return ret;
10406ea44308SJan Kiszka         memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
10419a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10429a8c4cceSKevin Wolf             return ret;
104383f64091Sbellard         count -= len;
104483f64091Sbellard         if (count == 0)
104583f64091Sbellard             return count1;
104683f64091Sbellard         sector_num++;
104783f64091Sbellard         buf += len;
104883f64091Sbellard     }
104983f64091Sbellard 
105083f64091Sbellard     /* write the sectors "in place" */
10516ea44308SJan Kiszka     nb_sectors = count >> BDRV_SECTOR_BITS;
105283f64091Sbellard     if (nb_sectors > 0) {
10539a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
10549a8c4cceSKevin Wolf             return ret;
105583f64091Sbellard         sector_num += nb_sectors;
10566ea44308SJan Kiszka         len = nb_sectors << BDRV_SECTOR_BITS;
105783f64091Sbellard         buf += len;
105883f64091Sbellard         count -= len;
105983f64091Sbellard     }
106083f64091Sbellard 
106183f64091Sbellard     /* add data from the last sector */
106283f64091Sbellard     if (count > 0) {
10639a8c4cceSKevin Wolf         if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
10649a8c4cceSKevin Wolf             return ret;
106583f64091Sbellard         memcpy(tmp_buf, buf, count);
10669a8c4cceSKevin Wolf         if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
10679a8c4cceSKevin Wolf             return ret;
106883f64091Sbellard     }
106983f64091Sbellard     return count1;
107083f64091Sbellard }
107183f64091Sbellard 
1072f08145feSKevin Wolf /*
1073f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1074f08145feSKevin Wolf  * request (acts as a barrier)
1075f08145feSKevin Wolf  *
1076f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1077f08145feSKevin Wolf  */
1078f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1079f08145feSKevin Wolf     const void *buf, int count)
1080f08145feSKevin Wolf {
1081f08145feSKevin Wolf     int ret;
1082f08145feSKevin Wolf 
1083f08145feSKevin Wolf     ret = bdrv_pwrite(bs, offset, buf, count);
1084f08145feSKevin Wolf     if (ret < 0) {
1085f08145feSKevin Wolf         return ret;
1086f08145feSKevin Wolf     }
1087f08145feSKevin Wolf 
1088f08145feSKevin Wolf     /* No flush needed for cache=writethrough, it uses O_DSYNC */
1089f08145feSKevin Wolf     if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) {
1090f08145feSKevin Wolf         bdrv_flush(bs);
1091f08145feSKevin Wolf     }
1092f08145feSKevin Wolf 
1093f08145feSKevin Wolf     return 0;
1094f08145feSKevin Wolf }
1095f08145feSKevin Wolf 
1096f08145feSKevin Wolf /*
1097f08145feSKevin Wolf  * Writes to the file and ensures that no writes are reordered across this
1098f08145feSKevin Wolf  * request (acts as a barrier)
1099f08145feSKevin Wolf  *
1100f08145feSKevin Wolf  * Returns 0 on success, -errno in error cases.
1101f08145feSKevin Wolf  */
1102f08145feSKevin Wolf int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num,
1103f08145feSKevin Wolf     const uint8_t *buf, int nb_sectors)
1104f08145feSKevin Wolf {
1105f08145feSKevin Wolf     return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num,
1106f08145feSKevin Wolf         buf, BDRV_SECTOR_SIZE * nb_sectors);
1107f08145feSKevin Wolf }
1108f08145feSKevin Wolf 
110983f64091Sbellard /**
111083f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
111183f64091Sbellard  */
111283f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
111383f64091Sbellard {
111483f64091Sbellard     BlockDriver *drv = bs->drv;
111551762288SStefan Hajnoczi     int ret;
111683f64091Sbellard     if (!drv)
111719cb3738Sbellard         return -ENOMEDIUM;
111883f64091Sbellard     if (!drv->bdrv_truncate)
111983f64091Sbellard         return -ENOTSUP;
112059f2689dSNaphtali Sprei     if (bs->read_only)
112159f2689dSNaphtali Sprei         return -EACCES;
112251762288SStefan Hajnoczi     ret = drv->bdrv_truncate(bs, offset);
112351762288SStefan Hajnoczi     if (ret == 0) {
112451762288SStefan Hajnoczi         ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
112551762288SStefan Hajnoczi     }
112651762288SStefan Hajnoczi     return ret;
112783f64091Sbellard }
112883f64091Sbellard 
112983f64091Sbellard /**
113083f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
113183f64091Sbellard  */
113283f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
113383f64091Sbellard {
113483f64091Sbellard     BlockDriver *drv = bs->drv;
113583f64091Sbellard     if (!drv)
113619cb3738Sbellard         return -ENOMEDIUM;
113751762288SStefan Hajnoczi 
113851762288SStefan Hajnoczi     /* Fixed size devices use the total_sectors value for speed instead of
113951762288SStefan Hajnoczi        issuing a length query (like lseek) on each call.  Also, legacy block
114051762288SStefan Hajnoczi        drivers don't provide a bdrv_getlength function and must use
114151762288SStefan Hajnoczi        total_sectors. */
114251762288SStefan Hajnoczi     if (!bs->growable || !drv->bdrv_getlength) {
11436ea44308SJan Kiszka         return bs->total_sectors * BDRV_SECTOR_SIZE;
114483f64091Sbellard     }
114583f64091Sbellard     return drv->bdrv_getlength(bs);
1146fc01f7e7Sbellard }
1147fc01f7e7Sbellard 
114819cb3738Sbellard /* return 0 as number of sectors if no device present or error */
114996b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1150fc01f7e7Sbellard {
115119cb3738Sbellard     int64_t length;
115219cb3738Sbellard     length = bdrv_getlength(bs);
115319cb3738Sbellard     if (length < 0)
115419cb3738Sbellard         length = 0;
115519cb3738Sbellard     else
11566ea44308SJan Kiszka         length = length >> BDRV_SECTOR_BITS;
115719cb3738Sbellard     *nb_sectors_ptr = length;
1158fc01f7e7Sbellard }
1159cf98951bSbellard 
1160f3d54fc4Saliguori struct partition {
1161f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
1162f3d54fc4Saliguori         uint8_t head;               /* starting head */
1163f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
1164f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
1165f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
1166f3d54fc4Saliguori         uint8_t end_head;           /* end head */
1167f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
1168f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
1169f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
1170f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
1171f3d54fc4Saliguori } __attribute__((packed));
1172f3d54fc4Saliguori 
1173f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1174f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
1175f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
1176f3d54fc4Saliguori {
1177eb5a3165SJes Sorensen     uint8_t buf[BDRV_SECTOR_SIZE];
1178f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
1179f3d54fc4Saliguori     struct partition *p;
1180f3d54fc4Saliguori     uint32_t nr_sects;
1181a38131b6Sblueswir1     uint64_t nb_sectors;
1182f3d54fc4Saliguori 
1183f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1184f3d54fc4Saliguori 
1185f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
1186f3d54fc4Saliguori     if (ret < 0)
1187f3d54fc4Saliguori         return -1;
1188f3d54fc4Saliguori     /* test msdos magic */
1189f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
1190f3d54fc4Saliguori         return -1;
1191f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
1192f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
1193f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
1194f3d54fc4Saliguori         if (nr_sects && p->end_head) {
1195f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
1196f3d54fc4Saliguori                a cylinder boundary */
1197f3d54fc4Saliguori             heads = p->end_head + 1;
1198f3d54fc4Saliguori             sectors = p->end_sector & 63;
1199f3d54fc4Saliguori             if (sectors == 0)
1200f3d54fc4Saliguori                 continue;
1201f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
1202f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
1203f3d54fc4Saliguori                 continue;
1204f3d54fc4Saliguori             *pheads = heads;
1205f3d54fc4Saliguori             *psectors = sectors;
1206f3d54fc4Saliguori             *pcylinders = cylinders;
1207f3d54fc4Saliguori #if 0
1208f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
1209f3d54fc4Saliguori                    cylinders, heads, sectors);
1210f3d54fc4Saliguori #endif
1211f3d54fc4Saliguori             return 0;
1212f3d54fc4Saliguori         }
1213f3d54fc4Saliguori     }
1214f3d54fc4Saliguori     return -1;
1215f3d54fc4Saliguori }
1216f3d54fc4Saliguori 
1217f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1218f3d54fc4Saliguori {
1219f3d54fc4Saliguori     int translation, lba_detected = 0;
1220f3d54fc4Saliguori     int cylinders, heads, secs;
1221a38131b6Sblueswir1     uint64_t nb_sectors;
1222f3d54fc4Saliguori 
1223f3d54fc4Saliguori     /* if a geometry hint is available, use it */
1224f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
1225f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1226f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
1227f3d54fc4Saliguori     if (cylinders != 0) {
1228f3d54fc4Saliguori         *pcyls = cylinders;
1229f3d54fc4Saliguori         *pheads = heads;
1230f3d54fc4Saliguori         *psecs = secs;
1231f3d54fc4Saliguori     } else {
1232f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1233f3d54fc4Saliguori             if (heads > 16) {
1234f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
1235f3d54fc4Saliguori                    translation was active, so the default
1236f3d54fc4Saliguori                    hardware geometry is OK */
1237f3d54fc4Saliguori                 lba_detected = 1;
1238f3d54fc4Saliguori                 goto default_geometry;
1239f3d54fc4Saliguori             } else {
1240f3d54fc4Saliguori                 *pcyls = cylinders;
1241f3d54fc4Saliguori                 *pheads = heads;
1242f3d54fc4Saliguori                 *psecs = secs;
1243f3d54fc4Saliguori                 /* disable any translation to be in sync with
1244f3d54fc4Saliguori                    the logical geometry */
1245f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1246f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1247f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
1248f3d54fc4Saliguori                 }
1249f3d54fc4Saliguori             }
1250f3d54fc4Saliguori         } else {
1251f3d54fc4Saliguori         default_geometry:
1252f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
1253f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
1254f3d54fc4Saliguori 
1255f3d54fc4Saliguori             if (cylinders > 16383)
1256f3d54fc4Saliguori                 cylinders = 16383;
1257f3d54fc4Saliguori             else if (cylinders < 2)
1258f3d54fc4Saliguori                 cylinders = 2;
1259f3d54fc4Saliguori             *pcyls = cylinders;
1260f3d54fc4Saliguori             *pheads = 16;
1261f3d54fc4Saliguori             *psecs = 63;
1262f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1263f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
1264f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1265f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
1266f3d54fc4Saliguori                 } else {
1267f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
1268f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
1269f3d54fc4Saliguori                 }
1270f3d54fc4Saliguori             }
1271f3d54fc4Saliguori         }
1272f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1273f3d54fc4Saliguori     }
1274f3d54fc4Saliguori }
1275f3d54fc4Saliguori 
1276b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
1277b338082bSbellard                             int cyls, int heads, int secs)
1278b338082bSbellard {
1279b338082bSbellard     bs->cyls = cyls;
1280b338082bSbellard     bs->heads = heads;
1281b338082bSbellard     bs->secs = secs;
1282b338082bSbellard }
1283b338082bSbellard 
1284b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
1285b338082bSbellard {
1286b338082bSbellard     bs->type = type;
1287b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
1288b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
1289b338082bSbellard }
1290b338082bSbellard 
129146d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
129246d4767dSbellard {
129346d4767dSbellard     bs->translation = translation;
129446d4767dSbellard }
129546d4767dSbellard 
1296b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
1297b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
1298b338082bSbellard {
1299b338082bSbellard     *pcyls = bs->cyls;
1300b338082bSbellard     *pheads = bs->heads;
1301b338082bSbellard     *psecs = bs->secs;
1302b338082bSbellard }
1303b338082bSbellard 
1304b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
1305b338082bSbellard {
1306b338082bSbellard     return bs->type;
1307b338082bSbellard }
1308b338082bSbellard 
130946d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
131046d4767dSbellard {
131146d4767dSbellard     return bs->translation;
131246d4767dSbellard }
131346d4767dSbellard 
1314abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
1315abd7f68dSMarkus Armbruster                        BlockErrorAction on_write_error)
1316abd7f68dSMarkus Armbruster {
1317abd7f68dSMarkus Armbruster     bs->on_read_error = on_read_error;
1318abd7f68dSMarkus Armbruster     bs->on_write_error = on_write_error;
1319abd7f68dSMarkus Armbruster }
1320abd7f68dSMarkus Armbruster 
1321abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
1322abd7f68dSMarkus Armbruster {
1323abd7f68dSMarkus Armbruster     return is_read ? bs->on_read_error : bs->on_write_error;
1324abd7f68dSMarkus Armbruster }
1325abd7f68dSMarkus Armbruster 
13267d0d6950SMarkus Armbruster void bdrv_set_removable(BlockDriverState *bs, int removable)
13277d0d6950SMarkus Armbruster {
13287d0d6950SMarkus Armbruster     bs->removable = removable;
13297d0d6950SMarkus Armbruster     if (removable && bs == bs_snapshots) {
13307d0d6950SMarkus Armbruster         bs_snapshots = NULL;
13317d0d6950SMarkus Armbruster     }
13327d0d6950SMarkus Armbruster }
13337d0d6950SMarkus Armbruster 
1334b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
1335b338082bSbellard {
1336b338082bSbellard     return bs->removable;
1337b338082bSbellard }
1338b338082bSbellard 
1339b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
1340b338082bSbellard {
1341b338082bSbellard     return bs->read_only;
1342b338082bSbellard }
1343b338082bSbellard 
1344985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
1345985a03b0Sths {
1346985a03b0Sths     return bs->sg;
1347985a03b0Sths }
1348985a03b0Sths 
1349e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs)
1350e900a7b7SChristoph Hellwig {
1351e900a7b7SChristoph Hellwig     return bs->enable_write_cache;
1352e900a7b7SChristoph Hellwig }
1353e900a7b7SChristoph Hellwig 
135419cb3738Sbellard /* XXX: no longer used */
1355b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
1356b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
1357b338082bSbellard {
1358b338082bSbellard     bs->change_cb = change_cb;
1359b338082bSbellard     bs->change_opaque = opaque;
1360b338082bSbellard }
1361b338082bSbellard 
1362ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
1363ea2384d3Sbellard {
1364ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
1365ea2384d3Sbellard         return 1;
1366ea2384d3Sbellard     return bs->encrypted;
1367ea2384d3Sbellard }
1368ea2384d3Sbellard 
1369c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
1370c0f4ce77Saliguori {
1371c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
1372c0f4ce77Saliguori 
1373c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1374c0f4ce77Saliguori         return 1;
1375c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
1376c0f4ce77Saliguori }
1377c0f4ce77Saliguori 
1378ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
1379ea2384d3Sbellard {
1380ea2384d3Sbellard     int ret;
1381ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
1382ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
1383ea2384d3Sbellard         if (ret < 0)
1384ea2384d3Sbellard             return ret;
1385ea2384d3Sbellard         if (!bs->encrypted)
1386ea2384d3Sbellard             return 0;
1387ea2384d3Sbellard     }
1388fd04a2aeSShahar Havivi     if (!bs->encrypted) {
1389fd04a2aeSShahar Havivi         return -EINVAL;
1390fd04a2aeSShahar Havivi     } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1391fd04a2aeSShahar Havivi         return -ENOMEDIUM;
1392fd04a2aeSShahar Havivi     }
1393c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
1394bb5fc20fSaliguori     if (ret < 0) {
1395bb5fc20fSaliguori         bs->valid_key = 0;
1396bb5fc20fSaliguori     } else if (!bs->valid_key) {
1397bb5fc20fSaliguori         bs->valid_key = 1;
1398bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
1399bb5fc20fSaliguori         bs->media_changed = 1;
1400bb5fc20fSaliguori         if (bs->change_cb)
1401bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
1402bb5fc20fSaliguori     }
1403c0f4ce77Saliguori     return ret;
1404ea2384d3Sbellard }
1405ea2384d3Sbellard 
1406ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1407ea2384d3Sbellard {
140819cb3738Sbellard     if (!bs->drv) {
1409ea2384d3Sbellard         buf[0] = '\0';
1410ea2384d3Sbellard     } else {
1411ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
1412ea2384d3Sbellard     }
1413ea2384d3Sbellard }
1414ea2384d3Sbellard 
1415ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1416ea2384d3Sbellard                          void *opaque)
1417ea2384d3Sbellard {
1418ea2384d3Sbellard     BlockDriver *drv;
1419ea2384d3Sbellard 
14208a22f02aSStefan Hajnoczi     QLIST_FOREACH(drv, &bdrv_drivers, list) {
1421ea2384d3Sbellard         it(opaque, drv->format_name);
1422ea2384d3Sbellard     }
1423ea2384d3Sbellard }
1424ea2384d3Sbellard 
1425b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1426b338082bSbellard {
1427b338082bSbellard     BlockDriverState *bs;
1428b338082bSbellard 
14291b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
14301b7bdbc1SStefan Hajnoczi         if (!strcmp(name, bs->device_name)) {
1431b338082bSbellard             return bs;
1432b338082bSbellard         }
14331b7bdbc1SStefan Hajnoczi     }
1434b338082bSbellard     return NULL;
1435b338082bSbellard }
1436b338082bSbellard 
14372f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs)
14382f399b0aSMarkus Armbruster {
14392f399b0aSMarkus Armbruster     if (!bs) {
14402f399b0aSMarkus Armbruster         return QTAILQ_FIRST(&bdrv_states);
14412f399b0aSMarkus Armbruster     }
14422f399b0aSMarkus Armbruster     return QTAILQ_NEXT(bs, list);
14432f399b0aSMarkus Armbruster }
14442f399b0aSMarkus Armbruster 
144551de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
144681d0912dSbellard {
144781d0912dSbellard     BlockDriverState *bs;
144881d0912dSbellard 
14491b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
145051de9760Saliguori         it(opaque, bs);
145181d0912dSbellard     }
145281d0912dSbellard }
145381d0912dSbellard 
1454ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1455ea2384d3Sbellard {
1456ea2384d3Sbellard     return bs->device_name;
1457ea2384d3Sbellard }
1458ea2384d3Sbellard 
14597a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
14607a6cba61Spbrook {
1461016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
1462016f5cf6SAlexander Graf         return;
1463016f5cf6SAlexander Graf     }
1464016f5cf6SAlexander Graf 
14653f5075aeSChristoph Hellwig     if (bs->drv && bs->drv->bdrv_flush)
14667a6cba61Spbrook         bs->drv->bdrv_flush(bs);
14677a6cba61Spbrook }
14687a6cba61Spbrook 
1469c6ca28d6Saliguori void bdrv_flush_all(void)
1470c6ca28d6Saliguori {
1471c6ca28d6Saliguori     BlockDriverState *bs;
1472c6ca28d6Saliguori 
14731b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1474c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
14751b7bdbc1SStefan Hajnoczi             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1476c6ca28d6Saliguori             bdrv_flush(bs);
1477c6ca28d6Saliguori         }
14781b7bdbc1SStefan Hajnoczi     }
14791b7bdbc1SStefan Hajnoczi }
1480c6ca28d6Saliguori 
1481f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs)
1482f2feebbdSKevin Wolf {
1483f2feebbdSKevin Wolf     assert(bs->drv);
1484f2feebbdSKevin Wolf 
1485336c1c12SKevin Wolf     if (bs->drv->bdrv_has_zero_init) {
1486336c1c12SKevin Wolf         return bs->drv->bdrv_has_zero_init(bs);
1487f2feebbdSKevin Wolf     }
1488f2feebbdSKevin Wolf 
1489f2feebbdSKevin Wolf     return 1;
1490f2feebbdSKevin Wolf }
1491f2feebbdSKevin Wolf 
1492f58c7b35Sths /*
1493f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1494f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1495f58c7b35Sths  * hence all their sectors are reported as allocated.
1496f58c7b35Sths  *
1497f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1498f58c7b35Sths  * the specified sector) that are known to be in the same
1499f58c7b35Sths  * allocated/unallocated state.
1500f58c7b35Sths  *
1501f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1502f58c7b35Sths  */
1503f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1504f58c7b35Sths 	int *pnum)
1505f58c7b35Sths {
1506f58c7b35Sths     int64_t n;
1507f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1508f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1509f58c7b35Sths             *pnum = 0;
1510f58c7b35Sths             return 0;
1511f58c7b35Sths         }
1512f58c7b35Sths         n = bs->total_sectors - sector_num;
1513f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1514f58c7b35Sths         return 1;
1515f58c7b35Sths     }
1516f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1517f58c7b35Sths }
1518f58c7b35Sths 
15192582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv,
15202582bfedSLuiz Capitulino                     BlockMonEventAction action, int is_read)
15212582bfedSLuiz Capitulino {
15222582bfedSLuiz Capitulino     QObject *data;
15232582bfedSLuiz Capitulino     const char *action_str;
15242582bfedSLuiz Capitulino 
15252582bfedSLuiz Capitulino     switch (action) {
15262582bfedSLuiz Capitulino     case BDRV_ACTION_REPORT:
15272582bfedSLuiz Capitulino         action_str = "report";
15282582bfedSLuiz Capitulino         break;
15292582bfedSLuiz Capitulino     case BDRV_ACTION_IGNORE:
15302582bfedSLuiz Capitulino         action_str = "ignore";
15312582bfedSLuiz Capitulino         break;
15322582bfedSLuiz Capitulino     case BDRV_ACTION_STOP:
15332582bfedSLuiz Capitulino         action_str = "stop";
15342582bfedSLuiz Capitulino         break;
15352582bfedSLuiz Capitulino     default:
15362582bfedSLuiz Capitulino         abort();
15372582bfedSLuiz Capitulino     }
15382582bfedSLuiz Capitulino 
15392582bfedSLuiz Capitulino     data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
15402582bfedSLuiz Capitulino                               bdrv->device_name,
15412582bfedSLuiz Capitulino                               action_str,
15422582bfedSLuiz Capitulino                               is_read ? "read" : "write");
15432582bfedSLuiz Capitulino     monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
15442582bfedSLuiz Capitulino 
15452582bfedSLuiz Capitulino     qobject_decref(data);
15462582bfedSLuiz Capitulino }
15472582bfedSLuiz Capitulino 
1548d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque)
1549b338082bSbellard {
1550d15e5465SLuiz Capitulino     QDict *bs_dict;
1551d15e5465SLuiz Capitulino     Monitor *mon = opaque;
1552b338082bSbellard 
1553d15e5465SLuiz Capitulino     bs_dict = qobject_to_qdict(obj);
1554d15e5465SLuiz Capitulino 
1555d15e5465SLuiz Capitulino     monitor_printf(mon, "%s: type=%s removable=%d",
1556d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "device"),
1557d15e5465SLuiz Capitulino                         qdict_get_str(bs_dict, "type"),
1558d15e5465SLuiz Capitulino                         qdict_get_bool(bs_dict, "removable"));
1559d15e5465SLuiz Capitulino 
1560d15e5465SLuiz Capitulino     if (qdict_get_bool(bs_dict, "removable")) {
1561d15e5465SLuiz Capitulino         monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1562b338082bSbellard     }
1563d15e5465SLuiz Capitulino 
1564d15e5465SLuiz Capitulino     if (qdict_haskey(bs_dict, "inserted")) {
1565d15e5465SLuiz Capitulino         QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1566d15e5465SLuiz Capitulino 
1567376253ecSaliguori         monitor_printf(mon, " file=");
1568d15e5465SLuiz Capitulino         monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1569d15e5465SLuiz Capitulino         if (qdict_haskey(qdict, "backing_file")) {
1570376253ecSaliguori             monitor_printf(mon, " backing_file=");
1571d15e5465SLuiz Capitulino             monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1572fef30743Sths         }
1573d15e5465SLuiz Capitulino         monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1574d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "ro"),
1575d15e5465SLuiz Capitulino                             qdict_get_str(qdict, "drv"),
1576d15e5465SLuiz Capitulino                             qdict_get_bool(qdict, "encrypted"));
1577b338082bSbellard     } else {
1578376253ecSaliguori         monitor_printf(mon, " [not inserted]");
1579b338082bSbellard     }
1580d15e5465SLuiz Capitulino 
1581376253ecSaliguori     monitor_printf(mon, "\n");
1582b338082bSbellard }
1583d15e5465SLuiz Capitulino 
1584d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data)
1585d15e5465SLuiz Capitulino {
1586d15e5465SLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1587d15e5465SLuiz Capitulino }
1588d15e5465SLuiz Capitulino 
1589d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data)
1590d15e5465SLuiz Capitulino {
1591d15e5465SLuiz Capitulino     QList *bs_list;
1592d15e5465SLuiz Capitulino     BlockDriverState *bs;
1593d15e5465SLuiz Capitulino 
1594d15e5465SLuiz Capitulino     bs_list = qlist_new();
1595d15e5465SLuiz Capitulino 
15961b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1597d15e5465SLuiz Capitulino         QObject *bs_obj;
1598d15e5465SLuiz Capitulino         const char *type = "unknown";
1599d15e5465SLuiz Capitulino 
1600d15e5465SLuiz Capitulino         switch(bs->type) {
1601d15e5465SLuiz Capitulino         case BDRV_TYPE_HD:
1602d15e5465SLuiz Capitulino             type = "hd";
1603d15e5465SLuiz Capitulino             break;
1604d15e5465SLuiz Capitulino         case BDRV_TYPE_CDROM:
1605d15e5465SLuiz Capitulino             type = "cdrom";
1606d15e5465SLuiz Capitulino             break;
1607d15e5465SLuiz Capitulino         case BDRV_TYPE_FLOPPY:
1608d15e5465SLuiz Capitulino             type = "floppy";
1609d15e5465SLuiz Capitulino             break;
1610d15e5465SLuiz Capitulino         }
1611d15e5465SLuiz Capitulino 
1612d15e5465SLuiz Capitulino         bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1613d15e5465SLuiz Capitulino                                     "'removable': %i, 'locked': %i }",
1614d15e5465SLuiz Capitulino                                     bs->device_name, type, bs->removable,
1615d15e5465SLuiz Capitulino                                     bs->locked);
1616d15e5465SLuiz Capitulino 
1617d15e5465SLuiz Capitulino         if (bs->drv) {
1618d15e5465SLuiz Capitulino             QObject *obj;
1619d15e5465SLuiz Capitulino             QDict *bs_dict = qobject_to_qdict(bs_obj);
1620d15e5465SLuiz Capitulino 
1621d15e5465SLuiz Capitulino             obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1622d15e5465SLuiz Capitulino                                      "'encrypted': %i }",
1623d15e5465SLuiz Capitulino                                      bs->filename, bs->read_only,
1624d15e5465SLuiz Capitulino                                      bs->drv->format_name,
1625d15e5465SLuiz Capitulino                                      bdrv_is_encrypted(bs));
1626d15e5465SLuiz Capitulino             if (bs->backing_file[0] != '\0') {
1627d15e5465SLuiz Capitulino                 QDict *qdict = qobject_to_qdict(obj);
1628d15e5465SLuiz Capitulino                 qdict_put(qdict, "backing_file",
1629d15e5465SLuiz Capitulino                           qstring_from_str(bs->backing_file));
1630d15e5465SLuiz Capitulino             }
1631d15e5465SLuiz Capitulino 
1632d15e5465SLuiz Capitulino             qdict_put_obj(bs_dict, "inserted", obj);
1633d15e5465SLuiz Capitulino         }
1634d15e5465SLuiz Capitulino         qlist_append_obj(bs_list, bs_obj);
1635d15e5465SLuiz Capitulino     }
1636d15e5465SLuiz Capitulino 
1637d15e5465SLuiz Capitulino     *ret_data = QOBJECT(bs_list);
1638b338082bSbellard }
1639a36e69ddSths 
1640218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque)
1641a36e69ddSths {
1642218a536aSLuiz Capitulino     QDict *qdict;
1643218a536aSLuiz Capitulino     Monitor *mon = opaque;
1644218a536aSLuiz Capitulino 
1645218a536aSLuiz Capitulino     qdict = qobject_to_qdict(data);
1646218a536aSLuiz Capitulino     monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1647218a536aSLuiz Capitulino 
1648218a536aSLuiz Capitulino     qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1649218a536aSLuiz Capitulino     monitor_printf(mon, " rd_bytes=%" PRId64
1650218a536aSLuiz Capitulino                         " wr_bytes=%" PRId64
1651218a536aSLuiz Capitulino                         " rd_operations=%" PRId64
1652218a536aSLuiz Capitulino                         " wr_operations=%" PRId64
1653218a536aSLuiz Capitulino                         "\n",
1654218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_bytes"),
1655218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_bytes"),
1656218a536aSLuiz Capitulino                         qdict_get_int(qdict, "rd_operations"),
1657218a536aSLuiz Capitulino                         qdict_get_int(qdict, "wr_operations"));
1658218a536aSLuiz Capitulino }
1659218a536aSLuiz Capitulino 
1660218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data)
1661218a536aSLuiz Capitulino {
1662218a536aSLuiz Capitulino     qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1663218a536aSLuiz Capitulino }
1664218a536aSLuiz Capitulino 
1665294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1666294cc35fSKevin Wolf {
1667294cc35fSKevin Wolf     QObject *res;
1668294cc35fSKevin Wolf     QDict *dict;
1669294cc35fSKevin Wolf 
1670294cc35fSKevin Wolf     res = qobject_from_jsonf("{ 'stats': {"
1671294cc35fSKevin Wolf                              "'rd_bytes': %" PRId64 ","
1672294cc35fSKevin Wolf                              "'wr_bytes': %" PRId64 ","
1673294cc35fSKevin Wolf                              "'rd_operations': %" PRId64 ","
1674294cc35fSKevin Wolf                              "'wr_operations': %" PRId64 ","
1675294cc35fSKevin Wolf                              "'wr_highest_offset': %" PRId64
1676294cc35fSKevin Wolf                              "} }",
1677294cc35fSKevin Wolf                              bs->rd_bytes, bs->wr_bytes,
1678294cc35fSKevin Wolf                              bs->rd_ops, bs->wr_ops,
16795ffbbc67SBlue Swirl                              bs->wr_highest_sector *
16805ffbbc67SBlue Swirl                              (uint64_t)BDRV_SECTOR_SIZE);
1681294cc35fSKevin Wolf     dict  = qobject_to_qdict(res);
1682294cc35fSKevin Wolf 
1683294cc35fSKevin Wolf     if (*bs->device_name) {
1684294cc35fSKevin Wolf         qdict_put(dict, "device", qstring_from_str(bs->device_name));
1685294cc35fSKevin Wolf     }
1686294cc35fSKevin Wolf 
1687294cc35fSKevin Wolf     if (bs->file) {
1688294cc35fSKevin Wolf         QObject *parent = bdrv_info_stats_bs(bs->file);
1689294cc35fSKevin Wolf         qdict_put_obj(dict, "parent", parent);
1690294cc35fSKevin Wolf     }
1691294cc35fSKevin Wolf 
1692294cc35fSKevin Wolf     return res;
1693294cc35fSKevin Wolf }
1694294cc35fSKevin Wolf 
1695218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1696218a536aSLuiz Capitulino {
1697218a536aSLuiz Capitulino     QObject *obj;
1698218a536aSLuiz Capitulino     QList *devices;
1699a36e69ddSths     BlockDriverState *bs;
1700a36e69ddSths 
1701218a536aSLuiz Capitulino     devices = qlist_new();
1702218a536aSLuiz Capitulino 
17031b7bdbc1SStefan Hajnoczi     QTAILQ_FOREACH(bs, &bdrv_states, list) {
1704294cc35fSKevin Wolf         obj = bdrv_info_stats_bs(bs);
1705218a536aSLuiz Capitulino         qlist_append_obj(devices, obj);
1706a36e69ddSths     }
1707218a536aSLuiz Capitulino 
1708218a536aSLuiz Capitulino     *ret_data = QOBJECT(devices);
1709a36e69ddSths }
1710ea2384d3Sbellard 
1711045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1712045df330Saliguori {
1713045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1714045df330Saliguori         return bs->backing_file;
1715045df330Saliguori     else if (bs->encrypted)
1716045df330Saliguori         return bs->filename;
1717045df330Saliguori     else
1718045df330Saliguori         return NULL;
1719045df330Saliguori }
1720045df330Saliguori 
172183f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
172283f64091Sbellard                                char *filename, int filename_size)
172383f64091Sbellard {
1724b783e409SKevin Wolf     if (!bs->backing_file) {
172583f64091Sbellard         pstrcpy(filename, filename_size, "");
172683f64091Sbellard     } else {
172783f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
172883f64091Sbellard     }
172983f64091Sbellard }
173083f64091Sbellard 
1731faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1732faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1733faea38e7Sbellard {
1734faea38e7Sbellard     BlockDriver *drv = bs->drv;
1735faea38e7Sbellard     if (!drv)
173619cb3738Sbellard         return -ENOMEDIUM;
1737faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1738faea38e7Sbellard         return -ENOTSUP;
1739fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1740fbb7b4e0SKevin Wolf         return -EIO;
17417cd1e32aSlirans@il.ibm.com 
1742c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
17437cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
17447cd1e32aSlirans@il.ibm.com     }
17457cd1e32aSlirans@il.ibm.com 
1746faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1747faea38e7Sbellard }
1748faea38e7Sbellard 
1749faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1750faea38e7Sbellard {
1751faea38e7Sbellard     BlockDriver *drv = bs->drv;
1752faea38e7Sbellard     if (!drv)
175319cb3738Sbellard         return -ENOMEDIUM;
1754faea38e7Sbellard     if (!drv->bdrv_get_info)
1755faea38e7Sbellard         return -ENOTSUP;
1756faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1757faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1758faea38e7Sbellard }
1759faea38e7Sbellard 
176045566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
176145566e9cSChristoph Hellwig                       int64_t pos, int size)
1762178e08a5Saliguori {
1763178e08a5Saliguori     BlockDriver *drv = bs->drv;
1764178e08a5Saliguori     if (!drv)
1765178e08a5Saliguori         return -ENOMEDIUM;
17667cdb1f6dSMORITA Kazutaka     if (drv->bdrv_save_vmstate)
176745566e9cSChristoph Hellwig         return drv->bdrv_save_vmstate(bs, buf, pos, size);
17687cdb1f6dSMORITA Kazutaka     if (bs->file)
17697cdb1f6dSMORITA Kazutaka         return bdrv_save_vmstate(bs->file, buf, pos, size);
17707cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1771178e08a5Saliguori }
1772178e08a5Saliguori 
177345566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
177445566e9cSChristoph Hellwig                       int64_t pos, int size)
1775178e08a5Saliguori {
1776178e08a5Saliguori     BlockDriver *drv = bs->drv;
1777178e08a5Saliguori     if (!drv)
1778178e08a5Saliguori         return -ENOMEDIUM;
17797cdb1f6dSMORITA Kazutaka     if (drv->bdrv_load_vmstate)
178045566e9cSChristoph Hellwig         return drv->bdrv_load_vmstate(bs, buf, pos, size);
17817cdb1f6dSMORITA Kazutaka     if (bs->file)
17827cdb1f6dSMORITA Kazutaka         return bdrv_load_vmstate(bs->file, buf, pos, size);
17837cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1784178e08a5Saliguori }
1785178e08a5Saliguori 
17868b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
17878b9b0cc2SKevin Wolf {
17888b9b0cc2SKevin Wolf     BlockDriver *drv = bs->drv;
17898b9b0cc2SKevin Wolf 
17908b9b0cc2SKevin Wolf     if (!drv || !drv->bdrv_debug_event) {
17918b9b0cc2SKevin Wolf         return;
17928b9b0cc2SKevin Wolf     }
17938b9b0cc2SKevin Wolf 
17948b9b0cc2SKevin Wolf     return drv->bdrv_debug_event(bs, event);
17958b9b0cc2SKevin Wolf 
17968b9b0cc2SKevin Wolf }
17978b9b0cc2SKevin Wolf 
1798faea38e7Sbellard /**************************************************************/
1799faea38e7Sbellard /* handling of snapshots */
1800faea38e7Sbellard 
1801feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs)
1802feeee5acSMiguel Di Ciurcio Filho {
1803feeee5acSMiguel Di Ciurcio Filho     BlockDriver *drv = bs->drv;
1804feeee5acSMiguel Di Ciurcio Filho     if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1805feeee5acSMiguel Di Ciurcio Filho         return 0;
1806feeee5acSMiguel Di Ciurcio Filho     }
1807feeee5acSMiguel Di Ciurcio Filho 
1808feeee5acSMiguel Di Ciurcio Filho     if (!drv->bdrv_snapshot_create) {
1809feeee5acSMiguel Di Ciurcio Filho         if (bs->file != NULL) {
1810feeee5acSMiguel Di Ciurcio Filho             return bdrv_can_snapshot(bs->file);
1811feeee5acSMiguel Di Ciurcio Filho         }
1812feeee5acSMiguel Di Ciurcio Filho         return 0;
1813feeee5acSMiguel Di Ciurcio Filho     }
1814feeee5acSMiguel Di Ciurcio Filho 
1815feeee5acSMiguel Di Ciurcio Filho     return 1;
1816feeee5acSMiguel Di Ciurcio Filho }
1817feeee5acSMiguel Di Ciurcio Filho 
1818199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs)
1819199630b6SBlue Swirl {
1820199630b6SBlue Swirl     return !!(bs->open_flags & BDRV_O_SNAPSHOT);
1821199630b6SBlue Swirl }
1822199630b6SBlue Swirl 
1823f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void)
1824f9092b10SMarkus Armbruster {
1825f9092b10SMarkus Armbruster     BlockDriverState *bs;
1826f9092b10SMarkus Armbruster 
18273ac906f7SMarkus Armbruster     if (bs_snapshots) {
1828f9092b10SMarkus Armbruster         return bs_snapshots;
18293ac906f7SMarkus Armbruster     }
1830f9092b10SMarkus Armbruster 
1831f9092b10SMarkus Armbruster     bs = NULL;
1832f9092b10SMarkus Armbruster     while ((bs = bdrv_next(bs))) {
1833f9092b10SMarkus Armbruster         if (bdrv_can_snapshot(bs)) {
18343ac906f7SMarkus Armbruster             bs_snapshots = bs;
18353ac906f7SMarkus Armbruster             return bs;
1836f9092b10SMarkus Armbruster         }
1837f9092b10SMarkus Armbruster     }
1838f9092b10SMarkus Armbruster     return NULL;
1839f9092b10SMarkus Armbruster }
1840f9092b10SMarkus Armbruster 
1841faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1842faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1843faea38e7Sbellard {
1844faea38e7Sbellard     BlockDriver *drv = bs->drv;
1845faea38e7Sbellard     if (!drv)
184619cb3738Sbellard         return -ENOMEDIUM;
18477cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_create)
1848faea38e7Sbellard         return drv->bdrv_snapshot_create(bs, sn_info);
18497cdb1f6dSMORITA Kazutaka     if (bs->file)
18507cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_create(bs->file, sn_info);
18517cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1852faea38e7Sbellard }
1853faea38e7Sbellard 
1854faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1855faea38e7Sbellard                        const char *snapshot_id)
1856faea38e7Sbellard {
1857faea38e7Sbellard     BlockDriver *drv = bs->drv;
18587cdb1f6dSMORITA Kazutaka     int ret, open_ret;
18597cdb1f6dSMORITA Kazutaka 
1860faea38e7Sbellard     if (!drv)
186119cb3738Sbellard         return -ENOMEDIUM;
18627cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_goto)
1863faea38e7Sbellard         return drv->bdrv_snapshot_goto(bs, snapshot_id);
18647cdb1f6dSMORITA Kazutaka 
18657cdb1f6dSMORITA Kazutaka     if (bs->file) {
18667cdb1f6dSMORITA Kazutaka         drv->bdrv_close(bs);
18677cdb1f6dSMORITA Kazutaka         ret = bdrv_snapshot_goto(bs->file, snapshot_id);
18687cdb1f6dSMORITA Kazutaka         open_ret = drv->bdrv_open(bs, bs->open_flags);
18697cdb1f6dSMORITA Kazutaka         if (open_ret < 0) {
18707cdb1f6dSMORITA Kazutaka             bdrv_delete(bs->file);
18717cdb1f6dSMORITA Kazutaka             bs->drv = NULL;
18727cdb1f6dSMORITA Kazutaka             return open_ret;
18737cdb1f6dSMORITA Kazutaka         }
18747cdb1f6dSMORITA Kazutaka         return ret;
18757cdb1f6dSMORITA Kazutaka     }
18767cdb1f6dSMORITA Kazutaka 
18777cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1878faea38e7Sbellard }
1879faea38e7Sbellard 
1880faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1881faea38e7Sbellard {
1882faea38e7Sbellard     BlockDriver *drv = bs->drv;
1883faea38e7Sbellard     if (!drv)
188419cb3738Sbellard         return -ENOMEDIUM;
18857cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_delete)
1886faea38e7Sbellard         return drv->bdrv_snapshot_delete(bs, snapshot_id);
18877cdb1f6dSMORITA Kazutaka     if (bs->file)
18887cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_delete(bs->file, snapshot_id);
18897cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1890faea38e7Sbellard }
1891faea38e7Sbellard 
1892faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1893faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1894faea38e7Sbellard {
1895faea38e7Sbellard     BlockDriver *drv = bs->drv;
1896faea38e7Sbellard     if (!drv)
189719cb3738Sbellard         return -ENOMEDIUM;
18987cdb1f6dSMORITA Kazutaka     if (drv->bdrv_snapshot_list)
1899faea38e7Sbellard         return drv->bdrv_snapshot_list(bs, psn_info);
19007cdb1f6dSMORITA Kazutaka     if (bs->file)
19017cdb1f6dSMORITA Kazutaka         return bdrv_snapshot_list(bs->file, psn_info);
19027cdb1f6dSMORITA Kazutaka     return -ENOTSUP;
1903faea38e7Sbellard }
1904faea38e7Sbellard 
1905faea38e7Sbellard #define NB_SUFFIXES 4
1906faea38e7Sbellard 
1907faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1908faea38e7Sbellard {
1909faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1910faea38e7Sbellard     int64_t base;
1911faea38e7Sbellard     int i;
1912faea38e7Sbellard 
1913faea38e7Sbellard     if (size <= 999) {
1914faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1915faea38e7Sbellard     } else {
1916faea38e7Sbellard         base = 1024;
1917faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1918faea38e7Sbellard             if (size < (10 * base)) {
1919faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1920faea38e7Sbellard                          (double)size / base,
1921faea38e7Sbellard                          suffixes[i]);
1922faea38e7Sbellard                 break;
1923faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1924faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1925faea38e7Sbellard                          ((size + (base >> 1)) / base),
1926faea38e7Sbellard                          suffixes[i]);
1927faea38e7Sbellard                 break;
1928faea38e7Sbellard             }
1929faea38e7Sbellard             base = base * 1024;
1930faea38e7Sbellard         }
1931faea38e7Sbellard     }
1932faea38e7Sbellard     return buf;
1933faea38e7Sbellard }
1934faea38e7Sbellard 
1935faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1936faea38e7Sbellard {
1937faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
19383b9f94e1Sbellard #ifdef _WIN32
19393b9f94e1Sbellard     struct tm *ptm;
19403b9f94e1Sbellard #else
1941faea38e7Sbellard     struct tm tm;
19423b9f94e1Sbellard #endif
1943faea38e7Sbellard     time_t ti;
1944faea38e7Sbellard     int64_t secs;
1945faea38e7Sbellard 
1946faea38e7Sbellard     if (!sn) {
1947faea38e7Sbellard         snprintf(buf, buf_size,
1948faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1949faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1950faea38e7Sbellard     } else {
1951faea38e7Sbellard         ti = sn->date_sec;
19523b9f94e1Sbellard #ifdef _WIN32
19533b9f94e1Sbellard         ptm = localtime(&ti);
19543b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
19553b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
19563b9f94e1Sbellard #else
1957faea38e7Sbellard         localtime_r(&ti, &tm);
1958faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1959faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
19603b9f94e1Sbellard #endif
1961faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1962faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1963faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1964faea38e7Sbellard                  (int)(secs / 3600),
1965faea38e7Sbellard                  (int)((secs / 60) % 60),
1966faea38e7Sbellard                  (int)(secs % 60),
1967faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1968faea38e7Sbellard         snprintf(buf, buf_size,
1969faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1970faea38e7Sbellard                  sn->id_str, sn->name,
1971faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1972faea38e7Sbellard                  date_buf,
1973faea38e7Sbellard                  clock_buf);
1974faea38e7Sbellard     }
1975faea38e7Sbellard     return buf;
1976faea38e7Sbellard }
1977faea38e7Sbellard 
197883f64091Sbellard 
1979ea2384d3Sbellard /**************************************************************/
198083f64091Sbellard /* async I/Os */
1981ea2384d3Sbellard 
19823b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1983f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
198483f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
1985ea2384d3Sbellard {
198683f64091Sbellard     BlockDriver *drv = bs->drv;
1987a36e69ddSths     BlockDriverAIOCB *ret;
1988ea2384d3Sbellard 
198919cb3738Sbellard     if (!drv)
1990ce1a14dcSpbrook         return NULL;
199171d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
199271d0770cSaliguori         return NULL;
199383f64091Sbellard 
1994f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1995f141eafeSaliguori                               cb, opaque);
1996a36e69ddSths 
1997a36e69ddSths     if (ret) {
1998a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
19996ea44308SJan Kiszka 	bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2000a36e69ddSths 	bs->rd_ops ++;
2001a36e69ddSths     }
2002a36e69ddSths 
2003a36e69ddSths     return ret;
200483f64091Sbellard }
200583f64091Sbellard 
2006f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2007f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
200883f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
20097674e7bfSbellard {
201083f64091Sbellard     BlockDriver *drv = bs->drv;
2011a36e69ddSths     BlockDriverAIOCB *ret;
201283f64091Sbellard 
201319cb3738Sbellard     if (!drv)
2014ce1a14dcSpbrook         return NULL;
201583f64091Sbellard     if (bs->read_only)
2016ce1a14dcSpbrook         return NULL;
201771d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
201871d0770cSaliguori         return NULL;
201983f64091Sbellard 
2020c6d22830SJan Kiszka     if (bs->dirty_bitmap) {
20217cd1e32aSlirans@il.ibm.com         set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
20227cd1e32aSlirans@il.ibm.com     }
20237cd1e32aSlirans@il.ibm.com 
2024f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
2025f141eafeSaliguori                                cb, opaque);
2026a36e69ddSths 
2027a36e69ddSths     if (ret) {
2028a36e69ddSths         /* Update stats even though technically transfer has not happened. */
20296ea44308SJan Kiszka         bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
2030a36e69ddSths         bs->wr_ops ++;
2031294cc35fSKevin Wolf         if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2032294cc35fSKevin Wolf             bs->wr_highest_sector = sector_num + nb_sectors - 1;
2033294cc35fSKevin Wolf         }
2034a36e69ddSths     }
2035a36e69ddSths 
2036a36e69ddSths     return ret;
203783f64091Sbellard }
203883f64091Sbellard 
203940b4f539SKevin Wolf 
204040b4f539SKevin Wolf typedef struct MultiwriteCB {
204140b4f539SKevin Wolf     int error;
204240b4f539SKevin Wolf     int num_requests;
204340b4f539SKevin Wolf     int num_callbacks;
204440b4f539SKevin Wolf     struct {
204540b4f539SKevin Wolf         BlockDriverCompletionFunc *cb;
204640b4f539SKevin Wolf         void *opaque;
204740b4f539SKevin Wolf         QEMUIOVector *free_qiov;
204840b4f539SKevin Wolf         void *free_buf;
204940b4f539SKevin Wolf     } callbacks[];
205040b4f539SKevin Wolf } MultiwriteCB;
205140b4f539SKevin Wolf 
205240b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb)
205340b4f539SKevin Wolf {
205440b4f539SKevin Wolf     int i;
205540b4f539SKevin Wolf 
205640b4f539SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
205740b4f539SKevin Wolf         mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
20581e1ea48dSStefan Hajnoczi         if (mcb->callbacks[i].free_qiov) {
20591e1ea48dSStefan Hajnoczi             qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
20601e1ea48dSStefan Hajnoczi         }
206140b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_qiov);
2062f8a83245SHerve Poussineau         qemu_vfree(mcb->callbacks[i].free_buf);
206340b4f539SKevin Wolf     }
206440b4f539SKevin Wolf }
206540b4f539SKevin Wolf 
206640b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret)
206740b4f539SKevin Wolf {
206840b4f539SKevin Wolf     MultiwriteCB *mcb = opaque;
206940b4f539SKevin Wolf 
2070cb6d3ca0SKevin Wolf     if (ret < 0 && !mcb->error) {
207140b4f539SKevin Wolf         mcb->error = ret;
207240b4f539SKevin Wolf     }
207340b4f539SKevin Wolf 
207440b4f539SKevin Wolf     mcb->num_requests--;
207540b4f539SKevin Wolf     if (mcb->num_requests == 0) {
207640b4f539SKevin Wolf         multiwrite_user_cb(mcb);
207740b4f539SKevin Wolf         qemu_free(mcb);
207840b4f539SKevin Wolf     }
207940b4f539SKevin Wolf }
208040b4f539SKevin Wolf 
208140b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b)
208240b4f539SKevin Wolf {
208377be4366SChristoph Hellwig     const BlockRequest *req1 = a, *req2 = b;
208477be4366SChristoph Hellwig 
208577be4366SChristoph Hellwig     /*
208677be4366SChristoph Hellwig      * Note that we can't simply subtract req2->sector from req1->sector
208777be4366SChristoph Hellwig      * here as that could overflow the return value.
208877be4366SChristoph Hellwig      */
208977be4366SChristoph Hellwig     if (req1->sector > req2->sector) {
209077be4366SChristoph Hellwig         return 1;
209177be4366SChristoph Hellwig     } else if (req1->sector < req2->sector) {
209277be4366SChristoph Hellwig         return -1;
209377be4366SChristoph Hellwig     } else {
209477be4366SChristoph Hellwig         return 0;
209577be4366SChristoph Hellwig     }
209640b4f539SKevin Wolf }
209740b4f539SKevin Wolf 
209840b4f539SKevin Wolf /*
209940b4f539SKevin Wolf  * Takes a bunch of requests and tries to merge them. Returns the number of
210040b4f539SKevin Wolf  * requests that remain after merging.
210140b4f539SKevin Wolf  */
210240b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
210340b4f539SKevin Wolf     int num_reqs, MultiwriteCB *mcb)
210440b4f539SKevin Wolf {
210540b4f539SKevin Wolf     int i, outidx;
210640b4f539SKevin Wolf 
210740b4f539SKevin Wolf     // Sort requests by start sector
210840b4f539SKevin Wolf     qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
210940b4f539SKevin Wolf 
211040b4f539SKevin Wolf     // Check if adjacent requests touch the same clusters. If so, combine them,
211140b4f539SKevin Wolf     // filling up gaps with zero sectors.
211240b4f539SKevin Wolf     outidx = 0;
211340b4f539SKevin Wolf     for (i = 1; i < num_reqs; i++) {
211440b4f539SKevin Wolf         int merge = 0;
211540b4f539SKevin Wolf         int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
211640b4f539SKevin Wolf 
211740b4f539SKevin Wolf         // This handles the cases that are valid for all block drivers, namely
211840b4f539SKevin Wolf         // exactly sequential writes and overlapping writes.
211940b4f539SKevin Wolf         if (reqs[i].sector <= oldreq_last) {
212040b4f539SKevin Wolf             merge = 1;
212140b4f539SKevin Wolf         }
212240b4f539SKevin Wolf 
212340b4f539SKevin Wolf         // The block driver may decide that it makes sense to combine requests
212440b4f539SKevin Wolf         // even if there is a gap of some sectors between them. In this case,
212540b4f539SKevin Wolf         // the gap is filled with zeros (therefore only applicable for yet
212640b4f539SKevin Wolf         // unused space in format like qcow2).
212740b4f539SKevin Wolf         if (!merge && bs->drv->bdrv_merge_requests) {
212840b4f539SKevin Wolf             merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
212940b4f539SKevin Wolf         }
213040b4f539SKevin Wolf 
2131e2a305fbSChristoph Hellwig         if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2132e2a305fbSChristoph Hellwig             merge = 0;
2133e2a305fbSChristoph Hellwig         }
2134e2a305fbSChristoph Hellwig 
213540b4f539SKevin Wolf         if (merge) {
213640b4f539SKevin Wolf             size_t size;
213740b4f539SKevin Wolf             QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
213840b4f539SKevin Wolf             qemu_iovec_init(qiov,
213940b4f539SKevin Wolf                 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
214040b4f539SKevin Wolf 
214140b4f539SKevin Wolf             // Add the first request to the merged one. If the requests are
214240b4f539SKevin Wolf             // overlapping, drop the last sectors of the first request.
214340b4f539SKevin Wolf             size = (reqs[i].sector - reqs[outidx].sector) << 9;
214440b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
214540b4f539SKevin Wolf 
214640b4f539SKevin Wolf             // We might need to add some zeros between the two requests
214740b4f539SKevin Wolf             if (reqs[i].sector > oldreq_last) {
214840b4f539SKevin Wolf                 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
214940b4f539SKevin Wolf                 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
215040b4f539SKevin Wolf                 memset(buf, 0, zero_bytes);
215140b4f539SKevin Wolf                 qemu_iovec_add(qiov, buf, zero_bytes);
215240b4f539SKevin Wolf                 mcb->callbacks[i].free_buf = buf;
215340b4f539SKevin Wolf             }
215440b4f539SKevin Wolf 
215540b4f539SKevin Wolf             // Add the second request
215640b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
215740b4f539SKevin Wolf 
2158cbf1dff2SKevin Wolf             reqs[outidx].nb_sectors = qiov->size >> 9;
215940b4f539SKevin Wolf             reqs[outidx].qiov = qiov;
216040b4f539SKevin Wolf 
216140b4f539SKevin Wolf             mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
216240b4f539SKevin Wolf         } else {
216340b4f539SKevin Wolf             outidx++;
216440b4f539SKevin Wolf             reqs[outidx].sector     = reqs[i].sector;
216540b4f539SKevin Wolf             reqs[outidx].nb_sectors = reqs[i].nb_sectors;
216640b4f539SKevin Wolf             reqs[outidx].qiov       = reqs[i].qiov;
216740b4f539SKevin Wolf         }
216840b4f539SKevin Wolf     }
216940b4f539SKevin Wolf 
217040b4f539SKevin Wolf     return outidx + 1;
217140b4f539SKevin Wolf }
217240b4f539SKevin Wolf 
217340b4f539SKevin Wolf /*
217440b4f539SKevin Wolf  * Submit multiple AIO write requests at once.
217540b4f539SKevin Wolf  *
217640b4f539SKevin Wolf  * On success, the function returns 0 and all requests in the reqs array have
217740b4f539SKevin Wolf  * been submitted. In error case this function returns -1, and any of the
217840b4f539SKevin Wolf  * requests may or may not be submitted yet. In particular, this means that the
217940b4f539SKevin Wolf  * callback will be called for some of the requests, for others it won't. The
218040b4f539SKevin Wolf  * caller must check the error field of the BlockRequest to wait for the right
218140b4f539SKevin Wolf  * callbacks (if error != 0, no callback will be called).
218240b4f539SKevin Wolf  *
218340b4f539SKevin Wolf  * The implementation may modify the contents of the reqs array, e.g. to merge
218440b4f539SKevin Wolf  * requests. However, the fields opaque and error are left unmodified as they
218540b4f539SKevin Wolf  * are used to signal failure for a single request to the caller.
218640b4f539SKevin Wolf  */
218740b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
218840b4f539SKevin Wolf {
218940b4f539SKevin Wolf     BlockDriverAIOCB *acb;
219040b4f539SKevin Wolf     MultiwriteCB *mcb;
219140b4f539SKevin Wolf     int i;
219240b4f539SKevin Wolf 
219340b4f539SKevin Wolf     if (num_reqs == 0) {
219440b4f539SKevin Wolf         return 0;
219540b4f539SKevin Wolf     }
219640b4f539SKevin Wolf 
219740b4f539SKevin Wolf     // Create MultiwriteCB structure
219840b4f539SKevin Wolf     mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
219940b4f539SKevin Wolf     mcb->num_requests = 0;
220040b4f539SKevin Wolf     mcb->num_callbacks = num_reqs;
220140b4f539SKevin Wolf 
220240b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
220340b4f539SKevin Wolf         mcb->callbacks[i].cb = reqs[i].cb;
220440b4f539SKevin Wolf         mcb->callbacks[i].opaque = reqs[i].opaque;
220540b4f539SKevin Wolf     }
220640b4f539SKevin Wolf 
220740b4f539SKevin Wolf     // Check for mergable requests
220840b4f539SKevin Wolf     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
220940b4f539SKevin Wolf 
2210453f9a16SKevin Wolf     /*
2211453f9a16SKevin Wolf      * Run the aio requests. As soon as one request can't be submitted
2212453f9a16SKevin Wolf      * successfully, fail all requests that are not yet submitted (we must
2213453f9a16SKevin Wolf      * return failure for all requests anyway)
2214453f9a16SKevin Wolf      *
2215453f9a16SKevin Wolf      * num_requests cannot be set to the right value immediately: If
2216453f9a16SKevin Wolf      * bdrv_aio_writev fails for some request, num_requests would be too high
2217453f9a16SKevin Wolf      * and therefore multiwrite_cb() would never recognize the multiwrite
2218453f9a16SKevin Wolf      * request as completed. We also cannot use the loop variable i to set it
2219453f9a16SKevin Wolf      * when the first request fails because the callback may already have been
2220453f9a16SKevin Wolf      * called for previously submitted requests. Thus, num_requests must be
2221453f9a16SKevin Wolf      * incremented for each request that is submitted.
2222453f9a16SKevin Wolf      *
2223453f9a16SKevin Wolf      * The problem that callbacks may be called early also means that we need
2224453f9a16SKevin Wolf      * to take care that num_requests doesn't become 0 before all requests are
2225453f9a16SKevin Wolf      * submitted - multiwrite_cb() would consider the multiwrite request
2226453f9a16SKevin Wolf      * completed. A dummy request that is "completed" by a manual call to
2227453f9a16SKevin Wolf      * multiwrite_cb() takes care of this.
2228453f9a16SKevin Wolf      */
2229453f9a16SKevin Wolf     mcb->num_requests = 1;
2230453f9a16SKevin Wolf 
223140b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
2232453f9a16SKevin Wolf         mcb->num_requests++;
223340b4f539SKevin Wolf         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
223440b4f539SKevin Wolf             reqs[i].nb_sectors, multiwrite_cb, mcb);
223540b4f539SKevin Wolf 
223640b4f539SKevin Wolf         if (acb == NULL) {
223740b4f539SKevin Wolf             // We can only fail the whole thing if no request has been
223840b4f539SKevin Wolf             // submitted yet. Otherwise we'll wait for the submitted AIOs to
223940b4f539SKevin Wolf             // complete and report the error in the callback.
2240453f9a16SKevin Wolf             if (i == 0) {
224140b4f539SKevin Wolf                 goto fail;
224240b4f539SKevin Wolf             } else {
22437eb58a6cSKevin Wolf                 multiwrite_cb(mcb, -EIO);
224440b4f539SKevin Wolf                 break;
224540b4f539SKevin Wolf             }
224640b4f539SKevin Wolf         }
224740b4f539SKevin Wolf     }
224840b4f539SKevin Wolf 
2249453f9a16SKevin Wolf     /* Complete the dummy request */
2250453f9a16SKevin Wolf     multiwrite_cb(mcb, 0);
2251453f9a16SKevin Wolf 
225240b4f539SKevin Wolf     return 0;
225340b4f539SKevin Wolf 
225440b4f539SKevin Wolf fail:
2255453f9a16SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
2256453f9a16SKevin Wolf         reqs[i].error = -EIO;
2257453f9a16SKevin Wolf     }
2258af474591SBruce Rogers     qemu_free(mcb);
225940b4f539SKevin Wolf     return -1;
226040b4f539SKevin Wolf }
226140b4f539SKevin Wolf 
2262b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2263b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2264b2e12bc6SChristoph Hellwig {
2265b2e12bc6SChristoph Hellwig     BlockDriver *drv = bs->drv;
2266b2e12bc6SChristoph Hellwig 
2267016f5cf6SAlexander Graf     if (bs->open_flags & BDRV_O_NO_FLUSH) {
2268016f5cf6SAlexander Graf         return bdrv_aio_noop_em(bs, cb, opaque);
2269016f5cf6SAlexander Graf     }
2270016f5cf6SAlexander Graf 
2271b2e12bc6SChristoph Hellwig     if (!drv)
2272b2e12bc6SChristoph Hellwig         return NULL;
2273b2e12bc6SChristoph Hellwig     return drv->bdrv_aio_flush(bs, cb, opaque);
2274b2e12bc6SChristoph Hellwig }
2275b2e12bc6SChristoph Hellwig 
227683f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
227783f64091Sbellard {
22786bbff9a0Saliguori     acb->pool->cancel(acb);
227983f64091Sbellard }
228083f64091Sbellard 
228183f64091Sbellard 
228283f64091Sbellard /**************************************************************/
228383f64091Sbellard /* async block device emulation */
228483f64091Sbellard 
2285c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync {
2286c16b5a2cSChristoph Hellwig     BlockDriverAIOCB common;
2287c16b5a2cSChristoph Hellwig     QEMUBH *bh;
2288c16b5a2cSChristoph Hellwig     int ret;
2289c16b5a2cSChristoph Hellwig     /* vector translation state */
2290c16b5a2cSChristoph Hellwig     QEMUIOVector *qiov;
2291c16b5a2cSChristoph Hellwig     uint8_t *bounce;
2292c16b5a2cSChristoph Hellwig     int is_write;
2293c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync;
2294c16b5a2cSChristoph Hellwig 
2295c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2296c16b5a2cSChristoph Hellwig {
2297b666d239SKevin Wolf     BlockDriverAIOCBSync *acb =
2298b666d239SKevin Wolf         container_of(blockacb, BlockDriverAIOCBSync, common);
22996a7ad299SDor Laor     qemu_bh_delete(acb->bh);
230036afc451SAvi Kivity     acb->bh = NULL;
2301c16b5a2cSChristoph Hellwig     qemu_aio_release(acb);
2302c16b5a2cSChristoph Hellwig }
2303c16b5a2cSChristoph Hellwig 
2304c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = {
2305c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2306c16b5a2cSChristoph Hellwig     .cancel             = bdrv_aio_cancel_em,
2307c16b5a2cSChristoph Hellwig };
2308c16b5a2cSChristoph Hellwig 
230983f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
2310beac80cdSbellard {
2311ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
2312f141eafeSaliguori 
2313f141eafeSaliguori     if (!acb->is_write)
2314f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2315ceb42de8Saliguori     qemu_vfree(acb->bounce);
2316ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
23176a7ad299SDor Laor     qemu_bh_delete(acb->bh);
231836afc451SAvi Kivity     acb->bh = NULL;
2319ce1a14dcSpbrook     qemu_aio_release(acb);
2320beac80cdSbellard }
2321beac80cdSbellard 
2322f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2323f141eafeSaliguori                                             int64_t sector_num,
2324f141eafeSaliguori                                             QEMUIOVector *qiov,
2325f141eafeSaliguori                                             int nb_sectors,
2326f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
2327f141eafeSaliguori                                             void *opaque,
2328f141eafeSaliguori                                             int is_write)
2329f141eafeSaliguori 
2330ea2384d3Sbellard {
2331ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
233283f64091Sbellard 
2333c16b5a2cSChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2334f141eafeSaliguori     acb->is_write = is_write;
2335f141eafeSaliguori     acb->qiov = qiov;
2336e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
2337f141eafeSaliguori 
2338ce1a14dcSpbrook     if (!acb->bh)
2339ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2340f141eafeSaliguori 
2341f141eafeSaliguori     if (is_write) {
2342f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2343f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2344f141eafeSaliguori     } else {
2345f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2346f141eafeSaliguori     }
2347f141eafeSaliguori 
2348ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
2349f141eafeSaliguori 
2350ce1a14dcSpbrook     return &acb->common;
23517a6cba61Spbrook }
23527a6cba61Spbrook 
2353f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2354f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2355ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
235683f64091Sbellard {
2357f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
235883f64091Sbellard }
235983f64091Sbellard 
2360f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2361f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2362f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
2363f141eafeSaliguori {
2364f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2365f141eafeSaliguori }
2366f141eafeSaliguori 
2367b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2368b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
2369b2e12bc6SChristoph Hellwig {
2370b2e12bc6SChristoph Hellwig     BlockDriverAIOCBSync *acb;
2371b2e12bc6SChristoph Hellwig 
2372b2e12bc6SChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2373b2e12bc6SChristoph Hellwig     acb->is_write = 1; /* don't bounce in the completion hadler */
2374b2e12bc6SChristoph Hellwig     acb->qiov = NULL;
2375b2e12bc6SChristoph Hellwig     acb->bounce = NULL;
2376b2e12bc6SChristoph Hellwig     acb->ret = 0;
2377b2e12bc6SChristoph Hellwig 
2378b2e12bc6SChristoph Hellwig     if (!acb->bh)
2379b2e12bc6SChristoph Hellwig         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2380b2e12bc6SChristoph Hellwig 
2381b2e12bc6SChristoph Hellwig     bdrv_flush(bs);
2382b2e12bc6SChristoph Hellwig     qemu_bh_schedule(acb->bh);
2383b2e12bc6SChristoph Hellwig     return &acb->common;
2384b2e12bc6SChristoph Hellwig }
2385b2e12bc6SChristoph Hellwig 
2386016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs,
2387016f5cf6SAlexander Graf         BlockDriverCompletionFunc *cb, void *opaque)
2388016f5cf6SAlexander Graf {
2389016f5cf6SAlexander Graf     BlockDriverAIOCBSync *acb;
2390016f5cf6SAlexander Graf 
2391016f5cf6SAlexander Graf     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2392016f5cf6SAlexander Graf     acb->is_write = 1; /* don't bounce in the completion handler */
2393016f5cf6SAlexander Graf     acb->qiov = NULL;
2394016f5cf6SAlexander Graf     acb->bounce = NULL;
2395016f5cf6SAlexander Graf     acb->ret = 0;
2396016f5cf6SAlexander Graf 
2397016f5cf6SAlexander Graf     if (!acb->bh) {
2398016f5cf6SAlexander Graf         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2399016f5cf6SAlexander Graf     }
2400016f5cf6SAlexander Graf 
2401016f5cf6SAlexander Graf     qemu_bh_schedule(acb->bh);
2402016f5cf6SAlexander Graf     return &acb->common;
2403016f5cf6SAlexander Graf }
2404016f5cf6SAlexander Graf 
240583f64091Sbellard /**************************************************************/
240683f64091Sbellard /* sync block device emulation */
240783f64091Sbellard 
240883f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
240983f64091Sbellard {
241083f64091Sbellard     *(int *)opaque = ret;
241183f64091Sbellard }
241283f64091Sbellard 
241383f64091Sbellard #define NOT_DONE 0x7fffffff
241483f64091Sbellard 
241583f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
241683f64091Sbellard                         uint8_t *buf, int nb_sectors)
241783f64091Sbellard {
2418ce1a14dcSpbrook     int async_ret;
2419ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2420f141eafeSaliguori     struct iovec iov;
2421f141eafeSaliguori     QEMUIOVector qiov;
242283f64091Sbellard 
242365d6b3d8SKevin Wolf     async_context_push();
242465d6b3d8SKevin Wolf 
242583f64091Sbellard     async_ret = NOT_DONE;
24263f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
2427eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2428f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2429f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
243083f64091Sbellard         bdrv_rw_em_cb, &async_ret);
243165d6b3d8SKevin Wolf     if (acb == NULL) {
243265d6b3d8SKevin Wolf         async_ret = -1;
243365d6b3d8SKevin Wolf         goto fail;
243465d6b3d8SKevin Wolf     }
2435baf35cb9Saliguori 
243683f64091Sbellard     while (async_ret == NOT_DONE) {
243783f64091Sbellard         qemu_aio_wait();
243883f64091Sbellard     }
2439baf35cb9Saliguori 
244065d6b3d8SKevin Wolf 
244165d6b3d8SKevin Wolf fail:
244265d6b3d8SKevin Wolf     async_context_pop();
244383f64091Sbellard     return async_ret;
244483f64091Sbellard }
244583f64091Sbellard 
244683f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
244783f64091Sbellard                          const uint8_t *buf, int nb_sectors)
244883f64091Sbellard {
2449ce1a14dcSpbrook     int async_ret;
2450ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2451f141eafeSaliguori     struct iovec iov;
2452f141eafeSaliguori     QEMUIOVector qiov;
245383f64091Sbellard 
245465d6b3d8SKevin Wolf     async_context_push();
245565d6b3d8SKevin Wolf 
245683f64091Sbellard     async_ret = NOT_DONE;
2457f141eafeSaliguori     iov.iov_base = (void *)buf;
2458eb5a3165SJes Sorensen     iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE;
2459f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
2460f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
246183f64091Sbellard         bdrv_rw_em_cb, &async_ret);
246265d6b3d8SKevin Wolf     if (acb == NULL) {
246365d6b3d8SKevin Wolf         async_ret = -1;
246465d6b3d8SKevin Wolf         goto fail;
246565d6b3d8SKevin Wolf     }
246683f64091Sbellard     while (async_ret == NOT_DONE) {
246783f64091Sbellard         qemu_aio_wait();
246883f64091Sbellard     }
246965d6b3d8SKevin Wolf 
247065d6b3d8SKevin Wolf fail:
247165d6b3d8SKevin Wolf     async_context_pop();
247283f64091Sbellard     return async_ret;
247383f64091Sbellard }
2474ea2384d3Sbellard 
2475ea2384d3Sbellard void bdrv_init(void)
2476ea2384d3Sbellard {
24775efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
2478ea2384d3Sbellard }
2479ce1a14dcSpbrook 
2480eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void)
2481eb852011SMarkus Armbruster {
2482eb852011SMarkus Armbruster     use_bdrv_whitelist = 1;
2483eb852011SMarkus Armbruster     bdrv_init();
2484eb852011SMarkus Armbruster }
2485eb852011SMarkus Armbruster 
2486c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
24876bbff9a0Saliguori                    BlockDriverCompletionFunc *cb, void *opaque)
24886bbff9a0Saliguori {
2489ce1a14dcSpbrook     BlockDriverAIOCB *acb;
2490ce1a14dcSpbrook 
24916bbff9a0Saliguori     if (pool->free_aiocb) {
24926bbff9a0Saliguori         acb = pool->free_aiocb;
24936bbff9a0Saliguori         pool->free_aiocb = acb->next;
2494ce1a14dcSpbrook     } else {
24956bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
24966bbff9a0Saliguori         acb->pool = pool;
2497ce1a14dcSpbrook     }
2498ce1a14dcSpbrook     acb->bs = bs;
2499ce1a14dcSpbrook     acb->cb = cb;
2500ce1a14dcSpbrook     acb->opaque = opaque;
2501ce1a14dcSpbrook     return acb;
2502ce1a14dcSpbrook }
2503ce1a14dcSpbrook 
2504ce1a14dcSpbrook void qemu_aio_release(void *p)
2505ce1a14dcSpbrook {
25066bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
25076bbff9a0Saliguori     AIOPool *pool = acb->pool;
25086bbff9a0Saliguori     acb->next = pool->free_aiocb;
25096bbff9a0Saliguori     pool->free_aiocb = acb;
2510ce1a14dcSpbrook }
251119cb3738Sbellard 
251219cb3738Sbellard /**************************************************************/
251319cb3738Sbellard /* removable device support */
251419cb3738Sbellard 
251519cb3738Sbellard /**
251619cb3738Sbellard  * Return TRUE if the media is present
251719cb3738Sbellard  */
251819cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
251919cb3738Sbellard {
252019cb3738Sbellard     BlockDriver *drv = bs->drv;
252119cb3738Sbellard     int ret;
252219cb3738Sbellard     if (!drv)
252319cb3738Sbellard         return 0;
252419cb3738Sbellard     if (!drv->bdrv_is_inserted)
25254be9762aSMarkus Armbruster         return !bs->tray_open;
252619cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
252719cb3738Sbellard     return ret;
252819cb3738Sbellard }
252919cb3738Sbellard 
253019cb3738Sbellard /**
253119cb3738Sbellard  * Return TRUE if the media changed since the last call to this
253219cb3738Sbellard  * function. It is currently only used for floppy disks
253319cb3738Sbellard  */
253419cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
253519cb3738Sbellard {
253619cb3738Sbellard     BlockDriver *drv = bs->drv;
253719cb3738Sbellard     int ret;
253819cb3738Sbellard 
253919cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
254019cb3738Sbellard         ret = -ENOTSUP;
254119cb3738Sbellard     else
254219cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
254319cb3738Sbellard     if (ret == -ENOTSUP)
254419cb3738Sbellard         ret = bs->media_changed;
254519cb3738Sbellard     bs->media_changed = 0;
254619cb3738Sbellard     return ret;
254719cb3738Sbellard }
254819cb3738Sbellard 
254919cb3738Sbellard /**
255019cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
255119cb3738Sbellard  */
2552aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag)
255319cb3738Sbellard {
255419cb3738Sbellard     BlockDriver *drv = bs->drv;
255519cb3738Sbellard     int ret;
255619cb3738Sbellard 
2557aea2a33cSMark McLoughlin     if (bs->locked) {
2558aea2a33cSMark McLoughlin         return -EBUSY;
2559aea2a33cSMark McLoughlin     }
2560aea2a33cSMark McLoughlin 
256119cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
256219cb3738Sbellard         ret = -ENOTSUP;
256319cb3738Sbellard     } else {
256419cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
256519cb3738Sbellard     }
256619cb3738Sbellard     if (ret == -ENOTSUP) {
2567aea2a33cSMark McLoughlin         ret = 0;
256819cb3738Sbellard     }
25694be9762aSMarkus Armbruster     if (ret >= 0) {
25704be9762aSMarkus Armbruster         bs->tray_open = eject_flag;
25714be9762aSMarkus Armbruster     }
2572aea2a33cSMark McLoughlin 
2573aea2a33cSMark McLoughlin     return ret;
257419cb3738Sbellard }
257519cb3738Sbellard 
257619cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
257719cb3738Sbellard {
257819cb3738Sbellard     return bs->locked;
257919cb3738Sbellard }
258019cb3738Sbellard 
258119cb3738Sbellard /**
258219cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
258319cb3738Sbellard  * to eject it manually).
258419cb3738Sbellard  */
258519cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
258619cb3738Sbellard {
258719cb3738Sbellard     BlockDriver *drv = bs->drv;
258819cb3738Sbellard 
258919cb3738Sbellard     bs->locked = locked;
259019cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
259119cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
259219cb3738Sbellard     }
259319cb3738Sbellard }
2594985a03b0Sths 
2595985a03b0Sths /* needed for generic scsi interface */
2596985a03b0Sths 
2597985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2598985a03b0Sths {
2599985a03b0Sths     BlockDriver *drv = bs->drv;
2600985a03b0Sths 
2601985a03b0Sths     if (drv && drv->bdrv_ioctl)
2602985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
2603985a03b0Sths     return -ENOTSUP;
2604985a03b0Sths }
26057d780669Saliguori 
2606221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2607221f715dSaliguori         unsigned long int req, void *buf,
26087d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
26097d780669Saliguori {
2610221f715dSaliguori     BlockDriver *drv = bs->drv;
26117d780669Saliguori 
2612221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
2613221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2614221f715dSaliguori     return NULL;
26157d780669Saliguori }
2616e268ca52Saliguori 
26177cd1e32aSlirans@il.ibm.com 
26187cd1e32aSlirans@il.ibm.com 
2619e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
2620e268ca52Saliguori {
2621e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2622e268ca52Saliguori }
26237cd1e32aSlirans@il.ibm.com 
26247cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
26257cd1e32aSlirans@il.ibm.com {
26267cd1e32aSlirans@il.ibm.com     int64_t bitmap_size;
2627a55eb92cSJan Kiszka 
2628aaa0eb75SLiran Schour     bs->dirty_count = 0;
26297cd1e32aSlirans@il.ibm.com     if (enable) {
2630c6d22830SJan Kiszka         if (!bs->dirty_bitmap) {
2631c6d22830SJan Kiszka             bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2632c6d22830SJan Kiszka                     BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2633c6d22830SJan Kiszka             bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
26347cd1e32aSlirans@il.ibm.com 
26357cd1e32aSlirans@il.ibm.com             bs->dirty_bitmap = qemu_mallocz(bitmap_size);
26367cd1e32aSlirans@il.ibm.com         }
26377cd1e32aSlirans@il.ibm.com     } else {
2638c6d22830SJan Kiszka         if (bs->dirty_bitmap) {
26397cd1e32aSlirans@il.ibm.com             qemu_free(bs->dirty_bitmap);
2640c6d22830SJan Kiszka             bs->dirty_bitmap = NULL;
26417cd1e32aSlirans@il.ibm.com         }
26427cd1e32aSlirans@il.ibm.com     }
26437cd1e32aSlirans@il.ibm.com }
26447cd1e32aSlirans@il.ibm.com 
26457cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
26467cd1e32aSlirans@il.ibm.com {
26476ea44308SJan Kiszka     int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
26487cd1e32aSlirans@il.ibm.com 
2649c6d22830SJan Kiszka     if (bs->dirty_bitmap &&
2650c6d22830SJan Kiszka         (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2651c6d22830SJan Kiszka         return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2652c6d22830SJan Kiszka             (1 << (chunk % (sizeof(unsigned long) * 8)));
26537cd1e32aSlirans@il.ibm.com     } else {
26547cd1e32aSlirans@il.ibm.com         return 0;
26557cd1e32aSlirans@il.ibm.com     }
26567cd1e32aSlirans@il.ibm.com }
26577cd1e32aSlirans@il.ibm.com 
26587cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
26597cd1e32aSlirans@il.ibm.com                       int nr_sectors)
26607cd1e32aSlirans@il.ibm.com {
26617cd1e32aSlirans@il.ibm.com     set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
26627cd1e32aSlirans@il.ibm.com }
2663aaa0eb75SLiran Schour 
2664aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2665aaa0eb75SLiran Schour {
2666aaa0eb75SLiran Schour     return bs->dirty_count;
2667aaa0eb75SLiran Schour }
2668