blob: ce104220060001f07dc5b36570d2008796edda1c [file] [log] [blame]
Christoph Hellwig84a12e62010-04-07 22:30:24 +02001
2#include "qemu-common.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +01003#include "block/block_int.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01004#include "qemu/module.h"
Christoph Hellwig84a12e62010-04-07 22:30:24 +02005
Kevin Wolf1a869382013-03-15 10:35:01 +01006static int raw_open(BlockDriverState *bs, QDict *options, int flags)
Christoph Hellwig84a12e62010-04-07 22:30:24 +02007{
Kevin Wolf66f82ce2010-04-14 14:17:38 +02008 bs->sg = bs->file->sg;
9 return 0;
Christoph Hellwig84a12e62010-04-07 22:30:24 +020010}
11
Jeff Cody01bdddb2012-09-20 15:13:26 -040012/* We have nothing to do for raw reopen, stubs just return
13 * success */
14static int raw_reopen_prepare(BDRVReopenState *state,
15 BlockReopenQueue *queue, Error **errp)
16{
17 return 0;
18}
19
Stefan Hajnoczid8b7e0a2011-10-13 21:09:30 +010020static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num,
21 int nb_sectors, QEMUIOVector *qiov)
Christoph Hellwig84a12e62010-04-07 22:30:24 +020022{
Paolo Bonzini5c171af2012-06-06 08:10:44 +020023 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
Stefan Hajnoczid8b7e0a2011-10-13 21:09:30 +010024 return bdrv_co_readv(bs->file, sector_num, nb_sectors, qiov);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020025}
26
Stefan Hajnoczid8b7e0a2011-10-13 21:09:30 +010027static int coroutine_fn raw_co_writev(BlockDriverState *bs, int64_t sector_num,
28 int nb_sectors, QEMUIOVector *qiov)
Christoph Hellwig84a12e62010-04-07 22:30:24 +020029{
Paolo Bonzini5c171af2012-06-06 08:10:44 +020030 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
Stefan Hajnoczid8b7e0a2011-10-13 21:09:30 +010031 return bdrv_co_writev(bs->file, sector_num, nb_sectors, qiov);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020032}
33
34static void raw_close(BlockDriverState *bs)
35{
Christoph Hellwig84a12e62010-04-07 22:30:24 +020036}
37
Paolo Bonzini55003162012-05-09 16:49:58 +020038static int coroutine_fn raw_co_is_allocated(BlockDriverState *bs,
39 int64_t sector_num,
40 int nb_sectors, int *pnum)
41{
42 return bdrv_co_is_allocated(bs->file, sector_num, nb_sectors, pnum);
43}
44
Christoph Hellwig84a12e62010-04-07 22:30:24 +020045static int64_t raw_getlength(BlockDriverState *bs)
46{
Kevin Wolf66f82ce2010-04-14 14:17:38 +020047 return bdrv_getlength(bs->file);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020048}
49
50static int raw_truncate(BlockDriverState *bs, int64_t offset)
51{
Kevin Wolf66f82ce2010-04-14 14:17:38 +020052 return bdrv_truncate(bs->file, offset);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020053}
54
55static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
56{
57 return 1; /* everything can be opened as raw image */
58}
59
Paolo Bonzini4265d622011-10-17 12:32:14 +020060static int coroutine_fn raw_co_discard(BlockDriverState *bs,
61 int64_t sector_num, int nb_sectors)
Christoph Hellwigbb8bf762010-12-16 19:36:31 +010062{
Paolo Bonzini4265d622011-10-17 12:32:14 +020063 return bdrv_co_discard(bs->file, sector_num, nb_sectors);
Christoph Hellwigbb8bf762010-12-16 19:36:31 +010064}
65
Christoph Hellwig84a12e62010-04-07 22:30:24 +020066static int raw_is_inserted(BlockDriverState *bs)
67{
Kevin Wolf66f82ce2010-04-14 14:17:38 +020068 return bdrv_is_inserted(bs->file);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020069}
70
Markus Armbrusterbe32f752011-08-03 15:08:07 +020071static int raw_media_changed(BlockDriverState *bs)
72{
73 return bdrv_media_changed(bs->file);
74}
75
Luiz Capitulinof36f3942012-02-03 16:24:53 -020076static void raw_eject(BlockDriverState *bs, bool eject_flag)
Christoph Hellwig84a12e62010-04-07 22:30:24 +020077{
Markus Armbruster822e1cd2011-07-20 18:23:42 +020078 bdrv_eject(bs->file, eject_flag);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020079}
80
Markus Armbruster025e8492011-09-06 18:58:47 +020081static void raw_lock_medium(BlockDriverState *bs, bool locked)
Christoph Hellwig84a12e62010-04-07 22:30:24 +020082{
Markus Armbruster025e8492011-09-06 18:58:47 +020083 bdrv_lock_medium(bs->file, locked);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020084}
85
86static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
87{
Kevin Wolf66f82ce2010-04-14 14:17:38 +020088 return bdrv_ioctl(bs->file, req, buf);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020089}
90
91static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
92 unsigned long int req, void *buf,
93 BlockDriverCompletionFunc *cb, void *opaque)
94{
Kevin Wolf66f82ce2010-04-14 14:17:38 +020095 return bdrv_aio_ioctl(bs->file, req, buf, cb, opaque);
Christoph Hellwig84a12e62010-04-07 22:30:24 +020096}
97
98static int raw_create(const char *filename, QEMUOptionParameter *options)
99{
100 return bdrv_create_file(filename, options);
101}
102
103static QEMUOptionParameter raw_create_options[] = {
104 {
105 .name = BLOCK_OPT_SIZE,
106 .type = OPT_SIZE,
107 .help = "Virtual disk size"
108 },
109 { NULL }
110};
111
Kevin Wolf336c1c12010-07-28 11:26:29 +0200112static int raw_has_zero_init(BlockDriverState *bs)
113{
114 return bdrv_has_zero_init(bs->file);
115}
116
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200117static BlockDriver bdrv_raw = {
118 .format_name = "raw",
119
Anthony Liguori7267c092011-08-20 22:09:37 -0500120 /* It's really 0, but we need to make g_malloc() happy */
Kevin Wolf66f82ce2010-04-14 14:17:38 +0200121 .instance_size = 1,
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200122
123 .bdrv_open = raw_open,
124 .bdrv_close = raw_close,
Paolo Bonzini4265d622011-10-17 12:32:14 +0200125
Jeff Cody01bdddb2012-09-20 15:13:26 -0400126 .bdrv_reopen_prepare = raw_reopen_prepare,
127
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100128 .bdrv_co_readv = raw_co_readv,
129 .bdrv_co_writev = raw_co_writev,
Paolo Bonzini55003162012-05-09 16:49:58 +0200130 .bdrv_co_is_allocated = raw_co_is_allocated,
Kevin Wolfc68b89a2011-11-10 17:25:44 +0100131 .bdrv_co_discard = raw_co_discard,
Paolo Bonzini4265d622011-10-17 12:32:14 +0200132
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200133 .bdrv_probe = raw_probe,
134 .bdrv_getlength = raw_getlength,
135 .bdrv_truncate = raw_truncate,
136
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200137 .bdrv_is_inserted = raw_is_inserted,
Markus Armbrusterbe32f752011-08-03 15:08:07 +0200138 .bdrv_media_changed = raw_media_changed,
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200139 .bdrv_eject = raw_eject,
Markus Armbruster025e8492011-09-06 18:58:47 +0200140 .bdrv_lock_medium = raw_lock_medium,
Markus Armbrusterbe32f752011-08-03 15:08:07 +0200141
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200142 .bdrv_ioctl = raw_ioctl,
143 .bdrv_aio_ioctl = raw_aio_ioctl,
144
145 .bdrv_create = raw_create,
146 .create_options = raw_create_options,
Kevin Wolf336c1c12010-07-28 11:26:29 +0200147 .bdrv_has_zero_init = raw_has_zero_init,
Christoph Hellwig84a12e62010-04-07 22:30:24 +0200148};
149
150static void bdrv_raw_init(void)
151{
152 bdrv_register(&bdrv_raw);
153}
154
155block_init(bdrv_raw_init);