2 * Block driver for RAW format
4 * Copyright (c) 2006 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "qemu-common.h"
26 #include "block/block_int.h"
27 #include "qemu/module.h"
29 static int raw_open(BlockDriverState
*bs
, QDict
*options
, int flags
)
31 bs
->sg
= bs
->file
->sg
;
35 /* We have nothing to do for raw reopen, stubs just return
37 static int raw_reopen_prepare(BDRVReopenState
*state
,
38 BlockReopenQueue
*queue
, Error
**errp
)
43 static int coroutine_fn
raw_co_readv(BlockDriverState
*bs
, int64_t sector_num
,
44 int nb_sectors
, QEMUIOVector
*qiov
)
46 BLKDBG_EVENT(bs
->file
, BLKDBG_READ_AIO
);
47 return bdrv_co_readv(bs
->file
, sector_num
, nb_sectors
, qiov
);
50 static int coroutine_fn
raw_co_writev(BlockDriverState
*bs
, int64_t sector_num
,
51 int nb_sectors
, QEMUIOVector
*qiov
)
53 BLKDBG_EVENT(bs
->file
, BLKDBG_WRITE_AIO
);
54 return bdrv_co_writev(bs
->file
, sector_num
, nb_sectors
, qiov
);
57 static void raw_close(BlockDriverState
*bs
)
61 static int coroutine_fn
raw_co_is_allocated(BlockDriverState
*bs
,
63 int nb_sectors
, int *pnum
)
65 return bdrv_co_is_allocated(bs
->file
, sector_num
, nb_sectors
, pnum
);
68 static int coroutine_fn
raw_co_write_zeroes(BlockDriverState
*bs
,
72 return bdrv_co_write_zeroes(bs
->file
, sector_num
, nb_sectors
);
75 static int64_t raw_getlength(BlockDriverState
*bs
)
77 return bdrv_getlength(bs
->file
);
80 static int raw_truncate(BlockDriverState
*bs
, int64_t offset
)
82 return bdrv_truncate(bs
->file
, offset
);
85 static int raw_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
87 return 1; /* everything can be opened as raw image */
90 static int coroutine_fn
raw_co_discard(BlockDriverState
*bs
,
91 int64_t sector_num
, int nb_sectors
)
93 return bdrv_co_discard(bs
->file
, sector_num
, nb_sectors
);
96 static int raw_is_inserted(BlockDriverState
*bs
)
98 return bdrv_is_inserted(bs
->file
);
101 static int raw_media_changed(BlockDriverState
*bs
)
103 return bdrv_media_changed(bs
->file
);
106 static void raw_eject(BlockDriverState
*bs
, bool eject_flag
)
108 bdrv_eject(bs
->file
, eject_flag
);
111 static void raw_lock_medium(BlockDriverState
*bs
, bool locked
)
113 bdrv_lock_medium(bs
->file
, locked
);
116 static int raw_ioctl(BlockDriverState
*bs
, unsigned long int req
, void *buf
)
118 return bdrv_ioctl(bs
->file
, req
, buf
);
121 static BlockDriverAIOCB
*raw_aio_ioctl(BlockDriverState
*bs
,
122 unsigned long int req
, void *buf
,
123 BlockDriverCompletionFunc
*cb
, void *opaque
)
125 return bdrv_aio_ioctl(bs
->file
, req
, buf
, cb
, opaque
);
128 static int raw_create(const char *filename
, QEMUOptionParameter
*options
)
130 return bdrv_create_file(filename
, options
);
133 static QEMUOptionParameter raw_create_options
[] = {
135 .name
= BLOCK_OPT_SIZE
,
137 .help
= "Virtual disk size"
142 static int raw_has_zero_init(BlockDriverState
*bs
)
144 return bdrv_has_zero_init(bs
->file
);
147 static int raw_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
149 return bdrv_get_info(bs
->file
, bdi
);
152 static BlockDriver bdrv_raw
= {
153 .format_name
= "raw",
155 /* It's really 0, but we need to make g_malloc() happy */
158 .bdrv_open
= raw_open
,
159 .bdrv_close
= raw_close
,
161 .bdrv_reopen_prepare
= raw_reopen_prepare
,
163 .bdrv_co_readv
= raw_co_readv
,
164 .bdrv_co_writev
= raw_co_writev
,
165 .bdrv_co_is_allocated
= raw_co_is_allocated
,
166 .bdrv_co_write_zeroes
= raw_co_write_zeroes
,
167 .bdrv_co_discard
= raw_co_discard
,
169 .bdrv_probe
= raw_probe
,
170 .bdrv_getlength
= raw_getlength
,
171 .bdrv_get_info
= raw_get_info
,
172 .bdrv_truncate
= raw_truncate
,
174 .bdrv_is_inserted
= raw_is_inserted
,
175 .bdrv_media_changed
= raw_media_changed
,
176 .bdrv_eject
= raw_eject
,
177 .bdrv_lock_medium
= raw_lock_medium
,
179 .bdrv_ioctl
= raw_ioctl
,
180 .bdrv_aio_ioctl
= raw_aio_ioctl
,
182 .bdrv_create
= raw_create
,
183 .create_options
= raw_create_options
,
184 .bdrv_has_zero_init
= raw_has_zero_init
,
187 static void bdrv_raw_init(void)
189 bdrv_register(&bdrv_raw
);
192 block_init(bdrv_raw_init
);