xref: /openbmc/qemu/block.c (revision 6bbff9a0b495918309074ac60375be5f9dc868b3)
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"
25179a2c19Sblueswir1 #ifdef HOST_BSD
263990d09aSblueswir1 /* include native header before sys-queue.h */
273990d09aSblueswir1 #include <sys/queue.h>
283990d09aSblueswir1 #endif
293990d09aSblueswir1 
30faf07963Spbrook #include "qemu-common.h"
31376253ecSaliguori #include "monitor.h"
32ea2384d3Sbellard #include "block_int.h"
33fc01f7e7Sbellard 
34179a2c19Sblueswir1 #ifdef HOST_BSD
357674e7bfSbellard #include <sys/types.h>
367674e7bfSbellard #include <sys/stat.h>
377674e7bfSbellard #include <sys/ioctl.h>
38c5e97233Sblueswir1 #ifndef __DragonFly__
397674e7bfSbellard #include <sys/disk.h>
407674e7bfSbellard #endif
41c5e97233Sblueswir1 #endif
427674e7bfSbellard 
4349dc768dSaliguori #ifdef _WIN32
4449dc768dSaliguori #include <windows.h>
4549dc768dSaliguori #endif
4649dc768dSaliguori 
4783f64091Sbellard #define SECTOR_BITS 9
4883f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS)
493b0d4f61Sbellard 
5090765429Sbellard typedef struct BlockDriverAIOCBSync {
5190765429Sbellard     BlockDriverAIOCB common;
5290765429Sbellard     QEMUBH *bh;
5390765429Sbellard     int ret;
5490765429Sbellard } BlockDriverAIOCBSync;
5590765429Sbellard 
56ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
57ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
58ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
59ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
60ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
61ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
6283f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
6383f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
6483f64091Sbellard                         uint8_t *buf, int nb_sectors);
6583f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
6683f64091Sbellard                          const uint8_t *buf, int nb_sectors);
67ec530c81Sbellard 
687ee930d0Sblueswir1 BlockDriverState *bdrv_first;
697ee930d0Sblueswir1 
70ea2384d3Sbellard static BlockDriver *first_drv;
71ea2384d3Sbellard 
7283f64091Sbellard int path_is_absolute(const char *path)
7383f64091Sbellard {
7483f64091Sbellard     const char *p;
7521664424Sbellard #ifdef _WIN32
7621664424Sbellard     /* specific case for names like: "\\.\d:" */
7721664424Sbellard     if (*path == '/' || *path == '\\')
7821664424Sbellard         return 1;
7921664424Sbellard #endif
8083f64091Sbellard     p = strchr(path, ':');
8183f64091Sbellard     if (p)
8283f64091Sbellard         p++;
8383f64091Sbellard     else
8483f64091Sbellard         p = path;
853b9f94e1Sbellard #ifdef _WIN32
863b9f94e1Sbellard     return (*p == '/' || *p == '\\');
873b9f94e1Sbellard #else
883b9f94e1Sbellard     return (*p == '/');
893b9f94e1Sbellard #endif
9083f64091Sbellard }
9183f64091Sbellard 
9283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
9383f64091Sbellard    path to it by considering it is relative to base_path. URL are
9483f64091Sbellard    supported. */
9583f64091Sbellard void path_combine(char *dest, int dest_size,
9683f64091Sbellard                   const char *base_path,
9783f64091Sbellard                   const char *filename)
9883f64091Sbellard {
9983f64091Sbellard     const char *p, *p1;
10083f64091Sbellard     int len;
10183f64091Sbellard 
10283f64091Sbellard     if (dest_size <= 0)
10383f64091Sbellard         return;
10483f64091Sbellard     if (path_is_absolute(filename)) {
10583f64091Sbellard         pstrcpy(dest, dest_size, filename);
10683f64091Sbellard     } else {
10783f64091Sbellard         p = strchr(base_path, ':');
10883f64091Sbellard         if (p)
10983f64091Sbellard             p++;
11083f64091Sbellard         else
11183f64091Sbellard             p = base_path;
1123b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1133b9f94e1Sbellard #ifdef _WIN32
1143b9f94e1Sbellard         {
1153b9f94e1Sbellard             const char *p2;
1163b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1173b9f94e1Sbellard             if (!p1 || p2 > p1)
1183b9f94e1Sbellard                 p1 = p2;
1193b9f94e1Sbellard         }
1203b9f94e1Sbellard #endif
12183f64091Sbellard         if (p1)
12283f64091Sbellard             p1++;
12383f64091Sbellard         else
12483f64091Sbellard             p1 = base_path;
12583f64091Sbellard         if (p1 > p)
12683f64091Sbellard             p = p1;
12783f64091Sbellard         len = p - base_path;
12883f64091Sbellard         if (len > dest_size - 1)
12983f64091Sbellard             len = dest_size - 1;
13083f64091Sbellard         memcpy(dest, base_path, len);
13183f64091Sbellard         dest[len] = '\0';
13283f64091Sbellard         pstrcat(dest, dest_size, filename);
13383f64091Sbellard     }
13483f64091Sbellard }
13583f64091Sbellard 
13683f64091Sbellard 
1379596ebb7Spbrook static void bdrv_register(BlockDriver *bdrv)
138ea2384d3Sbellard {
139ce1a14dcSpbrook     if (!bdrv->bdrv_aio_read) {
14083f64091Sbellard         /* add AIO emulation layer */
14183f64091Sbellard         bdrv->bdrv_aio_read = bdrv_aio_read_em;
14283f64091Sbellard         bdrv->bdrv_aio_write = bdrv_aio_write_em;
14383f64091Sbellard         bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
14490765429Sbellard         bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
145eda578e5Saliguori     } else if (!bdrv->bdrv_read) {
14683f64091Sbellard         /* add synchronous IO emulation layer */
14783f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
14883f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
14983f64091Sbellard     }
150*6bbff9a0Saliguori     aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
151ea2384d3Sbellard     bdrv->next = first_drv;
152ea2384d3Sbellard     first_drv = bdrv;
153ea2384d3Sbellard }
154b338082bSbellard 
155b338082bSbellard /* create a new block device (by default it is empty) */
156b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
157fc01f7e7Sbellard {
158b338082bSbellard     BlockDriverState **pbs, *bs;
159b338082bSbellard 
160b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
161b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
162ea2384d3Sbellard     if (device_name[0] != '\0') {
163b338082bSbellard         /* insert at the end */
164b338082bSbellard         pbs = &bdrv_first;
165b338082bSbellard         while (*pbs != NULL)
166b338082bSbellard             pbs = &(*pbs)->next;
167b338082bSbellard         *pbs = bs;
168ea2384d3Sbellard     }
169b338082bSbellard     return bs;
170b338082bSbellard }
171b338082bSbellard 
172ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
173ea2384d3Sbellard {
174ea2384d3Sbellard     BlockDriver *drv1;
175ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
176ea2384d3Sbellard         if (!strcmp(drv1->format_name, format_name))
177ea2384d3Sbellard             return drv1;
178ea2384d3Sbellard     }
179ea2384d3Sbellard     return NULL;
180ea2384d3Sbellard }
181ea2384d3Sbellard 
182ea2384d3Sbellard int bdrv_create(BlockDriver *drv,
183ea2384d3Sbellard                 const char *filename, int64_t size_in_sectors,
184ea2384d3Sbellard                 const char *backing_file, int flags)
185ea2384d3Sbellard {
186ea2384d3Sbellard     if (!drv->bdrv_create)
187ea2384d3Sbellard         return -ENOTSUP;
188ea2384d3Sbellard     return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
189ea2384d3Sbellard }
190ea2384d3Sbellard 
191d5249393Sbellard #ifdef _WIN32
19295389c86Sbellard void get_tmp_filename(char *filename, int size)
193d5249393Sbellard {
1943b9f94e1Sbellard     char temp_dir[MAX_PATH];
1953b9f94e1Sbellard 
1963b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
1973b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
198d5249393Sbellard }
199d5249393Sbellard #else
20095389c86Sbellard void get_tmp_filename(char *filename, int size)
201ea2384d3Sbellard {
202ea2384d3Sbellard     int fd;
2037ccfb2ebSblueswir1     const char *tmpdir;
204d5249393Sbellard     /* XXX: race condition possible */
2050badc1eeSaurel32     tmpdir = getenv("TMPDIR");
2060badc1eeSaurel32     if (!tmpdir)
2070badc1eeSaurel32         tmpdir = "/tmp";
2080badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
209ea2384d3Sbellard     fd = mkstemp(filename);
210ea2384d3Sbellard     close(fd);
211ea2384d3Sbellard }
212d5249393Sbellard #endif
213ea2384d3Sbellard 
21419cb3738Sbellard #ifdef _WIN32
215f45512feSbellard static int is_windows_drive_prefix(const char *filename)
216f45512feSbellard {
217f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
218f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
219f45512feSbellard             filename[1] == ':');
220f45512feSbellard }
221f45512feSbellard 
22219cb3738Sbellard static int is_windows_drive(const char *filename)
22319cb3738Sbellard {
224f45512feSbellard     if (is_windows_drive_prefix(filename) &&
225f45512feSbellard         filename[2] == '\0')
22619cb3738Sbellard         return 1;
22719cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
22819cb3738Sbellard         strstart(filename, "//./", NULL))
22919cb3738Sbellard         return 1;
23019cb3738Sbellard     return 0;
23119cb3738Sbellard }
23219cb3738Sbellard #endif
23319cb3738Sbellard 
23483f64091Sbellard static BlockDriver *find_protocol(const char *filename)
23583f64091Sbellard {
23683f64091Sbellard     BlockDriver *drv1;
23783f64091Sbellard     char protocol[128];
23883f64091Sbellard     int len;
23983f64091Sbellard     const char *p;
24019cb3738Sbellard 
24119cb3738Sbellard #ifdef _WIN32
242f45512feSbellard     if (is_windows_drive(filename) ||
243f45512feSbellard         is_windows_drive_prefix(filename))
24419cb3738Sbellard         return &bdrv_raw;
24519cb3738Sbellard #endif
24683f64091Sbellard     p = strchr(filename, ':');
24783f64091Sbellard     if (!p)
24883f64091Sbellard         return &bdrv_raw;
24983f64091Sbellard     len = p - filename;
25083f64091Sbellard     if (len > sizeof(protocol) - 1)
25183f64091Sbellard         len = sizeof(protocol) - 1;
25283f64091Sbellard     memcpy(protocol, filename, len);
25383f64091Sbellard     protocol[len] = '\0';
25483f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
25583f64091Sbellard         if (drv1->protocol_name &&
25683f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
25783f64091Sbellard             return drv1;
25883f64091Sbellard     }
25983f64091Sbellard     return NULL;
26083f64091Sbellard }
26183f64091Sbellard 
2627674e7bfSbellard /* XXX: force raw format if block or character device ? It would
2637674e7bfSbellard    simplify the BSD case */
264ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
265ea2384d3Sbellard {
26683f64091Sbellard     int ret, score, score_max;
267ea2384d3Sbellard     BlockDriver *drv1, *drv;
26883f64091Sbellard     uint8_t buf[2048];
26983f64091Sbellard     BlockDriverState *bs;
270ea2384d3Sbellard 
27119cb3738Sbellard     /* detect host devices. By convention, /dev/cdrom[N] is always
27219cb3738Sbellard        recognized as a host CDROM */
27319cb3738Sbellard     if (strstart(filename, "/dev/cdrom", NULL))
27419cb3738Sbellard         return &bdrv_host_device;
27519cb3738Sbellard #ifdef _WIN32
27619cb3738Sbellard     if (is_windows_drive(filename))
27719cb3738Sbellard         return &bdrv_host_device;
27819cb3738Sbellard #else
27919cb3738Sbellard     {
28019cb3738Sbellard         struct stat st;
28119cb3738Sbellard         if (stat(filename, &st) >= 0 &&
28219cb3738Sbellard             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
28319cb3738Sbellard             return &bdrv_host_device;
28419cb3738Sbellard         }
28519cb3738Sbellard     }
28619cb3738Sbellard #endif
28719cb3738Sbellard 
28883f64091Sbellard     drv = find_protocol(filename);
28919cb3738Sbellard     /* no need to test disk image formats for vvfat */
29083f64091Sbellard     if (drv == &bdrv_vvfat)
29183f64091Sbellard         return drv;
29283f64091Sbellard 
29383f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
29483f64091Sbellard     if (ret < 0)
2957674e7bfSbellard         return NULL;
29683f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
29783f64091Sbellard     bdrv_delete(bs);
298ea2384d3Sbellard     if (ret < 0) {
299ea2384d3Sbellard         return NULL;
300ea2384d3Sbellard     }
301ea2384d3Sbellard 
302ea2384d3Sbellard     score_max = 0;
303ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
30483f64091Sbellard         if (drv1->bdrv_probe) {
305ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
306ea2384d3Sbellard             if (score > score_max) {
307ea2384d3Sbellard                 score_max = score;
308ea2384d3Sbellard                 drv = drv1;
309ea2384d3Sbellard             }
310ea2384d3Sbellard         }
31183f64091Sbellard     }
312ea2384d3Sbellard     return drv;
313ea2384d3Sbellard }
314ea2384d3Sbellard 
31583f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
316b338082bSbellard {
31783f64091Sbellard     BlockDriverState *bs;
31883f64091Sbellard     int ret;
3193b0d4f61Sbellard 
32083f64091Sbellard     bs = bdrv_new("");
32183f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
32283f64091Sbellard     if (ret < 0) {
32383f64091Sbellard         bdrv_delete(bs);
32483f64091Sbellard         return ret;
3253b0d4f61Sbellard     }
32671d0770cSaliguori     bs->growable = 1;
32783f64091Sbellard     *pbs = bs;
32883f64091Sbellard     return 0;
3293b0d4f61Sbellard }
3303b0d4f61Sbellard 
33183f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
33283f64091Sbellard {
33383f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
334ea2384d3Sbellard }
335ea2384d3Sbellard 
33683f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
337ea2384d3Sbellard                BlockDriver *drv)
338ea2384d3Sbellard {
33983f64091Sbellard     int ret, open_flags;
340eb5c851fSths     char tmp_filename[PATH_MAX];
341eb5c851fSths     char backing_filename[PATH_MAX];
342fc01f7e7Sbellard 
3430849bf08Sbellard     bs->read_only = 0;
344ea2384d3Sbellard     bs->is_temporary = 0;
345ea2384d3Sbellard     bs->encrypted = 0;
346c0f4ce77Saliguori     bs->valid_key = 0;
34733e3963eSbellard 
34883f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
349ea2384d3Sbellard         BlockDriverState *bs1;
350ea2384d3Sbellard         int64_t total_size;
3517c96d46eSaliguori         int is_protocol = 0;
35233e3963eSbellard 
353ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
354ea2384d3Sbellard            instead of opening 'filename' directly */
355ea2384d3Sbellard 
356ea2384d3Sbellard         /* if there is a backing file, use it */
357ea2384d3Sbellard         bs1 = bdrv_new("");
35851d7c00cSaliguori         ret = bdrv_open(bs1, filename, 0);
35951d7c00cSaliguori         if (ret < 0) {
360ea2384d3Sbellard             bdrv_delete(bs1);
36151d7c00cSaliguori             return ret;
362ea2384d3Sbellard         }
36383f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
3647c96d46eSaliguori 
3657c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
3667c96d46eSaliguori             is_protocol = 1;
3677c96d46eSaliguori 
368ea2384d3Sbellard         bdrv_delete(bs1);
369ea2384d3Sbellard 
370ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
3717c96d46eSaliguori 
3727c96d46eSaliguori         /* Real path is meaningless for protocols */
3737c96d46eSaliguori         if (is_protocol)
3747c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
3757c96d46eSaliguori                      "%s", filename);
3767c96d46eSaliguori         else
377a817d936Sbellard             realpath(filename, backing_filename);
3787c96d46eSaliguori 
37951d7c00cSaliguori         ret = bdrv_create(&bdrv_qcow2, tmp_filename,
38051d7c00cSaliguori                           total_size, backing_filename, 0);
38151d7c00cSaliguori         if (ret < 0) {
38251d7c00cSaliguori             return ret;
383ea2384d3Sbellard         }
384ea2384d3Sbellard         filename = tmp_filename;
385ea2384d3Sbellard         bs->is_temporary = 1;
386ea2384d3Sbellard     }
387ea2384d3Sbellard 
388ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
38983f64091Sbellard     if (flags & BDRV_O_FILE) {
39083f64091Sbellard         drv = find_protocol(filename);
39151d7c00cSaliguori     } else if (!drv) {
392ea2384d3Sbellard         drv = find_image_format(filename);
393ea2384d3Sbellard     }
39451d7c00cSaliguori     if (!drv) {
39551d7c00cSaliguori         ret = -ENOENT;
39651d7c00cSaliguori         goto unlink_and_fail;
39783f64091Sbellard     }
398ea2384d3Sbellard     bs->drv = drv;
399ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
40083f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
40183f64091Sbellard        RDONLY as fallback */
40283f64091Sbellard     if (!(flags & BDRV_O_FILE))
4039f7965c7Saliguori         open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
40483f64091Sbellard     else
40583f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
40683f64091Sbellard     ret = drv->bdrv_open(bs, filename, open_flags);
407a0a83536Saurel32     if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
4089f7965c7Saliguori         ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
40983f64091Sbellard         bs->read_only = 1;
41083f64091Sbellard     }
411ea2384d3Sbellard     if (ret < 0) {
412ea2384d3Sbellard         qemu_free(bs->opaque);
4136b21b973Sbellard         bs->opaque = NULL;
4146b21b973Sbellard         bs->drv = NULL;
41551d7c00cSaliguori     unlink_and_fail:
41651d7c00cSaliguori         if (bs->is_temporary)
41751d7c00cSaliguori             unlink(filename);
41883f64091Sbellard         return ret;
419ea2384d3Sbellard     }
420d15a771dSbellard     if (drv->bdrv_getlength) {
421d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
422d15a771dSbellard     }
423ea2384d3Sbellard #ifndef _WIN32
424ea2384d3Sbellard     if (bs->is_temporary) {
425ea2384d3Sbellard         unlink(filename);
42633e3963eSbellard     }
42767b915a5Sbellard #endif
42883f64091Sbellard     if (bs->backing_file[0] != '\0') {
429ea2384d3Sbellard         /* if there is a backing file, use it */
430ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
43183f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
43283f64091Sbellard                      filename, bs->backing_file);
43351d7c00cSaliguori         ret = bdrv_open(bs->backing_hd, backing_filename, open_flags);
43451d7c00cSaliguori         if (ret < 0) {
43551d7c00cSaliguori             bdrv_close(bs);
43651d7c00cSaliguori             return ret;
43751d7c00cSaliguori         }
438ea2384d3Sbellard     }
43933e3963eSbellard 
440bb5fc20fSaliguori     if (!bdrv_key_required(bs)) {
441b338082bSbellard         /* call the change callback */
44219cb3738Sbellard         bs->media_changed = 1;
443b338082bSbellard         if (bs->change_cb)
444b338082bSbellard             bs->change_cb(bs->change_opaque);
445bb5fc20fSaliguori     }
446b338082bSbellard     return 0;
447fc01f7e7Sbellard }
448fc01f7e7Sbellard 
449fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
450fc01f7e7Sbellard {
45119cb3738Sbellard     if (bs->drv) {
452ea2384d3Sbellard         if (bs->backing_hd)
453ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
454ea2384d3Sbellard         bs->drv->bdrv_close(bs);
455ea2384d3Sbellard         qemu_free(bs->opaque);
456ea2384d3Sbellard #ifdef _WIN32
457ea2384d3Sbellard         if (bs->is_temporary) {
458ea2384d3Sbellard             unlink(bs->filename);
459ea2384d3Sbellard         }
46067b915a5Sbellard #endif
461ea2384d3Sbellard         bs->opaque = NULL;
462ea2384d3Sbellard         bs->drv = NULL;
463b338082bSbellard 
464b338082bSbellard         /* call the change callback */
46519cb3738Sbellard         bs->media_changed = 1;
466b338082bSbellard         if (bs->change_cb)
467b338082bSbellard             bs->change_cb(bs->change_opaque);
468b338082bSbellard     }
469b338082bSbellard }
470b338082bSbellard 
471b338082bSbellard void bdrv_delete(BlockDriverState *bs)
472b338082bSbellard {
47334c6f050Saurel32     BlockDriverState **pbs;
47434c6f050Saurel32 
47534c6f050Saurel32     pbs = &bdrv_first;
47634c6f050Saurel32     while (*pbs != bs && *pbs != NULL)
47734c6f050Saurel32         pbs = &(*pbs)->next;
47834c6f050Saurel32     if (*pbs == bs)
47934c6f050Saurel32         *pbs = bs->next;
48034c6f050Saurel32 
481b338082bSbellard     bdrv_close(bs);
482b338082bSbellard     qemu_free(bs);
483fc01f7e7Sbellard }
484fc01f7e7Sbellard 
48533e3963eSbellard /* commit COW file into the raw image */
48633e3963eSbellard int bdrv_commit(BlockDriverState *bs)
48733e3963eSbellard {
48819cb3738Sbellard     BlockDriver *drv = bs->drv;
48983f64091Sbellard     int64_t i, total_sectors;
490ea2384d3Sbellard     int n, j;
491ea2384d3Sbellard     unsigned char sector[512];
49233e3963eSbellard 
49319cb3738Sbellard     if (!drv)
49419cb3738Sbellard         return -ENOMEDIUM;
49533e3963eSbellard 
49633e3963eSbellard     if (bs->read_only) {
497ea2384d3Sbellard 	return -EACCES;
49833e3963eSbellard     }
49933e3963eSbellard 
500ea2384d3Sbellard     if (!bs->backing_hd) {
501ea2384d3Sbellard 	return -ENOTSUP;
502ea2384d3Sbellard     }
503ea2384d3Sbellard 
50483f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
50583f64091Sbellard     for (i = 0; i < total_sectors;) {
50619cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
507ea2384d3Sbellard             for(j = 0; j < n; j++) {
50833e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
509ea2384d3Sbellard                     return -EIO;
51033e3963eSbellard                 }
51133e3963eSbellard 
512ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
513ea2384d3Sbellard                     return -EIO;
51433e3963eSbellard                 }
515ea2384d3Sbellard                 i++;
516ea2384d3Sbellard 	    }
517ea2384d3Sbellard 	} else {
518ea2384d3Sbellard             i += n;
51933e3963eSbellard         }
52033e3963eSbellard     }
52195389c86Sbellard 
52219cb3738Sbellard     if (drv->bdrv_make_empty)
52319cb3738Sbellard 	return drv->bdrv_make_empty(bs);
52495389c86Sbellard 
52533e3963eSbellard     return 0;
52633e3963eSbellard }
52733e3963eSbellard 
52871d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
52971d0770cSaliguori                                    size_t size)
53071d0770cSaliguori {
53171d0770cSaliguori     int64_t len;
53271d0770cSaliguori 
53371d0770cSaliguori     if (!bdrv_is_inserted(bs))
53471d0770cSaliguori         return -ENOMEDIUM;
53571d0770cSaliguori 
53671d0770cSaliguori     if (bs->growable)
53771d0770cSaliguori         return 0;
53871d0770cSaliguori 
53971d0770cSaliguori     len = bdrv_getlength(bs);
54071d0770cSaliguori 
54171d0770cSaliguori     if ((offset + size) > len)
54271d0770cSaliguori         return -EIO;
54371d0770cSaliguori 
54471d0770cSaliguori     return 0;
54571d0770cSaliguori }
54671d0770cSaliguori 
54771d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
54871d0770cSaliguori                               int nb_sectors)
54971d0770cSaliguori {
55071d0770cSaliguori     int64_t offset;
55171d0770cSaliguori 
55271d0770cSaliguori     /* Deal with byte accesses */
55371d0770cSaliguori     if (sector_num < 0)
55471d0770cSaliguori         offset = -sector_num;
55571d0770cSaliguori     else
55671d0770cSaliguori         offset = sector_num * 512;
55771d0770cSaliguori 
55871d0770cSaliguori     return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
55971d0770cSaliguori }
56071d0770cSaliguori 
56119cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
562fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
563fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
564fc01f7e7Sbellard {
565ea2384d3Sbellard     BlockDriver *drv = bs->drv;
566fc01f7e7Sbellard 
56719cb3738Sbellard     if (!drv)
56819cb3738Sbellard         return -ENOMEDIUM;
56971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
57071d0770cSaliguori         return -EIO;
571b338082bSbellard 
57283f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
57383f64091Sbellard }
574fc01f7e7Sbellard 
57519cb3738Sbellard /* Return < 0 if error. Important errors are:
57619cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
57719cb3738Sbellard   -ENOMEDIUM   No media inserted.
57819cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
57919cb3738Sbellard   -EACCES      Trying to write a read-only device
58019cb3738Sbellard */
581fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
582fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
583fc01f7e7Sbellard {
58483f64091Sbellard     BlockDriver *drv = bs->drv;
58519cb3738Sbellard     if (!bs->drv)
58619cb3738Sbellard         return -ENOMEDIUM;
5870849bf08Sbellard     if (bs->read_only)
58819cb3738Sbellard         return -EACCES;
58971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
59071d0770cSaliguori         return -EIO;
59171d0770cSaliguori 
59283f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
59383f64091Sbellard }
59483f64091Sbellard 
595eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
596eda578e5Saliguori                void *buf, int count1)
59783f64091Sbellard {
59883f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
59983f64091Sbellard     int len, nb_sectors, count;
60083f64091Sbellard     int64_t sector_num;
60183f64091Sbellard 
60283f64091Sbellard     count = count1;
60383f64091Sbellard     /* first read to align to sector start */
60483f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
60583f64091Sbellard     if (len > count)
60683f64091Sbellard         len = count;
60783f64091Sbellard     sector_num = offset >> SECTOR_BITS;
60883f64091Sbellard     if (len > 0) {
60983f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
61083f64091Sbellard             return -EIO;
61183f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
61283f64091Sbellard         count -= len;
61383f64091Sbellard         if (count == 0)
61483f64091Sbellard             return count1;
61583f64091Sbellard         sector_num++;
61683f64091Sbellard         buf += len;
61783f64091Sbellard     }
61883f64091Sbellard 
61983f64091Sbellard     /* read the sectors "in place" */
62083f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
62183f64091Sbellard     if (nb_sectors > 0) {
62283f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
62383f64091Sbellard             return -EIO;
62483f64091Sbellard         sector_num += nb_sectors;
62583f64091Sbellard         len = nb_sectors << SECTOR_BITS;
62683f64091Sbellard         buf += len;
62783f64091Sbellard         count -= len;
62883f64091Sbellard     }
62983f64091Sbellard 
63083f64091Sbellard     /* add data from the last sector */
63183f64091Sbellard     if (count > 0) {
63283f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
63383f64091Sbellard             return -EIO;
63483f64091Sbellard         memcpy(buf, tmp_buf, count);
63583f64091Sbellard     }
63683f64091Sbellard     return count1;
63783f64091Sbellard }
63883f64091Sbellard 
639eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
640eda578e5Saliguori                 const void *buf, int count1)
64183f64091Sbellard {
64283f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
64383f64091Sbellard     int len, nb_sectors, count;
64483f64091Sbellard     int64_t sector_num;
64583f64091Sbellard 
64683f64091Sbellard     count = count1;
64783f64091Sbellard     /* first write to align to sector start */
64883f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
64983f64091Sbellard     if (len > count)
65083f64091Sbellard         len = count;
65183f64091Sbellard     sector_num = offset >> SECTOR_BITS;
65283f64091Sbellard     if (len > 0) {
65383f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
65483f64091Sbellard             return -EIO;
65583f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
65683f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
65783f64091Sbellard             return -EIO;
65883f64091Sbellard         count -= len;
65983f64091Sbellard         if (count == 0)
66083f64091Sbellard             return count1;
66183f64091Sbellard         sector_num++;
66283f64091Sbellard         buf += len;
66383f64091Sbellard     }
66483f64091Sbellard 
66583f64091Sbellard     /* write the sectors "in place" */
66683f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
66783f64091Sbellard     if (nb_sectors > 0) {
66883f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
66983f64091Sbellard             return -EIO;
67083f64091Sbellard         sector_num += nb_sectors;
67183f64091Sbellard         len = nb_sectors << SECTOR_BITS;
67283f64091Sbellard         buf += len;
67383f64091Sbellard         count -= len;
67483f64091Sbellard     }
67583f64091Sbellard 
67683f64091Sbellard     /* add data from the last sector */
67783f64091Sbellard     if (count > 0) {
67883f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
67983f64091Sbellard             return -EIO;
68083f64091Sbellard         memcpy(tmp_buf, buf, count);
68183f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
68283f64091Sbellard             return -EIO;
68383f64091Sbellard     }
68483f64091Sbellard     return count1;
68583f64091Sbellard }
68683f64091Sbellard 
68783f64091Sbellard /**
68883f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
68983f64091Sbellard  */
69083f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
69183f64091Sbellard {
69283f64091Sbellard     BlockDriver *drv = bs->drv;
69383f64091Sbellard     if (!drv)
69419cb3738Sbellard         return -ENOMEDIUM;
69583f64091Sbellard     if (!drv->bdrv_truncate)
69683f64091Sbellard         return -ENOTSUP;
69783f64091Sbellard     return drv->bdrv_truncate(bs, offset);
69883f64091Sbellard }
69983f64091Sbellard 
70083f64091Sbellard /**
70183f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
70283f64091Sbellard  */
70383f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
70483f64091Sbellard {
70583f64091Sbellard     BlockDriver *drv = bs->drv;
70683f64091Sbellard     if (!drv)
70719cb3738Sbellard         return -ENOMEDIUM;
70883f64091Sbellard     if (!drv->bdrv_getlength) {
70983f64091Sbellard         /* legacy mode */
71083f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
71183f64091Sbellard     }
71283f64091Sbellard     return drv->bdrv_getlength(bs);
713fc01f7e7Sbellard }
714fc01f7e7Sbellard 
71519cb3738Sbellard /* return 0 as number of sectors if no device present or error */
71696b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
717fc01f7e7Sbellard {
71819cb3738Sbellard     int64_t length;
71919cb3738Sbellard     length = bdrv_getlength(bs);
72019cb3738Sbellard     if (length < 0)
72119cb3738Sbellard         length = 0;
72219cb3738Sbellard     else
72319cb3738Sbellard         length = length >> SECTOR_BITS;
72419cb3738Sbellard     *nb_sectors_ptr = length;
725fc01f7e7Sbellard }
726cf98951bSbellard 
727f3d54fc4Saliguori struct partition {
728f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
729f3d54fc4Saliguori         uint8_t head;               /* starting head */
730f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
731f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
732f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
733f3d54fc4Saliguori         uint8_t end_head;           /* end head */
734f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
735f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
736f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
737f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
738f3d54fc4Saliguori } __attribute__((packed));
739f3d54fc4Saliguori 
740f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
741f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
742f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
743f3d54fc4Saliguori {
744f3d54fc4Saliguori     uint8_t buf[512];
745f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
746f3d54fc4Saliguori     struct partition *p;
747f3d54fc4Saliguori     uint32_t nr_sects;
748a38131b6Sblueswir1     uint64_t nb_sectors;
749f3d54fc4Saliguori 
750f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
751f3d54fc4Saliguori 
752f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
753f3d54fc4Saliguori     if (ret < 0)
754f3d54fc4Saliguori         return -1;
755f3d54fc4Saliguori     /* test msdos magic */
756f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
757f3d54fc4Saliguori         return -1;
758f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
759f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
760f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
761f3d54fc4Saliguori         if (nr_sects && p->end_head) {
762f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
763f3d54fc4Saliguori                a cylinder boundary */
764f3d54fc4Saliguori             heads = p->end_head + 1;
765f3d54fc4Saliguori             sectors = p->end_sector & 63;
766f3d54fc4Saliguori             if (sectors == 0)
767f3d54fc4Saliguori                 continue;
768f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
769f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
770f3d54fc4Saliguori                 continue;
771f3d54fc4Saliguori             *pheads = heads;
772f3d54fc4Saliguori             *psectors = sectors;
773f3d54fc4Saliguori             *pcylinders = cylinders;
774f3d54fc4Saliguori #if 0
775f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
776f3d54fc4Saliguori                    cylinders, heads, sectors);
777f3d54fc4Saliguori #endif
778f3d54fc4Saliguori             return 0;
779f3d54fc4Saliguori         }
780f3d54fc4Saliguori     }
781f3d54fc4Saliguori     return -1;
782f3d54fc4Saliguori }
783f3d54fc4Saliguori 
784f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
785f3d54fc4Saliguori {
786f3d54fc4Saliguori     int translation, lba_detected = 0;
787f3d54fc4Saliguori     int cylinders, heads, secs;
788a38131b6Sblueswir1     uint64_t nb_sectors;
789f3d54fc4Saliguori 
790f3d54fc4Saliguori     /* if a geometry hint is available, use it */
791f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
792f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
793f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
794f3d54fc4Saliguori     if (cylinders != 0) {
795f3d54fc4Saliguori         *pcyls = cylinders;
796f3d54fc4Saliguori         *pheads = heads;
797f3d54fc4Saliguori         *psecs = secs;
798f3d54fc4Saliguori     } else {
799f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
800f3d54fc4Saliguori             if (heads > 16) {
801f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
802f3d54fc4Saliguori                    translation was active, so the default
803f3d54fc4Saliguori                    hardware geometry is OK */
804f3d54fc4Saliguori                 lba_detected = 1;
805f3d54fc4Saliguori                 goto default_geometry;
806f3d54fc4Saliguori             } else {
807f3d54fc4Saliguori                 *pcyls = cylinders;
808f3d54fc4Saliguori                 *pheads = heads;
809f3d54fc4Saliguori                 *psecs = secs;
810f3d54fc4Saliguori                 /* disable any translation to be in sync with
811f3d54fc4Saliguori                    the logical geometry */
812f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
813f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
814f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
815f3d54fc4Saliguori                 }
816f3d54fc4Saliguori             }
817f3d54fc4Saliguori         } else {
818f3d54fc4Saliguori         default_geometry:
819f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
820f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
821f3d54fc4Saliguori 
822f3d54fc4Saliguori             if (cylinders > 16383)
823f3d54fc4Saliguori                 cylinders = 16383;
824f3d54fc4Saliguori             else if (cylinders < 2)
825f3d54fc4Saliguori                 cylinders = 2;
826f3d54fc4Saliguori             *pcyls = cylinders;
827f3d54fc4Saliguori             *pheads = 16;
828f3d54fc4Saliguori             *psecs = 63;
829f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
830f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
831f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
832f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
833f3d54fc4Saliguori                 } else {
834f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
835f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
836f3d54fc4Saliguori                 }
837f3d54fc4Saliguori             }
838f3d54fc4Saliguori         }
839f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
840f3d54fc4Saliguori     }
841f3d54fc4Saliguori }
842f3d54fc4Saliguori 
843b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
844b338082bSbellard                             int cyls, int heads, int secs)
845b338082bSbellard {
846b338082bSbellard     bs->cyls = cyls;
847b338082bSbellard     bs->heads = heads;
848b338082bSbellard     bs->secs = secs;
849b338082bSbellard }
850b338082bSbellard 
851b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
852b338082bSbellard {
853b338082bSbellard     bs->type = type;
854b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
855b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
856b338082bSbellard }
857b338082bSbellard 
85846d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
85946d4767dSbellard {
86046d4767dSbellard     bs->translation = translation;
86146d4767dSbellard }
86246d4767dSbellard 
863b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
864b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
865b338082bSbellard {
866b338082bSbellard     *pcyls = bs->cyls;
867b338082bSbellard     *pheads = bs->heads;
868b338082bSbellard     *psecs = bs->secs;
869b338082bSbellard }
870b338082bSbellard 
871b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
872b338082bSbellard {
873b338082bSbellard     return bs->type;
874b338082bSbellard }
875b338082bSbellard 
87646d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
87746d4767dSbellard {
87846d4767dSbellard     return bs->translation;
87946d4767dSbellard }
88046d4767dSbellard 
881b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
882b338082bSbellard {
883b338082bSbellard     return bs->removable;
884b338082bSbellard }
885b338082bSbellard 
886b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
887b338082bSbellard {
888b338082bSbellard     return bs->read_only;
889b338082bSbellard }
890b338082bSbellard 
891985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
892985a03b0Sths {
893985a03b0Sths     return bs->sg;
894985a03b0Sths }
895985a03b0Sths 
89619cb3738Sbellard /* XXX: no longer used */
897b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
898b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
899b338082bSbellard {
900b338082bSbellard     bs->change_cb = change_cb;
901b338082bSbellard     bs->change_opaque = opaque;
902b338082bSbellard }
903b338082bSbellard 
904ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
905ea2384d3Sbellard {
906ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
907ea2384d3Sbellard         return 1;
908ea2384d3Sbellard     return bs->encrypted;
909ea2384d3Sbellard }
910ea2384d3Sbellard 
911c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
912c0f4ce77Saliguori {
913c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
914c0f4ce77Saliguori 
915c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
916c0f4ce77Saliguori         return 1;
917c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
918c0f4ce77Saliguori }
919c0f4ce77Saliguori 
920ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
921ea2384d3Sbellard {
922ea2384d3Sbellard     int ret;
923ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
924ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
925ea2384d3Sbellard         if (ret < 0)
926ea2384d3Sbellard             return ret;
927ea2384d3Sbellard         if (!bs->encrypted)
928ea2384d3Sbellard             return 0;
929ea2384d3Sbellard     }
930ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
931ea2384d3Sbellard         return -1;
932c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
933bb5fc20fSaliguori     if (ret < 0) {
934bb5fc20fSaliguori         bs->valid_key = 0;
935bb5fc20fSaliguori     } else if (!bs->valid_key) {
936bb5fc20fSaliguori         bs->valid_key = 1;
937bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
938bb5fc20fSaliguori         bs->media_changed = 1;
939bb5fc20fSaliguori         if (bs->change_cb)
940bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
941bb5fc20fSaliguori     }
942c0f4ce77Saliguori     return ret;
943ea2384d3Sbellard }
944ea2384d3Sbellard 
945ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
946ea2384d3Sbellard {
94719cb3738Sbellard     if (!bs->drv) {
948ea2384d3Sbellard         buf[0] = '\0';
949ea2384d3Sbellard     } else {
950ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
951ea2384d3Sbellard     }
952ea2384d3Sbellard }
953ea2384d3Sbellard 
954ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
955ea2384d3Sbellard                          void *opaque)
956ea2384d3Sbellard {
957ea2384d3Sbellard     BlockDriver *drv;
958ea2384d3Sbellard 
959ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
960ea2384d3Sbellard         it(opaque, drv->format_name);
961ea2384d3Sbellard     }
962ea2384d3Sbellard }
963ea2384d3Sbellard 
964b338082bSbellard BlockDriverState *bdrv_find(const char *name)
965b338082bSbellard {
966b338082bSbellard     BlockDriverState *bs;
967b338082bSbellard 
968b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
969b338082bSbellard         if (!strcmp(name, bs->device_name))
970b338082bSbellard             return bs;
971b338082bSbellard     }
972b338082bSbellard     return NULL;
973b338082bSbellard }
974b338082bSbellard 
97551de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
97681d0912dSbellard {
97781d0912dSbellard     BlockDriverState *bs;
97881d0912dSbellard 
97981d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
98051de9760Saliguori         it(opaque, bs);
98181d0912dSbellard     }
98281d0912dSbellard }
98381d0912dSbellard 
984ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
985ea2384d3Sbellard {
986ea2384d3Sbellard     return bs->device_name;
987ea2384d3Sbellard }
988ea2384d3Sbellard 
9897a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
9907a6cba61Spbrook {
9917a6cba61Spbrook     if (bs->drv->bdrv_flush)
9927a6cba61Spbrook         bs->drv->bdrv_flush(bs);
9937a6cba61Spbrook     if (bs->backing_hd)
9947a6cba61Spbrook         bdrv_flush(bs->backing_hd);
9957a6cba61Spbrook }
9967a6cba61Spbrook 
997c6ca28d6Saliguori void bdrv_flush_all(void)
998c6ca28d6Saliguori {
999c6ca28d6Saliguori     BlockDriverState *bs;
1000c6ca28d6Saliguori 
1001c6ca28d6Saliguori     for (bs = bdrv_first; bs != NULL; bs = bs->next)
1002c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
1003c6ca28d6Saliguori             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1004c6ca28d6Saliguori             bdrv_flush(bs);
1005c6ca28d6Saliguori }
1006c6ca28d6Saliguori 
1007f58c7b35Sths /*
1008f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1009f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1010f58c7b35Sths  * hence all their sectors are reported as allocated.
1011f58c7b35Sths  *
1012f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1013f58c7b35Sths  * the specified sector) that are known to be in the same
1014f58c7b35Sths  * allocated/unallocated state.
1015f58c7b35Sths  *
1016f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1017f58c7b35Sths  */
1018f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1019f58c7b35Sths 	int *pnum)
1020f58c7b35Sths {
1021f58c7b35Sths     int64_t n;
1022f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1023f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1024f58c7b35Sths             *pnum = 0;
1025f58c7b35Sths             return 0;
1026f58c7b35Sths         }
1027f58c7b35Sths         n = bs->total_sectors - sector_num;
1028f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1029f58c7b35Sths         return 1;
1030f58c7b35Sths     }
1031f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1032f58c7b35Sths }
1033f58c7b35Sths 
1034376253ecSaliguori void bdrv_info(Monitor *mon)
1035b338082bSbellard {
1036b338082bSbellard     BlockDriverState *bs;
1037b338082bSbellard 
1038b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1039376253ecSaliguori         monitor_printf(mon, "%s:", bs->device_name);
1040376253ecSaliguori         monitor_printf(mon, " type=");
1041b338082bSbellard         switch(bs->type) {
1042b338082bSbellard         case BDRV_TYPE_HD:
1043376253ecSaliguori             monitor_printf(mon, "hd");
1044b338082bSbellard             break;
1045b338082bSbellard         case BDRV_TYPE_CDROM:
1046376253ecSaliguori             monitor_printf(mon, "cdrom");
1047b338082bSbellard             break;
1048b338082bSbellard         case BDRV_TYPE_FLOPPY:
1049376253ecSaliguori             monitor_printf(mon, "floppy");
1050b338082bSbellard             break;
1051b338082bSbellard         }
1052376253ecSaliguori         monitor_printf(mon, " removable=%d", bs->removable);
1053b338082bSbellard         if (bs->removable) {
1054376253ecSaliguori             monitor_printf(mon, " locked=%d", bs->locked);
1055b338082bSbellard         }
105619cb3738Sbellard         if (bs->drv) {
1057376253ecSaliguori             monitor_printf(mon, " file=");
1058376253ecSaliguori             monitor_print_filename(mon, bs->filename);
1059fef30743Sths             if (bs->backing_file[0] != '\0') {
1060376253ecSaliguori                 monitor_printf(mon, " backing_file=");
1061376253ecSaliguori                 monitor_print_filename(mon, bs->backing_file);
1062fef30743Sths             }
1063376253ecSaliguori             monitor_printf(mon, " ro=%d", bs->read_only);
1064376253ecSaliguori             monitor_printf(mon, " drv=%s", bs->drv->format_name);
1065376253ecSaliguori             monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1066b338082bSbellard         } else {
1067376253ecSaliguori             monitor_printf(mon, " [not inserted]");
1068b338082bSbellard         }
1069376253ecSaliguori         monitor_printf(mon, "\n");
1070b338082bSbellard     }
1071b338082bSbellard }
1072a36e69ddSths 
1073a36e69ddSths /* The "info blockstats" command. */
1074376253ecSaliguori void bdrv_info_stats(Monitor *mon)
1075a36e69ddSths {
1076a36e69ddSths     BlockDriverState *bs;
1077a36e69ddSths 
1078a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1079376253ecSaliguori         monitor_printf(mon, "%s:"
1080a36e69ddSths                        " rd_bytes=%" PRIu64
1081a36e69ddSths                        " wr_bytes=%" PRIu64
1082a36e69ddSths                        " rd_operations=%" PRIu64
1083a36e69ddSths                        " wr_operations=%" PRIu64
1084ebf53fcdSaliguori                        "\n",
1085a36e69ddSths                        bs->device_name,
1086a36e69ddSths                        bs->rd_bytes, bs->wr_bytes,
1087a36e69ddSths                        bs->rd_ops, bs->wr_ops);
1088a36e69ddSths     }
1089a36e69ddSths }
1090ea2384d3Sbellard 
1091045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1092045df330Saliguori {
1093045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1094045df330Saliguori         return bs->backing_file;
1095045df330Saliguori     else if (bs->encrypted)
1096045df330Saliguori         return bs->filename;
1097045df330Saliguori     else
1098045df330Saliguori         return NULL;
1099045df330Saliguori }
1100045df330Saliguori 
110183f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
110283f64091Sbellard                                char *filename, int filename_size)
110383f64091Sbellard {
110483f64091Sbellard     if (!bs->backing_hd) {
110583f64091Sbellard         pstrcpy(filename, filename_size, "");
110683f64091Sbellard     } else {
110783f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
110883f64091Sbellard     }
110983f64091Sbellard }
111083f64091Sbellard 
1111faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1112faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1113faea38e7Sbellard {
1114faea38e7Sbellard     BlockDriver *drv = bs->drv;
1115faea38e7Sbellard     if (!drv)
111619cb3738Sbellard         return -ENOMEDIUM;
1117faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1118faea38e7Sbellard         return -ENOTSUP;
1119faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1120faea38e7Sbellard }
1121faea38e7Sbellard 
1122faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1123faea38e7Sbellard {
1124faea38e7Sbellard     BlockDriver *drv = bs->drv;
1125faea38e7Sbellard     if (!drv)
112619cb3738Sbellard         return -ENOMEDIUM;
1127faea38e7Sbellard     if (!drv->bdrv_get_info)
1128faea38e7Sbellard         return -ENOTSUP;
1129faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1130faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1131faea38e7Sbellard }
1132faea38e7Sbellard 
1133faea38e7Sbellard /**************************************************************/
1134faea38e7Sbellard /* handling of snapshots */
1135faea38e7Sbellard 
1136faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1137faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1138faea38e7Sbellard {
1139faea38e7Sbellard     BlockDriver *drv = bs->drv;
1140faea38e7Sbellard     if (!drv)
114119cb3738Sbellard         return -ENOMEDIUM;
1142faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
1143faea38e7Sbellard         return -ENOTSUP;
1144faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
1145faea38e7Sbellard }
1146faea38e7Sbellard 
1147faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1148faea38e7Sbellard                        const char *snapshot_id)
1149faea38e7Sbellard {
1150faea38e7Sbellard     BlockDriver *drv = bs->drv;
1151faea38e7Sbellard     if (!drv)
115219cb3738Sbellard         return -ENOMEDIUM;
1153faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
1154faea38e7Sbellard         return -ENOTSUP;
1155faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
1156faea38e7Sbellard }
1157faea38e7Sbellard 
1158faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1159faea38e7Sbellard {
1160faea38e7Sbellard     BlockDriver *drv = bs->drv;
1161faea38e7Sbellard     if (!drv)
116219cb3738Sbellard         return -ENOMEDIUM;
1163faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
1164faea38e7Sbellard         return -ENOTSUP;
1165faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1166faea38e7Sbellard }
1167faea38e7Sbellard 
1168faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1169faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1170faea38e7Sbellard {
1171faea38e7Sbellard     BlockDriver *drv = bs->drv;
1172faea38e7Sbellard     if (!drv)
117319cb3738Sbellard         return -ENOMEDIUM;
1174faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1175faea38e7Sbellard         return -ENOTSUP;
1176faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1177faea38e7Sbellard }
1178faea38e7Sbellard 
1179faea38e7Sbellard #define NB_SUFFIXES 4
1180faea38e7Sbellard 
1181faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1182faea38e7Sbellard {
1183faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1184faea38e7Sbellard     int64_t base;
1185faea38e7Sbellard     int i;
1186faea38e7Sbellard 
1187faea38e7Sbellard     if (size <= 999) {
1188faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1189faea38e7Sbellard     } else {
1190faea38e7Sbellard         base = 1024;
1191faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1192faea38e7Sbellard             if (size < (10 * base)) {
1193faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1194faea38e7Sbellard                          (double)size / base,
1195faea38e7Sbellard                          suffixes[i]);
1196faea38e7Sbellard                 break;
1197faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1198faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1199faea38e7Sbellard                          ((size + (base >> 1)) / base),
1200faea38e7Sbellard                          suffixes[i]);
1201faea38e7Sbellard                 break;
1202faea38e7Sbellard             }
1203faea38e7Sbellard             base = base * 1024;
1204faea38e7Sbellard         }
1205faea38e7Sbellard     }
1206faea38e7Sbellard     return buf;
1207faea38e7Sbellard }
1208faea38e7Sbellard 
1209faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1210faea38e7Sbellard {
1211faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
12123b9f94e1Sbellard #ifdef _WIN32
12133b9f94e1Sbellard     struct tm *ptm;
12143b9f94e1Sbellard #else
1215faea38e7Sbellard     struct tm tm;
12163b9f94e1Sbellard #endif
1217faea38e7Sbellard     time_t ti;
1218faea38e7Sbellard     int64_t secs;
1219faea38e7Sbellard 
1220faea38e7Sbellard     if (!sn) {
1221faea38e7Sbellard         snprintf(buf, buf_size,
1222faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1223faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1224faea38e7Sbellard     } else {
1225faea38e7Sbellard         ti = sn->date_sec;
12263b9f94e1Sbellard #ifdef _WIN32
12273b9f94e1Sbellard         ptm = localtime(&ti);
12283b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
12293b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
12303b9f94e1Sbellard #else
1231faea38e7Sbellard         localtime_r(&ti, &tm);
1232faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1233faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
12343b9f94e1Sbellard #endif
1235faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1236faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1237faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1238faea38e7Sbellard                  (int)(secs / 3600),
1239faea38e7Sbellard                  (int)((secs / 60) % 60),
1240faea38e7Sbellard                  (int)(secs % 60),
1241faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1242faea38e7Sbellard         snprintf(buf, buf_size,
1243faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1244faea38e7Sbellard                  sn->id_str, sn->name,
1245faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1246faea38e7Sbellard                  date_buf,
1247faea38e7Sbellard                  clock_buf);
1248faea38e7Sbellard     }
1249faea38e7Sbellard     return buf;
1250faea38e7Sbellard }
1251faea38e7Sbellard 
125283f64091Sbellard 
1253ea2384d3Sbellard /**************************************************************/
125483f64091Sbellard /* async I/Os */
1255ea2384d3Sbellard 
12563b69e4b9Saliguori typedef struct VectorTranslationState {
12573b69e4b9Saliguori     QEMUIOVector *iov;
12583b69e4b9Saliguori     uint8_t *bounce;
12593b69e4b9Saliguori     int is_write;
12603b69e4b9Saliguori     BlockDriverAIOCB *aiocb;
12613b69e4b9Saliguori     BlockDriverAIOCB *this_aiocb;
12623b69e4b9Saliguori } VectorTranslationState;
12633b69e4b9Saliguori 
12643b69e4b9Saliguori static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
12653b69e4b9Saliguori {
12663b69e4b9Saliguori     VectorTranslationState *s = opaque;
12673b69e4b9Saliguori 
12683b69e4b9Saliguori     if (!s->is_write) {
1269249aa745Saliguori         qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
12703b69e4b9Saliguori     }
1271d905dba4Saurel32     qemu_vfree(s->bounce);
12723b69e4b9Saliguori     s->this_aiocb->cb(s->this_aiocb->opaque, ret);
12733b69e4b9Saliguori     qemu_aio_release(s->this_aiocb);
12743b69e4b9Saliguori }
12753b69e4b9Saliguori 
12763b69e4b9Saliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
12773b69e4b9Saliguori                                             int64_t sector_num,
12783b69e4b9Saliguori                                             QEMUIOVector *iov,
12793b69e4b9Saliguori                                             int nb_sectors,
12803b69e4b9Saliguori                                             BlockDriverCompletionFunc *cb,
12813b69e4b9Saliguori                                             void *opaque,
12823b69e4b9Saliguori                                             int is_write)
12833b69e4b9Saliguori 
12843b69e4b9Saliguori {
12853b69e4b9Saliguori     VectorTranslationState *s = qemu_mallocz(sizeof(*s));
12863b69e4b9Saliguori     BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
12873b69e4b9Saliguori 
12883b69e4b9Saliguori     s->this_aiocb = aiocb;
12893b69e4b9Saliguori     s->iov = iov;
12903b69e4b9Saliguori     s->bounce = qemu_memalign(512, nb_sectors * 512);
12913b69e4b9Saliguori     s->is_write = is_write;
12923b69e4b9Saliguori     if (is_write) {
12933b69e4b9Saliguori         qemu_iovec_to_buffer(s->iov, s->bounce);
12943b69e4b9Saliguori         s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
12953b69e4b9Saliguori                                   bdrv_aio_rw_vector_cb, s);
12963b69e4b9Saliguori     } else {
12973b69e4b9Saliguori         s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
12983b69e4b9Saliguori                                  bdrv_aio_rw_vector_cb, s);
12993b69e4b9Saliguori     }
13003b69e4b9Saliguori     return aiocb;
13013b69e4b9Saliguori }
13023b69e4b9Saliguori 
13033b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
13043b69e4b9Saliguori                                  QEMUIOVector *iov, int nb_sectors,
13053b69e4b9Saliguori                                  BlockDriverCompletionFunc *cb, void *opaque)
13063b69e4b9Saliguori {
130771d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
130871d0770cSaliguori         return NULL;
130971d0770cSaliguori 
13103b69e4b9Saliguori     return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
13113b69e4b9Saliguori                               cb, opaque, 0);
13123b69e4b9Saliguori }
13133b69e4b9Saliguori 
13143b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
13153b69e4b9Saliguori                                   QEMUIOVector *iov, int nb_sectors,
13163b69e4b9Saliguori                                   BlockDriverCompletionFunc *cb, void *opaque)
13173b69e4b9Saliguori {
131871d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
131971d0770cSaliguori         return NULL;
132071d0770cSaliguori 
13213b69e4b9Saliguori     return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
13223b69e4b9Saliguori                               cb, opaque, 1);
13233b69e4b9Saliguori }
13243b69e4b9Saliguori 
1325ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
132683f64091Sbellard                                 uint8_t *buf, int nb_sectors,
132783f64091Sbellard                                 BlockDriverCompletionFunc *cb, void *opaque)
1328ea2384d3Sbellard {
132983f64091Sbellard     BlockDriver *drv = bs->drv;
1330a36e69ddSths     BlockDriverAIOCB *ret;
1331ea2384d3Sbellard 
133219cb3738Sbellard     if (!drv)
1333ce1a14dcSpbrook         return NULL;
133471d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
133571d0770cSaliguori         return NULL;
133683f64091Sbellard 
1337a36e69ddSths     ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1338a36e69ddSths 
1339a36e69ddSths     if (ret) {
1340a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1341a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1342a36e69ddSths 	bs->rd_ops ++;
1343a36e69ddSths     }
1344a36e69ddSths 
1345a36e69ddSths     return ret;
134683f64091Sbellard }
134783f64091Sbellard 
1348ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
134983f64091Sbellard                                  const uint8_t *buf, int nb_sectors,
135083f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
13517674e7bfSbellard {
135283f64091Sbellard     BlockDriver *drv = bs->drv;
1353a36e69ddSths     BlockDriverAIOCB *ret;
135483f64091Sbellard 
135519cb3738Sbellard     if (!drv)
1356ce1a14dcSpbrook         return NULL;
135783f64091Sbellard     if (bs->read_only)
1358ce1a14dcSpbrook         return NULL;
135971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
136071d0770cSaliguori         return NULL;
136183f64091Sbellard 
1362a36e69ddSths     ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1363a36e69ddSths 
1364a36e69ddSths     if (ret) {
1365a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1366a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1367a36e69ddSths 	bs->wr_ops ++;
1368a36e69ddSths     }
1369a36e69ddSths 
1370a36e69ddSths     return ret;
137183f64091Sbellard }
137283f64091Sbellard 
137383f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
137483f64091Sbellard {
13753b69e4b9Saliguori     if (acb->cb == bdrv_aio_rw_vector_cb) {
13763b69e4b9Saliguori         VectorTranslationState *s = acb->opaque;
13773b69e4b9Saliguori         acb = s->aiocb;
13783b69e4b9Saliguori     }
13793b69e4b9Saliguori 
1380*6bbff9a0Saliguori     acb->pool->cancel(acb);
138183f64091Sbellard }
138283f64091Sbellard 
138383f64091Sbellard 
138483f64091Sbellard /**************************************************************/
138583f64091Sbellard /* async block device emulation */
138683f64091Sbellard 
138783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1388beac80cdSbellard {
1389ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1390ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1391ce1a14dcSpbrook     qemu_aio_release(acb);
1392beac80cdSbellard }
1393beac80cdSbellard 
1394ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1395ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1396ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1397ea2384d3Sbellard {
1398ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
139983f64091Sbellard     int ret;
140083f64091Sbellard 
1401ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1402ce1a14dcSpbrook     if (!acb->bh)
1403ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1404ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1405ce1a14dcSpbrook     acb->ret = ret;
1406ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1407ce1a14dcSpbrook     return &acb->common;
14087a6cba61Spbrook }
14097a6cba61Spbrook 
1410ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1411ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1412ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
141383f64091Sbellard {
1414ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
141583f64091Sbellard     int ret;
141683f64091Sbellard 
1417ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1418ce1a14dcSpbrook     if (!acb->bh)
1419ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1420ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1421ce1a14dcSpbrook     acb->ret = ret;
1422ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1423ce1a14dcSpbrook     return &acb->common;
142483f64091Sbellard }
142583f64091Sbellard 
1426ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
142783f64091Sbellard {
1428ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1429ce1a14dcSpbrook     qemu_bh_cancel(acb->bh);
1430ce1a14dcSpbrook     qemu_aio_release(acb);
143183f64091Sbellard }
143283f64091Sbellard 
143383f64091Sbellard /**************************************************************/
143483f64091Sbellard /* sync block device emulation */
143583f64091Sbellard 
143683f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
143783f64091Sbellard {
143883f64091Sbellard     *(int *)opaque = ret;
143983f64091Sbellard }
144083f64091Sbellard 
144183f64091Sbellard #define NOT_DONE 0x7fffffff
144283f64091Sbellard 
144383f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
144483f64091Sbellard                         uint8_t *buf, int nb_sectors)
144583f64091Sbellard {
1446ce1a14dcSpbrook     int async_ret;
1447ce1a14dcSpbrook     BlockDriverAIOCB *acb;
144883f64091Sbellard 
144983f64091Sbellard     async_ret = NOT_DONE;
1450ce1a14dcSpbrook     acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
145183f64091Sbellard                         bdrv_rw_em_cb, &async_ret);
1452baf35cb9Saliguori     if (acb == NULL)
1453ce1a14dcSpbrook         return -1;
1454baf35cb9Saliguori 
145583f64091Sbellard     while (async_ret == NOT_DONE) {
145683f64091Sbellard         qemu_aio_wait();
145783f64091Sbellard     }
1458baf35cb9Saliguori 
145983f64091Sbellard     return async_ret;
146083f64091Sbellard }
146183f64091Sbellard 
146283f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
146383f64091Sbellard                          const uint8_t *buf, int nb_sectors)
146483f64091Sbellard {
1465ce1a14dcSpbrook     int async_ret;
1466ce1a14dcSpbrook     BlockDriverAIOCB *acb;
146783f64091Sbellard 
146883f64091Sbellard     async_ret = NOT_DONE;
1469ce1a14dcSpbrook     acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
147083f64091Sbellard                          bdrv_rw_em_cb, &async_ret);
1471baf35cb9Saliguori     if (acb == NULL)
1472ce1a14dcSpbrook         return -1;
147383f64091Sbellard     while (async_ret == NOT_DONE) {
147483f64091Sbellard         qemu_aio_wait();
147583f64091Sbellard     }
147683f64091Sbellard     return async_ret;
147783f64091Sbellard }
1478ea2384d3Sbellard 
1479ea2384d3Sbellard void bdrv_init(void)
1480ea2384d3Sbellard {
1481ea2384d3Sbellard     bdrv_register(&bdrv_raw);
148219cb3738Sbellard     bdrv_register(&bdrv_host_device);
1483ea2384d3Sbellard #ifndef _WIN32
1484ea2384d3Sbellard     bdrv_register(&bdrv_cow);
1485ea2384d3Sbellard #endif
1486ea2384d3Sbellard     bdrv_register(&bdrv_qcow);
1487ea2384d3Sbellard     bdrv_register(&bdrv_vmdk);
14883c56521bSbellard     bdrv_register(&bdrv_cloop);
1489585d0ed9Sbellard     bdrv_register(&bdrv_dmg);
1490a8753c34Sbellard     bdrv_register(&bdrv_bochs);
14916a0f9e82Sbellard     bdrv_register(&bdrv_vpc);
1492712e7874Sbellard     bdrv_register(&bdrv_vvfat);
1493faea38e7Sbellard     bdrv_register(&bdrv_qcow2);
14946ada7453Sths     bdrv_register(&bdrv_parallels);
149575818250Sths     bdrv_register(&bdrv_nbd);
1496ea2384d3Sbellard }
1497ce1a14dcSpbrook 
1498*6bbff9a0Saliguori void aio_pool_init(AIOPool *pool, int aiocb_size,
1499*6bbff9a0Saliguori                    void (*cancel)(BlockDriverAIOCB *acb))
1500ce1a14dcSpbrook {
1501*6bbff9a0Saliguori     pool->aiocb_size = aiocb_size;
1502*6bbff9a0Saliguori     pool->cancel = cancel;
1503*6bbff9a0Saliguori     pool->free_aiocb = NULL;
1504*6bbff9a0Saliguori }
1505*6bbff9a0Saliguori 
1506*6bbff9a0Saliguori void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
1507*6bbff9a0Saliguori                         BlockDriverCompletionFunc *cb, void *opaque)
1508*6bbff9a0Saliguori {
1509ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1510ce1a14dcSpbrook 
1511*6bbff9a0Saliguori     if (pool->free_aiocb) {
1512*6bbff9a0Saliguori         acb = pool->free_aiocb;
1513*6bbff9a0Saliguori         pool->free_aiocb = acb->next;
1514ce1a14dcSpbrook     } else {
1515*6bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
1516*6bbff9a0Saliguori         acb->pool = pool;
1517ce1a14dcSpbrook     }
1518ce1a14dcSpbrook     acb->bs = bs;
1519ce1a14dcSpbrook     acb->cb = cb;
1520ce1a14dcSpbrook     acb->opaque = opaque;
1521ce1a14dcSpbrook     return acb;
1522ce1a14dcSpbrook }
1523ce1a14dcSpbrook 
1524*6bbff9a0Saliguori void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1525*6bbff9a0Saliguori                    void *opaque)
1526*6bbff9a0Saliguori {
1527*6bbff9a0Saliguori     return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
1528*6bbff9a0Saliguori }
1529*6bbff9a0Saliguori 
1530ce1a14dcSpbrook void qemu_aio_release(void *p)
1531ce1a14dcSpbrook {
1532*6bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1533*6bbff9a0Saliguori     AIOPool *pool = acb->pool;
1534*6bbff9a0Saliguori     acb->next = pool->free_aiocb;
1535*6bbff9a0Saliguori     pool->free_aiocb = acb;
1536ce1a14dcSpbrook }
153719cb3738Sbellard 
153819cb3738Sbellard /**************************************************************/
153919cb3738Sbellard /* removable device support */
154019cb3738Sbellard 
154119cb3738Sbellard /**
154219cb3738Sbellard  * Return TRUE if the media is present
154319cb3738Sbellard  */
154419cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
154519cb3738Sbellard {
154619cb3738Sbellard     BlockDriver *drv = bs->drv;
154719cb3738Sbellard     int ret;
154819cb3738Sbellard     if (!drv)
154919cb3738Sbellard         return 0;
155019cb3738Sbellard     if (!drv->bdrv_is_inserted)
155119cb3738Sbellard         return 1;
155219cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
155319cb3738Sbellard     return ret;
155419cb3738Sbellard }
155519cb3738Sbellard 
155619cb3738Sbellard /**
155719cb3738Sbellard  * Return TRUE if the media changed since the last call to this
155819cb3738Sbellard  * function. It is currently only used for floppy disks
155919cb3738Sbellard  */
156019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
156119cb3738Sbellard {
156219cb3738Sbellard     BlockDriver *drv = bs->drv;
156319cb3738Sbellard     int ret;
156419cb3738Sbellard 
156519cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
156619cb3738Sbellard         ret = -ENOTSUP;
156719cb3738Sbellard     else
156819cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
156919cb3738Sbellard     if (ret == -ENOTSUP)
157019cb3738Sbellard         ret = bs->media_changed;
157119cb3738Sbellard     bs->media_changed = 0;
157219cb3738Sbellard     return ret;
157319cb3738Sbellard }
157419cb3738Sbellard 
157519cb3738Sbellard /**
157619cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
157719cb3738Sbellard  */
157819cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
157919cb3738Sbellard {
158019cb3738Sbellard     BlockDriver *drv = bs->drv;
158119cb3738Sbellard     int ret;
158219cb3738Sbellard 
158319cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
158419cb3738Sbellard         ret = -ENOTSUP;
158519cb3738Sbellard     } else {
158619cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
158719cb3738Sbellard     }
158819cb3738Sbellard     if (ret == -ENOTSUP) {
158919cb3738Sbellard         if (eject_flag)
159019cb3738Sbellard             bdrv_close(bs);
159119cb3738Sbellard     }
159219cb3738Sbellard }
159319cb3738Sbellard 
159419cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
159519cb3738Sbellard {
159619cb3738Sbellard     return bs->locked;
159719cb3738Sbellard }
159819cb3738Sbellard 
159919cb3738Sbellard /**
160019cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
160119cb3738Sbellard  * to eject it manually).
160219cb3738Sbellard  */
160319cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
160419cb3738Sbellard {
160519cb3738Sbellard     BlockDriver *drv = bs->drv;
160619cb3738Sbellard 
160719cb3738Sbellard     bs->locked = locked;
160819cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
160919cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
161019cb3738Sbellard     }
161119cb3738Sbellard }
1612985a03b0Sths 
1613985a03b0Sths /* needed for generic scsi interface */
1614985a03b0Sths 
1615985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1616985a03b0Sths {
1617985a03b0Sths     BlockDriver *drv = bs->drv;
1618985a03b0Sths 
1619985a03b0Sths     if (drv && drv->bdrv_ioctl)
1620985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1621985a03b0Sths     return -ENOTSUP;
1622985a03b0Sths }
16237d780669Saliguori 
16247d780669Saliguori int bdrv_sg_send_command(BlockDriverState *bs, void *buf, int count)
16257d780669Saliguori {
162604eeb8b6Saliguori     return bs->drv->bdrv_sg_send_command(bs, buf, count);
16277d780669Saliguori }
16287d780669Saliguori 
16297d780669Saliguori int bdrv_sg_recv_response(BlockDriverState *bs, void *buf, int count)
16307d780669Saliguori {
163104eeb8b6Saliguori     return bs->drv->bdrv_sg_recv_response(bs, buf, count);
16327d780669Saliguori }
16337d780669Saliguori 
16347d780669Saliguori BlockDriverAIOCB *bdrv_sg_aio_read(BlockDriverState *bs, void *buf, int count,
16357d780669Saliguori                                    BlockDriverCompletionFunc *cb, void *opaque)
16367d780669Saliguori {
163704eeb8b6Saliguori     return bs->drv->bdrv_sg_aio_read(bs, buf, count, cb, opaque);
16387d780669Saliguori }
16397d780669Saliguori 
16407d780669Saliguori BlockDriverAIOCB *bdrv_sg_aio_write(BlockDriverState *bs, void *buf, int count,
16417d780669Saliguori                                     BlockDriverCompletionFunc *cb, void *opaque)
16427d780669Saliguori {
164304eeb8b6Saliguori     return bs->drv->bdrv_sg_aio_write(bs, buf, count, cb, opaque);
16447d780669Saliguori }
1645