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>
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
29 #include "qemu/osdep.h"
30 #include "block/block_int.h"
31 #include "qemu/option.h"
33 static QemuOptsList raw_create_opts
= {
34 .name
= "raw-create-opts",
35 .head
= QTAILQ_HEAD_INITIALIZER(raw_create_opts
.head
),
38 .name
= BLOCK_OPT_SIZE
,
39 .type
= QEMU_OPT_SIZE
,
40 .help
= "Virtual disk size"
46 static int raw_reopen_prepare(BDRVReopenState
*reopen_state
,
47 BlockReopenQueue
*queue
, Error
**errp
)
52 static int coroutine_fn
raw_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
53 int nb_sectors
, QEMUIOVector
*qiov
)
55 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
56 return bdrv_co_readv(bs
->file
->bs
, sector_num
, nb_sectors
, qiov
);
59 static int coroutine_fn
raw_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
60 int nb_sectors
, QEMUIOVector
*qiov
)
64 QEMUIOVector local_qiov
;
67 if (bs
->probed
&& sector_num
== 0) {
68 /* As long as these conditions are true, we can't get partial writes to
69 * the probe buffer and can just directly check the request. */
70 QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE
!= 512);
71 QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE
!= 512);
73 if (nb_sectors
== 0) {
74 /* qemu_iovec_to_buf() would fail, but we want to return success
75 * instead of -EINVAL in this case. */
79 buf
= qemu_try_blockalign(bs
->file
->bs
, 512);
85 ret
= qemu_iovec_to_buf(qiov
, 0, buf
, 512);
91 drv
= bdrv_probe_all(buf
, 512, NULL
);
97 /* Use the checked buffer, a malicious guest might be overwriting its
98 * original buffer in the background. */
99 qemu_iovec_init(&local_qiov
, qiov
->niov
+ 1);
100 qemu_iovec_add(&local_qiov
, buf
, 512);
101 qemu_iovec_concat(&local_qiov
, qiov
, 512, qiov
->size
- 512);
105 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
106 ret
= bdrv_co_writev(bs
->file
->bs
, sector_num
, nb_sectors
, qiov
);
109 if (qiov
== &local_qiov
) {
110 qemu_iovec_destroy(&local_qiov
);
116 static int64_t coroutine_fn
raw_co_get_block_status(BlockDriverState
*bs
,
118 int nb_sectors
, int *pnum
)
121 return BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
| BDRV_BLOCK_DATA
|
122 (sector_num
<< BDRV_SECTOR_BITS
);
125 static int coroutine_fn
raw_co_write_zeroes(BlockDriverState
*bs
,
126 int64_t sector_num
, int nb_sectors
,
127 BdrvRequestFlags flags
)
129 return bdrv_co_write_zeroes(bs
->file
->bs
, sector_num
, nb_sectors
, flags
);
132 static int coroutine_fn
raw_co_discard(BlockDriverState
*bs
,
133 int64_t sector_num
, int nb_sectors
)
135 return bdrv_co_discard(bs
->file
->bs
, sector_num
, nb_sectors
);
138 static int64_t raw_getlength(BlockDriverState
*bs
)
140 return bdrv_getlength(bs
->file
->bs
);
143 static int raw_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
145 return bdrv_get_info(bs
->file
->bs
, bdi
);
148 static void raw_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
150 bs
->bl
= bs
->file
->bs
->bl
;
153 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
155 return bdrv_truncate(bs
->file
->bs
, offset
);
158 static int raw_media_changed(BlockDriverState
*bs
)
160 return bdrv_media_changed(bs
->file
->bs
);
163 static void raw_eject(BlockDriverState
*bs
, bool eject_flag
)
165 bdrv_eject(bs
->file
->bs
, eject_flag
);
168 static void raw_lock_medium(BlockDriverState
*bs
, bool locked
)
170 bdrv_lock_medium(bs
->file
->bs
, locked
);
173 static BlockAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
174 unsigned long int req
, void *buf
,
175 BlockCompletionFunc
*cb
,
178 return bdrv_aio_ioctl(bs
->file
->bs
, req
, buf
, cb
, opaque
);
181 static int raw_has_zero_init(BlockDriverState
*bs
)
183 return bdrv_has_zero_init(bs
->file
->bs
);
186 static int raw_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
188 Error
*local_err
= NULL
;
191 ret
= bdrv_create_file(filename
, opts
, &local_err
);
193 error_propagate(errp
, local_err
);
198 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
201 bs
->sg
= bs
->file
->bs
->sg
;
203 if (bs
->probed
&& !bdrv_is_read_only(bs
)) {
205 "WARNING: Image format was not specified for '%s' and probing "
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 "
211 bs
->file
->bs
->filename
);
217 static void raw_close(BlockDriverState
*bs
)
221 static int raw_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
223 /* smallest possible positive score so that raw is used if and only if no
224 * other block driver works
229 static int raw_probe_blocksizes(BlockDriverState
*bs
, BlockSizes
*bsz
)
231 return bdrv_probe_blocksizes(bs
->file
->bs
, bsz
);
234 static int raw_probe_geometry(BlockDriverState
*bs
, HDGeometry
*geo
)
236 return bdrv_probe_geometry(bs
->file
->bs
, geo
);
239 BlockDriver bdrv_raw
= {
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
,
245 .bdrv_create
= &raw_create
,
246 .bdrv_co_readv
= &raw_co_readv
,
247 .bdrv_co_writev
= &raw_co_writev
,
248 .bdrv_co_write_zeroes
= &raw_co_write_zeroes
,
249 .bdrv_co_discard
= &raw_co_discard
,
250 .bdrv_co_get_block_status
= &raw_co_get_block_status
,
251 .bdrv_truncate
= &raw_truncate
,
252 .bdrv_getlength
= &raw_getlength
,
253 .has_variable_length
= true,
254 .bdrv_get_info
= &raw_get_info
,
255 .bdrv_refresh_limits
= &raw_refresh_limits
,
256 .bdrv_probe_blocksizes
= &raw_probe_blocksizes
,
257 .bdrv_probe_geometry
= &raw_probe_geometry
,
258 .bdrv_media_changed
= &raw_media_changed
,
259 .bdrv_eject
= &raw_eject
,
260 .bdrv_lock_medium
= &raw_lock_medium
,
261 .bdrv_aio_ioctl
= &raw_aio_ioctl
,
262 .create_opts
= &raw_create_opts
,
263 .bdrv_has_zero_init
= &raw_has_zero_init
266 static void bdrv_raw_init(void)
268 bdrv_register(&bdrv_raw
);
271 block_init(bdrv_raw_init
);