blob: 15f807a0cad19a234284045ea9470268ee24a49e [file] [log] [blame]
bellardfc01f7e2003-06-30 10:03:06 +00001/*
2 * QEMU System Emulator block driver
ths5fafdf22007-09-16 21:08:06 +00003 *
bellardfc01f7e2003-06-30 10:03:06 +00004 * Copyright (c) 2003 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardfc01f7e2003-06-30 10:03:06 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
pbrookfaf07962007-11-11 02:51:17 +000024#include "qemu-common.h"
pbrook87ecb682007-11-17 17:14:51 +000025#include "console.h"
bellardea2384d2004-08-01 21:59:26 +000026#include "block_int.h"
bellardfc01f7e2003-06-30 10:03:06 +000027
bellard7674e7b2005-04-26 21:59:26 +000028#ifdef _BSD
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <sys/ioctl.h>
32#include <sys/queue.h>
33#include <sys/disk.h>
34#endif
35
bellard83f64092006-08-01 16:21:11 +000036#define SECTOR_BITS 9
37#define SECTOR_SIZE (1 << SECTOR_BITS)
bellard3b0d4f62005-10-30 18:30:10 +000038
bellard90765422006-08-07 19:10:16 +000039typedef struct BlockDriverAIOCBSync {
40 BlockDriverAIOCB common;
41 QEMUBH *bh;
42 int ret;
43} BlockDriverAIOCBSync;
44
pbrookce1a14d2006-08-07 02:38:06 +000045static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
46 int64_t sector_num, uint8_t *buf, int nb_sectors,
47 BlockDriverCompletionFunc *cb, void *opaque);
48static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
49 int64_t sector_num, const uint8_t *buf, int nb_sectors,
50 BlockDriverCompletionFunc *cb, void *opaque);
bellard83f64092006-08-01 16:21:11 +000051static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
ths5fafdf22007-09-16 21:08:06 +000052static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +000053 uint8_t *buf, int nb_sectors);
54static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
55 const uint8_t *buf, int nb_sectors);
bellardec530c82006-04-25 22:36:06 +000056
blueswir17ee930d2008-09-17 19:04:14 +000057BlockDriverState *bdrv_first;
58
bellardea2384d2004-08-01 21:59:26 +000059static BlockDriver *first_drv;
60
bellard83f64092006-08-01 16:21:11 +000061int path_is_absolute(const char *path)
62{
63 const char *p;
bellard21664422007-01-07 18:22:37 +000064#ifdef _WIN32
65 /* specific case for names like: "\\.\d:" */
66 if (*path == '/' || *path == '\\')
67 return 1;
68#endif
bellard83f64092006-08-01 16:21:11 +000069 p = strchr(path, ':');
70 if (p)
71 p++;
72 else
73 p = path;
bellard3b9f94e2007-01-07 17:27:07 +000074#ifdef _WIN32
75 return (*p == '/' || *p == '\\');
76#else
77 return (*p == '/');
78#endif
bellard83f64092006-08-01 16:21:11 +000079}
80
81/* if filename is absolute, just copy it to dest. Otherwise, build a
82 path to it by considering it is relative to base_path. URL are
83 supported. */
84void path_combine(char *dest, int dest_size,
85 const char *base_path,
86 const char *filename)
87{
88 const char *p, *p1;
89 int len;
90
91 if (dest_size <= 0)
92 return;
93 if (path_is_absolute(filename)) {
94 pstrcpy(dest, dest_size, filename);
95 } else {
96 p = strchr(base_path, ':');
97 if (p)
98 p++;
99 else
100 p = base_path;
bellard3b9f94e2007-01-07 17:27:07 +0000101 p1 = strrchr(base_path, '/');
102#ifdef _WIN32
103 {
104 const char *p2;
105 p2 = strrchr(base_path, '\\');
106 if (!p1 || p2 > p1)
107 p1 = p2;
108 }
109#endif
bellard83f64092006-08-01 16:21:11 +0000110 if (p1)
111 p1++;
112 else
113 p1 = base_path;
114 if (p1 > p)
115 p = p1;
116 len = p - base_path;
117 if (len > dest_size - 1)
118 len = dest_size - 1;
119 memcpy(dest, base_path, len);
120 dest[len] = '\0';
121 pstrcat(dest, dest_size, filename);
122 }
123}
124
125
pbrook9596ebb2007-11-18 01:44:38 +0000126static void bdrv_register(BlockDriver *bdrv)
bellardea2384d2004-08-01 21:59:26 +0000127{
pbrookce1a14d2006-08-07 02:38:06 +0000128 if (!bdrv->bdrv_aio_read) {
bellard83f64092006-08-01 16:21:11 +0000129 /* add AIO emulation layer */
bellard83f64092006-08-01 16:21:11 +0000130 bdrv->bdrv_aio_read = bdrv_aio_read_em;
131 bdrv->bdrv_aio_write = bdrv_aio_write_em;
132 bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
bellard90765422006-08-07 19:10:16 +0000133 bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
bellard83f64092006-08-01 16:21:11 +0000134 } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
135 /* add synchronous IO emulation layer */
136 bdrv->bdrv_read = bdrv_read_em;
137 bdrv->bdrv_write = bdrv_write_em;
138 }
bellardea2384d2004-08-01 21:59:26 +0000139 bdrv->next = first_drv;
140 first_drv = bdrv;
141}
bellardb3380822004-03-14 21:38:54 +0000142
143/* create a new block device (by default it is empty) */
144BlockDriverState *bdrv_new(const char *device_name)
bellardfc01f7e2003-06-30 10:03:06 +0000145{
bellardb3380822004-03-14 21:38:54 +0000146 BlockDriverState **pbs, *bs;
147
148 bs = qemu_mallocz(sizeof(BlockDriverState));
149 if(!bs)
150 return NULL;
151 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
bellardea2384d2004-08-01 21:59:26 +0000152 if (device_name[0] != '\0') {
153 /* insert at the end */
154 pbs = &bdrv_first;
155 while (*pbs != NULL)
156 pbs = &(*pbs)->next;
157 *pbs = bs;
158 }
bellardb3380822004-03-14 21:38:54 +0000159 return bs;
160}
161
bellardea2384d2004-08-01 21:59:26 +0000162BlockDriver *bdrv_find_format(const char *format_name)
163{
164 BlockDriver *drv1;
165 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
166 if (!strcmp(drv1->format_name, format_name))
167 return drv1;
168 }
169 return NULL;
170}
171
ths5fafdf22007-09-16 21:08:06 +0000172int bdrv_create(BlockDriver *drv,
bellardea2384d2004-08-01 21:59:26 +0000173 const char *filename, int64_t size_in_sectors,
174 const char *backing_file, int flags)
175{
176 if (!drv->bdrv_create)
177 return -ENOTSUP;
178 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
179}
180
bellardd5249392004-08-03 21:14:23 +0000181#ifdef _WIN32
bellard95389c82005-12-18 18:28:15 +0000182void get_tmp_filename(char *filename, int size)
bellardd5249392004-08-03 21:14:23 +0000183{
bellard3b9f94e2007-01-07 17:27:07 +0000184 char temp_dir[MAX_PATH];
ths3b46e622007-09-17 08:09:54 +0000185
bellard3b9f94e2007-01-07 17:27:07 +0000186 GetTempPath(MAX_PATH, temp_dir);
187 GetTempFileName(temp_dir, "qem", 0, filename);
bellardd5249392004-08-03 21:14:23 +0000188}
189#else
bellard95389c82005-12-18 18:28:15 +0000190void get_tmp_filename(char *filename, int size)
bellardea2384d2004-08-01 21:59:26 +0000191{
192 int fd;
blueswir17ccfb2e2008-09-14 06:45:34 +0000193 const char *tmpdir;
bellardd5249392004-08-03 21:14:23 +0000194 /* XXX: race condition possible */
aurel320badc1e2008-03-10 00:05:34 +0000195 tmpdir = getenv("TMPDIR");
196 if (!tmpdir)
197 tmpdir = "/tmp";
198 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
bellardea2384d2004-08-01 21:59:26 +0000199 fd = mkstemp(filename);
200 close(fd);
201}
bellardd5249392004-08-03 21:14:23 +0000202#endif
bellardea2384d2004-08-01 21:59:26 +0000203
bellard19cb3732006-08-19 11:45:59 +0000204#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000205static int is_windows_drive_prefix(const char *filename)
206{
207 return (((filename[0] >= 'a' && filename[0] <= 'z') ||
208 (filename[0] >= 'A' && filename[0] <= 'Z')) &&
209 filename[1] == ':');
210}
ths3b46e622007-09-17 08:09:54 +0000211
bellard19cb3732006-08-19 11:45:59 +0000212static int is_windows_drive(const char *filename)
213{
ths5fafdf22007-09-16 21:08:06 +0000214 if (is_windows_drive_prefix(filename) &&
bellardf45512f2006-08-23 21:40:13 +0000215 filename[2] == '\0')
bellard19cb3732006-08-19 11:45:59 +0000216 return 1;
217 if (strstart(filename, "\\\\.\\", NULL) ||
218 strstart(filename, "//./", NULL))
219 return 1;
220 return 0;
221}
222#endif
223
bellard83f64092006-08-01 16:21:11 +0000224static BlockDriver *find_protocol(const char *filename)
225{
226 BlockDriver *drv1;
227 char protocol[128];
228 int len;
229 const char *p;
bellard19cb3732006-08-19 11:45:59 +0000230
231#ifdef _WIN32
bellardf45512f2006-08-23 21:40:13 +0000232 if (is_windows_drive(filename) ||
233 is_windows_drive_prefix(filename))
bellard19cb3732006-08-19 11:45:59 +0000234 return &bdrv_raw;
235#endif
bellard83f64092006-08-01 16:21:11 +0000236 p = strchr(filename, ':');
237 if (!p)
238 return &bdrv_raw;
239 len = p - filename;
240 if (len > sizeof(protocol) - 1)
241 len = sizeof(protocol) - 1;
bellard83f64092006-08-01 16:21:11 +0000242 memcpy(protocol, filename, len);
243 protocol[len] = '\0';
244 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
ths5fafdf22007-09-16 21:08:06 +0000245 if (drv1->protocol_name &&
bellard83f64092006-08-01 16:21:11 +0000246 !strcmp(drv1->protocol_name, protocol))
247 return drv1;
248 }
249 return NULL;
250}
251
bellard7674e7b2005-04-26 21:59:26 +0000252/* XXX: force raw format if block or character device ? It would
253 simplify the BSD case */
bellardea2384d2004-08-01 21:59:26 +0000254static BlockDriver *find_image_format(const char *filename)
255{
bellard83f64092006-08-01 16:21:11 +0000256 int ret, score, score_max;
bellardea2384d2004-08-01 21:59:26 +0000257 BlockDriver *drv1, *drv;
bellard83f64092006-08-01 16:21:11 +0000258 uint8_t buf[2048];
259 BlockDriverState *bs;
ths3b46e622007-09-17 08:09:54 +0000260
bellard19cb3732006-08-19 11:45:59 +0000261 /* detect host devices. By convention, /dev/cdrom[N] is always
262 recognized as a host CDROM */
263 if (strstart(filename, "/dev/cdrom", NULL))
264 return &bdrv_host_device;
265#ifdef _WIN32
266 if (is_windows_drive(filename))
267 return &bdrv_host_device;
268#else
269 {
270 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000271 if (stat(filename, &st) >= 0 &&
bellard19cb3732006-08-19 11:45:59 +0000272 (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
273 return &bdrv_host_device;
274 }
275 }
276#endif
ths3b46e622007-09-17 08:09:54 +0000277
bellard83f64092006-08-01 16:21:11 +0000278 drv = find_protocol(filename);
bellard19cb3732006-08-19 11:45:59 +0000279 /* no need to test disk image formats for vvfat */
bellard83f64092006-08-01 16:21:11 +0000280 if (drv == &bdrv_vvfat)
281 return drv;
bellard19cb3732006-08-19 11:45:59 +0000282
bellard83f64092006-08-01 16:21:11 +0000283 ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
284 if (ret < 0)
285 return NULL;
286 ret = bdrv_pread(bs, 0, buf, sizeof(buf));
287 bdrv_delete(bs);
288 if (ret < 0) {
289 return NULL;
290 }
291
bellardea2384d2004-08-01 21:59:26 +0000292 score_max = 0;
293 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
bellard83f64092006-08-01 16:21:11 +0000294 if (drv1->bdrv_probe) {
295 score = drv1->bdrv_probe(buf, ret, filename);
296 if (score > score_max) {
297 score_max = score;
298 drv = drv1;
299 }
bellardea2384d2004-08-01 21:59:26 +0000300 }
301 }
302 return drv;
303}
304
bellard83f64092006-08-01 16:21:11 +0000305int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
bellardb3380822004-03-14 21:38:54 +0000306{
bellard83f64092006-08-01 16:21:11 +0000307 BlockDriverState *bs;
308 int ret;
309
310 bs = bdrv_new("");
311 if (!bs)
312 return -ENOMEM;
313 ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
314 if (ret < 0) {
315 bdrv_delete(bs);
316 return ret;
bellard3b0d4f62005-10-30 18:30:10 +0000317 }
bellard83f64092006-08-01 16:21:11 +0000318 *pbs = bs;
319 return 0;
bellardea2384d2004-08-01 21:59:26 +0000320}
bellardfc01f7e2003-06-30 10:03:06 +0000321
bellard83f64092006-08-01 16:21:11 +0000322int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
323{
324 return bdrv_open2(bs, filename, flags, NULL);
325}
326
327int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
bellardea2384d2004-08-01 21:59:26 +0000328 BlockDriver *drv)
329{
bellard83f64092006-08-01 16:21:11 +0000330 int ret, open_flags;
thseb5c8512007-02-11 15:06:09 +0000331 char tmp_filename[PATH_MAX];
332 char backing_filename[PATH_MAX];
ths3b46e622007-09-17 08:09:54 +0000333
bellardea2384d2004-08-01 21:59:26 +0000334 bs->read_only = 0;
335 bs->is_temporary = 0;
336 bs->encrypted = 0;
bellard712e7872005-04-28 21:09:32 +0000337
bellard83f64092006-08-01 16:21:11 +0000338 if (flags & BDRV_O_SNAPSHOT) {
bellardea2384d2004-08-01 21:59:26 +0000339 BlockDriverState *bs1;
340 int64_t total_size;
aliguori7c96d462008-09-12 17:54:13 +0000341 int is_protocol = 0;
ths3b46e622007-09-17 08:09:54 +0000342
bellardea2384d2004-08-01 21:59:26 +0000343 /* if snapshot, we create a temporary backing file and open it
344 instead of opening 'filename' directly */
345
346 /* if there is a backing file, use it */
347 bs1 = bdrv_new("");
348 if (!bs1) {
bellard83f64092006-08-01 16:21:11 +0000349 return -ENOMEM;
bellardea2384d2004-08-01 21:59:26 +0000350 }
351 if (bdrv_open(bs1, filename, 0) < 0) {
352 bdrv_delete(bs1);
353 return -1;
354 }
bellard83f64092006-08-01 16:21:11 +0000355 total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
aliguori7c96d462008-09-12 17:54:13 +0000356
357 if (bs1->drv && bs1->drv->protocol_name)
358 is_protocol = 1;
359
bellardea2384d2004-08-01 21:59:26 +0000360 bdrv_delete(bs1);
ths3b46e622007-09-17 08:09:54 +0000361
bellardea2384d2004-08-01 21:59:26 +0000362 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
aliguori7c96d462008-09-12 17:54:13 +0000363
364 /* Real path is meaningless for protocols */
365 if (is_protocol)
366 snprintf(backing_filename, sizeof(backing_filename),
367 "%s", filename);
368 else
369 realpath(filename, backing_filename);
370
ths5fafdf22007-09-16 21:08:06 +0000371 if (bdrv_create(&bdrv_qcow2, tmp_filename,
bellarda817d932006-08-24 19:53:37 +0000372 total_size, backing_filename, 0) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000373 return -1;
374 }
375 filename = tmp_filename;
376 bs->is_temporary = 1;
377 }
bellard712e7872005-04-28 21:09:32 +0000378
bellardea2384d2004-08-01 21:59:26 +0000379 pstrcpy(bs->filename, sizeof(bs->filename), filename);
bellard83f64092006-08-01 16:21:11 +0000380 if (flags & BDRV_O_FILE) {
381 drv = find_protocol(filename);
bellardea2384d2004-08-01 21:59:26 +0000382 if (!drv)
bellard83f64092006-08-01 16:21:11 +0000383 return -ENOENT;
384 } else {
385 if (!drv) {
386 drv = find_image_format(filename);
387 if (!drv)
388 return -1;
389 }
bellardea2384d2004-08-01 21:59:26 +0000390 }
391 bs->drv = drv;
392 bs->opaque = qemu_mallocz(drv->instance_size);
393 if (bs->opaque == NULL && drv->instance_size > 0)
394 return -1;
bellard83f64092006-08-01 16:21:11 +0000395 /* Note: for compatibility, we open disk image files as RDWR, and
396 RDONLY as fallback */
397 if (!(flags & BDRV_O_FILE))
balrog33f00272007-12-24 14:33:24 +0000398 open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
bellard83f64092006-08-01 16:21:11 +0000399 else
400 open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
401 ret = drv->bdrv_open(bs, filename, open_flags);
402 if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
403 ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
404 bs->read_only = 1;
405 }
bellardea2384d2004-08-01 21:59:26 +0000406 if (ret < 0) {
407 qemu_free(bs->opaque);
bellard6b21b972006-08-23 21:14:37 +0000408 bs->opaque = NULL;
409 bs->drv = NULL;
bellard83f64092006-08-01 16:21:11 +0000410 return ret;
bellardea2384d2004-08-01 21:59:26 +0000411 }
bellardd15a7712006-08-06 13:35:09 +0000412 if (drv->bdrv_getlength) {
413 bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
414 }
bellardea2384d2004-08-01 21:59:26 +0000415#ifndef _WIN32
416 if (bs->is_temporary) {
417 unlink(filename);
418 }
419#endif
bellard83f64092006-08-01 16:21:11 +0000420 if (bs->backing_file[0] != '\0') {
bellardea2384d2004-08-01 21:59:26 +0000421 /* if there is a backing file, use it */
422 bs->backing_hd = bdrv_new("");
423 if (!bs->backing_hd) {
424 fail:
425 bdrv_close(bs);
bellard6b21b972006-08-23 21:14:37 +0000426 return -ENOMEM;
bellardea2384d2004-08-01 21:59:26 +0000427 }
bellard83f64092006-08-01 16:21:11 +0000428 path_combine(backing_filename, sizeof(backing_filename),
429 filename, bs->backing_file);
430 if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
bellardea2384d2004-08-01 21:59:26 +0000431 goto fail;
432 }
433
bellardb3380822004-03-14 21:38:54 +0000434 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000435 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000436 if (bs->change_cb)
437 bs->change_cb(bs->change_opaque);
438
439 return 0;
bellardfc01f7e2003-06-30 10:03:06 +0000440}
441
442void bdrv_close(BlockDriverState *bs)
443{
bellard19cb3732006-08-19 11:45:59 +0000444 if (bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000445 if (bs->backing_hd)
446 bdrv_delete(bs->backing_hd);
447 bs->drv->bdrv_close(bs);
448 qemu_free(bs->opaque);
449#ifdef _WIN32
450 if (bs->is_temporary) {
451 unlink(bs->filename);
452 }
bellard67b915a2004-03-31 23:37:16 +0000453#endif
bellardea2384d2004-08-01 21:59:26 +0000454 bs->opaque = NULL;
455 bs->drv = NULL;
bellardb3380822004-03-14 21:38:54 +0000456
457 /* call the change callback */
bellard19cb3732006-08-19 11:45:59 +0000458 bs->media_changed = 1;
bellardb3380822004-03-14 21:38:54 +0000459 if (bs->change_cb)
460 bs->change_cb(bs->change_opaque);
461 }
462}
463
464void bdrv_delete(BlockDriverState *bs)
465{
aurel3234c6f052008-04-08 19:51:21 +0000466 BlockDriverState **pbs;
467
468 pbs = &bdrv_first;
469 while (*pbs != bs && *pbs != NULL)
470 pbs = &(*pbs)->next;
471 if (*pbs == bs)
472 *pbs = bs->next;
473
bellardb3380822004-03-14 21:38:54 +0000474 bdrv_close(bs);
475 qemu_free(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000476}
477
bellard33e39632003-07-06 17:15:21 +0000478/* commit COW file into the raw image */
479int bdrv_commit(BlockDriverState *bs)
480{
bellard19cb3732006-08-19 11:45:59 +0000481 BlockDriver *drv = bs->drv;
bellard83f64092006-08-01 16:21:11 +0000482 int64_t i, total_sectors;
bellardea2384d2004-08-01 21:59:26 +0000483 int n, j;
484 unsigned char sector[512];
bellard33e39632003-07-06 17:15:21 +0000485
bellard19cb3732006-08-19 11:45:59 +0000486 if (!drv)
487 return -ENOMEDIUM;
bellard33e39632003-07-06 17:15:21 +0000488
489 if (bs->read_only) {
bellardea2384d2004-08-01 21:59:26 +0000490 return -EACCES;
bellard33e39632003-07-06 17:15:21 +0000491 }
492
bellardea2384d2004-08-01 21:59:26 +0000493 if (!bs->backing_hd) {
494 return -ENOTSUP;
bellard33e39632003-07-06 17:15:21 +0000495 }
bellardea2384d2004-08-01 21:59:26 +0000496
bellard83f64092006-08-01 16:21:11 +0000497 total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
498 for (i = 0; i < total_sectors;) {
bellard19cb3732006-08-19 11:45:59 +0000499 if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
bellardea2384d2004-08-01 21:59:26 +0000500 for(j = 0; j < n; j++) {
501 if (bdrv_read(bs, i, sector, 1) != 0) {
502 return -EIO;
503 }
504
505 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
506 return -EIO;
507 }
508 i++;
509 }
510 } else {
511 i += n;
512 }
513 }
bellard95389c82005-12-18 18:28:15 +0000514
bellard19cb3732006-08-19 11:45:59 +0000515 if (drv->bdrv_make_empty)
516 return drv->bdrv_make_empty(bs);
bellard95389c82005-12-18 18:28:15 +0000517
bellard33e39632003-07-06 17:15:21 +0000518 return 0;
519}
520
bellard19cb3732006-08-19 11:45:59 +0000521/* return < 0 if error. See bdrv_write() for the return codes */
ths5fafdf22007-09-16 21:08:06 +0000522int bdrv_read(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000523 uint8_t *buf, int nb_sectors)
524{
bellardea2384d2004-08-01 21:59:26 +0000525 BlockDriver *drv = bs->drv;
526
bellard19cb3732006-08-19 11:45:59 +0000527 if (!drv)
528 return -ENOMEDIUM;
bellardb3380822004-03-14 21:38:54 +0000529
bellard83f64092006-08-01 16:21:11 +0000530 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
bellardcf989512004-02-16 21:56:36 +0000531 memcpy(buf, bs->boot_sector_data, 512);
bellard83f64092006-08-01 16:21:11 +0000532 sector_num++;
533 nb_sectors--;
534 buf += 512;
535 if (nb_sectors == 0)
536 return 0;
bellard33e39632003-07-06 17:15:21 +0000537 }
bellard83f64092006-08-01 16:21:11 +0000538 if (drv->bdrv_pread) {
539 int ret, len;
540 len = nb_sectors * 512;
541 ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
542 if (ret < 0)
543 return ret;
544 else if (ret != len)
bellard19cb3732006-08-19 11:45:59 +0000545 return -EINVAL;
thsa36e69d2007-12-02 05:18:19 +0000546 else {
547 bs->rd_bytes += (unsigned) len;
548 bs->rd_ops ++;
bellard83f64092006-08-01 16:21:11 +0000549 return 0;
thsa36e69d2007-12-02 05:18:19 +0000550 }
bellard83f64092006-08-01 16:21:11 +0000551 } else {
552 return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
553 }
bellardfc01f7e2003-06-30 10:03:06 +0000554}
555
ths5fafdf22007-09-16 21:08:06 +0000556/* Return < 0 if error. Important errors are:
bellard19cb3732006-08-19 11:45:59 +0000557 -EIO generic I/O error (may happen for all errors)
558 -ENOMEDIUM No media inserted.
559 -EINVAL Invalid sector number or nb_sectors
560 -EACCES Trying to write a read-only device
561*/
ths5fafdf22007-09-16 21:08:06 +0000562int bdrv_write(BlockDriverState *bs, int64_t sector_num,
bellardfc01f7e2003-06-30 10:03:06 +0000563 const uint8_t *buf, int nb_sectors)
564{
bellard83f64092006-08-01 16:21:11 +0000565 BlockDriver *drv = bs->drv;
bellard19cb3732006-08-19 11:45:59 +0000566 if (!bs->drv)
567 return -ENOMEDIUM;
bellard0849bf02003-06-30 23:17:31 +0000568 if (bs->read_only)
bellard19cb3732006-08-19 11:45:59 +0000569 return -EACCES;
bellard79639d42005-11-26 10:58:41 +0000570 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
ths3b46e622007-09-17 08:09:54 +0000571 memcpy(bs->boot_sector_data, buf, 512);
bellard79639d42005-11-26 10:58:41 +0000572 }
bellard83f64092006-08-01 16:21:11 +0000573 if (drv->bdrv_pwrite) {
574 int ret, len;
575 len = nb_sectors * 512;
576 ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
577 if (ret < 0)
578 return ret;
579 else if (ret != len)
580 return -EIO;
thsa36e69d2007-12-02 05:18:19 +0000581 else {
582 bs->wr_bytes += (unsigned) len;
583 bs->wr_ops ++;
bellard83f64092006-08-01 16:21:11 +0000584 return 0;
thsa36e69d2007-12-02 05:18:19 +0000585 }
bellard83f64092006-08-01 16:21:11 +0000586 } else {
587 return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
588 }
589}
590
ths5fafdf22007-09-16 21:08:06 +0000591static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000592 uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000593{
bellard83f64092006-08-01 16:21:11 +0000594 uint8_t tmp_buf[SECTOR_SIZE];
595 int len, nb_sectors, count;
596 int64_t sector_num;
597
598 count = count1;
599 /* first read to align to sector start */
600 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
601 if (len > count)
602 len = count;
603 sector_num = offset >> SECTOR_BITS;
604 if (len > 0) {
605 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
606 return -EIO;
607 memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
608 count -= len;
609 if (count == 0)
610 return count1;
611 sector_num++;
612 buf += len;
613 }
614
615 /* read the sectors "in place" */
616 nb_sectors = count >> SECTOR_BITS;
617 if (nb_sectors > 0) {
618 if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
619 return -EIO;
620 sector_num += nb_sectors;
621 len = nb_sectors << SECTOR_BITS;
622 buf += len;
623 count -= len;
624 }
625
626 /* add data from the last sector */
627 if (count > 0) {
628 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
629 return -EIO;
630 memcpy(buf, tmp_buf, count);
631 }
632 return count1;
633}
634
ths5fafdf22007-09-16 21:08:06 +0000635static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
bellardfaea38e2006-08-05 21:31:00 +0000636 const uint8_t *buf, int count1)
bellard83f64092006-08-01 16:21:11 +0000637{
bellard83f64092006-08-01 16:21:11 +0000638 uint8_t tmp_buf[SECTOR_SIZE];
639 int len, nb_sectors, count;
640 int64_t sector_num;
641
642 count = count1;
643 /* first write to align to sector start */
644 len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
645 if (len > count)
646 len = count;
647 sector_num = offset >> SECTOR_BITS;
648 if (len > 0) {
649 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
650 return -EIO;
651 memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
652 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
653 return -EIO;
654 count -= len;
655 if (count == 0)
656 return count1;
657 sector_num++;
658 buf += len;
659 }
660
661 /* write the sectors "in place" */
662 nb_sectors = count >> SECTOR_BITS;
663 if (nb_sectors > 0) {
664 if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
665 return -EIO;
666 sector_num += nb_sectors;
667 len = nb_sectors << SECTOR_BITS;
668 buf += len;
669 count -= len;
670 }
671
672 /* add data from the last sector */
673 if (count > 0) {
674 if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
675 return -EIO;
676 memcpy(tmp_buf, buf, count);
677 if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
678 return -EIO;
679 }
680 return count1;
681}
bellard83f64092006-08-01 16:21:11 +0000682
683/**
ths5fafdf22007-09-16 21:08:06 +0000684 * Read with byte offsets (needed only for file protocols)
bellard83f64092006-08-01 16:21:11 +0000685 */
ths5fafdf22007-09-16 21:08:06 +0000686int bdrv_pread(BlockDriverState *bs, int64_t offset,
bellard83f64092006-08-01 16:21:11 +0000687 void *buf1, int count1)
688{
689 BlockDriver *drv = bs->drv;
690
691 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000692 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000693 if (!drv->bdrv_pread)
bellardfaea38e2006-08-05 21:31:00 +0000694 return bdrv_pread_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000695 return drv->bdrv_pread(bs, offset, buf1, count1);
696}
697
ths5fafdf22007-09-16 21:08:06 +0000698/**
699 * Write with byte offsets (needed only for file protocols)
bellard83f64092006-08-01 16:21:11 +0000700 */
ths5fafdf22007-09-16 21:08:06 +0000701int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
bellard83f64092006-08-01 16:21:11 +0000702 const void *buf1, int count1)
703{
704 BlockDriver *drv = bs->drv;
705
706 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000707 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000708 if (!drv->bdrv_pwrite)
bellardfaea38e2006-08-05 21:31:00 +0000709 return bdrv_pwrite_em(bs, offset, buf1, count1);
bellard83f64092006-08-01 16:21:11 +0000710 return drv->bdrv_pwrite(bs, offset, buf1, count1);
711}
712
713/**
714 * Truncate file to 'offset' bytes (needed only for file protocols)
715 */
716int bdrv_truncate(BlockDriverState *bs, int64_t offset)
717{
718 BlockDriver *drv = bs->drv;
719 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000720 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000721 if (!drv->bdrv_truncate)
722 return -ENOTSUP;
723 return drv->bdrv_truncate(bs, offset);
724}
725
726/**
727 * Length of a file in bytes. Return < 0 if error or unknown.
728 */
729int64_t bdrv_getlength(BlockDriverState *bs)
730{
731 BlockDriver *drv = bs->drv;
732 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000733 return -ENOMEDIUM;
bellard83f64092006-08-01 16:21:11 +0000734 if (!drv->bdrv_getlength) {
735 /* legacy mode */
736 return bs->total_sectors * SECTOR_SIZE;
737 }
738 return drv->bdrv_getlength(bs);
bellardfc01f7e2003-06-30 10:03:06 +0000739}
740
bellard19cb3732006-08-19 11:45:59 +0000741/* return 0 as number of sectors if no device present or error */
ths96b8f132007-12-17 01:35:20 +0000742void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
bellardfc01f7e2003-06-30 10:03:06 +0000743{
bellard19cb3732006-08-19 11:45:59 +0000744 int64_t length;
745 length = bdrv_getlength(bs);
746 if (length < 0)
747 length = 0;
748 else
749 length = length >> SECTOR_BITS;
750 *nb_sectors_ptr = length;
bellardfc01f7e2003-06-30 10:03:06 +0000751}
bellardcf989512004-02-16 21:56:36 +0000752
753/* force a given boot sector. */
754void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
755{
756 bs->boot_sector_enabled = 1;
757 if (size > 512)
758 size = 512;
759 memcpy(bs->boot_sector_data, data, size);
760 memset(bs->boot_sector_data + size, 0, 512 - size);
761}
bellardb3380822004-03-14 21:38:54 +0000762
ths5fafdf22007-09-16 21:08:06 +0000763void bdrv_set_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000764 int cyls, int heads, int secs)
765{
766 bs->cyls = cyls;
767 bs->heads = heads;
768 bs->secs = secs;
769}
770
771void bdrv_set_type_hint(BlockDriverState *bs, int type)
772{
773 bs->type = type;
774 bs->removable = ((type == BDRV_TYPE_CDROM ||
775 type == BDRV_TYPE_FLOPPY));
776}
777
bellard46d47672004-11-16 01:45:27 +0000778void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
779{
780 bs->translation = translation;
781}
782
ths5fafdf22007-09-16 21:08:06 +0000783void bdrv_get_geometry_hint(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000784 int *pcyls, int *pheads, int *psecs)
785{
786 *pcyls = bs->cyls;
787 *pheads = bs->heads;
788 *psecs = bs->secs;
789}
790
791int bdrv_get_type_hint(BlockDriverState *bs)
792{
793 return bs->type;
794}
795
bellard46d47672004-11-16 01:45:27 +0000796int bdrv_get_translation_hint(BlockDriverState *bs)
797{
798 return bs->translation;
799}
800
bellardb3380822004-03-14 21:38:54 +0000801int bdrv_is_removable(BlockDriverState *bs)
802{
803 return bs->removable;
804}
805
806int bdrv_is_read_only(BlockDriverState *bs)
807{
808 return bs->read_only;
809}
810
ths985a03b2007-12-24 16:10:43 +0000811int bdrv_is_sg(BlockDriverState *bs)
812{
813 return bs->sg;
814}
815
bellard19cb3732006-08-19 11:45:59 +0000816/* XXX: no longer used */
ths5fafdf22007-09-16 21:08:06 +0000817void bdrv_set_change_cb(BlockDriverState *bs,
bellardb3380822004-03-14 21:38:54 +0000818 void (*change_cb)(void *opaque), void *opaque)
819{
820 bs->change_cb = change_cb;
821 bs->change_opaque = opaque;
822}
823
bellardea2384d2004-08-01 21:59:26 +0000824int bdrv_is_encrypted(BlockDriverState *bs)
825{
826 if (bs->backing_hd && bs->backing_hd->encrypted)
827 return 1;
828 return bs->encrypted;
829}
830
831int bdrv_set_key(BlockDriverState *bs, const char *key)
832{
833 int ret;
834 if (bs->backing_hd && bs->backing_hd->encrypted) {
835 ret = bdrv_set_key(bs->backing_hd, key);
836 if (ret < 0)
837 return ret;
838 if (!bs->encrypted)
839 return 0;
840 }
841 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
842 return -1;
843 return bs->drv->bdrv_set_key(bs, key);
844}
845
846void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
847{
bellard19cb3732006-08-19 11:45:59 +0000848 if (!bs->drv) {
bellardea2384d2004-08-01 21:59:26 +0000849 buf[0] = '\0';
850 } else {
851 pstrcpy(buf, buf_size, bs->drv->format_name);
852 }
853}
854
ths5fafdf22007-09-16 21:08:06 +0000855void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
bellardea2384d2004-08-01 21:59:26 +0000856 void *opaque)
857{
858 BlockDriver *drv;
859
860 for (drv = first_drv; drv != NULL; drv = drv->next) {
861 it(opaque, drv->format_name);
862 }
863}
864
bellardb3380822004-03-14 21:38:54 +0000865BlockDriverState *bdrv_find(const char *name)
866{
867 BlockDriverState *bs;
868
869 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
870 if (!strcmp(name, bs->device_name))
871 return bs;
872 }
873 return NULL;
874}
875
bellard81d09122004-07-14 17:21:37 +0000876void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
877{
878 BlockDriverState *bs;
879
880 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
881 it(opaque, bs->device_name);
882 }
883}
884
bellardea2384d2004-08-01 21:59:26 +0000885const char *bdrv_get_device_name(BlockDriverState *bs)
886{
887 return bs->device_name;
888}
889
pbrook7a6cba62006-06-04 11:39:07 +0000890void bdrv_flush(BlockDriverState *bs)
891{
892 if (bs->drv->bdrv_flush)
893 bs->drv->bdrv_flush(bs);
894 if (bs->backing_hd)
895 bdrv_flush(bs->backing_hd);
896}
897
thsf58c7b32008-06-05 21:53:49 +0000898/*
899 * Returns true iff the specified sector is present in the disk image. Drivers
900 * not implementing the functionality are assumed to not support backing files,
901 * hence all their sectors are reported as allocated.
902 *
903 * 'pnum' is set to the number of sectors (including and immediately following
904 * the specified sector) that are known to be in the same
905 * allocated/unallocated state.
906 *
907 * 'nb_sectors' is the max value 'pnum' should be set to.
908 */
909int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
910 int *pnum)
911{
912 int64_t n;
913 if (!bs->drv->bdrv_is_allocated) {
914 if (sector_num >= bs->total_sectors) {
915 *pnum = 0;
916 return 0;
917 }
918 n = bs->total_sectors - sector_num;
919 *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
920 return 1;
921 }
922 return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
923}
924
bellardb3380822004-03-14 21:38:54 +0000925void bdrv_info(void)
926{
927 BlockDriverState *bs;
928
929 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
930 term_printf("%s:", bs->device_name);
931 term_printf(" type=");
932 switch(bs->type) {
933 case BDRV_TYPE_HD:
934 term_printf("hd");
935 break;
936 case BDRV_TYPE_CDROM:
937 term_printf("cdrom");
938 break;
939 case BDRV_TYPE_FLOPPY:
940 term_printf("floppy");
941 break;
942 }
943 term_printf(" removable=%d", bs->removable);
944 if (bs->removable) {
945 term_printf(" locked=%d", bs->locked);
946 }
bellard19cb3732006-08-19 11:45:59 +0000947 if (bs->drv) {
thsfef30742006-12-22 14:11:32 +0000948 term_printf(" file=");
949 term_print_filename(bs->filename);
950 if (bs->backing_file[0] != '\0') {
951 term_printf(" backing_file=");
952 term_print_filename(bs->backing_file);
953 }
bellardb3380822004-03-14 21:38:54 +0000954 term_printf(" ro=%d", bs->read_only);
bellardea2384d2004-08-01 21:59:26 +0000955 term_printf(" drv=%s", bs->drv->format_name);
956 if (bs->encrypted)
957 term_printf(" encrypted");
bellardb3380822004-03-14 21:38:54 +0000958 } else {
959 term_printf(" [not inserted]");
960 }
961 term_printf("\n");
962 }
963}
thsa36e69d2007-12-02 05:18:19 +0000964
965/* The "info blockstats" command. */
966void bdrv_info_stats (void)
967{
968 BlockDriverState *bs;
969
970 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
971 term_printf ("%s:"
972 " rd_bytes=%" PRIu64
973 " wr_bytes=%" PRIu64
974 " rd_operations=%" PRIu64
975 " wr_operations=%" PRIu64
976 "\n",
977 bs->device_name,
978 bs->rd_bytes, bs->wr_bytes,
979 bs->rd_ops, bs->wr_ops);
980 }
981}
bellardea2384d2004-08-01 21:59:26 +0000982
ths5fafdf22007-09-16 21:08:06 +0000983void bdrv_get_backing_filename(BlockDriverState *bs,
bellard83f64092006-08-01 16:21:11 +0000984 char *filename, int filename_size)
bellardea2384d2004-08-01 21:59:26 +0000985{
bellard83f64092006-08-01 16:21:11 +0000986 if (!bs->backing_hd) {
987 pstrcpy(filename, filename_size, "");
988 } else {
989 pstrcpy(filename, filename_size, bs->backing_file);
990 }
bellardea2384d2004-08-01 21:59:26 +0000991}
992
ths5fafdf22007-09-16 21:08:06 +0000993int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
bellardfaea38e2006-08-05 21:31:00 +0000994 const uint8_t *buf, int nb_sectors)
995{
996 BlockDriver *drv = bs->drv;
997 if (!drv)
bellard19cb3732006-08-19 11:45:59 +0000998 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +0000999 if (!drv->bdrv_write_compressed)
1000 return -ENOTSUP;
1001 return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1002}
ths3b46e622007-09-17 08:09:54 +00001003
bellardfaea38e2006-08-05 21:31:00 +00001004int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1005{
1006 BlockDriver *drv = bs->drv;
1007 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001008 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001009 if (!drv->bdrv_get_info)
1010 return -ENOTSUP;
1011 memset(bdi, 0, sizeof(*bdi));
1012 return drv->bdrv_get_info(bs, bdi);
1013}
1014
1015/**************************************************************/
1016/* handling of snapshots */
1017
ths5fafdf22007-09-16 21:08:06 +00001018int bdrv_snapshot_create(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001019 QEMUSnapshotInfo *sn_info)
1020{
1021 BlockDriver *drv = bs->drv;
1022 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001023 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001024 if (!drv->bdrv_snapshot_create)
1025 return -ENOTSUP;
1026 return drv->bdrv_snapshot_create(bs, sn_info);
1027}
1028
ths5fafdf22007-09-16 21:08:06 +00001029int bdrv_snapshot_goto(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001030 const char *snapshot_id)
1031{
1032 BlockDriver *drv = bs->drv;
1033 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001034 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001035 if (!drv->bdrv_snapshot_goto)
1036 return -ENOTSUP;
1037 return drv->bdrv_snapshot_goto(bs, snapshot_id);
1038}
1039
1040int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1041{
1042 BlockDriver *drv = bs->drv;
1043 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001044 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001045 if (!drv->bdrv_snapshot_delete)
1046 return -ENOTSUP;
1047 return drv->bdrv_snapshot_delete(bs, snapshot_id);
1048}
1049
ths5fafdf22007-09-16 21:08:06 +00001050int bdrv_snapshot_list(BlockDriverState *bs,
bellardfaea38e2006-08-05 21:31:00 +00001051 QEMUSnapshotInfo **psn_info)
1052{
1053 BlockDriver *drv = bs->drv;
1054 if (!drv)
bellard19cb3732006-08-19 11:45:59 +00001055 return -ENOMEDIUM;
bellardfaea38e2006-08-05 21:31:00 +00001056 if (!drv->bdrv_snapshot_list)
1057 return -ENOTSUP;
1058 return drv->bdrv_snapshot_list(bs, psn_info);
1059}
1060
1061#define NB_SUFFIXES 4
1062
1063char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1064{
1065 static const char suffixes[NB_SUFFIXES] = "KMGT";
1066 int64_t base;
1067 int i;
1068
1069 if (size <= 999) {
1070 snprintf(buf, buf_size, "%" PRId64, size);
1071 } else {
1072 base = 1024;
1073 for(i = 0; i < NB_SUFFIXES; i++) {
1074 if (size < (10 * base)) {
ths5fafdf22007-09-16 21:08:06 +00001075 snprintf(buf, buf_size, "%0.1f%c",
bellardfaea38e2006-08-05 21:31:00 +00001076 (double)size / base,
1077 suffixes[i]);
1078 break;
1079 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
ths5fafdf22007-09-16 21:08:06 +00001080 snprintf(buf, buf_size, "%" PRId64 "%c",
bellardfaea38e2006-08-05 21:31:00 +00001081 ((size + (base >> 1)) / base),
1082 suffixes[i]);
1083 break;
1084 }
1085 base = base * 1024;
1086 }
1087 }
1088 return buf;
1089}
1090
1091char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1092{
1093 char buf1[128], date_buf[128], clock_buf[128];
bellard3b9f94e2007-01-07 17:27:07 +00001094#ifdef _WIN32
1095 struct tm *ptm;
1096#else
bellardfaea38e2006-08-05 21:31:00 +00001097 struct tm tm;
bellard3b9f94e2007-01-07 17:27:07 +00001098#endif
bellardfaea38e2006-08-05 21:31:00 +00001099 time_t ti;
1100 int64_t secs;
1101
1102 if (!sn) {
ths5fafdf22007-09-16 21:08:06 +00001103 snprintf(buf, buf_size,
1104 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001105 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1106 } else {
1107 ti = sn->date_sec;
bellard3b9f94e2007-01-07 17:27:07 +00001108#ifdef _WIN32
1109 ptm = localtime(&ti);
1110 strftime(date_buf, sizeof(date_buf),
1111 "%Y-%m-%d %H:%M:%S", ptm);
1112#else
bellardfaea38e2006-08-05 21:31:00 +00001113 localtime_r(&ti, &tm);
1114 strftime(date_buf, sizeof(date_buf),
1115 "%Y-%m-%d %H:%M:%S", &tm);
bellard3b9f94e2007-01-07 17:27:07 +00001116#endif
bellardfaea38e2006-08-05 21:31:00 +00001117 secs = sn->vm_clock_nsec / 1000000000;
1118 snprintf(clock_buf, sizeof(clock_buf),
1119 "%02d:%02d:%02d.%03d",
1120 (int)(secs / 3600),
1121 (int)((secs / 60) % 60),
ths5fafdf22007-09-16 21:08:06 +00001122 (int)(secs % 60),
bellardfaea38e2006-08-05 21:31:00 +00001123 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1124 snprintf(buf, buf_size,
ths5fafdf22007-09-16 21:08:06 +00001125 "%-10s%-20s%7s%20s%15s",
bellardfaea38e2006-08-05 21:31:00 +00001126 sn->id_str, sn->name,
1127 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1128 date_buf,
1129 clock_buf);
1130 }
1131 return buf;
1132}
1133
bellardea2384d2004-08-01 21:59:26 +00001134
bellard83f64092006-08-01 16:21:11 +00001135/**************************************************************/
1136/* async I/Os */
1137
pbrookce1a14d2006-08-07 02:38:06 +00001138BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1139 uint8_t *buf, int nb_sectors,
1140 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001141{
1142 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001143 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001144
bellard19cb3732006-08-19 11:45:59 +00001145 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001146 return NULL;
ths3b46e622007-09-17 08:09:54 +00001147
bellard83f64092006-08-01 16:21:11 +00001148 /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1149 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1150 memcpy(buf, bs->boot_sector_data, 512);
1151 sector_num++;
1152 nb_sectors--;
1153 buf += 512;
1154 }
1155
thsa36e69d2007-12-02 05:18:19 +00001156 ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1157
1158 if (ret) {
1159 /* Update stats even though technically transfer has not happened. */
1160 bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1161 bs->rd_ops ++;
1162 }
1163
1164 return ret;
bellard83f64092006-08-01 16:21:11 +00001165}
1166
pbrookce1a14d2006-08-07 02:38:06 +00001167BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1168 const uint8_t *buf, int nb_sectors,
1169 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001170{
bellard83f64092006-08-01 16:21:11 +00001171 BlockDriver *drv = bs->drv;
thsa36e69d2007-12-02 05:18:19 +00001172 BlockDriverAIOCB *ret;
bellard83f64092006-08-01 16:21:11 +00001173
bellard19cb3732006-08-19 11:45:59 +00001174 if (!drv)
pbrookce1a14d2006-08-07 02:38:06 +00001175 return NULL;
bellard83f64092006-08-01 16:21:11 +00001176 if (bs->read_only)
pbrookce1a14d2006-08-07 02:38:06 +00001177 return NULL;
bellard83f64092006-08-01 16:21:11 +00001178 if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
ths3b46e622007-09-17 08:09:54 +00001179 memcpy(bs->boot_sector_data, buf, 512);
bellardea2384d2004-08-01 21:59:26 +00001180 }
bellard83f64092006-08-01 16:21:11 +00001181
thsa36e69d2007-12-02 05:18:19 +00001182 ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1183
1184 if (ret) {
1185 /* Update stats even though technically transfer has not happened. */
1186 bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1187 bs->wr_ops ++;
1188 }
1189
1190 return ret;
bellard83f64092006-08-01 16:21:11 +00001191}
1192
1193void bdrv_aio_cancel(BlockDriverAIOCB *acb)
pbrookce1a14d2006-08-07 02:38:06 +00001194{
1195 BlockDriver *drv = acb->bs->drv;
bellard83f64092006-08-01 16:21:11 +00001196
1197 drv->bdrv_aio_cancel(acb);
bellard83f64092006-08-01 16:21:11 +00001198}
1199
pbrookce1a14d2006-08-07 02:38:06 +00001200
bellard83f64092006-08-01 16:21:11 +00001201/**************************************************************/
1202/* async block device emulation */
1203
bellard83f64092006-08-01 16:21:11 +00001204static void bdrv_aio_bh_cb(void *opaque)
bellardbeac80c2006-06-26 20:08:57 +00001205{
pbrookce1a14d2006-08-07 02:38:06 +00001206 BlockDriverAIOCBSync *acb = opaque;
1207 acb->common.cb(acb->common.opaque, acb->ret);
1208 qemu_aio_release(acb);
bellardbeac80c2006-06-26 20:08:57 +00001209}
bellardbeac80c2006-06-26 20:08:57 +00001210
pbrookce1a14d2006-08-07 02:38:06 +00001211static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1212 int64_t sector_num, uint8_t *buf, int nb_sectors,
1213 BlockDriverCompletionFunc *cb, void *opaque)
bellardea2384d2004-08-01 21:59:26 +00001214{
pbrookce1a14d2006-08-07 02:38:06 +00001215 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001216 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001217
1218 acb = qemu_aio_get(bs, cb, opaque);
1219 if (!acb->bh)
1220 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1221 ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1222 acb->ret = ret;
1223 qemu_bh_schedule(acb->bh);
1224 return &acb->common;
pbrook7a6cba62006-06-04 11:39:07 +00001225}
1226
pbrookce1a14d2006-08-07 02:38:06 +00001227static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1228 int64_t sector_num, const uint8_t *buf, int nb_sectors,
1229 BlockDriverCompletionFunc *cb, void *opaque)
bellard83f64092006-08-01 16:21:11 +00001230{
pbrookce1a14d2006-08-07 02:38:06 +00001231 BlockDriverAIOCBSync *acb;
bellard83f64092006-08-01 16:21:11 +00001232 int ret;
pbrookce1a14d2006-08-07 02:38:06 +00001233
1234 acb = qemu_aio_get(bs, cb, opaque);
1235 if (!acb->bh)
1236 acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1237 ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1238 acb->ret = ret;
1239 qemu_bh_schedule(acb->bh);
1240 return &acb->common;
bellard83f64092006-08-01 16:21:11 +00001241}
1242
pbrookce1a14d2006-08-07 02:38:06 +00001243static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
bellard83f64092006-08-01 16:21:11 +00001244{
pbrookce1a14d2006-08-07 02:38:06 +00001245 BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1246 qemu_bh_cancel(acb->bh);
1247 qemu_aio_release(acb);
bellard83f64092006-08-01 16:21:11 +00001248}
bellard83f64092006-08-01 16:21:11 +00001249
1250/**************************************************************/
1251/* sync block device emulation */
1252
1253static void bdrv_rw_em_cb(void *opaque, int ret)
1254{
1255 *(int *)opaque = ret;
1256}
1257
1258#define NOT_DONE 0x7fffffff
1259
ths5fafdf22007-09-16 21:08:06 +00001260static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
bellard83f64092006-08-01 16:21:11 +00001261 uint8_t *buf, int nb_sectors)
1262{
pbrookce1a14d2006-08-07 02:38:06 +00001263 int async_ret;
1264 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001265
bellard83f64092006-08-01 16:21:11 +00001266 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001267 acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001268 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001269 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001270 return -1;
aliguoribaf35cb2008-09-10 15:45:19 +00001271
bellard83f64092006-08-01 16:21:11 +00001272 while (async_ret == NOT_DONE) {
1273 qemu_aio_wait();
1274 }
aliguoribaf35cb2008-09-10 15:45:19 +00001275
bellard83f64092006-08-01 16:21:11 +00001276 return async_ret;
1277}
1278
1279static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1280 const uint8_t *buf, int nb_sectors)
1281{
pbrookce1a14d2006-08-07 02:38:06 +00001282 int async_ret;
1283 BlockDriverAIOCB *acb;
bellard83f64092006-08-01 16:21:11 +00001284
bellard83f64092006-08-01 16:21:11 +00001285 async_ret = NOT_DONE;
ths5fafdf22007-09-16 21:08:06 +00001286 acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
bellard83f64092006-08-01 16:21:11 +00001287 bdrv_rw_em_cb, &async_ret);
aliguoribaf35cb2008-09-10 15:45:19 +00001288 if (acb == NULL)
pbrookce1a14d2006-08-07 02:38:06 +00001289 return -1;
bellard83f64092006-08-01 16:21:11 +00001290 while (async_ret == NOT_DONE) {
1291 qemu_aio_wait();
1292 }
bellard83f64092006-08-01 16:21:11 +00001293 return async_ret;
1294}
bellardea2384d2004-08-01 21:59:26 +00001295
1296void bdrv_init(void)
1297{
1298 bdrv_register(&bdrv_raw);
bellard19cb3732006-08-19 11:45:59 +00001299 bdrv_register(&bdrv_host_device);
bellardea2384d2004-08-01 21:59:26 +00001300#ifndef _WIN32
1301 bdrv_register(&bdrv_cow);
1302#endif
1303 bdrv_register(&bdrv_qcow);
1304 bdrv_register(&bdrv_vmdk);
bellard3c565212004-09-29 21:29:14 +00001305 bdrv_register(&bdrv_cloop);
bellard585d0ed2004-12-12 11:24:44 +00001306 bdrv_register(&bdrv_dmg);
bellarda8753c32005-04-26 21:34:00 +00001307 bdrv_register(&bdrv_bochs);
bellard6a0f9e82005-04-27 20:17:58 +00001308 bdrv_register(&bdrv_vpc);
bellard712e7872005-04-28 21:09:32 +00001309 bdrv_register(&bdrv_vvfat);
bellardfaea38e2006-08-05 21:31:00 +00001310 bdrv_register(&bdrv_qcow2);
ths6ada7452007-07-31 23:28:53 +00001311 bdrv_register(&bdrv_parallels);
ths75818252008-07-03 13:41:03 +00001312 bdrv_register(&bdrv_nbd);
aliguoria3392f92008-09-11 18:00:19 +00001313
1314 qemu_aio_init();
bellardea2384d2004-08-01 21:59:26 +00001315}
pbrookce1a14d2006-08-07 02:38:06 +00001316
1317void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1318 void *opaque)
1319{
1320 BlockDriver *drv;
1321 BlockDriverAIOCB *acb;
1322
1323 drv = bs->drv;
1324 if (drv->free_aiocb) {
1325 acb = drv->free_aiocb;
1326 drv->free_aiocb = acb->next;
1327 } else {
1328 acb = qemu_mallocz(drv->aiocb_size);
1329 if (!acb)
1330 return NULL;
1331 }
1332 acb->bs = bs;
1333 acb->cb = cb;
1334 acb->opaque = opaque;
1335 return acb;
1336}
1337
1338void qemu_aio_release(void *p)
1339{
1340 BlockDriverAIOCB *acb = p;
1341 BlockDriver *drv = acb->bs->drv;
1342 acb->next = drv->free_aiocb;
1343 drv->free_aiocb = acb;
1344}
bellard19cb3732006-08-19 11:45:59 +00001345
1346/**************************************************************/
1347/* removable device support */
1348
1349/**
1350 * Return TRUE if the media is present
1351 */
1352int bdrv_is_inserted(BlockDriverState *bs)
1353{
1354 BlockDriver *drv = bs->drv;
1355 int ret;
1356 if (!drv)
1357 return 0;
1358 if (!drv->bdrv_is_inserted)
1359 return 1;
1360 ret = drv->bdrv_is_inserted(bs);
1361 return ret;
1362}
1363
1364/**
1365 * Return TRUE if the media changed since the last call to this
ths5fafdf22007-09-16 21:08:06 +00001366 * function. It is currently only used for floppy disks
bellard19cb3732006-08-19 11:45:59 +00001367 */
1368int bdrv_media_changed(BlockDriverState *bs)
1369{
1370 BlockDriver *drv = bs->drv;
1371 int ret;
1372
1373 if (!drv || !drv->bdrv_media_changed)
1374 ret = -ENOTSUP;
1375 else
1376 ret = drv->bdrv_media_changed(bs);
1377 if (ret == -ENOTSUP)
1378 ret = bs->media_changed;
1379 bs->media_changed = 0;
1380 return ret;
1381}
1382
1383/**
1384 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1385 */
1386void bdrv_eject(BlockDriverState *bs, int eject_flag)
1387{
1388 BlockDriver *drv = bs->drv;
1389 int ret;
1390
1391 if (!drv || !drv->bdrv_eject) {
1392 ret = -ENOTSUP;
1393 } else {
1394 ret = drv->bdrv_eject(bs, eject_flag);
1395 }
1396 if (ret == -ENOTSUP) {
1397 if (eject_flag)
1398 bdrv_close(bs);
1399 }
1400}
1401
1402int bdrv_is_locked(BlockDriverState *bs)
1403{
1404 return bs->locked;
1405}
1406
1407/**
1408 * Lock or unlock the media (if it is locked, the user won't be able
1409 * to eject it manually).
1410 */
1411void bdrv_set_locked(BlockDriverState *bs, int locked)
1412{
1413 BlockDriver *drv = bs->drv;
1414
1415 bs->locked = locked;
1416 if (drv && drv->bdrv_set_locked) {
1417 drv->bdrv_set_locked(bs, locked);
1418 }
1419}
ths985a03b2007-12-24 16:10:43 +00001420
1421/* needed for generic scsi interface */
1422
1423int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1424{
1425 BlockDriver *drv = bs->drv;
1426
1427 if (drv && drv->bdrv_ioctl)
1428 return drv->bdrv_ioctl(bs, req, buf);
1429 return -ENOTSUP;
1430}