xref: /openbmc/qemu/block.c (revision 59f2689d9082f2f631253c810f73cd22290144a9)
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>
3472cf2d4fSBlue 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 {
334*59f2689dSNaphtali Sprei     int ret, open_flags, try_rw;
335eb5c851fSths     char tmp_filename[PATH_MAX];
336eb5c851fSths     char backing_filename[PATH_MAX];
337fc01f7e7Sbellard 
338ea2384d3Sbellard     bs->is_temporary = 0;
339ea2384d3Sbellard     bs->encrypted = 0;
340c0f4ce77Saliguori     bs->valid_key = 0;
341e268ca52Saliguori     /* buffer_alignment defaulted to 512, drivers can change this value */
342e268ca52Saliguori     bs->buffer_alignment = 512;
34333e3963eSbellard 
34483f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
345ea2384d3Sbellard         BlockDriverState *bs1;
346ea2384d3Sbellard         int64_t total_size;
3477c96d46eSaliguori         int is_protocol = 0;
34891a073a9SKevin Wolf         BlockDriver *bdrv_qcow2;
34991a073a9SKevin Wolf         QEMUOptionParameter *options;
35033e3963eSbellard 
351ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
352ea2384d3Sbellard            instead of opening 'filename' directly */
353ea2384d3Sbellard 
354ea2384d3Sbellard         /* if there is a backing file, use it */
355ea2384d3Sbellard         bs1 = bdrv_new("");
3565eb45639Saliguori         ret = bdrv_open2(bs1, filename, 0, drv);
35751d7c00cSaliguori         if (ret < 0) {
358ea2384d3Sbellard             bdrv_delete(bs1);
35951d7c00cSaliguori             return ret;
360ea2384d3Sbellard         }
36183f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
3627c96d46eSaliguori 
3637c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
3647c96d46eSaliguori             is_protocol = 1;
3657c96d46eSaliguori 
366ea2384d3Sbellard         bdrv_delete(bs1);
367ea2384d3Sbellard 
368ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
3697c96d46eSaliguori 
3707c96d46eSaliguori         /* Real path is meaningless for protocols */
3717c96d46eSaliguori         if (is_protocol)
3727c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
3737c96d46eSaliguori                      "%s", filename);
3747c96d46eSaliguori         else
375a817d936Sbellard             realpath(filename, backing_filename);
3767c96d46eSaliguori 
37791a073a9SKevin Wolf         bdrv_qcow2 = bdrv_find_format("qcow2");
37891a073a9SKevin Wolf         options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
37991a073a9SKevin Wolf 
38091a073a9SKevin Wolf         set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
38191a073a9SKevin Wolf         set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
38291a073a9SKevin Wolf         if (drv) {
38391a073a9SKevin Wolf             set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
38491a073a9SKevin Wolf                 drv->format_name);
38591a073a9SKevin Wolf         }
38691a073a9SKevin Wolf 
38791a073a9SKevin Wolf         ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
38851d7c00cSaliguori         if (ret < 0) {
38951d7c00cSaliguori             return ret;
390ea2384d3Sbellard         }
39191a073a9SKevin Wolf 
392ea2384d3Sbellard         filename = tmp_filename;
39391a073a9SKevin Wolf         drv = bdrv_qcow2;
394ea2384d3Sbellard         bs->is_temporary = 1;
395ea2384d3Sbellard     }
396ea2384d3Sbellard 
397ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
39883f64091Sbellard     if (flags & BDRV_O_FILE) {
39983f64091Sbellard         drv = find_protocol(filename);
40051d7c00cSaliguori     } else if (!drv) {
401f3a5d3f8SChristoph Hellwig         drv = find_hdev_driver(filename);
402f3a5d3f8SChristoph Hellwig         if (!drv) {
403ea2384d3Sbellard             drv = find_image_format(filename);
404ea2384d3Sbellard         }
405f3a5d3f8SChristoph Hellwig     }
40651d7c00cSaliguori     if (!drv) {
40751d7c00cSaliguori         ret = -ENOENT;
40851d7c00cSaliguori         goto unlink_and_fail;
40983f64091Sbellard     }
410ea2384d3Sbellard     bs->drv = drv;
411ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
412e900a7b7SChristoph Hellwig 
413e900a7b7SChristoph Hellwig     /*
414e900a7b7SChristoph Hellwig      * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
415e900a7b7SChristoph Hellwig      * write cache to the guest.  We do need the fdatasync to flush
416e900a7b7SChristoph Hellwig      * out transactions for block allocations, and we maybe have a
417e900a7b7SChristoph Hellwig      * volatile write cache in our backing device to deal with.
418e900a7b7SChristoph Hellwig      */
419e900a7b7SChristoph Hellwig     if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
420e900a7b7SChristoph Hellwig         bs->enable_write_cache = 1;
421e900a7b7SChristoph Hellwig 
42283f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
42383f64091Sbellard        RDONLY as fallback */
424*59f2689dSNaphtali Sprei     try_rw = !bs->read_only || bs->is_temporary;
42583f64091Sbellard     if (!(flags & BDRV_O_FILE))
426*59f2689dSNaphtali Sprei         open_flags = (try_rw ? BDRV_O_RDWR : 0) |
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("");
456*59f2689dSNaphtali Sprei         /* pass on read_only property to the backing_hd */
457*59f2689dSNaphtali Sprei         bs->backing_hd->read_only = bs->read_only;
45883f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
45983f64091Sbellard                      filename, bs->backing_file);
4605eb45639Saliguori         if (bs->backing_format[0] != '\0')
4615eb45639Saliguori             back_drv = bdrv_find_format(bs->backing_format);
4625eb45639Saliguori         ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
4635eb45639Saliguori                          back_drv);
46451d7c00cSaliguori         if (ret < 0) {
46551d7c00cSaliguori             bdrv_close(bs);
46651d7c00cSaliguori             return ret;
46751d7c00cSaliguori         }
468ea2384d3Sbellard     }
46933e3963eSbellard 
470bb5fc20fSaliguori     if (!bdrv_key_required(bs)) {
471b338082bSbellard         /* call the change callback */
47219cb3738Sbellard         bs->media_changed = 1;
473b338082bSbellard         if (bs->change_cb)
474b338082bSbellard             bs->change_cb(bs->change_opaque);
475bb5fc20fSaliguori     }
476b338082bSbellard     return 0;
477fc01f7e7Sbellard }
478fc01f7e7Sbellard 
479fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
480fc01f7e7Sbellard {
48119cb3738Sbellard     if (bs->drv) {
482ea2384d3Sbellard         if (bs->backing_hd)
483ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
484ea2384d3Sbellard         bs->drv->bdrv_close(bs);
485ea2384d3Sbellard         qemu_free(bs->opaque);
486ea2384d3Sbellard #ifdef _WIN32
487ea2384d3Sbellard         if (bs->is_temporary) {
488ea2384d3Sbellard             unlink(bs->filename);
489ea2384d3Sbellard         }
49067b915a5Sbellard #endif
491ea2384d3Sbellard         bs->opaque = NULL;
492ea2384d3Sbellard         bs->drv = NULL;
493b338082bSbellard 
494b338082bSbellard         /* call the change callback */
49519cb3738Sbellard         bs->media_changed = 1;
496b338082bSbellard         if (bs->change_cb)
497b338082bSbellard             bs->change_cb(bs->change_opaque);
498b338082bSbellard     }
499b338082bSbellard }
500b338082bSbellard 
501b338082bSbellard void bdrv_delete(BlockDriverState *bs)
502b338082bSbellard {
50334c6f050Saurel32     BlockDriverState **pbs;
50434c6f050Saurel32 
50534c6f050Saurel32     pbs = &bdrv_first;
50634c6f050Saurel32     while (*pbs != bs && *pbs != NULL)
50734c6f050Saurel32         pbs = &(*pbs)->next;
50834c6f050Saurel32     if (*pbs == bs)
50934c6f050Saurel32         *pbs = bs->next;
51034c6f050Saurel32 
511b338082bSbellard     bdrv_close(bs);
512b338082bSbellard     qemu_free(bs);
513fc01f7e7Sbellard }
514fc01f7e7Sbellard 
515e97fc193Saliguori /*
516e97fc193Saliguori  * Run consistency checks on an image
517e97fc193Saliguori  *
518e97fc193Saliguori  * Returns the number of errors or -errno when an internal error occurs
519e97fc193Saliguori  */
520e97fc193Saliguori int bdrv_check(BlockDriverState *bs)
521e97fc193Saliguori {
522e97fc193Saliguori     if (bs->drv->bdrv_check == NULL) {
523e97fc193Saliguori         return -ENOTSUP;
524e97fc193Saliguori     }
525e97fc193Saliguori 
526e97fc193Saliguori     return bs->drv->bdrv_check(bs);
527e97fc193Saliguori }
528e97fc193Saliguori 
52933e3963eSbellard /* commit COW file into the raw image */
53033e3963eSbellard int bdrv_commit(BlockDriverState *bs)
53133e3963eSbellard {
53219cb3738Sbellard     BlockDriver *drv = bs->drv;
53383f64091Sbellard     int64_t i, total_sectors;
534ea2384d3Sbellard     int n, j;
535ea2384d3Sbellard     unsigned char sector[512];
53633e3963eSbellard 
53719cb3738Sbellard     if (!drv)
53819cb3738Sbellard         return -ENOMEDIUM;
53933e3963eSbellard 
54033e3963eSbellard     if (bs->read_only) {
541ea2384d3Sbellard 	return -EACCES;
54233e3963eSbellard     }
54333e3963eSbellard 
544ea2384d3Sbellard     if (!bs->backing_hd) {
545ea2384d3Sbellard 	return -ENOTSUP;
546ea2384d3Sbellard     }
547ea2384d3Sbellard 
54883f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
54983f64091Sbellard     for (i = 0; i < total_sectors;) {
55019cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
551ea2384d3Sbellard             for(j = 0; j < n; j++) {
55233e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
553ea2384d3Sbellard                     return -EIO;
55433e3963eSbellard                 }
55533e3963eSbellard 
556ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
557ea2384d3Sbellard                     return -EIO;
55833e3963eSbellard                 }
559ea2384d3Sbellard                 i++;
560ea2384d3Sbellard 	    }
561ea2384d3Sbellard 	} else {
562ea2384d3Sbellard             i += n;
56333e3963eSbellard         }
56433e3963eSbellard     }
56595389c86Sbellard 
56619cb3738Sbellard     if (drv->bdrv_make_empty)
56719cb3738Sbellard 	return drv->bdrv_make_empty(bs);
56895389c86Sbellard 
56933e3963eSbellard     return 0;
57033e3963eSbellard }
57133e3963eSbellard 
57271d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
57371d0770cSaliguori                                    size_t size)
57471d0770cSaliguori {
57571d0770cSaliguori     int64_t len;
57671d0770cSaliguori 
57771d0770cSaliguori     if (!bdrv_is_inserted(bs))
57871d0770cSaliguori         return -ENOMEDIUM;
57971d0770cSaliguori 
58071d0770cSaliguori     if (bs->growable)
58171d0770cSaliguori         return 0;
58271d0770cSaliguori 
58371d0770cSaliguori     len = bdrv_getlength(bs);
58471d0770cSaliguori 
585fbb7b4e0SKevin Wolf     if (offset < 0)
586fbb7b4e0SKevin Wolf         return -EIO;
587fbb7b4e0SKevin Wolf 
588fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
58971d0770cSaliguori         return -EIO;
59071d0770cSaliguori 
59171d0770cSaliguori     return 0;
59271d0770cSaliguori }
59371d0770cSaliguori 
59471d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
59571d0770cSaliguori                               int nb_sectors)
59671d0770cSaliguori {
597999dec57Saliguori     return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
59871d0770cSaliguori }
59971d0770cSaliguori 
60019cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
601fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
602fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
603fc01f7e7Sbellard {
604ea2384d3Sbellard     BlockDriver *drv = bs->drv;
605fc01f7e7Sbellard 
60619cb3738Sbellard     if (!drv)
60719cb3738Sbellard         return -ENOMEDIUM;
60871d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
60971d0770cSaliguori         return -EIO;
610b338082bSbellard 
61183f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
61283f64091Sbellard }
613fc01f7e7Sbellard 
61419cb3738Sbellard /* Return < 0 if error. Important errors are:
61519cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
61619cb3738Sbellard   -ENOMEDIUM   No media inserted.
61719cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
61819cb3738Sbellard   -EACCES      Trying to write a read-only device
61919cb3738Sbellard */
620fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
621fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
622fc01f7e7Sbellard {
62383f64091Sbellard     BlockDriver *drv = bs->drv;
62419cb3738Sbellard     if (!bs->drv)
62519cb3738Sbellard         return -ENOMEDIUM;
6260849bf08Sbellard     if (bs->read_only)
62719cb3738Sbellard         return -EACCES;
62871d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
62971d0770cSaliguori         return -EIO;
63071d0770cSaliguori 
63183f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
63283f64091Sbellard }
63383f64091Sbellard 
634eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
635eda578e5Saliguori                void *buf, int count1)
63683f64091Sbellard {
63783f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
63883f64091Sbellard     int len, nb_sectors, count;
63983f64091Sbellard     int64_t sector_num;
64083f64091Sbellard 
64183f64091Sbellard     count = count1;
64283f64091Sbellard     /* first read to align to sector start */
64383f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
64483f64091Sbellard     if (len > count)
64583f64091Sbellard         len = count;
64683f64091Sbellard     sector_num = offset >> SECTOR_BITS;
64783f64091Sbellard     if (len > 0) {
64883f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
64983f64091Sbellard             return -EIO;
65083f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
65183f64091Sbellard         count -= len;
65283f64091Sbellard         if (count == 0)
65383f64091Sbellard             return count1;
65483f64091Sbellard         sector_num++;
65583f64091Sbellard         buf += len;
65683f64091Sbellard     }
65783f64091Sbellard 
65883f64091Sbellard     /* read the sectors "in place" */
65983f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
66083f64091Sbellard     if (nb_sectors > 0) {
66183f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
66283f64091Sbellard             return -EIO;
66383f64091Sbellard         sector_num += nb_sectors;
66483f64091Sbellard         len = nb_sectors << SECTOR_BITS;
66583f64091Sbellard         buf += len;
66683f64091Sbellard         count -= len;
66783f64091Sbellard     }
66883f64091Sbellard 
66983f64091Sbellard     /* add data from the last sector */
67083f64091Sbellard     if (count > 0) {
67183f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
67283f64091Sbellard             return -EIO;
67383f64091Sbellard         memcpy(buf, tmp_buf, count);
67483f64091Sbellard     }
67583f64091Sbellard     return count1;
67683f64091Sbellard }
67783f64091Sbellard 
678eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
679eda578e5Saliguori                 const void *buf, int count1)
68083f64091Sbellard {
68183f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
68283f64091Sbellard     int len, nb_sectors, count;
68383f64091Sbellard     int64_t sector_num;
68483f64091Sbellard 
68583f64091Sbellard     count = count1;
68683f64091Sbellard     /* first write to align to sector start */
68783f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
68883f64091Sbellard     if (len > count)
68983f64091Sbellard         len = count;
69083f64091Sbellard     sector_num = offset >> SECTOR_BITS;
69183f64091Sbellard     if (len > 0) {
69283f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
69383f64091Sbellard             return -EIO;
69483f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
69583f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
69683f64091Sbellard             return -EIO;
69783f64091Sbellard         count -= len;
69883f64091Sbellard         if (count == 0)
69983f64091Sbellard             return count1;
70083f64091Sbellard         sector_num++;
70183f64091Sbellard         buf += len;
70283f64091Sbellard     }
70383f64091Sbellard 
70483f64091Sbellard     /* write the sectors "in place" */
70583f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
70683f64091Sbellard     if (nb_sectors > 0) {
70783f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
70883f64091Sbellard             return -EIO;
70983f64091Sbellard         sector_num += nb_sectors;
71083f64091Sbellard         len = nb_sectors << SECTOR_BITS;
71183f64091Sbellard         buf += len;
71283f64091Sbellard         count -= len;
71383f64091Sbellard     }
71483f64091Sbellard 
71583f64091Sbellard     /* add data from the last sector */
71683f64091Sbellard     if (count > 0) {
71783f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
71883f64091Sbellard             return -EIO;
71983f64091Sbellard         memcpy(tmp_buf, buf, count);
72083f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
72183f64091Sbellard             return -EIO;
72283f64091Sbellard     }
72383f64091Sbellard     return count1;
72483f64091Sbellard }
72583f64091Sbellard 
72683f64091Sbellard /**
72783f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
72883f64091Sbellard  */
72983f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
73083f64091Sbellard {
73183f64091Sbellard     BlockDriver *drv = bs->drv;
73283f64091Sbellard     if (!drv)
73319cb3738Sbellard         return -ENOMEDIUM;
73483f64091Sbellard     if (!drv->bdrv_truncate)
73583f64091Sbellard         return -ENOTSUP;
736*59f2689dSNaphtali Sprei     if (bs->read_only)
737*59f2689dSNaphtali Sprei         return -EACCES;
73883f64091Sbellard     return drv->bdrv_truncate(bs, offset);
73983f64091Sbellard }
74083f64091Sbellard 
74183f64091Sbellard /**
74283f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
74383f64091Sbellard  */
74483f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
74583f64091Sbellard {
74683f64091Sbellard     BlockDriver *drv = bs->drv;
74783f64091Sbellard     if (!drv)
74819cb3738Sbellard         return -ENOMEDIUM;
74983f64091Sbellard     if (!drv->bdrv_getlength) {
75083f64091Sbellard         /* legacy mode */
75183f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
75283f64091Sbellard     }
75383f64091Sbellard     return drv->bdrv_getlength(bs);
754fc01f7e7Sbellard }
755fc01f7e7Sbellard 
75619cb3738Sbellard /* return 0 as number of sectors if no device present or error */
75796b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
758fc01f7e7Sbellard {
75919cb3738Sbellard     int64_t length;
76019cb3738Sbellard     length = bdrv_getlength(bs);
76119cb3738Sbellard     if (length < 0)
76219cb3738Sbellard         length = 0;
76319cb3738Sbellard     else
76419cb3738Sbellard         length = length >> SECTOR_BITS;
76519cb3738Sbellard     *nb_sectors_ptr = length;
766fc01f7e7Sbellard }
767cf98951bSbellard 
768f3d54fc4Saliguori struct partition {
769f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
770f3d54fc4Saliguori         uint8_t head;               /* starting head */
771f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
772f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
773f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
774f3d54fc4Saliguori         uint8_t end_head;           /* end head */
775f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
776f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
777f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
778f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
779f3d54fc4Saliguori } __attribute__((packed));
780f3d54fc4Saliguori 
781f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
782f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
783f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
784f3d54fc4Saliguori {
785f3d54fc4Saliguori     uint8_t buf[512];
786f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
787f3d54fc4Saliguori     struct partition *p;
788f3d54fc4Saliguori     uint32_t nr_sects;
789a38131b6Sblueswir1     uint64_t nb_sectors;
790f3d54fc4Saliguori 
791f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
792f3d54fc4Saliguori 
793f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
794f3d54fc4Saliguori     if (ret < 0)
795f3d54fc4Saliguori         return -1;
796f3d54fc4Saliguori     /* test msdos magic */
797f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
798f3d54fc4Saliguori         return -1;
799f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
800f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
801f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
802f3d54fc4Saliguori         if (nr_sects && p->end_head) {
803f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
804f3d54fc4Saliguori                a cylinder boundary */
805f3d54fc4Saliguori             heads = p->end_head + 1;
806f3d54fc4Saliguori             sectors = p->end_sector & 63;
807f3d54fc4Saliguori             if (sectors == 0)
808f3d54fc4Saliguori                 continue;
809f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
810f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
811f3d54fc4Saliguori                 continue;
812f3d54fc4Saliguori             *pheads = heads;
813f3d54fc4Saliguori             *psectors = sectors;
814f3d54fc4Saliguori             *pcylinders = cylinders;
815f3d54fc4Saliguori #if 0
816f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
817f3d54fc4Saliguori                    cylinders, heads, sectors);
818f3d54fc4Saliguori #endif
819f3d54fc4Saliguori             return 0;
820f3d54fc4Saliguori         }
821f3d54fc4Saliguori     }
822f3d54fc4Saliguori     return -1;
823f3d54fc4Saliguori }
824f3d54fc4Saliguori 
825f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
826f3d54fc4Saliguori {
827f3d54fc4Saliguori     int translation, lba_detected = 0;
828f3d54fc4Saliguori     int cylinders, heads, secs;
829a38131b6Sblueswir1     uint64_t nb_sectors;
830f3d54fc4Saliguori 
831f3d54fc4Saliguori     /* if a geometry hint is available, use it */
832f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
833f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
834f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
835f3d54fc4Saliguori     if (cylinders != 0) {
836f3d54fc4Saliguori         *pcyls = cylinders;
837f3d54fc4Saliguori         *pheads = heads;
838f3d54fc4Saliguori         *psecs = secs;
839f3d54fc4Saliguori     } else {
840f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
841f3d54fc4Saliguori             if (heads > 16) {
842f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
843f3d54fc4Saliguori                    translation was active, so the default
844f3d54fc4Saliguori                    hardware geometry is OK */
845f3d54fc4Saliguori                 lba_detected = 1;
846f3d54fc4Saliguori                 goto default_geometry;
847f3d54fc4Saliguori             } else {
848f3d54fc4Saliguori                 *pcyls = cylinders;
849f3d54fc4Saliguori                 *pheads = heads;
850f3d54fc4Saliguori                 *psecs = secs;
851f3d54fc4Saliguori                 /* disable any translation to be in sync with
852f3d54fc4Saliguori                    the logical geometry */
853f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
854f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
855f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
856f3d54fc4Saliguori                 }
857f3d54fc4Saliguori             }
858f3d54fc4Saliguori         } else {
859f3d54fc4Saliguori         default_geometry:
860f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
861f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
862f3d54fc4Saliguori 
863f3d54fc4Saliguori             if (cylinders > 16383)
864f3d54fc4Saliguori                 cylinders = 16383;
865f3d54fc4Saliguori             else if (cylinders < 2)
866f3d54fc4Saliguori                 cylinders = 2;
867f3d54fc4Saliguori             *pcyls = cylinders;
868f3d54fc4Saliguori             *pheads = 16;
869f3d54fc4Saliguori             *psecs = 63;
870f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
871f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
872f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
873f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
874f3d54fc4Saliguori                 } else {
875f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
876f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
877f3d54fc4Saliguori                 }
878f3d54fc4Saliguori             }
879f3d54fc4Saliguori         }
880f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
881f3d54fc4Saliguori     }
882f3d54fc4Saliguori }
883f3d54fc4Saliguori 
884b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
885b338082bSbellard                             int cyls, int heads, int secs)
886b338082bSbellard {
887b338082bSbellard     bs->cyls = cyls;
888b338082bSbellard     bs->heads = heads;
889b338082bSbellard     bs->secs = secs;
890b338082bSbellard }
891b338082bSbellard 
892b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
893b338082bSbellard {
894b338082bSbellard     bs->type = type;
895b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
896b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
897b338082bSbellard }
898b338082bSbellard 
89946d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
90046d4767dSbellard {
90146d4767dSbellard     bs->translation = translation;
90246d4767dSbellard }
90346d4767dSbellard 
904b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
905b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
906b338082bSbellard {
907b338082bSbellard     *pcyls = bs->cyls;
908b338082bSbellard     *pheads = bs->heads;
909b338082bSbellard     *psecs = bs->secs;
910b338082bSbellard }
911b338082bSbellard 
912b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
913b338082bSbellard {
914b338082bSbellard     return bs->type;
915b338082bSbellard }
916b338082bSbellard 
91746d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
91846d4767dSbellard {
91946d4767dSbellard     return bs->translation;
92046d4767dSbellard }
92146d4767dSbellard 
922b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
923b338082bSbellard {
924b338082bSbellard     return bs->removable;
925b338082bSbellard }
926b338082bSbellard 
927b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
928b338082bSbellard {
929b338082bSbellard     return bs->read_only;
930b338082bSbellard }
931b338082bSbellard 
932*59f2689dSNaphtali Sprei int bdrv_set_read_only(BlockDriverState *bs, int read_only)
933*59f2689dSNaphtali Sprei {
934*59f2689dSNaphtali Sprei     int ret = bs->read_only;
935*59f2689dSNaphtali Sprei     bs->read_only = read_only;
936*59f2689dSNaphtali Sprei     return ret;
937*59f2689dSNaphtali Sprei }
938*59f2689dSNaphtali Sprei 
939985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
940985a03b0Sths {
941985a03b0Sths     return bs->sg;
942985a03b0Sths }
943985a03b0Sths 
944e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs)
945e900a7b7SChristoph Hellwig {
946e900a7b7SChristoph Hellwig     return bs->enable_write_cache;
947e900a7b7SChristoph Hellwig }
948e900a7b7SChristoph Hellwig 
94919cb3738Sbellard /* XXX: no longer used */
950b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
951b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
952b338082bSbellard {
953b338082bSbellard     bs->change_cb = change_cb;
954b338082bSbellard     bs->change_opaque = opaque;
955b338082bSbellard }
956b338082bSbellard 
957ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
958ea2384d3Sbellard {
959ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
960ea2384d3Sbellard         return 1;
961ea2384d3Sbellard     return bs->encrypted;
962ea2384d3Sbellard }
963ea2384d3Sbellard 
964c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
965c0f4ce77Saliguori {
966c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
967c0f4ce77Saliguori 
968c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
969c0f4ce77Saliguori         return 1;
970c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
971c0f4ce77Saliguori }
972c0f4ce77Saliguori 
973ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
974ea2384d3Sbellard {
975ea2384d3Sbellard     int ret;
976ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
977ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
978ea2384d3Sbellard         if (ret < 0)
979ea2384d3Sbellard             return ret;
980ea2384d3Sbellard         if (!bs->encrypted)
981ea2384d3Sbellard             return 0;
982ea2384d3Sbellard     }
983ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
984ea2384d3Sbellard         return -1;
985c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
986bb5fc20fSaliguori     if (ret < 0) {
987bb5fc20fSaliguori         bs->valid_key = 0;
988bb5fc20fSaliguori     } else if (!bs->valid_key) {
989bb5fc20fSaliguori         bs->valid_key = 1;
990bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
991bb5fc20fSaliguori         bs->media_changed = 1;
992bb5fc20fSaliguori         if (bs->change_cb)
993bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
994bb5fc20fSaliguori     }
995c0f4ce77Saliguori     return ret;
996ea2384d3Sbellard }
997ea2384d3Sbellard 
998ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
999ea2384d3Sbellard {
100019cb3738Sbellard     if (!bs->drv) {
1001ea2384d3Sbellard         buf[0] = '\0';
1002ea2384d3Sbellard     } else {
1003ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
1004ea2384d3Sbellard     }
1005ea2384d3Sbellard }
1006ea2384d3Sbellard 
1007ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1008ea2384d3Sbellard                          void *opaque)
1009ea2384d3Sbellard {
1010ea2384d3Sbellard     BlockDriver *drv;
1011ea2384d3Sbellard 
1012ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
1013ea2384d3Sbellard         it(opaque, drv->format_name);
1014ea2384d3Sbellard     }
1015ea2384d3Sbellard }
1016ea2384d3Sbellard 
1017b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1018b338082bSbellard {
1019b338082bSbellard     BlockDriverState *bs;
1020b338082bSbellard 
1021b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1022b338082bSbellard         if (!strcmp(name, bs->device_name))
1023b338082bSbellard             return bs;
1024b338082bSbellard     }
1025b338082bSbellard     return NULL;
1026b338082bSbellard }
1027b338082bSbellard 
102851de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
102981d0912dSbellard {
103081d0912dSbellard     BlockDriverState *bs;
103181d0912dSbellard 
103281d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
103351de9760Saliguori         it(opaque, bs);
103481d0912dSbellard     }
103581d0912dSbellard }
103681d0912dSbellard 
1037ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1038ea2384d3Sbellard {
1039ea2384d3Sbellard     return bs->device_name;
1040ea2384d3Sbellard }
1041ea2384d3Sbellard 
10427a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
10437a6cba61Spbrook {
1044081501daSaliguori     if (!bs->drv)
1045081501daSaliguori         return;
10467a6cba61Spbrook     if (bs->drv->bdrv_flush)
10477a6cba61Spbrook         bs->drv->bdrv_flush(bs);
10487a6cba61Spbrook     if (bs->backing_hd)
10497a6cba61Spbrook         bdrv_flush(bs->backing_hd);
10507a6cba61Spbrook }
10517a6cba61Spbrook 
1052c6ca28d6Saliguori void bdrv_flush_all(void)
1053c6ca28d6Saliguori {
1054c6ca28d6Saliguori     BlockDriverState *bs;
1055c6ca28d6Saliguori 
1056c6ca28d6Saliguori     for (bs = bdrv_first; bs != NULL; bs = bs->next)
1057c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
1058c6ca28d6Saliguori             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1059c6ca28d6Saliguori             bdrv_flush(bs);
1060c6ca28d6Saliguori }
1061c6ca28d6Saliguori 
1062f58c7b35Sths /*
1063f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1064f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1065f58c7b35Sths  * hence all their sectors are reported as allocated.
1066f58c7b35Sths  *
1067f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1068f58c7b35Sths  * the specified sector) that are known to be in the same
1069f58c7b35Sths  * allocated/unallocated state.
1070f58c7b35Sths  *
1071f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1072f58c7b35Sths  */
1073f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1074f58c7b35Sths 	int *pnum)
1075f58c7b35Sths {
1076f58c7b35Sths     int64_t n;
1077f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1078f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1079f58c7b35Sths             *pnum = 0;
1080f58c7b35Sths             return 0;
1081f58c7b35Sths         }
1082f58c7b35Sths         n = bs->total_sectors - sector_num;
1083f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1084f58c7b35Sths         return 1;
1085f58c7b35Sths     }
1086f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1087f58c7b35Sths }
1088f58c7b35Sths 
1089376253ecSaliguori void bdrv_info(Monitor *mon)
1090b338082bSbellard {
1091b338082bSbellard     BlockDriverState *bs;
1092b338082bSbellard 
1093b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1094376253ecSaliguori         monitor_printf(mon, "%s:", bs->device_name);
1095376253ecSaliguori         monitor_printf(mon, " type=");
1096b338082bSbellard         switch(bs->type) {
1097b338082bSbellard         case BDRV_TYPE_HD:
1098376253ecSaliguori             monitor_printf(mon, "hd");
1099b338082bSbellard             break;
1100b338082bSbellard         case BDRV_TYPE_CDROM:
1101376253ecSaliguori             monitor_printf(mon, "cdrom");
1102b338082bSbellard             break;
1103b338082bSbellard         case BDRV_TYPE_FLOPPY:
1104376253ecSaliguori             monitor_printf(mon, "floppy");
1105b338082bSbellard             break;
1106b338082bSbellard         }
1107376253ecSaliguori         monitor_printf(mon, " removable=%d", bs->removable);
1108b338082bSbellard         if (bs->removable) {
1109376253ecSaliguori             monitor_printf(mon, " locked=%d", bs->locked);
1110b338082bSbellard         }
111119cb3738Sbellard         if (bs->drv) {
1112376253ecSaliguori             monitor_printf(mon, " file=");
1113376253ecSaliguori             monitor_print_filename(mon, bs->filename);
1114fef30743Sths             if (bs->backing_file[0] != '\0') {
1115376253ecSaliguori                 monitor_printf(mon, " backing_file=");
1116376253ecSaliguori                 monitor_print_filename(mon, bs->backing_file);
1117fef30743Sths             }
1118376253ecSaliguori             monitor_printf(mon, " ro=%d", bs->read_only);
1119376253ecSaliguori             monitor_printf(mon, " drv=%s", bs->drv->format_name);
1120376253ecSaliguori             monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1121b338082bSbellard         } else {
1122376253ecSaliguori             monitor_printf(mon, " [not inserted]");
1123b338082bSbellard         }
1124376253ecSaliguori         monitor_printf(mon, "\n");
1125b338082bSbellard     }
1126b338082bSbellard }
1127a36e69ddSths 
1128a36e69ddSths /* The "info blockstats" command. */
1129376253ecSaliguori void bdrv_info_stats(Monitor *mon)
1130a36e69ddSths {
1131a36e69ddSths     BlockDriverState *bs;
1132a36e69ddSths 
1133a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1134376253ecSaliguori         monitor_printf(mon, "%s:"
1135a36e69ddSths                        " rd_bytes=%" PRIu64
1136a36e69ddSths                        " wr_bytes=%" PRIu64
1137a36e69ddSths                        " rd_operations=%" PRIu64
1138a36e69ddSths                        " wr_operations=%" PRIu64
1139ebf53fcdSaliguori                        "\n",
1140a36e69ddSths                        bs->device_name,
1141a36e69ddSths                        bs->rd_bytes, bs->wr_bytes,
1142a36e69ddSths                        bs->rd_ops, bs->wr_ops);
1143a36e69ddSths     }
1144a36e69ddSths }
1145ea2384d3Sbellard 
1146045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1147045df330Saliguori {
1148045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1149045df330Saliguori         return bs->backing_file;
1150045df330Saliguori     else if (bs->encrypted)
1151045df330Saliguori         return bs->filename;
1152045df330Saliguori     else
1153045df330Saliguori         return NULL;
1154045df330Saliguori }
1155045df330Saliguori 
115683f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
115783f64091Sbellard                                char *filename, int filename_size)
115883f64091Sbellard {
115983f64091Sbellard     if (!bs->backing_hd) {
116083f64091Sbellard         pstrcpy(filename, filename_size, "");
116183f64091Sbellard     } else {
116283f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
116383f64091Sbellard     }
116483f64091Sbellard }
116583f64091Sbellard 
1166faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1167faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1168faea38e7Sbellard {
1169faea38e7Sbellard     BlockDriver *drv = bs->drv;
1170faea38e7Sbellard     if (!drv)
117119cb3738Sbellard         return -ENOMEDIUM;
1172faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1173faea38e7Sbellard         return -ENOTSUP;
1174fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1175fbb7b4e0SKevin Wolf         return -EIO;
1176faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1177faea38e7Sbellard }
1178faea38e7Sbellard 
1179faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1180faea38e7Sbellard {
1181faea38e7Sbellard     BlockDriver *drv = bs->drv;
1182faea38e7Sbellard     if (!drv)
118319cb3738Sbellard         return -ENOMEDIUM;
1184faea38e7Sbellard     if (!drv->bdrv_get_info)
1185faea38e7Sbellard         return -ENOTSUP;
1186faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1187faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1188faea38e7Sbellard }
1189faea38e7Sbellard 
119045566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const 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_save_vmstate)
1197178e08a5Saliguori         return -ENOTSUP;
119845566e9cSChristoph Hellwig     return drv->bdrv_save_vmstate(bs, buf, pos, size);
1199178e08a5Saliguori }
1200178e08a5Saliguori 
120145566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
120245566e9cSChristoph Hellwig                       int64_t pos, int size)
1203178e08a5Saliguori {
1204178e08a5Saliguori     BlockDriver *drv = bs->drv;
1205178e08a5Saliguori     if (!drv)
1206178e08a5Saliguori         return -ENOMEDIUM;
120745566e9cSChristoph Hellwig     if (!drv->bdrv_load_vmstate)
1208178e08a5Saliguori         return -ENOTSUP;
120945566e9cSChristoph Hellwig     return drv->bdrv_load_vmstate(bs, buf, pos, size);
1210178e08a5Saliguori }
1211178e08a5Saliguori 
1212faea38e7Sbellard /**************************************************************/
1213faea38e7Sbellard /* handling of snapshots */
1214faea38e7Sbellard 
1215faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1216faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1217faea38e7Sbellard {
1218faea38e7Sbellard     BlockDriver *drv = bs->drv;
1219faea38e7Sbellard     if (!drv)
122019cb3738Sbellard         return -ENOMEDIUM;
1221faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
1222faea38e7Sbellard         return -ENOTSUP;
1223faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
1224faea38e7Sbellard }
1225faea38e7Sbellard 
1226faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1227faea38e7Sbellard                        const char *snapshot_id)
1228faea38e7Sbellard {
1229faea38e7Sbellard     BlockDriver *drv = bs->drv;
1230faea38e7Sbellard     if (!drv)
123119cb3738Sbellard         return -ENOMEDIUM;
1232faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
1233faea38e7Sbellard         return -ENOTSUP;
1234faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
1235faea38e7Sbellard }
1236faea38e7Sbellard 
1237faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1238faea38e7Sbellard {
1239faea38e7Sbellard     BlockDriver *drv = bs->drv;
1240faea38e7Sbellard     if (!drv)
124119cb3738Sbellard         return -ENOMEDIUM;
1242faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
1243faea38e7Sbellard         return -ENOTSUP;
1244faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1245faea38e7Sbellard }
1246faea38e7Sbellard 
1247faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1248faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1249faea38e7Sbellard {
1250faea38e7Sbellard     BlockDriver *drv = bs->drv;
1251faea38e7Sbellard     if (!drv)
125219cb3738Sbellard         return -ENOMEDIUM;
1253faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1254faea38e7Sbellard         return -ENOTSUP;
1255faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1256faea38e7Sbellard }
1257faea38e7Sbellard 
1258faea38e7Sbellard #define NB_SUFFIXES 4
1259faea38e7Sbellard 
1260faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1261faea38e7Sbellard {
1262faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1263faea38e7Sbellard     int64_t base;
1264faea38e7Sbellard     int i;
1265faea38e7Sbellard 
1266faea38e7Sbellard     if (size <= 999) {
1267faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1268faea38e7Sbellard     } else {
1269faea38e7Sbellard         base = 1024;
1270faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1271faea38e7Sbellard             if (size < (10 * base)) {
1272faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1273faea38e7Sbellard                          (double)size / base,
1274faea38e7Sbellard                          suffixes[i]);
1275faea38e7Sbellard                 break;
1276faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1277faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1278faea38e7Sbellard                          ((size + (base >> 1)) / base),
1279faea38e7Sbellard                          suffixes[i]);
1280faea38e7Sbellard                 break;
1281faea38e7Sbellard             }
1282faea38e7Sbellard             base = base * 1024;
1283faea38e7Sbellard         }
1284faea38e7Sbellard     }
1285faea38e7Sbellard     return buf;
1286faea38e7Sbellard }
1287faea38e7Sbellard 
1288faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1289faea38e7Sbellard {
1290faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
12913b9f94e1Sbellard #ifdef _WIN32
12923b9f94e1Sbellard     struct tm *ptm;
12933b9f94e1Sbellard #else
1294faea38e7Sbellard     struct tm tm;
12953b9f94e1Sbellard #endif
1296faea38e7Sbellard     time_t ti;
1297faea38e7Sbellard     int64_t secs;
1298faea38e7Sbellard 
1299faea38e7Sbellard     if (!sn) {
1300faea38e7Sbellard         snprintf(buf, buf_size,
1301faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1302faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1303faea38e7Sbellard     } else {
1304faea38e7Sbellard         ti = sn->date_sec;
13053b9f94e1Sbellard #ifdef _WIN32
13063b9f94e1Sbellard         ptm = localtime(&ti);
13073b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
13083b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
13093b9f94e1Sbellard #else
1310faea38e7Sbellard         localtime_r(&ti, &tm);
1311faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1312faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
13133b9f94e1Sbellard #endif
1314faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1315faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1316faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1317faea38e7Sbellard                  (int)(secs / 3600),
1318faea38e7Sbellard                  (int)((secs / 60) % 60),
1319faea38e7Sbellard                  (int)(secs % 60),
1320faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1321faea38e7Sbellard         snprintf(buf, buf_size,
1322faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1323faea38e7Sbellard                  sn->id_str, sn->name,
1324faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1325faea38e7Sbellard                  date_buf,
1326faea38e7Sbellard                  clock_buf);
1327faea38e7Sbellard     }
1328faea38e7Sbellard     return buf;
1329faea38e7Sbellard }
1330faea38e7Sbellard 
133183f64091Sbellard 
1332ea2384d3Sbellard /**************************************************************/
133383f64091Sbellard /* async I/Os */
1334ea2384d3Sbellard 
13353b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1336f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
133783f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
1338ea2384d3Sbellard {
133983f64091Sbellard     BlockDriver *drv = bs->drv;
1340a36e69ddSths     BlockDriverAIOCB *ret;
1341ea2384d3Sbellard 
134219cb3738Sbellard     if (!drv)
1343ce1a14dcSpbrook         return NULL;
134471d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
134571d0770cSaliguori         return NULL;
134683f64091Sbellard 
1347f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1348f141eafeSaliguori                               cb, opaque);
1349a36e69ddSths 
1350a36e69ddSths     if (ret) {
1351a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1352a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1353a36e69ddSths 	bs->rd_ops ++;
1354a36e69ddSths     }
1355a36e69ddSths 
1356a36e69ddSths     return ret;
135783f64091Sbellard }
135883f64091Sbellard 
1359f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1360f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
136183f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
13627674e7bfSbellard {
136383f64091Sbellard     BlockDriver *drv = bs->drv;
1364a36e69ddSths     BlockDriverAIOCB *ret;
136583f64091Sbellard 
136619cb3738Sbellard     if (!drv)
1367ce1a14dcSpbrook         return NULL;
136883f64091Sbellard     if (bs->read_only)
1369ce1a14dcSpbrook         return NULL;
137071d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
137171d0770cSaliguori         return NULL;
137283f64091Sbellard 
1373f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1374f141eafeSaliguori                                cb, opaque);
1375a36e69ddSths 
1376a36e69ddSths     if (ret) {
1377a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1378a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1379a36e69ddSths 	bs->wr_ops ++;
1380a36e69ddSths     }
1381a36e69ddSths 
1382a36e69ddSths     return ret;
138383f64091Sbellard }
138483f64091Sbellard 
138540b4f539SKevin Wolf 
138640b4f539SKevin Wolf typedef struct MultiwriteCB {
138740b4f539SKevin Wolf     int error;
138840b4f539SKevin Wolf     int num_requests;
138940b4f539SKevin Wolf     int num_callbacks;
139040b4f539SKevin Wolf     struct {
139140b4f539SKevin Wolf         BlockDriverCompletionFunc *cb;
139240b4f539SKevin Wolf         void *opaque;
139340b4f539SKevin Wolf         QEMUIOVector *free_qiov;
139440b4f539SKevin Wolf         void *free_buf;
139540b4f539SKevin Wolf     } callbacks[];
139640b4f539SKevin Wolf } MultiwriteCB;
139740b4f539SKevin Wolf 
139840b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb)
139940b4f539SKevin Wolf {
140040b4f539SKevin Wolf     int i;
140140b4f539SKevin Wolf 
140240b4f539SKevin Wolf     for (i = 0; i < mcb->num_callbacks; i++) {
140340b4f539SKevin Wolf         mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
140440b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_qiov);
140540b4f539SKevin Wolf         qemu_free(mcb->callbacks[i].free_buf);
140640b4f539SKevin Wolf     }
140740b4f539SKevin Wolf }
140840b4f539SKevin Wolf 
140940b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret)
141040b4f539SKevin Wolf {
141140b4f539SKevin Wolf     MultiwriteCB *mcb = opaque;
141240b4f539SKevin Wolf 
141340b4f539SKevin Wolf     if (ret < 0) {
141440b4f539SKevin Wolf         mcb->error = ret;
141540b4f539SKevin Wolf         multiwrite_user_cb(mcb);
141640b4f539SKevin Wolf     }
141740b4f539SKevin Wolf 
141840b4f539SKevin Wolf     mcb->num_requests--;
141940b4f539SKevin Wolf     if (mcb->num_requests == 0) {
142040b4f539SKevin Wolf         if (mcb->error == 0) {
142140b4f539SKevin Wolf             multiwrite_user_cb(mcb);
142240b4f539SKevin Wolf         }
142340b4f539SKevin Wolf         qemu_free(mcb);
142440b4f539SKevin Wolf     }
142540b4f539SKevin Wolf }
142640b4f539SKevin Wolf 
142740b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b)
142840b4f539SKevin Wolf {
142940b4f539SKevin Wolf     return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
143040b4f539SKevin Wolf }
143140b4f539SKevin Wolf 
143240b4f539SKevin Wolf /*
143340b4f539SKevin Wolf  * Takes a bunch of requests and tries to merge them. Returns the number of
143440b4f539SKevin Wolf  * requests that remain after merging.
143540b4f539SKevin Wolf  */
143640b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
143740b4f539SKevin Wolf     int num_reqs, MultiwriteCB *mcb)
143840b4f539SKevin Wolf {
143940b4f539SKevin Wolf     int i, outidx;
144040b4f539SKevin Wolf 
144140b4f539SKevin Wolf     // Sort requests by start sector
144240b4f539SKevin Wolf     qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
144340b4f539SKevin Wolf 
144440b4f539SKevin Wolf     // Check if adjacent requests touch the same clusters. If so, combine them,
144540b4f539SKevin Wolf     // filling up gaps with zero sectors.
144640b4f539SKevin Wolf     outidx = 0;
144740b4f539SKevin Wolf     for (i = 1; i < num_reqs; i++) {
144840b4f539SKevin Wolf         int merge = 0;
144940b4f539SKevin Wolf         int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
145040b4f539SKevin Wolf 
145140b4f539SKevin Wolf         // This handles the cases that are valid for all block drivers, namely
145240b4f539SKevin Wolf         // exactly sequential writes and overlapping writes.
145340b4f539SKevin Wolf         if (reqs[i].sector <= oldreq_last) {
145440b4f539SKevin Wolf             merge = 1;
145540b4f539SKevin Wolf         }
145640b4f539SKevin Wolf 
145740b4f539SKevin Wolf         // The block driver may decide that it makes sense to combine requests
145840b4f539SKevin Wolf         // even if there is a gap of some sectors between them. In this case,
145940b4f539SKevin Wolf         // the gap is filled with zeros (therefore only applicable for yet
146040b4f539SKevin Wolf         // unused space in format like qcow2).
146140b4f539SKevin Wolf         if (!merge && bs->drv->bdrv_merge_requests) {
146240b4f539SKevin Wolf             merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
146340b4f539SKevin Wolf         }
146440b4f539SKevin Wolf 
146540b4f539SKevin Wolf         if (merge) {
146640b4f539SKevin Wolf             size_t size;
146740b4f539SKevin Wolf             QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
146840b4f539SKevin Wolf             qemu_iovec_init(qiov,
146940b4f539SKevin Wolf                 reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
147040b4f539SKevin Wolf 
147140b4f539SKevin Wolf             // Add the first request to the merged one. If the requests are
147240b4f539SKevin Wolf             // overlapping, drop the last sectors of the first request.
147340b4f539SKevin Wolf             size = (reqs[i].sector - reqs[outidx].sector) << 9;
147440b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
147540b4f539SKevin Wolf 
147640b4f539SKevin Wolf             // We might need to add some zeros between the two requests
147740b4f539SKevin Wolf             if (reqs[i].sector > oldreq_last) {
147840b4f539SKevin Wolf                 size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
147940b4f539SKevin Wolf                 uint8_t *buf = qemu_blockalign(bs, zero_bytes);
148040b4f539SKevin Wolf                 memset(buf, 0, zero_bytes);
148140b4f539SKevin Wolf                 qemu_iovec_add(qiov, buf, zero_bytes);
148240b4f539SKevin Wolf                 mcb->callbacks[i].free_buf = buf;
148340b4f539SKevin Wolf             }
148440b4f539SKevin Wolf 
148540b4f539SKevin Wolf             // Add the second request
148640b4f539SKevin Wolf             qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
148740b4f539SKevin Wolf 
148840b4f539SKevin Wolf             reqs[outidx].nb_sectors += reqs[i].nb_sectors;
148940b4f539SKevin Wolf             reqs[outidx].qiov = qiov;
149040b4f539SKevin Wolf 
149140b4f539SKevin Wolf             mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
149240b4f539SKevin Wolf         } else {
149340b4f539SKevin Wolf             outidx++;
149440b4f539SKevin Wolf             reqs[outidx].sector     = reqs[i].sector;
149540b4f539SKevin Wolf             reqs[outidx].nb_sectors = reqs[i].nb_sectors;
149640b4f539SKevin Wolf             reqs[outidx].qiov       = reqs[i].qiov;
149740b4f539SKevin Wolf         }
149840b4f539SKevin Wolf     }
149940b4f539SKevin Wolf 
150040b4f539SKevin Wolf     return outidx + 1;
150140b4f539SKevin Wolf }
150240b4f539SKevin Wolf 
150340b4f539SKevin Wolf /*
150440b4f539SKevin Wolf  * Submit multiple AIO write requests at once.
150540b4f539SKevin Wolf  *
150640b4f539SKevin Wolf  * On success, the function returns 0 and all requests in the reqs array have
150740b4f539SKevin Wolf  * been submitted. In error case this function returns -1, and any of the
150840b4f539SKevin Wolf  * requests may or may not be submitted yet. In particular, this means that the
150940b4f539SKevin Wolf  * callback will be called for some of the requests, for others it won't. The
151040b4f539SKevin Wolf  * caller must check the error field of the BlockRequest to wait for the right
151140b4f539SKevin Wolf  * callbacks (if error != 0, no callback will be called).
151240b4f539SKevin Wolf  *
151340b4f539SKevin Wolf  * The implementation may modify the contents of the reqs array, e.g. to merge
151440b4f539SKevin Wolf  * requests. However, the fields opaque and error are left unmodified as they
151540b4f539SKevin Wolf  * are used to signal failure for a single request to the caller.
151640b4f539SKevin Wolf  */
151740b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
151840b4f539SKevin Wolf {
151940b4f539SKevin Wolf     BlockDriverAIOCB *acb;
152040b4f539SKevin Wolf     MultiwriteCB *mcb;
152140b4f539SKevin Wolf     int i;
152240b4f539SKevin Wolf 
152340b4f539SKevin Wolf     if (num_reqs == 0) {
152440b4f539SKevin Wolf         return 0;
152540b4f539SKevin Wolf     }
152640b4f539SKevin Wolf 
152740b4f539SKevin Wolf     // Create MultiwriteCB structure
152840b4f539SKevin Wolf     mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
152940b4f539SKevin Wolf     mcb->num_requests = 0;
153040b4f539SKevin Wolf     mcb->num_callbacks = num_reqs;
153140b4f539SKevin Wolf 
153240b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
153340b4f539SKevin Wolf         mcb->callbacks[i].cb = reqs[i].cb;
153440b4f539SKevin Wolf         mcb->callbacks[i].opaque = reqs[i].opaque;
153540b4f539SKevin Wolf     }
153640b4f539SKevin Wolf 
153740b4f539SKevin Wolf     // Check for mergable requests
153840b4f539SKevin Wolf     num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
153940b4f539SKevin Wolf 
154040b4f539SKevin Wolf     // Run the aio requests
154140b4f539SKevin Wolf     for (i = 0; i < num_reqs; i++) {
154240b4f539SKevin Wolf         acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
154340b4f539SKevin Wolf             reqs[i].nb_sectors, multiwrite_cb, mcb);
154440b4f539SKevin Wolf 
154540b4f539SKevin Wolf         if (acb == NULL) {
154640b4f539SKevin Wolf             // We can only fail the whole thing if no request has been
154740b4f539SKevin Wolf             // submitted yet. Otherwise we'll wait for the submitted AIOs to
154840b4f539SKevin Wolf             // complete and report the error in the callback.
154940b4f539SKevin Wolf             if (mcb->num_requests == 0) {
155040b4f539SKevin Wolf                 reqs[i].error = EIO;
155140b4f539SKevin Wolf                 goto fail;
155240b4f539SKevin Wolf             } else {
155340b4f539SKevin Wolf                 mcb->error = EIO;
155440b4f539SKevin Wolf                 break;
155540b4f539SKevin Wolf             }
155640b4f539SKevin Wolf         } else {
155740b4f539SKevin Wolf             mcb->num_requests++;
155840b4f539SKevin Wolf         }
155940b4f539SKevin Wolf     }
156040b4f539SKevin Wolf 
156140b4f539SKevin Wolf     return 0;
156240b4f539SKevin Wolf 
156340b4f539SKevin Wolf fail:
156440b4f539SKevin Wolf     free(mcb);
156540b4f539SKevin Wolf     return -1;
156640b4f539SKevin Wolf }
156740b4f539SKevin Wolf 
1568b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1569b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
1570b2e12bc6SChristoph Hellwig {
1571b2e12bc6SChristoph Hellwig     BlockDriver *drv = bs->drv;
1572b2e12bc6SChristoph Hellwig 
1573b2e12bc6SChristoph Hellwig     if (!drv)
1574b2e12bc6SChristoph Hellwig         return NULL;
1575b2e12bc6SChristoph Hellwig 
1576b2e12bc6SChristoph Hellwig     /*
1577b2e12bc6SChristoph Hellwig      * Note that unlike bdrv_flush the driver is reponsible for flushing a
1578b2e12bc6SChristoph Hellwig      * backing image if it exists.
1579b2e12bc6SChristoph Hellwig      */
1580b2e12bc6SChristoph Hellwig     return drv->bdrv_aio_flush(bs, cb, opaque);
1581b2e12bc6SChristoph Hellwig }
1582b2e12bc6SChristoph Hellwig 
158383f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
158483f64091Sbellard {
15856bbff9a0Saliguori     acb->pool->cancel(acb);
158683f64091Sbellard }
158783f64091Sbellard 
158883f64091Sbellard 
158983f64091Sbellard /**************************************************************/
159083f64091Sbellard /* async block device emulation */
159183f64091Sbellard 
1592c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync {
1593c16b5a2cSChristoph Hellwig     BlockDriverAIOCB common;
1594c16b5a2cSChristoph Hellwig     QEMUBH *bh;
1595c16b5a2cSChristoph Hellwig     int ret;
1596c16b5a2cSChristoph Hellwig     /* vector translation state */
1597c16b5a2cSChristoph Hellwig     QEMUIOVector *qiov;
1598c16b5a2cSChristoph Hellwig     uint8_t *bounce;
1599c16b5a2cSChristoph Hellwig     int is_write;
1600c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync;
1601c16b5a2cSChristoph Hellwig 
1602c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1603c16b5a2cSChristoph Hellwig {
1604c16b5a2cSChristoph Hellwig     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
16056a7ad299SDor Laor     qemu_bh_delete(acb->bh);
160636afc451SAvi Kivity     acb->bh = NULL;
1607c16b5a2cSChristoph Hellwig     qemu_aio_release(acb);
1608c16b5a2cSChristoph Hellwig }
1609c16b5a2cSChristoph Hellwig 
1610c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = {
1611c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
1612c16b5a2cSChristoph Hellwig     .cancel             = bdrv_aio_cancel_em,
1613c16b5a2cSChristoph Hellwig };
1614c16b5a2cSChristoph Hellwig 
161583f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1616beac80cdSbellard {
1617ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1618f141eafeSaliguori 
1619f141eafeSaliguori     if (!acb->is_write)
1620f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1621ceb42de8Saliguori     qemu_vfree(acb->bounce);
1622ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
16236a7ad299SDor Laor     qemu_bh_delete(acb->bh);
162436afc451SAvi Kivity     acb->bh = NULL;
1625ce1a14dcSpbrook     qemu_aio_release(acb);
1626beac80cdSbellard }
1627beac80cdSbellard 
1628f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1629f141eafeSaliguori                                             int64_t sector_num,
1630f141eafeSaliguori                                             QEMUIOVector *qiov,
1631f141eafeSaliguori                                             int nb_sectors,
1632f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
1633f141eafeSaliguori                                             void *opaque,
1634f141eafeSaliguori                                             int is_write)
1635f141eafeSaliguori 
1636ea2384d3Sbellard {
1637ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
163883f64091Sbellard 
1639c16b5a2cSChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1640f141eafeSaliguori     acb->is_write = is_write;
1641f141eafeSaliguori     acb->qiov = qiov;
1642e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
1643f141eafeSaliguori 
1644ce1a14dcSpbrook     if (!acb->bh)
1645ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1646f141eafeSaliguori 
1647f141eafeSaliguori     if (is_write) {
1648f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1649f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1650f141eafeSaliguori     } else {
1651f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1652f141eafeSaliguori     }
1653f141eafeSaliguori 
1654ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1655f141eafeSaliguori 
1656ce1a14dcSpbrook     return &acb->common;
16577a6cba61Spbrook }
16587a6cba61Spbrook 
1659f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1660f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1661ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
166283f64091Sbellard {
1663f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
166483f64091Sbellard }
166583f64091Sbellard 
1666f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1667f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1668f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
1669f141eafeSaliguori {
1670f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1671f141eafeSaliguori }
1672f141eafeSaliguori 
1673b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
1674b2e12bc6SChristoph Hellwig         BlockDriverCompletionFunc *cb, void *opaque)
1675b2e12bc6SChristoph Hellwig {
1676b2e12bc6SChristoph Hellwig     BlockDriverAIOCBSync *acb;
1677b2e12bc6SChristoph Hellwig 
1678b2e12bc6SChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1679b2e12bc6SChristoph Hellwig     acb->is_write = 1; /* don't bounce in the completion hadler */
1680b2e12bc6SChristoph Hellwig     acb->qiov = NULL;
1681b2e12bc6SChristoph Hellwig     acb->bounce = NULL;
1682b2e12bc6SChristoph Hellwig     acb->ret = 0;
1683b2e12bc6SChristoph Hellwig 
1684b2e12bc6SChristoph Hellwig     if (!acb->bh)
1685b2e12bc6SChristoph Hellwig         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1686b2e12bc6SChristoph Hellwig 
1687b2e12bc6SChristoph Hellwig     bdrv_flush(bs);
1688b2e12bc6SChristoph Hellwig     qemu_bh_schedule(acb->bh);
1689b2e12bc6SChristoph Hellwig     return &acb->common;
1690b2e12bc6SChristoph Hellwig }
1691b2e12bc6SChristoph Hellwig 
169283f64091Sbellard /**************************************************************/
169383f64091Sbellard /* sync block device emulation */
169483f64091Sbellard 
169583f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
169683f64091Sbellard {
169783f64091Sbellard     *(int *)opaque = ret;
169883f64091Sbellard }
169983f64091Sbellard 
170083f64091Sbellard #define NOT_DONE 0x7fffffff
170183f64091Sbellard 
170283f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
170383f64091Sbellard                         uint8_t *buf, int nb_sectors)
170483f64091Sbellard {
1705ce1a14dcSpbrook     int async_ret;
1706ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1707f141eafeSaliguori     struct iovec iov;
1708f141eafeSaliguori     QEMUIOVector qiov;
170983f64091Sbellard 
171065d6b3d8SKevin Wolf     async_context_push();
171165d6b3d8SKevin Wolf 
171283f64091Sbellard     async_ret = NOT_DONE;
17133f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
1714f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1715f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1716f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
171783f64091Sbellard         bdrv_rw_em_cb, &async_ret);
171865d6b3d8SKevin Wolf     if (acb == NULL) {
171965d6b3d8SKevin Wolf         async_ret = -1;
172065d6b3d8SKevin Wolf         goto fail;
172165d6b3d8SKevin Wolf     }
1722baf35cb9Saliguori 
172383f64091Sbellard     while (async_ret == NOT_DONE) {
172483f64091Sbellard         qemu_aio_wait();
172583f64091Sbellard     }
1726baf35cb9Saliguori 
172765d6b3d8SKevin Wolf 
172865d6b3d8SKevin Wolf fail:
172965d6b3d8SKevin Wolf     async_context_pop();
173083f64091Sbellard     return async_ret;
173183f64091Sbellard }
173283f64091Sbellard 
173383f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
173483f64091Sbellard                          const uint8_t *buf, int nb_sectors)
173583f64091Sbellard {
1736ce1a14dcSpbrook     int async_ret;
1737ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1738f141eafeSaliguori     struct iovec iov;
1739f141eafeSaliguori     QEMUIOVector qiov;
174083f64091Sbellard 
174165d6b3d8SKevin Wolf     async_context_push();
174265d6b3d8SKevin Wolf 
174383f64091Sbellard     async_ret = NOT_DONE;
1744f141eafeSaliguori     iov.iov_base = (void *)buf;
1745f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1746f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1747f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
174883f64091Sbellard         bdrv_rw_em_cb, &async_ret);
174965d6b3d8SKevin Wolf     if (acb == NULL) {
175065d6b3d8SKevin Wolf         async_ret = -1;
175165d6b3d8SKevin Wolf         goto fail;
175265d6b3d8SKevin Wolf     }
175383f64091Sbellard     while (async_ret == NOT_DONE) {
175483f64091Sbellard         qemu_aio_wait();
175583f64091Sbellard     }
175665d6b3d8SKevin Wolf 
175765d6b3d8SKevin Wolf fail:
175865d6b3d8SKevin Wolf     async_context_pop();
175983f64091Sbellard     return async_ret;
176083f64091Sbellard }
1761ea2384d3Sbellard 
1762ea2384d3Sbellard void bdrv_init(void)
1763ea2384d3Sbellard {
17645efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
1765ea2384d3Sbellard }
1766ce1a14dcSpbrook 
1767c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
17686bbff9a0Saliguori                    BlockDriverCompletionFunc *cb, void *opaque)
17696bbff9a0Saliguori {
1770ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1771ce1a14dcSpbrook 
17726bbff9a0Saliguori     if (pool->free_aiocb) {
17736bbff9a0Saliguori         acb = pool->free_aiocb;
17746bbff9a0Saliguori         pool->free_aiocb = acb->next;
1775ce1a14dcSpbrook     } else {
17766bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
17776bbff9a0Saliguori         acb->pool = pool;
1778ce1a14dcSpbrook     }
1779ce1a14dcSpbrook     acb->bs = bs;
1780ce1a14dcSpbrook     acb->cb = cb;
1781ce1a14dcSpbrook     acb->opaque = opaque;
1782ce1a14dcSpbrook     return acb;
1783ce1a14dcSpbrook }
1784ce1a14dcSpbrook 
1785ce1a14dcSpbrook void qemu_aio_release(void *p)
1786ce1a14dcSpbrook {
17876bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
17886bbff9a0Saliguori     AIOPool *pool = acb->pool;
17896bbff9a0Saliguori     acb->next = pool->free_aiocb;
17906bbff9a0Saliguori     pool->free_aiocb = acb;
1791ce1a14dcSpbrook }
179219cb3738Sbellard 
179319cb3738Sbellard /**************************************************************/
179419cb3738Sbellard /* removable device support */
179519cb3738Sbellard 
179619cb3738Sbellard /**
179719cb3738Sbellard  * Return TRUE if the media is present
179819cb3738Sbellard  */
179919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
180019cb3738Sbellard {
180119cb3738Sbellard     BlockDriver *drv = bs->drv;
180219cb3738Sbellard     int ret;
180319cb3738Sbellard     if (!drv)
180419cb3738Sbellard         return 0;
180519cb3738Sbellard     if (!drv->bdrv_is_inserted)
180619cb3738Sbellard         return 1;
180719cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
180819cb3738Sbellard     return ret;
180919cb3738Sbellard }
181019cb3738Sbellard 
181119cb3738Sbellard /**
181219cb3738Sbellard  * Return TRUE if the media changed since the last call to this
181319cb3738Sbellard  * function. It is currently only used for floppy disks
181419cb3738Sbellard  */
181519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
181619cb3738Sbellard {
181719cb3738Sbellard     BlockDriver *drv = bs->drv;
181819cb3738Sbellard     int ret;
181919cb3738Sbellard 
182019cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
182119cb3738Sbellard         ret = -ENOTSUP;
182219cb3738Sbellard     else
182319cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
182419cb3738Sbellard     if (ret == -ENOTSUP)
182519cb3738Sbellard         ret = bs->media_changed;
182619cb3738Sbellard     bs->media_changed = 0;
182719cb3738Sbellard     return ret;
182819cb3738Sbellard }
182919cb3738Sbellard 
183019cb3738Sbellard /**
183119cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
183219cb3738Sbellard  */
1833aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag)
183419cb3738Sbellard {
183519cb3738Sbellard     BlockDriver *drv = bs->drv;
183619cb3738Sbellard     int ret;
183719cb3738Sbellard 
1838aea2a33cSMark McLoughlin     if (bs->locked) {
1839aea2a33cSMark McLoughlin         return -EBUSY;
1840aea2a33cSMark McLoughlin     }
1841aea2a33cSMark McLoughlin 
184219cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
184319cb3738Sbellard         ret = -ENOTSUP;
184419cb3738Sbellard     } else {
184519cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
184619cb3738Sbellard     }
184719cb3738Sbellard     if (ret == -ENOTSUP) {
184819cb3738Sbellard         if (eject_flag)
184919cb3738Sbellard             bdrv_close(bs);
1850aea2a33cSMark McLoughlin         ret = 0;
185119cb3738Sbellard     }
1852aea2a33cSMark McLoughlin 
1853aea2a33cSMark McLoughlin     return ret;
185419cb3738Sbellard }
185519cb3738Sbellard 
185619cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
185719cb3738Sbellard {
185819cb3738Sbellard     return bs->locked;
185919cb3738Sbellard }
186019cb3738Sbellard 
186119cb3738Sbellard /**
186219cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
186319cb3738Sbellard  * to eject it manually).
186419cb3738Sbellard  */
186519cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
186619cb3738Sbellard {
186719cb3738Sbellard     BlockDriver *drv = bs->drv;
186819cb3738Sbellard 
186919cb3738Sbellard     bs->locked = locked;
187019cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
187119cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
187219cb3738Sbellard     }
187319cb3738Sbellard }
1874985a03b0Sths 
1875985a03b0Sths /* needed for generic scsi interface */
1876985a03b0Sths 
1877985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1878985a03b0Sths {
1879985a03b0Sths     BlockDriver *drv = bs->drv;
1880985a03b0Sths 
1881985a03b0Sths     if (drv && drv->bdrv_ioctl)
1882985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1883985a03b0Sths     return -ENOTSUP;
1884985a03b0Sths }
18857d780669Saliguori 
1886221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1887221f715dSaliguori         unsigned long int req, void *buf,
18887d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
18897d780669Saliguori {
1890221f715dSaliguori     BlockDriver *drv = bs->drv;
18917d780669Saliguori 
1892221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
1893221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1894221f715dSaliguori     return NULL;
18957d780669Saliguori }
1896e268ca52Saliguori 
1897e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
1898e268ca52Saliguori {
1899e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1900e268ca52Saliguori }
1901