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