]> git.proxmox.com Git - mirror_qemu.git/blame - block/raw_bsd.c
qemu-iotests: Test the 'base-node' parameter of 'block-stream'
[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
decaeed7
EB
53static int coroutine_fn raw_co_preadv(BlockDriverState *bs, uint64_t offset,
54 uint64_t bytes, QEMUIOVector *qiov,
55 int flags)
e1c66c6d 56{
9eaafd90 57 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
decaeed7 58 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
e1c66c6d
LE
59}
60
decaeed7
EB
61static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset,
62 uint64_t bytes, QEMUIOVector *qiov,
63 int flags)
e1c66c6d 64{
38f3ef57
KW
65 void *buf = NULL;
66 BlockDriver *drv;
67 QEMUIOVector local_qiov;
68 int ret;
69
decaeed7
EB
70 if (bs->probed && offset < BLOCK_PROBE_BUF_SIZE && bytes) {
71 /* Handling partial writes would be a pain - so we just
72 * require that guests have 512-byte request alignment if
73 * probing occurred */
38f3ef57
KW
74 QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE != 512);
75 QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512);
decaeed7 76 assert(offset == 0 && bytes >= BLOCK_PROBE_BUF_SIZE);
38f3ef57 77
9a4f4c31 78 buf = qemu_try_blockalign(bs->file->bs, 512);
38f3ef57
KW
79 if (!buf) {
80 ret = -ENOMEM;
81 goto fail;
82 }
83
84 ret = qemu_iovec_to_buf(qiov, 0, buf, 512);
85 if (ret != 512) {
86 ret = -EINVAL;
87 goto fail;
88 }
89
90 drv = bdrv_probe_all(buf, 512, NULL);
91 if (drv != bs->drv) {
92 ret = -EPERM;
93 goto fail;
94 }
95
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;
102 }
103
9eaafd90 104 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
decaeed7 105 ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
38f3ef57
KW
106
107fail:
108 if (qiov == &local_qiov) {
109 qemu_iovec_destroy(&local_qiov);
110 }
111 qemu_vfree(buf);
112 return ret;
e1c66c6d
LE
113}
114
b6b8a333
PB
115static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
116 int64_t sector_num,
67a0fd2a
FZ
117 int nb_sectors, int *pnum,
118 BlockDriverState **file)
e1c66c6d 119{
92bc50a5 120 *pnum = nb_sectors;
02650acb 121 *file = bs->file->bs;
92bc50a5
PL
122 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
123 (sector_num << BDRV_SECTOR_BITS);
e1c66c6d
LE
124}
125
39ad937e
EB
126static int coroutine_fn raw_co_pwrite_zeroes(BlockDriverState *bs,
127 int64_t offset, int count,
128 BdrvRequestFlags flags)
e1c66c6d 129{
a03ef88f 130 return bdrv_co_pwrite_zeroes(bs->file, offset, count, flags);
e1c66c6d
LE
131}
132
5f61ad07
EB
133static int coroutine_fn raw_co_pdiscard(BlockDriverState *bs,
134 int64_t offset, int count)
e1c66c6d 135{
5f61ad07 136 return bdrv_co_pdiscard(bs->file->bs, offset, count);
e1c66c6d
LE
137}
138
7a6d3fc5 139static int64_t raw_getlength(BlockDriverState *bs)
e1c66c6d 140{
9a4f4c31 141 return bdrv_getlength(bs->file->bs);
e1c66c6d
LE
142}
143
7a6d3fc5 144static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
e1c66c6d 145{
9a4f4c31 146 return bdrv_get_info(bs->file->bs, bdi);
e1c66c6d
LE
147}
148
decaeed7
EB
149static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
150{
151 if (bs->probed) {
152 /* To make it easier to protect the first sector, any probed
153 * image is restricted to read-modify-write on sub-sector
154 * operations. */
155 bs->bl.request_alignment = BDRV_SECTOR_SIZE;
156 }
157}
158
7a6d3fc5 159static int raw_truncate(BlockDriverState *bs, int64_t offset)
e1c66c6d 160{
9a4f4c31 161 return bdrv_truncate(bs->file->bs, offset);
e1c66c6d
LE
162}
163
7a6d3fc5 164static int raw_media_changed(BlockDriverState *bs)
e1c66c6d 165{
9a4f4c31 166 return bdrv_media_changed(bs->file->bs);
e1c66c6d
LE
167}
168
7a6d3fc5 169static void raw_eject(BlockDriverState *bs, bool eject_flag)
e1c66c6d 170{
9a4f4c31 171 bdrv_eject(bs->file->bs, eject_flag);
e1c66c6d
LE
172}
173
7a6d3fc5 174static void raw_lock_medium(BlockDriverState *bs, bool locked)
e1c66c6d 175{
9a4f4c31 176 bdrv_lock_medium(bs->file->bs, locked);
e1c66c6d
LE
177}
178
151a2930 179static int raw_co_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
e1c66c6d 180{
151a2930 181 return bdrv_co_ioctl(bs->file->bs, req, buf);
e1c66c6d
LE
182}
183
7a6d3fc5 184static int raw_has_zero_init(BlockDriverState *bs)
e1c66c6d 185{
9a4f4c31 186 return bdrv_has_zero_init(bs->file->bs);
e1c66c6d
LE
187}
188
cd3a4cf6 189static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
1565262c 190{
9be38598 191 return bdrv_create_file(filename, opts, errp);
1565262c 192}
01dd96d8 193
015a1036
HR
194static int raw_open(BlockDriverState *bs, QDict *options, int flags,
195 Error **errp)
01dd96d8 196{
9a4f4c31 197 bs->sg = bs->file->bs->sg;
8a39b4d6
EB
198 bs->supported_write_flags = BDRV_REQ_FUA &
199 bs->file->bs->supported_write_flags;
200 bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
201 bs->file->bs->supported_zero_flags;
38f3ef57
KW
202
203 if (bs->probed && !bdrv_is_read_only(bs)) {
204 fprintf(stderr,
205 "WARNING: Image format was not specified for '%s' and probing "
206 "guessed raw.\n"
207 " Automatically detecting the format is dangerous for "
208 "raw images, write operations on block 0 will be restricted.\n"
209 " Specify the 'raw' format explicitly to remove the "
210 "restrictions.\n",
9a4f4c31 211 bs->file->bs->filename);
38f3ef57
KW
212 }
213
01dd96d8
LE
214 return 0;
215}
216
7a6d3fc5 217static void raw_close(BlockDriverState *bs)
01dd96d8
LE
218{
219}
220
7a6d3fc5 221static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
01dd96d8
LE
222{
223 /* smallest possible positive score so that raw is used if and only if no
224 * other block driver works
225 */
226 return 1;
227}
775d6afd 228
1a9335e4
ET
229static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
230{
9a4f4c31 231 return bdrv_probe_blocksizes(bs->file->bs, bsz);
1a9335e4
ET
232}
233
234static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
235{
9a4f4c31 236 return bdrv_probe_geometry(bs->file->bs, geo);
1a9335e4
ET
237}
238
5f535a94 239BlockDriver bdrv_raw = {
775d6afd
LE
240 .format_name = "raw",
241 .bdrv_probe = &raw_probe,
242 .bdrv_reopen_prepare = &raw_reopen_prepare,
243 .bdrv_open = &raw_open,
244 .bdrv_close = &raw_close,
c282e1fd 245 .bdrv_create = &raw_create,
decaeed7
EB
246 .bdrv_co_preadv = &raw_co_preadv,
247 .bdrv_co_pwritev = &raw_co_pwritev,
39ad937e 248 .bdrv_co_pwrite_zeroes = &raw_co_pwrite_zeroes,
5f61ad07 249 .bdrv_co_pdiscard = &raw_co_pdiscard,
b6b8a333 250 .bdrv_co_get_block_status = &raw_co_get_block_status,
775d6afd
LE
251 .bdrv_truncate = &raw_truncate,
252 .bdrv_getlength = &raw_getlength,
b94a2610 253 .has_variable_length = true,
775d6afd 254 .bdrv_get_info = &raw_get_info,
decaeed7 255 .bdrv_refresh_limits = &raw_refresh_limits,
1a9335e4
ET
256 .bdrv_probe_blocksizes = &raw_probe_blocksizes,
257 .bdrv_probe_geometry = &raw_probe_geometry,
775d6afd
LE
258 .bdrv_media_changed = &raw_media_changed,
259 .bdrv_eject = &raw_eject,
260 .bdrv_lock_medium = &raw_lock_medium,
151a2930 261 .bdrv_co_ioctl = &raw_co_ioctl,
cd3a4cf6 262 .create_opts = &raw_create_opts,
775d6afd
LE
263 .bdrv_has_zero_init = &raw_has_zero_init
264};
265
266static void bdrv_raw_init(void)
267{
268 bdrv_register(&bdrv_raw);
269}
270
271block_init(bdrv_raw_init);