xref: /openbmc/qemu/block.c (revision c5e97233e8c8e53aab1b80b5e9891a71c4edea3e)
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"
253990d09aSblueswir1 #ifdef _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 
347674e7bfSbellard #ifdef _BSD
357674e7bfSbellard #include <sys/types.h>
367674e7bfSbellard #include <sys/stat.h>
377674e7bfSbellard #include <sys/ioctl.h>
38*c5e97233Sblueswir1 #ifndef __DragonFly__
397674e7bfSbellard #include <sys/disk.h>
407674e7bfSbellard #endif
41*c5e97233Sblueswir1 #endif
427674e7bfSbellard 
4383f64091Sbellard #define SECTOR_BITS 9
4483f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS)
453b0d4f61Sbellard 
4690765429Sbellard typedef struct BlockDriverAIOCBSync {
4790765429Sbellard     BlockDriverAIOCB common;
4890765429Sbellard     QEMUBH *bh;
4990765429Sbellard     int ret;
5090765429Sbellard } BlockDriverAIOCBSync;
5190765429Sbellard 
52ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
53ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
54ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
55ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
56ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
57ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
5883f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
5983f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
6083f64091Sbellard                         uint8_t *buf, int nb_sectors);
6183f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
6283f64091Sbellard                          const uint8_t *buf, int nb_sectors);
63ec530c81Sbellard 
647ee930d0Sblueswir1 BlockDriverState *bdrv_first;
657ee930d0Sblueswir1 
66ea2384d3Sbellard static BlockDriver *first_drv;
67ea2384d3Sbellard 
6883f64091Sbellard int path_is_absolute(const char *path)
6983f64091Sbellard {
7083f64091Sbellard     const char *p;
7121664424Sbellard #ifdef _WIN32
7221664424Sbellard     /* specific case for names like: "\\.\d:" */
7321664424Sbellard     if (*path == '/' || *path == '\\')
7421664424Sbellard         return 1;
7521664424Sbellard #endif
7683f64091Sbellard     p = strchr(path, ':');
7783f64091Sbellard     if (p)
7883f64091Sbellard         p++;
7983f64091Sbellard     else
8083f64091Sbellard         p = path;
813b9f94e1Sbellard #ifdef _WIN32
823b9f94e1Sbellard     return (*p == '/' || *p == '\\');
833b9f94e1Sbellard #else
843b9f94e1Sbellard     return (*p == '/');
853b9f94e1Sbellard #endif
8683f64091Sbellard }
8783f64091Sbellard 
8883f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
8983f64091Sbellard    path to it by considering it is relative to base_path. URL are
9083f64091Sbellard    supported. */
9183f64091Sbellard void path_combine(char *dest, int dest_size,
9283f64091Sbellard                   const char *base_path,
9383f64091Sbellard                   const char *filename)
9483f64091Sbellard {
9583f64091Sbellard     const char *p, *p1;
9683f64091Sbellard     int len;
9783f64091Sbellard 
9883f64091Sbellard     if (dest_size <= 0)
9983f64091Sbellard         return;
10083f64091Sbellard     if (path_is_absolute(filename)) {
10183f64091Sbellard         pstrcpy(dest, dest_size, filename);
10283f64091Sbellard     } else {
10383f64091Sbellard         p = strchr(base_path, ':');
10483f64091Sbellard         if (p)
10583f64091Sbellard             p++;
10683f64091Sbellard         else
10783f64091Sbellard             p = base_path;
1083b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1093b9f94e1Sbellard #ifdef _WIN32
1103b9f94e1Sbellard         {
1113b9f94e1Sbellard             const char *p2;
1123b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1133b9f94e1Sbellard             if (!p1 || p2 > p1)
1143b9f94e1Sbellard                 p1 = p2;
1153b9f94e1Sbellard         }
1163b9f94e1Sbellard #endif
11783f64091Sbellard         if (p1)
11883f64091Sbellard             p1++;
11983f64091Sbellard         else
12083f64091Sbellard             p1 = base_path;
12183f64091Sbellard         if (p1 > p)
12283f64091Sbellard             p = p1;
12383f64091Sbellard         len = p - base_path;
12483f64091Sbellard         if (len > dest_size - 1)
12583f64091Sbellard             len = dest_size - 1;
12683f64091Sbellard         memcpy(dest, base_path, len);
12783f64091Sbellard         dest[len] = '\0';
12883f64091Sbellard         pstrcat(dest, dest_size, filename);
12983f64091Sbellard     }
13083f64091Sbellard }
13183f64091Sbellard 
13283f64091Sbellard 
1339596ebb7Spbrook static void bdrv_register(BlockDriver *bdrv)
134ea2384d3Sbellard {
135ce1a14dcSpbrook     if (!bdrv->bdrv_aio_read) {
13683f64091Sbellard         /* add AIO emulation layer */
13783f64091Sbellard         bdrv->bdrv_aio_read = bdrv_aio_read_em;
13883f64091Sbellard         bdrv->bdrv_aio_write = bdrv_aio_write_em;
13983f64091Sbellard         bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
14090765429Sbellard         bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
14183f64091Sbellard     } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
14283f64091Sbellard         /* add synchronous IO emulation layer */
14383f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
14483f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
14583f64091Sbellard     }
146ea2384d3Sbellard     bdrv->next = first_drv;
147ea2384d3Sbellard     first_drv = bdrv;
148ea2384d3Sbellard }
149b338082bSbellard 
150b338082bSbellard /* create a new block device (by default it is empty) */
151b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
152fc01f7e7Sbellard {
153b338082bSbellard     BlockDriverState **pbs, *bs;
154b338082bSbellard 
155b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
156b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
157ea2384d3Sbellard     if (device_name[0] != '\0') {
158b338082bSbellard         /* insert at the end */
159b338082bSbellard         pbs = &bdrv_first;
160b338082bSbellard         while (*pbs != NULL)
161b338082bSbellard             pbs = &(*pbs)->next;
162b338082bSbellard         *pbs = bs;
163ea2384d3Sbellard     }
164b338082bSbellard     return bs;
165b338082bSbellard }
166b338082bSbellard 
167ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
168ea2384d3Sbellard {
169ea2384d3Sbellard     BlockDriver *drv1;
170ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
171ea2384d3Sbellard         if (!strcmp(drv1->format_name, format_name))
172ea2384d3Sbellard             return drv1;
173ea2384d3Sbellard     }
174ea2384d3Sbellard     return NULL;
175ea2384d3Sbellard }
176ea2384d3Sbellard 
177ea2384d3Sbellard int bdrv_create(BlockDriver *drv,
178ea2384d3Sbellard                 const char *filename, int64_t size_in_sectors,
179ea2384d3Sbellard                 const char *backing_file, int flags)
180ea2384d3Sbellard {
181ea2384d3Sbellard     if (!drv->bdrv_create)
182ea2384d3Sbellard         return -ENOTSUP;
183ea2384d3Sbellard     return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
184ea2384d3Sbellard }
185ea2384d3Sbellard 
186d5249393Sbellard #ifdef _WIN32
18795389c86Sbellard void get_tmp_filename(char *filename, int size)
188d5249393Sbellard {
1893b9f94e1Sbellard     char temp_dir[MAX_PATH];
1903b9f94e1Sbellard 
1913b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
1923b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
193d5249393Sbellard }
194d5249393Sbellard #else
19595389c86Sbellard void get_tmp_filename(char *filename, int size)
196ea2384d3Sbellard {
197ea2384d3Sbellard     int fd;
1987ccfb2ebSblueswir1     const char *tmpdir;
199d5249393Sbellard     /* XXX: race condition possible */
2000badc1eeSaurel32     tmpdir = getenv("TMPDIR");
2010badc1eeSaurel32     if (!tmpdir)
2020badc1eeSaurel32         tmpdir = "/tmp";
2030badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
204ea2384d3Sbellard     fd = mkstemp(filename);
205ea2384d3Sbellard     close(fd);
206ea2384d3Sbellard }
207d5249393Sbellard #endif
208ea2384d3Sbellard 
20919cb3738Sbellard #ifdef _WIN32
210f45512feSbellard static int is_windows_drive_prefix(const char *filename)
211f45512feSbellard {
212f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
213f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
214f45512feSbellard             filename[1] == ':');
215f45512feSbellard }
216f45512feSbellard 
21719cb3738Sbellard static int is_windows_drive(const char *filename)
21819cb3738Sbellard {
219f45512feSbellard     if (is_windows_drive_prefix(filename) &&
220f45512feSbellard         filename[2] == '\0')
22119cb3738Sbellard         return 1;
22219cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
22319cb3738Sbellard         strstart(filename, "//./", NULL))
22419cb3738Sbellard         return 1;
22519cb3738Sbellard     return 0;
22619cb3738Sbellard }
22719cb3738Sbellard #endif
22819cb3738Sbellard 
22983f64091Sbellard static BlockDriver *find_protocol(const char *filename)
23083f64091Sbellard {
23183f64091Sbellard     BlockDriver *drv1;
23283f64091Sbellard     char protocol[128];
23383f64091Sbellard     int len;
23483f64091Sbellard     const char *p;
23519cb3738Sbellard 
23619cb3738Sbellard #ifdef _WIN32
237f45512feSbellard     if (is_windows_drive(filename) ||
238f45512feSbellard         is_windows_drive_prefix(filename))
23919cb3738Sbellard         return &bdrv_raw;
24019cb3738Sbellard #endif
24183f64091Sbellard     p = strchr(filename, ':');
24283f64091Sbellard     if (!p)
24383f64091Sbellard         return &bdrv_raw;
24483f64091Sbellard     len = p - filename;
24583f64091Sbellard     if (len > sizeof(protocol) - 1)
24683f64091Sbellard         len = sizeof(protocol) - 1;
24783f64091Sbellard     memcpy(protocol, filename, len);
24883f64091Sbellard     protocol[len] = '\0';
24983f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
25083f64091Sbellard         if (drv1->protocol_name &&
25183f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
25283f64091Sbellard             return drv1;
25383f64091Sbellard     }
25483f64091Sbellard     return NULL;
25583f64091Sbellard }
25683f64091Sbellard 
2577674e7bfSbellard /* XXX: force raw format if block or character device ? It would
2587674e7bfSbellard    simplify the BSD case */
259ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
260ea2384d3Sbellard {
26183f64091Sbellard     int ret, score, score_max;
262ea2384d3Sbellard     BlockDriver *drv1, *drv;
26383f64091Sbellard     uint8_t buf[2048];
26483f64091Sbellard     BlockDriverState *bs;
265ea2384d3Sbellard 
26619cb3738Sbellard     /* detect host devices. By convention, /dev/cdrom[N] is always
26719cb3738Sbellard        recognized as a host CDROM */
26819cb3738Sbellard     if (strstart(filename, "/dev/cdrom", NULL))
26919cb3738Sbellard         return &bdrv_host_device;
27019cb3738Sbellard #ifdef _WIN32
27119cb3738Sbellard     if (is_windows_drive(filename))
27219cb3738Sbellard         return &bdrv_host_device;
27319cb3738Sbellard #else
27419cb3738Sbellard     {
27519cb3738Sbellard         struct stat st;
27619cb3738Sbellard         if (stat(filename, &st) >= 0 &&
27719cb3738Sbellard             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
27819cb3738Sbellard             return &bdrv_host_device;
27919cb3738Sbellard         }
28019cb3738Sbellard     }
28119cb3738Sbellard #endif
28219cb3738Sbellard 
28383f64091Sbellard     drv = find_protocol(filename);
28419cb3738Sbellard     /* no need to test disk image formats for vvfat */
28583f64091Sbellard     if (drv == &bdrv_vvfat)
28683f64091Sbellard         return drv;
28783f64091Sbellard 
28883f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
28983f64091Sbellard     if (ret < 0)
2907674e7bfSbellard         return NULL;
29183f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
29283f64091Sbellard     bdrv_delete(bs);
293ea2384d3Sbellard     if (ret < 0) {
294ea2384d3Sbellard         return NULL;
295ea2384d3Sbellard     }
296ea2384d3Sbellard 
297ea2384d3Sbellard     score_max = 0;
298ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
29983f64091Sbellard         if (drv1->bdrv_probe) {
300ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
301ea2384d3Sbellard             if (score > score_max) {
302ea2384d3Sbellard                 score_max = score;
303ea2384d3Sbellard                 drv = drv1;
304ea2384d3Sbellard             }
305ea2384d3Sbellard         }
30683f64091Sbellard     }
307ea2384d3Sbellard     return drv;
308ea2384d3Sbellard }
309ea2384d3Sbellard 
31083f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
311b338082bSbellard {
31283f64091Sbellard     BlockDriverState *bs;
31383f64091Sbellard     int ret;
3143b0d4f61Sbellard 
31583f64091Sbellard     bs = bdrv_new("");
31683f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
31783f64091Sbellard     if (ret < 0) {
31883f64091Sbellard         bdrv_delete(bs);
31983f64091Sbellard         return ret;
3203b0d4f61Sbellard     }
32171d0770cSaliguori     bs->growable = 1;
32283f64091Sbellard     *pbs = bs;
32383f64091Sbellard     return 0;
3243b0d4f61Sbellard }
3253b0d4f61Sbellard 
32683f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
32783f64091Sbellard {
32883f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
329ea2384d3Sbellard }
330ea2384d3Sbellard 
33183f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
332ea2384d3Sbellard                BlockDriver *drv)
333ea2384d3Sbellard {
33483f64091Sbellard     int ret, open_flags;
335eb5c851fSths     char tmp_filename[PATH_MAX];
336eb5c851fSths     char backing_filename[PATH_MAX];
337fc01f7e7Sbellard 
3380849bf08Sbellard     bs->read_only = 0;
339ea2384d3Sbellard     bs->is_temporary = 0;
340ea2384d3Sbellard     bs->encrypted = 0;
341c0f4ce77Saliguori     bs->valid_key = 0;
34233e3963eSbellard 
34383f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
344ea2384d3Sbellard         BlockDriverState *bs1;
345ea2384d3Sbellard         int64_t total_size;
3467c96d46eSaliguori         int is_protocol = 0;
34733e3963eSbellard 
348ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
349ea2384d3Sbellard            instead of opening 'filename' directly */
350ea2384d3Sbellard 
351ea2384d3Sbellard         /* if there is a backing file, use it */
352ea2384d3Sbellard         bs1 = bdrv_new("");
35351d7c00cSaliguori         ret = bdrv_open(bs1, filename, 0);
35451d7c00cSaliguori         if (ret < 0) {
355ea2384d3Sbellard             bdrv_delete(bs1);
35651d7c00cSaliguori             return ret;
357ea2384d3Sbellard         }
35883f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
3597c96d46eSaliguori 
3607c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
3617c96d46eSaliguori             is_protocol = 1;
3627c96d46eSaliguori 
363ea2384d3Sbellard         bdrv_delete(bs1);
364ea2384d3Sbellard 
365ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
3667c96d46eSaliguori 
3677c96d46eSaliguori         /* Real path is meaningless for protocols */
3687c96d46eSaliguori         if (is_protocol)
3697c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
3707c96d46eSaliguori                      "%s", filename);
3717c96d46eSaliguori         else
372a817d936Sbellard             realpath(filename, backing_filename);
3737c96d46eSaliguori 
37451d7c00cSaliguori         ret = bdrv_create(&bdrv_qcow2, tmp_filename,
37551d7c00cSaliguori                           total_size, backing_filename, 0);
37651d7c00cSaliguori         if (ret < 0) {
37751d7c00cSaliguori             return ret;
378ea2384d3Sbellard         }
379ea2384d3Sbellard         filename = tmp_filename;
380ea2384d3Sbellard         bs->is_temporary = 1;
381ea2384d3Sbellard     }
382ea2384d3Sbellard 
383ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
38483f64091Sbellard     if (flags & BDRV_O_FILE) {
38583f64091Sbellard         drv = find_protocol(filename);
38651d7c00cSaliguori     } else if (!drv) {
387ea2384d3Sbellard         drv = find_image_format(filename);
388ea2384d3Sbellard     }
38951d7c00cSaliguori     if (!drv) {
39051d7c00cSaliguori         ret = -ENOENT;
39151d7c00cSaliguori         goto unlink_and_fail;
39283f64091Sbellard     }
393ea2384d3Sbellard     bs->drv = drv;
394ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
39583f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
39683f64091Sbellard        RDONLY as fallback */
39783f64091Sbellard     if (!(flags & BDRV_O_FILE))
3989f7965c7Saliguori         open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
39983f64091Sbellard     else
40083f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
40183f64091Sbellard     ret = drv->bdrv_open(bs, filename, open_flags);
402a0a83536Saurel32     if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
4039f7965c7Saliguori         ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
40483f64091Sbellard         bs->read_only = 1;
40583f64091Sbellard     }
406ea2384d3Sbellard     if (ret < 0) {
407ea2384d3Sbellard         qemu_free(bs->opaque);
4086b21b973Sbellard         bs->opaque = NULL;
4096b21b973Sbellard         bs->drv = NULL;
41051d7c00cSaliguori     unlink_and_fail:
41151d7c00cSaliguori         if (bs->is_temporary)
41251d7c00cSaliguori             unlink(filename);
41383f64091Sbellard         return ret;
414ea2384d3Sbellard     }
415d15a771dSbellard     if (drv->bdrv_getlength) {
416d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
417d15a771dSbellard     }
418ea2384d3Sbellard #ifndef _WIN32
419ea2384d3Sbellard     if (bs->is_temporary) {
420ea2384d3Sbellard         unlink(filename);
42133e3963eSbellard     }
42267b915a5Sbellard #endif
42383f64091Sbellard     if (bs->backing_file[0] != '\0') {
424ea2384d3Sbellard         /* if there is a backing file, use it */
425ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
42683f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
42783f64091Sbellard                      filename, bs->backing_file);
42851d7c00cSaliguori         ret = bdrv_open(bs->backing_hd, backing_filename, open_flags);
42951d7c00cSaliguori         if (ret < 0) {
43051d7c00cSaliguori             bdrv_close(bs);
43151d7c00cSaliguori             return ret;
43251d7c00cSaliguori         }
433ea2384d3Sbellard     }
43433e3963eSbellard 
435bb5fc20fSaliguori     if (!bdrv_key_required(bs)) {
436b338082bSbellard         /* call the change callback */
43719cb3738Sbellard         bs->media_changed = 1;
438b338082bSbellard         if (bs->change_cb)
439b338082bSbellard             bs->change_cb(bs->change_opaque);
440bb5fc20fSaliguori     }
441b338082bSbellard     return 0;
442fc01f7e7Sbellard }
443fc01f7e7Sbellard 
444fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
445fc01f7e7Sbellard {
44619cb3738Sbellard     if (bs->drv) {
447ea2384d3Sbellard         if (bs->backing_hd)
448ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
449ea2384d3Sbellard         bs->drv->bdrv_close(bs);
450ea2384d3Sbellard         qemu_free(bs->opaque);
451ea2384d3Sbellard #ifdef _WIN32
452ea2384d3Sbellard         if (bs->is_temporary) {
453ea2384d3Sbellard             unlink(bs->filename);
454ea2384d3Sbellard         }
45567b915a5Sbellard #endif
456ea2384d3Sbellard         bs->opaque = NULL;
457ea2384d3Sbellard         bs->drv = NULL;
458b338082bSbellard 
459b338082bSbellard         /* call the change callback */
46019cb3738Sbellard         bs->media_changed = 1;
461b338082bSbellard         if (bs->change_cb)
462b338082bSbellard             bs->change_cb(bs->change_opaque);
463b338082bSbellard     }
464b338082bSbellard }
465b338082bSbellard 
466b338082bSbellard void bdrv_delete(BlockDriverState *bs)
467b338082bSbellard {
46834c6f050Saurel32     BlockDriverState **pbs;
46934c6f050Saurel32 
47034c6f050Saurel32     pbs = &bdrv_first;
47134c6f050Saurel32     while (*pbs != bs && *pbs != NULL)
47234c6f050Saurel32         pbs = &(*pbs)->next;
47334c6f050Saurel32     if (*pbs == bs)
47434c6f050Saurel32         *pbs = bs->next;
47534c6f050Saurel32 
476b338082bSbellard     bdrv_close(bs);
477b338082bSbellard     qemu_free(bs);
478fc01f7e7Sbellard }
479fc01f7e7Sbellard 
48033e3963eSbellard /* commit COW file into the raw image */
48133e3963eSbellard int bdrv_commit(BlockDriverState *bs)
48233e3963eSbellard {
48319cb3738Sbellard     BlockDriver *drv = bs->drv;
48483f64091Sbellard     int64_t i, total_sectors;
485ea2384d3Sbellard     int n, j;
486ea2384d3Sbellard     unsigned char sector[512];
48733e3963eSbellard 
48819cb3738Sbellard     if (!drv)
48919cb3738Sbellard         return -ENOMEDIUM;
49033e3963eSbellard 
49133e3963eSbellard     if (bs->read_only) {
492ea2384d3Sbellard 	return -EACCES;
49333e3963eSbellard     }
49433e3963eSbellard 
495ea2384d3Sbellard     if (!bs->backing_hd) {
496ea2384d3Sbellard 	return -ENOTSUP;
497ea2384d3Sbellard     }
498ea2384d3Sbellard 
49983f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
50083f64091Sbellard     for (i = 0; i < total_sectors;) {
50119cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
502ea2384d3Sbellard             for(j = 0; j < n; j++) {
50333e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
504ea2384d3Sbellard                     return -EIO;
50533e3963eSbellard                 }
50633e3963eSbellard 
507ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
508ea2384d3Sbellard                     return -EIO;
50933e3963eSbellard                 }
510ea2384d3Sbellard                 i++;
511ea2384d3Sbellard 	    }
512ea2384d3Sbellard 	} else {
513ea2384d3Sbellard             i += n;
51433e3963eSbellard         }
51533e3963eSbellard     }
51695389c86Sbellard 
51719cb3738Sbellard     if (drv->bdrv_make_empty)
51819cb3738Sbellard 	return drv->bdrv_make_empty(bs);
51995389c86Sbellard 
52033e3963eSbellard     return 0;
52133e3963eSbellard }
52233e3963eSbellard 
52371d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
52471d0770cSaliguori                                    size_t size)
52571d0770cSaliguori {
52671d0770cSaliguori     int64_t len;
52771d0770cSaliguori 
52871d0770cSaliguori     if (!bdrv_is_inserted(bs))
52971d0770cSaliguori         return -ENOMEDIUM;
53071d0770cSaliguori 
53171d0770cSaliguori     if (bs->growable)
53271d0770cSaliguori         return 0;
53371d0770cSaliguori 
53471d0770cSaliguori     len = bdrv_getlength(bs);
53571d0770cSaliguori 
53671d0770cSaliguori     if ((offset + size) > len)
53771d0770cSaliguori         return -EIO;
53871d0770cSaliguori 
53971d0770cSaliguori     return 0;
54071d0770cSaliguori }
54171d0770cSaliguori 
54271d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
54371d0770cSaliguori                               int nb_sectors)
54471d0770cSaliguori {
54571d0770cSaliguori     int64_t offset;
54671d0770cSaliguori 
54771d0770cSaliguori     /* Deal with byte accesses */
54871d0770cSaliguori     if (sector_num < 0)
54971d0770cSaliguori         offset = -sector_num;
55071d0770cSaliguori     else
55171d0770cSaliguori         offset = sector_num * 512;
55271d0770cSaliguori 
55371d0770cSaliguori     return bdrv_check_byte_request(bs, offset, nb_sectors * 512);
55471d0770cSaliguori }
55571d0770cSaliguori 
55619cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
557fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
558fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
559fc01f7e7Sbellard {
560ea2384d3Sbellard     BlockDriver *drv = bs->drv;
561fc01f7e7Sbellard 
56219cb3738Sbellard     if (!drv)
56319cb3738Sbellard         return -ENOMEDIUM;
56471d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
56571d0770cSaliguori         return -EIO;
566b338082bSbellard 
56783f64091Sbellard     if (drv->bdrv_pread) {
56883f64091Sbellard         int ret, len;
56983f64091Sbellard         len = nb_sectors * 512;
57083f64091Sbellard         ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
57183f64091Sbellard         if (ret < 0)
57283f64091Sbellard             return ret;
57383f64091Sbellard         else if (ret != len)
57419cb3738Sbellard             return -EINVAL;
575a36e69ddSths         else {
576a36e69ddSths 	    bs->rd_bytes += (unsigned) len;
577a36e69ddSths 	    bs->rd_ops ++;
57883f64091Sbellard             return 0;
579a36e69ddSths 	}
58083f64091Sbellard     } else {
58183f64091Sbellard         return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
58283f64091Sbellard     }
58383f64091Sbellard }
584fc01f7e7Sbellard 
58519cb3738Sbellard /* Return < 0 if error. Important errors are:
58619cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
58719cb3738Sbellard   -ENOMEDIUM   No media inserted.
58819cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
58919cb3738Sbellard   -EACCES      Trying to write a read-only device
59019cb3738Sbellard */
591fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
592fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
593fc01f7e7Sbellard {
59483f64091Sbellard     BlockDriver *drv = bs->drv;
59519cb3738Sbellard     if (!bs->drv)
59619cb3738Sbellard         return -ENOMEDIUM;
5970849bf08Sbellard     if (bs->read_only)
59819cb3738Sbellard         return -EACCES;
59971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
60071d0770cSaliguori         return -EIO;
60171d0770cSaliguori 
60283f64091Sbellard     if (drv->bdrv_pwrite) {
60342fb2807Saliguori         int ret, len, count = 0;
60483f64091Sbellard         len = nb_sectors * 512;
60542fb2807Saliguori         do {
60642fb2807Saliguori             ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count);
60742fb2807Saliguori             if (ret < 0) {
60842fb2807Saliguori                 printf("bdrv_write ret=%d\n", ret);
60983f64091Sbellard                 return ret;
61042fb2807Saliguori             }
61142fb2807Saliguori             count += ret;
61242fb2807Saliguori             buf += ret;
61342fb2807Saliguori         } while (count != len);
614a36e69ddSths         bs->wr_bytes += (unsigned) len;
615a36e69ddSths         bs->wr_ops ++;
61683f64091Sbellard         return 0;
617a36e69ddSths     }
61883f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
61983f64091Sbellard }
62083f64091Sbellard 
62183f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
622faea38e7Sbellard                          uint8_t *buf, int count1)
62383f64091Sbellard {
62483f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
62583f64091Sbellard     int len, nb_sectors, count;
62683f64091Sbellard     int64_t sector_num;
62783f64091Sbellard 
62883f64091Sbellard     count = count1;
62983f64091Sbellard     /* first read to align to sector start */
63083f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
63183f64091Sbellard     if (len > count)
63283f64091Sbellard         len = count;
63383f64091Sbellard     sector_num = offset >> SECTOR_BITS;
63483f64091Sbellard     if (len > 0) {
63583f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
63683f64091Sbellard             return -EIO;
63783f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
63883f64091Sbellard         count -= len;
63983f64091Sbellard         if (count == 0)
64083f64091Sbellard             return count1;
64183f64091Sbellard         sector_num++;
64283f64091Sbellard         buf += len;
64383f64091Sbellard     }
64483f64091Sbellard 
64583f64091Sbellard     /* read the sectors "in place" */
64683f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
64783f64091Sbellard     if (nb_sectors > 0) {
64883f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
64983f64091Sbellard             return -EIO;
65083f64091Sbellard         sector_num += nb_sectors;
65183f64091Sbellard         len = nb_sectors << SECTOR_BITS;
65283f64091Sbellard         buf += len;
65383f64091Sbellard         count -= len;
65483f64091Sbellard     }
65583f64091Sbellard 
65683f64091Sbellard     /* add data from the last sector */
65783f64091Sbellard     if (count > 0) {
65883f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
65983f64091Sbellard             return -EIO;
66083f64091Sbellard         memcpy(buf, tmp_buf, count);
66183f64091Sbellard     }
66283f64091Sbellard     return count1;
66383f64091Sbellard }
66483f64091Sbellard 
66583f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
666faea38e7Sbellard                           const uint8_t *buf, int count1)
66783f64091Sbellard {
66883f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
66983f64091Sbellard     int len, nb_sectors, count;
67083f64091Sbellard     int64_t sector_num;
67183f64091Sbellard 
67283f64091Sbellard     count = count1;
67383f64091Sbellard     /* first write to align to sector start */
67483f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
67583f64091Sbellard     if (len > count)
67683f64091Sbellard         len = count;
67783f64091Sbellard     sector_num = offset >> SECTOR_BITS;
67883f64091Sbellard     if (len > 0) {
67983f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
68083f64091Sbellard             return -EIO;
68183f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
68283f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
68383f64091Sbellard             return -EIO;
68483f64091Sbellard         count -= len;
68583f64091Sbellard         if (count == 0)
68683f64091Sbellard             return count1;
68783f64091Sbellard         sector_num++;
68883f64091Sbellard         buf += len;
68983f64091Sbellard     }
69083f64091Sbellard 
69183f64091Sbellard     /* write the sectors "in place" */
69283f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
69383f64091Sbellard     if (nb_sectors > 0) {
69483f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
69583f64091Sbellard             return -EIO;
69683f64091Sbellard         sector_num += nb_sectors;
69783f64091Sbellard         len = nb_sectors << SECTOR_BITS;
69883f64091Sbellard         buf += len;
69983f64091Sbellard         count -= len;
70083f64091Sbellard     }
70183f64091Sbellard 
70283f64091Sbellard     /* add data from the last sector */
70383f64091Sbellard     if (count > 0) {
70483f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
70583f64091Sbellard             return -EIO;
70683f64091Sbellard         memcpy(tmp_buf, buf, count);
70783f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
70883f64091Sbellard             return -EIO;
70983f64091Sbellard     }
71083f64091Sbellard     return count1;
71183f64091Sbellard }
71283f64091Sbellard 
71383f64091Sbellard /**
71483f64091Sbellard  * Read with byte offsets (needed only for file protocols)
71583f64091Sbellard  */
71683f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset,
71783f64091Sbellard                void *buf1, int count1)
71883f64091Sbellard {
71983f64091Sbellard     BlockDriver *drv = bs->drv;
72083f64091Sbellard 
72183f64091Sbellard     if (!drv)
72219cb3738Sbellard         return -ENOMEDIUM;
72371d0770cSaliguori     if (bdrv_check_byte_request(bs, offset, count1))
72471d0770cSaliguori         return -EIO;
72571d0770cSaliguori 
72683f64091Sbellard     if (!drv->bdrv_pread)
727faea38e7Sbellard         return bdrv_pread_em(bs, offset, buf1, count1);
72883f64091Sbellard     return drv->bdrv_pread(bs, offset, buf1, count1);
72983f64091Sbellard }
73083f64091Sbellard 
73183f64091Sbellard /**
73283f64091Sbellard  * Write with byte offsets (needed only for file protocols)
73383f64091Sbellard  */
73483f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
73583f64091Sbellard                 const void *buf1, int count1)
73683f64091Sbellard {
73783f64091Sbellard     BlockDriver *drv = bs->drv;
73883f64091Sbellard 
73983f64091Sbellard     if (!drv)
74019cb3738Sbellard         return -ENOMEDIUM;
74171d0770cSaliguori     if (bdrv_check_byte_request(bs, offset, count1))
74271d0770cSaliguori         return -EIO;
74371d0770cSaliguori 
74483f64091Sbellard     if (!drv->bdrv_pwrite)
745faea38e7Sbellard         return bdrv_pwrite_em(bs, offset, buf1, count1);
74683f64091Sbellard     return drv->bdrv_pwrite(bs, offset, buf1, count1);
74783f64091Sbellard }
74883f64091Sbellard 
74983f64091Sbellard /**
75083f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
75183f64091Sbellard  */
75283f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
75383f64091Sbellard {
75483f64091Sbellard     BlockDriver *drv = bs->drv;
75583f64091Sbellard     if (!drv)
75619cb3738Sbellard         return -ENOMEDIUM;
75783f64091Sbellard     if (!drv->bdrv_truncate)
75883f64091Sbellard         return -ENOTSUP;
75983f64091Sbellard     return drv->bdrv_truncate(bs, offset);
76083f64091Sbellard }
76183f64091Sbellard 
76283f64091Sbellard /**
76383f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
76483f64091Sbellard  */
76583f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
76683f64091Sbellard {
76783f64091Sbellard     BlockDriver *drv = bs->drv;
76883f64091Sbellard     if (!drv)
76919cb3738Sbellard         return -ENOMEDIUM;
77083f64091Sbellard     if (!drv->bdrv_getlength) {
77183f64091Sbellard         /* legacy mode */
77283f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
77383f64091Sbellard     }
77483f64091Sbellard     return drv->bdrv_getlength(bs);
775fc01f7e7Sbellard }
776fc01f7e7Sbellard 
77719cb3738Sbellard /* return 0 as number of sectors if no device present or error */
77896b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
779fc01f7e7Sbellard {
78019cb3738Sbellard     int64_t length;
78119cb3738Sbellard     length = bdrv_getlength(bs);
78219cb3738Sbellard     if (length < 0)
78319cb3738Sbellard         length = 0;
78419cb3738Sbellard     else
78519cb3738Sbellard         length = length >> SECTOR_BITS;
78619cb3738Sbellard     *nb_sectors_ptr = length;
787fc01f7e7Sbellard }
788cf98951bSbellard 
789f3d54fc4Saliguori struct partition {
790f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
791f3d54fc4Saliguori         uint8_t head;               /* starting head */
792f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
793f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
794f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
795f3d54fc4Saliguori         uint8_t end_head;           /* end head */
796f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
797f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
798f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
799f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
800f3d54fc4Saliguori } __attribute__((packed));
801f3d54fc4Saliguori 
802f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
803f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
804f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
805f3d54fc4Saliguori {
806f3d54fc4Saliguori     uint8_t buf[512];
807f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
808f3d54fc4Saliguori     struct partition *p;
809f3d54fc4Saliguori     uint32_t nr_sects;
810a38131b6Sblueswir1     uint64_t nb_sectors;
811f3d54fc4Saliguori 
812f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
813f3d54fc4Saliguori 
814f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
815f3d54fc4Saliguori     if (ret < 0)
816f3d54fc4Saliguori         return -1;
817f3d54fc4Saliguori     /* test msdos magic */
818f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
819f3d54fc4Saliguori         return -1;
820f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
821f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
822f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
823f3d54fc4Saliguori         if (nr_sects && p->end_head) {
824f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
825f3d54fc4Saliguori                a cylinder boundary */
826f3d54fc4Saliguori             heads = p->end_head + 1;
827f3d54fc4Saliguori             sectors = p->end_sector & 63;
828f3d54fc4Saliguori             if (sectors == 0)
829f3d54fc4Saliguori                 continue;
830f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
831f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
832f3d54fc4Saliguori                 continue;
833f3d54fc4Saliguori             *pheads = heads;
834f3d54fc4Saliguori             *psectors = sectors;
835f3d54fc4Saliguori             *pcylinders = cylinders;
836f3d54fc4Saliguori #if 0
837f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
838f3d54fc4Saliguori                    cylinders, heads, sectors);
839f3d54fc4Saliguori #endif
840f3d54fc4Saliguori             return 0;
841f3d54fc4Saliguori         }
842f3d54fc4Saliguori     }
843f3d54fc4Saliguori     return -1;
844f3d54fc4Saliguori }
845f3d54fc4Saliguori 
846f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
847f3d54fc4Saliguori {
848f3d54fc4Saliguori     int translation, lba_detected = 0;
849f3d54fc4Saliguori     int cylinders, heads, secs;
850a38131b6Sblueswir1     uint64_t nb_sectors;
851f3d54fc4Saliguori 
852f3d54fc4Saliguori     /* if a geometry hint is available, use it */
853f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
854f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
855f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
856f3d54fc4Saliguori     if (cylinders != 0) {
857f3d54fc4Saliguori         *pcyls = cylinders;
858f3d54fc4Saliguori         *pheads = heads;
859f3d54fc4Saliguori         *psecs = secs;
860f3d54fc4Saliguori     } else {
861f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
862f3d54fc4Saliguori             if (heads > 16) {
863f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
864f3d54fc4Saliguori                    translation was active, so the default
865f3d54fc4Saliguori                    hardware geometry is OK */
866f3d54fc4Saliguori                 lba_detected = 1;
867f3d54fc4Saliguori                 goto default_geometry;
868f3d54fc4Saliguori             } else {
869f3d54fc4Saliguori                 *pcyls = cylinders;
870f3d54fc4Saliguori                 *pheads = heads;
871f3d54fc4Saliguori                 *psecs = secs;
872f3d54fc4Saliguori                 /* disable any translation to be in sync with
873f3d54fc4Saliguori                    the logical geometry */
874f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
875f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
876f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
877f3d54fc4Saliguori                 }
878f3d54fc4Saliguori             }
879f3d54fc4Saliguori         } else {
880f3d54fc4Saliguori         default_geometry:
881f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
882f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
883f3d54fc4Saliguori 
884f3d54fc4Saliguori             if (cylinders > 16383)
885f3d54fc4Saliguori                 cylinders = 16383;
886f3d54fc4Saliguori             else if (cylinders < 2)
887f3d54fc4Saliguori                 cylinders = 2;
888f3d54fc4Saliguori             *pcyls = cylinders;
889f3d54fc4Saliguori             *pheads = 16;
890f3d54fc4Saliguori             *psecs = 63;
891f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
892f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
893f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
894f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
895f3d54fc4Saliguori                 } else {
896f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
897f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
898f3d54fc4Saliguori                 }
899f3d54fc4Saliguori             }
900f3d54fc4Saliguori         }
901f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
902f3d54fc4Saliguori     }
903f3d54fc4Saliguori }
904f3d54fc4Saliguori 
905b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
906b338082bSbellard                             int cyls, int heads, int secs)
907b338082bSbellard {
908b338082bSbellard     bs->cyls = cyls;
909b338082bSbellard     bs->heads = heads;
910b338082bSbellard     bs->secs = secs;
911b338082bSbellard }
912b338082bSbellard 
913b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
914b338082bSbellard {
915b338082bSbellard     bs->type = type;
916b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
917b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
918b338082bSbellard }
919b338082bSbellard 
92046d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
92146d4767dSbellard {
92246d4767dSbellard     bs->translation = translation;
92346d4767dSbellard }
92446d4767dSbellard 
925b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
926b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
927b338082bSbellard {
928b338082bSbellard     *pcyls = bs->cyls;
929b338082bSbellard     *pheads = bs->heads;
930b338082bSbellard     *psecs = bs->secs;
931b338082bSbellard }
932b338082bSbellard 
933b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
934b338082bSbellard {
935b338082bSbellard     return bs->type;
936b338082bSbellard }
937b338082bSbellard 
93846d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
93946d4767dSbellard {
94046d4767dSbellard     return bs->translation;
94146d4767dSbellard }
94246d4767dSbellard 
943b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
944b338082bSbellard {
945b338082bSbellard     return bs->removable;
946b338082bSbellard }
947b338082bSbellard 
948b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
949b338082bSbellard {
950b338082bSbellard     return bs->read_only;
951b338082bSbellard }
952b338082bSbellard 
953985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
954985a03b0Sths {
955985a03b0Sths     return bs->sg;
956985a03b0Sths }
957985a03b0Sths 
95819cb3738Sbellard /* XXX: no longer used */
959b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
960b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
961b338082bSbellard {
962b338082bSbellard     bs->change_cb = change_cb;
963b338082bSbellard     bs->change_opaque = opaque;
964b338082bSbellard }
965b338082bSbellard 
966ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
967ea2384d3Sbellard {
968ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
969ea2384d3Sbellard         return 1;
970ea2384d3Sbellard     return bs->encrypted;
971ea2384d3Sbellard }
972ea2384d3Sbellard 
973c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
974c0f4ce77Saliguori {
975c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
976c0f4ce77Saliguori 
977c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
978c0f4ce77Saliguori         return 1;
979c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
980c0f4ce77Saliguori }
981c0f4ce77Saliguori 
982ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
983ea2384d3Sbellard {
984ea2384d3Sbellard     int ret;
985ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
986ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
987ea2384d3Sbellard         if (ret < 0)
988ea2384d3Sbellard             return ret;
989ea2384d3Sbellard         if (!bs->encrypted)
990ea2384d3Sbellard             return 0;
991ea2384d3Sbellard     }
992ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
993ea2384d3Sbellard         return -1;
994c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
995bb5fc20fSaliguori     if (ret < 0) {
996bb5fc20fSaliguori         bs->valid_key = 0;
997bb5fc20fSaliguori     } else if (!bs->valid_key) {
998bb5fc20fSaliguori         bs->valid_key = 1;
999bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
1000bb5fc20fSaliguori         bs->media_changed = 1;
1001bb5fc20fSaliguori         if (bs->change_cb)
1002bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
1003bb5fc20fSaliguori     }
1004c0f4ce77Saliguori     return ret;
1005ea2384d3Sbellard }
1006ea2384d3Sbellard 
1007ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1008ea2384d3Sbellard {
100919cb3738Sbellard     if (!bs->drv) {
1010ea2384d3Sbellard         buf[0] = '\0';
1011ea2384d3Sbellard     } else {
1012ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
1013ea2384d3Sbellard     }
1014ea2384d3Sbellard }
1015ea2384d3Sbellard 
1016ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1017ea2384d3Sbellard                          void *opaque)
1018ea2384d3Sbellard {
1019ea2384d3Sbellard     BlockDriver *drv;
1020ea2384d3Sbellard 
1021ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
1022ea2384d3Sbellard         it(opaque, drv->format_name);
1023ea2384d3Sbellard     }
1024ea2384d3Sbellard }
1025ea2384d3Sbellard 
1026b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1027b338082bSbellard {
1028b338082bSbellard     BlockDriverState *bs;
1029b338082bSbellard 
1030b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1031b338082bSbellard         if (!strcmp(name, bs->device_name))
1032b338082bSbellard             return bs;
1033b338082bSbellard     }
1034b338082bSbellard     return NULL;
1035b338082bSbellard }
1036b338082bSbellard 
103751de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
103881d0912dSbellard {
103981d0912dSbellard     BlockDriverState *bs;
104081d0912dSbellard 
104181d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
104251de9760Saliguori         it(opaque, bs);
104381d0912dSbellard     }
104481d0912dSbellard }
104581d0912dSbellard 
1046ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1047ea2384d3Sbellard {
1048ea2384d3Sbellard     return bs->device_name;
1049ea2384d3Sbellard }
1050ea2384d3Sbellard 
10517a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
10527a6cba61Spbrook {
10537a6cba61Spbrook     if (bs->drv->bdrv_flush)
10547a6cba61Spbrook         bs->drv->bdrv_flush(bs);
10557a6cba61Spbrook     if (bs->backing_hd)
10567a6cba61Spbrook         bdrv_flush(bs->backing_hd);
10577a6cba61Spbrook }
10587a6cba61Spbrook 
1059c6ca28d6Saliguori void bdrv_flush_all(void)
1060c6ca28d6Saliguori {
1061c6ca28d6Saliguori     BlockDriverState *bs;
1062c6ca28d6Saliguori 
1063c6ca28d6Saliguori     for (bs = bdrv_first; bs != NULL; bs = bs->next)
1064c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
1065c6ca28d6Saliguori             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1066c6ca28d6Saliguori             bdrv_flush(bs);
1067c6ca28d6Saliguori }
1068c6ca28d6Saliguori 
1069f58c7b35Sths /*
1070f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1071f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1072f58c7b35Sths  * hence all their sectors are reported as allocated.
1073f58c7b35Sths  *
1074f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1075f58c7b35Sths  * the specified sector) that are known to be in the same
1076f58c7b35Sths  * allocated/unallocated state.
1077f58c7b35Sths  *
1078f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1079f58c7b35Sths  */
1080f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1081f58c7b35Sths 	int *pnum)
1082f58c7b35Sths {
1083f58c7b35Sths     int64_t n;
1084f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1085f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1086f58c7b35Sths             *pnum = 0;
1087f58c7b35Sths             return 0;
1088f58c7b35Sths         }
1089f58c7b35Sths         n = bs->total_sectors - sector_num;
1090f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1091f58c7b35Sths         return 1;
1092f58c7b35Sths     }
1093f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1094f58c7b35Sths }
1095f58c7b35Sths 
1096376253ecSaliguori void bdrv_info(Monitor *mon)
1097b338082bSbellard {
1098b338082bSbellard     BlockDriverState *bs;
1099b338082bSbellard 
1100b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1101376253ecSaliguori         monitor_printf(mon, "%s:", bs->device_name);
1102376253ecSaliguori         monitor_printf(mon, " type=");
1103b338082bSbellard         switch(bs->type) {
1104b338082bSbellard         case BDRV_TYPE_HD:
1105376253ecSaliguori             monitor_printf(mon, "hd");
1106b338082bSbellard             break;
1107b338082bSbellard         case BDRV_TYPE_CDROM:
1108376253ecSaliguori             monitor_printf(mon, "cdrom");
1109b338082bSbellard             break;
1110b338082bSbellard         case BDRV_TYPE_FLOPPY:
1111376253ecSaliguori             monitor_printf(mon, "floppy");
1112b338082bSbellard             break;
1113b338082bSbellard         }
1114376253ecSaliguori         monitor_printf(mon, " removable=%d", bs->removable);
1115b338082bSbellard         if (bs->removable) {
1116376253ecSaliguori             monitor_printf(mon, " locked=%d", bs->locked);
1117b338082bSbellard         }
111819cb3738Sbellard         if (bs->drv) {
1119376253ecSaliguori             monitor_printf(mon, " file=");
1120376253ecSaliguori             monitor_print_filename(mon, bs->filename);
1121fef30743Sths             if (bs->backing_file[0] != '\0') {
1122376253ecSaliguori                 monitor_printf(mon, " backing_file=");
1123376253ecSaliguori                 monitor_print_filename(mon, bs->backing_file);
1124fef30743Sths             }
1125376253ecSaliguori             monitor_printf(mon, " ro=%d", bs->read_only);
1126376253ecSaliguori             monitor_printf(mon, " drv=%s", bs->drv->format_name);
1127376253ecSaliguori             monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1128b338082bSbellard         } else {
1129376253ecSaliguori             monitor_printf(mon, " [not inserted]");
1130b338082bSbellard         }
1131376253ecSaliguori         monitor_printf(mon, "\n");
1132b338082bSbellard     }
1133b338082bSbellard }
1134a36e69ddSths 
1135a36e69ddSths /* The "info blockstats" command. */
1136376253ecSaliguori void bdrv_info_stats(Monitor *mon)
1137a36e69ddSths {
1138a36e69ddSths     BlockDriverState *bs;
1139a7cbfae0Saliguori     BlockDriverInfo bdi;
1140a36e69ddSths 
1141a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1142376253ecSaliguori         monitor_printf(mon, "%s:"
1143a36e69ddSths                        " rd_bytes=%" PRIu64
1144a36e69ddSths                        " wr_bytes=%" PRIu64
1145a36e69ddSths                        " rd_operations=%" PRIu64
1146a36e69ddSths                        " wr_operations=%" PRIu64
1147a7cbfae0Saliguori                        ,
1148a36e69ddSths                        bs->device_name,
1149a36e69ddSths                        bs->rd_bytes, bs->wr_bytes,
1150a36e69ddSths                        bs->rd_ops, bs->wr_ops);
1151a7cbfae0Saliguori         if (bdrv_get_info(bs, &bdi) == 0)
1152376253ecSaliguori             monitor_printf(mon, " high=%" PRId64
11531987530fSaliguori                            " bytes_free=%" PRId64,
11541987530fSaliguori                            bdi.highest_alloc, bdi.num_free_bytes);
1155376253ecSaliguori         monitor_printf(mon, "\n");
1156a36e69ddSths     }
1157a36e69ddSths }
1158ea2384d3Sbellard 
1159045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1160045df330Saliguori {
1161045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1162045df330Saliguori         return bs->backing_file;
1163045df330Saliguori     else if (bs->encrypted)
1164045df330Saliguori         return bs->filename;
1165045df330Saliguori     else
1166045df330Saliguori         return NULL;
1167045df330Saliguori }
1168045df330Saliguori 
116983f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
117083f64091Sbellard                                char *filename, int filename_size)
117183f64091Sbellard {
117283f64091Sbellard     if (!bs->backing_hd) {
117383f64091Sbellard         pstrcpy(filename, filename_size, "");
117483f64091Sbellard     } else {
117583f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
117683f64091Sbellard     }
117783f64091Sbellard }
117883f64091Sbellard 
1179faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1180faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1181faea38e7Sbellard {
1182faea38e7Sbellard     BlockDriver *drv = bs->drv;
1183faea38e7Sbellard     if (!drv)
118419cb3738Sbellard         return -ENOMEDIUM;
1185faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1186faea38e7Sbellard         return -ENOTSUP;
1187faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1188faea38e7Sbellard }
1189faea38e7Sbellard 
1190faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1191faea38e7Sbellard {
1192faea38e7Sbellard     BlockDriver *drv = bs->drv;
1193faea38e7Sbellard     if (!drv)
119419cb3738Sbellard         return -ENOMEDIUM;
1195faea38e7Sbellard     if (!drv->bdrv_get_info)
1196faea38e7Sbellard         return -ENOTSUP;
1197faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1198faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1199faea38e7Sbellard }
1200faea38e7Sbellard 
1201faea38e7Sbellard /**************************************************************/
1202faea38e7Sbellard /* handling of snapshots */
1203faea38e7Sbellard 
1204faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1205faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1206faea38e7Sbellard {
1207faea38e7Sbellard     BlockDriver *drv = bs->drv;
1208faea38e7Sbellard     if (!drv)
120919cb3738Sbellard         return -ENOMEDIUM;
1210faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
1211faea38e7Sbellard         return -ENOTSUP;
1212faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
1213faea38e7Sbellard }
1214faea38e7Sbellard 
1215faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1216faea38e7Sbellard                        const char *snapshot_id)
1217faea38e7Sbellard {
1218faea38e7Sbellard     BlockDriver *drv = bs->drv;
1219faea38e7Sbellard     if (!drv)
122019cb3738Sbellard         return -ENOMEDIUM;
1221faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
1222faea38e7Sbellard         return -ENOTSUP;
1223faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
1224faea38e7Sbellard }
1225faea38e7Sbellard 
1226faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1227faea38e7Sbellard {
1228faea38e7Sbellard     BlockDriver *drv = bs->drv;
1229faea38e7Sbellard     if (!drv)
123019cb3738Sbellard         return -ENOMEDIUM;
1231faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
1232faea38e7Sbellard         return -ENOTSUP;
1233faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1234faea38e7Sbellard }
1235faea38e7Sbellard 
1236faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1237faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1238faea38e7Sbellard {
1239faea38e7Sbellard     BlockDriver *drv = bs->drv;
1240faea38e7Sbellard     if (!drv)
124119cb3738Sbellard         return -ENOMEDIUM;
1242faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1243faea38e7Sbellard         return -ENOTSUP;
1244faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1245faea38e7Sbellard }
1246faea38e7Sbellard 
1247faea38e7Sbellard #define NB_SUFFIXES 4
1248faea38e7Sbellard 
1249faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1250faea38e7Sbellard {
1251faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1252faea38e7Sbellard     int64_t base;
1253faea38e7Sbellard     int i;
1254faea38e7Sbellard 
1255faea38e7Sbellard     if (size <= 999) {
1256faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1257faea38e7Sbellard     } else {
1258faea38e7Sbellard         base = 1024;
1259faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1260faea38e7Sbellard             if (size < (10 * base)) {
1261faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1262faea38e7Sbellard                          (double)size / base,
1263faea38e7Sbellard                          suffixes[i]);
1264faea38e7Sbellard                 break;
1265faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1266faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1267faea38e7Sbellard                          ((size + (base >> 1)) / base),
1268faea38e7Sbellard                          suffixes[i]);
1269faea38e7Sbellard                 break;
1270faea38e7Sbellard             }
1271faea38e7Sbellard             base = base * 1024;
1272faea38e7Sbellard         }
1273faea38e7Sbellard     }
1274faea38e7Sbellard     return buf;
1275faea38e7Sbellard }
1276faea38e7Sbellard 
1277faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1278faea38e7Sbellard {
1279faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
12803b9f94e1Sbellard #ifdef _WIN32
12813b9f94e1Sbellard     struct tm *ptm;
12823b9f94e1Sbellard #else
1283faea38e7Sbellard     struct tm tm;
12843b9f94e1Sbellard #endif
1285faea38e7Sbellard     time_t ti;
1286faea38e7Sbellard     int64_t secs;
1287faea38e7Sbellard 
1288faea38e7Sbellard     if (!sn) {
1289faea38e7Sbellard         snprintf(buf, buf_size,
1290faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1291faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1292faea38e7Sbellard     } else {
1293faea38e7Sbellard         ti = sn->date_sec;
12943b9f94e1Sbellard #ifdef _WIN32
12953b9f94e1Sbellard         ptm = localtime(&ti);
12963b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
12973b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
12983b9f94e1Sbellard #else
1299faea38e7Sbellard         localtime_r(&ti, &tm);
1300faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1301faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
13023b9f94e1Sbellard #endif
1303faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1304faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1305faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1306faea38e7Sbellard                  (int)(secs / 3600),
1307faea38e7Sbellard                  (int)((secs / 60) % 60),
1308faea38e7Sbellard                  (int)(secs % 60),
1309faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1310faea38e7Sbellard         snprintf(buf, buf_size,
1311faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1312faea38e7Sbellard                  sn->id_str, sn->name,
1313faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1314faea38e7Sbellard                  date_buf,
1315faea38e7Sbellard                  clock_buf);
1316faea38e7Sbellard     }
1317faea38e7Sbellard     return buf;
1318faea38e7Sbellard }
1319faea38e7Sbellard 
132083f64091Sbellard 
1321ea2384d3Sbellard /**************************************************************/
132283f64091Sbellard /* async I/Os */
1323ea2384d3Sbellard 
13243b69e4b9Saliguori typedef struct VectorTranslationState {
13253b69e4b9Saliguori     QEMUIOVector *iov;
13263b69e4b9Saliguori     uint8_t *bounce;
13273b69e4b9Saliguori     int is_write;
13283b69e4b9Saliguori     BlockDriverAIOCB *aiocb;
13293b69e4b9Saliguori     BlockDriverAIOCB *this_aiocb;
13303b69e4b9Saliguori } VectorTranslationState;
13313b69e4b9Saliguori 
13323b69e4b9Saliguori static void bdrv_aio_rw_vector_cb(void *opaque, int ret)
13333b69e4b9Saliguori {
13343b69e4b9Saliguori     VectorTranslationState *s = opaque;
13353b69e4b9Saliguori 
13363b69e4b9Saliguori     if (!s->is_write) {
1337249aa745Saliguori         qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size);
13383b69e4b9Saliguori     }
1339d905dba4Saurel32     qemu_vfree(s->bounce);
13403b69e4b9Saliguori     s->this_aiocb->cb(s->this_aiocb->opaque, ret);
13413b69e4b9Saliguori     qemu_aio_release(s->this_aiocb);
13423b69e4b9Saliguori }
13433b69e4b9Saliguori 
13443b69e4b9Saliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
13453b69e4b9Saliguori                                             int64_t sector_num,
13463b69e4b9Saliguori                                             QEMUIOVector *iov,
13473b69e4b9Saliguori                                             int nb_sectors,
13483b69e4b9Saliguori                                             BlockDriverCompletionFunc *cb,
13493b69e4b9Saliguori                                             void *opaque,
13503b69e4b9Saliguori                                             int is_write)
13513b69e4b9Saliguori 
13523b69e4b9Saliguori {
13533b69e4b9Saliguori     VectorTranslationState *s = qemu_mallocz(sizeof(*s));
13543b69e4b9Saliguori     BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque);
13553b69e4b9Saliguori 
13563b69e4b9Saliguori     s->this_aiocb = aiocb;
13573b69e4b9Saliguori     s->iov = iov;
13583b69e4b9Saliguori     s->bounce = qemu_memalign(512, nb_sectors * 512);
13593b69e4b9Saliguori     s->is_write = is_write;
13603b69e4b9Saliguori     if (is_write) {
13613b69e4b9Saliguori         qemu_iovec_to_buffer(s->iov, s->bounce);
13623b69e4b9Saliguori         s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors,
13633b69e4b9Saliguori                                   bdrv_aio_rw_vector_cb, s);
13643b69e4b9Saliguori     } else {
13653b69e4b9Saliguori         s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors,
13663b69e4b9Saliguori                                  bdrv_aio_rw_vector_cb, s);
13673b69e4b9Saliguori     }
13683b69e4b9Saliguori     return aiocb;
13693b69e4b9Saliguori }
13703b69e4b9Saliguori 
13713b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
13723b69e4b9Saliguori                                  QEMUIOVector *iov, int nb_sectors,
13733b69e4b9Saliguori                                  BlockDriverCompletionFunc *cb, void *opaque)
13743b69e4b9Saliguori {
137571d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
137671d0770cSaliguori         return NULL;
137771d0770cSaliguori 
13783b69e4b9Saliguori     return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
13793b69e4b9Saliguori                               cb, opaque, 0);
13803b69e4b9Saliguori }
13813b69e4b9Saliguori 
13823b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
13833b69e4b9Saliguori                                   QEMUIOVector *iov, int nb_sectors,
13843b69e4b9Saliguori                                   BlockDriverCompletionFunc *cb, void *opaque)
13853b69e4b9Saliguori {
138671d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
138771d0770cSaliguori         return NULL;
138871d0770cSaliguori 
13893b69e4b9Saliguori     return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors,
13903b69e4b9Saliguori                               cb, opaque, 1);
13913b69e4b9Saliguori }
13923b69e4b9Saliguori 
1393ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
139483f64091Sbellard                                 uint8_t *buf, int nb_sectors,
139583f64091Sbellard                                 BlockDriverCompletionFunc *cb, void *opaque)
1396ea2384d3Sbellard {
139783f64091Sbellard     BlockDriver *drv = bs->drv;
1398a36e69ddSths     BlockDriverAIOCB *ret;
1399ea2384d3Sbellard 
140019cb3738Sbellard     if (!drv)
1401ce1a14dcSpbrook         return NULL;
140271d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
140371d0770cSaliguori         return NULL;
140483f64091Sbellard 
1405a36e69ddSths     ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1406a36e69ddSths 
1407a36e69ddSths     if (ret) {
1408a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1409a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1410a36e69ddSths 	bs->rd_ops ++;
1411a36e69ddSths     }
1412a36e69ddSths 
1413a36e69ddSths     return ret;
141483f64091Sbellard }
141583f64091Sbellard 
1416ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
141783f64091Sbellard                                  const uint8_t *buf, int nb_sectors,
141883f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
14197674e7bfSbellard {
142083f64091Sbellard     BlockDriver *drv = bs->drv;
1421a36e69ddSths     BlockDriverAIOCB *ret;
142283f64091Sbellard 
142319cb3738Sbellard     if (!drv)
1424ce1a14dcSpbrook         return NULL;
142583f64091Sbellard     if (bs->read_only)
1426ce1a14dcSpbrook         return NULL;
142771d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
142871d0770cSaliguori         return NULL;
142983f64091Sbellard 
1430a36e69ddSths     ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1431a36e69ddSths 
1432a36e69ddSths     if (ret) {
1433a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1434a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1435a36e69ddSths 	bs->wr_ops ++;
1436a36e69ddSths     }
1437a36e69ddSths 
1438a36e69ddSths     return ret;
143983f64091Sbellard }
144083f64091Sbellard 
144183f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
144283f64091Sbellard {
1443ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
144483f64091Sbellard 
14453b69e4b9Saliguori     if (acb->cb == bdrv_aio_rw_vector_cb) {
14463b69e4b9Saliguori         VectorTranslationState *s = acb->opaque;
14473b69e4b9Saliguori         acb = s->aiocb;
14483b69e4b9Saliguori     }
14493b69e4b9Saliguori 
145083f64091Sbellard     drv->bdrv_aio_cancel(acb);
145183f64091Sbellard }
145283f64091Sbellard 
145383f64091Sbellard 
145483f64091Sbellard /**************************************************************/
145583f64091Sbellard /* async block device emulation */
145683f64091Sbellard 
145783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1458beac80cdSbellard {
1459ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1460ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1461ce1a14dcSpbrook     qemu_aio_release(acb);
1462beac80cdSbellard }
1463beac80cdSbellard 
1464ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1465ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1466ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1467ea2384d3Sbellard {
1468ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
146983f64091Sbellard     int ret;
147083f64091Sbellard 
1471ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1472ce1a14dcSpbrook     if (!acb->bh)
1473ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1474ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1475ce1a14dcSpbrook     acb->ret = ret;
1476ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1477ce1a14dcSpbrook     return &acb->common;
14787a6cba61Spbrook }
14797a6cba61Spbrook 
1480ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1481ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1482ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
148383f64091Sbellard {
1484ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
148583f64091Sbellard     int ret;
148683f64091Sbellard 
1487ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1488ce1a14dcSpbrook     if (!acb->bh)
1489ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1490ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1491ce1a14dcSpbrook     acb->ret = ret;
1492ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1493ce1a14dcSpbrook     return &acb->common;
149483f64091Sbellard }
149583f64091Sbellard 
1496ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
149783f64091Sbellard {
1498ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1499ce1a14dcSpbrook     qemu_bh_cancel(acb->bh);
1500ce1a14dcSpbrook     qemu_aio_release(acb);
150183f64091Sbellard }
150283f64091Sbellard 
150383f64091Sbellard /**************************************************************/
150483f64091Sbellard /* sync block device emulation */
150583f64091Sbellard 
150683f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
150783f64091Sbellard {
150883f64091Sbellard     *(int *)opaque = ret;
150983f64091Sbellard }
151083f64091Sbellard 
151183f64091Sbellard #define NOT_DONE 0x7fffffff
151283f64091Sbellard 
151383f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
151483f64091Sbellard                         uint8_t *buf, int nb_sectors)
151583f64091Sbellard {
1516ce1a14dcSpbrook     int async_ret;
1517ce1a14dcSpbrook     BlockDriverAIOCB *acb;
151883f64091Sbellard 
151983f64091Sbellard     async_ret = NOT_DONE;
1520ce1a14dcSpbrook     acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
152183f64091Sbellard                         bdrv_rw_em_cb, &async_ret);
1522baf35cb9Saliguori     if (acb == NULL)
1523ce1a14dcSpbrook         return -1;
1524baf35cb9Saliguori 
152583f64091Sbellard     while (async_ret == NOT_DONE) {
152683f64091Sbellard         qemu_aio_wait();
152783f64091Sbellard     }
1528baf35cb9Saliguori 
152983f64091Sbellard     return async_ret;
153083f64091Sbellard }
153183f64091Sbellard 
153283f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
153383f64091Sbellard                          const uint8_t *buf, int nb_sectors)
153483f64091Sbellard {
1535ce1a14dcSpbrook     int async_ret;
1536ce1a14dcSpbrook     BlockDriverAIOCB *acb;
153783f64091Sbellard 
153883f64091Sbellard     async_ret = NOT_DONE;
1539ce1a14dcSpbrook     acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
154083f64091Sbellard                          bdrv_rw_em_cb, &async_ret);
1541baf35cb9Saliguori     if (acb == NULL)
1542ce1a14dcSpbrook         return -1;
154383f64091Sbellard     while (async_ret == NOT_DONE) {
154483f64091Sbellard         qemu_aio_wait();
154583f64091Sbellard     }
154683f64091Sbellard     return async_ret;
154783f64091Sbellard }
1548ea2384d3Sbellard 
1549ea2384d3Sbellard void bdrv_init(void)
1550ea2384d3Sbellard {
1551ea2384d3Sbellard     bdrv_register(&bdrv_raw);
155219cb3738Sbellard     bdrv_register(&bdrv_host_device);
1553ea2384d3Sbellard #ifndef _WIN32
1554ea2384d3Sbellard     bdrv_register(&bdrv_cow);
1555ea2384d3Sbellard #endif
1556ea2384d3Sbellard     bdrv_register(&bdrv_qcow);
1557ea2384d3Sbellard     bdrv_register(&bdrv_vmdk);
15583c56521bSbellard     bdrv_register(&bdrv_cloop);
1559585d0ed9Sbellard     bdrv_register(&bdrv_dmg);
1560a8753c34Sbellard     bdrv_register(&bdrv_bochs);
15616a0f9e82Sbellard     bdrv_register(&bdrv_vpc);
1562712e7874Sbellard     bdrv_register(&bdrv_vvfat);
1563faea38e7Sbellard     bdrv_register(&bdrv_qcow2);
15646ada7453Sths     bdrv_register(&bdrv_parallels);
156575818250Sths     bdrv_register(&bdrv_nbd);
1566ea2384d3Sbellard }
1567ce1a14dcSpbrook 
1568ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1569ce1a14dcSpbrook                    void *opaque)
1570ce1a14dcSpbrook {
1571ce1a14dcSpbrook     BlockDriver *drv;
1572ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1573ce1a14dcSpbrook 
1574ce1a14dcSpbrook     drv = bs->drv;
1575ce1a14dcSpbrook     if (drv->free_aiocb) {
1576ce1a14dcSpbrook         acb = drv->free_aiocb;
1577ce1a14dcSpbrook         drv->free_aiocb = acb->next;
1578ce1a14dcSpbrook     } else {
1579ce1a14dcSpbrook         acb = qemu_mallocz(drv->aiocb_size);
1580ce1a14dcSpbrook     }
1581ce1a14dcSpbrook     acb->bs = bs;
1582ce1a14dcSpbrook     acb->cb = cb;
1583ce1a14dcSpbrook     acb->opaque = opaque;
1584ce1a14dcSpbrook     return acb;
1585ce1a14dcSpbrook }
1586ce1a14dcSpbrook 
1587ce1a14dcSpbrook void qemu_aio_release(void *p)
1588ce1a14dcSpbrook {
1589ce1a14dcSpbrook     BlockDriverAIOCB *acb = p;
1590ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
1591ce1a14dcSpbrook     acb->next = drv->free_aiocb;
1592ce1a14dcSpbrook     drv->free_aiocb = acb;
1593ce1a14dcSpbrook }
159419cb3738Sbellard 
159519cb3738Sbellard /**************************************************************/
159619cb3738Sbellard /* removable device support */
159719cb3738Sbellard 
159819cb3738Sbellard /**
159919cb3738Sbellard  * Return TRUE if the media is present
160019cb3738Sbellard  */
160119cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
160219cb3738Sbellard {
160319cb3738Sbellard     BlockDriver *drv = bs->drv;
160419cb3738Sbellard     int ret;
160519cb3738Sbellard     if (!drv)
160619cb3738Sbellard         return 0;
160719cb3738Sbellard     if (!drv->bdrv_is_inserted)
160819cb3738Sbellard         return 1;
160919cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
161019cb3738Sbellard     return ret;
161119cb3738Sbellard }
161219cb3738Sbellard 
161319cb3738Sbellard /**
161419cb3738Sbellard  * Return TRUE if the media changed since the last call to this
161519cb3738Sbellard  * function. It is currently only used for floppy disks
161619cb3738Sbellard  */
161719cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
161819cb3738Sbellard {
161919cb3738Sbellard     BlockDriver *drv = bs->drv;
162019cb3738Sbellard     int ret;
162119cb3738Sbellard 
162219cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
162319cb3738Sbellard         ret = -ENOTSUP;
162419cb3738Sbellard     else
162519cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
162619cb3738Sbellard     if (ret == -ENOTSUP)
162719cb3738Sbellard         ret = bs->media_changed;
162819cb3738Sbellard     bs->media_changed = 0;
162919cb3738Sbellard     return ret;
163019cb3738Sbellard }
163119cb3738Sbellard 
163219cb3738Sbellard /**
163319cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
163419cb3738Sbellard  */
163519cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
163619cb3738Sbellard {
163719cb3738Sbellard     BlockDriver *drv = bs->drv;
163819cb3738Sbellard     int ret;
163919cb3738Sbellard 
164019cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
164119cb3738Sbellard         ret = -ENOTSUP;
164219cb3738Sbellard     } else {
164319cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
164419cb3738Sbellard     }
164519cb3738Sbellard     if (ret == -ENOTSUP) {
164619cb3738Sbellard         if (eject_flag)
164719cb3738Sbellard             bdrv_close(bs);
164819cb3738Sbellard     }
164919cb3738Sbellard }
165019cb3738Sbellard 
165119cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
165219cb3738Sbellard {
165319cb3738Sbellard     return bs->locked;
165419cb3738Sbellard }
165519cb3738Sbellard 
165619cb3738Sbellard /**
165719cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
165819cb3738Sbellard  * to eject it manually).
165919cb3738Sbellard  */
166019cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
166119cb3738Sbellard {
166219cb3738Sbellard     BlockDriver *drv = bs->drv;
166319cb3738Sbellard 
166419cb3738Sbellard     bs->locked = locked;
166519cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
166619cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
166719cb3738Sbellard     }
166819cb3738Sbellard }
1669985a03b0Sths 
1670985a03b0Sths /* needed for generic scsi interface */
1671985a03b0Sths 
1672985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1673985a03b0Sths {
1674985a03b0Sths     BlockDriver *drv = bs->drv;
1675985a03b0Sths 
1676985a03b0Sths     if (drv && drv->bdrv_ioctl)
1677985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1678985a03b0Sths     return -ENOTSUP;
1679985a03b0Sths }
1680