virtio: sync the dataplane vring state to the virtqueue before virtio_save
[qemu/cris-port.git] / block / raw_bsd.c
blob0aded31c22cd5275d6c5421faa20ad0797954de0
1 /* BlockDriver implementation for "raw"
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>
7 * Author:
8 * Laszlo Ersek <lersek@redhat.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
29 #include "block/block_int.h"
30 #include "qemu/option.h"
32 static QemuOptsList raw_create_opts = {
33 .name = "raw-create-opts",
34 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
35 .desc = {
37 .name = BLOCK_OPT_SIZE,
38 .type = QEMU_OPT_SIZE,
39 .help = "Virtual disk size"
41 { /* end of list */ }
45 static int raw_reopen_prepare(BDRVReopenState *reopen_state,
46 BlockReopenQueue *queue, Error **errp)
48 return 0;
51 static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num,
52 int nb_sectors, QEMUIOVector *qiov)
54 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
55 return bdrv_co_readv(bs->file->bs, sector_num, nb_sectors, qiov);
58 static int coroutine_fn raw_co_writev(BlockDriverState *bs, int64_t sector_num,
59 int nb_sectors, QEMUIOVector *qiov)
61 void *buf = NULL;
62 BlockDriver *drv;
63 QEMUIOVector local_qiov;
64 int ret;
66 if (bs->probed && sector_num == 0) {
67 /* As long as these conditions are true, we can't get partial writes to
68 * the probe buffer and can just directly check the request. */
69 QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE != 512);
70 QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512);
72 if (nb_sectors == 0) {
73 /* qemu_iovec_to_buf() would fail, but we want to return success
74 * instead of -EINVAL in this case. */
75 return 0;
78 buf = qemu_try_blockalign(bs->file->bs, 512);
79 if (!buf) {
80 ret = -ENOMEM;
81 goto fail;
84 ret = qemu_iovec_to_buf(qiov, 0, buf, 512);
85 if (ret != 512) {
86 ret = -EINVAL;
87 goto fail;
90 drv = bdrv_probe_all(buf, 512, NULL);
91 if (drv != bs->drv) {
92 ret = -EPERM;
93 goto fail;
96 /* Use the checked buffer, a malicious guest might be overwriting its
97 * original buffer in the background. */
98 qemu_iovec_init(&local_qiov, qiov->niov + 1);
99 qemu_iovec_add(&local_qiov, buf, 512);
100 qemu_iovec_concat(&local_qiov, qiov, 512, qiov->size - 512);
101 qiov = &local_qiov;
104 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
105 ret = bdrv_co_writev(bs->file->bs, sector_num, nb_sectors, qiov);
107 fail:
108 if (qiov == &local_qiov) {
109 qemu_iovec_destroy(&local_qiov);
111 qemu_vfree(buf);
112 return ret;
115 static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
116 int64_t sector_num,
117 int nb_sectors, int *pnum)
119 *pnum = nb_sectors;
120 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
121 (sector_num << BDRV_SECTOR_BITS);
124 static int coroutine_fn raw_co_write_zeroes(BlockDriverState *bs,
125 int64_t sector_num, int nb_sectors,
126 BdrvRequestFlags flags)
128 return bdrv_co_write_zeroes(bs->file->bs, sector_num, nb_sectors, flags);
131 static int coroutine_fn raw_co_discard(BlockDriverState *bs,
132 int64_t sector_num, int nb_sectors)
134 return bdrv_co_discard(bs->file->bs, sector_num, nb_sectors);
137 static int64_t raw_getlength(BlockDriverState *bs)
139 return bdrv_getlength(bs->file->bs);
142 static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
144 return bdrv_get_info(bs->file->bs, bdi);
147 static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
149 bs->bl = bs->file->bs->bl;
152 static int raw_truncate(BlockDriverState *bs, int64_t offset)
154 return bdrv_truncate(bs->file->bs, offset);
157 static int raw_media_changed(BlockDriverState *bs)
159 return bdrv_media_changed(bs->file->bs);
162 static void raw_eject(BlockDriverState *bs, bool eject_flag)
164 bdrv_eject(bs->file->bs, eject_flag);
167 static void raw_lock_medium(BlockDriverState *bs, bool locked)
169 bdrv_lock_medium(bs->file->bs, locked);
172 static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
174 return bdrv_ioctl(bs->file->bs, req, buf);
177 static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs,
178 unsigned long int req, void *buf,
179 BlockCompletionFunc *cb,
180 void *opaque)
182 return bdrv_aio_ioctl(bs->file->bs, req, buf, cb, opaque);
185 static int raw_has_zero_init(BlockDriverState *bs)
187 return bdrv_has_zero_init(bs->file->bs);
190 static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
192 Error *local_err = NULL;
193 int ret;
195 ret = bdrv_create_file(filename, opts, &local_err);
196 if (local_err) {
197 error_propagate(errp, local_err);
199 return ret;
202 static int raw_open(BlockDriverState *bs, QDict *options, int flags,
203 Error **errp)
205 bs->sg = bs->file->bs->sg;
207 if (bs->probed && !bdrv_is_read_only(bs)) {
208 fprintf(stderr,
209 "WARNING: Image format was not specified for '%s' and probing "
210 "guessed raw.\n"
211 " Automatically detecting the format is dangerous for "
212 "raw images, write operations on block 0 will be restricted.\n"
213 " Specify the 'raw' format explicitly to remove the "
214 "restrictions.\n",
215 bs->file->bs->filename);
218 return 0;
221 static void raw_close(BlockDriverState *bs)
225 static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
227 /* smallest possible positive score so that raw is used if and only if no
228 * other block driver works
230 return 1;
233 static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
235 return bdrv_probe_blocksizes(bs->file->bs, bsz);
238 static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
240 return bdrv_probe_geometry(bs->file->bs, geo);
243 BlockDriver bdrv_raw = {
244 .format_name = "raw",
245 .bdrv_probe = &raw_probe,
246 .bdrv_reopen_prepare = &raw_reopen_prepare,
247 .bdrv_open = &raw_open,
248 .bdrv_close = &raw_close,
249 .bdrv_create = &raw_create,
250 .bdrv_co_readv = &raw_co_readv,
251 .bdrv_co_writev = &raw_co_writev,
252 .bdrv_co_write_zeroes = &raw_co_write_zeroes,
253 .bdrv_co_discard = &raw_co_discard,
254 .bdrv_co_get_block_status = &raw_co_get_block_status,
255 .bdrv_truncate = &raw_truncate,
256 .bdrv_getlength = &raw_getlength,
257 .has_variable_length = true,
258 .bdrv_get_info = &raw_get_info,
259 .bdrv_refresh_limits = &raw_refresh_limits,
260 .bdrv_probe_blocksizes = &raw_probe_blocksizes,
261 .bdrv_probe_geometry = &raw_probe_geometry,
262 .bdrv_media_changed = &raw_media_changed,
263 .bdrv_eject = &raw_eject,
264 .bdrv_lock_medium = &raw_lock_medium,
265 .bdrv_ioctl = &raw_ioctl,
266 .bdrv_aio_ioctl = &raw_aio_ioctl,
267 .create_opts = &raw_create_opts,
268 .bdrv_has_zero_init = &raw_has_zero_init
271 static void bdrv_raw_init(void)
273 bdrv_register(&bdrv_raw);
276 block_init(bdrv_raw_init);