]> git.proxmox.com Git - mirror_qemu.git/blob - block/raw_bsd.c
include/qemu/osdep.h: Don't include qapi/error.h
[mirror_qemu.git] / block / raw_bsd.c
1 /* BlockDriver implementation for "raw"
2 *
3 * Copyright (C) 2010, 2013, Red Hat, Inc.
4 * Copyright (C) 2010, Blue Swirl <blauwirbel@gmail.com>
5 * Copyright (C) 2009, Anthony Liguori <aliguori@us.ibm.com>
6 *
7 * Author:
8 * Laszlo Ersek <lersek@redhat.com>
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
29 #include "qemu/osdep.h"
30 #include "block/block_int.h"
31 #include "qapi/error.h"
32 #include "qemu/option.h"
33
34 static QemuOptsList raw_create_opts = {
35 .name = "raw-create-opts",
36 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
37 .desc = {
38 {
39 .name = BLOCK_OPT_SIZE,
40 .type = QEMU_OPT_SIZE,
41 .help = "Virtual disk size"
42 },
43 { /* end of list */ }
44 }
45 };
46
47 static int raw_reopen_prepare(BDRVReopenState *reopen_state,
48 BlockReopenQueue *queue, Error **errp)
49 {
50 return 0;
51 }
52
53 static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num,
54 int nb_sectors, QEMUIOVector *qiov)
55 {
56 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
57 return bdrv_co_readv(bs->file->bs, sector_num, nb_sectors, qiov);
58 }
59
60 static int coroutine_fn raw_co_writev(BlockDriverState *bs, int64_t sector_num,
61 int nb_sectors, QEMUIOVector *qiov)
62 {
63 void *buf = NULL;
64 BlockDriver *drv;
65 QEMUIOVector local_qiov;
66 int ret;
67
68 if (bs->probed && sector_num == 0) {
69 /* As long as these conditions are true, we can't get partial writes to
70 * the probe buffer and can just directly check the request. */
71 QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE != 512);
72 QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512);
73
74 if (nb_sectors == 0) {
75 /* qemu_iovec_to_buf() would fail, but we want to return success
76 * instead of -EINVAL in this case. */
77 return 0;
78 }
79
80 buf = qemu_try_blockalign(bs->file->bs, 512);
81 if (!buf) {
82 ret = -ENOMEM;
83 goto fail;
84 }
85
86 ret = qemu_iovec_to_buf(qiov, 0, buf, 512);
87 if (ret != 512) {
88 ret = -EINVAL;
89 goto fail;
90 }
91
92 drv = bdrv_probe_all(buf, 512, NULL);
93 if (drv != bs->drv) {
94 ret = -EPERM;
95 goto fail;
96 }
97
98 /* Use the checked buffer, a malicious guest might be overwriting its
99 * original buffer in the background. */
100 qemu_iovec_init(&local_qiov, qiov->niov + 1);
101 qemu_iovec_add(&local_qiov, buf, 512);
102 qemu_iovec_concat(&local_qiov, qiov, 512, qiov->size - 512);
103 qiov = &local_qiov;
104 }
105
106 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
107 ret = bdrv_co_writev(bs->file->bs, sector_num, nb_sectors, qiov);
108
109 fail:
110 if (qiov == &local_qiov) {
111 qemu_iovec_destroy(&local_qiov);
112 }
113 qemu_vfree(buf);
114 return ret;
115 }
116
117 static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
118 int64_t sector_num,
119 int nb_sectors, int *pnum,
120 BlockDriverState **file)
121 {
122 *pnum = nb_sectors;
123 *file = bs->file->bs;
124 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
125 (sector_num << BDRV_SECTOR_BITS);
126 }
127
128 static int coroutine_fn raw_co_write_zeroes(BlockDriverState *bs,
129 int64_t sector_num, int nb_sectors,
130 BdrvRequestFlags flags)
131 {
132 return bdrv_co_write_zeroes(bs->file->bs, sector_num, nb_sectors, flags);
133 }
134
135 static int coroutine_fn raw_co_discard(BlockDriverState *bs,
136 int64_t sector_num, int nb_sectors)
137 {
138 return bdrv_co_discard(bs->file->bs, sector_num, nb_sectors);
139 }
140
141 static int64_t raw_getlength(BlockDriverState *bs)
142 {
143 return bdrv_getlength(bs->file->bs);
144 }
145
146 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
147 {
148 return bdrv_get_info(bs->file->bs, bdi);
149 }
150
151 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
152 {
153 bs->bl = bs->file->bs->bl;
154 }
155
156 static int raw_truncate(BlockDriverState *bs, int64_t offset)
157 {
158 return bdrv_truncate(bs->file->bs, offset);
159 }
160
161 static int raw_media_changed(BlockDriverState *bs)
162 {
163 return bdrv_media_changed(bs->file->bs);
164 }
165
166 static void raw_eject(BlockDriverState *bs, bool eject_flag)
167 {
168 bdrv_eject(bs->file->bs, eject_flag);
169 }
170
171 static void raw_lock_medium(BlockDriverState *bs, bool locked)
172 {
173 bdrv_lock_medium(bs->file->bs, locked);
174 }
175
176 static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs,
177 unsigned long int req, void *buf,
178 BlockCompletionFunc *cb,
179 void *opaque)
180 {
181 return bdrv_aio_ioctl(bs->file->bs, req, buf, cb, opaque);
182 }
183
184 static int raw_has_zero_init(BlockDriverState *bs)
185 {
186 return bdrv_has_zero_init(bs->file->bs);
187 }
188
189 static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
190 {
191 Error *local_err = NULL;
192 int ret;
193
194 ret = bdrv_create_file(filename, opts, &local_err);
195 if (local_err) {
196 error_propagate(errp, local_err);
197 }
198 return ret;
199 }
200
201 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
202 Error **errp)
203 {
204 bs->sg = bs->file->bs->sg;
205
206 if (bs->probed && !bdrv_is_read_only(bs)) {
207 fprintf(stderr,
208 "WARNING: Image format was not specified for '%s' and probing "
209 "guessed raw.\n"
210 " Automatically detecting the format is dangerous for "
211 "raw images, write operations on block 0 will be restricted.\n"
212 " Specify the 'raw' format explicitly to remove the "
213 "restrictions.\n",
214 bs->file->bs->filename);
215 }
216
217 return 0;
218 }
219
220 static void raw_close(BlockDriverState *bs)
221 {
222 }
223
224 static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
225 {
226 /* smallest possible positive score so that raw is used if and only if no
227 * other block driver works
228 */
229 return 1;
230 }
231
232 static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
233 {
234 return bdrv_probe_blocksizes(bs->file->bs, bsz);
235 }
236
237 static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
238 {
239 return bdrv_probe_geometry(bs->file->bs, geo);
240 }
241
242 BlockDriver bdrv_raw = {
243 .format_name = "raw",
244 .bdrv_probe = &raw_probe,
245 .bdrv_reopen_prepare = &raw_reopen_prepare,
246 .bdrv_open = &raw_open,
247 .bdrv_close = &raw_close,
248 .bdrv_create = &raw_create,
249 .bdrv_co_readv = &raw_co_readv,
250 .bdrv_co_writev = &raw_co_writev,
251 .bdrv_co_write_zeroes = &raw_co_write_zeroes,
252 .bdrv_co_discard = &raw_co_discard,
253 .bdrv_co_get_block_status = &raw_co_get_block_status,
254 .bdrv_truncate = &raw_truncate,
255 .bdrv_getlength = &raw_getlength,
256 .has_variable_length = true,
257 .bdrv_get_info = &raw_get_info,
258 .bdrv_refresh_limits = &raw_refresh_limits,
259 .bdrv_probe_blocksizes = &raw_probe_blocksizes,
260 .bdrv_probe_geometry = &raw_probe_geometry,
261 .bdrv_media_changed = &raw_media_changed,
262 .bdrv_eject = &raw_eject,
263 .bdrv_lock_medium = &raw_lock_medium,
264 .bdrv_aio_ioctl = &raw_aio_ioctl,
265 .create_opts = &raw_create_opts,
266 .bdrv_has_zero_init = &raw_has_zero_init
267 };
268
269 static void bdrv_raw_init(void)
270 {
271 bdrv_register(&bdrv_raw);
272 }
273
274 block_init(bdrv_raw_init);