]> git.proxmox.com Git - qemu.git/blame - block.c
update
[qemu.git] / block.c
CommitLineData
fc01f7e7
FB
1/*
2 * QEMU System Emulator block driver
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
6 * 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 */
fc01f7e7 24#include "vl.h"
ea2384d3 25#include "block_int.h"
fc01f7e7 26
b338082b 27static BlockDriverState *bdrv_first;
ea2384d3
FB
28static BlockDriver *first_drv;
29
30void bdrv_register(BlockDriver *bdrv)
31{
32 bdrv->next = first_drv;
33 first_drv = bdrv;
34}
b338082b
FB
35
36/* create a new block device (by default it is empty) */
37BlockDriverState *bdrv_new(const char *device_name)
38{
39 BlockDriverState **pbs, *bs;
40
41 bs = qemu_mallocz(sizeof(BlockDriverState));
42 if(!bs)
43 return NULL;
44 pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
ea2384d3
FB
45 if (device_name[0] != '\0') {
46 /* insert at the end */
47 pbs = &bdrv_first;
48 while (*pbs != NULL)
49 pbs = &(*pbs)->next;
50 *pbs = bs;
51 }
b338082b
FB
52 return bs;
53}
54
ea2384d3
FB
55BlockDriver *bdrv_find_format(const char *format_name)
56{
57 BlockDriver *drv1;
58 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
59 if (!strcmp(drv1->format_name, format_name))
60 return drv1;
61 }
62 return NULL;
63}
64
65int bdrv_create(BlockDriver *drv,
66 const char *filename, int64_t size_in_sectors,
67 const char *backing_file, int flags)
68{
69 if (!drv->bdrv_create)
70 return -ENOTSUP;
71 return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
72}
73
74/* XXX: race condition possible */
75static void get_tmp_filename(char *filename, int size)
fc01f7e7 76{
67b915a5 77 int fd;
ea2384d3
FB
78 pstrcpy(filename, size, "/tmp/vl.XXXXXX");
79 fd = mkstemp(filename);
80 close(fd);
81}
fc01f7e7 82
ea2384d3
FB
83static BlockDriver *find_image_format(const char *filename)
84{
85 int fd, ret, score, score_max;
86 BlockDriver *drv1, *drv;
87 uint8_t buf[1024];
33e3963e 88
ea2384d3
FB
89 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
90 if (fd < 0)
91 return NULL;
92 ret = read(fd, buf, sizeof(buf));
93 if (ret < 0) {
94 close(fd);
95 return NULL;
96 }
97 close(fd);
98
99 drv = NULL;
100 score_max = 0;
101 for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
102 score = drv1->bdrv_probe(buf, ret, filename);
103 if (score > score_max) {
104 score_max = score;
105 drv = drv1;
0849bf08 106 }
fc01f7e7 107 }
ea2384d3
FB
108 return drv;
109}
110
111int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
112{
113 return bdrv_open2(bs, filename, snapshot, NULL);
114}
115
116int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot,
117 BlockDriver *drv)
118{
119 int ret;
120 char tmp_filename[1024];
121
122 bs->read_only = 0;
123 bs->is_temporary = 0;
124 bs->encrypted = 0;
125
126 if (snapshot) {
127 BlockDriverState *bs1;
128 int64_t total_size;
129
130 /* if snapshot, we create a temporary backing file and open it
131 instead of opening 'filename' directly */
33e3963e 132
ea2384d3
FB
133 /* if there is a backing file, use it */
134 bs1 = bdrv_new("");
135 if (!bs1) {
136 return -1;
137 }
138 if (bdrv_open(bs1, filename, 0) < 0) {
139 bdrv_delete(bs1);
140 return -1;
141 }
142 total_size = bs1->total_sectors;
143 bdrv_delete(bs1);
144
145 get_tmp_filename(tmp_filename, sizeof(tmp_filename));
146 /* XXX: use cow for linux as it is more efficient ? */
147 if (bdrv_create(&bdrv_qcow, tmp_filename,
148 total_size, filename, 0) < 0) {
149 return -1;
150 }
151 filename = tmp_filename;
152 bs->is_temporary = 1;
153 }
154
155 pstrcpy(bs->filename, sizeof(bs->filename), filename);
156 if (!drv) {
157 drv = find_image_format(filename);
158 if (!drv)
159 return -1;
160 }
161 bs->drv = drv;
162 bs->opaque = qemu_mallocz(drv->instance_size);
163 if (bs->opaque == NULL && drv->instance_size > 0)
164 return -1;
165
166 ret = drv->bdrv_open(bs, filename);
167 if (ret < 0) {
168 qemu_free(bs->opaque);
169 return -1;
33e3963e 170 }
67b915a5 171#ifndef _WIN32
ea2384d3
FB
172 if (bs->is_temporary) {
173 unlink(filename);
174 }
175#endif
176 if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) {
177 /* if there is a backing file, use it */
178 bs->backing_hd = bdrv_new("");
179 if (!bs->backing_hd) {
180 fail:
181 bdrv_close(bs);
182 return -1;
33e3963e 183 }
ea2384d3 184 if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
33e3963e 185 goto fail;
33e3963e
FB
186 }
187
b338082b
FB
188 bs->inserted = 1;
189
190 /* call the change callback */
191 if (bs->change_cb)
192 bs->change_cb(bs->change_opaque);
193
194 return 0;
fc01f7e7
FB
195}
196
197void bdrv_close(BlockDriverState *bs)
198{
b338082b 199 if (bs->inserted) {
ea2384d3
FB
200 if (bs->backing_hd)
201 bdrv_delete(bs->backing_hd);
202 bs->drv->bdrv_close(bs);
203 qemu_free(bs->opaque);
204#ifdef _WIN32
205 if (bs->is_temporary) {
206 unlink(bs->filename);
207 }
67b915a5 208#endif
ea2384d3
FB
209 bs->opaque = NULL;
210 bs->drv = NULL;
b338082b
FB
211 bs->inserted = 0;
212
213 /* call the change callback */
214 if (bs->change_cb)
215 bs->change_cb(bs->change_opaque);
216 }
217}
218
219void bdrv_delete(BlockDriverState *bs)
220{
ea2384d3 221 /* XXX: remove the driver list */
b338082b
FB
222 bdrv_close(bs);
223 qemu_free(bs);
fc01f7e7
FB
224}
225
33e3963e
FB
226/* commit COW file into the raw image */
227int bdrv_commit(BlockDriverState *bs)
228{
229 int64_t i;
ea2384d3
FB
230 int n, j;
231 unsigned char sector[512];
33e3963e 232
b338082b 233 if (!bs->inserted)
ea2384d3 234 return -ENOENT;
33e3963e
FB
235
236 if (bs->read_only) {
ea2384d3 237 return -EACCES;
33e3963e
FB
238 }
239
ea2384d3
FB
240 if (!bs->backing_hd) {
241 return -ENOTSUP;
242 }
33e3963e 243
ea2384d3
FB
244 for (i = 0; i < bs->total_sectors;) {
245 if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) {
246 for(j = 0; j < n; j++) {
247 if (bdrv_read(bs, i, sector, 1) != 0) {
248 return -EIO;
249 }
250
251 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
252 return -EIO;
253 }
254 i++;
33e3963e 255 }
ea2384d3
FB
256 } else {
257 i += n;
258 }
33e3963e 259 }
33e3963e
FB
260 return 0;
261}
262
fc01f7e7
FB
263/* return -1 if error */
264int bdrv_read(BlockDriverState *bs, int64_t sector_num,
265 uint8_t *buf, int nb_sectors)
266{
ea2384d3
FB
267 int ret, n;
268 BlockDriver *drv = bs->drv;
269
b338082b
FB
270 if (!bs->inserted)
271 return -1;
272
33e3963e 273 while (nb_sectors > 0) {
ea2384d3 274 if (sector_num == 0 && bs->boot_sector_enabled) {
cf98951b
FB
275 memcpy(buf, bs->boot_sector_data, 512);
276 n = 1;
ea2384d3
FB
277 } else if (bs->backing_hd) {
278 if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
279 ret = drv->bdrv_read(bs, sector_num, buf, n);
280 if (ret < 0)
281 return -1;
282 } else {
283 /* read from the base image */
284 ret = bdrv_read(bs->backing_hd, sector_num, buf, n);
285 if (ret < 0)
286 return -1;
287 }
33e3963e 288 } else {
ea2384d3
FB
289 ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
290 if (ret < 0)
33e3963e 291 return -1;
ea2384d3
FB
292 /* no need to loop */
293 break;
33e3963e
FB
294 }
295 nb_sectors -= n;
296 sector_num += n;
297 buf += n * 512;
298 }
299 return 0;
fc01f7e7
FB
300}
301
302/* return -1 if error */
303int bdrv_write(BlockDriverState *bs, int64_t sector_num,
304 const uint8_t *buf, int nb_sectors)
305{
b338082b
FB
306 if (!bs->inserted)
307 return -1;
0849bf08
FB
308 if (bs->read_only)
309 return -1;
ea2384d3 310 return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
fc01f7e7
FB
311}
312
313void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
314{
315 *nb_sectors_ptr = bs->total_sectors;
316}
cf98951b
FB
317
318/* force a given boot sector. */
319void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
320{
321 bs->boot_sector_enabled = 1;
322 if (size > 512)
323 size = 512;
324 memcpy(bs->boot_sector_data, data, size);
325 memset(bs->boot_sector_data + size, 0, 512 - size);
326}
b338082b
FB
327
328void bdrv_set_geometry_hint(BlockDriverState *bs,
329 int cyls, int heads, int secs)
330{
331 bs->cyls = cyls;
332 bs->heads = heads;
333 bs->secs = secs;
334}
335
336void bdrv_set_type_hint(BlockDriverState *bs, int type)
337{
338 bs->type = type;
339 bs->removable = ((type == BDRV_TYPE_CDROM ||
340 type == BDRV_TYPE_FLOPPY));
341}
342
343void bdrv_get_geometry_hint(BlockDriverState *bs,
344 int *pcyls, int *pheads, int *psecs)
345{
346 *pcyls = bs->cyls;
347 *pheads = bs->heads;
348 *psecs = bs->secs;
349}
350
351int bdrv_get_type_hint(BlockDriverState *bs)
352{
353 return bs->type;
354}
355
356int bdrv_is_removable(BlockDriverState *bs)
357{
358 return bs->removable;
359}
360
361int bdrv_is_read_only(BlockDriverState *bs)
362{
363 return bs->read_only;
364}
365
366int bdrv_is_inserted(BlockDriverState *bs)
367{
368 return bs->inserted;
369}
370
371int bdrv_is_locked(BlockDriverState *bs)
372{
373 return bs->locked;
374}
375
376void bdrv_set_locked(BlockDriverState *bs, int locked)
377{
378 bs->locked = locked;
379}
380
381void bdrv_set_change_cb(BlockDriverState *bs,
382 void (*change_cb)(void *opaque), void *opaque)
383{
384 bs->change_cb = change_cb;
385 bs->change_opaque = opaque;
386}
387
ea2384d3
FB
388int bdrv_is_encrypted(BlockDriverState *bs)
389{
390 if (bs->backing_hd && bs->backing_hd->encrypted)
391 return 1;
392 return bs->encrypted;
393}
394
395int bdrv_set_key(BlockDriverState *bs, const char *key)
396{
397 int ret;
398 if (bs->backing_hd && bs->backing_hd->encrypted) {
399 ret = bdrv_set_key(bs->backing_hd, key);
400 if (ret < 0)
401 return ret;
402 if (!bs->encrypted)
403 return 0;
404 }
405 if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
406 return -1;
407 return bs->drv->bdrv_set_key(bs, key);
408}
409
410void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
411{
412 if (!bs->inserted || !bs->drv) {
413 buf[0] = '\0';
414 } else {
415 pstrcpy(buf, buf_size, bs->drv->format_name);
416 }
417}
418
419void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
420 void *opaque)
421{
422 BlockDriver *drv;
423
424 for (drv = first_drv; drv != NULL; drv = drv->next) {
425 it(opaque, drv->format_name);
426 }
427}
428
b338082b
FB
429BlockDriverState *bdrv_find(const char *name)
430{
431 BlockDriverState *bs;
432
433 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
434 if (!strcmp(name, bs->device_name))
435 return bs;
436 }
437 return NULL;
438}
439
81d0912d
FB
440void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
441{
442 BlockDriverState *bs;
443
444 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
445 it(opaque, bs->device_name);
446 }
447}
448
ea2384d3
FB
449const char *bdrv_get_device_name(BlockDriverState *bs)
450{
451 return bs->device_name;
452}
453
b338082b
FB
454void bdrv_info(void)
455{
456 BlockDriverState *bs;
457
458 for (bs = bdrv_first; bs != NULL; bs = bs->next) {
459 term_printf("%s:", bs->device_name);
460 term_printf(" type=");
461 switch(bs->type) {
462 case BDRV_TYPE_HD:
463 term_printf("hd");
464 break;
465 case BDRV_TYPE_CDROM:
466 term_printf("cdrom");
467 break;
468 case BDRV_TYPE_FLOPPY:
469 term_printf("floppy");
470 break;
471 }
472 term_printf(" removable=%d", bs->removable);
473 if (bs->removable) {
474 term_printf(" locked=%d", bs->locked);
475 }
476 if (bs->inserted) {
477 term_printf(" file=%s", bs->filename);
ea2384d3
FB
478 if (bs->backing_file[0] != '\0')
479 term_printf(" backing_file=%s", bs->backing_file);
b338082b 480 term_printf(" ro=%d", bs->read_only);
ea2384d3
FB
481 term_printf(" drv=%s", bs->drv->format_name);
482 if (bs->encrypted)
483 term_printf(" encrypted");
b338082b
FB
484 } else {
485 term_printf(" [not inserted]");
486 }
487 term_printf("\n");
488 }
489}
ea2384d3
FB
490
491
492/**************************************************************/
493/* RAW block driver */
494
495typedef struct BDRVRawState {
496 int fd;
497} BDRVRawState;
498
499static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
500{
501 return 1; /* maybe */
502}
503
504static int raw_open(BlockDriverState *bs, const char *filename)
505{
506 BDRVRawState *s = bs->opaque;
507 int fd;
508 int64_t size;
509
510 fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE);
511 if (fd < 0) {
512 fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE);
513 if (fd < 0)
514 return -1;
515 bs->read_only = 1;
516 }
517 size = lseek64(fd, 0, SEEK_END);
518 bs->total_sectors = size / 512;
519 s->fd = fd;
520 return 0;
521}
522
523static int raw_read(BlockDriverState *bs, int64_t sector_num,
524 uint8_t *buf, int nb_sectors)
525{
526 BDRVRawState *s = bs->opaque;
527 int ret;
528
529 lseek64(s->fd, sector_num * 512, SEEK_SET);
530 ret = read(s->fd, buf, nb_sectors * 512);
531 if (ret != nb_sectors * 512)
532 return -1;
533 return 0;
534}
535
536static int raw_write(BlockDriverState *bs, int64_t sector_num,
537 const uint8_t *buf, int nb_sectors)
538{
539 BDRVRawState *s = bs->opaque;
540 int ret;
541
542 lseek64(s->fd, sector_num * 512, SEEK_SET);
543 ret = write(s->fd, buf, nb_sectors * 512);
544 if (ret != nb_sectors * 512)
545 return -1;
546 return 0;
547}
548
549static int raw_close(BlockDriverState *bs)
550{
551 BDRVRawState *s = bs->opaque;
552 close(s->fd);
553}
554
555static int raw_create(const char *filename, int64_t total_size,
556 const char *backing_file, int flags)
557{
558 int fd;
559
560 if (flags || backing_file)
561 return -ENOTSUP;
562
563 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
564 0644);
565 if (fd < 0)
566 return -EIO;
567 ftruncate64(fd, total_size * 512);
568 close(fd);
569 return 0;
570}
571
572BlockDriver bdrv_raw = {
573 "raw",
574 sizeof(BDRVRawState),
575 raw_probe,
576 raw_open,
577 raw_read,
578 raw_write,
579 raw_close,
580 raw_create,
581};
582
583void bdrv_init(void)
584{
585 bdrv_register(&bdrv_raw);
586#ifndef _WIN32
587 bdrv_register(&bdrv_cow);
588#endif
589 bdrv_register(&bdrv_qcow);
590 bdrv_register(&bdrv_vmdk);
591}