new disk image layer
[qemu] / block.c
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  */
24 #include "vl.h"
25 #include "block_int.h"
26
27 static BlockDriverState *bdrv_first;
28 static BlockDriver *first_drv;
29
30 void bdrv_register(BlockDriver *bdrv)
31 {
32     bdrv->next = first_drv;
33     first_drv = bdrv;
34 }
35
36 /* create a new block device (by default it is empty) */
37 BlockDriverState *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);
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     }
52     return bs;
53 }
54
55 BlockDriver *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
65 int 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 */
75 static void get_tmp_filename(char *filename, int size)
76 {
77     int fd;
78     pstrcpy(filename, size, "/tmp/vl.XXXXXX");
79     fd = mkstemp(filename);
80     close(fd);
81 }
82
83 static BlockDriver *find_image_format(const char *filename)
84 {
85     int fd, ret, score, score_max;
86     BlockDriver *drv1, *drv;
87     uint8_t buf[1024];
88
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;
106         }
107     }
108     return drv;
109 }
110
111 int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot)
112 {
113     return bdrv_open2(bs, filename, snapshot, NULL);
114 }
115
116 int 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 */
132
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;
170     }
171 #ifndef _WIN32
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;
183         }
184         if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0)
185             goto fail;
186     }
187
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;
195 }
196
197 void bdrv_close(BlockDriverState *bs)
198 {
199     if (bs->inserted) {
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         }
208 #endif
209         bs->opaque = NULL;
210         bs->drv = NULL;
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
219 void bdrv_delete(BlockDriverState *bs)
220 {
221     /* XXX: remove the driver list */
222     bdrv_close(bs);
223     qemu_free(bs);
224 }
225
226 /* commit COW file into the raw image */
227 int bdrv_commit(BlockDriverState *bs)
228 {
229     int64_t i;
230     int n, j;
231     unsigned char sector[512];
232
233     if (!bs->inserted)
234         return -ENOENT;
235
236     if (bs->read_only) {
237         return -EACCES;
238     }
239
240     if (!bs->backing_hd) {
241         return -ENOTSUP;
242     }
243
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++;
255             }
256         } else {
257             i += n;
258         }
259     }
260     return 0;
261 }
262
263 /* return -1 if error */
264 int bdrv_read(BlockDriverState *bs, int64_t sector_num, 
265               uint8_t *buf, int nb_sectors)
266 {
267     int ret, n;
268     BlockDriver *drv = bs->drv;
269
270     if (!bs->inserted)
271         return -1;
272
273     while (nb_sectors > 0) {
274         if (sector_num == 0 && bs->boot_sector_enabled) {
275             memcpy(buf, bs->boot_sector_data, 512);
276             n = 1;
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             }
288         } else {
289             ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors);
290             if (ret < 0)
291                 return -1;
292             /* no need to loop */
293             break;
294         }
295         nb_sectors -= n;
296         sector_num += n;
297         buf += n * 512;
298     }
299     return 0;
300 }
301
302 /* return -1 if error */
303 int bdrv_write(BlockDriverState *bs, int64_t sector_num, 
304                const uint8_t *buf, int nb_sectors)
305 {
306     if (!bs->inserted)
307         return -1;
308     if (bs->read_only)
309         return -1;
310     return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
311 }
312
313 void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
314 {
315     *nb_sectors_ptr = bs->total_sectors;
316 }
317
318 /* force a given boot sector. */
319 void 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 }
327
328 void 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
336 void 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
343 void 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
351 int bdrv_get_type_hint(BlockDriverState *bs)
352 {
353     return bs->type;
354 }
355
356 int bdrv_is_removable(BlockDriverState *bs)
357 {
358     return bs->removable;
359 }
360
361 int bdrv_is_read_only(BlockDriverState *bs)
362 {
363     return bs->read_only;
364 }
365
366 int bdrv_is_inserted(BlockDriverState *bs)
367 {
368     return bs->inserted;
369 }
370
371 int bdrv_is_locked(BlockDriverState *bs)
372 {
373     return bs->locked;
374 }
375
376 void bdrv_set_locked(BlockDriverState *bs, int locked)
377 {
378     bs->locked = locked;
379 }
380
381 void 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
388 int bdrv_is_encrypted(BlockDriverState *bs)
389 {
390     if (bs->backing_hd && bs->backing_hd->encrypted)
391         return 1;
392     return bs->encrypted;
393 }
394
395 int 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
410 void 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
419 void 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
429 BlockDriverState *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
440 void 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
449 const char *bdrv_get_device_name(BlockDriverState *bs)
450 {
451     return bs->device_name;
452 }
453
454 void 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);
478             if (bs->backing_file[0] != '\0')
479                 term_printf(" backing_file=%s", bs->backing_file);
480             term_printf(" ro=%d", bs->read_only);
481             term_printf(" drv=%s", bs->drv->format_name);
482             if (bs->encrypted)
483                 term_printf(" encrypted");
484         } else {
485             term_printf(" [not inserted]");
486         }
487         term_printf("\n");
488     }
489 }
490
491
492 /**************************************************************/
493 /* RAW block driver */
494
495 typedef struct BDRVRawState {
496     int fd;
497 } BDRVRawState;
498
499 static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
500 {
501     return 1; /* maybe */
502 }
503
504 static 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
523 static 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
536 static 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
549 static int raw_close(BlockDriverState *bs)
550 {
551     BDRVRawState *s = bs->opaque;
552     close(s->fd);
553 }
554
555 static 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
572 BlockDriver 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
583 void 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 }