root / block.c @ d3c61721
History | View | Annotate | Download (14.7 kB)
1 | fc01f7e7 | bellard | /*
|
---|---|---|---|
2 | fc01f7e7 | bellard | * QEMU System Emulator block driver
|
3 | fc01f7e7 | bellard | *
|
4 | fc01f7e7 | bellard | * Copyright (c) 2003 Fabrice Bellard
|
5 | fc01f7e7 | bellard | *
|
6 | fc01f7e7 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | fc01f7e7 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | fc01f7e7 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | fc01f7e7 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | fc01f7e7 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | fc01f7e7 | bellard | * furnished to do so, subject to the following conditions:
|
12 | fc01f7e7 | bellard | *
|
13 | fc01f7e7 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | fc01f7e7 | bellard | * all copies or substantial portions of the Software.
|
15 | fc01f7e7 | bellard | *
|
16 | fc01f7e7 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | fc01f7e7 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | fc01f7e7 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | fc01f7e7 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | fc01f7e7 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | fc01f7e7 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | fc01f7e7 | bellard | * THE SOFTWARE.
|
23 | fc01f7e7 | bellard | */
|
24 | fc01f7e7 | bellard | #include "vl.h" |
25 | ea2384d3 | bellard | #include "block_int.h" |
26 | fc01f7e7 | bellard | |
27 | b338082b | bellard | static BlockDriverState *bdrv_first;
|
28 | ea2384d3 | bellard | static BlockDriver *first_drv;
|
29 | ea2384d3 | bellard | |
30 | ea2384d3 | bellard | void bdrv_register(BlockDriver *bdrv)
|
31 | ea2384d3 | bellard | { |
32 | ea2384d3 | bellard | bdrv->next = first_drv; |
33 | ea2384d3 | bellard | first_drv = bdrv; |
34 | ea2384d3 | bellard | } |
35 | b338082b | bellard | |
36 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
37 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
38 | b338082b | bellard | { |
39 | b338082b | bellard | BlockDriverState **pbs, *bs; |
40 | b338082b | bellard | |
41 | b338082b | bellard | bs = qemu_mallocz(sizeof(BlockDriverState));
|
42 | b338082b | bellard | if(!bs)
|
43 | b338082b | bellard | return NULL; |
44 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
45 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
46 | ea2384d3 | bellard | /* insert at the end */
|
47 | ea2384d3 | bellard | pbs = &bdrv_first; |
48 | ea2384d3 | bellard | while (*pbs != NULL) |
49 | ea2384d3 | bellard | pbs = &(*pbs)->next; |
50 | ea2384d3 | bellard | *pbs = bs; |
51 | ea2384d3 | bellard | } |
52 | b338082b | bellard | return bs;
|
53 | b338082b | bellard | } |
54 | b338082b | bellard | |
55 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
56 | ea2384d3 | bellard | { |
57 | ea2384d3 | bellard | BlockDriver *drv1; |
58 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
59 | ea2384d3 | bellard | if (!strcmp(drv1->format_name, format_name))
|
60 | ea2384d3 | bellard | return drv1;
|
61 | ea2384d3 | bellard | } |
62 | ea2384d3 | bellard | return NULL; |
63 | ea2384d3 | bellard | } |
64 | ea2384d3 | bellard | |
65 | ea2384d3 | bellard | int bdrv_create(BlockDriver *drv,
|
66 | ea2384d3 | bellard | const char *filename, int64_t size_in_sectors, |
67 | ea2384d3 | bellard | const char *backing_file, int flags) |
68 | ea2384d3 | bellard | { |
69 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
70 | ea2384d3 | bellard | return -ENOTSUP;
|
71 | ea2384d3 | bellard | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
|
72 | ea2384d3 | bellard | } |
73 | ea2384d3 | bellard | |
74 | d5249393 | bellard | #ifdef _WIN32
|
75 | d5249393 | bellard | static void get_tmp_filename(char *filename, int size) |
76 | d5249393 | bellard | { |
77 | d5249393 | bellard | /* XXX: find a better function */
|
78 | d5249393 | bellard | tmpnam(filename); |
79 | d5249393 | bellard | } |
80 | d5249393 | bellard | #else
|
81 | ea2384d3 | bellard | static void get_tmp_filename(char *filename, int size) |
82 | fc01f7e7 | bellard | { |
83 | 67b915a5 | bellard | int fd;
|
84 | d5249393 | bellard | /* XXX: race condition possible */
|
85 | ea2384d3 | bellard | pstrcpy(filename, size, "/tmp/vl.XXXXXX");
|
86 | ea2384d3 | bellard | fd = mkstemp(filename); |
87 | ea2384d3 | bellard | close(fd); |
88 | ea2384d3 | bellard | } |
89 | d5249393 | bellard | #endif
|
90 | fc01f7e7 | bellard | |
91 | ea2384d3 | bellard | static BlockDriver *find_image_format(const char *filename) |
92 | ea2384d3 | bellard | { |
93 | ea2384d3 | bellard | int fd, ret, score, score_max;
|
94 | ea2384d3 | bellard | BlockDriver *drv1, *drv; |
95 | ea2384d3 | bellard | uint8_t buf[1024];
|
96 | 33e3963e | bellard | |
97 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
98 | ea2384d3 | bellard | if (fd < 0) |
99 | ea2384d3 | bellard | return NULL; |
100 | ea2384d3 | bellard | ret = read(fd, buf, sizeof(buf));
|
101 | ea2384d3 | bellard | if (ret < 0) { |
102 | ea2384d3 | bellard | close(fd); |
103 | ea2384d3 | bellard | return NULL; |
104 | ea2384d3 | bellard | } |
105 | ea2384d3 | bellard | close(fd); |
106 | ea2384d3 | bellard | |
107 | ea2384d3 | bellard | drv = NULL;
|
108 | ea2384d3 | bellard | score_max = 0;
|
109 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
110 | ea2384d3 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
111 | ea2384d3 | bellard | if (score > score_max) {
|
112 | ea2384d3 | bellard | score_max = score; |
113 | ea2384d3 | bellard | drv = drv1; |
114 | 0849bf08 | bellard | } |
115 | fc01f7e7 | bellard | } |
116 | ea2384d3 | bellard | return drv;
|
117 | ea2384d3 | bellard | } |
118 | ea2384d3 | bellard | |
119 | ea2384d3 | bellard | int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
120 | ea2384d3 | bellard | { |
121 | ea2384d3 | bellard | return bdrv_open2(bs, filename, snapshot, NULL); |
122 | ea2384d3 | bellard | } |
123 | ea2384d3 | bellard | |
124 | ea2384d3 | bellard | int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot, |
125 | ea2384d3 | bellard | BlockDriver *drv) |
126 | ea2384d3 | bellard | { |
127 | ea2384d3 | bellard | int ret;
|
128 | ea2384d3 | bellard | char tmp_filename[1024]; |
129 | ea2384d3 | bellard | |
130 | ea2384d3 | bellard | bs->read_only = 0;
|
131 | ea2384d3 | bellard | bs->is_temporary = 0;
|
132 | ea2384d3 | bellard | bs->encrypted = 0;
|
133 | ea2384d3 | bellard | |
134 | ea2384d3 | bellard | if (snapshot) {
|
135 | ea2384d3 | bellard | BlockDriverState *bs1; |
136 | ea2384d3 | bellard | int64_t total_size; |
137 | ea2384d3 | bellard | |
138 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
139 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
140 | 33e3963e | bellard | |
141 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
142 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
143 | ea2384d3 | bellard | if (!bs1) {
|
144 | ea2384d3 | bellard | return -1; |
145 | ea2384d3 | bellard | } |
146 | ea2384d3 | bellard | if (bdrv_open(bs1, filename, 0) < 0) { |
147 | ea2384d3 | bellard | bdrv_delete(bs1); |
148 | ea2384d3 | bellard | return -1; |
149 | ea2384d3 | bellard | } |
150 | ea2384d3 | bellard | total_size = bs1->total_sectors; |
151 | ea2384d3 | bellard | bdrv_delete(bs1); |
152 | ea2384d3 | bellard | |
153 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
154 | ea2384d3 | bellard | /* XXX: use cow for linux as it is more efficient ? */
|
155 | ea2384d3 | bellard | if (bdrv_create(&bdrv_qcow, tmp_filename,
|
156 | ea2384d3 | bellard | total_size, filename, 0) < 0) { |
157 | ea2384d3 | bellard | return -1; |
158 | ea2384d3 | bellard | } |
159 | ea2384d3 | bellard | filename = tmp_filename; |
160 | ea2384d3 | bellard | bs->is_temporary = 1;
|
161 | ea2384d3 | bellard | } |
162 | ea2384d3 | bellard | |
163 | ea2384d3 | bellard | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
164 | ea2384d3 | bellard | if (!drv) {
|
165 | ea2384d3 | bellard | drv = find_image_format(filename); |
166 | ea2384d3 | bellard | if (!drv)
|
167 | ea2384d3 | bellard | return -1; |
168 | ea2384d3 | bellard | } |
169 | ea2384d3 | bellard | bs->drv = drv; |
170 | ea2384d3 | bellard | bs->opaque = qemu_mallocz(drv->instance_size); |
171 | ea2384d3 | bellard | if (bs->opaque == NULL && drv->instance_size > 0) |
172 | ea2384d3 | bellard | return -1; |
173 | ea2384d3 | bellard | |
174 | ea2384d3 | bellard | ret = drv->bdrv_open(bs, filename); |
175 | ea2384d3 | bellard | if (ret < 0) { |
176 | ea2384d3 | bellard | qemu_free(bs->opaque); |
177 | ea2384d3 | bellard | return -1; |
178 | 33e3963e | bellard | } |
179 | 67b915a5 | bellard | #ifndef _WIN32
|
180 | ea2384d3 | bellard | if (bs->is_temporary) {
|
181 | ea2384d3 | bellard | unlink(filename); |
182 | ea2384d3 | bellard | } |
183 | ea2384d3 | bellard | #endif
|
184 | ea2384d3 | bellard | if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) { |
185 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
186 | ea2384d3 | bellard | bs->backing_hd = bdrv_new("");
|
187 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
188 | ea2384d3 | bellard | fail:
|
189 | ea2384d3 | bellard | bdrv_close(bs); |
190 | ea2384d3 | bellard | return -1; |
191 | 33e3963e | bellard | } |
192 | ea2384d3 | bellard | if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0) |
193 | 33e3963e | bellard | goto fail;
|
194 | 33e3963e | bellard | } |
195 | 33e3963e | bellard | |
196 | b338082b | bellard | bs->inserted = 1;
|
197 | b338082b | bellard | |
198 | b338082b | bellard | /* call the change callback */
|
199 | b338082b | bellard | if (bs->change_cb)
|
200 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
201 | b338082b | bellard | |
202 | b338082b | bellard | return 0; |
203 | fc01f7e7 | bellard | } |
204 | fc01f7e7 | bellard | |
205 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
206 | fc01f7e7 | bellard | { |
207 | b338082b | bellard | if (bs->inserted) {
|
208 | ea2384d3 | bellard | if (bs->backing_hd)
|
209 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
210 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
211 | ea2384d3 | bellard | qemu_free(bs->opaque); |
212 | ea2384d3 | bellard | #ifdef _WIN32
|
213 | ea2384d3 | bellard | if (bs->is_temporary) {
|
214 | ea2384d3 | bellard | unlink(bs->filename); |
215 | ea2384d3 | bellard | } |
216 | 67b915a5 | bellard | #endif
|
217 | ea2384d3 | bellard | bs->opaque = NULL;
|
218 | ea2384d3 | bellard | bs->drv = NULL;
|
219 | b338082b | bellard | bs->inserted = 0;
|
220 | b338082b | bellard | |
221 | b338082b | bellard | /* call the change callback */
|
222 | b338082b | bellard | if (bs->change_cb)
|
223 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
224 | b338082b | bellard | } |
225 | b338082b | bellard | } |
226 | b338082b | bellard | |
227 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
228 | b338082b | bellard | { |
229 | ea2384d3 | bellard | /* XXX: remove the driver list */
|
230 | b338082b | bellard | bdrv_close(bs); |
231 | b338082b | bellard | qemu_free(bs); |
232 | fc01f7e7 | bellard | } |
233 | fc01f7e7 | bellard | |
234 | 33e3963e | bellard | /* commit COW file into the raw image */
|
235 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
236 | 33e3963e | bellard | { |
237 | 33e3963e | bellard | int64_t i; |
238 | ea2384d3 | bellard | int n, j;
|
239 | ea2384d3 | bellard | unsigned char sector[512]; |
240 | 33e3963e | bellard | |
241 | b338082b | bellard | if (!bs->inserted)
|
242 | ea2384d3 | bellard | return -ENOENT;
|
243 | 33e3963e | bellard | |
244 | 33e3963e | bellard | if (bs->read_only) {
|
245 | ea2384d3 | bellard | return -EACCES;
|
246 | 33e3963e | bellard | } |
247 | 33e3963e | bellard | |
248 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
249 | ea2384d3 | bellard | return -ENOTSUP;
|
250 | ea2384d3 | bellard | } |
251 | 33e3963e | bellard | |
252 | ea2384d3 | bellard | for (i = 0; i < bs->total_sectors;) { |
253 | ea2384d3 | bellard | if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
254 | ea2384d3 | bellard | for(j = 0; j < n; j++) { |
255 | ea2384d3 | bellard | if (bdrv_read(bs, i, sector, 1) != 0) { |
256 | ea2384d3 | bellard | return -EIO;
|
257 | ea2384d3 | bellard | } |
258 | ea2384d3 | bellard | |
259 | ea2384d3 | bellard | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
260 | ea2384d3 | bellard | return -EIO;
|
261 | ea2384d3 | bellard | } |
262 | ea2384d3 | bellard | i++; |
263 | 33e3963e | bellard | } |
264 | ea2384d3 | bellard | } else {
|
265 | ea2384d3 | bellard | i += n; |
266 | ea2384d3 | bellard | } |
267 | 33e3963e | bellard | } |
268 | 33e3963e | bellard | return 0; |
269 | 33e3963e | bellard | } |
270 | 33e3963e | bellard | |
271 | fc01f7e7 | bellard | /* return -1 if error */
|
272 | fc01f7e7 | bellard | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
273 | fc01f7e7 | bellard | uint8_t *buf, int nb_sectors)
|
274 | fc01f7e7 | bellard | { |
275 | ea2384d3 | bellard | int ret, n;
|
276 | ea2384d3 | bellard | BlockDriver *drv = bs->drv; |
277 | ea2384d3 | bellard | |
278 | b338082b | bellard | if (!bs->inserted)
|
279 | b338082b | bellard | return -1; |
280 | b338082b | bellard | |
281 | 33e3963e | bellard | while (nb_sectors > 0) { |
282 | ea2384d3 | bellard | if (sector_num == 0 && bs->boot_sector_enabled) { |
283 | cf98951b | bellard | memcpy(buf, bs->boot_sector_data, 512);
|
284 | cf98951b | bellard | n = 1;
|
285 | ea2384d3 | bellard | } else if (bs->backing_hd) { |
286 | ea2384d3 | bellard | if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
|
287 | ea2384d3 | bellard | ret = drv->bdrv_read(bs, sector_num, buf, n); |
288 | ea2384d3 | bellard | if (ret < 0) |
289 | ea2384d3 | bellard | return -1; |
290 | ea2384d3 | bellard | } else {
|
291 | ea2384d3 | bellard | /* read from the base image */
|
292 | ea2384d3 | bellard | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); |
293 | ea2384d3 | bellard | if (ret < 0) |
294 | ea2384d3 | bellard | return -1; |
295 | ea2384d3 | bellard | } |
296 | 33e3963e | bellard | } else {
|
297 | ea2384d3 | bellard | ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
298 | ea2384d3 | bellard | if (ret < 0) |
299 | 33e3963e | bellard | return -1; |
300 | ea2384d3 | bellard | /* no need to loop */
|
301 | ea2384d3 | bellard | break;
|
302 | 33e3963e | bellard | } |
303 | 33e3963e | bellard | nb_sectors -= n; |
304 | 33e3963e | bellard | sector_num += n; |
305 | 33e3963e | bellard | buf += n * 512;
|
306 | 33e3963e | bellard | } |
307 | 33e3963e | bellard | return 0; |
308 | fc01f7e7 | bellard | } |
309 | fc01f7e7 | bellard | |
310 | fc01f7e7 | bellard | /* return -1 if error */
|
311 | fc01f7e7 | bellard | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
312 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
313 | fc01f7e7 | bellard | { |
314 | b338082b | bellard | if (!bs->inserted)
|
315 | b338082b | bellard | return -1; |
316 | 0849bf08 | bellard | if (bs->read_only)
|
317 | 0849bf08 | bellard | return -1; |
318 | ea2384d3 | bellard | return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
|
319 | fc01f7e7 | bellard | } |
320 | fc01f7e7 | bellard | |
321 | fc01f7e7 | bellard | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
|
322 | fc01f7e7 | bellard | { |
323 | fc01f7e7 | bellard | *nb_sectors_ptr = bs->total_sectors; |
324 | fc01f7e7 | bellard | } |
325 | cf98951b | bellard | |
326 | cf98951b | bellard | /* force a given boot sector. */
|
327 | cf98951b | bellard | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) |
328 | cf98951b | bellard | { |
329 | cf98951b | bellard | bs->boot_sector_enabled = 1;
|
330 | cf98951b | bellard | if (size > 512) |
331 | cf98951b | bellard | size = 512;
|
332 | cf98951b | bellard | memcpy(bs->boot_sector_data, data, size); |
333 | cf98951b | bellard | memset(bs->boot_sector_data + size, 0, 512 - size); |
334 | cf98951b | bellard | } |
335 | b338082b | bellard | |
336 | b338082b | bellard | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
337 | b338082b | bellard | int cyls, int heads, int secs) |
338 | b338082b | bellard | { |
339 | b338082b | bellard | bs->cyls = cyls; |
340 | b338082b | bellard | bs->heads = heads; |
341 | b338082b | bellard | bs->secs = secs; |
342 | b338082b | bellard | } |
343 | b338082b | bellard | |
344 | b338082b | bellard | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
345 | b338082b | bellard | { |
346 | b338082b | bellard | bs->type = type; |
347 | b338082b | bellard | bs->removable = ((type == BDRV_TYPE_CDROM || |
348 | b338082b | bellard | type == BDRV_TYPE_FLOPPY)); |
349 | b338082b | bellard | } |
350 | b338082b | bellard | |
351 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
352 | 46d4767d | bellard | { |
353 | 46d4767d | bellard | bs->translation = translation; |
354 | 46d4767d | bellard | } |
355 | 46d4767d | bellard | |
356 | b338082b | bellard | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
357 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
358 | b338082b | bellard | { |
359 | b338082b | bellard | *pcyls = bs->cyls; |
360 | b338082b | bellard | *pheads = bs->heads; |
361 | b338082b | bellard | *psecs = bs->secs; |
362 | b338082b | bellard | } |
363 | b338082b | bellard | |
364 | b338082b | bellard | int bdrv_get_type_hint(BlockDriverState *bs)
|
365 | b338082b | bellard | { |
366 | b338082b | bellard | return bs->type;
|
367 | b338082b | bellard | } |
368 | b338082b | bellard | |
369 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
370 | 46d4767d | bellard | { |
371 | 46d4767d | bellard | return bs->translation;
|
372 | 46d4767d | bellard | } |
373 | 46d4767d | bellard | |
374 | b338082b | bellard | int bdrv_is_removable(BlockDriverState *bs)
|
375 | b338082b | bellard | { |
376 | b338082b | bellard | return bs->removable;
|
377 | b338082b | bellard | } |
378 | b338082b | bellard | |
379 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
380 | b338082b | bellard | { |
381 | b338082b | bellard | return bs->read_only;
|
382 | b338082b | bellard | } |
383 | b338082b | bellard | |
384 | b338082b | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
385 | b338082b | bellard | { |
386 | b338082b | bellard | return bs->inserted;
|
387 | b338082b | bellard | } |
388 | b338082b | bellard | |
389 | b338082b | bellard | int bdrv_is_locked(BlockDriverState *bs)
|
390 | b338082b | bellard | { |
391 | b338082b | bellard | return bs->locked;
|
392 | b338082b | bellard | } |
393 | b338082b | bellard | |
394 | b338082b | bellard | void bdrv_set_locked(BlockDriverState *bs, int locked) |
395 | b338082b | bellard | { |
396 | b338082b | bellard | bs->locked = locked; |
397 | b338082b | bellard | } |
398 | b338082b | bellard | |
399 | b338082b | bellard | void bdrv_set_change_cb(BlockDriverState *bs,
|
400 | b338082b | bellard | void (*change_cb)(void *opaque), void *opaque) |
401 | b338082b | bellard | { |
402 | b338082b | bellard | bs->change_cb = change_cb; |
403 | b338082b | bellard | bs->change_opaque = opaque; |
404 | b338082b | bellard | } |
405 | b338082b | bellard | |
406 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
407 | ea2384d3 | bellard | { |
408 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
409 | ea2384d3 | bellard | return 1; |
410 | ea2384d3 | bellard | return bs->encrypted;
|
411 | ea2384d3 | bellard | } |
412 | ea2384d3 | bellard | |
413 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
414 | ea2384d3 | bellard | { |
415 | ea2384d3 | bellard | int ret;
|
416 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
417 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
418 | ea2384d3 | bellard | if (ret < 0) |
419 | ea2384d3 | bellard | return ret;
|
420 | ea2384d3 | bellard | if (!bs->encrypted)
|
421 | ea2384d3 | bellard | return 0; |
422 | ea2384d3 | bellard | } |
423 | ea2384d3 | bellard | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
|
424 | ea2384d3 | bellard | return -1; |
425 | ea2384d3 | bellard | return bs->drv->bdrv_set_key(bs, key);
|
426 | ea2384d3 | bellard | } |
427 | ea2384d3 | bellard | |
428 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
429 | ea2384d3 | bellard | { |
430 | ea2384d3 | bellard | if (!bs->inserted || !bs->drv) {
|
431 | ea2384d3 | bellard | buf[0] = '\0'; |
432 | ea2384d3 | bellard | } else {
|
433 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
434 | ea2384d3 | bellard | } |
435 | ea2384d3 | bellard | } |
436 | ea2384d3 | bellard | |
437 | ea2384d3 | bellard | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
438 | ea2384d3 | bellard | void *opaque)
|
439 | ea2384d3 | bellard | { |
440 | ea2384d3 | bellard | BlockDriver *drv; |
441 | ea2384d3 | bellard | |
442 | ea2384d3 | bellard | for (drv = first_drv; drv != NULL; drv = drv->next) { |
443 | ea2384d3 | bellard | it(opaque, drv->format_name); |
444 | ea2384d3 | bellard | } |
445 | ea2384d3 | bellard | } |
446 | ea2384d3 | bellard | |
447 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
448 | b338082b | bellard | { |
449 | b338082b | bellard | BlockDriverState *bs; |
450 | b338082b | bellard | |
451 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
452 | b338082b | bellard | if (!strcmp(name, bs->device_name))
|
453 | b338082b | bellard | return bs;
|
454 | b338082b | bellard | } |
455 | b338082b | bellard | return NULL; |
456 | b338082b | bellard | } |
457 | b338082b | bellard | |
458 | 81d0912d | bellard | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
459 | 81d0912d | bellard | { |
460 | 81d0912d | bellard | BlockDriverState *bs; |
461 | 81d0912d | bellard | |
462 | 81d0912d | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
463 | 81d0912d | bellard | it(opaque, bs->device_name); |
464 | 81d0912d | bellard | } |
465 | 81d0912d | bellard | } |
466 | 81d0912d | bellard | |
467 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
468 | ea2384d3 | bellard | { |
469 | ea2384d3 | bellard | return bs->device_name;
|
470 | ea2384d3 | bellard | } |
471 | ea2384d3 | bellard | |
472 | b338082b | bellard | void bdrv_info(void) |
473 | b338082b | bellard | { |
474 | b338082b | bellard | BlockDriverState *bs; |
475 | b338082b | bellard | |
476 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
477 | b338082b | bellard | term_printf("%s:", bs->device_name);
|
478 | b338082b | bellard | term_printf(" type=");
|
479 | b338082b | bellard | switch(bs->type) {
|
480 | b338082b | bellard | case BDRV_TYPE_HD:
|
481 | b338082b | bellard | term_printf("hd");
|
482 | b338082b | bellard | break;
|
483 | b338082b | bellard | case BDRV_TYPE_CDROM:
|
484 | b338082b | bellard | term_printf("cdrom");
|
485 | b338082b | bellard | break;
|
486 | b338082b | bellard | case BDRV_TYPE_FLOPPY:
|
487 | b338082b | bellard | term_printf("floppy");
|
488 | b338082b | bellard | break;
|
489 | b338082b | bellard | } |
490 | b338082b | bellard | term_printf(" removable=%d", bs->removable);
|
491 | b338082b | bellard | if (bs->removable) {
|
492 | b338082b | bellard | term_printf(" locked=%d", bs->locked);
|
493 | b338082b | bellard | } |
494 | b338082b | bellard | if (bs->inserted) {
|
495 | b338082b | bellard | term_printf(" file=%s", bs->filename);
|
496 | ea2384d3 | bellard | if (bs->backing_file[0] != '\0') |
497 | ea2384d3 | bellard | term_printf(" backing_file=%s", bs->backing_file);
|
498 | b338082b | bellard | term_printf(" ro=%d", bs->read_only);
|
499 | ea2384d3 | bellard | term_printf(" drv=%s", bs->drv->format_name);
|
500 | ea2384d3 | bellard | if (bs->encrypted)
|
501 | ea2384d3 | bellard | term_printf(" encrypted");
|
502 | b338082b | bellard | } else {
|
503 | b338082b | bellard | term_printf(" [not inserted]");
|
504 | b338082b | bellard | } |
505 | b338082b | bellard | term_printf("\n");
|
506 | b338082b | bellard | } |
507 | b338082b | bellard | } |
508 | ea2384d3 | bellard | |
509 | ea2384d3 | bellard | |
510 | ea2384d3 | bellard | /**************************************************************/
|
511 | ea2384d3 | bellard | /* RAW block driver */
|
512 | ea2384d3 | bellard | |
513 | ea2384d3 | bellard | typedef struct BDRVRawState { |
514 | ea2384d3 | bellard | int fd;
|
515 | ea2384d3 | bellard | } BDRVRawState; |
516 | ea2384d3 | bellard | |
517 | ea2384d3 | bellard | static int raw_probe(const uint8_t *buf, int buf_size, const char *filename) |
518 | ea2384d3 | bellard | { |
519 | ea2384d3 | bellard | return 1; /* maybe */ |
520 | ea2384d3 | bellard | } |
521 | ea2384d3 | bellard | |
522 | ea2384d3 | bellard | static int raw_open(BlockDriverState *bs, const char *filename) |
523 | ea2384d3 | bellard | { |
524 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
525 | ea2384d3 | bellard | int fd;
|
526 | ea2384d3 | bellard | int64_t size; |
527 | ea2384d3 | bellard | |
528 | ea2384d3 | bellard | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
529 | ea2384d3 | bellard | if (fd < 0) { |
530 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
531 | ea2384d3 | bellard | if (fd < 0) |
532 | ea2384d3 | bellard | return -1; |
533 | ea2384d3 | bellard | bs->read_only = 1;
|
534 | ea2384d3 | bellard | } |
535 | d5249393 | bellard | size = lseek(fd, 0, SEEK_END);
|
536 | ea2384d3 | bellard | bs->total_sectors = size / 512;
|
537 | ea2384d3 | bellard | s->fd = fd; |
538 | ea2384d3 | bellard | return 0; |
539 | ea2384d3 | bellard | } |
540 | ea2384d3 | bellard | |
541 | ea2384d3 | bellard | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
542 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
543 | ea2384d3 | bellard | { |
544 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
545 | ea2384d3 | bellard | int ret;
|
546 | ea2384d3 | bellard | |
547 | d5249393 | bellard | lseek(s->fd, sector_num * 512, SEEK_SET);
|
548 | ea2384d3 | bellard | ret = read(s->fd, buf, nb_sectors * 512);
|
549 | ea2384d3 | bellard | if (ret != nb_sectors * 512) |
550 | ea2384d3 | bellard | return -1; |
551 | ea2384d3 | bellard | return 0; |
552 | ea2384d3 | bellard | } |
553 | ea2384d3 | bellard | |
554 | ea2384d3 | bellard | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
555 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
556 | ea2384d3 | bellard | { |
557 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
558 | ea2384d3 | bellard | int ret;
|
559 | ea2384d3 | bellard | |
560 | d5249393 | bellard | lseek(s->fd, sector_num * 512, SEEK_SET);
|
561 | ea2384d3 | bellard | ret = write(s->fd, buf, nb_sectors * 512);
|
562 | ea2384d3 | bellard | if (ret != nb_sectors * 512) |
563 | ea2384d3 | bellard | return -1; |
564 | ea2384d3 | bellard | return 0; |
565 | ea2384d3 | bellard | } |
566 | ea2384d3 | bellard | |
567 | e2731add | bellard | static void raw_close(BlockDriverState *bs) |
568 | ea2384d3 | bellard | { |
569 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
570 | ea2384d3 | bellard | close(s->fd); |
571 | ea2384d3 | bellard | } |
572 | ea2384d3 | bellard | |
573 | ea2384d3 | bellard | static int raw_create(const char *filename, int64_t total_size, |
574 | ea2384d3 | bellard | const char *backing_file, int flags) |
575 | ea2384d3 | bellard | { |
576 | ea2384d3 | bellard | int fd;
|
577 | ea2384d3 | bellard | |
578 | ea2384d3 | bellard | if (flags || backing_file)
|
579 | ea2384d3 | bellard | return -ENOTSUP;
|
580 | ea2384d3 | bellard | |
581 | ea2384d3 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
582 | ea2384d3 | bellard | 0644);
|
583 | ea2384d3 | bellard | if (fd < 0) |
584 | ea2384d3 | bellard | return -EIO;
|
585 | d5249393 | bellard | ftruncate(fd, total_size * 512);
|
586 | ea2384d3 | bellard | close(fd); |
587 | ea2384d3 | bellard | return 0; |
588 | ea2384d3 | bellard | } |
589 | ea2384d3 | bellard | |
590 | ea2384d3 | bellard | BlockDriver bdrv_raw = { |
591 | ea2384d3 | bellard | "raw",
|
592 | ea2384d3 | bellard | sizeof(BDRVRawState),
|
593 | ea2384d3 | bellard | raw_probe, |
594 | ea2384d3 | bellard | raw_open, |
595 | ea2384d3 | bellard | raw_read, |
596 | ea2384d3 | bellard | raw_write, |
597 | ea2384d3 | bellard | raw_close, |
598 | ea2384d3 | bellard | raw_create, |
599 | ea2384d3 | bellard | }; |
600 | ea2384d3 | bellard | |
601 | ea2384d3 | bellard | void bdrv_init(void) |
602 | ea2384d3 | bellard | { |
603 | ea2384d3 | bellard | bdrv_register(&bdrv_raw); |
604 | ea2384d3 | bellard | #ifndef _WIN32
|
605 | ea2384d3 | bellard | bdrv_register(&bdrv_cow); |
606 | ea2384d3 | bellard | #endif
|
607 | ea2384d3 | bellard | bdrv_register(&bdrv_qcow); |
608 | ea2384d3 | bellard | bdrv_register(&bdrv_vmdk); |
609 | 3c56521b | bellard | bdrv_register(&bdrv_cloop); |
610 | 585d0ed9 | bellard | bdrv_register(&bdrv_dmg); |
611 | ea2384d3 | bellard | } |