xref: /openbmc/qemu/block.c (revision 0e7e1989f7fced8e39f140e1958f0557b60d4532)
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 
5190765429Sbellard typedef struct BlockDriverAIOCBSync {
5290765429Sbellard     BlockDriverAIOCB common;
5390765429Sbellard     QEMUBH *bh;
5490765429Sbellard     int ret;
55f141eafeSaliguori     /* vector translation state */
56f141eafeSaliguori     QEMUIOVector *qiov;
57f141eafeSaliguori     uint8_t *bounce;
58f141eafeSaliguori     int is_write;
5990765429Sbellard } BlockDriverAIOCBSync;
6090765429Sbellard 
61f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
62f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
63c87c0672Saliguori         BlockDriverCompletionFunc *cb, void *opaque);
64f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
65f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
66ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
6783f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
6883f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
6983f64091Sbellard                         uint8_t *buf, int nb_sectors);
7083f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
7183f64091Sbellard                          const uint8_t *buf, int nb_sectors);
72ec530c81Sbellard 
737ee930d0Sblueswir1 BlockDriverState *bdrv_first;
747ee930d0Sblueswir1 
75ea2384d3Sbellard static BlockDriver *first_drv;
76ea2384d3Sbellard 
7783f64091Sbellard int path_is_absolute(const char *path)
7883f64091Sbellard {
7983f64091Sbellard     const char *p;
8021664424Sbellard #ifdef _WIN32
8121664424Sbellard     /* specific case for names like: "\\.\d:" */
8221664424Sbellard     if (*path == '/' || *path == '\\')
8321664424Sbellard         return 1;
8421664424Sbellard #endif
8583f64091Sbellard     p = strchr(path, ':');
8683f64091Sbellard     if (p)
8783f64091Sbellard         p++;
8883f64091Sbellard     else
8983f64091Sbellard         p = path;
903b9f94e1Sbellard #ifdef _WIN32
913b9f94e1Sbellard     return (*p == '/' || *p == '\\');
923b9f94e1Sbellard #else
933b9f94e1Sbellard     return (*p == '/');
943b9f94e1Sbellard #endif
9583f64091Sbellard }
9683f64091Sbellard 
9783f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
9883f64091Sbellard    path to it by considering it is relative to base_path. URL are
9983f64091Sbellard    supported. */
10083f64091Sbellard void path_combine(char *dest, int dest_size,
10183f64091Sbellard                   const char *base_path,
10283f64091Sbellard                   const char *filename)
10383f64091Sbellard {
10483f64091Sbellard     const char *p, *p1;
10583f64091Sbellard     int len;
10683f64091Sbellard 
10783f64091Sbellard     if (dest_size <= 0)
10883f64091Sbellard         return;
10983f64091Sbellard     if (path_is_absolute(filename)) {
11083f64091Sbellard         pstrcpy(dest, dest_size, filename);
11183f64091Sbellard     } else {
11283f64091Sbellard         p = strchr(base_path, ':');
11383f64091Sbellard         if (p)
11483f64091Sbellard             p++;
11583f64091Sbellard         else
11683f64091Sbellard             p = base_path;
1173b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1183b9f94e1Sbellard #ifdef _WIN32
1193b9f94e1Sbellard         {
1203b9f94e1Sbellard             const char *p2;
1213b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1223b9f94e1Sbellard             if (!p1 || p2 > p1)
1233b9f94e1Sbellard                 p1 = p2;
1243b9f94e1Sbellard         }
1253b9f94e1Sbellard #endif
12683f64091Sbellard         if (p1)
12783f64091Sbellard             p1++;
12883f64091Sbellard         else
12983f64091Sbellard             p1 = base_path;
13083f64091Sbellard         if (p1 > p)
13183f64091Sbellard             p = p1;
13283f64091Sbellard         len = p - base_path;
13383f64091Sbellard         if (len > dest_size - 1)
13483f64091Sbellard             len = dest_size - 1;
13583f64091Sbellard         memcpy(dest, base_path, len);
13683f64091Sbellard         dest[len] = '\0';
13783f64091Sbellard         pstrcat(dest, dest_size, filename);
13883f64091Sbellard     }
13983f64091Sbellard }
14083f64091Sbellard 
14183f64091Sbellard 
1425efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv)
143ea2384d3Sbellard {
144f141eafeSaliguori     if (!bdrv->bdrv_aio_readv) {
14583f64091Sbellard         /* add AIO emulation layer */
146f141eafeSaliguori         bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
147f141eafeSaliguori         bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
14883f64091Sbellard         bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
14990765429Sbellard         bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
150eda578e5Saliguori     } else if (!bdrv->bdrv_read) {
15183f64091Sbellard         /* add synchronous IO emulation layer */
15283f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
15383f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
15483f64091Sbellard     }
1556bbff9a0Saliguori     aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel);
156ea2384d3Sbellard     bdrv->next = first_drv;
157ea2384d3Sbellard     first_drv = bdrv;
158ea2384d3Sbellard }
159b338082bSbellard 
160b338082bSbellard /* create a new block device (by default it is empty) */
161b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
162fc01f7e7Sbellard {
163b338082bSbellard     BlockDriverState **pbs, *bs;
164b338082bSbellard 
165b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
166b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
167ea2384d3Sbellard     if (device_name[0] != '\0') {
168b338082bSbellard         /* insert at the end */
169b338082bSbellard         pbs = &bdrv_first;
170b338082bSbellard         while (*pbs != NULL)
171b338082bSbellard             pbs = &(*pbs)->next;
172b338082bSbellard         *pbs = bs;
173ea2384d3Sbellard     }
174b338082bSbellard     return bs;
175b338082bSbellard }
176b338082bSbellard 
177ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
178ea2384d3Sbellard {
179ea2384d3Sbellard     BlockDriver *drv1;
180ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
181ea2384d3Sbellard         if (!strcmp(drv1->format_name, format_name))
182ea2384d3Sbellard             return drv1;
183ea2384d3Sbellard     }
184ea2384d3Sbellard     return NULL;
185ea2384d3Sbellard }
186ea2384d3Sbellard 
1875eb45639Saliguori int bdrv_create2(BlockDriver *drv,
1885eb45639Saliguori                 const char *filename, int64_t size_in_sectors,
1895eb45639Saliguori                 const char *backing_file, const char *backing_format,
1905eb45639Saliguori                 int flags)
1915eb45639Saliguori {
192*0e7e1989SKevin Wolf     QEMUOptionParameter *options;
193*0e7e1989SKevin Wolf 
194*0e7e1989SKevin Wolf     options = parse_option_parameters("", drv->create_options, NULL);
195*0e7e1989SKevin Wolf 
196*0e7e1989SKevin Wolf     // Process flags
197*0e7e1989SKevin Wolf     if (flags & ~(BLOCK_FLAG_ENCRYPT | BLOCK_FLAG_COMPAT6 | BLOCK_FLAG_COMPRESS)) {
1985eb45639Saliguori         return -ENOTSUP;
1995eb45639Saliguori     }
2005eb45639Saliguori 
201*0e7e1989SKevin Wolf     if (flags & BLOCK_FLAG_ENCRYPT) {
202*0e7e1989SKevin Wolf         set_option_parameter_int(options, BLOCK_OPT_ENCRYPT, 1);
203*0e7e1989SKevin Wolf     }
204*0e7e1989SKevin Wolf     if (flags & BLOCK_FLAG_COMPAT6) {
205*0e7e1989SKevin Wolf         set_option_parameter_int(options, BLOCK_OPT_COMPAT6, 1);
206*0e7e1989SKevin Wolf     }
207*0e7e1989SKevin Wolf 
208*0e7e1989SKevin Wolf     // Add size to options
209*0e7e1989SKevin Wolf     set_option_parameter_int(options, BLOCK_OPT_SIZE, size_in_sectors * 512);
210*0e7e1989SKevin Wolf 
211*0e7e1989SKevin Wolf     // Backing files
212*0e7e1989SKevin Wolf     if ((backing_file != NULL && set_option_parameter(options,
213*0e7e1989SKevin Wolf             BLOCK_OPT_BACKING_FILE, backing_file))
214*0e7e1989SKevin Wolf         || (backing_format != NULL && set_option_parameter(options,
215*0e7e1989SKevin Wolf             BLOCK_OPT_BACKING_FMT, backing_format)))
216*0e7e1989SKevin Wolf     {
217*0e7e1989SKevin Wolf         return -ENOTSUP;
218*0e7e1989SKevin Wolf     }
219*0e7e1989SKevin Wolf 
220*0e7e1989SKevin Wolf     return bdrv_create(drv, filename, options);
221*0e7e1989SKevin Wolf }
222*0e7e1989SKevin Wolf 
223*0e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename,
224*0e7e1989SKevin Wolf     QEMUOptionParameter *options)
225ea2384d3Sbellard {
226ea2384d3Sbellard     if (!drv->bdrv_create)
227ea2384d3Sbellard         return -ENOTSUP;
228*0e7e1989SKevin Wolf 
229*0e7e1989SKevin Wolf     return drv->bdrv_create(filename, options);
230ea2384d3Sbellard }
231ea2384d3Sbellard 
232d5249393Sbellard #ifdef _WIN32
23395389c86Sbellard void get_tmp_filename(char *filename, int size)
234d5249393Sbellard {
2353b9f94e1Sbellard     char temp_dir[MAX_PATH];
2363b9f94e1Sbellard 
2373b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
2383b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
239d5249393Sbellard }
240d5249393Sbellard #else
24195389c86Sbellard void get_tmp_filename(char *filename, int size)
242ea2384d3Sbellard {
243ea2384d3Sbellard     int fd;
2447ccfb2ebSblueswir1     const char *tmpdir;
245d5249393Sbellard     /* XXX: race condition possible */
2460badc1eeSaurel32     tmpdir = getenv("TMPDIR");
2470badc1eeSaurel32     if (!tmpdir)
2480badc1eeSaurel32         tmpdir = "/tmp";
2490badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
250ea2384d3Sbellard     fd = mkstemp(filename);
251ea2384d3Sbellard     close(fd);
252ea2384d3Sbellard }
253d5249393Sbellard #endif
254ea2384d3Sbellard 
25519cb3738Sbellard #ifdef _WIN32
256f45512feSbellard static int is_windows_drive_prefix(const char *filename)
257f45512feSbellard {
258f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
259f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
260f45512feSbellard             filename[1] == ':');
261f45512feSbellard }
262f45512feSbellard 
26319cb3738Sbellard static int is_windows_drive(const char *filename)
26419cb3738Sbellard {
265f45512feSbellard     if (is_windows_drive_prefix(filename) &&
266f45512feSbellard         filename[2] == '\0')
26719cb3738Sbellard         return 1;
26819cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
26919cb3738Sbellard         strstart(filename, "//./", NULL))
27019cb3738Sbellard         return 1;
27119cb3738Sbellard     return 0;
27219cb3738Sbellard }
27319cb3738Sbellard #endif
27419cb3738Sbellard 
27583f64091Sbellard static BlockDriver *find_protocol(const char *filename)
27683f64091Sbellard {
27783f64091Sbellard     BlockDriver *drv1;
27883f64091Sbellard     char protocol[128];
27983f64091Sbellard     int len;
28083f64091Sbellard     const char *p;
28119cb3738Sbellard 
28219cb3738Sbellard #ifdef _WIN32
283f45512feSbellard     if (is_windows_drive(filename) ||
284f45512feSbellard         is_windows_drive_prefix(filename))
2855efa9d5aSAnthony Liguori         return bdrv_find_format("raw");
28619cb3738Sbellard #endif
28783f64091Sbellard     p = strchr(filename, ':');
28883f64091Sbellard     if (!p)
2895efa9d5aSAnthony Liguori         return bdrv_find_format("raw");
29083f64091Sbellard     len = p - filename;
29183f64091Sbellard     if (len > sizeof(protocol) - 1)
29283f64091Sbellard         len = sizeof(protocol) - 1;
29383f64091Sbellard     memcpy(protocol, filename, len);
29483f64091Sbellard     protocol[len] = '\0';
29583f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
29683f64091Sbellard         if (drv1->protocol_name &&
29783f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
29883f64091Sbellard             return drv1;
29983f64091Sbellard     }
30083f64091Sbellard     return NULL;
30183f64091Sbellard }
30283f64091Sbellard 
3037674e7bfSbellard /* XXX: force raw format if block or character device ? It would
3047674e7bfSbellard    simplify the BSD case */
305ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
306ea2384d3Sbellard {
30783f64091Sbellard     int ret, score, score_max;
308ea2384d3Sbellard     BlockDriver *drv1, *drv;
30983f64091Sbellard     uint8_t buf[2048];
31083f64091Sbellard     BlockDriverState *bs;
311ea2384d3Sbellard 
31219cb3738Sbellard     /* detect host devices. By convention, /dev/cdrom[N] is always
31319cb3738Sbellard        recognized as a host CDROM */
31419cb3738Sbellard     if (strstart(filename, "/dev/cdrom", NULL))
3155efa9d5aSAnthony Liguori         return bdrv_find_format("host_device");
31619cb3738Sbellard #ifdef _WIN32
31719cb3738Sbellard     if (is_windows_drive(filename))
3185efa9d5aSAnthony Liguori         return bdrv_find_format("host_device");
31919cb3738Sbellard #else
32019cb3738Sbellard     {
32119cb3738Sbellard         struct stat st;
32219cb3738Sbellard         if (stat(filename, &st) >= 0 &&
32319cb3738Sbellard             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
3245efa9d5aSAnthony Liguori             return bdrv_find_format("host_device");
32519cb3738Sbellard         }
32619cb3738Sbellard     }
32719cb3738Sbellard #endif
32819cb3738Sbellard 
32983f64091Sbellard     drv = find_protocol(filename);
33019cb3738Sbellard     /* no need to test disk image formats for vvfat */
331c833ab73SAnthony Liguori     if (drv && strcmp(drv->format_name, "vvfat") == 0)
33283f64091Sbellard         return drv;
33383f64091Sbellard 
33483f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
33583f64091Sbellard     if (ret < 0)
3367674e7bfSbellard         return NULL;
33783f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
33883f64091Sbellard     bdrv_delete(bs);
339ea2384d3Sbellard     if (ret < 0) {
340ea2384d3Sbellard         return NULL;
341ea2384d3Sbellard     }
342ea2384d3Sbellard 
343ea2384d3Sbellard     score_max = 0;
344ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
34583f64091Sbellard         if (drv1->bdrv_probe) {
346ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
347ea2384d3Sbellard             if (score > score_max) {
348ea2384d3Sbellard                 score_max = score;
349ea2384d3Sbellard                 drv = drv1;
350ea2384d3Sbellard             }
351ea2384d3Sbellard         }
35283f64091Sbellard     }
353ea2384d3Sbellard     return drv;
354ea2384d3Sbellard }
355ea2384d3Sbellard 
35683f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
357b338082bSbellard {
35883f64091Sbellard     BlockDriverState *bs;
35983f64091Sbellard     int ret;
3603b0d4f61Sbellard 
36183f64091Sbellard     bs = bdrv_new("");
36283f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
36383f64091Sbellard     if (ret < 0) {
36483f64091Sbellard         bdrv_delete(bs);
36583f64091Sbellard         return ret;
3663b0d4f61Sbellard     }
36771d0770cSaliguori     bs->growable = 1;
36883f64091Sbellard     *pbs = bs;
36983f64091Sbellard     return 0;
3703b0d4f61Sbellard }
3713b0d4f61Sbellard 
37283f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
37383f64091Sbellard {
37483f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
375ea2384d3Sbellard }
376ea2384d3Sbellard 
37783f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
378ea2384d3Sbellard                BlockDriver *drv)
379ea2384d3Sbellard {
38083f64091Sbellard     int ret, open_flags;
381eb5c851fSths     char tmp_filename[PATH_MAX];
382eb5c851fSths     char backing_filename[PATH_MAX];
383fc01f7e7Sbellard 
3840849bf08Sbellard     bs->read_only = 0;
385ea2384d3Sbellard     bs->is_temporary = 0;
386ea2384d3Sbellard     bs->encrypted = 0;
387c0f4ce77Saliguori     bs->valid_key = 0;
388e268ca52Saliguori     /* buffer_alignment defaulted to 512, drivers can change this value */
389e268ca52Saliguori     bs->buffer_alignment = 512;
39033e3963eSbellard 
39183f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
392ea2384d3Sbellard         BlockDriverState *bs1;
393ea2384d3Sbellard         int64_t total_size;
3947c96d46eSaliguori         int is_protocol = 0;
39533e3963eSbellard 
396ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
397ea2384d3Sbellard            instead of opening 'filename' directly */
398ea2384d3Sbellard 
399ea2384d3Sbellard         /* if there is a backing file, use it */
400ea2384d3Sbellard         bs1 = bdrv_new("");
4015eb45639Saliguori         ret = bdrv_open2(bs1, filename, 0, drv);
40251d7c00cSaliguori         if (ret < 0) {
403ea2384d3Sbellard             bdrv_delete(bs1);
40451d7c00cSaliguori             return ret;
405ea2384d3Sbellard         }
40683f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
4077c96d46eSaliguori 
4087c96d46eSaliguori         if (bs1->drv && bs1->drv->protocol_name)
4097c96d46eSaliguori             is_protocol = 1;
4107c96d46eSaliguori 
411ea2384d3Sbellard         bdrv_delete(bs1);
412ea2384d3Sbellard 
413ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
4147c96d46eSaliguori 
4157c96d46eSaliguori         /* Real path is meaningless for protocols */
4167c96d46eSaliguori         if (is_protocol)
4177c96d46eSaliguori             snprintf(backing_filename, sizeof(backing_filename),
4187c96d46eSaliguori                      "%s", filename);
4197c96d46eSaliguori         else
420a817d936Sbellard             realpath(filename, backing_filename);
4217c96d46eSaliguori 
4225efa9d5aSAnthony Liguori         ret = bdrv_create2(bdrv_find_format("qcow2"), tmp_filename,
4235eb45639Saliguori                            total_size, backing_filename,
4245eb45639Saliguori                            (drv ? drv->format_name : NULL), 0);
42551d7c00cSaliguori         if (ret < 0) {
42651d7c00cSaliguori             return ret;
427ea2384d3Sbellard         }
428ea2384d3Sbellard         filename = tmp_filename;
4295efa9d5aSAnthony Liguori         drv = bdrv_find_format("qcow2");
430ea2384d3Sbellard         bs->is_temporary = 1;
431ea2384d3Sbellard     }
432ea2384d3Sbellard 
433ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
43483f64091Sbellard     if (flags & BDRV_O_FILE) {
43583f64091Sbellard         drv = find_protocol(filename);
43651d7c00cSaliguori     } else if (!drv) {
437ea2384d3Sbellard         drv = find_image_format(filename);
438ea2384d3Sbellard     }
43951d7c00cSaliguori     if (!drv) {
44051d7c00cSaliguori         ret = -ENOENT;
44151d7c00cSaliguori         goto unlink_and_fail;
44283f64091Sbellard     }
443ea2384d3Sbellard     bs->drv = drv;
444ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
44583f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
44683f64091Sbellard        RDONLY as fallback */
44783f64091Sbellard     if (!(flags & BDRV_O_FILE))
4489f7965c7Saliguori         open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK);
44983f64091Sbellard     else
45083f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
45183f64091Sbellard     ret = drv->bdrv_open(bs, filename, open_flags);
452a0a83536Saurel32     if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
4539f7965c7Saliguori         ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
45483f64091Sbellard         bs->read_only = 1;
45583f64091Sbellard     }
456ea2384d3Sbellard     if (ret < 0) {
457ea2384d3Sbellard         qemu_free(bs->opaque);
4586b21b973Sbellard         bs->opaque = NULL;
4596b21b973Sbellard         bs->drv = NULL;
46051d7c00cSaliguori     unlink_and_fail:
46151d7c00cSaliguori         if (bs->is_temporary)
46251d7c00cSaliguori             unlink(filename);
46383f64091Sbellard         return ret;
464ea2384d3Sbellard     }
465d15a771dSbellard     if (drv->bdrv_getlength) {
466d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
467d15a771dSbellard     }
468ea2384d3Sbellard #ifndef _WIN32
469ea2384d3Sbellard     if (bs->is_temporary) {
470ea2384d3Sbellard         unlink(filename);
47133e3963eSbellard     }
47267b915a5Sbellard #endif
47383f64091Sbellard     if (bs->backing_file[0] != '\0') {
474ea2384d3Sbellard         /* if there is a backing file, use it */
4755eb45639Saliguori         BlockDriver *back_drv = NULL;
476ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
47783f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
47883f64091Sbellard                      filename, bs->backing_file);
4795eb45639Saliguori         if (bs->backing_format[0] != '\0')
4805eb45639Saliguori             back_drv = bdrv_find_format(bs->backing_format);
4815eb45639Saliguori         ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
4825eb45639Saliguori                          back_drv);
48351d7c00cSaliguori         if (ret < 0) {
48451d7c00cSaliguori             bdrv_close(bs);
48551d7c00cSaliguori             return ret;
48651d7c00cSaliguori         }
487ea2384d3Sbellard     }
48833e3963eSbellard 
489bb5fc20fSaliguori     if (!bdrv_key_required(bs)) {
490b338082bSbellard         /* call the change callback */
49119cb3738Sbellard         bs->media_changed = 1;
492b338082bSbellard         if (bs->change_cb)
493b338082bSbellard             bs->change_cb(bs->change_opaque);
494bb5fc20fSaliguori     }
495b338082bSbellard     return 0;
496fc01f7e7Sbellard }
497fc01f7e7Sbellard 
498fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
499fc01f7e7Sbellard {
50019cb3738Sbellard     if (bs->drv) {
501ea2384d3Sbellard         if (bs->backing_hd)
502ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
503ea2384d3Sbellard         bs->drv->bdrv_close(bs);
504ea2384d3Sbellard         qemu_free(bs->opaque);
505ea2384d3Sbellard #ifdef _WIN32
506ea2384d3Sbellard         if (bs->is_temporary) {
507ea2384d3Sbellard             unlink(bs->filename);
508ea2384d3Sbellard         }
50967b915a5Sbellard #endif
510ea2384d3Sbellard         bs->opaque = NULL;
511ea2384d3Sbellard         bs->drv = NULL;
512b338082bSbellard 
513b338082bSbellard         /* call the change callback */
51419cb3738Sbellard         bs->media_changed = 1;
515b338082bSbellard         if (bs->change_cb)
516b338082bSbellard             bs->change_cb(bs->change_opaque);
517b338082bSbellard     }
518b338082bSbellard }
519b338082bSbellard 
520b338082bSbellard void bdrv_delete(BlockDriverState *bs)
521b338082bSbellard {
52234c6f050Saurel32     BlockDriverState **pbs;
52334c6f050Saurel32 
52434c6f050Saurel32     pbs = &bdrv_first;
52534c6f050Saurel32     while (*pbs != bs && *pbs != NULL)
52634c6f050Saurel32         pbs = &(*pbs)->next;
52734c6f050Saurel32     if (*pbs == bs)
52834c6f050Saurel32         *pbs = bs->next;
52934c6f050Saurel32 
530b338082bSbellard     bdrv_close(bs);
531b338082bSbellard     qemu_free(bs);
532fc01f7e7Sbellard }
533fc01f7e7Sbellard 
534e97fc193Saliguori /*
535e97fc193Saliguori  * Run consistency checks on an image
536e97fc193Saliguori  *
537e97fc193Saliguori  * Returns the number of errors or -errno when an internal error occurs
538e97fc193Saliguori  */
539e97fc193Saliguori int bdrv_check(BlockDriverState *bs)
540e97fc193Saliguori {
541e97fc193Saliguori     if (bs->drv->bdrv_check == NULL) {
542e97fc193Saliguori         return -ENOTSUP;
543e97fc193Saliguori     }
544e97fc193Saliguori 
545e97fc193Saliguori     return bs->drv->bdrv_check(bs);
546e97fc193Saliguori }
547e97fc193Saliguori 
54833e3963eSbellard /* commit COW file into the raw image */
54933e3963eSbellard int bdrv_commit(BlockDriverState *bs)
55033e3963eSbellard {
55119cb3738Sbellard     BlockDriver *drv = bs->drv;
55283f64091Sbellard     int64_t i, total_sectors;
553ea2384d3Sbellard     int n, j;
554ea2384d3Sbellard     unsigned char sector[512];
55533e3963eSbellard 
55619cb3738Sbellard     if (!drv)
55719cb3738Sbellard         return -ENOMEDIUM;
55833e3963eSbellard 
55933e3963eSbellard     if (bs->read_only) {
560ea2384d3Sbellard 	return -EACCES;
56133e3963eSbellard     }
56233e3963eSbellard 
563ea2384d3Sbellard     if (!bs->backing_hd) {
564ea2384d3Sbellard 	return -ENOTSUP;
565ea2384d3Sbellard     }
566ea2384d3Sbellard 
56783f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
56883f64091Sbellard     for (i = 0; i < total_sectors;) {
56919cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
570ea2384d3Sbellard             for(j = 0; j < n; j++) {
57133e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
572ea2384d3Sbellard                     return -EIO;
57333e3963eSbellard                 }
57433e3963eSbellard 
575ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
576ea2384d3Sbellard                     return -EIO;
57733e3963eSbellard                 }
578ea2384d3Sbellard                 i++;
579ea2384d3Sbellard 	    }
580ea2384d3Sbellard 	} else {
581ea2384d3Sbellard             i += n;
58233e3963eSbellard         }
58333e3963eSbellard     }
58495389c86Sbellard 
58519cb3738Sbellard     if (drv->bdrv_make_empty)
58619cb3738Sbellard 	return drv->bdrv_make_empty(bs);
58795389c86Sbellard 
58833e3963eSbellard     return 0;
58933e3963eSbellard }
59033e3963eSbellard 
59171d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
59271d0770cSaliguori                                    size_t size)
59371d0770cSaliguori {
59471d0770cSaliguori     int64_t len;
59571d0770cSaliguori 
59671d0770cSaliguori     if (!bdrv_is_inserted(bs))
59771d0770cSaliguori         return -ENOMEDIUM;
59871d0770cSaliguori 
59971d0770cSaliguori     if (bs->growable)
60071d0770cSaliguori         return 0;
60171d0770cSaliguori 
60271d0770cSaliguori     len = bdrv_getlength(bs);
60371d0770cSaliguori 
604fbb7b4e0SKevin Wolf     if (offset < 0)
605fbb7b4e0SKevin Wolf         return -EIO;
606fbb7b4e0SKevin Wolf 
607fbb7b4e0SKevin Wolf     if ((offset > len) || (len - offset < size))
60871d0770cSaliguori         return -EIO;
60971d0770cSaliguori 
61071d0770cSaliguori     return 0;
61171d0770cSaliguori }
61271d0770cSaliguori 
61371d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
61471d0770cSaliguori                               int nb_sectors)
61571d0770cSaliguori {
616999dec57Saliguori     return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
61771d0770cSaliguori }
61871d0770cSaliguori 
61919cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
620fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
621fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
622fc01f7e7Sbellard {
623ea2384d3Sbellard     BlockDriver *drv = bs->drv;
624fc01f7e7Sbellard 
62519cb3738Sbellard     if (!drv)
62619cb3738Sbellard         return -ENOMEDIUM;
62771d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
62871d0770cSaliguori         return -EIO;
629b338082bSbellard 
63083f64091Sbellard     return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
63183f64091Sbellard }
632fc01f7e7Sbellard 
63319cb3738Sbellard /* Return < 0 if error. Important errors are:
63419cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
63519cb3738Sbellard   -ENOMEDIUM   No media inserted.
63619cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
63719cb3738Sbellard   -EACCES      Trying to write a read-only device
63819cb3738Sbellard */
639fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
640fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
641fc01f7e7Sbellard {
64283f64091Sbellard     BlockDriver *drv = bs->drv;
64319cb3738Sbellard     if (!bs->drv)
64419cb3738Sbellard         return -ENOMEDIUM;
6450849bf08Sbellard     if (bs->read_only)
64619cb3738Sbellard         return -EACCES;
64771d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
64871d0770cSaliguori         return -EIO;
64971d0770cSaliguori 
65083f64091Sbellard     return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
65183f64091Sbellard }
65283f64091Sbellard 
653eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset,
654eda578e5Saliguori                void *buf, int count1)
65583f64091Sbellard {
65683f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
65783f64091Sbellard     int len, nb_sectors, count;
65883f64091Sbellard     int64_t sector_num;
65983f64091Sbellard 
66083f64091Sbellard     count = count1;
66183f64091Sbellard     /* first read to align to sector start */
66283f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
66383f64091Sbellard     if (len > count)
66483f64091Sbellard         len = count;
66583f64091Sbellard     sector_num = offset >> SECTOR_BITS;
66683f64091Sbellard     if (len > 0) {
66783f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
66883f64091Sbellard             return -EIO;
66983f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
67083f64091Sbellard         count -= len;
67183f64091Sbellard         if (count == 0)
67283f64091Sbellard             return count1;
67383f64091Sbellard         sector_num++;
67483f64091Sbellard         buf += len;
67583f64091Sbellard     }
67683f64091Sbellard 
67783f64091Sbellard     /* read the sectors "in place" */
67883f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
67983f64091Sbellard     if (nb_sectors > 0) {
68083f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
68183f64091Sbellard             return -EIO;
68283f64091Sbellard         sector_num += nb_sectors;
68383f64091Sbellard         len = nb_sectors << SECTOR_BITS;
68483f64091Sbellard         buf += len;
68583f64091Sbellard         count -= len;
68683f64091Sbellard     }
68783f64091Sbellard 
68883f64091Sbellard     /* add data from the last sector */
68983f64091Sbellard     if (count > 0) {
69083f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
69183f64091Sbellard             return -EIO;
69283f64091Sbellard         memcpy(buf, tmp_buf, count);
69383f64091Sbellard     }
69483f64091Sbellard     return count1;
69583f64091Sbellard }
69683f64091Sbellard 
697eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
698eda578e5Saliguori                 const void *buf, int count1)
69983f64091Sbellard {
70083f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
70183f64091Sbellard     int len, nb_sectors, count;
70283f64091Sbellard     int64_t sector_num;
70383f64091Sbellard 
70483f64091Sbellard     count = count1;
70583f64091Sbellard     /* first write to align to sector start */
70683f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
70783f64091Sbellard     if (len > count)
70883f64091Sbellard         len = count;
70983f64091Sbellard     sector_num = offset >> SECTOR_BITS;
71083f64091Sbellard     if (len > 0) {
71183f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
71283f64091Sbellard             return -EIO;
71383f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
71483f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
71583f64091Sbellard             return -EIO;
71683f64091Sbellard         count -= len;
71783f64091Sbellard         if (count == 0)
71883f64091Sbellard             return count1;
71983f64091Sbellard         sector_num++;
72083f64091Sbellard         buf += len;
72183f64091Sbellard     }
72283f64091Sbellard 
72383f64091Sbellard     /* write the sectors "in place" */
72483f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
72583f64091Sbellard     if (nb_sectors > 0) {
72683f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
72783f64091Sbellard             return -EIO;
72883f64091Sbellard         sector_num += nb_sectors;
72983f64091Sbellard         len = nb_sectors << SECTOR_BITS;
73083f64091Sbellard         buf += len;
73183f64091Sbellard         count -= len;
73283f64091Sbellard     }
73383f64091Sbellard 
73483f64091Sbellard     /* add data from the last sector */
73583f64091Sbellard     if (count > 0) {
73683f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
73783f64091Sbellard             return -EIO;
73883f64091Sbellard         memcpy(tmp_buf, buf, count);
73983f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
74083f64091Sbellard             return -EIO;
74183f64091Sbellard     }
74283f64091Sbellard     return count1;
74383f64091Sbellard }
74483f64091Sbellard 
74583f64091Sbellard /**
74683f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
74783f64091Sbellard  */
74883f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
74983f64091Sbellard {
75083f64091Sbellard     BlockDriver *drv = bs->drv;
75183f64091Sbellard     if (!drv)
75219cb3738Sbellard         return -ENOMEDIUM;
75383f64091Sbellard     if (!drv->bdrv_truncate)
75483f64091Sbellard         return -ENOTSUP;
75583f64091Sbellard     return drv->bdrv_truncate(bs, offset);
75683f64091Sbellard }
75783f64091Sbellard 
75883f64091Sbellard /**
75983f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
76083f64091Sbellard  */
76183f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
76283f64091Sbellard {
76383f64091Sbellard     BlockDriver *drv = bs->drv;
76483f64091Sbellard     if (!drv)
76519cb3738Sbellard         return -ENOMEDIUM;
76683f64091Sbellard     if (!drv->bdrv_getlength) {
76783f64091Sbellard         /* legacy mode */
76883f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
76983f64091Sbellard     }
77083f64091Sbellard     return drv->bdrv_getlength(bs);
771fc01f7e7Sbellard }
772fc01f7e7Sbellard 
77319cb3738Sbellard /* return 0 as number of sectors if no device present or error */
77496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
775fc01f7e7Sbellard {
77619cb3738Sbellard     int64_t length;
77719cb3738Sbellard     length = bdrv_getlength(bs);
77819cb3738Sbellard     if (length < 0)
77919cb3738Sbellard         length = 0;
78019cb3738Sbellard     else
78119cb3738Sbellard         length = length >> SECTOR_BITS;
78219cb3738Sbellard     *nb_sectors_ptr = length;
783fc01f7e7Sbellard }
784cf98951bSbellard 
785f3d54fc4Saliguori struct partition {
786f3d54fc4Saliguori         uint8_t boot_ind;           /* 0x80 - active */
787f3d54fc4Saliguori         uint8_t head;               /* starting head */
788f3d54fc4Saliguori         uint8_t sector;             /* starting sector */
789f3d54fc4Saliguori         uint8_t cyl;                /* starting cylinder */
790f3d54fc4Saliguori         uint8_t sys_ind;            /* What partition type */
791f3d54fc4Saliguori         uint8_t end_head;           /* end head */
792f3d54fc4Saliguori         uint8_t end_sector;         /* end sector */
793f3d54fc4Saliguori         uint8_t end_cyl;            /* end cylinder */
794f3d54fc4Saliguori         uint32_t start_sect;        /* starting sector counting from 0 */
795f3d54fc4Saliguori         uint32_t nr_sects;          /* nr of sectors in partition */
796f3d54fc4Saliguori } __attribute__((packed));
797f3d54fc4Saliguori 
798f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
799f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs,
800f3d54fc4Saliguori                            int *pcylinders, int *pheads, int *psectors)
801f3d54fc4Saliguori {
802f3d54fc4Saliguori     uint8_t buf[512];
803f3d54fc4Saliguori     int ret, i, heads, sectors, cylinders;
804f3d54fc4Saliguori     struct partition *p;
805f3d54fc4Saliguori     uint32_t nr_sects;
806a38131b6Sblueswir1     uint64_t nb_sectors;
807f3d54fc4Saliguori 
808f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
809f3d54fc4Saliguori 
810f3d54fc4Saliguori     ret = bdrv_read(bs, 0, buf, 1);
811f3d54fc4Saliguori     if (ret < 0)
812f3d54fc4Saliguori         return -1;
813f3d54fc4Saliguori     /* test msdos magic */
814f3d54fc4Saliguori     if (buf[510] != 0x55 || buf[511] != 0xaa)
815f3d54fc4Saliguori         return -1;
816f3d54fc4Saliguori     for(i = 0; i < 4; i++) {
817f3d54fc4Saliguori         p = ((struct partition *)(buf + 0x1be)) + i;
818f3d54fc4Saliguori         nr_sects = le32_to_cpu(p->nr_sects);
819f3d54fc4Saliguori         if (nr_sects && p->end_head) {
820f3d54fc4Saliguori             /* We make the assumption that the partition terminates on
821f3d54fc4Saliguori                a cylinder boundary */
822f3d54fc4Saliguori             heads = p->end_head + 1;
823f3d54fc4Saliguori             sectors = p->end_sector & 63;
824f3d54fc4Saliguori             if (sectors == 0)
825f3d54fc4Saliguori                 continue;
826f3d54fc4Saliguori             cylinders = nb_sectors / (heads * sectors);
827f3d54fc4Saliguori             if (cylinders < 1 || cylinders > 16383)
828f3d54fc4Saliguori                 continue;
829f3d54fc4Saliguori             *pheads = heads;
830f3d54fc4Saliguori             *psectors = sectors;
831f3d54fc4Saliguori             *pcylinders = cylinders;
832f3d54fc4Saliguori #if 0
833f3d54fc4Saliguori             printf("guessed geometry: LCHS=%d %d %d\n",
834f3d54fc4Saliguori                    cylinders, heads, sectors);
835f3d54fc4Saliguori #endif
836f3d54fc4Saliguori             return 0;
837f3d54fc4Saliguori         }
838f3d54fc4Saliguori     }
839f3d54fc4Saliguori     return -1;
840f3d54fc4Saliguori }
841f3d54fc4Saliguori 
842f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
843f3d54fc4Saliguori {
844f3d54fc4Saliguori     int translation, lba_detected = 0;
845f3d54fc4Saliguori     int cylinders, heads, secs;
846a38131b6Sblueswir1     uint64_t nb_sectors;
847f3d54fc4Saliguori 
848f3d54fc4Saliguori     /* if a geometry hint is available, use it */
849f3d54fc4Saliguori     bdrv_get_geometry(bs, &nb_sectors);
850f3d54fc4Saliguori     bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
851f3d54fc4Saliguori     translation = bdrv_get_translation_hint(bs);
852f3d54fc4Saliguori     if (cylinders != 0) {
853f3d54fc4Saliguori         *pcyls = cylinders;
854f3d54fc4Saliguori         *pheads = heads;
855f3d54fc4Saliguori         *psecs = secs;
856f3d54fc4Saliguori     } else {
857f3d54fc4Saliguori         if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
858f3d54fc4Saliguori             if (heads > 16) {
859f3d54fc4Saliguori                 /* if heads > 16, it means that a BIOS LBA
860f3d54fc4Saliguori                    translation was active, so the default
861f3d54fc4Saliguori                    hardware geometry is OK */
862f3d54fc4Saliguori                 lba_detected = 1;
863f3d54fc4Saliguori                 goto default_geometry;
864f3d54fc4Saliguori             } else {
865f3d54fc4Saliguori                 *pcyls = cylinders;
866f3d54fc4Saliguori                 *pheads = heads;
867f3d54fc4Saliguori                 *psecs = secs;
868f3d54fc4Saliguori                 /* disable any translation to be in sync with
869f3d54fc4Saliguori                    the logical geometry */
870f3d54fc4Saliguori                 if (translation == BIOS_ATA_TRANSLATION_AUTO) {
871f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
872f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_NONE);
873f3d54fc4Saliguori                 }
874f3d54fc4Saliguori             }
875f3d54fc4Saliguori         } else {
876f3d54fc4Saliguori         default_geometry:
877f3d54fc4Saliguori             /* if no geometry, use a standard physical disk geometry */
878f3d54fc4Saliguori             cylinders = nb_sectors / (16 * 63);
879f3d54fc4Saliguori 
880f3d54fc4Saliguori             if (cylinders > 16383)
881f3d54fc4Saliguori                 cylinders = 16383;
882f3d54fc4Saliguori             else if (cylinders < 2)
883f3d54fc4Saliguori                 cylinders = 2;
884f3d54fc4Saliguori             *pcyls = cylinders;
885f3d54fc4Saliguori             *pheads = 16;
886f3d54fc4Saliguori             *psecs = 63;
887f3d54fc4Saliguori             if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
888f3d54fc4Saliguori                 if ((*pcyls * *pheads) <= 131072) {
889f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
890f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LARGE);
891f3d54fc4Saliguori                 } else {
892f3d54fc4Saliguori                     bdrv_set_translation_hint(bs,
893f3d54fc4Saliguori                                               BIOS_ATA_TRANSLATION_LBA);
894f3d54fc4Saliguori                 }
895f3d54fc4Saliguori             }
896f3d54fc4Saliguori         }
897f3d54fc4Saliguori         bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
898f3d54fc4Saliguori     }
899f3d54fc4Saliguori }
900f3d54fc4Saliguori 
901b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
902b338082bSbellard                             int cyls, int heads, int secs)
903b338082bSbellard {
904b338082bSbellard     bs->cyls = cyls;
905b338082bSbellard     bs->heads = heads;
906b338082bSbellard     bs->secs = secs;
907b338082bSbellard }
908b338082bSbellard 
909b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
910b338082bSbellard {
911b338082bSbellard     bs->type = type;
912b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
913b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
914b338082bSbellard }
915b338082bSbellard 
91646d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
91746d4767dSbellard {
91846d4767dSbellard     bs->translation = translation;
91946d4767dSbellard }
92046d4767dSbellard 
921b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
922b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
923b338082bSbellard {
924b338082bSbellard     *pcyls = bs->cyls;
925b338082bSbellard     *pheads = bs->heads;
926b338082bSbellard     *psecs = bs->secs;
927b338082bSbellard }
928b338082bSbellard 
929b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
930b338082bSbellard {
931b338082bSbellard     return bs->type;
932b338082bSbellard }
933b338082bSbellard 
93446d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
93546d4767dSbellard {
93646d4767dSbellard     return bs->translation;
93746d4767dSbellard }
93846d4767dSbellard 
939b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
940b338082bSbellard {
941b338082bSbellard     return bs->removable;
942b338082bSbellard }
943b338082bSbellard 
944b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
945b338082bSbellard {
946b338082bSbellard     return bs->read_only;
947b338082bSbellard }
948b338082bSbellard 
949985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
950985a03b0Sths {
951985a03b0Sths     return bs->sg;
952985a03b0Sths }
953985a03b0Sths 
95419cb3738Sbellard /* XXX: no longer used */
955b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
956b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
957b338082bSbellard {
958b338082bSbellard     bs->change_cb = change_cb;
959b338082bSbellard     bs->change_opaque = opaque;
960b338082bSbellard }
961b338082bSbellard 
962ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
963ea2384d3Sbellard {
964ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
965ea2384d3Sbellard         return 1;
966ea2384d3Sbellard     return bs->encrypted;
967ea2384d3Sbellard }
968ea2384d3Sbellard 
969c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs)
970c0f4ce77Saliguori {
971c0f4ce77Saliguori     BlockDriverState *backing_hd = bs->backing_hd;
972c0f4ce77Saliguori 
973c0f4ce77Saliguori     if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
974c0f4ce77Saliguori         return 1;
975c0f4ce77Saliguori     return (bs->encrypted && !bs->valid_key);
976c0f4ce77Saliguori }
977c0f4ce77Saliguori 
978ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
979ea2384d3Sbellard {
980ea2384d3Sbellard     int ret;
981ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
982ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
983ea2384d3Sbellard         if (ret < 0)
984ea2384d3Sbellard             return ret;
985ea2384d3Sbellard         if (!bs->encrypted)
986ea2384d3Sbellard             return 0;
987ea2384d3Sbellard     }
988ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
989ea2384d3Sbellard         return -1;
990c0f4ce77Saliguori     ret = bs->drv->bdrv_set_key(bs, key);
991bb5fc20fSaliguori     if (ret < 0) {
992bb5fc20fSaliguori         bs->valid_key = 0;
993bb5fc20fSaliguori     } else if (!bs->valid_key) {
994bb5fc20fSaliguori         bs->valid_key = 1;
995bb5fc20fSaliguori         /* call the change callback now, we skipped it on open */
996bb5fc20fSaliguori         bs->media_changed = 1;
997bb5fc20fSaliguori         if (bs->change_cb)
998bb5fc20fSaliguori             bs->change_cb(bs->change_opaque);
999bb5fc20fSaliguori     }
1000c0f4ce77Saliguori     return ret;
1001ea2384d3Sbellard }
1002ea2384d3Sbellard 
1003ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1004ea2384d3Sbellard {
100519cb3738Sbellard     if (!bs->drv) {
1006ea2384d3Sbellard         buf[0] = '\0';
1007ea2384d3Sbellard     } else {
1008ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
1009ea2384d3Sbellard     }
1010ea2384d3Sbellard }
1011ea2384d3Sbellard 
1012ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1013ea2384d3Sbellard                          void *opaque)
1014ea2384d3Sbellard {
1015ea2384d3Sbellard     BlockDriver *drv;
1016ea2384d3Sbellard 
1017ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
1018ea2384d3Sbellard         it(opaque, drv->format_name);
1019ea2384d3Sbellard     }
1020ea2384d3Sbellard }
1021ea2384d3Sbellard 
1022b338082bSbellard BlockDriverState *bdrv_find(const char *name)
1023b338082bSbellard {
1024b338082bSbellard     BlockDriverState *bs;
1025b338082bSbellard 
1026b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1027b338082bSbellard         if (!strcmp(name, bs->device_name))
1028b338082bSbellard             return bs;
1029b338082bSbellard     }
1030b338082bSbellard     return NULL;
1031b338082bSbellard }
1032b338082bSbellard 
103351de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
103481d0912dSbellard {
103581d0912dSbellard     BlockDriverState *bs;
103681d0912dSbellard 
103781d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
103851de9760Saliguori         it(opaque, bs);
103981d0912dSbellard     }
104081d0912dSbellard }
104181d0912dSbellard 
1042ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
1043ea2384d3Sbellard {
1044ea2384d3Sbellard     return bs->device_name;
1045ea2384d3Sbellard }
1046ea2384d3Sbellard 
10477a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
10487a6cba61Spbrook {
1049081501daSaliguori     if (!bs->drv)
1050081501daSaliguori         return;
10517a6cba61Spbrook     if (bs->drv->bdrv_flush)
10527a6cba61Spbrook         bs->drv->bdrv_flush(bs);
10537a6cba61Spbrook     if (bs->backing_hd)
10547a6cba61Spbrook         bdrv_flush(bs->backing_hd);
10557a6cba61Spbrook }
10567a6cba61Spbrook 
1057c6ca28d6Saliguori void bdrv_flush_all(void)
1058c6ca28d6Saliguori {
1059c6ca28d6Saliguori     BlockDriverState *bs;
1060c6ca28d6Saliguori 
1061c6ca28d6Saliguori     for (bs = bdrv_first; bs != NULL; bs = bs->next)
1062c6ca28d6Saliguori         if (bs->drv && !bdrv_is_read_only(bs) &&
1063c6ca28d6Saliguori             (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1064c6ca28d6Saliguori             bdrv_flush(bs);
1065c6ca28d6Saliguori }
1066c6ca28d6Saliguori 
1067f58c7b35Sths /*
1068f58c7b35Sths  * Returns true iff the specified sector is present in the disk image. Drivers
1069f58c7b35Sths  * not implementing the functionality are assumed to not support backing files,
1070f58c7b35Sths  * hence all their sectors are reported as allocated.
1071f58c7b35Sths  *
1072f58c7b35Sths  * 'pnum' is set to the number of sectors (including and immediately following
1073f58c7b35Sths  * the specified sector) that are known to be in the same
1074f58c7b35Sths  * allocated/unallocated state.
1075f58c7b35Sths  *
1076f58c7b35Sths  * 'nb_sectors' is the max value 'pnum' should be set to.
1077f58c7b35Sths  */
1078f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1079f58c7b35Sths 	int *pnum)
1080f58c7b35Sths {
1081f58c7b35Sths     int64_t n;
1082f58c7b35Sths     if (!bs->drv->bdrv_is_allocated) {
1083f58c7b35Sths         if (sector_num >= bs->total_sectors) {
1084f58c7b35Sths             *pnum = 0;
1085f58c7b35Sths             return 0;
1086f58c7b35Sths         }
1087f58c7b35Sths         n = bs->total_sectors - sector_num;
1088f58c7b35Sths         *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1089f58c7b35Sths         return 1;
1090f58c7b35Sths     }
1091f58c7b35Sths     return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1092f58c7b35Sths }
1093f58c7b35Sths 
1094376253ecSaliguori void bdrv_info(Monitor *mon)
1095b338082bSbellard {
1096b338082bSbellard     BlockDriverState *bs;
1097b338082bSbellard 
1098b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1099376253ecSaliguori         monitor_printf(mon, "%s:", bs->device_name);
1100376253ecSaliguori         monitor_printf(mon, " type=");
1101b338082bSbellard         switch(bs->type) {
1102b338082bSbellard         case BDRV_TYPE_HD:
1103376253ecSaliguori             monitor_printf(mon, "hd");
1104b338082bSbellard             break;
1105b338082bSbellard         case BDRV_TYPE_CDROM:
1106376253ecSaliguori             monitor_printf(mon, "cdrom");
1107b338082bSbellard             break;
1108b338082bSbellard         case BDRV_TYPE_FLOPPY:
1109376253ecSaliguori             monitor_printf(mon, "floppy");
1110b338082bSbellard             break;
1111b338082bSbellard         }
1112376253ecSaliguori         monitor_printf(mon, " removable=%d", bs->removable);
1113b338082bSbellard         if (bs->removable) {
1114376253ecSaliguori             monitor_printf(mon, " locked=%d", bs->locked);
1115b338082bSbellard         }
111619cb3738Sbellard         if (bs->drv) {
1117376253ecSaliguori             monitor_printf(mon, " file=");
1118376253ecSaliguori             monitor_print_filename(mon, bs->filename);
1119fef30743Sths             if (bs->backing_file[0] != '\0') {
1120376253ecSaliguori                 monitor_printf(mon, " backing_file=");
1121376253ecSaliguori                 monitor_print_filename(mon, bs->backing_file);
1122fef30743Sths             }
1123376253ecSaliguori             monitor_printf(mon, " ro=%d", bs->read_only);
1124376253ecSaliguori             monitor_printf(mon, " drv=%s", bs->drv->format_name);
1125376253ecSaliguori             monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1126b338082bSbellard         } else {
1127376253ecSaliguori             monitor_printf(mon, " [not inserted]");
1128b338082bSbellard         }
1129376253ecSaliguori         monitor_printf(mon, "\n");
1130b338082bSbellard     }
1131b338082bSbellard }
1132a36e69ddSths 
1133a36e69ddSths /* The "info blockstats" command. */
1134376253ecSaliguori void bdrv_info_stats(Monitor *mon)
1135a36e69ddSths {
1136a36e69ddSths     BlockDriverState *bs;
1137a36e69ddSths 
1138a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1139376253ecSaliguori         monitor_printf(mon, "%s:"
1140a36e69ddSths                        " rd_bytes=%" PRIu64
1141a36e69ddSths                        " wr_bytes=%" PRIu64
1142a36e69ddSths                        " rd_operations=%" PRIu64
1143a36e69ddSths                        " wr_operations=%" PRIu64
1144ebf53fcdSaliguori                        "\n",
1145a36e69ddSths                        bs->device_name,
1146a36e69ddSths                        bs->rd_bytes, bs->wr_bytes,
1147a36e69ddSths                        bs->rd_ops, bs->wr_ops);
1148a36e69ddSths     }
1149a36e69ddSths }
1150ea2384d3Sbellard 
1151045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1152045df330Saliguori {
1153045df330Saliguori     if (bs->backing_hd && bs->backing_hd->encrypted)
1154045df330Saliguori         return bs->backing_file;
1155045df330Saliguori     else if (bs->encrypted)
1156045df330Saliguori         return bs->filename;
1157045df330Saliguori     else
1158045df330Saliguori         return NULL;
1159045df330Saliguori }
1160045df330Saliguori 
116183f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
116283f64091Sbellard                                char *filename, int filename_size)
116383f64091Sbellard {
116483f64091Sbellard     if (!bs->backing_hd) {
116583f64091Sbellard         pstrcpy(filename, filename_size, "");
116683f64091Sbellard     } else {
116783f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
116883f64091Sbellard     }
116983f64091Sbellard }
117083f64091Sbellard 
1171faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1172faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
1173faea38e7Sbellard {
1174faea38e7Sbellard     BlockDriver *drv = bs->drv;
1175faea38e7Sbellard     if (!drv)
117619cb3738Sbellard         return -ENOMEDIUM;
1177faea38e7Sbellard     if (!drv->bdrv_write_compressed)
1178faea38e7Sbellard         return -ENOTSUP;
1179fbb7b4e0SKevin Wolf     if (bdrv_check_request(bs, sector_num, nb_sectors))
1180fbb7b4e0SKevin Wolf         return -EIO;
1181faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1182faea38e7Sbellard }
1183faea38e7Sbellard 
1184faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1185faea38e7Sbellard {
1186faea38e7Sbellard     BlockDriver *drv = bs->drv;
1187faea38e7Sbellard     if (!drv)
118819cb3738Sbellard         return -ENOMEDIUM;
1189faea38e7Sbellard     if (!drv->bdrv_get_info)
1190faea38e7Sbellard         return -ENOTSUP;
1191faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
1192faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
1193faea38e7Sbellard }
1194faea38e7Sbellard 
1195178e08a5Saliguori int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size)
1196178e08a5Saliguori {
1197178e08a5Saliguori     BlockDriver *drv = bs->drv;
1198178e08a5Saliguori     if (!drv)
1199178e08a5Saliguori         return -ENOMEDIUM;
1200178e08a5Saliguori     if (!drv->bdrv_put_buffer)
1201178e08a5Saliguori         return -ENOTSUP;
1202178e08a5Saliguori     return drv->bdrv_put_buffer(bs, buf, pos, size);
1203178e08a5Saliguori }
1204178e08a5Saliguori 
1205178e08a5Saliguori int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size)
1206178e08a5Saliguori {
1207178e08a5Saliguori     BlockDriver *drv = bs->drv;
1208178e08a5Saliguori     if (!drv)
1209178e08a5Saliguori         return -ENOMEDIUM;
1210178e08a5Saliguori     if (!drv->bdrv_get_buffer)
1211178e08a5Saliguori         return -ENOTSUP;
1212178e08a5Saliguori     return drv->bdrv_get_buffer(bs, buf, pos, size);
1213178e08a5Saliguori }
1214178e08a5Saliguori 
1215faea38e7Sbellard /**************************************************************/
1216faea38e7Sbellard /* handling of snapshots */
1217faea38e7Sbellard 
1218faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
1219faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
1220faea38e7Sbellard {
1221faea38e7Sbellard     BlockDriver *drv = bs->drv;
1222faea38e7Sbellard     if (!drv)
122319cb3738Sbellard         return -ENOMEDIUM;
1224faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
1225faea38e7Sbellard         return -ENOTSUP;
1226faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
1227faea38e7Sbellard }
1228faea38e7Sbellard 
1229faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
1230faea38e7Sbellard                        const char *snapshot_id)
1231faea38e7Sbellard {
1232faea38e7Sbellard     BlockDriver *drv = bs->drv;
1233faea38e7Sbellard     if (!drv)
123419cb3738Sbellard         return -ENOMEDIUM;
1235faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
1236faea38e7Sbellard         return -ENOTSUP;
1237faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
1238faea38e7Sbellard }
1239faea38e7Sbellard 
1240faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1241faea38e7Sbellard {
1242faea38e7Sbellard     BlockDriver *drv = bs->drv;
1243faea38e7Sbellard     if (!drv)
124419cb3738Sbellard         return -ENOMEDIUM;
1245faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
1246faea38e7Sbellard         return -ENOTSUP;
1247faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1248faea38e7Sbellard }
1249faea38e7Sbellard 
1250faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1251faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1252faea38e7Sbellard {
1253faea38e7Sbellard     BlockDriver *drv = bs->drv;
1254faea38e7Sbellard     if (!drv)
125519cb3738Sbellard         return -ENOMEDIUM;
1256faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1257faea38e7Sbellard         return -ENOTSUP;
1258faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1259faea38e7Sbellard }
1260faea38e7Sbellard 
1261faea38e7Sbellard #define NB_SUFFIXES 4
1262faea38e7Sbellard 
1263faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1264faea38e7Sbellard {
1265faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1266faea38e7Sbellard     int64_t base;
1267faea38e7Sbellard     int i;
1268faea38e7Sbellard 
1269faea38e7Sbellard     if (size <= 999) {
1270faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1271faea38e7Sbellard     } else {
1272faea38e7Sbellard         base = 1024;
1273faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1274faea38e7Sbellard             if (size < (10 * base)) {
1275faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1276faea38e7Sbellard                          (double)size / base,
1277faea38e7Sbellard                          suffixes[i]);
1278faea38e7Sbellard                 break;
1279faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1280faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1281faea38e7Sbellard                          ((size + (base >> 1)) / base),
1282faea38e7Sbellard                          suffixes[i]);
1283faea38e7Sbellard                 break;
1284faea38e7Sbellard             }
1285faea38e7Sbellard             base = base * 1024;
1286faea38e7Sbellard         }
1287faea38e7Sbellard     }
1288faea38e7Sbellard     return buf;
1289faea38e7Sbellard }
1290faea38e7Sbellard 
1291faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1292faea38e7Sbellard {
1293faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
12943b9f94e1Sbellard #ifdef _WIN32
12953b9f94e1Sbellard     struct tm *ptm;
12963b9f94e1Sbellard #else
1297faea38e7Sbellard     struct tm tm;
12983b9f94e1Sbellard #endif
1299faea38e7Sbellard     time_t ti;
1300faea38e7Sbellard     int64_t secs;
1301faea38e7Sbellard 
1302faea38e7Sbellard     if (!sn) {
1303faea38e7Sbellard         snprintf(buf, buf_size,
1304faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1305faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1306faea38e7Sbellard     } else {
1307faea38e7Sbellard         ti = sn->date_sec;
13083b9f94e1Sbellard #ifdef _WIN32
13093b9f94e1Sbellard         ptm = localtime(&ti);
13103b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
13113b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
13123b9f94e1Sbellard #else
1313faea38e7Sbellard         localtime_r(&ti, &tm);
1314faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1315faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
13163b9f94e1Sbellard #endif
1317faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1318faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1319faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1320faea38e7Sbellard                  (int)(secs / 3600),
1321faea38e7Sbellard                  (int)((secs / 60) % 60),
1322faea38e7Sbellard                  (int)(secs % 60),
1323faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1324faea38e7Sbellard         snprintf(buf, buf_size,
1325faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1326faea38e7Sbellard                  sn->id_str, sn->name,
1327faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1328faea38e7Sbellard                  date_buf,
1329faea38e7Sbellard                  clock_buf);
1330faea38e7Sbellard     }
1331faea38e7Sbellard     return buf;
1332faea38e7Sbellard }
1333faea38e7Sbellard 
133483f64091Sbellard 
1335ea2384d3Sbellard /**************************************************************/
133683f64091Sbellard /* async I/Os */
1337ea2384d3Sbellard 
13383b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1339f141eafeSaliguori                                  QEMUIOVector *qiov, int nb_sectors,
134083f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
1341ea2384d3Sbellard {
134283f64091Sbellard     BlockDriver *drv = bs->drv;
1343a36e69ddSths     BlockDriverAIOCB *ret;
1344ea2384d3Sbellard 
134519cb3738Sbellard     if (!drv)
1346ce1a14dcSpbrook         return NULL;
134771d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
134871d0770cSaliguori         return NULL;
134983f64091Sbellard 
1350f141eafeSaliguori     ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1351f141eafeSaliguori                               cb, opaque);
1352a36e69ddSths 
1353a36e69ddSths     if (ret) {
1354a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1355a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1356a36e69ddSths 	bs->rd_ops ++;
1357a36e69ddSths     }
1358a36e69ddSths 
1359a36e69ddSths     return ret;
136083f64091Sbellard }
136183f64091Sbellard 
1362f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1363f141eafeSaliguori                                   QEMUIOVector *qiov, int nb_sectors,
136483f64091Sbellard                                   BlockDriverCompletionFunc *cb, void *opaque)
13657674e7bfSbellard {
136683f64091Sbellard     BlockDriver *drv = bs->drv;
1367a36e69ddSths     BlockDriverAIOCB *ret;
136883f64091Sbellard 
136919cb3738Sbellard     if (!drv)
1370ce1a14dcSpbrook         return NULL;
137183f64091Sbellard     if (bs->read_only)
1372ce1a14dcSpbrook         return NULL;
137371d0770cSaliguori     if (bdrv_check_request(bs, sector_num, nb_sectors))
137471d0770cSaliguori         return NULL;
137583f64091Sbellard 
1376f141eafeSaliguori     ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1377f141eafeSaliguori                                cb, opaque);
1378a36e69ddSths 
1379a36e69ddSths     if (ret) {
1380a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1381a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1382a36e69ddSths 	bs->wr_ops ++;
1383a36e69ddSths     }
1384a36e69ddSths 
1385a36e69ddSths     return ret;
138683f64091Sbellard }
138783f64091Sbellard 
138883f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
138983f64091Sbellard {
13906bbff9a0Saliguori     acb->pool->cancel(acb);
139183f64091Sbellard }
139283f64091Sbellard 
139383f64091Sbellard 
139483f64091Sbellard /**************************************************************/
139583f64091Sbellard /* async block device emulation */
139683f64091Sbellard 
139783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1398beac80cdSbellard {
1399ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1400f141eafeSaliguori 
1401f141eafeSaliguori     if (!acb->is_write)
1402f141eafeSaliguori         qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1403ceb42de8Saliguori     qemu_vfree(acb->bounce);
1404ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1405f141eafeSaliguori 
1406ce1a14dcSpbrook     qemu_aio_release(acb);
1407beac80cdSbellard }
1408beac80cdSbellard 
1409f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1410f141eafeSaliguori                                             int64_t sector_num,
1411f141eafeSaliguori                                             QEMUIOVector *qiov,
1412f141eafeSaliguori                                             int nb_sectors,
1413f141eafeSaliguori                                             BlockDriverCompletionFunc *cb,
1414f141eafeSaliguori                                             void *opaque,
1415f141eafeSaliguori                                             int is_write)
1416f141eafeSaliguori 
1417ea2384d3Sbellard {
1418ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
141983f64091Sbellard 
1420ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1421f141eafeSaliguori     acb->is_write = is_write;
1422f141eafeSaliguori     acb->qiov = qiov;
1423e268ca52Saliguori     acb->bounce = qemu_blockalign(bs, qiov->size);
1424f141eafeSaliguori 
1425ce1a14dcSpbrook     if (!acb->bh)
1426ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1427f141eafeSaliguori 
1428f141eafeSaliguori     if (is_write) {
1429f141eafeSaliguori         qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1430f141eafeSaliguori         acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1431f141eafeSaliguori     } else {
1432f141eafeSaliguori         acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1433f141eafeSaliguori     }
1434f141eafeSaliguori 
1435ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1436f141eafeSaliguori 
1437ce1a14dcSpbrook     return &acb->common;
14387a6cba61Spbrook }
14397a6cba61Spbrook 
1440f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1441f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1442ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
144383f64091Sbellard {
1444f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
144583f64091Sbellard }
144683f64091Sbellard 
1447f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1448f141eafeSaliguori         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1449f141eafeSaliguori         BlockDriverCompletionFunc *cb, void *opaque)
1450f141eafeSaliguori {
1451f141eafeSaliguori     return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1452f141eafeSaliguori }
1453f141eafeSaliguori 
1454f141eafeSaliguori 
1455ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
145683f64091Sbellard {
1457ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1458ce1a14dcSpbrook     qemu_bh_cancel(acb->bh);
1459ce1a14dcSpbrook     qemu_aio_release(acb);
146083f64091Sbellard }
146183f64091Sbellard 
146283f64091Sbellard /**************************************************************/
146383f64091Sbellard /* sync block device emulation */
146483f64091Sbellard 
146583f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
146683f64091Sbellard {
146783f64091Sbellard     *(int *)opaque = ret;
146883f64091Sbellard }
146983f64091Sbellard 
147083f64091Sbellard #define NOT_DONE 0x7fffffff
147183f64091Sbellard 
147283f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
147383f64091Sbellard                         uint8_t *buf, int nb_sectors)
147483f64091Sbellard {
1475ce1a14dcSpbrook     int async_ret;
1476ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1477f141eafeSaliguori     struct iovec iov;
1478f141eafeSaliguori     QEMUIOVector qiov;
147983f64091Sbellard 
148083f64091Sbellard     async_ret = NOT_DONE;
14813f4cb3d3Sblueswir1     iov.iov_base = (void *)buf;
1482f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1483f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1484f141eafeSaliguori     acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
148583f64091Sbellard         bdrv_rw_em_cb, &async_ret);
1486baf35cb9Saliguori     if (acb == NULL)
1487ce1a14dcSpbrook         return -1;
1488baf35cb9Saliguori 
148983f64091Sbellard     while (async_ret == NOT_DONE) {
149083f64091Sbellard         qemu_aio_wait();
149183f64091Sbellard     }
1492baf35cb9Saliguori 
149383f64091Sbellard     return async_ret;
149483f64091Sbellard }
149583f64091Sbellard 
149683f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
149783f64091Sbellard                          const uint8_t *buf, int nb_sectors)
149883f64091Sbellard {
1499ce1a14dcSpbrook     int async_ret;
1500ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1501f141eafeSaliguori     struct iovec iov;
1502f141eafeSaliguori     QEMUIOVector qiov;
150383f64091Sbellard 
150483f64091Sbellard     async_ret = NOT_DONE;
1505f141eafeSaliguori     iov.iov_base = (void *)buf;
1506f141eafeSaliguori     iov.iov_len = nb_sectors * 512;
1507f141eafeSaliguori     qemu_iovec_init_external(&qiov, &iov, 1);
1508f141eafeSaliguori     acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
150983f64091Sbellard         bdrv_rw_em_cb, &async_ret);
1510baf35cb9Saliguori     if (acb == NULL)
1511ce1a14dcSpbrook         return -1;
151283f64091Sbellard     while (async_ret == NOT_DONE) {
151383f64091Sbellard         qemu_aio_wait();
151483f64091Sbellard     }
151583f64091Sbellard     return async_ret;
151683f64091Sbellard }
1517ea2384d3Sbellard 
1518ea2384d3Sbellard void bdrv_init(void)
1519ea2384d3Sbellard {
15205efa9d5aSAnthony Liguori     module_call_init(MODULE_INIT_BLOCK);
1521ea2384d3Sbellard }
1522ce1a14dcSpbrook 
15236bbff9a0Saliguori void aio_pool_init(AIOPool *pool, int aiocb_size,
15246bbff9a0Saliguori                    void (*cancel)(BlockDriverAIOCB *acb))
1525ce1a14dcSpbrook {
15266bbff9a0Saliguori     pool->aiocb_size = aiocb_size;
15276bbff9a0Saliguori     pool->cancel = cancel;
15286bbff9a0Saliguori     pool->free_aiocb = NULL;
15296bbff9a0Saliguori }
15306bbff9a0Saliguori 
15316bbff9a0Saliguori void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs,
15326bbff9a0Saliguori                         BlockDriverCompletionFunc *cb, void *opaque)
15336bbff9a0Saliguori {
1534ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1535ce1a14dcSpbrook 
15366bbff9a0Saliguori     if (pool->free_aiocb) {
15376bbff9a0Saliguori         acb = pool->free_aiocb;
15386bbff9a0Saliguori         pool->free_aiocb = acb->next;
1539ce1a14dcSpbrook     } else {
15406bbff9a0Saliguori         acb = qemu_mallocz(pool->aiocb_size);
15416bbff9a0Saliguori         acb->pool = pool;
1542ce1a14dcSpbrook     }
1543ce1a14dcSpbrook     acb->bs = bs;
1544ce1a14dcSpbrook     acb->cb = cb;
1545ce1a14dcSpbrook     acb->opaque = opaque;
1546ce1a14dcSpbrook     return acb;
1547ce1a14dcSpbrook }
1548ce1a14dcSpbrook 
15496bbff9a0Saliguori void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
15506bbff9a0Saliguori                    void *opaque)
15516bbff9a0Saliguori {
15526bbff9a0Saliguori     return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque);
15536bbff9a0Saliguori }
15546bbff9a0Saliguori 
1555ce1a14dcSpbrook void qemu_aio_release(void *p)
1556ce1a14dcSpbrook {
15576bbff9a0Saliguori     BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
15586bbff9a0Saliguori     AIOPool *pool = acb->pool;
15596bbff9a0Saliguori     acb->next = pool->free_aiocb;
15606bbff9a0Saliguori     pool->free_aiocb = acb;
1561ce1a14dcSpbrook }
156219cb3738Sbellard 
156319cb3738Sbellard /**************************************************************/
156419cb3738Sbellard /* removable device support */
156519cb3738Sbellard 
156619cb3738Sbellard /**
156719cb3738Sbellard  * Return TRUE if the media is present
156819cb3738Sbellard  */
156919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
157019cb3738Sbellard {
157119cb3738Sbellard     BlockDriver *drv = bs->drv;
157219cb3738Sbellard     int ret;
157319cb3738Sbellard     if (!drv)
157419cb3738Sbellard         return 0;
157519cb3738Sbellard     if (!drv->bdrv_is_inserted)
157619cb3738Sbellard         return 1;
157719cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
157819cb3738Sbellard     return ret;
157919cb3738Sbellard }
158019cb3738Sbellard 
158119cb3738Sbellard /**
158219cb3738Sbellard  * Return TRUE if the media changed since the last call to this
158319cb3738Sbellard  * function. It is currently only used for floppy disks
158419cb3738Sbellard  */
158519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
158619cb3738Sbellard {
158719cb3738Sbellard     BlockDriver *drv = bs->drv;
158819cb3738Sbellard     int ret;
158919cb3738Sbellard 
159019cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
159119cb3738Sbellard         ret = -ENOTSUP;
159219cb3738Sbellard     else
159319cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
159419cb3738Sbellard     if (ret == -ENOTSUP)
159519cb3738Sbellard         ret = bs->media_changed;
159619cb3738Sbellard     bs->media_changed = 0;
159719cb3738Sbellard     return ret;
159819cb3738Sbellard }
159919cb3738Sbellard 
160019cb3738Sbellard /**
160119cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
160219cb3738Sbellard  */
160319cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
160419cb3738Sbellard {
160519cb3738Sbellard     BlockDriver *drv = bs->drv;
160619cb3738Sbellard     int ret;
160719cb3738Sbellard 
160819cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
160919cb3738Sbellard         ret = -ENOTSUP;
161019cb3738Sbellard     } else {
161119cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
161219cb3738Sbellard     }
161319cb3738Sbellard     if (ret == -ENOTSUP) {
161419cb3738Sbellard         if (eject_flag)
161519cb3738Sbellard             bdrv_close(bs);
161619cb3738Sbellard     }
161719cb3738Sbellard }
161819cb3738Sbellard 
161919cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
162019cb3738Sbellard {
162119cb3738Sbellard     return bs->locked;
162219cb3738Sbellard }
162319cb3738Sbellard 
162419cb3738Sbellard /**
162519cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
162619cb3738Sbellard  * to eject it manually).
162719cb3738Sbellard  */
162819cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
162919cb3738Sbellard {
163019cb3738Sbellard     BlockDriver *drv = bs->drv;
163119cb3738Sbellard 
163219cb3738Sbellard     bs->locked = locked;
163319cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
163419cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
163519cb3738Sbellard     }
163619cb3738Sbellard }
1637985a03b0Sths 
1638985a03b0Sths /* needed for generic scsi interface */
1639985a03b0Sths 
1640985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1641985a03b0Sths {
1642985a03b0Sths     BlockDriver *drv = bs->drv;
1643985a03b0Sths 
1644985a03b0Sths     if (drv && drv->bdrv_ioctl)
1645985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1646985a03b0Sths     return -ENOTSUP;
1647985a03b0Sths }
16487d780669Saliguori 
1649221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1650221f715dSaliguori         unsigned long int req, void *buf,
16517d780669Saliguori         BlockDriverCompletionFunc *cb, void *opaque)
16527d780669Saliguori {
1653221f715dSaliguori     BlockDriver *drv = bs->drv;
16547d780669Saliguori 
1655221f715dSaliguori     if (drv && drv->bdrv_aio_ioctl)
1656221f715dSaliguori         return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1657221f715dSaliguori     return NULL;
16587d780669Saliguori }
1659e268ca52Saliguori 
1660e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size)
1661e268ca52Saliguori {
1662e268ca52Saliguori     return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1663e268ca52Saliguori }
1664