xref: /openbmc/qemu/block.c (revision 6a7ad2998ca47711eddd191caa1a95f14459793f)
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"
335efa9d5aSAnthony Liguori #include "module.h"
34fc01f7e7Sbellard 
35179a2c19Sblueswir1 #ifdef HOST_BSD
367674e7bfSbellard #include <sys/types.h>
377674e7bfSbellard #include <sys/stat.h>
387674e7bfSbellard #include <sys/ioctl.h>
39c5e97233Sblueswir1 #ifndef __DragonFly__
407674e7bfSbellard #include <sys/disk.h>
417674e7bfSbellard #endif
42c5e97233Sblueswir1 #endif
437674e7bfSbellard 
4449dc768dSaliguori #ifdef _WIN32
4549dc768dSaliguori #include <windows.h>
4649dc768dSaliguori #endif
4749dc768dSaliguori 
4883f64091Sbellard #define SECTOR_BITS 9
4983f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS)
503b0d4f61Sbellard 
51f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
52f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
53c87c0672Saliguori         BlockDriverCompletionFunc *cb, void *opaque);
54f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
55f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
56ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
5783f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
5883f64091Sbellard                         uint8_t *buf, int nb_sectors);
5983f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
6083f64091Sbellard                          const uint8_t *buf, int nb_sectors);
61ec530c81Sbellard 
627ee930d0Sblueswir1 BlockDriverState *bdrv_first;
637ee930d0Sblueswir1 
64ea2384d3Sbellard static BlockDriver *first_drv;
65ea2384d3Sbellard 
6683f64091Sbellard int path_is_absolute(const char *path)
6783f64091Sbellard {
6883f64091Sbellard     const char *p;
6921664424Sbellard #ifdef _WIN32
7021664424Sbellard     /* specific case for names like: "\\.\d:" */
7121664424Sbellard     if (*path == '/' || *path == '\\')
7221664424Sbellard         return 1;
7321664424Sbellard #endif
7483f64091Sbellard     p = strchr(path, ':');
7583f64091Sbellard     if (p)
7683f64091Sbellard         p++;
7783f64091Sbellard     else
7883f64091Sbellard         p = path;
793b9f94e1Sbellard #ifdef _WIN32
803b9f94e1Sbellard     return (*p == '/' || *p == '\\');
813b9f94e1Sbellard #else
823b9f94e1Sbellard     return (*p == '/');
833b9f94e1Sbellard #endif
8483f64091Sbellard }
8583f64091Sbellard 
8683f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
8783f64091Sbellard    path to it by considering it is relative to base_path. URL are
8883f64091Sbellard    supported. */
8983f64091Sbellard void path_combine(char *dest, int dest_size,
9083f64091Sbellard                   const char *base_path,
9183f64091Sbellard                   const char *filename)
9283f64091Sbellard {
9383f64091Sbellard     const char *p, *p1;
9483f64091Sbellard     int len;
9583f64091Sbellard 
9683f64091Sbellard     if (dest_size <= 0)
9783f64091Sbellard         return;
9883f64091Sbellard     if (path_is_absolute(filename)) {
9983f64091Sbellard         pstrcpy(dest, dest_size, filename);
10083f64091Sbellard     } else {
10183f64091Sbellard         p = strchr(base_path, ':');
10283f64091Sbellard         if (p)
10383f64091Sbellard             p++;
10483f64091Sbellard         else
10583f64091Sbellard             p = base_path;
1063b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1073b9f94e1Sbellard #ifdef _WIN32
1083b9f94e1Sbellard         {
1093b9f94e1Sbellard             const char *p2;
1103b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1113b9f94e1Sbellard             if (!p1 || p2 > p1)
1123b9f94e1Sbellard                 p1 = p2;
1133b9f94e1Sbellard         }
1143b9f94e1Sbellard #endif
11583f64091Sbellard         if (p1)
11683f64091Sbellard             p1++;
11783f64091Sbellard         else
11883f64091Sbellard             p1 = base_path;
11983f64091Sbellard         if (p1 > p)
12083f64091Sbellard             p = p1;
12183f64091Sbellard         len = p - base_path;
12283f64091Sbellard         if (len > dest_size - 1)
12383f64091Sbellard             len = dest_size - 1;
12483f64091Sbellard         memcpy(dest, base_path, len);
12583f64091Sbellard         dest[len] = '\0';
12683f64091Sbellard         pstrcat(dest, dest_size, filename);
12783f64091Sbellard     }
12883f64091Sbellard }
12983f64091Sbellard 
1305efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv)
131ea2384d3Sbellard {
132f141eafeSaliguori     if (!bdrv->bdrv_aio_readv) {
13383f64091Sbellard         /* add AIO emulation layer */
134f141eafeSaliguori         bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
135f141eafeSaliguori         bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
136eda578e5Saliguori     } else if (!bdrv->bdrv_read) {
13783f64091Sbellard         /* add synchronous IO emulation layer */
13883f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
13983f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
14083f64091Sbellard     }
141ea2384d3Sbellard     bdrv->next = first_drv;
142ea2384d3Sbellard     first_drv = bdrv;
143ea2384d3Sbellard }
144b338082bSbellard 
145b338082bSbellard /* create a new block device (by default it is empty) */
146b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
147fc01f7e7Sbellard {
148b338082bSbellard     BlockDriverState **pbs, *bs;
149b338082bSbellard 
150b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
151b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
152ea2384d3Sbellard     if (device_name[0] != '\0') {
153b338082bSbellard         /* insert at the end */
154b338082bSbellard         pbs = &bdrv_first;
155b338082bSbellard         while (*pbs != NULL)
156b338082bSbellard             pbs = &(*pbs)->next;
157b338082bSbellard         *pbs = bs;
158ea2384d3Sbellard     }
159b338082bSbellard     return bs;
160b338082bSbellard }
161b338082bSbellard 
162ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
163ea2384d3Sbellard {
164ea2384d3Sbellard     BlockDriver *drv1;
165ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
166ea2384d3Sbellard         if (!strcmp(drv1->format_name, format_name))
167ea2384d3Sbellard             return drv1;
168ea2384d3Sbellard     }
169ea2384d3Sbellard     return NULL;
170ea2384d3Sbellard }
171ea2384d3Sbellard 
1720e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename,
1730e7e1989SKevin Wolf     QEMUOptionParameter *options)
174ea2384d3Sbellard {
175ea2384d3Sbellard     if (!drv->bdrv_create)
176ea2384d3Sbellard         return -ENOTSUP;
1770e7e1989SKevin Wolf 
1780e7e1989SKevin Wolf     return drv->bdrv_create(filename, options);
179ea2384d3Sbellard }
180ea2384d3Sbellard 
181d5249393Sbellard #ifdef _WIN32
18295389c86Sbellard void get_tmp_filename(char *filename, int size)
183d5249393Sbellard {
1843b9f94e1Sbellard     char temp_dir[MAX_PATH];
1853b9f94e1Sbellard 
1863b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
1873b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
188d5249393Sbellard }
189d5249393Sbellard #else
19095389c86Sbellard void get_tmp_filename(char *filename, int size)
191ea2384d3Sbellard {
192ea2384d3Sbellard     int fd;
1937ccfb2ebSblueswir1     const char *tmpdir;
194d5249393Sbellard     /* XXX: race condition possible */
1950badc1eeSaurel32     tmpdir = getenv("TMPDIR");
1960badc1eeSaurel32     if (!tmpdir)
1970badc1eeSaurel32         tmpdir = "/tmp";
1980badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
199ea2384d3Sbellard     fd = mkstemp(filename);
200ea2384d3Sbellard     close(fd);
201ea2384d3Sbellard }
202d5249393Sbellard #endif
203ea2384d3Sbellard 
20419cb3738Sbellard #ifdef _WIN32
205f45512feSbellard static int is_windows_drive_prefix(const char *filename)
206f45512feSbellard {
207f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
208f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
209f45512feSbellard             filename[1] == ':');
210f45512feSbellard }
211f45512feSbellard 
212508c7cb3SChristoph Hellwig int is_windows_drive(const char *filename)
21319cb3738Sbellard {
214f45512feSbellard     if (is_windows_drive_prefix(filename) &&
215f45512feSbellard         filename[2] == '\0')
21619cb3738Sbellard         return 1;
21719cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
21819cb3738Sbellard         strstart(filename, "//./", NULL))
21919cb3738Sbellard         return 1;
22019cb3738Sbellard     return 0;
22119cb3738Sbellard }
22219cb3738Sbellard #endif
22319cb3738Sbellard 
22483f64091Sbellard static BlockDriver *find_protocol(const char *filename)
22583f64091Sbellard {
22683f64091Sbellard     BlockDriver *drv1;
22783f64091Sbellard     char protocol[128];
22883f64091Sbellard     int len;
22983f64091Sbellard     const char *p;
23019cb3738Sbellard 
23119cb3738Sbellard #ifdef _WIN32
232f45512feSbellard     if (is_windows_drive(filename) ||
233f45512feSbellard         is_windows_drive_prefix(filename))
2345efa9d5aSAnthony Liguori         return bdrv_find_format("raw");
23519cb3738Sbellard #endif
23683f64091Sbellard     p = strchr(filename, ':');
23783f64091Sbellard     if (!p)
2385efa9d5aSAnthony Liguori         return bdrv_find_format("raw");
23983f64091Sbellard     len = p - filename;
24083f64091Sbellard     if (len > sizeof(protocol) - 1)
24183f64091Sbellard         len = sizeof(protocol) - 1;
24283f64091Sbellard     memcpy(protocol, filename, len);
24383f64091Sbellard     protocol[len] = '\0';
24483f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
24583f64091Sbellard         if (drv1->protocol_name &&
24683f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
24783f64091Sbellard             return drv1;
24883f64091Sbellard     }
24983f64091Sbellard     return NULL;
25083f64091Sbellard }
25183f64091Sbellard 
252f3a5d3f8SChristoph Hellwig /*
253f3a5d3f8SChristoph Hellwig  * Detect host devices. By convention, /dev/cdrom[N] is always
254f3a5d3f8SChristoph Hellwig  * recognized as a host CDROM.
255f3a5d3f8SChristoph Hellwig  */
256f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename)
257f3a5d3f8SChristoph Hellwig {
258508c7cb3SChristoph Hellwig     int score_max = 0, score;
259508c7cb3SChristoph Hellwig     BlockDriver *drv = NULL, *d;
260f3a5d3f8SChristoph Hellwig 
261508c7cb3SChristoph Hellwig     for (d = first_drv; d; d = d->next) {
262508c7cb3SChristoph Hellwig         if (d->bdrv_probe_device) {
263508c7cb3SChristoph Hellwig             score = d->bdrv_probe_device(filename);
264508c7cb3SChristoph Hellwig             if (score > score_max) {
265508c7cb3SChristoph Hellwig                 score_max = score;
266508c7cb3SChristoph Hellwig                 drv = d;
267f3a5d3f8SChristoph Hellwig             }
268508c7cb3SChristoph Hellwig         }
269f3a5d3f8SChristoph Hellwig     }
270f3a5d3f8SChristoph Hellwig 
271508c7cb3SChristoph Hellwig     return drv;
272f3a5d3f8SChristoph Hellwig }
273f3a5d3f8SChristoph Hellwig 
274ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
275ea2384d3Sbellard {
27683f64091Sbellard     int ret, score, score_max;
277ea2384d3Sbellard     BlockDriver *drv1, *drv;
27883f64091Sbellard     uint8_t buf[2048];
27983f64091Sbellard     BlockDriverState *bs;
280ea2384d3Sbellard 
28183f64091Sbellard     drv = find_protocol(filename);
28219cb3738Sbellard     /* no need to test disk image formats for vvfat */
283c833ab73SAnthony Liguori     if (drv && strcmp(drv->format_name, "vvfat") == 0)
28483f64091Sbellard         return drv;
28583f64091Sbellard 
28683f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
28783f64091Sbellard     if (ret < 0)
2887674e7bfSbellard         return NULL;
28983f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
29083f64091Sbellard     bdrv_delete(bs);
291ea2384d3Sbellard     if (ret < 0) {
292ea2384d3Sbellard         return NULL;
293ea2384d3Sbellard     }
294ea2384d3Sbellard 
295ea2384d3Sbellard     score_max = 0;
296ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
29783f64091Sbellard         if (drv1->bdrv_probe) {
298ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
299ea2384d3Sbellard             if (score > score_max) {
300ea2384d3Sbellard                 score_max = score;
301ea2384d3Sbellard                 drv = drv1;
302ea2384d3Sbellard             }
303ea2384d3Sbellard         }
30483f64091Sbellard     }
305ea2384d3Sbellard     return drv;
306ea2384d3Sbellard }
307ea2384d3Sbellard 
30883f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
309b338082bSbellard {
31083f64091Sbellard     BlockDriverState *bs;
31183f64091Sbellard     int ret;
3123b0d4f61Sbellard 
31383f64091Sbellard     bs = bdrv_new("");
31483f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
31583f64091Sbellard     if (ret < 0) {
31683f64091Sbellard         bdrv_delete(bs);
31783f64091Sbellard         return ret;
3183b0d4f61Sbellard     }
31971d0770cSaliguori     bs->growable = 1;
32083f64091Sbellard     *pbs = bs;
32183f64091Sbellard     return 0;
3223b0d4f61Sbellard }
3233b0d4f61Sbellard 
32483f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
32583f64091Sbellard {
32683f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
327ea2384d3Sbellard }
328ea2384d3Sbellard 
32983f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
330ea2384d3Sbellard                BlockDriver *drv)
331ea2384d3Sbellard {
33283f64091Sbellard     int ret, open_flags;
333eb5c851fSths     char tmp_filename[PATH_MAX];
334eb5c851fSths     char backing_filename[PATH_MAX];
335fc01f7e7Sbellard 
3360849bf08Sbellard     bs->read_only = 0;
337ea2384d3Sbellard     bs->is_temporary = 0;
338ea2384d3Sbellard     bs->encrypted = 0;
339c0f4ce77Saliguori     bs->valid_key = 0;
340e268ca52Saliguori     /* buffer_alignment defaulted to 512, drivers can change this value */
341e268ca52Saliguori     bs->buffer_alignment = 512;
34233e3963eSbellard 
34383f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
344ea2384d3Sbellard         BlockDriverState *bs1;
345ea2384d3Sbellard         int64_t total_size;
3467c96d46eSaliguori         int is_protocol = 0;
34791a073a9SKevin Wolf         BlockDriver *bdrv_qcow2;
34891a073a9SKevin Wolf         QEMUOptionParameter *options;
34933e3963eSbellard 
350ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
351ea2384d3Sbellard            instead of opening 'filename' directly */
352ea2384d3Sbellard 
353ea2384d3Sbellard         /* if there is a backing file, use it */
354ea2384d3Sbellard         bs1 = bdrv_new("");
3555eb45639Saliguori         ret = bdrv_open2(bs1, filename, 0, drv);
35651d7c00cSaliguori         if (ret < 0) {
357ea2384d3Sbellard             bdrv_delete(bs1);
35851d7c00cSaliguori             return ret;
359ea2384d3Sbellard         }
36083f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
3617c96d46eSaliguori 
3627c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
3637c96d46eSaliguori             is_protocol = 1;
3647c96d46eSaliguori 
365ea2384d3Sbellard         bdrv_delete(bs1);
366ea2384d3Sbellard 
367ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
3687c96d46eSaliguori 
3697c96d46eSaliguori         /* Real path is meaningless for protocols */
3707c96d46eSaliguori         if (is_protocol)
3717c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
3727c96d46eSaliguori                      "%s", filename);
3737c96d46eSaliguori         else
374a817d936Sbellard             realpath(filename, backing_filename);
3757c96d46eSaliguori 
37691a073a9SKevin Wolf         bdrv_qcow2 = bdrv_find_format("qcow2");
37791a073a9SKevin Wolf         options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
37891a073a9SKevin Wolf 
37991a073a9SKevin Wolf         set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
38091a073a9SKevin Wolf         set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
38191a073a9SKevin Wolf         if (drv) {
38291a073a9SKevin Wolf             set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
38391a073a9SKevin Wolf                 drv->format_name);
38491a073a9SKevin Wolf         }
38591a073a9SKevin Wolf 
38691a073a9SKevin Wolf         ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
38751d7c00cSaliguori         if (ret < 0) {
38851d7c00cSaliguori             return ret;
389ea2384d3Sbellard         }
39091a073a9SKevin Wolf 
391ea2384d3Sbellard         filename = tmp_filename;
39291a073a9SKevin Wolf         drv = bdrv_qcow2;
393ea2384d3Sbellard         bs->is_temporary = 1;
394ea2384d3Sbellard     }
395ea2384d3Sbellard 
396ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
39783f64091Sbellard     if (flags & BDRV_O_FILE) {
39883f64091Sbellard         drv = find_protocol(filename);
39951d7c00cSaliguori     } else if (!drv) {
400f3a5d3f8SChristoph Hellwig         drv = find_hdev_driver(filename);
401f3a5d3f8SChristoph Hellwig         if (!drv) {
402ea2384d3Sbellard             drv = find_image_format(filename);
403ea2384d3Sbellard         }
404f3a5d3f8SChristoph Hellwig     }
40551d7c00cSaliguori     if (!drv) {
40651d7c00cSaliguori         ret = -ENOENT;
40751d7c00cSaliguori         goto unlink_and_fail;
40883f64091Sbellard     }
409ea2384d3Sbellard     bs->drv = drv;
410ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
41183f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
41283f64091Sbellard        RDONLY as fallback */
41383f64091Sbellard     if (!(flags & BDRV_O_FILE))
4149f7965c7Saliguori         open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
41583f64091Sbellard     else
41683f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
41783f64091Sbellard     ret = drv->bdrv_open(bs, filename, open_flags);
418a0a83536Saurel32     if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
4199f7965c7Saliguori         ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
42083f64091Sbellard         bs->read_only = 1;
42183f64091Sbellard     }
422ea2384d3Sbellard     if (ret < 0) {
423ea2384d3Sbellard         qemu_free(bs->opaque);
4246b21b973Sbellard         bs->opaque = NULL;
4256b21b973Sbellard         bs->drv = NULL;
42651d7c00cSaliguori     unlink_and_fail:
42751d7c00cSaliguori         if (bs->is_temporary)
42851d7c00cSaliguori             unlink(filename);
42983f64091Sbellard         return ret;
430ea2384d3Sbellard     }
431d15a771dSbellard     if (drv->bdrv_getlength) {
432d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
433d15a771dSbellard     }
434ea2384d3Sbellard #ifndef _WIN32
435ea2384d3Sbellard     if (bs->is_temporary) {
436ea2384d3Sbellard         unlink(filename);
43733e3963eSbellard     }
43867b915a5Sbellard #endif
43983f64091Sbellard     if (bs->backing_file[0] != '\0') {
440ea2384d3Sbellard         /* if there is a backing file, use it */
4415eb45639Saliguori         BlockDriver *back_drv = NULL;
442ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
44383f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
44483f64091Sbellard                      filename, bs->backing_file);
4455eb45639Saliguori         if (bs->backing_format[0] != '\0')
4465eb45639Saliguori             back_drv = bdrv_find_format(bs->backing_format);
4475eb45639Saliguori         ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
4485eb45639Saliguori                          back_drv);
44951d7c00cSaliguori         if (ret < 0) {
45051d7c00cSaliguori             bdrv_close(bs);
45151d7c00cSaliguori             return ret;
45251d7c00cSaliguori         }
453ea2384d3Sbellard     }
45433e3963eSbellard 
455bb5fc20fSaliguori     if (!bdrv_key_required(bs)) {
456b338082bSbellard         /* call the change callback */
45719cb3738Sbellard         bs->media_changed = 1;
458b338082bSbellard         if (bs->change_cb)
459b338082bSbellard             bs->change_cb(bs->change_opaque);
460bb5fc20fSaliguori     }
461b338082bSbellard     return 0;
462fc01f7e7Sbellard }
463fc01f7e7Sbellard 
464fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
465fc01f7e7Sbellard {
46619cb3738Sbellard     if (bs->drv) {
467ea2384d3Sbellard         if (bs->backing_hd)
468ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
469ea2384d3Sbellard         bs->drv->bdrv_close(bs);
470ea2384d3Sbellard         qemu_free(bs->opaque);
471ea2384d3Sbellard #ifdef _WIN32
472ea2384d3Sbellard         if (bs->is_temporary) {
473ea2384d3Sbellard             unlink(bs->filename);
474ea2384d3Sbellard         }
47567b915a5Sbellard #endif
476ea2384d3Sbellard         bs->opaque = NULL;
477ea2384d3Sbellard         bs->drv = NULL;
478b338082bSbellard 
479b338082bSbellard         /* call the change callback */
48019cb3738Sbellard         bs->media_changed = 1;
481b338082bSbellard         if (bs->change_cb)
482b338082bSbellard             bs->change_cb(bs->change_opaque);
483b338082bSbellard     }
484b338082bSbellard }
485b338082bSbellard 
486b338082bSbellard void bdrv_delete(BlockDriverState *bs)
487b338082bSbellard {
48834c6f050Saurel32     BlockDriverState **pbs;
48934c6f050Saurel32 
49034c6f050Saurel32     pbs = &bdrv_first;
49134c6f050Saurel32     while (*pbs != bs && *pbs != NULL)
49234c6f050Saurel32         pbs = &(*pbs)->next;
49334c6f050Saurel32     if (*pbs == bs)
49434c6f050Saurel32         *pbs = bs->next;
49534c6f050Saurel32 
496b338082bSbellard     bdrv_close(bs);
497b338082bSbellard     qemu_free(bs);
498fc01f7e7Sbellard }
499fc01f7e7Sbellard 
500e97fc193Saliguori /*
501e97fc193Saliguori  * Run consistency checks on an image
502e97fc193Saliguori  *
503e97fc193Saliguori  * Returns the number of errors or -errno when an internal error occurs
504e97fc193Saliguori  */
505e97fc193Saliguori int bdrv_check(BlockDriverState *bs)
506e97fc193Saliguori {
507e97fc193Saliguori     if (bs->drv->bdrv_check == NULL) {
508e97fc193Saliguori         return -ENOTSUP;
509e97fc193Saliguori     }
510e97fc193Saliguori 
511e97fc193Saliguori     return bs->drv->bdrv_check(bs);
512e97fc193Saliguori }
513e97fc193Saliguori 
51433e3963eSbellard /* commit COW file into the raw image */
51533e3963eSbellard int bdrv_commit(BlockDriverState *bs)
51633e3963eSbellard {
51719cb3738Sbellard     BlockDriver *drv = bs->drv;
51883f64091Sbellard     int64_t i, total_sectors;
519ea2384d3Sbellard     int n, j;
520ea2384d3Sbellard     unsigned char sector[512];
52133e3963eSbellard 
52219cb3738Sbellard     if (!drv)
52319cb3738Sbellard         return -ENOMEDIUM;
52433e3963eSbellard 
52533e3963eSbellard     if (bs->read_only) {
526ea2384d3Sbellard 	return -EACCES;
52733e3963eSbellard     }
52833e3963eSbellard 
529ea2384d3Sbellard     if (!bs->backing_hd) {
530ea2384d3Sbellard 	return -ENOTSUP;
531ea2384d3Sbellard     }
532ea2384d3Sbellard 
53383f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
53483f64091Sbellard     for (i = 0; i < total_sectors;) {
53519cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
536ea2384d3Sbellard             for(j = 0; j < n; j++) {
53733e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
538ea2384d3Sbellard                     return -EIO;
53933e3963eSbellard                 }
54033e3963eSbellard 
541ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
542ea2384d3Sbellard                     return -EIO;
54333e3963eSbellard                 }
544ea2384d3Sbellard                 i++;
545ea2384d3Sbellard 	    }
546ea2384d3Sbellard 	} else {
547ea2384d3Sbellard             i += n;
54833e3963eSbellard         }
54933e3963eSbellard     }
55095389c86Sbellard 
55119cb3738Sbellard     if (drv->bdrv_make_empty)
55219cb3738Sbellard 	return drv->bdrv_make_empty(bs);
55395389c86Sbellard 
55433e3963eSbellard     return 0;
55533e3963eSbellard }
55633e3963eSbellard 
55771d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
55871d0770cSaliguori                                    size_t size)
55971d0770cSaliguori {
56071d0770cSaliguori     int64_t len;
56171d0770cSaliguori 
56271d0770cSaliguori     if (!bdrv_is_inserted(bs))
56371d0770cSaliguori         return -ENOMEDIUM;
56471d0770cSaliguori 
56571d0770cSaliguori     if (bs->growable)
56671d0770cSaliguori         return 0;
56771d0770cSaliguori 
56871d0770cSaliguori     len = bdrv_getlength(bs);
56971d0770cSaliguori 
570fbb7b4e0SKevin Wolf     if (offset < 0)
571fbb7b4e0SKevin Wolf         return -EIO;
572fbb7b4e0SKevin Wolf 
573fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
57471d0770cSaliguori         return -EIO;
57571d0770cSaliguori 
57671d0770cSaliguori     return 0;
57771d0770cSaliguori }
57871d0770cSaliguori 
57971d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
58071d0770cSaliguori                               int nb_sectors)
58171d0770cSaliguori {
582999dec57Saliguori     return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
58371d0770cSaliguori }
58471d0770cSaliguori 
58519cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
586fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
587fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
588fc01f7e7Sbellard {
589ea2384d3Sbellard     BlockDriver *drv = bs->drv;
590fc01f7e7Sbellard 
59119cb3738Sbellard     if (!drv)
59219cb3738Sbellard         return -ENOMEDIUM;
59371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
59471d0770cSaliguori         return -EIO;
595b338082bSbellard 
59683f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
59783f64091Sbellard }
598fc01f7e7Sbellard 
59919cb3738Sbellard /* Return < 0 if error. Important errors are:
60019cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
60119cb3738Sbellard   -ENOMEDIUM   No media inserted.
60219cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
60319cb3738Sbellard   -EACCES      Trying to write a read-only device
60419cb3738Sbellard */
605fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
606fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
607fc01f7e7Sbellard {
60883f64091Sbellard     BlockDriver *drv = bs->drv;
60919cb3738Sbellard     if (!bs->drv)
61019cb3738Sbellard         return -ENOMEDIUM;
6110849bf08Sbellard     if (bs->read_only)
61219cb3738Sbellard         return -EACCES;
61371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
61471d0770cSaliguori         return -EIO;
61571d0770cSaliguori 
61683f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
61783f64091Sbellard }
61883f64091Sbellard 
619eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
620eda578e5Saliguori                void *buf, int count1)
62183f64091Sbellard {
62283f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
62383f64091Sbellard     int len, nb_sectors, count;
62483f64091Sbellard     int64_t sector_num;
62583f64091Sbellard 
62683f64091Sbellard     count = count1;
62783f64091Sbellard     /* first read to align to sector start */
62883f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
62983f64091Sbellard     if (len > count)
63083f64091Sbellard         len = count;
63183f64091Sbellard     sector_num = offset >> SECTOR_BITS;
63283f64091Sbellard     if (len > 0) {
63383f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
63483f64091Sbellard             return -EIO;
63583f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
63683f64091Sbellard         count -= len;
63783f64091Sbellard         if (count == 0)
63883f64091Sbellard             return count1;
63983f64091Sbellard         sector_num++;
64083f64091Sbellard         buf += len;
64183f64091Sbellard     }
64283f64091Sbellard 
64383f64091Sbellard     /* read the sectors "in place" */
64483f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
64583f64091Sbellard     if (nb_sectors > 0) {
64683f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
64783f64091Sbellard             return -EIO;
64883f64091Sbellard         sector_num += nb_sectors;
64983f64091Sbellard         len = nb_sectors << SECTOR_BITS;
65083f64091Sbellard         buf += len;
65183f64091Sbellard         count -= len;
65283f64091Sbellard     }
65383f64091Sbellard 
65483f64091Sbellard     /* add data from the last sector */
65583f64091Sbellard     if (count > 0) {
65683f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
65783f64091Sbellard             return -EIO;
65883f64091Sbellard         memcpy(buf, tmp_buf, count);
65983f64091Sbellard     }
66083f64091Sbellard     return count1;
66183f64091Sbellard }
66283f64091Sbellard 
663eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
664eda578e5Saliguori                 const void *buf, int count1)
66583f64091Sbellard {
66683f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
66783f64091Sbellard     int len, nb_sectors, count;
66883f64091Sbellard     int64_t sector_num;
66983f64091Sbellard 
67083f64091Sbellard     count = count1;
67183f64091Sbellard     /* first write to align to sector start */
67283f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
67383f64091Sbellard     if (len > count)
67483f64091Sbellard         len = count;
67583f64091Sbellard     sector_num = offset >> SECTOR_BITS;
67683f64091Sbellard     if (len > 0) {
67783f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
67883f64091Sbellard             return -EIO;
67983f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
68083f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
68183f64091Sbellard             return -EIO;
68283f64091Sbellard         count -= len;
68383f64091Sbellard         if (count == 0)
68483f64091Sbellard             return count1;
68583f64091Sbellard         sector_num++;
68683f64091Sbellard         buf += len;
68783f64091Sbellard     }
68883f64091Sbellard 
68983f64091Sbellard     /* write the sectors "in place" */
69083f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
69183f64091Sbellard     if (nb_sectors > 0) {
69283f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
69383f64091Sbellard             return -EIO;
69483f64091Sbellard         sector_num += nb_sectors;
69583f64091Sbellard         len = nb_sectors << SECTOR_BITS;
69683f64091Sbellard         buf += len;
69783f64091Sbellard         count -= len;
69883f64091Sbellard     }
69983f64091Sbellard 
70083f64091Sbellard     /* add data from the last sector */
70183f64091Sbellard     if (count > 0) {
70283f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
70383f64091Sbellard             return -EIO;
70483f64091Sbellard         memcpy(tmp_buf, buf, count);
70583f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
70683f64091Sbellard             return -EIO;
70783f64091Sbellard     }
70883f64091Sbellard     return count1;
70983f64091Sbellard }
71083f64091Sbellard 
71183f64091Sbellard /**
71283f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
71383f64091Sbellard  */
71483f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
71583f64091Sbellard {
71683f64091Sbellard     BlockDriver *drv = bs->drv;
71783f64091Sbellard     if (!drv)
71819cb3738Sbellard         return -ENOMEDIUM;
71983f64091Sbellard     if (!drv->bdrv_truncate)
72083f64091Sbellard         return -ENOTSUP;
72183f64091Sbellard     return drv->bdrv_truncate(bs, offset);
72283f64091Sbellard }
72383f64091Sbellard 
72483f64091Sbellard /**
72583f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
72683f64091Sbellard  */
72783f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
72883f64091Sbellard {
72983f64091Sbellard     BlockDriver *drv = bs->drv;
73083f64091Sbellard     if (!drv)
73119cb3738Sbellard         return -ENOMEDIUM;
73283f64091Sbellard     if (!drv->bdrv_getlength) {
73383f64091Sbellard         /* legacy mode */
73483f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
73583f64091Sbellard     }
73683f64091Sbellard     return drv->bdrv_getlength(bs);
737fc01f7e7Sbellard }
738fc01f7e7Sbellard 
73919cb3738Sbellard /* return 0 as number of sectors if no device present or error */
74096b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
741fc01f7e7Sbellard {
74219cb3738Sbellard     int64_t length;
74319cb3738Sbellard     length = bdrv_getlength(bs);
74419cb3738Sbellard     if (length < 0)
74519cb3738Sbellard         length = 0;
74619cb3738Sbellard     else
74719cb3738Sbellard         length = length >> SECTOR_BITS;
74819cb3738Sbellard     *nb_sectors_ptr = length;
749fc01f7e7Sbellard }
750cf98951bSbellard 
751f3d54fc4Saliguori struct partition {
752f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
753f3d54fc4Saliguori         uint8_t head;               /* starting head */
754f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
755f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
756f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
757f3d54fc4Saliguori         uint8_t end_head;           /* end head */
758f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
759f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
760f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
761f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
762f3d54fc4Saliguori } __attribute__((packed));
763f3d54fc4Saliguori 
764f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
765f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
766f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
767f3d54fc4Saliguori {
768f3d54fc4Saliguori     uint8_t buf[512];
769f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
770f3d54fc4Saliguori     struct partition *p;
771f3d54fc4Saliguori     uint32_t nr_sects;
772a38131b6Sblueswir1     uint64_t nb_sectors;
773f3d54fc4Saliguori 
774f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
775f3d54fc4Saliguori 
776f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
777f3d54fc4Saliguori     if (ret < 0)
778f3d54fc4Saliguori         return -1;
779f3d54fc4Saliguori     /* test msdos magic */
780f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
781f3d54fc4Saliguori         return -1;
782f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
783f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
784f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
785f3d54fc4Saliguori         if (nr_sects && p->end_head) {
786f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
787f3d54fc4Saliguori                a cylinder boundary */
788f3d54fc4Saliguori             heads = p->end_head + 1;
789f3d54fc4Saliguori             sectors = p->end_sector & 63;
790f3d54fc4Saliguori             if (sectors == 0)
791f3d54fc4Saliguori                 continue;
792f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
793f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
794f3d54fc4Saliguori                 continue;
795f3d54fc4Saliguori             *pheads = heads;
796f3d54fc4Saliguori             *psectors = sectors;
797f3d54fc4Saliguori             *pcylinders = cylinders;
798f3d54fc4Saliguori #if 0
799f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
800f3d54fc4Saliguori                    cylinders, heads, sectors);
801f3d54fc4Saliguori #endif
802f3d54fc4Saliguori             return 0;
803f3d54fc4Saliguori         }
804f3d54fc4Saliguori     }
805f3d54fc4Saliguori     return -1;
806f3d54fc4Saliguori }
807f3d54fc4Saliguori 
808f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
809f3d54fc4Saliguori {
810f3d54fc4Saliguori     int translation, lba_detected = 0;
811f3d54fc4Saliguori     int cylinders, heads, secs;
812a38131b6Sblueswir1     uint64_t nb_sectors;
813f3d54fc4Saliguori 
814f3d54fc4Saliguori     /* if a geometry hint is available, use it */
815f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
816f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
817f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
818f3d54fc4Saliguori     if (cylinders != 0) {
819f3d54fc4Saliguori         *pcyls = cylinders;
820f3d54fc4Saliguori         *pheads = heads;
821f3d54fc4Saliguori         *psecs = secs;
822f3d54fc4Saliguori     } else {
823f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
824f3d54fc4Saliguori             if (heads > 16) {
825f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
826f3d54fc4Saliguori                    translation was active, so the default
827f3d54fc4Saliguori                    hardware geometry is OK */
828f3d54fc4Saliguori                 lba_detected = 1;
829f3d54fc4Saliguori                 goto default_geometry;
830f3d54fc4Saliguori             } else {
831f3d54fc4Saliguori                 *pcyls = cylinders;
832f3d54fc4Saliguori                 *pheads = heads;
833f3d54fc4Saliguori                 *psecs = secs;
834f3d54fc4Saliguori                 /* disable any translation to be in sync with
835f3d54fc4Saliguori                    the logical geometry */
836f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
837f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
838f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
839f3d54fc4Saliguori                 }
840f3d54fc4Saliguori             }
841f3d54fc4Saliguori         } else {
842f3d54fc4Saliguori         default_geometry:
843f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
844f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
845f3d54fc4Saliguori 
846f3d54fc4Saliguori             if (cylinders > 16383)
847f3d54fc4Saliguori                 cylinders = 16383;
848f3d54fc4Saliguori             else if (cylinders < 2)
849f3d54fc4Saliguori                 cylinders = 2;
850f3d54fc4Saliguori             *pcyls = cylinders;
851f3d54fc4Saliguori             *pheads = 16;
852f3d54fc4Saliguori             *psecs = 63;
853f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
854f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
855f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
856f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
857f3d54fc4Saliguori                 } else {
858f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
859f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
860f3d54fc4Saliguori                 }
861f3d54fc4Saliguori             }
862f3d54fc4Saliguori         }
863f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
864f3d54fc4Saliguori     }
865f3d54fc4Saliguori }
866f3d54fc4Saliguori 
867b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
868b338082bSbellard                             int cyls, int heads, int secs)
869b338082bSbellard {
870b338082bSbellard     bs->cyls = cyls;
871b338082bSbellard     bs->heads = heads;
872b338082bSbellard     bs->secs = secs;
873b338082bSbellard }
874b338082bSbellard 
875b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
876b338082bSbellard {
877b338082bSbellard     bs->type = type;
878b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
879b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
880b338082bSbellard }
881b338082bSbellard 
88246d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
88346d4767dSbellard {
88446d4767dSbellard     bs->translation = translation;
88546d4767dSbellard }
88646d4767dSbellard 
887b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
888b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
889b338082bSbellard {
890b338082bSbellard     *pcyls = bs->cyls;
891b338082bSbellard     *pheads = bs->heads;
892b338082bSbellard     *psecs = bs->secs;
893b338082bSbellard }
894b338082bSbellard 
895b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
896b338082bSbellard {
897b338082bSbellard     return bs->type;
898b338082bSbellard }
899b338082bSbellard 
90046d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
90146d4767dSbellard {
90246d4767dSbellard     return bs->translation;
90346d4767dSbellard }
90446d4767dSbellard 
905b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
906b338082bSbellard {
907b338082bSbellard     return bs->removable;
908b338082bSbellard }
909b338082bSbellard 
910b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
911b338082bSbellard {
912b338082bSbellard     return bs->read_only;
913b338082bSbellard }
914b338082bSbellard 
915985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
916985a03b0Sths {
917985a03b0Sths     return bs->sg;
918985a03b0Sths }
919985a03b0Sths 
92019cb3738Sbellard /* XXX: no longer used */
921b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
922b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
923b338082bSbellard {
924b338082bSbellard     bs->change_cb = change_cb;
925b338082bSbellard     bs->change_opaque = opaque;
926b338082bSbellard }
927b338082bSbellard 
928ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
929ea2384d3Sbellard {
930ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
931ea2384d3Sbellard         return 1;
932ea2384d3Sbellard     return bs->encrypted;
933ea2384d3Sbellard }
934ea2384d3Sbellard 
935c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
936c0f4ce77Saliguori {
937c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
938c0f4ce77Saliguori 
939c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
940c0f4ce77Saliguori         return 1;
941c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
942c0f4ce77Saliguori }
943c0f4ce77Saliguori 
944ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
945ea2384d3Sbellard {
946ea2384d3Sbellard     int ret;
947ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
948ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
949ea2384d3Sbellard         if (ret < 0)
950ea2384d3Sbellard             return ret;
951ea2384d3Sbellard         if (!bs->encrypted)
952ea2384d3Sbellard             return 0;
953ea2384d3Sbellard     }
954ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
955ea2384d3Sbellard         return -1;
956c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
957bb5fc20fSaliguori     if (ret < 0) {
958bb5fc20fSaliguori         bs->valid_key = 0;
959bb5fc20fSaliguori     } else if (!bs->valid_key) {
960bb5fc20fSaliguori         bs->valid_key = 1;
961bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
962bb5fc20fSaliguori         bs->media_changed = 1;
963bb5fc20fSaliguori         if (bs->change_cb)
964bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
965bb5fc20fSaliguori     }
966c0f4ce77Saliguori     return ret;
967ea2384d3Sbellard }
968ea2384d3Sbellard 
969ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
970ea2384d3Sbellard {
97119cb3738Sbellard     if (!bs->drv) {
972ea2384d3Sbellard         buf[0] = '\0';
973ea2384d3Sbellard     } else {
974ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
975ea2384d3Sbellard     }
976ea2384d3Sbellard }
977ea2384d3Sbellard 
978ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
979ea2384d3Sbellard                          void *opaque)
980ea2384d3Sbellard {
981ea2384d3Sbellard     BlockDriver *drv;
982ea2384d3Sbellard 
983ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
984ea2384d3Sbellard         it(opaque, drv->format_name);
985ea2384d3Sbellard     }
986ea2384d3Sbellard }
987ea2384d3Sbellard 
988b338082bSbellard BlockDriverState *bdrv_find(const char *name)
989b338082bSbellard {
990b338082bSbellard     BlockDriverState *bs;
991b338082bSbellard 
992b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
993b338082bSbellard         if (!strcmp(name, bs->device_name))
994b338082bSbellard             return bs;
995b338082bSbellard     }
996b338082bSbellard     return NULL;
997b338082bSbellard }
998b338082bSbellard 
99951de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
100081d0912dSbellard {
100181d0912dSbellard     BlockDriverState *bs;
100281d0912dSbellard 
100381d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
100451de9760Saliguori         it(opaque, bs);
100581d0912dSbellard     }
100681d0912dSbellard }
100781d0912dSbellard 
1008ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1009ea2384d3Sbellard {
1010ea2384d3Sbellard     return bs->device_name;
1011ea2384d3Sbellard }
1012ea2384d3Sbellard 
10137a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
10147a6cba61Spbrook {
1015081501daSaliguori     if (!bs->drv)
1016081501daSaliguori         return;
10177a6cba61Spbrook     if (bs->drv->bdrv_flush)
10187a6cba61Spbrook         bs->drv->bdrv_flush(bs);
10197a6cba61Spbrook     if (bs->backing_hd)
10207a6cba61Spbrook         bdrv_flush(bs->backing_hd);
10217a6cba61Spbrook }
10227a6cba61Spbrook 
1023c6ca28d6Saliguori void bdrv_flush_all(void)
1024c6ca28d6Saliguori {
1025c6ca28d6Saliguori     BlockDriverState *bs;
1026c6ca28d6Saliguori 
1027c6ca28d6Saliguori     for (bs = bdrv_first; bs != NULL; bs = bs->next)
1028c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
1029c6ca28d6Saliguori             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1030c6ca28d6Saliguori             bdrv_flush(bs);
1031c6ca28d6Saliguori }
1032c6ca28d6Saliguori 
1033f58c7b35Sths /*
1034f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1035f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1036f58c7b35Sths  * hence all their sectors are reported as allocated.
1037f58c7b35Sths  *
1038f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1039f58c7b35Sths  * the specified sector) that are known to be in the same
1040f58c7b35Sths  * allocated/unallocated state.
1041f58c7b35Sths  *
1042f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1043f58c7b35Sths  */
1044f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1045f58c7b35Sths 	int *pnum)
1046f58c7b35Sths {
1047f58c7b35Sths     int64_t n;
1048f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1049f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1050f58c7b35Sths             *pnum = 0;
1051f58c7b35Sths             return 0;
1052f58c7b35Sths         }
1053f58c7b35Sths         n = bs->total_sectors - sector_num;
1054f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1055f58c7b35Sths         return 1;
1056f58c7b35Sths     }
1057f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1058f58c7b35Sths }
1059f58c7b35Sths 
1060376253ecSaliguori void bdrv_info(Monitor *mon)
1061b338082bSbellard {
1062b338082bSbellard     BlockDriverState *bs;
1063b338082bSbellard 
1064b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1065376253ecSaliguori         monitor_printf(mon, "%s:", bs->device_name);
1066376253ecSaliguori         monitor_printf(mon, " type=");
1067b338082bSbellard         switch(bs->type) {
1068b338082bSbellard         case BDRV_TYPE_HD:
1069376253ecSaliguori             monitor_printf(mon, "hd");
1070b338082bSbellard             break;
1071b338082bSbellard         case BDRV_TYPE_CDROM:
1072376253ecSaliguori             monitor_printf(mon, "cdrom");
1073b338082bSbellard             break;
1074b338082bSbellard         case BDRV_TYPE_FLOPPY:
1075376253ecSaliguori             monitor_printf(mon, "floppy");
1076b338082bSbellard             break;
1077b338082bSbellard         }
1078376253ecSaliguori         monitor_printf(mon, " removable=%d", bs->removable);
1079b338082bSbellard         if (bs->removable) {
1080376253ecSaliguori             monitor_printf(mon, " locked=%d", bs->locked);
1081b338082bSbellard         }
108219cb3738Sbellard         if (bs->drv) {
1083376253ecSaliguori             monitor_printf(mon, " file=");
1084376253ecSaliguori             monitor_print_filename(mon, bs->filename);
1085fef30743Sths             if (bs->backing_file[0] != '\0') {
1086376253ecSaliguori                 monitor_printf(mon, " backing_file=");
1087376253ecSaliguori                 monitor_print_filename(mon, bs->backing_file);
1088fef30743Sths             }
1089376253ecSaliguori             monitor_printf(mon, " ro=%d", bs->read_only);
1090376253ecSaliguori             monitor_printf(mon, " drv=%s", bs->drv->format_name);
1091376253ecSaliguori             monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1092b338082bSbellard         } else {
1093376253ecSaliguori             monitor_printf(mon, " [not inserted]");
1094b338082bSbellard         }
1095376253ecSaliguori         monitor_printf(mon, "\n");
1096b338082bSbellard     }
1097b338082bSbellard }
1098a36e69ddSths 
1099a36e69ddSths /* The "info blockstats" command. */
1100376253ecSaliguori void bdrv_info_stats(Monitor *mon)
1101a36e69ddSths {
1102a36e69ddSths     BlockDriverState *bs;
1103a36e69ddSths 
1104a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1105376253ecSaliguori         monitor_printf(mon, "%s:"
1106a36e69ddSths                        " rd_bytes=%" PRIu64
1107a36e69ddSths                        " wr_bytes=%" PRIu64
1108a36e69ddSths                        " rd_operations=%" PRIu64
1109a36e69ddSths                        " wr_operations=%" PRIu64
1110ebf53fcdSaliguori                        "\n",
1111a36e69ddSths                        bs->device_name,
1112a36e69ddSths                        bs->rd_bytes, bs->wr_bytes,
1113a36e69ddSths                        bs->rd_ops, bs->wr_ops);
1114a36e69ddSths     }
1115a36e69ddSths }
1116ea2384d3Sbellard 
1117045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1118045df330Saliguori {
1119045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1120045df330Saliguori         return bs->backing_file;
1121045df330Saliguori     else if (bs->encrypted)
1122045df330Saliguori         return bs->filename;
1123045df330Saliguori     else
1124045df330Saliguori         return NULL;
1125045df330Saliguori }
1126045df330Saliguori 
112783f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
112883f64091Sbellard                                char *filename, int filename_size)
112983f64091Sbellard {
113083f64091Sbellard     if (!bs->backing_hd) {
113183f64091Sbellard         pstrcpy(filename, filename_size, "");
113283f64091Sbellard     } else {
113383f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
113483f64091Sbellard     }
113583f64091Sbellard }
113683f64091Sbellard 
1137faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1138faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1139faea38e7Sbellard {
1140faea38e7Sbellard     BlockDriver *drv = bs->drv;
1141faea38e7Sbellard     if (!drv)
114219cb3738Sbellard         return -ENOMEDIUM;
1143faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1144faea38e7Sbellard         return -ENOTSUP;
1145fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1146fbb7b4e0SKevin Wolf         return -EIO;
1147faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1148faea38e7Sbellard }
1149faea38e7Sbellard 
1150faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1151faea38e7Sbellard {
1152faea38e7Sbellard     BlockDriver *drv = bs->drv;
1153faea38e7Sbellard     if (!drv)
115419cb3738Sbellard         return -ENOMEDIUM;
1155faea38e7Sbellard     if (!drv->bdrv_get_info)
1156faea38e7Sbellard         return -ENOTSUP;
1157faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1158faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1159faea38e7Sbellard }
1160faea38e7Sbellard 
1161178e08a5Saliguori int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1162178e08a5Saliguori {
1163178e08a5Saliguori     BlockDriver *drv = bs->drv;
1164178e08a5Saliguori     if (!drv)
1165178e08a5Saliguori         return -ENOMEDIUM;
1166178e08a5Saliguori     if (!drv->bdrv_put_buffer)
1167178e08a5Saliguori         return -ENOTSUP;
1168178e08a5Saliguori     return drv->bdrv_put_buffer(bs, buf, pos, size);
1169178e08a5Saliguori }
1170178e08a5Saliguori 
1171178e08a5Saliguori int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1172178e08a5Saliguori {
1173178e08a5Saliguori     BlockDriver *drv = bs->drv;
1174178e08a5Saliguori     if (!drv)
1175178e08a5Saliguori         return -ENOMEDIUM;
1176178e08a5Saliguori     if (!drv->bdrv_get_buffer)
1177178e08a5Saliguori         return -ENOTSUP;
1178178e08a5Saliguori     return drv->bdrv_get_buffer(bs, buf, pos, size);
1179178e08a5Saliguori }
1180178e08a5Saliguori 
1181faea38e7Sbellard /**************************************************************/
1182faea38e7Sbellard /* handling of snapshots */
1183faea38e7Sbellard 
1184faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1185faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1186faea38e7Sbellard {
1187faea38e7Sbellard     BlockDriver *drv = bs->drv;
1188faea38e7Sbellard     if (!drv)
118919cb3738Sbellard         return -ENOMEDIUM;
1190faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
1191faea38e7Sbellard         return -ENOTSUP;
1192faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
1193faea38e7Sbellard }
1194faea38e7Sbellard 
1195faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1196faea38e7Sbellard                        const char *snapshot_id)
1197faea38e7Sbellard {
1198faea38e7Sbellard     BlockDriver *drv = bs->drv;
1199faea38e7Sbellard     if (!drv)
120019cb3738Sbellard         return -ENOMEDIUM;
1201faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
1202faea38e7Sbellard         return -ENOTSUP;
1203faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
1204faea38e7Sbellard }
1205faea38e7Sbellard 
1206faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1207faea38e7Sbellard {
1208faea38e7Sbellard     BlockDriver *drv = bs->drv;
1209faea38e7Sbellard     if (!drv)
121019cb3738Sbellard         return -ENOMEDIUM;
1211faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
1212faea38e7Sbellard         return -ENOTSUP;
1213faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1214faea38e7Sbellard }
1215faea38e7Sbellard 
1216faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1217faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1218faea38e7Sbellard {
1219faea38e7Sbellard     BlockDriver *drv = bs->drv;
1220faea38e7Sbellard     if (!drv)
122119cb3738Sbellard         return -ENOMEDIUM;
1222faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1223faea38e7Sbellard         return -ENOTSUP;
1224faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1225faea38e7Sbellard }
1226faea38e7Sbellard 
1227faea38e7Sbellard #define NB_SUFFIXES 4
1228faea38e7Sbellard 
1229faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1230faea38e7Sbellard {
1231faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1232faea38e7Sbellard     int64_t base;
1233faea38e7Sbellard     int i;
1234faea38e7Sbellard 
1235faea38e7Sbellard     if (size <= 999) {
1236faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1237faea38e7Sbellard     } else {
1238faea38e7Sbellard         base = 1024;
1239faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1240faea38e7Sbellard             if (size < (10 * base)) {
1241faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1242faea38e7Sbellard                          (double)size / base,
1243faea38e7Sbellard                          suffixes[i]);
1244faea38e7Sbellard                 break;
1245faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1246faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1247faea38e7Sbellard                          ((size + (base >> 1)) / base),
1248faea38e7Sbellard                          suffixes[i]);
1249faea38e7Sbellard                 break;
1250faea38e7Sbellard             }
1251faea38e7Sbellard             base = base * 1024;
1252faea38e7Sbellard         }
1253faea38e7Sbellard     }
1254faea38e7Sbellard     return buf;
1255faea38e7Sbellard }
1256faea38e7Sbellard 
1257faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1258faea38e7Sbellard {
1259faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
12603b9f94e1Sbellard #ifdef _WIN32
12613b9f94e1Sbellard     struct tm *ptm;
12623b9f94e1Sbellard #else
1263faea38e7Sbellard     struct tm tm;
12643b9f94e1Sbellard #endif
1265faea38e7Sbellard     time_t ti;
1266faea38e7Sbellard     int64_t secs;
1267faea38e7Sbellard 
1268faea38e7Sbellard     if (!sn) {
1269faea38e7Sbellard         snprintf(buf, buf_size,
1270faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1271faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1272faea38e7Sbellard     } else {
1273faea38e7Sbellard         ti = sn->date_sec;
12743b9f94e1Sbellard #ifdef _WIN32
12753b9f94e1Sbellard         ptm = localtime(&ti);
12763b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
12773b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
12783b9f94e1Sbellard #else
1279faea38e7Sbellard         localtime_r(&ti, &tm);
1280faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1281faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
12823b9f94e1Sbellard #endif
1283faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1284faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1285faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1286faea38e7Sbellard                  (int)(secs / 3600),
1287faea38e7Sbellard                  (int)((secs / 60) % 60),
1288faea38e7Sbellard                  (int)(secs % 60),
1289faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1290faea38e7Sbellard         snprintf(buf, buf_size,
1291faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1292faea38e7Sbellard                  sn->id_str, sn->name,
1293faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1294faea38e7Sbellard                  date_buf,
1295faea38e7Sbellard                  clock_buf);
1296faea38e7Sbellard     }
1297faea38e7Sbellard     return buf;
1298faea38e7Sbellard }
1299faea38e7Sbellard 
130083f64091Sbellard 
1301ea2384d3Sbellard /**************************************************************/
130283f64091Sbellard /* async I/Os */
1303ea2384d3Sbellard 
13043b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1305f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
130683f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
1307ea2384d3Sbellard {
130883f64091Sbellard     BlockDriver *drv = bs->drv;
1309a36e69ddSths     BlockDriverAIOCB *ret;
1310ea2384d3Sbellard 
131119cb3738Sbellard     if (!drv)
1312ce1a14dcSpbrook         return NULL;
131371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
131471d0770cSaliguori         return NULL;
131583f64091Sbellard 
1316f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1317f141eafeSaliguori                               cb, opaque);
1318a36e69ddSths 
1319a36e69ddSths     if (ret) {
1320a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1321a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1322a36e69ddSths 	bs->rd_ops ++;
1323a36e69ddSths     }
1324a36e69ddSths 
1325a36e69ddSths     return ret;
132683f64091Sbellard }
132783f64091Sbellard 
1328f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1329f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
133083f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
13317674e7bfSbellard {
133283f64091Sbellard     BlockDriver *drv = bs->drv;
1333a36e69ddSths     BlockDriverAIOCB *ret;
133483f64091Sbellard 
133519cb3738Sbellard     if (!drv)
1336ce1a14dcSpbrook         return NULL;
133783f64091Sbellard     if (bs->read_only)
1338ce1a14dcSpbrook         return NULL;
133971d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
134071d0770cSaliguori         return NULL;
134183f64091Sbellard 
1342f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1343f141eafeSaliguori                                cb, opaque);
1344a36e69ddSths 
1345a36e69ddSths     if (ret) {
1346a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1347a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1348a36e69ddSths 	bs->wr_ops ++;
1349a36e69ddSths     }
1350a36e69ddSths 
1351a36e69ddSths     return ret;
135283f64091Sbellard }
135383f64091Sbellard 
135483f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
135583f64091Sbellard {
13566bbff9a0Saliguori     acb->pool->cancel(acb);
135783f64091Sbellard }
135883f64091Sbellard 
135983f64091Sbellard 
136083f64091Sbellard /**************************************************************/
136183f64091Sbellard /* async block device emulation */
136283f64091Sbellard 
1363c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync {
1364c16b5a2cSChristoph Hellwig     BlockDriverAIOCB common;
1365c16b5a2cSChristoph Hellwig     QEMUBH *bh;
1366c16b5a2cSChristoph Hellwig     int ret;
1367c16b5a2cSChristoph Hellwig     /* vector translation state */
1368c16b5a2cSChristoph Hellwig     QEMUIOVector *qiov;
1369c16b5a2cSChristoph Hellwig     uint8_t *bounce;
1370c16b5a2cSChristoph Hellwig     int is_write;
1371c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync;
1372c16b5a2cSChristoph Hellwig 
1373c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1374c16b5a2cSChristoph Hellwig {
1375c16b5a2cSChristoph Hellwig     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1376*6a7ad299SDor Laor     qemu_bh_delete(acb->bh);
1377c16b5a2cSChristoph Hellwig     qemu_aio_release(acb);
1378c16b5a2cSChristoph Hellwig }
1379c16b5a2cSChristoph Hellwig 
1380c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = {
1381c16b5a2cSChristoph Hellwig     .aiocb_size         = sizeof(BlockDriverAIOCBSync),
1382c16b5a2cSChristoph Hellwig     .cancel             = bdrv_aio_cancel_em,
1383c16b5a2cSChristoph Hellwig };
1384c16b5a2cSChristoph Hellwig 
138583f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1386beac80cdSbellard {
1387ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1388f141eafeSaliguori 
1389f141eafeSaliguori     if (!acb->is_write)
1390f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1391ceb42de8Saliguori     qemu_vfree(acb->bounce);
1392ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1393*6a7ad299SDor Laor     qemu_bh_delete(acb->bh);
1394ce1a14dcSpbrook     qemu_aio_release(acb);
1395beac80cdSbellard }
1396beac80cdSbellard 
1397f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1398f141eafeSaliguori                                             int64_t sector_num,
1399f141eafeSaliguori                                             QEMUIOVector *qiov,
1400f141eafeSaliguori                                             int nb_sectors,
1401f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
1402f141eafeSaliguori                                             void *opaque,
1403f141eafeSaliguori                                             int is_write)
1404f141eafeSaliguori 
1405ea2384d3Sbellard {
1406ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
140783f64091Sbellard 
1408c16b5a2cSChristoph Hellwig     acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1409f141eafeSaliguori     acb->is_write = is_write;
1410f141eafeSaliguori     acb->qiov = qiov;
1411e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
1412f141eafeSaliguori 
1413ce1a14dcSpbrook     if (!acb->bh)
1414ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1415f141eafeSaliguori 
1416f141eafeSaliguori     if (is_write) {
1417f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1418f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1419f141eafeSaliguori     } else {
1420f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1421f141eafeSaliguori     }
1422f141eafeSaliguori 
1423ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1424f141eafeSaliguori 
1425ce1a14dcSpbrook     return &acb->common;
14267a6cba61Spbrook }
14277a6cba61Spbrook 
1428f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1429f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1430ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
143183f64091Sbellard {
1432f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
143383f64091Sbellard }
143483f64091Sbellard 
1435f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1436f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1437f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
1438f141eafeSaliguori {
1439f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1440f141eafeSaliguori }
1441f141eafeSaliguori 
144283f64091Sbellard /**************************************************************/
144383f64091Sbellard /* sync block device emulation */
144483f64091Sbellard 
144583f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
144683f64091Sbellard {
144783f64091Sbellard     *(int *)opaque = ret;
144883f64091Sbellard }
144983f64091Sbellard 
145083f64091Sbellard #define NOT_DONE 0x7fffffff
145183f64091Sbellard 
145283f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
145383f64091Sbellard                         uint8_t *buf, int nb_sectors)
145483f64091Sbellard {
1455ce1a14dcSpbrook     int async_ret;
1456ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1457f141eafeSaliguori     struct iovec iov;
1458f141eafeSaliguori     QEMUIOVector qiov;
145983f64091Sbellard 
146083f64091Sbellard     async_ret = NOT_DONE;
14613f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
1462f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1463f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1464f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
146583f64091Sbellard         bdrv_rw_em_cb, &async_ret);
1466baf35cb9Saliguori     if (acb == NULL)
1467ce1a14dcSpbrook         return -1;
1468baf35cb9Saliguori 
146983f64091Sbellard     while (async_ret == NOT_DONE) {
147083f64091Sbellard         qemu_aio_wait();
147183f64091Sbellard     }
1472baf35cb9Saliguori 
147383f64091Sbellard     return async_ret;
147483f64091Sbellard }
147583f64091Sbellard 
147683f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
147783f64091Sbellard                          const uint8_t *buf, int nb_sectors)
147883f64091Sbellard {
1479ce1a14dcSpbrook     int async_ret;
1480ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1481f141eafeSaliguori     struct iovec iov;
1482f141eafeSaliguori     QEMUIOVector qiov;
148383f64091Sbellard 
148483f64091Sbellard     async_ret = NOT_DONE;
1485f141eafeSaliguori     iov.iov_base = (void *)buf;
1486f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1487f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1488f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
148983f64091Sbellard         bdrv_rw_em_cb, &async_ret);
1490baf35cb9Saliguori     if (acb == NULL)
1491ce1a14dcSpbrook         return -1;
149283f64091Sbellard     while (async_ret == NOT_DONE) {
149383f64091Sbellard         qemu_aio_wait();
149483f64091Sbellard     }
149583f64091Sbellard     return async_ret;
149683f64091Sbellard }
1497ea2384d3Sbellard 
1498ea2384d3Sbellard void bdrv_init(void)
1499ea2384d3Sbellard {
15005efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
1501ea2384d3Sbellard }
1502ce1a14dcSpbrook 
1503c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
15046bbff9a0Saliguori                    BlockDriverCompletionFunc *cb, void *opaque)
15056bbff9a0Saliguori {
1506ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1507ce1a14dcSpbrook 
15086bbff9a0Saliguori     if (pool->free_aiocb) {
15096bbff9a0Saliguori         acb = pool->free_aiocb;
15106bbff9a0Saliguori         pool->free_aiocb = acb->next;
1511ce1a14dcSpbrook     } else {
15126bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
15136bbff9a0Saliguori         acb->pool = pool;
1514ce1a14dcSpbrook     }
1515ce1a14dcSpbrook     acb->bs = bs;
1516ce1a14dcSpbrook     acb->cb = cb;
1517ce1a14dcSpbrook     acb->opaque = opaque;
1518ce1a14dcSpbrook     return acb;
1519ce1a14dcSpbrook }
1520ce1a14dcSpbrook 
1521ce1a14dcSpbrook void qemu_aio_release(void *p)
1522ce1a14dcSpbrook {
15236bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
15246bbff9a0Saliguori     AIOPool *pool = acb->pool;
15256bbff9a0Saliguori     acb->next = pool->free_aiocb;
15266bbff9a0Saliguori     pool->free_aiocb = acb;
1527ce1a14dcSpbrook }
152819cb3738Sbellard 
152919cb3738Sbellard /**************************************************************/
153019cb3738Sbellard /* removable device support */
153119cb3738Sbellard 
153219cb3738Sbellard /**
153319cb3738Sbellard  * Return TRUE if the media is present
153419cb3738Sbellard  */
153519cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
153619cb3738Sbellard {
153719cb3738Sbellard     BlockDriver *drv = bs->drv;
153819cb3738Sbellard     int ret;
153919cb3738Sbellard     if (!drv)
154019cb3738Sbellard         return 0;
154119cb3738Sbellard     if (!drv->bdrv_is_inserted)
154219cb3738Sbellard         return 1;
154319cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
154419cb3738Sbellard     return ret;
154519cb3738Sbellard }
154619cb3738Sbellard 
154719cb3738Sbellard /**
154819cb3738Sbellard  * Return TRUE if the media changed since the last call to this
154919cb3738Sbellard  * function. It is currently only used for floppy disks
155019cb3738Sbellard  */
155119cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
155219cb3738Sbellard {
155319cb3738Sbellard     BlockDriver *drv = bs->drv;
155419cb3738Sbellard     int ret;
155519cb3738Sbellard 
155619cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
155719cb3738Sbellard         ret = -ENOTSUP;
155819cb3738Sbellard     else
155919cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
156019cb3738Sbellard     if (ret == -ENOTSUP)
156119cb3738Sbellard         ret = bs->media_changed;
156219cb3738Sbellard     bs->media_changed = 0;
156319cb3738Sbellard     return ret;
156419cb3738Sbellard }
156519cb3738Sbellard 
156619cb3738Sbellard /**
156719cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
156819cb3738Sbellard  */
156919cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
157019cb3738Sbellard {
157119cb3738Sbellard     BlockDriver *drv = bs->drv;
157219cb3738Sbellard     int ret;
157319cb3738Sbellard 
157419cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
157519cb3738Sbellard         ret = -ENOTSUP;
157619cb3738Sbellard     } else {
157719cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
157819cb3738Sbellard     }
157919cb3738Sbellard     if (ret == -ENOTSUP) {
158019cb3738Sbellard         if (eject_flag)
158119cb3738Sbellard             bdrv_close(bs);
158219cb3738Sbellard     }
158319cb3738Sbellard }
158419cb3738Sbellard 
158519cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
158619cb3738Sbellard {
158719cb3738Sbellard     return bs->locked;
158819cb3738Sbellard }
158919cb3738Sbellard 
159019cb3738Sbellard /**
159119cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
159219cb3738Sbellard  * to eject it manually).
159319cb3738Sbellard  */
159419cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
159519cb3738Sbellard {
159619cb3738Sbellard     BlockDriver *drv = bs->drv;
159719cb3738Sbellard 
159819cb3738Sbellard     bs->locked = locked;
159919cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
160019cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
160119cb3738Sbellard     }
160219cb3738Sbellard }
1603985a03b0Sths 
1604985a03b0Sths /* needed for generic scsi interface */
1605985a03b0Sths 
1606985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1607985a03b0Sths {
1608985a03b0Sths     BlockDriver *drv = bs->drv;
1609985a03b0Sths 
1610985a03b0Sths     if (drv && drv->bdrv_ioctl)
1611985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1612985a03b0Sths     return -ENOTSUP;
1613985a03b0Sths }
16147d780669Saliguori 
1615221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1616221f715dSaliguori         unsigned long int req, void *buf,
16177d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
16187d780669Saliguori {
1619221f715dSaliguori     BlockDriver *drv = bs->drv;
16207d780669Saliguori 
1621221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
1622221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1623221f715dSaliguori     return NULL;
16247d780669Saliguori }
1625e268ca52Saliguori 
1626e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
1627e268ca52Saliguori {
1628e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1629e268ca52Saliguori }
1630