Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 1 | |
| 2 | #include "qemu-common.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 3 | #include "block/block_int.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 4 | #include "qemu/module.h" |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 5 | |
Kevin Wolf | 1a86938 | 2013-03-15 10:35:01 +0100 | [diff] [blame] | 6 | static int raw_open(BlockDriverState *bs, QDict *options, int flags) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 7 | { |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 8 | bs->sg = bs->file->sg; |
| 9 | return 0; |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 10 | } |
| 11 | |
Jeff Cody | 01bdddb | 2012-09-20 15:13:26 -0400 | [diff] [blame] | 12 | /* We have nothing to do for raw reopen, stubs just return |
| 13 | * success */ |
| 14 | static int raw_reopen_prepare(BDRVReopenState *state, |
| 15 | BlockReopenQueue *queue, Error **errp) |
| 16 | { |
| 17 | return 0; |
| 18 | } |
| 19 | |
Stefan Hajnoczi | d8b7e0a | 2011-10-13 21:09:30 +0100 | [diff] [blame] | 20 | static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num, |
| 21 | int nb_sectors, QEMUIOVector *qiov) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 22 | { |
Paolo Bonzini | 5c171af | 2012-06-06 08:10:44 +0200 | [diff] [blame] | 23 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); |
Stefan Hajnoczi | d8b7e0a | 2011-10-13 21:09:30 +0100 | [diff] [blame] | 24 | return bdrv_co_readv(bs->file, sector_num, nb_sectors, qiov); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 25 | } |
| 26 | |
Stefan Hajnoczi | d8b7e0a | 2011-10-13 21:09:30 +0100 | [diff] [blame] | 27 | static int coroutine_fn raw_co_writev(BlockDriverState *bs, int64_t sector_num, |
| 28 | int nb_sectors, QEMUIOVector *qiov) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 29 | { |
Paolo Bonzini | 5c171af | 2012-06-06 08:10:44 +0200 | [diff] [blame] | 30 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); |
Stefan Hajnoczi | d8b7e0a | 2011-10-13 21:09:30 +0100 | [diff] [blame] | 31 | return bdrv_co_writev(bs->file, sector_num, nb_sectors, qiov); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void raw_close(BlockDriverState *bs) |
| 35 | { |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 36 | } |
| 37 | |
Paolo Bonzini | 5500316 | 2012-05-09 16:49:58 +0200 | [diff] [blame] | 38 | static 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 Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 45 | static int64_t raw_getlength(BlockDriverState *bs) |
| 46 | { |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 47 | return bdrv_getlength(bs->file); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
| 51 | { |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 52 | return bdrv_truncate(bs->file, offset); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | static 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 Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 60 | static int coroutine_fn raw_co_discard(BlockDriverState *bs, |
| 61 | int64_t sector_num, int nb_sectors) |
Christoph Hellwig | bb8bf76 | 2010-12-16 19:36:31 +0100 | [diff] [blame] | 62 | { |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 63 | return bdrv_co_discard(bs->file, sector_num, nb_sectors); |
Christoph Hellwig | bb8bf76 | 2010-12-16 19:36:31 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 66 | static int raw_is_inserted(BlockDriverState *bs) |
| 67 | { |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 68 | return bdrv_is_inserted(bs->file); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 69 | } |
| 70 | |
Markus Armbruster | be32f75 | 2011-08-03 15:08:07 +0200 | [diff] [blame] | 71 | static int raw_media_changed(BlockDriverState *bs) |
| 72 | { |
| 73 | return bdrv_media_changed(bs->file); |
| 74 | } |
| 75 | |
Luiz Capitulino | f36f394 | 2012-02-03 16:24:53 -0200 | [diff] [blame] | 76 | static void raw_eject(BlockDriverState *bs, bool eject_flag) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 77 | { |
Markus Armbruster | 822e1cd | 2011-07-20 18:23:42 +0200 | [diff] [blame] | 78 | bdrv_eject(bs->file, eject_flag); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Markus Armbruster | 025e849 | 2011-09-06 18:58:47 +0200 | [diff] [blame] | 81 | static void raw_lock_medium(BlockDriverState *bs, bool locked) |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 82 | { |
Markus Armbruster | 025e849 | 2011-09-06 18:58:47 +0200 | [diff] [blame] | 83 | bdrv_lock_medium(bs->file, locked); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
| 87 | { |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 88 | return bdrv_ioctl(bs->file, req, buf); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs, |
| 92 | unsigned long int req, void *buf, |
| 93 | BlockDriverCompletionFunc *cb, void *opaque) |
| 94 | { |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 95 | return bdrv_aio_ioctl(bs->file, req, buf, cb, opaque); |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int raw_create(const char *filename, QEMUOptionParameter *options) |
| 99 | { |
| 100 | return bdrv_create_file(filename, options); |
| 101 | } |
| 102 | |
| 103 | static 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 Wolf | 336c1c1 | 2010-07-28 11:26:29 +0200 | [diff] [blame] | 112 | static int raw_has_zero_init(BlockDriverState *bs) |
| 113 | { |
| 114 | return bdrv_has_zero_init(bs->file); |
| 115 | } |
| 116 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 117 | static BlockDriver bdrv_raw = { |
| 118 | .format_name = "raw", |
| 119 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 120 | /* It's really 0, but we need to make g_malloc() happy */ |
Kevin Wolf | 66f82ce | 2010-04-14 14:17:38 +0200 | [diff] [blame] | 121 | .instance_size = 1, |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 122 | |
| 123 | .bdrv_open = raw_open, |
| 124 | .bdrv_close = raw_close, |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 125 | |
Jeff Cody | 01bdddb | 2012-09-20 15:13:26 -0400 | [diff] [blame] | 126 | .bdrv_reopen_prepare = raw_reopen_prepare, |
| 127 | |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 128 | .bdrv_co_readv = raw_co_readv, |
| 129 | .bdrv_co_writev = raw_co_writev, |
Paolo Bonzini | 5500316 | 2012-05-09 16:49:58 +0200 | [diff] [blame] | 130 | .bdrv_co_is_allocated = raw_co_is_allocated, |
Kevin Wolf | c68b89a | 2011-11-10 17:25:44 +0100 | [diff] [blame] | 131 | .bdrv_co_discard = raw_co_discard, |
Paolo Bonzini | 4265d62 | 2011-10-17 12:32:14 +0200 | [diff] [blame] | 132 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 133 | .bdrv_probe = raw_probe, |
| 134 | .bdrv_getlength = raw_getlength, |
| 135 | .bdrv_truncate = raw_truncate, |
| 136 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 137 | .bdrv_is_inserted = raw_is_inserted, |
Markus Armbruster | be32f75 | 2011-08-03 15:08:07 +0200 | [diff] [blame] | 138 | .bdrv_media_changed = raw_media_changed, |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 139 | .bdrv_eject = raw_eject, |
Markus Armbruster | 025e849 | 2011-09-06 18:58:47 +0200 | [diff] [blame] | 140 | .bdrv_lock_medium = raw_lock_medium, |
Markus Armbruster | be32f75 | 2011-08-03 15:08:07 +0200 | [diff] [blame] | 141 | |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 142 | .bdrv_ioctl = raw_ioctl, |
| 143 | .bdrv_aio_ioctl = raw_aio_ioctl, |
| 144 | |
| 145 | .bdrv_create = raw_create, |
| 146 | .create_options = raw_create_options, |
Kevin Wolf | 336c1c1 | 2010-07-28 11:26:29 +0200 | [diff] [blame] | 147 | .bdrv_has_zero_init = raw_has_zero_init, |
Christoph Hellwig | 84a12e6 | 2010-04-07 22:30:24 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | static void bdrv_raw_init(void) |
| 151 | { |
| 152 | bdrv_register(&bdrv_raw); |
| 153 | } |
| 154 | |
| 155 | block_init(bdrv_raw_init); |