xref: /openbmc/qemu/block.c (revision 72cf2d4f0e181d0d3a3122e04129c58a95da713e)
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"
29fc01f7e7Sbellard 
3071e72a19SJuan Quintela #ifdef CONFIG_BSD
317674e7bfSbellard #include <sys/types.h>
327674e7bfSbellard #include <sys/stat.h>
337674e7bfSbellard #include <sys/ioctl.h>
34*72cf2d4fSBlue Swirl #include <sys/queue.h>
35c5e97233Sblueswir1 #ifndef __DragonFly__
367674e7bfSbellard #include <sys/disk.h>
377674e7bfSbellard #endif
38c5e97233Sblueswir1 #endif
397674e7bfSbellard 
4049dc768dSaliguori #ifdef _WIN32
4149dc768dSaliguori #include <windows.h>
4249dc768dSaliguori #endif
4349dc768dSaliguori 
4483f64091Sbellard #define SECTOR_BITS 9
4583f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS)
463b0d4f61Sbellard 
47f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
48f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
49c87c0672Saliguori         BlockDriverCompletionFunc *cb, void *opaque);
50f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
51f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
52ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
53b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
54b2e12bc6SChristoph Hellwig         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 
607ee930d0Sblueswir1 BlockDriverState *bdrv_first;
617ee930d0Sblueswir1 
62ea2384d3Sbellard static BlockDriver *first_drv;
63ea2384d3Sbellard 
6483f64091Sbellard int path_is_absolute(const char *path)
6583f64091Sbellard {
6683f64091Sbellard     const char *p;
6721664424Sbellard #ifdef _WIN32
6821664424Sbellard     /* specific case for names like: "\\.\d:" */
6921664424Sbellard     if (*path == '/' || *path == '\\')
7021664424Sbellard         return 1;
7121664424Sbellard #endif
7283f64091Sbellard     p = strchr(path, ':');
7383f64091Sbellard     if (p)
7483f64091Sbellard         p++;
7583f64091Sbellard     else
7683f64091Sbellard         p = path;
773b9f94e1Sbellard #ifdef _WIN32
783b9f94e1Sbellard     return (*p == '/' || *p == '\\');
793b9f94e1Sbellard #else
803b9f94e1Sbellard     return (*p == '/');
813b9f94e1Sbellard #endif
8283f64091Sbellard }
8383f64091Sbellard 
8483f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
8583f64091Sbellard    path to it by considering it is relative to base_path. URL are
8683f64091Sbellard    supported. */
8783f64091Sbellard void path_combine(char *dest, int dest_size,
8883f64091Sbellard                   const char *base_path,
8983f64091Sbellard                   const char *filename)
9083f64091Sbellard {
9183f64091Sbellard     const char *p, *p1;
9283f64091Sbellard     int len;
9383f64091Sbellard 
9483f64091Sbellard     if (dest_size <= 0)
9583f64091Sbellard         return;
9683f64091Sbellard     if (path_is_absolute(filename)) {
9783f64091Sbellard         pstrcpy(dest, dest_size, filename);
9883f64091Sbellard     } else {
9983f64091Sbellard         p = strchr(base_path, ':');
10083f64091Sbellard         if (p)
10183f64091Sbellard             p++;
10283f64091Sbellard         else
10383f64091Sbellard             p = base_path;
1043b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1053b9f94e1Sbellard #ifdef _WIN32
1063b9f94e1Sbellard         {
1073b9f94e1Sbellard             const char *p2;
1083b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1093b9f94e1Sbellard             if (!p1 || p2 > p1)
1103b9f94e1Sbellard                 p1 = p2;
1113b9f94e1Sbellard         }
1123b9f94e1Sbellard #endif
11383f64091Sbellard         if (p1)
11483f64091Sbellard             p1++;
11583f64091Sbellard         else
11683f64091Sbellard             p1 = base_path;
11783f64091Sbellard         if (p1 > p)
11883f64091Sbellard             p = p1;
11983f64091Sbellard         len = p - base_path;
12083f64091Sbellard         if (len > dest_size - 1)
12183f64091Sbellard             len = dest_size - 1;
12283f64091Sbellard         memcpy(dest, base_path, len);
12383f64091Sbellard         dest[len] = '\0';
12483f64091Sbellard         pstrcat(dest, dest_size, filename);
12583f64091Sbellard     }
12683f64091Sbellard }
12783f64091Sbellard 
1285efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv)
129ea2384d3Sbellard {
130f141eafeSaliguori     if (!bdrv->bdrv_aio_readv) {
13183f64091Sbellard         /* add AIO emulation layer */
132f141eafeSaliguori         bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
133f141eafeSaliguori         bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
134eda578e5Saliguori     } else if (!bdrv->bdrv_read) {
13583f64091Sbellard         /* add synchronous IO emulation layer */
13683f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
13783f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
13883f64091Sbellard     }
139b2e12bc6SChristoph Hellwig 
140b2e12bc6SChristoph Hellwig     if (!bdrv->bdrv_aio_flush)
141b2e12bc6SChristoph Hellwig         bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
142b2e12bc6SChristoph Hellwig 
143ea2384d3Sbellard     bdrv->next = first_drv;
144ea2384d3Sbellard     first_drv = bdrv;
145ea2384d3Sbellard }
146b338082bSbellard 
147b338082bSbellard /* create a new block device (by default it is empty) */
148b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
149fc01f7e7Sbellard {
150b338082bSbellard     BlockDriverState **pbs, *bs;
151b338082bSbellard 
152b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
153b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
154ea2384d3Sbellard     if (device_name[0] != '\0') {
155b338082bSbellard         /* insert at the end */
156b338082bSbellard         pbs = &bdrv_first;
157b338082bSbellard         while (*pbs != NULL)
158b338082bSbellard             pbs = &(*pbs)->next;
159b338082bSbellard         *pbs = bs;
160ea2384d3Sbellard     }
161b338082bSbellard     return bs;
162b338082bSbellard }
163b338082bSbellard 
164ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
165ea2384d3Sbellard {
166ea2384d3Sbellard     BlockDriver *drv1;
167ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
168ea2384d3Sbellard         if (!strcmp(drv1->format_name, format_name))
169ea2384d3Sbellard             return drv1;
170ea2384d3Sbellard     }
171ea2384d3Sbellard     return NULL;
172ea2384d3Sbellard }
173ea2384d3Sbellard 
1740e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename,
1750e7e1989SKevin Wolf     QEMUOptionParameter *options)
176ea2384d3Sbellard {
177ea2384d3Sbellard     if (!drv->bdrv_create)
178ea2384d3Sbellard         return -ENOTSUP;
1790e7e1989SKevin Wolf 
1800e7e1989SKevin Wolf     return drv->bdrv_create(filename, options);
181ea2384d3Sbellard }
182ea2384d3Sbellard 
183d5249393Sbellard #ifdef _WIN32
18495389c86Sbellard void get_tmp_filename(char *filename, int size)
185d5249393Sbellard {
1863b9f94e1Sbellard     char temp_dir[MAX_PATH];
1873b9f94e1Sbellard 
1883b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
1893b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
190d5249393Sbellard }
191d5249393Sbellard #else
19295389c86Sbellard void get_tmp_filename(char *filename, int size)
193ea2384d3Sbellard {
194ea2384d3Sbellard     int fd;
1957ccfb2ebSblueswir1     const char *tmpdir;
196d5249393Sbellard     /* XXX: race condition possible */
1970badc1eeSaurel32     tmpdir = getenv("TMPDIR");
1980badc1eeSaurel32     if (!tmpdir)
1990badc1eeSaurel32         tmpdir = "/tmp";
2000badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
201ea2384d3Sbellard     fd = mkstemp(filename);
202ea2384d3Sbellard     close(fd);
203ea2384d3Sbellard }
204d5249393Sbellard #endif
205ea2384d3Sbellard 
20619cb3738Sbellard #ifdef _WIN32
207f45512feSbellard static int is_windows_drive_prefix(const char *filename)
208f45512feSbellard {
209f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
210f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
211f45512feSbellard             filename[1] == ':');
212f45512feSbellard }
213f45512feSbellard 
214508c7cb3SChristoph Hellwig int is_windows_drive(const char *filename)
21519cb3738Sbellard {
216f45512feSbellard     if (is_windows_drive_prefix(filename) &&
217f45512feSbellard         filename[2] == '\0')
21819cb3738Sbellard         return 1;
21919cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
22019cb3738Sbellard         strstart(filename, "//./", NULL))
22119cb3738Sbellard         return 1;
22219cb3738Sbellard     return 0;
22319cb3738Sbellard }
22419cb3738Sbellard #endif
22519cb3738Sbellard 
22683f64091Sbellard static BlockDriver *find_protocol(const char *filename)
22783f64091Sbellard {
22883f64091Sbellard     BlockDriver *drv1;
22983f64091Sbellard     char protocol[128];
2301cec71e3SAnthony Liguori     int len;
23183f64091Sbellard     const char *p;
23219cb3738Sbellard 
23319cb3738Sbellard #ifdef _WIN32
234f45512feSbellard     if (is_windows_drive(filename) ||
235f45512feSbellard         is_windows_drive_prefix(filename))
2365efa9d5aSAnthony Liguori         return bdrv_find_format("raw");
23719cb3738Sbellard #endif
2381cec71e3SAnthony Liguori     p = strchr(filename, ':');
2391cec71e3SAnthony Liguori     if (!p)
2405efa9d5aSAnthony Liguori         return bdrv_find_format("raw");
2411cec71e3SAnthony Liguori     len = p - filename;
2421cec71e3SAnthony Liguori     if (len > sizeof(protocol) - 1)
2431cec71e3SAnthony Liguori         len = sizeof(protocol) - 1;
2441cec71e3SAnthony Liguori     memcpy(protocol, filename, len);
2451cec71e3SAnthony Liguori     protocol[len] = '\0';
24683f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
24783f64091Sbellard         if (drv1->protocol_name &&
24883f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
24983f64091Sbellard             return drv1;
25083f64091Sbellard     }
25183f64091Sbellard     return NULL;
25283f64091Sbellard }
25383f64091Sbellard 
254f3a5d3f8SChristoph Hellwig /*
255f3a5d3f8SChristoph Hellwig  * Detect host devices. By convention, /dev/cdrom[N] is always
256f3a5d3f8SChristoph Hellwig  * recognized as a host CDROM.
257f3a5d3f8SChristoph Hellwig  */
258f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename)
259f3a5d3f8SChristoph Hellwig {
260508c7cb3SChristoph Hellwig     int score_max = 0, score;
261508c7cb3SChristoph Hellwig     BlockDriver *drv = NULL, *d;
262f3a5d3f8SChristoph Hellwig 
263508c7cb3SChristoph Hellwig     for (d = first_drv; d; d = d->next) {
264508c7cb3SChristoph Hellwig         if (d->bdrv_probe_device) {
265508c7cb3SChristoph Hellwig             score = d->bdrv_probe_device(filename);
266508c7cb3SChristoph Hellwig             if (score > score_max) {
267508c7cb3SChristoph Hellwig                 score_max = score;
268508c7cb3SChristoph Hellwig                 drv = d;
269f3a5d3f8SChristoph Hellwig             }
270508c7cb3SChristoph Hellwig         }
271f3a5d3f8SChristoph Hellwig     }
272f3a5d3f8SChristoph Hellwig 
273508c7cb3SChristoph Hellwig     return drv;
274f3a5d3f8SChristoph Hellwig }
275f3a5d3f8SChristoph Hellwig 
276ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
277ea2384d3Sbellard {
27883f64091Sbellard     int ret, score, score_max;
279ea2384d3Sbellard     BlockDriver *drv1, *drv;
28083f64091Sbellard     uint8_t buf[2048];
28183f64091Sbellard     BlockDriverState *bs;
282ea2384d3Sbellard 
28383f64091Sbellard     drv = find_protocol(filename);
28419cb3738Sbellard     /* no need to test disk image formats for vvfat */
285c833ab73SAnthony Liguori     if (drv && strcmp(drv->format_name, "vvfat") == 0)
28683f64091Sbellard         return drv;
28783f64091Sbellard 
28883f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
28983f64091Sbellard     if (ret < 0)
2907674e7bfSbellard         return NULL;
29183f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
29283f64091Sbellard     bdrv_delete(bs);
293ea2384d3Sbellard     if (ret < 0) {
294ea2384d3Sbellard         return NULL;
295ea2384d3Sbellard     }
296ea2384d3Sbellard 
297ea2384d3Sbellard     score_max = 0;
298ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
29983f64091Sbellard         if (drv1->bdrv_probe) {
300ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
301ea2384d3Sbellard             if (score > score_max) {
302ea2384d3Sbellard                 score_max = score;
303ea2384d3Sbellard                 drv = drv1;
304ea2384d3Sbellard             }
305ea2384d3Sbellard         }
30683f64091Sbellard     }
307ea2384d3Sbellard     return drv;
308ea2384d3Sbellard }
309ea2384d3Sbellard 
31083f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
311b338082bSbellard {
31283f64091Sbellard     BlockDriverState *bs;
31383f64091Sbellard     int ret;
3143b0d4f61Sbellard 
31583f64091Sbellard     bs = bdrv_new("");
31683f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
31783f64091Sbellard     if (ret < 0) {
31883f64091Sbellard         bdrv_delete(bs);
31983f64091Sbellard         return ret;
3203b0d4f61Sbellard     }
32171d0770cSaliguori     bs->growable = 1;
32283f64091Sbellard     *pbs = bs;
32383f64091Sbellard     return 0;
3243b0d4f61Sbellard }
3253b0d4f61Sbellard 
32683f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
32783f64091Sbellard {
32883f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
329ea2384d3Sbellard }
330ea2384d3Sbellard 
33183f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
332ea2384d3Sbellard                BlockDriver *drv)
333ea2384d3Sbellard {
33483f64091Sbellard     int ret, open_flags;
335eb5c851fSths     char tmp_filename[PATH_MAX];
336eb5c851fSths     char backing_filename[PATH_MAX];
337fc01f7e7Sbellard 
3380849bf08Sbellard     bs->read_only = 0;
339ea2384d3Sbellard     bs->is_temporary = 0;
340ea2384d3Sbellard     bs->encrypted = 0;
341c0f4ce77Saliguori     bs->valid_key = 0;
342e268ca52Saliguori     /* buffer_alignment defaulted to 512, drivers can change this value */
343e268ca52Saliguori     bs->buffer_alignment = 512;
34433e3963eSbellard 
34583f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
346ea2384d3Sbellard         BlockDriverState *bs1;
347ea2384d3Sbellard         int64_t total_size;
3487c96d46eSaliguori         int is_protocol = 0;
34991a073a9SKevin Wolf         BlockDriver *bdrv_qcow2;
35091a073a9SKevin Wolf         QEMUOptionParameter *options;
35133e3963eSbellard 
352ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
353ea2384d3Sbellard            instead of opening 'filename' directly */
354ea2384d3Sbellard 
355ea2384d3Sbellard         /* if there is a backing file, use it */
356ea2384d3Sbellard         bs1 = bdrv_new("");
3575eb45639Saliguori         ret = bdrv_open2(bs1, filename, 0, drv);
35851d7c00cSaliguori         if (ret < 0) {
359ea2384d3Sbellard             bdrv_delete(bs1);
36051d7c00cSaliguori             return ret;
361ea2384d3Sbellard         }
36283f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
3637c96d46eSaliguori 
3647c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
3657c96d46eSaliguori             is_protocol = 1;
3667c96d46eSaliguori 
367ea2384d3Sbellard         bdrv_delete(bs1);
368ea2384d3Sbellard 
369ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
3707c96d46eSaliguori 
3717c96d46eSaliguori         /* Real path is meaningless for protocols */
3727c96d46eSaliguori         if (is_protocol)
3737c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
3747c96d46eSaliguori                      "%s", filename);
3757c96d46eSaliguori         else
376a817d936Sbellard             realpath(filename, backing_filename);
3777c96d46eSaliguori 
37891a073a9SKevin Wolf         bdrv_qcow2 = bdrv_find_format("qcow2");
37991a073a9SKevin Wolf         options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
38091a073a9SKevin Wolf 
38191a073a9SKevin Wolf         set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
38291a073a9SKevin Wolf         set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
38391a073a9SKevin Wolf         if (drv) {
38491a073a9SKevin Wolf             set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
38591a073a9SKevin Wolf                 drv->format_name);
38691a073a9SKevin Wolf         }
38791a073a9SKevin Wolf 
38891a073a9SKevin Wolf         ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
38951d7c00cSaliguori         if (ret < 0) {
39051d7c00cSaliguori             return ret;
391ea2384d3Sbellard         }
39291a073a9SKevin Wolf 
393ea2384d3Sbellard         filename = tmp_filename;
39491a073a9SKevin Wolf         drv = bdrv_qcow2;
395ea2384d3Sbellard         bs->is_temporary = 1;
396ea2384d3Sbellard     }
397ea2384d3Sbellard 
398ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
39983f64091Sbellard     if (flags & BDRV_O_FILE) {
40083f64091Sbellard         drv = find_protocol(filename);
40151d7c00cSaliguori     } else if (!drv) {
402f3a5d3f8SChristoph Hellwig         drv = find_hdev_driver(filename);
403f3a5d3f8SChristoph Hellwig         if (!drv) {
404ea2384d3Sbellard             drv = find_image_format(filename);
405ea2384d3Sbellard         }
406f3a5d3f8SChristoph Hellwig     }
40751d7c00cSaliguori     if (!drv) {
40851d7c00cSaliguori         ret = -ENOENT;
40951d7c00cSaliguori         goto unlink_and_fail;
41083f64091Sbellard     }
411ea2384d3Sbellard     bs->drv = drv;
412ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
413e900a7b7SChristoph Hellwig 
414e900a7b7SChristoph Hellwig     /*
415e900a7b7SChristoph Hellwig      * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
416e900a7b7SChristoph Hellwig      * write cache to the guest.  We do need the fdatasync to flush
417e900a7b7SChristoph Hellwig      * out transactions for block allocations, and we maybe have a
418e900a7b7SChristoph Hellwig      * volatile write cache in our backing device to deal with.
419e900a7b7SChristoph Hellwig      */
420e900a7b7SChristoph Hellwig     if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
421e900a7b7SChristoph Hellwig         bs->enable_write_cache = 1;
422e900a7b7SChristoph Hellwig 
42383f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
42483f64091Sbellard        RDONLY as fallback */
42583f64091Sbellard     if (!(flags & BDRV_O_FILE))
4265c6c3a6cSChristoph Hellwig         open_flags = BDRV_O_RDWR |
4275c6c3a6cSChristoph Hellwig 		(flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
42883f64091Sbellard     else
42983f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
4301cec71e3SAnthony Liguori     ret = drv->bdrv_open(bs, filename, open_flags);
431a0a83536Saurel32     if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
4321cec71e3SAnthony Liguori         ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
43383f64091Sbellard         bs->read_only = 1;
43483f64091Sbellard     }
435ea2384d3Sbellard     if (ret < 0) {
436ea2384d3Sbellard         qemu_free(bs->opaque);
4376b21b973Sbellard         bs->opaque = NULL;
4386b21b973Sbellard         bs->drv = NULL;
43951d7c00cSaliguori     unlink_and_fail:
44051d7c00cSaliguori         if (bs->is_temporary)
44151d7c00cSaliguori             unlink(filename);
44283f64091Sbellard         return ret;
443ea2384d3Sbellard     }
444d15a771dSbellard     if (drv->bdrv_getlength) {
445d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
446d15a771dSbellard     }
447ea2384d3Sbellard #ifndef _WIN32
448ea2384d3Sbellard     if (bs->is_temporary) {
449ea2384d3Sbellard         unlink(filename);
45033e3963eSbellard     }
45167b915a5Sbellard #endif
45283f64091Sbellard     if (bs->backing_file[0] != '\0') {
453ea2384d3Sbellard         /* if there is a backing file, use it */
4545eb45639Saliguori         BlockDriver *back_drv = NULL;
455ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
45683f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
45783f64091Sbellard                      filename, bs->backing_file);
4585eb45639Saliguori         if (bs->backing_format[0] != '\0')
4595eb45639Saliguori             back_drv = bdrv_find_format(bs->backing_format);
4605eb45639Saliguori         ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
4615eb45639Saliguori                          back_drv);
46251d7c00cSaliguori         if (ret < 0) {
46351d7c00cSaliguori             bdrv_close(bs);
46451d7c00cSaliguori             return ret;
46551d7c00cSaliguori         }
466ea2384d3Sbellard     }
46733e3963eSbellard 
468bb5fc20fSaliguori     if (!bdrv_key_required(bs)) {
469b338082bSbellard         /* call the change callback */
47019cb3738Sbellard         bs->media_changed = 1;
471b338082bSbellard         if (bs->change_cb)
472b338082bSbellard             bs->change_cb(bs->change_opaque);
473bb5fc20fSaliguori     }
474b338082bSbellard     return 0;
475fc01f7e7Sbellard }
476fc01f7e7Sbellard 
477fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
478fc01f7e7Sbellard {
47919cb3738Sbellard     if (bs->drv) {
480ea2384d3Sbellard         if (bs->backing_hd)
481ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
482ea2384d3Sbellard         bs->drv->bdrv_close(bs);
483ea2384d3Sbellard         qemu_free(bs->opaque);
484ea2384d3Sbellard #ifdef _WIN32
485ea2384d3Sbellard         if (bs->is_temporary) {
486ea2384d3Sbellard             unlink(bs->filename);
487ea2384d3Sbellard         }
48867b915a5Sbellard #endif
489ea2384d3Sbellard         bs->opaque = NULL;
490ea2384d3Sbellard         bs->drv = NULL;
491b338082bSbellard 
492b338082bSbellard         /* call the change callback */
49319cb3738Sbellard         bs->media_changed = 1;
494b338082bSbellard         if (bs->change_cb)
495b338082bSbellard             bs->change_cb(bs->change_opaque);
496b338082bSbellard     }
497b338082bSbellard }
498b338082bSbellard 
499b338082bSbellard void bdrv_delete(BlockDriverState *bs)
500b338082bSbellard {
50134c6f050Saurel32     BlockDriverState **pbs;
50234c6f050Saurel32 
50334c6f050Saurel32     pbs = &bdrv_first;
50434c6f050Saurel32     while (*pbs != bs && *pbs != NULL)
50534c6f050Saurel32         pbs = &(*pbs)->next;
50634c6f050Saurel32     if (*pbs == bs)
50734c6f050Saurel32         *pbs = bs->next;
50834c6f050Saurel32 
509b338082bSbellard     bdrv_close(bs);
510b338082bSbellard     qemu_free(bs);
511fc01f7e7Sbellard }
512fc01f7e7Sbellard 
513e97fc193Saliguori /*
514e97fc193Saliguori  * Run consistency checks on an image
515e97fc193Saliguori  *
516e97fc193Saliguori  * Returns the number of errors or -errno when an internal error occurs
517e97fc193Saliguori  */
518e97fc193Saliguori int bdrv_check(BlockDriverState *bs)
519e97fc193Saliguori {
520e97fc193Saliguori     if (bs->drv->bdrv_check == NULL) {
521e97fc193Saliguori         return -ENOTSUP;
522e97fc193Saliguori     }
523e97fc193Saliguori 
524e97fc193Saliguori     return bs->drv->bdrv_check(bs);
525e97fc193Saliguori }
526e97fc193Saliguori 
52733e3963eSbellard /* commit COW file into the raw image */
52833e3963eSbellard int bdrv_commit(BlockDriverState *bs)
52933e3963eSbellard {
53019cb3738Sbellard     BlockDriver *drv = bs->drv;
53183f64091Sbellard     int64_t i, total_sectors;
532ea2384d3Sbellard     int n, j;
533ea2384d3Sbellard     unsigned char sector[512];
53433e3963eSbellard 
53519cb3738Sbellard     if (!drv)
53619cb3738Sbellard         return -ENOMEDIUM;
53733e3963eSbellard 
53833e3963eSbellard     if (bs->read_only) {
539ea2384d3Sbellard 	return -EACCES;
54033e3963eSbellard     }
54133e3963eSbellard 
542ea2384d3Sbellard     if (!bs->backing_hd) {
543ea2384d3Sbellard 	return -ENOTSUP;
544ea2384d3Sbellard     }
545ea2384d3Sbellard 
54683f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
54783f64091Sbellard     for (i = 0; i < total_sectors;) {
54819cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
549ea2384d3Sbellard             for(j = 0; j < n; j++) {
55033e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
551ea2384d3Sbellard                     return -EIO;
55233e3963eSbellard                 }
55333e3963eSbellard 
554ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
555ea2384d3Sbellard                     return -EIO;
55633e3963eSbellard                 }
557ea2384d3Sbellard                 i++;
558ea2384d3Sbellard 	    }
559ea2384d3Sbellard 	} else {
560ea2384d3Sbellard             i += n;
56133e3963eSbellard         }
56233e3963eSbellard     }
56395389c86Sbellard 
56419cb3738Sbellard     if (drv->bdrv_make_empty)
56519cb3738Sbellard 	return drv->bdrv_make_empty(bs);
56695389c86Sbellard 
56733e3963eSbellard     return 0;
56833e3963eSbellard }
56933e3963eSbellard 
57071d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
57171d0770cSaliguori                                    size_t size)
57271d0770cSaliguori {
57371d0770cSaliguori     int64_t len;
57471d0770cSaliguori 
57571d0770cSaliguori     if (!bdrv_is_inserted(bs))
57671d0770cSaliguori         return -ENOMEDIUM;
57771d0770cSaliguori 
57871d0770cSaliguori     if (bs->growable)
57971d0770cSaliguori         return 0;
58071d0770cSaliguori 
58171d0770cSaliguori     len = bdrv_getlength(bs);
58271d0770cSaliguori 
583fbb7b4e0SKevin Wolf     if (offset < 0)
584fbb7b4e0SKevin Wolf         return -EIO;
585fbb7b4e0SKevin Wolf 
586fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
58771d0770cSaliguori         return -EIO;
58871d0770cSaliguori 
58971d0770cSaliguori     return 0;
59071d0770cSaliguori }
59171d0770cSaliguori 
59271d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
59371d0770cSaliguori                               int nb_sectors)
59471d0770cSaliguori {
595999dec57Saliguori     return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
59671d0770cSaliguori }
59771d0770cSaliguori 
59819cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
599fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
600fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
601fc01f7e7Sbellard {
602ea2384d3Sbellard     BlockDriver *drv = bs->drv;
603fc01f7e7Sbellard 
60419cb3738Sbellard     if (!drv)
60519cb3738Sbellard         return -ENOMEDIUM;
60671d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
60771d0770cSaliguori         return -EIO;
608b338082bSbellard 
60983f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
61083f64091Sbellard }
611fc01f7e7Sbellard 
61219cb3738Sbellard /* Return < 0 if error. Important errors are:
61319cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
61419cb3738Sbellard   -ENOMEDIUM   No media inserted.
61519cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
61619cb3738Sbellard   -EACCES      Trying to write a read-only device
61719cb3738Sbellard */
618fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
619fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
620fc01f7e7Sbellard {
62183f64091Sbellard     BlockDriver *drv = bs->drv;
62219cb3738Sbellard     if (!bs->drv)
62319cb3738Sbellard         return -ENOMEDIUM;
6240849bf08Sbellard     if (bs->read_only)
62519cb3738Sbellard         return -EACCES;
62671d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
62771d0770cSaliguori         return -EIO;
62871d0770cSaliguori 
62983f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
63083f64091Sbellard }
63183f64091Sbellard 
632eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
633eda578e5Saliguori                void *buf, int count1)
63483f64091Sbellard {
63583f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
63683f64091Sbellard     int len, nb_sectors, count;
63783f64091Sbellard     int64_t sector_num;
63883f64091Sbellard 
63983f64091Sbellard     count = count1;
64083f64091Sbellard     /* first read to align to sector start */
64183f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
64283f64091Sbellard     if (len > count)
64383f64091Sbellard         len = count;
64483f64091Sbellard     sector_num = offset >> SECTOR_BITS;
64583f64091Sbellard     if (len > 0) {
64683f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
64783f64091Sbellard             return -EIO;
64883f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
64983f64091Sbellard         count -= len;
65083f64091Sbellard         if (count == 0)
65183f64091Sbellard             return count1;
65283f64091Sbellard         sector_num++;
65383f64091Sbellard         buf += len;
65483f64091Sbellard     }
65583f64091Sbellard 
65683f64091Sbellard     /* read the sectors "in place" */
65783f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
65883f64091Sbellard     if (nb_sectors > 0) {
65983f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
66083f64091Sbellard             return -EIO;
66183f64091Sbellard         sector_num += nb_sectors;
66283f64091Sbellard         len = nb_sectors << SECTOR_BITS;
66383f64091Sbellard         buf += len;
66483f64091Sbellard         count -= len;
66583f64091Sbellard     }
66683f64091Sbellard 
66783f64091Sbellard     /* add data from the last sector */
66883f64091Sbellard     if (count > 0) {
66983f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
67083f64091Sbellard             return -EIO;
67183f64091Sbellard         memcpy(buf, tmp_buf, count);
67283f64091Sbellard     }
67383f64091Sbellard     return count1;
67483f64091Sbellard }
67583f64091Sbellard 
676eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
677eda578e5Saliguori                 const void *buf, int count1)
67883f64091Sbellard {
67983f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
68083f64091Sbellard     int len, nb_sectors, count;
68183f64091Sbellard     int64_t sector_num;
68283f64091Sbellard 
68383f64091Sbellard     count = count1;
68483f64091Sbellard     /* first write to align to sector start */
68583f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
68683f64091Sbellard     if (len > count)
68783f64091Sbellard         len = count;
68883f64091Sbellard     sector_num = offset >> SECTOR_BITS;
68983f64091Sbellard     if (len > 0) {
69083f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
69183f64091Sbellard             return -EIO;
69283f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
69383f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
69483f64091Sbellard             return -EIO;
69583f64091Sbellard         count -= len;
69683f64091Sbellard         if (count == 0)
69783f64091Sbellard             return count1;
69883f64091Sbellard         sector_num++;
69983f64091Sbellard         buf += len;
70083f64091Sbellard     }
70183f64091Sbellard 
70283f64091Sbellard     /* write the sectors "in place" */
70383f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
70483f64091Sbellard     if (nb_sectors > 0) {
70583f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
70683f64091Sbellard             return -EIO;
70783f64091Sbellard         sector_num += nb_sectors;
70883f64091Sbellard         len = nb_sectors << SECTOR_BITS;
70983f64091Sbellard         buf += len;
71083f64091Sbellard         count -= len;
71183f64091Sbellard     }
71283f64091Sbellard 
71383f64091Sbellard     /* add data from the last sector */
71483f64091Sbellard     if (count > 0) {
71583f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
71683f64091Sbellard             return -EIO;
71783f64091Sbellard         memcpy(tmp_buf, buf, count);
71883f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
71983f64091Sbellard             return -EIO;
72083f64091Sbellard     }
72183f64091Sbellard     return count1;
72283f64091Sbellard }
72383f64091Sbellard 
72483f64091Sbellard /**
72583f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
72683f64091Sbellard  */
72783f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
72883f64091Sbellard {
72983f64091Sbellard     BlockDriver *drv = bs->drv;
73083f64091Sbellard     if (!drv)
73119cb3738Sbellard         return -ENOMEDIUM;
73283f64091Sbellard     if (!drv->bdrv_truncate)
73383f64091Sbellard         return -ENOTSUP;
73483f64091Sbellard     return drv->bdrv_truncate(bs, offset);
73583f64091Sbellard }
73683f64091Sbellard 
73783f64091Sbellard /**
73883f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
73983f64091Sbellard  */
74083f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
74183f64091Sbellard {
74283f64091Sbellard     BlockDriver *drv = bs->drv;
74383f64091Sbellard     if (!drv)
74419cb3738Sbellard         return -ENOMEDIUM;
74583f64091Sbellard     if (!drv->bdrv_getlength) {
74683f64091Sbellard         /* legacy mode */
74783f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
74883f64091Sbellard     }
74983f64091Sbellard     return drv->bdrv_getlength(bs);
750fc01f7e7Sbellard }
751fc01f7e7Sbellard 
75219cb3738Sbellard /* return 0 as number of sectors if no device present or error */
75396b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
754fc01f7e7Sbellard {
75519cb3738Sbellard     int64_t length;
75619cb3738Sbellard     length = bdrv_getlength(bs);
75719cb3738Sbellard     if (length < 0)
75819cb3738Sbellard         length = 0;
75919cb3738Sbellard     else
76019cb3738Sbellard         length = length >> SECTOR_BITS;
76119cb3738Sbellard     *nb_sectors_ptr = length;
762fc01f7e7Sbellard }
763cf98951bSbellard 
764f3d54fc4Saliguori struct partition {
765f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
766f3d54fc4Saliguori         uint8_t head;               /* starting head */
767f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
768f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
769f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
770f3d54fc4Saliguori         uint8_t end_head;           /* end head */
771f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
772f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
773f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
774f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
775f3d54fc4Saliguori } __attribute__((packed));
776f3d54fc4Saliguori 
777f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
778f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
779f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
780f3d54fc4Saliguori {
781f3d54fc4Saliguori     uint8_t buf[512];
782f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
783f3d54fc4Saliguori     struct partition *p;
784f3d54fc4Saliguori     uint32_t nr_sects;
785a38131b6Sblueswir1     uint64_t nb_sectors;
786f3d54fc4Saliguori 
787f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
788f3d54fc4Saliguori 
789f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
790f3d54fc4Saliguori     if (ret < 0)
791f3d54fc4Saliguori         return -1;
792f3d54fc4Saliguori     /* test msdos magic */
793f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
794f3d54fc4Saliguori         return -1;
795f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
796f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
797f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
798f3d54fc4Saliguori         if (nr_sects && p->end_head) {
799f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
800f3d54fc4Saliguori                a cylinder boundary */
801f3d54fc4Saliguori             heads = p->end_head + 1;
802f3d54fc4Saliguori             sectors = p->end_sector & 63;
803f3d54fc4Saliguori             if (sectors == 0)
804f3d54fc4Saliguori                 continue;
805f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
806f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
807f3d54fc4Saliguori                 continue;
808f3d54fc4Saliguori             *pheads = heads;
809f3d54fc4Saliguori             *psectors = sectors;
810f3d54fc4Saliguori             *pcylinders = cylinders;
811f3d54fc4Saliguori #if 0
812f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
813f3d54fc4Saliguori                    cylinders, heads, sectors);
814f3d54fc4Saliguori #endif
815f3d54fc4Saliguori             return 0;
816f3d54fc4Saliguori         }
817f3d54fc4Saliguori     }
818f3d54fc4Saliguori     return -1;
819f3d54fc4Saliguori }
820f3d54fc4Saliguori 
821f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
822f3d54fc4Saliguori {
823f3d54fc4Saliguori     int translation, lba_detected = 0;
824f3d54fc4Saliguori     int cylinders, heads, secs;
825a38131b6Sblueswir1     uint64_t nb_sectors;
826f3d54fc4Saliguori 
827f3d54fc4Saliguori     /* if a geometry hint is available, use it */
828f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
829f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
830f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
831f3d54fc4Saliguori     if (cylinders != 0) {
832f3d54fc4Saliguori         *pcyls = cylinders;
833f3d54fc4Saliguori         *pheads = heads;
834f3d54fc4Saliguori         *psecs = secs;
835f3d54fc4Saliguori     } else {
836f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
837f3d54fc4Saliguori             if (heads > 16) {
838f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
839f3d54fc4Saliguori                    translation was active, so the default
840f3d54fc4Saliguori                    hardware geometry is OK */
841f3d54fc4Saliguori                 lba_detected = 1;
842f3d54fc4Saliguori                 goto default_geometry;
843f3d54fc4Saliguori             } else {
844f3d54fc4Saliguori                 *pcyls = cylinders;
845f3d54fc4Saliguori                 *pheads = heads;
846f3d54fc4Saliguori                 *psecs = secs;
847f3d54fc4Saliguori                 /* disable any translation to be in sync with
848f3d54fc4Saliguori                    the logical geometry */
849f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
850f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
851f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
852f3d54fc4Saliguori                 }
853f3d54fc4Saliguori             }
854f3d54fc4Saliguori         } else {
855f3d54fc4Saliguori         default_geometry:
856f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
857f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
858f3d54fc4Saliguori 
859f3d54fc4Saliguori             if (cylinders > 16383)
860f3d54fc4Saliguori                 cylinders = 16383;
861f3d54fc4Saliguori             else if (cylinders < 2)
862f3d54fc4Saliguori                 cylinders = 2;
863f3d54fc4Saliguori             *pcyls = cylinders;
864f3d54fc4Saliguori             *pheads = 16;
865f3d54fc4Saliguori             *psecs = 63;
866f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
867f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
868f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
869f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
870f3d54fc4Saliguori                 } else {
871f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
872f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
873f3d54fc4Saliguori                 }
874f3d54fc4Saliguori             }
875f3d54fc4Saliguori         }
876f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
877f3d54fc4Saliguori     }
878f3d54fc4Saliguori }
879f3d54fc4Saliguori 
880b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
881b338082bSbellard                             int cyls, int heads, int secs)
882b338082bSbellard {
883b338082bSbellard     bs->cyls = cyls;
884b338082bSbellard     bs->heads = heads;
885b338082bSbellard     bs->secs = secs;
886b338082bSbellard }
887b338082bSbellard 
888b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
889b338082bSbellard {
890b338082bSbellard     bs->type = type;
891b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
892b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
893b338082bSbellard }
894b338082bSbellard 
89546d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
89646d4767dSbellard {
89746d4767dSbellard     bs->translation = translation;
89846d4767dSbellard }
89946d4767dSbellard 
900b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
901b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
902b338082bSbellard {
903b338082bSbellard     *pcyls = bs->cyls;
904b338082bSbellard     *pheads = bs->heads;
905b338082bSbellard     *psecs = bs->secs;
906b338082bSbellard }
907b338082bSbellard 
908b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
909b338082bSbellard {
910b338082bSbellard     return bs->type;
911b338082bSbellard }
912b338082bSbellard 
91346d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
91446d4767dSbellard {
91546d4767dSbellard     return bs->translation;
91646d4767dSbellard }
91746d4767dSbellard 
918b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
919b338082bSbellard {
920b338082bSbellard     return bs->removable;
921b338082bSbellard }
922b338082bSbellard 
923b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
924b338082bSbellard {
925b338082bSbellard     return bs->read_only;
926b338082bSbellard }
927b338082bSbellard 
928985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
929985a03b0Sths {
930985a03b0Sths     return bs->sg;
931985a03b0Sths }
932985a03b0Sths 
933e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs)
934e900a7b7SChristoph Hellwig {
935e900a7b7SChristoph Hellwig     return bs->enable_write_cache;
936e900a7b7SChristoph Hellwig }
937e900a7b7SChristoph Hellwig 
93819cb3738Sbellard /* XXX: no longer used */
939b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
940b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
941b338082bSbellard {
942b338082bSbellard     bs->change_cb = change_cb;
943b338082bSbellard     bs->change_opaque = opaque;
944b338082bSbellard }
945b338082bSbellard 
946ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
947ea2384d3Sbellard {
948ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
949ea2384d3Sbellard         return 1;
950ea2384d3Sbellard     return bs->encrypted;
951ea2384d3Sbellard }
952ea2384d3Sbellard 
953c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
954c0f4ce77Saliguori {
955c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
956c0f4ce77Saliguori 
957c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
958c0f4ce77Saliguori         return 1;
959c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
960c0f4ce77Saliguori }
961c0f4ce77Saliguori 
962ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
963ea2384d3Sbellard {
964ea2384d3Sbellard     int ret;
965ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
966ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
967ea2384d3Sbellard         if (ret < 0)
968ea2384d3Sbellard             return ret;
969ea2384d3Sbellard         if (!bs->encrypted)
970ea2384d3Sbellard             return 0;
971ea2384d3Sbellard     }
972ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
973ea2384d3Sbellard         return -1;
974c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
975bb5fc20fSaliguori     if (ret < 0) {
976bb5fc20fSaliguori         bs->valid_key = 0;
977bb5fc20fSaliguori     } else if (!bs->valid_key) {
978bb5fc20fSaliguori         bs->valid_key = 1;
979bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
980bb5fc20fSaliguori         bs->media_changed = 1;
981bb5fc20fSaliguori         if (bs->change_cb)
982bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
983bb5fc20fSaliguori     }
984c0f4ce77Saliguori     return ret;
985ea2384d3Sbellard }
986ea2384d3Sbellard 
987ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
988ea2384d3Sbellard {
98919cb3738Sbellard     if (!bs->drv) {
990ea2384d3Sbellard         buf[0] = '\0';
991ea2384d3Sbellard     } else {
992ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
993ea2384d3Sbellard     }
994ea2384d3Sbellard }
995ea2384d3Sbellard 
996ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
997ea2384d3Sbellard                          void *opaque)
998ea2384d3Sbellard {
999ea2384d3Sbellard     BlockDriver *drv;
1000ea2384d3Sbellard 
1001ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
1002ea2384d3Sbellard         it(opaque, drv->format_name);
1003ea2384d3Sbellard     }
1004ea2384d3Sbellard }
1005ea2384d3Sbellard 
1006b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1007b338082bSbellard {
1008b338082bSbellard     BlockDriverState *bs;
1009b338082bSbellard 
1010b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1011b338082bSbellard         if (!strcmp(name, bs->device_name))
1012b338082bSbellard             return bs;
1013b338082bSbellard     }
1014b338082bSbellard     return NULL;
1015b338082bSbellard }
1016b338082bSbellard 
101751de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
101881d0912dSbellard {
101981d0912dSbellard     BlockDriverState *bs;
102081d0912dSbellard 
102181d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
102251de9760Saliguori         it(opaque, bs);
102381d0912dSbellard     }
102481d0912dSbellard }
102581d0912dSbellard 
1026ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1027ea2384d3Sbellard {
1028ea2384d3Sbellard     return bs->device_name;
1029ea2384d3Sbellard }
1030ea2384d3Sbellard 
10317a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
10327a6cba61Spbrook {
1033081501daSaliguori     if (!bs->drv)
1034081501daSaliguori         return;
10357a6cba61Spbrook     if (bs->drv->bdrv_flush)
10367a6cba61Spbrook         bs->drv->bdrv_flush(bs);
10377a6cba61Spbrook     if (bs->backing_hd)
10387a6cba61Spbrook         bdrv_flush(bs->backing_hd);
10397a6cba61Spbrook }
10407a6cba61Spbrook 
1041c6ca28d6Saliguori void bdrv_flush_all(void)
1042c6ca28d6Saliguori {
1043c6ca28d6Saliguori     BlockDriverState *bs;
1044c6ca28d6Saliguori 
1045c6ca28d6Saliguori     for (bs = bdrv_first; bs != NULL; bs = bs->next)
1046c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
1047c6ca28d6Saliguori             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1048c6ca28d6Saliguori             bdrv_flush(bs);
1049c6ca28d6Saliguori }
1050c6ca28d6Saliguori 
1051f58c7b35Sths /*
1052f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1053f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1054f58c7b35Sths  * hence all their sectors are reported as allocated.
1055f58c7b35Sths  *
1056f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1057f58c7b35Sths  * the specified sector) that are known to be in the same
1058f58c7b35Sths  * allocated/unallocated state.
1059f58c7b35Sths  *
1060f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1061f58c7b35Sths  */
1062f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1063f58c7b35Sths 	int *pnum)
1064f58c7b35Sths {
1065f58c7b35Sths     int64_t n;
1066f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1067f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1068f58c7b35Sths             *pnum = 0;
1069f58c7b35Sths             return 0;
1070f58c7b35Sths         }
1071f58c7b35Sths         n = bs->total_sectors - sector_num;
1072f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1073f58c7b35Sths         return 1;
1074f58c7b35Sths     }
1075f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1076f58c7b35Sths }
1077f58c7b35Sths 
1078376253ecSaliguori void bdrv_info(Monitor *mon)
1079b338082bSbellard {
1080b338082bSbellard     BlockDriverState *bs;
1081b338082bSbellard 
1082b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1083376253ecSaliguori         monitor_printf(mon, "%s:", bs->device_name);
1084376253ecSaliguori         monitor_printf(mon, " type=");
1085b338082bSbellard         switch(bs->type) {
1086b338082bSbellard         case BDRV_TYPE_HD:
1087376253ecSaliguori             monitor_printf(mon, "hd");
1088b338082bSbellard             break;
1089b338082bSbellard         case BDRV_TYPE_CDROM:
1090376253ecSaliguori             monitor_printf(mon, "cdrom");
1091b338082bSbellard             break;
1092b338082bSbellard         case BDRV_TYPE_FLOPPY:
1093376253ecSaliguori             monitor_printf(mon, "floppy");
1094b338082bSbellard             break;
1095b338082bSbellard         }
1096376253ecSaliguori         monitor_printf(mon, " removable=%d", bs->removable);
1097b338082bSbellard         if (bs->removable) {
1098376253ecSaliguori             monitor_printf(mon, " locked=%d", bs->locked);
1099b338082bSbellard         }
110019cb3738Sbellard         if (bs->drv) {
1101376253ecSaliguori             monitor_printf(mon, " file=");
1102376253ecSaliguori             monitor_print_filename(mon, bs->filename);
1103fef30743Sths             if (bs->backing_file[0] != '\0') {
1104376253ecSaliguori                 monitor_printf(mon, " backing_file=");
1105376253ecSaliguori                 monitor_print_filename(mon, bs->backing_file);
1106fef30743Sths             }
1107376253ecSaliguori             monitor_printf(mon, " ro=%d", bs->read_only);
1108376253ecSaliguori             monitor_printf(mon, " drv=%s", bs->drv->format_name);
1109376253ecSaliguori             monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1110b338082bSbellard         } else {
1111376253ecSaliguori             monitor_printf(mon, " [not inserted]");
1112b338082bSbellard         }
1113376253ecSaliguori         monitor_printf(mon, "\n");
1114b338082bSbellard     }
1115b338082bSbellard }
1116a36e69ddSths 
1117a36e69ddSths /* The "info blockstats" command. */
1118376253ecSaliguori void bdrv_info_stats(Monitor *mon)
1119a36e69ddSths {
1120a36e69ddSths     BlockDriverState *bs;
1121a36e69ddSths 
1122a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1123376253ecSaliguori         monitor_printf(mon, "%s:"
1124a36e69ddSths                        " rd_bytes=%" PRIu64
1125a36e69ddSths                        " wr_bytes=%" PRIu64
1126a36e69ddSths                        " rd_operations=%" PRIu64
1127a36e69ddSths                        " wr_operations=%" PRIu64
1128ebf53fcdSaliguori                        "\n",
1129a36e69ddSths                        bs->device_name,
1130a36e69ddSths                        bs->rd_bytes, bs->wr_bytes,
1131a36e69ddSths                        bs->rd_ops, bs->wr_ops);
1132a36e69ddSths     }
1133a36e69ddSths }
1134ea2384d3Sbellard 
1135045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1136045df330Saliguori {
1137045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1138045df330Saliguori         return bs->backing_file;
1139045df330Saliguori     else if (bs->encrypted)
1140045df330Saliguori         return bs->filename;
1141045df330Saliguori     else
1142045df330Saliguori         return NULL;
1143045df330Saliguori }
1144045df330Saliguori 
114583f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
114683f64091Sbellard                                char *filename, int filename_size)
114783f64091Sbellard {
114883f64091Sbellard     if (!bs->backing_hd) {
114983f64091Sbellard         pstrcpy(filename, filename_size, "");
115083f64091Sbellard     } else {
115183f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
115283f64091Sbellard     }
115383f64091Sbellard }
115483f64091Sbellard 
1155faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1156faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1157faea38e7Sbellard {
1158faea38e7Sbellard     BlockDriver *drv = bs->drv;
1159faea38e7Sbellard     if (!drv)
116019cb3738Sbellard         return -ENOMEDIUM;
1161faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1162faea38e7Sbellard         return -ENOTSUP;
1163fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1164fbb7b4e0SKevin Wolf         return -EIO;
1165faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1166faea38e7Sbellard }
1167faea38e7Sbellard 
1168faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1169faea38e7Sbellard {
1170faea38e7Sbellard     BlockDriver *drv = bs->drv;
1171faea38e7Sbellard     if (!drv)
117219cb3738Sbellard         return -ENOMEDIUM;
1173faea38e7Sbellard     if (!drv->bdrv_get_info)
1174faea38e7Sbellard         return -ENOTSUP;
1175faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1176faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1177faea38e7Sbellard }
1178faea38e7Sbellard 
117945566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
118045566e9cSChristoph Hellwig                       int64_t pos, int size)
1181178e08a5Saliguori {
1182178e08a5Saliguori     BlockDriver *drv = bs->drv;
1183178e08a5Saliguori     if (!drv)
1184178e08a5Saliguori         return -ENOMEDIUM;
118545566e9cSChristoph Hellwig     if (!drv->bdrv_save_vmstate)
1186178e08a5Saliguori         return -ENOTSUP;
118745566e9cSChristoph Hellwig     return drv->bdrv_save_vmstate(bs, buf, pos, size);
1188178e08a5Saliguori }
1189178e08a5Saliguori 
119045566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
119145566e9cSChristoph Hellwig                       int64_t pos, int size)
1192178e08a5Saliguori {
1193178e08a5Saliguori     BlockDriver *drv = bs->drv;
1194178e08a5Saliguori     if (!drv)
1195178e08a5Saliguori         return -ENOMEDIUM;
119645566e9cSChristoph Hellwig     if (!drv->bdrv_load_vmstate)
1197178e08a5Saliguori         return -ENOTSUP;
119845566e9cSChristoph Hellwig     return drv->bdrv_load_vmstate(bs, buf, pos, size);
1199178e08a5Saliguori }
1200178e08a5Saliguori 
1201faea38e7Sbellard /**************************************************************/
1202faea38e7Sbellard /* handling of snapshots */
1203faea38e7Sbellard 
1204faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1205faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1206faea38e7Sbellard {
1207faea38e7Sbellard     BlockDriver *drv = bs->drv;
1208faea38e7Sbellard     if (!drv)
120919cb3738Sbellard         return -ENOMEDIUM;
1210faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
1211faea38e7Sbellard         return -ENOTSUP;
1212faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
1213faea38e7Sbellard }
1214faea38e7Sbellard 
1215faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1216faea38e7Sbellard                        const char *snapshot_id)
1217faea38e7Sbellard {
1218faea38e7Sbellard     BlockDriver *drv = bs->drv;
1219faea38e7Sbellard     if (!drv)
122019cb3738Sbellard         return -ENOMEDIUM;
1221faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
1222faea38e7Sbellard         return -ENOTSUP;
1223faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
1224faea38e7Sbellard }
1225faea38e7Sbellard 
1226faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1227faea38e7Sbellard {
1228faea38e7Sbellard     BlockDriver *drv = bs->drv;
1229faea38e7Sbellard     if (!drv)
123019cb3738Sbellard         return -ENOMEDIUM;
1231faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
1232faea38e7Sbellard         return -ENOTSUP;
1233faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1234faea38e7Sbellard }
1235faea38e7Sbellard 
1236faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1237faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1238faea38e7Sbellard {
1239faea38e7Sbellard     BlockDriver *drv = bs->drv;
1240faea38e7Sbellard     if (!drv)
124119cb3738Sbellard         return -ENOMEDIUM;
1242faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1243faea38e7Sbellard         return -ENOTSUP;
1244faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1245faea38e7Sbellard }
1246faea38e7Sbellard 
1247faea38e7Sbellard #define NB_SUFFIXES 4
1248faea38e7Sbellard 
1249faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1250faea38e7Sbellard {
1251faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1252faea38e7Sbellard     int64_t base;
1253faea38e7Sbellard     int i;
1254faea38e7Sbellard 
1255faea38e7Sbellard     if (size <= 999) {
1256faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1257faea38e7Sbellard     } else {
1258faea38e7Sbellard         base = 1024;
1259faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1260faea38e7Sbellard             if (size < (10 * base)) {
1261faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1262faea38e7Sbellard                          (double)size / base,
1263faea38e7Sbellard                          suffixes[i]);
1264faea38e7Sbellard                 break;
1265faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1266faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1267faea38e7Sbellard                          ((size + (base >> 1)) / base),
1268faea38e7Sbellard                          suffixes[i]);
1269faea38e7Sbellard                 break;
1270faea38e7Sbellard             }
1271faea38e7Sbellard             base = base * 1024;
1272faea38e7Sbellard         }
1273faea38e7Sbellard     }
1274faea38e7Sbellard     return buf;
1275faea38e7Sbellard }
1276faea38e7Sbellard 
1277faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1278faea38e7Sbellard {
1279faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
12803b9f94e1Sbellard #ifdef _WIN32
12813b9f94e1Sbellard     struct tm *ptm;
12823b9f94e1Sbellard #else
1283faea38e7Sbellard     struct tm tm;
12843b9f94e1Sbellard #endif
1285faea38e7Sbellard     time_t ti;
1286faea38e7Sbellard     int64_t secs;
1287faea38e7Sbellard 
1288faea38e7Sbellard     if (!sn) {
1289faea38e7Sbellard         snprintf(buf, buf_size,
1290faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1291faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1292faea38e7Sbellard     } else {
1293faea38e7Sbellard         ti = sn->date_sec;
12943b9f94e1Sbellard #ifdef _WIN32
12953b9f94e1Sbellard         ptm = localtime(&ti);
12963b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
12973b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
12983b9f94e1Sbellard #else
1299faea38e7Sbellard         localtime_r(&ti, &tm);
1300faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1301faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
13023b9f94e1Sbellard #endif
1303faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1304faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1305faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1306faea38e7Sbellard                  (int)(secs / 3600),
1307faea38e7Sbellard                  (int)((secs / 60) % 60),
1308faea38e7Sbellard                  (int)(secs % 60),
1309faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1310faea38e7Sbellard         snprintf(buf, buf_size,
1311faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1312faea38e7Sbellard                  sn->id_str, sn->name,
1313faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1314faea38e7Sbellard                  date_buf,
1315faea38e7Sbellard                  clock_buf);
1316faea38e7Sbellard     }
1317faea38e7Sbellard     return buf;
1318faea38e7Sbellard }
1319faea38e7Sbellard 
132083f64091Sbellard 
1321ea2384d3Sbellard /**************************************************************/
132283f64091Sbellard /* async I/Os */
1323ea2384d3Sbellard 
13243b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1325f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
132683f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
1327ea2384d3Sbellard {
132883f64091Sbellard     BlockDriver *drv = bs->drv;
1329a36e69ddSths     BlockDriverAIOCB *ret;
1330ea2384d3Sbellard 
133119cb3738Sbellard     if (!drv)
1332ce1a14dcSpbrook         return NULL;
133371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
133471d0770cSaliguori         return NULL;
133583f64091Sbellard 
1336f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1337f141eafeSaliguori                               cb, opaque);
1338a36e69ddSths 
1339a36e69ddSths     if (ret) {
1340a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1341a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1342a36e69ddSths 	bs->rd_ops ++;
1343a36e69ddSths     }
1344a36e69ddSths 
1345a36e69ddSths     return ret;
134683f64091Sbellard }
134783f64091Sbellard 
1348f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1349f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
135083f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
13517674e7bfSbellard {
135283f64091Sbellard     BlockDriver *drv = bs->drv;
1353a36e69ddSths     BlockDriverAIOCB *ret;
135483f64091Sbellard 
135519cb3738Sbellard     if (!drv)
1356ce1a14dcSpbrook         return NULL;
135783f64091Sbellard     if (bs->read_only)
1358ce1a14dcSpbrook         return NULL;
135971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
136071d0770cSaliguori         return NULL;
136183f64091Sbellard 
1362f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1363f141eafeSaliguori                                cb, opaque);
1364a36e69ddSths 
1365a36e69ddSths     if (ret) {
1366a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1367a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1368a36e69ddSths 	bs->wr_ops ++;
1369a36e69ddSths     }
1370a36e69ddSths 
1371a36e69ddSths     return ret;
137283f64091Sbellard }
137383f64091Sbellard 
137440b4f539SKevin Wolf 
137540b4f539SKevin Wolf typedef struct MultiwriteCB {
137640b4f539SKevin Wolf     int error;
137740b4f539SKevin Wolf     int num_requests;
137840b4f539SKevin Wolf     int num_callbacks;
137940b4f539SKevin Wolf     struct {
138040b4f539SKevin Wolf         BlockDriverCompletionFunc *cb;
138140b4f539SKevin Wolf         void *opaque;
138240b4f539SKevin Wolf         QEMUIOVector *free_qiov;
138340b4f539SKevin Wolf         void *free_buf;
138440b4f539SKevin Wolf     } callbacks[];
138540b4f539SKevin Wolf } MultiwriteCB;
138640b4f539SKevin Wolf 
138740b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb)
138840b4f539SKevin Wolf {
138940b4f539SKevin Wolf     int i;
139040b4f539SKevin Wolf 
139140b4f539SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
139240b4f539SKevin Wolf         mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
139340b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_qiov);
139440b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_buf);
139540b4f539SKevin Wolf     }
139640b4f539SKevin Wolf }
139740b4f539SKevin Wolf 
139840b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret)
139940b4f539SKevin Wolf {
140040b4f539SKevin Wolf     MultiwriteCB *mcb = opaque;
140140b4f539SKevin Wolf 
140240b4f539SKevin Wolf     if (ret < 0) {
140340b4f539SKevin Wolf         mcb->error = ret;
140440b4f539SKevin Wolf         multiwrite_user_cb(mcb);
140540b4f539SKevin Wolf     }
140640b4f539SKevin Wolf 
140740b4f539SKevin Wolf     mcb->num_requests--;
140840b4f539SKevin Wolf     if (mcb->num_requests == 0) {
140940b4f539SKevin Wolf         if (mcb->error == 0) {
141040b4f539SKevin Wolf             multiwrite_user_cb(mcb);
141140b4f539SKevin Wolf         }
141240b4f539SKevin Wolf         qemu_free(mcb);
141340b4f539SKevin Wolf     }
141440b4f539SKevin Wolf }
141540b4f539SKevin Wolf 
141640b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b)
141740b4f539SKevin Wolf {
141840b4f539SKevin Wolf     return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
141940b4f539SKevin Wolf }
142040b4f539SKevin Wolf 
142140b4f539SKevin Wolf /*
142240b4f539SKevin Wolf  * Takes a bunch of requests and tries to merge them. Returns the number of
142340b4f539SKevin Wolf  * requests that remain after merging.
142440b4f539SKevin Wolf  */
142540b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
142640b4f539SKevin Wolf     int num_reqs, MultiwriteCB *mcb)
142740b4f539SKevin Wolf {
142840b4f539SKevin Wolf     int i, outidx;
142940b4f539SKevin Wolf 
143040b4f539SKevin Wolf     // Sort requests by start sector
143140b4f539SKevin Wolf     qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
143240b4f539SKevin Wolf 
143340b4f539SKevin Wolf     // Check if adjacent requests touch the same clusters. If so, combine them,
143440b4f539SKevin Wolf     // filling up gaps with zero sectors.
143540b4f539SKevin Wolf     outidx = 0;
143640b4f539SKevin Wolf     for (i = 1; i < num_reqs; i++) {
143740b4f539SKevin Wolf         int merge = 0;
143840b4f539SKevin Wolf         int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
143940b4f539SKevin Wolf 
144040b4f539SKevin Wolf         // This handles the cases that are valid for all block drivers, namely
144140b4f539SKevin Wolf         // exactly sequential writes and overlapping writes.
144240b4f539SKevin Wolf         if (reqs[i].sector <= oldreq_last) {
144340b4f539SKevin Wolf             merge = 1;
144440b4f539SKevin Wolf         }
144540b4f539SKevin Wolf 
144640b4f539SKevin Wolf         // The block driver may decide that it makes sense to combine requests
144740b4f539SKevin Wolf         // even if there is a gap of some sectors between them. In this case,
144840b4f539SKevin Wolf         // the gap is filled with zeros (therefore only applicable for yet
144940b4f539SKevin Wolf         // unused space in format like qcow2).
145040b4f539SKevin Wolf         if (!merge && bs->drv->bdrv_merge_requests) {
145140b4f539SKevin Wolf             merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
145240b4f539SKevin Wolf         }
145340b4f539SKevin Wolf 
145440b4f539SKevin Wolf         if (merge) {
145540b4f539SKevin Wolf             size_t size;
145640b4f539SKevin Wolf             QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
145740b4f539SKevin Wolf             qemu_iovec_init(qiov,
145840b4f539SKevin Wolf                 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
145940b4f539SKevin Wolf 
146040b4f539SKevin Wolf             // Add the first request to the merged one. If the requests are
146140b4f539SKevin Wolf             // overlapping, drop the last sectors of the first request.
146240b4f539SKevin Wolf             size = (reqs[i].sector - reqs[outidx].sector) << 9;
146340b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
146440b4f539SKevin Wolf 
146540b4f539SKevin Wolf             // We might need to add some zeros between the two requests
146640b4f539SKevin Wolf             if (reqs[i].sector > oldreq_last) {
146740b4f539SKevin Wolf                 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
146840b4f539SKevin Wolf                 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
146940b4f539SKevin Wolf                 memset(buf, 0, zero_bytes);
147040b4f539SKevin Wolf                 qemu_iovec_add(qiov, buf, zero_bytes);
147140b4f539SKevin Wolf                 mcb->callbacks[i].free_buf = buf;
147240b4f539SKevin Wolf             }
147340b4f539SKevin Wolf 
147440b4f539SKevin Wolf             // Add the second request
147540b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
147640b4f539SKevin Wolf 
147740b4f539SKevin Wolf             reqs[outidx].nb_sectors += reqs[i].nb_sectors;
147840b4f539SKevin Wolf             reqs[outidx].qiov = qiov;
147940b4f539SKevin Wolf 
148040b4f539SKevin Wolf             mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
148140b4f539SKevin Wolf         } else {
148240b4f539SKevin Wolf             outidx++;
148340b4f539SKevin Wolf             reqs[outidx].sector     = reqs[i].sector;
148440b4f539SKevin Wolf             reqs[outidx].nb_sectors = reqs[i].nb_sectors;
148540b4f539SKevin Wolf             reqs[outidx].qiov       = reqs[i].qiov;
148640b4f539SKevin Wolf         }
148740b4f539SKevin Wolf     }
148840b4f539SKevin Wolf 
148940b4f539SKevin Wolf     return outidx + 1;
149040b4f539SKevin Wolf }
149140b4f539SKevin Wolf 
149240b4f539SKevin Wolf /*
149340b4f539SKevin Wolf  * Submit multiple AIO write requests at once.
149440b4f539SKevin Wolf  *
149540b4f539SKevin Wolf  * On success, the function returns 0 and all requests in the reqs array have
149640b4f539SKevin Wolf  * been submitted. In error case this function returns -1, and any of the
149740b4f539SKevin Wolf  * requests may or may not be submitted yet. In particular, this means that the
149840b4f539SKevin Wolf  * callback will be called for some of the requests, for others it won't. The
149940b4f539SKevin Wolf  * caller must check the error field of the BlockRequest to wait for the right
150040b4f539SKevin Wolf  * callbacks (if error != 0, no callback will be called).
150140b4f539SKevin Wolf  *
150240b4f539SKevin Wolf  * The implementation may modify the contents of the reqs array, e.g. to merge
150340b4f539SKevin Wolf  * requests. However, the fields opaque and error are left unmodified as they
150440b4f539SKevin Wolf  * are used to signal failure for a single request to the caller.
150540b4f539SKevin Wolf  */
150640b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
150740b4f539SKevin Wolf {
150840b4f539SKevin Wolf     BlockDriverAIOCB *acb;
150940b4f539SKevin Wolf     MultiwriteCB *mcb;
151040b4f539SKevin Wolf     int i;
151140b4f539SKevin Wolf 
151240b4f539SKevin Wolf     if (num_reqs == 0) {
151340b4f539SKevin Wolf         return 0;
151440b4f539SKevin Wolf     }
151540b4f539SKevin Wolf 
151640b4f539SKevin Wolf     // Create MultiwriteCB structure
151740b4f539SKevin Wolf     mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
151840b4f539SKevin Wolf     mcb->num_requests = 0;
151940b4f539SKevin Wolf     mcb->num_callbacks = num_reqs;
152040b4f539SKevin Wolf 
152140b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
152240b4f539SKevin Wolf         mcb->callbacks[i].cb = reqs[i].cb;
152340b4f539SKevin Wolf         mcb->callbacks[i].opaque = reqs[i].opaque;
152440b4f539SKevin Wolf     }
152540b4f539SKevin Wolf 
152640b4f539SKevin Wolf     // Check for mergable requests
152740b4f539SKevin Wolf     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
152840b4f539SKevin Wolf 
152940b4f539SKevin Wolf     // Run the aio requests
153040b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
153140b4f539SKevin Wolf         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
153240b4f539SKevin Wolf             reqs[i].nb_sectors, multiwrite_cb, mcb);
153340b4f539SKevin Wolf 
153440b4f539SKevin Wolf         if (acb == NULL) {
153540b4f539SKevin Wolf             // We can only fail the whole thing if no request has been
153640b4f539SKevin Wolf             // submitted yet. Otherwise we'll wait for the submitted AIOs to
153740b4f539SKevin Wolf             // complete and report the error in the callback.
153840b4f539SKevin Wolf             if (mcb->num_requests == 0) {
153940b4f539SKevin Wolf                 reqs[i].error = EIO;
154040b4f539SKevin Wolf                 goto fail;
154140b4f539SKevin Wolf             } else {
154240b4f539SKevin Wolf                 mcb->error = EIO;
154340b4f539SKevin Wolf                 break;
154440b4f539SKevin Wolf             }
154540b4f539SKevin Wolf         } else {
154640b4f539SKevin Wolf             mcb->num_requests++;
154740b4f539SKevin Wolf         }
154840b4f539SKevin Wolf     }
154940b4f539SKevin Wolf 
155040b4f539SKevin Wolf     return 0;
155140b4f539SKevin Wolf 
155240b4f539SKevin Wolf fail:
155340b4f539SKevin Wolf     free(mcb);
155440b4f539SKevin Wolf     return -1;
155540b4f539SKevin Wolf }
155640b4f539SKevin Wolf 
1557b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1558b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
1559b2e12bc6SChristoph Hellwig {
1560b2e12bc6SChristoph Hellwig     BlockDriver *drv = bs->drv;
1561b2e12bc6SChristoph Hellwig 
1562b2e12bc6SChristoph Hellwig     if (!drv)
1563b2e12bc6SChristoph Hellwig         return NULL;
1564b2e12bc6SChristoph Hellwig 
1565b2e12bc6SChristoph Hellwig     /*
1566b2e12bc6SChristoph Hellwig      * Note that unlike bdrv_flush the driver is reponsible for flushing a
1567b2e12bc6SChristoph Hellwig      * backing image if it exists.
1568b2e12bc6SChristoph Hellwig      */
1569b2e12bc6SChristoph Hellwig     return drv->bdrv_aio_flush(bs, cb, opaque);
1570b2e12bc6SChristoph Hellwig }
1571b2e12bc6SChristoph Hellwig 
157283f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
157383f64091Sbellard {
15746bbff9a0Saliguori     acb->pool->cancel(acb);
157583f64091Sbellard }
157683f64091Sbellard 
157783f64091Sbellard 
157883f64091Sbellard /**************************************************************/
157983f64091Sbellard /* async block device emulation */
158083f64091Sbellard 
1581c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync {
1582c16b5a2cSChristoph Hellwig     BlockDriverAIOCB common;
1583c16b5a2cSChristoph Hellwig     QEMUBH *bh;
1584c16b5a2cSChristoph Hellwig     int ret;
1585c16b5a2cSChristoph Hellwig     /* vector translation state */
1586c16b5a2cSChristoph Hellwig     QEMUIOVector *qiov;
1587c16b5a2cSChristoph Hellwig     uint8_t *bounce;
1588c16b5a2cSChristoph Hellwig     int is_write;
1589c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync;
1590c16b5a2cSChristoph Hellwig 
1591c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1592c16b5a2cSChristoph Hellwig {
1593c16b5a2cSChristoph Hellwig     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
15946a7ad299SDor Laor     qemu_bh_delete(acb->bh);
159536afc451SAvi Kivity     acb->bh = NULL;
1596c16b5a2cSChristoph Hellwig     qemu_aio_release(acb);
1597c16b5a2cSChristoph Hellwig }
1598c16b5a2cSChristoph Hellwig 
1599c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = {
1600c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
1601c16b5a2cSChristoph Hellwig     .cancel             = bdrv_aio_cancel_em,
1602c16b5a2cSChristoph Hellwig };
1603c16b5a2cSChristoph Hellwig 
160483f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1605beac80cdSbellard {
1606ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1607f141eafeSaliguori 
1608f141eafeSaliguori     if (!acb->is_write)
1609f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1610ceb42de8Saliguori     qemu_vfree(acb->bounce);
1611ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
16126a7ad299SDor Laor     qemu_bh_delete(acb->bh);
161336afc451SAvi Kivity     acb->bh = NULL;
1614ce1a14dcSpbrook     qemu_aio_release(acb);
1615beac80cdSbellard }
1616beac80cdSbellard 
1617f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1618f141eafeSaliguori                                             int64_t sector_num,
1619f141eafeSaliguori                                             QEMUIOVector *qiov,
1620f141eafeSaliguori                                             int nb_sectors,
1621f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
1622f141eafeSaliguori                                             void *opaque,
1623f141eafeSaliguori                                             int is_write)
1624f141eafeSaliguori 
1625ea2384d3Sbellard {
1626ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
162783f64091Sbellard 
1628c16b5a2cSChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1629f141eafeSaliguori     acb->is_write = is_write;
1630f141eafeSaliguori     acb->qiov = qiov;
1631e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
1632f141eafeSaliguori 
1633ce1a14dcSpbrook     if (!acb->bh)
1634ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1635f141eafeSaliguori 
1636f141eafeSaliguori     if (is_write) {
1637f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1638f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1639f141eafeSaliguori     } else {
1640f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1641f141eafeSaliguori     }
1642f141eafeSaliguori 
1643ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1644f141eafeSaliguori 
1645ce1a14dcSpbrook     return &acb->common;
16467a6cba61Spbrook }
16477a6cba61Spbrook 
1648f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1649f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1650ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
165183f64091Sbellard {
1652f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
165383f64091Sbellard }
165483f64091Sbellard 
1655f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1656f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1657f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
1658f141eafeSaliguori {
1659f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1660f141eafeSaliguori }
1661f141eafeSaliguori 
1662b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
1663b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
1664b2e12bc6SChristoph Hellwig {
1665b2e12bc6SChristoph Hellwig     BlockDriverAIOCBSync *acb;
1666b2e12bc6SChristoph Hellwig 
1667b2e12bc6SChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1668b2e12bc6SChristoph Hellwig     acb->is_write = 1; /* don't bounce in the completion hadler */
1669b2e12bc6SChristoph Hellwig     acb->qiov = NULL;
1670b2e12bc6SChristoph Hellwig     acb->bounce = NULL;
1671b2e12bc6SChristoph Hellwig     acb->ret = 0;
1672b2e12bc6SChristoph Hellwig 
1673b2e12bc6SChristoph Hellwig     if (!acb->bh)
1674b2e12bc6SChristoph Hellwig         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1675b2e12bc6SChristoph Hellwig 
1676b2e12bc6SChristoph Hellwig     bdrv_flush(bs);
1677b2e12bc6SChristoph Hellwig     qemu_bh_schedule(acb->bh);
1678b2e12bc6SChristoph Hellwig     return &acb->common;
1679b2e12bc6SChristoph Hellwig }
1680b2e12bc6SChristoph Hellwig 
168183f64091Sbellard /**************************************************************/
168283f64091Sbellard /* sync block device emulation */
168383f64091Sbellard 
168483f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
168583f64091Sbellard {
168683f64091Sbellard     *(int *)opaque = ret;
168783f64091Sbellard }
168883f64091Sbellard 
168983f64091Sbellard #define NOT_DONE 0x7fffffff
169083f64091Sbellard 
169183f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
169283f64091Sbellard                         uint8_t *buf, int nb_sectors)
169383f64091Sbellard {
1694ce1a14dcSpbrook     int async_ret;
1695ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1696f141eafeSaliguori     struct iovec iov;
1697f141eafeSaliguori     QEMUIOVector qiov;
169883f64091Sbellard 
169983f64091Sbellard     async_ret = NOT_DONE;
17003f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
1701f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1702f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1703f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
170483f64091Sbellard         bdrv_rw_em_cb, &async_ret);
1705baf35cb9Saliguori     if (acb == NULL)
1706ce1a14dcSpbrook         return -1;
1707baf35cb9Saliguori 
170883f64091Sbellard     while (async_ret == NOT_DONE) {
170983f64091Sbellard         qemu_aio_wait();
171083f64091Sbellard     }
1711baf35cb9Saliguori 
171283f64091Sbellard     return async_ret;
171383f64091Sbellard }
171483f64091Sbellard 
171583f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
171683f64091Sbellard                          const uint8_t *buf, int nb_sectors)
171783f64091Sbellard {
1718ce1a14dcSpbrook     int async_ret;
1719ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1720f141eafeSaliguori     struct iovec iov;
1721f141eafeSaliguori     QEMUIOVector qiov;
172283f64091Sbellard 
172383f64091Sbellard     async_ret = NOT_DONE;
1724f141eafeSaliguori     iov.iov_base = (void *)buf;
1725f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1726f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1727f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
172883f64091Sbellard         bdrv_rw_em_cb, &async_ret);
1729baf35cb9Saliguori     if (acb == NULL)
1730ce1a14dcSpbrook         return -1;
173183f64091Sbellard     while (async_ret == NOT_DONE) {
173283f64091Sbellard         qemu_aio_wait();
173383f64091Sbellard     }
173483f64091Sbellard     return async_ret;
173583f64091Sbellard }
1736ea2384d3Sbellard 
1737ea2384d3Sbellard void bdrv_init(void)
1738ea2384d3Sbellard {
17395efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
1740ea2384d3Sbellard }
1741ce1a14dcSpbrook 
1742c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
17436bbff9a0Saliguori                    BlockDriverCompletionFunc *cb, void *opaque)
17446bbff9a0Saliguori {
1745ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1746ce1a14dcSpbrook 
17476bbff9a0Saliguori     if (pool->free_aiocb) {
17486bbff9a0Saliguori         acb = pool->free_aiocb;
17496bbff9a0Saliguori         pool->free_aiocb = acb->next;
1750ce1a14dcSpbrook     } else {
17516bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
17526bbff9a0Saliguori         acb->pool = pool;
1753ce1a14dcSpbrook     }
1754ce1a14dcSpbrook     acb->bs = bs;
1755ce1a14dcSpbrook     acb->cb = cb;
1756ce1a14dcSpbrook     acb->opaque = opaque;
1757ce1a14dcSpbrook     return acb;
1758ce1a14dcSpbrook }
1759ce1a14dcSpbrook 
1760ce1a14dcSpbrook void qemu_aio_release(void *p)
1761ce1a14dcSpbrook {
17626bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
17636bbff9a0Saliguori     AIOPool *pool = acb->pool;
17646bbff9a0Saliguori     acb->next = pool->free_aiocb;
17656bbff9a0Saliguori     pool->free_aiocb = acb;
1766ce1a14dcSpbrook }
176719cb3738Sbellard 
176819cb3738Sbellard /**************************************************************/
176919cb3738Sbellard /* removable device support */
177019cb3738Sbellard 
177119cb3738Sbellard /**
177219cb3738Sbellard  * Return TRUE if the media is present
177319cb3738Sbellard  */
177419cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
177519cb3738Sbellard {
177619cb3738Sbellard     BlockDriver *drv = bs->drv;
177719cb3738Sbellard     int ret;
177819cb3738Sbellard     if (!drv)
177919cb3738Sbellard         return 0;
178019cb3738Sbellard     if (!drv->bdrv_is_inserted)
178119cb3738Sbellard         return 1;
178219cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
178319cb3738Sbellard     return ret;
178419cb3738Sbellard }
178519cb3738Sbellard 
178619cb3738Sbellard /**
178719cb3738Sbellard  * Return TRUE if the media changed since the last call to this
178819cb3738Sbellard  * function. It is currently only used for floppy disks
178919cb3738Sbellard  */
179019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
179119cb3738Sbellard {
179219cb3738Sbellard     BlockDriver *drv = bs->drv;
179319cb3738Sbellard     int ret;
179419cb3738Sbellard 
179519cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
179619cb3738Sbellard         ret = -ENOTSUP;
179719cb3738Sbellard     else
179819cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
179919cb3738Sbellard     if (ret == -ENOTSUP)
180019cb3738Sbellard         ret = bs->media_changed;
180119cb3738Sbellard     bs->media_changed = 0;
180219cb3738Sbellard     return ret;
180319cb3738Sbellard }
180419cb3738Sbellard 
180519cb3738Sbellard /**
180619cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
180719cb3738Sbellard  */
1808aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag)
180919cb3738Sbellard {
181019cb3738Sbellard     BlockDriver *drv = bs->drv;
181119cb3738Sbellard     int ret;
181219cb3738Sbellard 
1813aea2a33cSMark McLoughlin     if (bs->locked) {
1814aea2a33cSMark McLoughlin         return -EBUSY;
1815aea2a33cSMark McLoughlin     }
1816aea2a33cSMark McLoughlin 
181719cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
181819cb3738Sbellard         ret = -ENOTSUP;
181919cb3738Sbellard     } else {
182019cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
182119cb3738Sbellard     }
182219cb3738Sbellard     if (ret == -ENOTSUP) {
182319cb3738Sbellard         if (eject_flag)
182419cb3738Sbellard             bdrv_close(bs);
1825aea2a33cSMark McLoughlin         ret = 0;
182619cb3738Sbellard     }
1827aea2a33cSMark McLoughlin 
1828aea2a33cSMark McLoughlin     return ret;
182919cb3738Sbellard }
183019cb3738Sbellard 
183119cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
183219cb3738Sbellard {
183319cb3738Sbellard     return bs->locked;
183419cb3738Sbellard }
183519cb3738Sbellard 
183619cb3738Sbellard /**
183719cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
183819cb3738Sbellard  * to eject it manually).
183919cb3738Sbellard  */
184019cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
184119cb3738Sbellard {
184219cb3738Sbellard     BlockDriver *drv = bs->drv;
184319cb3738Sbellard 
184419cb3738Sbellard     bs->locked = locked;
184519cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
184619cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
184719cb3738Sbellard     }
184819cb3738Sbellard }
1849985a03b0Sths 
1850985a03b0Sths /* needed for generic scsi interface */
1851985a03b0Sths 
1852985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1853985a03b0Sths {
1854985a03b0Sths     BlockDriver *drv = bs->drv;
1855985a03b0Sths 
1856985a03b0Sths     if (drv && drv->bdrv_ioctl)
1857985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1858985a03b0Sths     return -ENOTSUP;
1859985a03b0Sths }
18607d780669Saliguori 
1861221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1862221f715dSaliguori         unsigned long int req, void *buf,
18637d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
18647d780669Saliguori {
1865221f715dSaliguori     BlockDriver *drv = bs->drv;
18667d780669Saliguori 
1867221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
1868221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1869221f715dSaliguori     return NULL;
18707d780669Saliguori }
1871e268ca52Saliguori 
1872e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
1873e268ca52Saliguori {
1874e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1875e268ca52Saliguori }
1876