]>
Commit | Line | Data |
---|---|---|
e1c66c6d LE |
1 | /* BlockDriver implementation for "raw" |
2 | * | |
ff369a48 LE |
3 | * Copyright (C) 2010, 2013, Red Hat, Inc. |
4 | * Copyright (C) 2010, Blue Swirl <[email protected]> | |
775d6afd | 5 | * Copyright (C) 2009, Anthony Liguori <[email protected]> |
e1c66c6d LE |
6 | * |
7 | * Author: | |
8 | * Laszlo Ersek <[email protected]> | |
9 | * | |
10 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
11 | * of this software and associated documentation files (the "Software"), to | |
12 | * deal in the Software without restriction, including without limitation the | |
13 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | |
14 | * sell copies of the Software, and to permit persons to whom the Software is | |
15 | * furnished to do so, subject to the following conditions: | |
16 | * | |
17 | * The above copyright notice and this permission notice shall be included in | |
18 | * all copies or substantial portions of the Software. | |
19 | * | |
20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
26 | * IN THE SOFTWARE. | |
27 | */ | |
28 | ||
80c71a24 | 29 | #include "qemu/osdep.h" |
e1c66c6d | 30 | #include "block/block_int.h" |
ff369a48 LE |
31 | #include "qemu/option.h" |
32 | ||
cd3a4cf6 CL |
33 | static QemuOptsList raw_create_opts = { |
34 | .name = "raw-create-opts", | |
35 | .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head), | |
36 | .desc = { | |
37 | { | |
38 | .name = BLOCK_OPT_SIZE, | |
39 | .type = QEMU_OPT_SIZE, | |
40 | .help = "Virtual disk size" | |
41 | }, | |
42 | { /* end of list */ } | |
43 | } | |
ff369a48 | 44 | }; |
e1c66c6d | 45 | |
7a6d3fc5 LE |
46 | static int raw_reopen_prepare(BDRVReopenState *reopen_state, |
47 | BlockReopenQueue *queue, Error **errp) | |
e1c66c6d | 48 | { |
7a6d3fc5 | 49 | return 0; |
e1c66c6d LE |
50 | } |
51 | ||
7a6d3fc5 LE |
52 | static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num, |
53 | int nb_sectors, QEMUIOVector *qiov) | |
e1c66c6d | 54 | { |
9eaafd90 | 55 | BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO); |
9a4f4c31 | 56 | return bdrv_co_readv(bs->file->bs, sector_num, nb_sectors, qiov); |
e1c66c6d LE |
57 | } |
58 | ||
7a6d3fc5 LE |
59 | static int coroutine_fn raw_co_writev(BlockDriverState *bs, int64_t sector_num, |
60 | int nb_sectors, QEMUIOVector *qiov) | |
e1c66c6d | 61 | { |
38f3ef57 KW |
62 | void *buf = NULL; |
63 | BlockDriver *drv; | |
64 | QEMUIOVector local_qiov; | |
65 | int ret; | |
66 | ||
67 | if (bs->probed && sector_num == 0) { | |
68 | /* As long as these conditions are true, we can't get partial writes to | |
69 | * the probe buffer and can just directly check the request. */ | |
70 | QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE != 512); | |
71 | QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512); | |
72 | ||
73 | if (nb_sectors == 0) { | |
74 | /* qemu_iovec_to_buf() would fail, but we want to return success | |
75 | * instead of -EINVAL in this case. */ | |
76 | return 0; | |
77 | } | |
78 | ||
9a4f4c31 | 79 | buf = qemu_try_blockalign(bs->file->bs, 512); |
38f3ef57 KW |
80 | if (!buf) { |
81 | ret = -ENOMEM; | |
82 | goto fail; | |
83 | } | |
84 | ||
85 | ret = qemu_iovec_to_buf(qiov, 0, buf, 512); | |
86 | if (ret != 512) { | |
87 | ret = -EINVAL; | |
88 | goto fail; | |
89 | } | |
90 | ||
91 | drv = bdrv_probe_all(buf, 512, NULL); | |
92 | if (drv != bs->drv) { | |
93 | ret = -EPERM; | |
94 | goto fail; | |
95 | } | |
96 | ||
97 | /* Use the checked buffer, a malicious guest might be overwriting its | |
98 | * original buffer in the background. */ | |
99 | qemu_iovec_init(&local_qiov, qiov->niov + 1); | |
100 | qemu_iovec_add(&local_qiov, buf, 512); | |
101 | qemu_iovec_concat(&local_qiov, qiov, 512, qiov->size - 512); | |
102 | qiov = &local_qiov; | |
103 | } | |
104 | ||
9eaafd90 | 105 | BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO); |
9a4f4c31 | 106 | ret = bdrv_co_writev(bs->file->bs, sector_num, nb_sectors, qiov); |
38f3ef57 KW |
107 | |
108 | fail: | |
109 | if (qiov == &local_qiov) { | |
110 | qemu_iovec_destroy(&local_qiov); | |
111 | } | |
112 | qemu_vfree(buf); | |
113 | return ret; | |
e1c66c6d LE |
114 | } |
115 | ||
b6b8a333 PB |
116 | static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs, |
117 | int64_t sector_num, | |
67a0fd2a FZ |
118 | int nb_sectors, int *pnum, |
119 | BlockDriverState **file) | |
e1c66c6d | 120 | { |
92bc50a5 | 121 | *pnum = nb_sectors; |
02650acb | 122 | *file = bs->file->bs; |
92bc50a5 PL |
123 | return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA | |
124 | (sector_num << BDRV_SECTOR_BITS); | |
e1c66c6d LE |
125 | } |
126 | ||
7a6d3fc5 | 127 | static int coroutine_fn raw_co_write_zeroes(BlockDriverState *bs, |
aa7bfbff PL |
128 | int64_t sector_num, int nb_sectors, |
129 | BdrvRequestFlags flags) | |
e1c66c6d | 130 | { |
9a4f4c31 | 131 | return bdrv_co_write_zeroes(bs->file->bs, sector_num, nb_sectors, flags); |
e1c66c6d LE |
132 | } |
133 | ||
7a6d3fc5 LE |
134 | static int coroutine_fn raw_co_discard(BlockDriverState *bs, |
135 | int64_t sector_num, int nb_sectors) | |
e1c66c6d | 136 | { |
9a4f4c31 | 137 | return bdrv_co_discard(bs->file->bs, sector_num, nb_sectors); |
e1c66c6d LE |
138 | } |
139 | ||
7a6d3fc5 | 140 | static int64_t raw_getlength(BlockDriverState *bs) |
e1c66c6d | 141 | { |
9a4f4c31 | 142 | return bdrv_getlength(bs->file->bs); |
e1c66c6d LE |
143 | } |
144 | ||
7a6d3fc5 | 145 | static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
e1c66c6d | 146 | { |
9a4f4c31 | 147 | return bdrv_get_info(bs->file->bs, bdi); |
e1c66c6d LE |
148 | } |
149 | ||
3baca891 | 150 | static void raw_refresh_limits(BlockDriverState *bs, Error **errp) |
ad6aef43 | 151 | { |
9a4f4c31 | 152 | bs->bl = bs->file->bs->bl; |
ad6aef43 KW |
153 | } |
154 | ||
7a6d3fc5 | 155 | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
e1c66c6d | 156 | { |
9a4f4c31 | 157 | return bdrv_truncate(bs->file->bs, offset); |
e1c66c6d LE |
158 | } |
159 | ||
7a6d3fc5 | 160 | static int raw_media_changed(BlockDriverState *bs) |
e1c66c6d | 161 | { |
9a4f4c31 | 162 | return bdrv_media_changed(bs->file->bs); |
e1c66c6d LE |
163 | } |
164 | ||
7a6d3fc5 | 165 | static void raw_eject(BlockDriverState *bs, bool eject_flag) |
e1c66c6d | 166 | { |
9a4f4c31 | 167 | bdrv_eject(bs->file->bs, eject_flag); |
e1c66c6d LE |
168 | } |
169 | ||
7a6d3fc5 | 170 | static void raw_lock_medium(BlockDriverState *bs, bool locked) |
e1c66c6d | 171 | { |
9a4f4c31 | 172 | bdrv_lock_medium(bs->file->bs, locked); |
e1c66c6d LE |
173 | } |
174 | ||
7c84b1b8 MA |
175 | static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs, |
176 | unsigned long int req, void *buf, | |
097310b5 | 177 | BlockCompletionFunc *cb, |
7c84b1b8 | 178 | void *opaque) |
e1c66c6d | 179 | { |
9a4f4c31 | 180 | return bdrv_aio_ioctl(bs->file->bs, req, buf, cb, opaque); |
e1c66c6d LE |
181 | } |
182 | ||
7a6d3fc5 | 183 | static int raw_has_zero_init(BlockDriverState *bs) |
e1c66c6d | 184 | { |
9a4f4c31 | 185 | return bdrv_has_zero_init(bs->file->bs); |
e1c66c6d LE |
186 | } |
187 | ||
cd3a4cf6 | 188 | static int raw_create(const char *filename, QemuOpts *opts, Error **errp) |
1565262c | 189 | { |
cc84d90f HR |
190 | Error *local_err = NULL; |
191 | int ret; | |
192 | ||
c282e1fd | 193 | ret = bdrv_create_file(filename, opts, &local_err); |
84d18f06 | 194 | if (local_err) { |
92f1deec | 195 | error_propagate(errp, local_err); |
cc84d90f HR |
196 | } |
197 | return ret; | |
1565262c | 198 | } |
01dd96d8 | 199 | |
015a1036 HR |
200 | static int raw_open(BlockDriverState *bs, QDict *options, int flags, |
201 | Error **errp) | |
01dd96d8 | 202 | { |
9a4f4c31 | 203 | bs->sg = bs->file->bs->sg; |
38f3ef57 KW |
204 | |
205 | if (bs->probed && !bdrv_is_read_only(bs)) { | |
206 | fprintf(stderr, | |
207 | "WARNING: Image format was not specified for '%s' and probing " | |
208 | "guessed raw.\n" | |
209 | " Automatically detecting the format is dangerous for " | |
210 | "raw images, write operations on block 0 will be restricted.\n" | |
211 | " Specify the 'raw' format explicitly to remove the " | |
212 | "restrictions.\n", | |
9a4f4c31 | 213 | bs->file->bs->filename); |
38f3ef57 KW |
214 | } |
215 | ||
01dd96d8 LE |
216 | return 0; |
217 | } | |
218 | ||
7a6d3fc5 | 219 | static void raw_close(BlockDriverState *bs) |
01dd96d8 LE |
220 | { |
221 | } | |
222 | ||
7a6d3fc5 | 223 | static int raw_probe(const uint8_t *buf, int buf_size, const char *filename) |
01dd96d8 LE |
224 | { |
225 | /* smallest possible positive score so that raw is used if and only if no | |
226 | * other block driver works | |
227 | */ | |
228 | return 1; | |
229 | } | |
775d6afd | 230 | |
1a9335e4 ET |
231 | static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz) |
232 | { | |
9a4f4c31 | 233 | return bdrv_probe_blocksizes(bs->file->bs, bsz); |
1a9335e4 ET |
234 | } |
235 | ||
236 | static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo) | |
237 | { | |
9a4f4c31 | 238 | return bdrv_probe_geometry(bs->file->bs, geo); |
1a9335e4 ET |
239 | } |
240 | ||
5f535a94 | 241 | BlockDriver bdrv_raw = { |
775d6afd LE |
242 | .format_name = "raw", |
243 | .bdrv_probe = &raw_probe, | |
244 | .bdrv_reopen_prepare = &raw_reopen_prepare, | |
245 | .bdrv_open = &raw_open, | |
246 | .bdrv_close = &raw_close, | |
c282e1fd | 247 | .bdrv_create = &raw_create, |
775d6afd LE |
248 | .bdrv_co_readv = &raw_co_readv, |
249 | .bdrv_co_writev = &raw_co_writev, | |
250 | .bdrv_co_write_zeroes = &raw_co_write_zeroes, | |
251 | .bdrv_co_discard = &raw_co_discard, | |
b6b8a333 | 252 | .bdrv_co_get_block_status = &raw_co_get_block_status, |
775d6afd LE |
253 | .bdrv_truncate = &raw_truncate, |
254 | .bdrv_getlength = &raw_getlength, | |
b94a2610 | 255 | .has_variable_length = true, |
775d6afd | 256 | .bdrv_get_info = &raw_get_info, |
ad6aef43 | 257 | .bdrv_refresh_limits = &raw_refresh_limits, |
1a9335e4 ET |
258 | .bdrv_probe_blocksizes = &raw_probe_blocksizes, |
259 | .bdrv_probe_geometry = &raw_probe_geometry, | |
775d6afd LE |
260 | .bdrv_media_changed = &raw_media_changed, |
261 | .bdrv_eject = &raw_eject, | |
262 | .bdrv_lock_medium = &raw_lock_medium, | |
775d6afd | 263 | .bdrv_aio_ioctl = &raw_aio_ioctl, |
cd3a4cf6 | 264 | .create_opts = &raw_create_opts, |
775d6afd LE |
265 | .bdrv_has_zero_init = &raw_has_zero_init |
266 | }; | |
267 | ||
268 | static void bdrv_raw_init(void) | |
269 | { | |
270 | bdrv_register(&bdrv_raw); | |
271 | } | |
272 | ||
273 | block_init(bdrv_raw_init); |