]> git.proxmox.com Git - mirror_qemu.git/blame - block/raw_bsd.c
raw-posix: Factor block size detection out of raw_probe_alignment()
[mirror_qemu.git] / block / raw_bsd.c
CommitLineData
e1c66c6d
LE
1/* BlockDriver implementation for "raw"
2 *
ff369a48
LE
3 * Copyright (C) 2010, 2013, Red Hat, Inc.
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
29#include "block/block_int.h"
ff369a48
LE
30#include "qemu/option.h"
31
cd3a4cf6
CL
32static QemuOptsList raw_create_opts = {
33 .name = "raw-create-opts",
34 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
35 .desc = {
36 {
37 .name = BLOCK_OPT_SIZE,
38 .type = QEMU_OPT_SIZE,
39 .help = "Virtual disk size"
40 },
41 { /* end of list */ }
42 }
ff369a48 43};
e1c66c6d 44
7a6d3fc5
LE
45static int raw_reopen_prepare(BDRVReopenState *reopen_state,
46 BlockReopenQueue *queue, Error **errp)
e1c66c6d 47{
7a6d3fc5 48 return 0;
e1c66c6d
LE
49}
50
7a6d3fc5
LE
51static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num,
52 int nb_sectors, QEMUIOVector *qiov)
e1c66c6d 53{
9eaafd90 54 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
7a6d3fc5 55 return bdrv_co_readv(bs->file, sector_num, nb_sectors, qiov);
e1c66c6d
LE
56}
57
7a6d3fc5
LE
58static int coroutine_fn raw_co_writev(BlockDriverState *bs, int64_t sector_num,
59 int nb_sectors, QEMUIOVector *qiov)
e1c66c6d 60{
38f3ef57
KW
61 void *buf = NULL;
62 BlockDriver *drv;
63 QEMUIOVector local_qiov;
64 int ret;
65
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);
71
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;
76 }
77
78 buf = qemu_try_blockalign(bs->file, 512);
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);
38f3ef57
KW
105 ret = bdrv_co_writev(bs->file, sector_num, nb_sectors, qiov);
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,
117 int nb_sectors, int *pnum)
e1c66c6d 118{
92bc50a5
PL
119 *pnum = nb_sectors;
120 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
121 (sector_num << BDRV_SECTOR_BITS);
e1c66c6d
LE
122}
123
7a6d3fc5 124static int coroutine_fn raw_co_write_zeroes(BlockDriverState *bs,
aa7bfbff
PL
125 int64_t sector_num, int nb_sectors,
126 BdrvRequestFlags flags)
e1c66c6d 127{
aa7bfbff 128 return bdrv_co_write_zeroes(bs->file, sector_num, nb_sectors, flags);
e1c66c6d
LE
129}
130
7a6d3fc5
LE
131static int coroutine_fn raw_co_discard(BlockDriverState *bs,
132 int64_t sector_num, int nb_sectors)
e1c66c6d 133{
7a6d3fc5 134 return bdrv_co_discard(bs->file, sector_num, nb_sectors);
e1c66c6d
LE
135}
136
7a6d3fc5 137static int64_t raw_getlength(BlockDriverState *bs)
e1c66c6d
LE
138{
139 return bdrv_getlength(bs->file);
140}
141
7a6d3fc5 142static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
e1c66c6d 143{
7a6d3fc5 144 return bdrv_get_info(bs->file, bdi);
e1c66c6d
LE
145}
146
3baca891 147static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
ad6aef43
KW
148{
149 bs->bl = bs->file->bl;
ad6aef43
KW
150}
151
7a6d3fc5 152static int raw_truncate(BlockDriverState *bs, int64_t offset)
e1c66c6d 153{
7a6d3fc5 154 return bdrv_truncate(bs->file, offset);
e1c66c6d
LE
155}
156
7a6d3fc5 157static int raw_is_inserted(BlockDriverState *bs)
e1c66c6d
LE
158{
159 return bdrv_is_inserted(bs->file);
160}
161
7a6d3fc5 162static int raw_media_changed(BlockDriverState *bs)
e1c66c6d
LE
163{
164 return bdrv_media_changed(bs->file);
165}
166
7a6d3fc5 167static void raw_eject(BlockDriverState *bs, bool eject_flag)
e1c66c6d 168{
7a6d3fc5 169 bdrv_eject(bs->file, eject_flag);
e1c66c6d
LE
170}
171
7a6d3fc5 172static void raw_lock_medium(BlockDriverState *bs, bool locked)
e1c66c6d 173{
7a6d3fc5 174 bdrv_lock_medium(bs->file, locked);
e1c66c6d
LE
175}
176
7a6d3fc5 177static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
e1c66c6d 178{
7a6d3fc5 179 return bdrv_ioctl(bs->file, req, buf);
e1c66c6d
LE
180}
181
7c84b1b8
MA
182static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs,
183 unsigned long int req, void *buf,
097310b5 184 BlockCompletionFunc *cb,
7c84b1b8 185 void *opaque)
e1c66c6d 186{
7a6d3fc5 187 return bdrv_aio_ioctl(bs->file, req, buf, cb, opaque);
e1c66c6d
LE
188}
189
7a6d3fc5 190static int raw_has_zero_init(BlockDriverState *bs)
e1c66c6d
LE
191{
192 return bdrv_has_zero_init(bs->file);
193}
194
cd3a4cf6 195static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
1565262c 196{
cc84d90f
HR
197 Error *local_err = NULL;
198 int ret;
199
c282e1fd 200 ret = bdrv_create_file(filename, opts, &local_err);
84d18f06 201 if (local_err) {
92f1deec 202 error_propagate(errp, local_err);
cc84d90f
HR
203 }
204 return ret;
1565262c 205}
01dd96d8 206
015a1036
HR
207static int raw_open(BlockDriverState *bs, QDict *options, int flags,
208 Error **errp)
01dd96d8
LE
209{
210 bs->sg = bs->file->sg;
38f3ef57
KW
211
212 if (bs->probed && !bdrv_is_read_only(bs)) {
213 fprintf(stderr,
214 "WARNING: Image format was not specified for '%s' and probing "
215 "guessed raw.\n"
216 " Automatically detecting the format is dangerous for "
217 "raw images, write operations on block 0 will be restricted.\n"
218 " Specify the 'raw' format explicitly to remove the "
219 "restrictions.\n",
220 bs->file->filename);
221 }
222
01dd96d8
LE
223 return 0;
224}
225
7a6d3fc5 226static void raw_close(BlockDriverState *bs)
01dd96d8
LE
227{
228}
229
7a6d3fc5 230static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
01dd96d8
LE
231{
232 /* smallest possible positive score so that raw is used if and only if no
233 * other block driver works
234 */
235 return 1;
236}
775d6afd 237
5f535a94 238BlockDriver bdrv_raw = {
775d6afd
LE
239 .format_name = "raw",
240 .bdrv_probe = &raw_probe,
241 .bdrv_reopen_prepare = &raw_reopen_prepare,
242 .bdrv_open = &raw_open,
243 .bdrv_close = &raw_close,
c282e1fd 244 .bdrv_create = &raw_create,
775d6afd
LE
245 .bdrv_co_readv = &raw_co_readv,
246 .bdrv_co_writev = &raw_co_writev,
247 .bdrv_co_write_zeroes = &raw_co_write_zeroes,
248 .bdrv_co_discard = &raw_co_discard,
b6b8a333 249 .bdrv_co_get_block_status = &raw_co_get_block_status,
775d6afd
LE
250 .bdrv_truncate = &raw_truncate,
251 .bdrv_getlength = &raw_getlength,
b94a2610 252 .has_variable_length = true,
775d6afd 253 .bdrv_get_info = &raw_get_info,
ad6aef43 254 .bdrv_refresh_limits = &raw_refresh_limits,
775d6afd
LE
255 .bdrv_is_inserted = &raw_is_inserted,
256 .bdrv_media_changed = &raw_media_changed,
257 .bdrv_eject = &raw_eject,
258 .bdrv_lock_medium = &raw_lock_medium,
259 .bdrv_ioctl = &raw_ioctl,
260 .bdrv_aio_ioctl = &raw_aio_ioctl,
cd3a4cf6 261 .create_opts = &raw_create_opts,
775d6afd
LE
262 .bdrv_has_zero_init = &raw_has_zero_init
263};
264
265static void bdrv_raw_init(void)
266{
267 bdrv_register(&bdrv_raw);
268}
269
270block_init(bdrv_raw_init);