root / block.c @ 71be0fc3
History | View | Annotate | Download (15.9 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 | 7674e7bf | bellard | #ifdef _BSD
|
28 | 7674e7bf | bellard | #include <sys/types.h> |
29 | 7674e7bf | bellard | #include <sys/stat.h> |
30 | 7674e7bf | bellard | #include <sys/ioctl.h> |
31 | 7674e7bf | bellard | #include <sys/queue.h> |
32 | 7674e7bf | bellard | #include <sys/disk.h> |
33 | 7674e7bf | bellard | #endif
|
34 | 7674e7bf | bellard | |
35 | b338082b | bellard | static BlockDriverState *bdrv_first;
|
36 | ea2384d3 | bellard | static BlockDriver *first_drv;
|
37 | ea2384d3 | bellard | |
38 | ea2384d3 | bellard | void bdrv_register(BlockDriver *bdrv)
|
39 | ea2384d3 | bellard | { |
40 | ea2384d3 | bellard | bdrv->next = first_drv; |
41 | ea2384d3 | bellard | first_drv = bdrv; |
42 | ea2384d3 | bellard | } |
43 | b338082b | bellard | |
44 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
45 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
46 | b338082b | bellard | { |
47 | b338082b | bellard | BlockDriverState **pbs, *bs; |
48 | b338082b | bellard | |
49 | b338082b | bellard | bs = qemu_mallocz(sizeof(BlockDriverState));
|
50 | b338082b | bellard | if(!bs)
|
51 | b338082b | bellard | return NULL; |
52 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
53 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
54 | ea2384d3 | bellard | /* insert at the end */
|
55 | ea2384d3 | bellard | pbs = &bdrv_first; |
56 | ea2384d3 | bellard | while (*pbs != NULL) |
57 | ea2384d3 | bellard | pbs = &(*pbs)->next; |
58 | ea2384d3 | bellard | *pbs = bs; |
59 | ea2384d3 | bellard | } |
60 | b338082b | bellard | return bs;
|
61 | b338082b | bellard | } |
62 | b338082b | bellard | |
63 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
64 | ea2384d3 | bellard | { |
65 | ea2384d3 | bellard | BlockDriver *drv1; |
66 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
67 | ea2384d3 | bellard | if (!strcmp(drv1->format_name, format_name))
|
68 | ea2384d3 | bellard | return drv1;
|
69 | ea2384d3 | bellard | } |
70 | ea2384d3 | bellard | return NULL; |
71 | ea2384d3 | bellard | } |
72 | ea2384d3 | bellard | |
73 | ea2384d3 | bellard | int bdrv_create(BlockDriver *drv,
|
74 | ea2384d3 | bellard | const char *filename, int64_t size_in_sectors, |
75 | ea2384d3 | bellard | const char *backing_file, int flags) |
76 | ea2384d3 | bellard | { |
77 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
78 | ea2384d3 | bellard | return -ENOTSUP;
|
79 | ea2384d3 | bellard | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
|
80 | ea2384d3 | bellard | } |
81 | ea2384d3 | bellard | |
82 | d5249393 | bellard | #ifdef _WIN32
|
83 | d5249393 | bellard | static void get_tmp_filename(char *filename, int size) |
84 | d5249393 | bellard | { |
85 | d5249393 | bellard | /* XXX: find a better function */
|
86 | d5249393 | bellard | tmpnam(filename); |
87 | d5249393 | bellard | } |
88 | d5249393 | bellard | #else
|
89 | ea2384d3 | bellard | static void get_tmp_filename(char *filename, int size) |
90 | fc01f7e7 | bellard | { |
91 | 67b915a5 | bellard | int fd;
|
92 | d5249393 | bellard | /* XXX: race condition possible */
|
93 | ea2384d3 | bellard | pstrcpy(filename, size, "/tmp/vl.XXXXXX");
|
94 | ea2384d3 | bellard | fd = mkstemp(filename); |
95 | ea2384d3 | bellard | close(fd); |
96 | ea2384d3 | bellard | } |
97 | d5249393 | bellard | #endif
|
98 | fc01f7e7 | bellard | |
99 | 7674e7bf | bellard | /* XXX: force raw format if block or character device ? It would
|
100 | 7674e7bf | bellard | simplify the BSD case */
|
101 | ea2384d3 | bellard | static BlockDriver *find_image_format(const char *filename) |
102 | ea2384d3 | bellard | { |
103 | ea2384d3 | bellard | int fd, ret, score, score_max;
|
104 | ea2384d3 | bellard | BlockDriver *drv1, *drv; |
105 | 7674e7bf | bellard | uint8_t *buf; |
106 | 7674e7bf | bellard | size_t bufsize = 1024;
|
107 | 33e3963e | bellard | |
108 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
109 | 712e7874 | bellard | if (fd < 0) { |
110 | 712e7874 | bellard | buf = NULL;
|
111 | 712e7874 | bellard | ret = 0;
|
112 | 712e7874 | bellard | } else {
|
113 | 7674e7bf | bellard | #ifdef DIOCGSECTORSIZE
|
114 | 712e7874 | bellard | { |
115 | 712e7874 | bellard | unsigned int sectorsize = 512; |
116 | 712e7874 | bellard | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
|
117 | 712e7874 | bellard | sectorsize > bufsize) |
118 | 712e7874 | bellard | bufsize = sectorsize; |
119 | 712e7874 | bellard | } |
120 | 7674e7bf | bellard | #endif
|
121 | 712e7874 | bellard | buf = qemu_malloc(bufsize); |
122 | 712e7874 | bellard | if (!buf)
|
123 | 712e7874 | bellard | return NULL; |
124 | 712e7874 | bellard | ret = read(fd, buf, bufsize); |
125 | 712e7874 | bellard | if (ret < 0) { |
126 | 712e7874 | bellard | close(fd); |
127 | 712e7874 | bellard | qemu_free(buf); |
128 | 712e7874 | bellard | return NULL; |
129 | 712e7874 | bellard | } |
130 | ea2384d3 | bellard | close(fd); |
131 | ea2384d3 | bellard | } |
132 | ea2384d3 | bellard | |
133 | ea2384d3 | bellard | drv = NULL;
|
134 | ea2384d3 | bellard | score_max = 0;
|
135 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
136 | ea2384d3 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
137 | ea2384d3 | bellard | if (score > score_max) {
|
138 | ea2384d3 | bellard | score_max = score; |
139 | ea2384d3 | bellard | drv = drv1; |
140 | 0849bf08 | bellard | } |
141 | fc01f7e7 | bellard | } |
142 | 712e7874 | bellard | qemu_free(buf); |
143 | ea2384d3 | bellard | return drv;
|
144 | ea2384d3 | bellard | } |
145 | ea2384d3 | bellard | |
146 | ea2384d3 | bellard | int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
147 | ea2384d3 | bellard | { |
148 | ea2384d3 | bellard | return bdrv_open2(bs, filename, snapshot, NULL); |
149 | ea2384d3 | bellard | } |
150 | ea2384d3 | bellard | |
151 | ea2384d3 | bellard | int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot, |
152 | ea2384d3 | bellard | BlockDriver *drv) |
153 | ea2384d3 | bellard | { |
154 | ea2384d3 | bellard | int ret;
|
155 | ea2384d3 | bellard | char tmp_filename[1024]; |
156 | ea2384d3 | bellard | |
157 | ea2384d3 | bellard | bs->read_only = 0;
|
158 | ea2384d3 | bellard | bs->is_temporary = 0;
|
159 | ea2384d3 | bellard | bs->encrypted = 0;
|
160 | 712e7874 | bellard | |
161 | ea2384d3 | bellard | if (snapshot) {
|
162 | ea2384d3 | bellard | BlockDriverState *bs1; |
163 | ea2384d3 | bellard | int64_t total_size; |
164 | ea2384d3 | bellard | |
165 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
166 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
167 | 33e3963e | bellard | |
168 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
169 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
170 | ea2384d3 | bellard | if (!bs1) {
|
171 | ea2384d3 | bellard | return -1; |
172 | ea2384d3 | bellard | } |
173 | ea2384d3 | bellard | if (bdrv_open(bs1, filename, 0) < 0) { |
174 | ea2384d3 | bellard | bdrv_delete(bs1); |
175 | ea2384d3 | bellard | return -1; |
176 | ea2384d3 | bellard | } |
177 | ea2384d3 | bellard | total_size = bs1->total_sectors; |
178 | ea2384d3 | bellard | bdrv_delete(bs1); |
179 | ea2384d3 | bellard | |
180 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
181 | ea2384d3 | bellard | /* XXX: use cow for linux as it is more efficient ? */
|
182 | ea2384d3 | bellard | if (bdrv_create(&bdrv_qcow, tmp_filename,
|
183 | ea2384d3 | bellard | total_size, filename, 0) < 0) { |
184 | ea2384d3 | bellard | return -1; |
185 | ea2384d3 | bellard | } |
186 | ea2384d3 | bellard | filename = tmp_filename; |
187 | ea2384d3 | bellard | bs->is_temporary = 1;
|
188 | ea2384d3 | bellard | } |
189 | 712e7874 | bellard | |
190 | ea2384d3 | bellard | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
191 | ea2384d3 | bellard | if (!drv) {
|
192 | ea2384d3 | bellard | drv = find_image_format(filename); |
193 | ea2384d3 | bellard | if (!drv)
|
194 | ea2384d3 | bellard | return -1; |
195 | ea2384d3 | bellard | } |
196 | ea2384d3 | bellard | bs->drv = drv; |
197 | ea2384d3 | bellard | bs->opaque = qemu_mallocz(drv->instance_size); |
198 | ea2384d3 | bellard | if (bs->opaque == NULL && drv->instance_size > 0) |
199 | ea2384d3 | bellard | return -1; |
200 | ea2384d3 | bellard | |
201 | ea2384d3 | bellard | ret = drv->bdrv_open(bs, filename); |
202 | ea2384d3 | bellard | if (ret < 0) { |
203 | ea2384d3 | bellard | qemu_free(bs->opaque); |
204 | ea2384d3 | bellard | return -1; |
205 | 33e3963e | bellard | } |
206 | 67b915a5 | bellard | #ifndef _WIN32
|
207 | ea2384d3 | bellard | if (bs->is_temporary) {
|
208 | ea2384d3 | bellard | unlink(filename); |
209 | ea2384d3 | bellard | } |
210 | ea2384d3 | bellard | #endif
|
211 | ea2384d3 | bellard | if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) { |
212 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
213 | ea2384d3 | bellard | bs->backing_hd = bdrv_new("");
|
214 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
215 | ea2384d3 | bellard | fail:
|
216 | ea2384d3 | bellard | bdrv_close(bs); |
217 | ea2384d3 | bellard | return -1; |
218 | 33e3963e | bellard | } |
219 | ea2384d3 | bellard | if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0) |
220 | 33e3963e | bellard | goto fail;
|
221 | 33e3963e | bellard | } |
222 | 33e3963e | bellard | |
223 | b338082b | bellard | bs->inserted = 1;
|
224 | b338082b | bellard | |
225 | b338082b | bellard | /* call the change callback */
|
226 | b338082b | bellard | if (bs->change_cb)
|
227 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
228 | b338082b | bellard | |
229 | b338082b | bellard | return 0; |
230 | fc01f7e7 | bellard | } |
231 | fc01f7e7 | bellard | |
232 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
233 | fc01f7e7 | bellard | { |
234 | b338082b | bellard | if (bs->inserted) {
|
235 | ea2384d3 | bellard | if (bs->backing_hd)
|
236 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
237 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
238 | ea2384d3 | bellard | qemu_free(bs->opaque); |
239 | ea2384d3 | bellard | #ifdef _WIN32
|
240 | ea2384d3 | bellard | if (bs->is_temporary) {
|
241 | ea2384d3 | bellard | unlink(bs->filename); |
242 | ea2384d3 | bellard | } |
243 | 67b915a5 | bellard | #endif
|
244 | ea2384d3 | bellard | bs->opaque = NULL;
|
245 | ea2384d3 | bellard | bs->drv = NULL;
|
246 | b338082b | bellard | bs->inserted = 0;
|
247 | b338082b | bellard | |
248 | b338082b | bellard | /* call the change callback */
|
249 | b338082b | bellard | if (bs->change_cb)
|
250 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
251 | b338082b | bellard | } |
252 | b338082b | bellard | } |
253 | b338082b | bellard | |
254 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
255 | b338082b | bellard | { |
256 | ea2384d3 | bellard | /* XXX: remove the driver list */
|
257 | b338082b | bellard | bdrv_close(bs); |
258 | b338082b | bellard | qemu_free(bs); |
259 | fc01f7e7 | bellard | } |
260 | fc01f7e7 | bellard | |
261 | 33e3963e | bellard | /* commit COW file into the raw image */
|
262 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
263 | 33e3963e | bellard | { |
264 | 33e3963e | bellard | int64_t i; |
265 | ea2384d3 | bellard | int n, j;
|
266 | ea2384d3 | bellard | unsigned char sector[512]; |
267 | 33e3963e | bellard | |
268 | b338082b | bellard | if (!bs->inserted)
|
269 | ea2384d3 | bellard | return -ENOENT;
|
270 | 33e3963e | bellard | |
271 | 33e3963e | bellard | if (bs->read_only) {
|
272 | ea2384d3 | bellard | return -EACCES;
|
273 | 33e3963e | bellard | } |
274 | 33e3963e | bellard | |
275 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
276 | ea2384d3 | bellard | return -ENOTSUP;
|
277 | ea2384d3 | bellard | } |
278 | 33e3963e | bellard | |
279 | ea2384d3 | bellard | for (i = 0; i < bs->total_sectors;) { |
280 | ea2384d3 | bellard | if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
281 | ea2384d3 | bellard | for(j = 0; j < n; j++) { |
282 | ea2384d3 | bellard | if (bdrv_read(bs, i, sector, 1) != 0) { |
283 | ea2384d3 | bellard | return -EIO;
|
284 | ea2384d3 | bellard | } |
285 | ea2384d3 | bellard | |
286 | ea2384d3 | bellard | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
287 | ea2384d3 | bellard | return -EIO;
|
288 | ea2384d3 | bellard | } |
289 | ea2384d3 | bellard | i++; |
290 | 33e3963e | bellard | } |
291 | ea2384d3 | bellard | } else {
|
292 | ea2384d3 | bellard | i += n; |
293 | ea2384d3 | bellard | } |
294 | 33e3963e | bellard | } |
295 | 33e3963e | bellard | return 0; |
296 | 33e3963e | bellard | } |
297 | 33e3963e | bellard | |
298 | fc01f7e7 | bellard | /* return -1 if error */
|
299 | fc01f7e7 | bellard | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
300 | fc01f7e7 | bellard | uint8_t *buf, int nb_sectors)
|
301 | fc01f7e7 | bellard | { |
302 | ea2384d3 | bellard | int ret, n;
|
303 | ea2384d3 | bellard | BlockDriver *drv = bs->drv; |
304 | ea2384d3 | bellard | |
305 | b338082b | bellard | if (!bs->inserted)
|
306 | b338082b | bellard | return -1; |
307 | b338082b | bellard | |
308 | 33e3963e | bellard | while (nb_sectors > 0) { |
309 | ea2384d3 | bellard | if (sector_num == 0 && bs->boot_sector_enabled) { |
310 | cf98951b | bellard | memcpy(buf, bs->boot_sector_data, 512);
|
311 | cf98951b | bellard | n = 1;
|
312 | ea2384d3 | bellard | } else if (bs->backing_hd) { |
313 | ea2384d3 | bellard | if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
|
314 | ea2384d3 | bellard | ret = drv->bdrv_read(bs, sector_num, buf, n); |
315 | ea2384d3 | bellard | if (ret < 0) |
316 | ea2384d3 | bellard | return -1; |
317 | ea2384d3 | bellard | } else {
|
318 | ea2384d3 | bellard | /* read from the base image */
|
319 | ea2384d3 | bellard | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); |
320 | ea2384d3 | bellard | if (ret < 0) |
321 | ea2384d3 | bellard | return -1; |
322 | ea2384d3 | bellard | } |
323 | 33e3963e | bellard | } else {
|
324 | ea2384d3 | bellard | ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
325 | ea2384d3 | bellard | if (ret < 0) |
326 | 33e3963e | bellard | return -1; |
327 | ea2384d3 | bellard | /* no need to loop */
|
328 | ea2384d3 | bellard | break;
|
329 | 33e3963e | bellard | } |
330 | 33e3963e | bellard | nb_sectors -= n; |
331 | 33e3963e | bellard | sector_num += n; |
332 | 33e3963e | bellard | buf += n * 512;
|
333 | 33e3963e | bellard | } |
334 | 33e3963e | bellard | return 0; |
335 | fc01f7e7 | bellard | } |
336 | fc01f7e7 | bellard | |
337 | fc01f7e7 | bellard | /* return -1 if error */
|
338 | fc01f7e7 | bellard | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
339 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
340 | fc01f7e7 | bellard | { |
341 | b338082b | bellard | if (!bs->inserted)
|
342 | b338082b | bellard | return -1; |
343 | 0849bf08 | bellard | if (bs->read_only)
|
344 | 0849bf08 | bellard | return -1; |
345 | ea2384d3 | bellard | return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
|
346 | fc01f7e7 | bellard | } |
347 | fc01f7e7 | bellard | |
348 | fc01f7e7 | bellard | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
|
349 | fc01f7e7 | bellard | { |
350 | fc01f7e7 | bellard | *nb_sectors_ptr = bs->total_sectors; |
351 | fc01f7e7 | bellard | } |
352 | cf98951b | bellard | |
353 | cf98951b | bellard | /* force a given boot sector. */
|
354 | cf98951b | bellard | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) |
355 | cf98951b | bellard | { |
356 | cf98951b | bellard | bs->boot_sector_enabled = 1;
|
357 | cf98951b | bellard | if (size > 512) |
358 | cf98951b | bellard | size = 512;
|
359 | cf98951b | bellard | memcpy(bs->boot_sector_data, data, size); |
360 | cf98951b | bellard | memset(bs->boot_sector_data + size, 0, 512 - size); |
361 | cf98951b | bellard | } |
362 | b338082b | bellard | |
363 | b338082b | bellard | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
364 | b338082b | bellard | int cyls, int heads, int secs) |
365 | b338082b | bellard | { |
366 | b338082b | bellard | bs->cyls = cyls; |
367 | b338082b | bellard | bs->heads = heads; |
368 | b338082b | bellard | bs->secs = secs; |
369 | b338082b | bellard | } |
370 | b338082b | bellard | |
371 | b338082b | bellard | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
372 | b338082b | bellard | { |
373 | b338082b | bellard | bs->type = type; |
374 | b338082b | bellard | bs->removable = ((type == BDRV_TYPE_CDROM || |
375 | b338082b | bellard | type == BDRV_TYPE_FLOPPY)); |
376 | b338082b | bellard | } |
377 | b338082b | bellard | |
378 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
379 | 46d4767d | bellard | { |
380 | 46d4767d | bellard | bs->translation = translation; |
381 | 46d4767d | bellard | } |
382 | 46d4767d | bellard | |
383 | b338082b | bellard | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
384 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
385 | b338082b | bellard | { |
386 | b338082b | bellard | *pcyls = bs->cyls; |
387 | b338082b | bellard | *pheads = bs->heads; |
388 | b338082b | bellard | *psecs = bs->secs; |
389 | b338082b | bellard | } |
390 | b338082b | bellard | |
391 | b338082b | bellard | int bdrv_get_type_hint(BlockDriverState *bs)
|
392 | b338082b | bellard | { |
393 | b338082b | bellard | return bs->type;
|
394 | b338082b | bellard | } |
395 | b338082b | bellard | |
396 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
397 | 46d4767d | bellard | { |
398 | 46d4767d | bellard | return bs->translation;
|
399 | 46d4767d | bellard | } |
400 | 46d4767d | bellard | |
401 | b338082b | bellard | int bdrv_is_removable(BlockDriverState *bs)
|
402 | b338082b | bellard | { |
403 | b338082b | bellard | return bs->removable;
|
404 | b338082b | bellard | } |
405 | b338082b | bellard | |
406 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
407 | b338082b | bellard | { |
408 | b338082b | bellard | return bs->read_only;
|
409 | b338082b | bellard | } |
410 | b338082b | bellard | |
411 | b338082b | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
412 | b338082b | bellard | { |
413 | b338082b | bellard | return bs->inserted;
|
414 | b338082b | bellard | } |
415 | b338082b | bellard | |
416 | b338082b | bellard | int bdrv_is_locked(BlockDriverState *bs)
|
417 | b338082b | bellard | { |
418 | b338082b | bellard | return bs->locked;
|
419 | b338082b | bellard | } |
420 | b338082b | bellard | |
421 | b338082b | bellard | void bdrv_set_locked(BlockDriverState *bs, int locked) |
422 | b338082b | bellard | { |
423 | b338082b | bellard | bs->locked = locked; |
424 | b338082b | bellard | } |
425 | b338082b | bellard | |
426 | b338082b | bellard | void bdrv_set_change_cb(BlockDriverState *bs,
|
427 | b338082b | bellard | void (*change_cb)(void *opaque), void *opaque) |
428 | b338082b | bellard | { |
429 | b338082b | bellard | bs->change_cb = change_cb; |
430 | b338082b | bellard | bs->change_opaque = opaque; |
431 | b338082b | bellard | } |
432 | b338082b | bellard | |
433 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
434 | ea2384d3 | bellard | { |
435 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
436 | ea2384d3 | bellard | return 1; |
437 | ea2384d3 | bellard | return bs->encrypted;
|
438 | ea2384d3 | bellard | } |
439 | ea2384d3 | bellard | |
440 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
441 | ea2384d3 | bellard | { |
442 | ea2384d3 | bellard | int ret;
|
443 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
444 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
445 | ea2384d3 | bellard | if (ret < 0) |
446 | ea2384d3 | bellard | return ret;
|
447 | ea2384d3 | bellard | if (!bs->encrypted)
|
448 | ea2384d3 | bellard | return 0; |
449 | ea2384d3 | bellard | } |
450 | ea2384d3 | bellard | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
|
451 | ea2384d3 | bellard | return -1; |
452 | ea2384d3 | bellard | return bs->drv->bdrv_set_key(bs, key);
|
453 | ea2384d3 | bellard | } |
454 | ea2384d3 | bellard | |
455 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
456 | ea2384d3 | bellard | { |
457 | ea2384d3 | bellard | if (!bs->inserted || !bs->drv) {
|
458 | ea2384d3 | bellard | buf[0] = '\0'; |
459 | ea2384d3 | bellard | } else {
|
460 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
461 | ea2384d3 | bellard | } |
462 | ea2384d3 | bellard | } |
463 | ea2384d3 | bellard | |
464 | ea2384d3 | bellard | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
465 | ea2384d3 | bellard | void *opaque)
|
466 | ea2384d3 | bellard | { |
467 | ea2384d3 | bellard | BlockDriver *drv; |
468 | ea2384d3 | bellard | |
469 | ea2384d3 | bellard | for (drv = first_drv; drv != NULL; drv = drv->next) { |
470 | ea2384d3 | bellard | it(opaque, drv->format_name); |
471 | ea2384d3 | bellard | } |
472 | ea2384d3 | bellard | } |
473 | ea2384d3 | bellard | |
474 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
475 | b338082b | bellard | { |
476 | b338082b | bellard | BlockDriverState *bs; |
477 | b338082b | bellard | |
478 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
479 | b338082b | bellard | if (!strcmp(name, bs->device_name))
|
480 | b338082b | bellard | return bs;
|
481 | b338082b | bellard | } |
482 | b338082b | bellard | return NULL; |
483 | b338082b | bellard | } |
484 | b338082b | bellard | |
485 | 81d0912d | bellard | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
486 | 81d0912d | bellard | { |
487 | 81d0912d | bellard | BlockDriverState *bs; |
488 | 81d0912d | bellard | |
489 | 81d0912d | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
490 | 81d0912d | bellard | it(opaque, bs->device_name); |
491 | 81d0912d | bellard | } |
492 | 81d0912d | bellard | } |
493 | 81d0912d | bellard | |
494 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
495 | ea2384d3 | bellard | { |
496 | ea2384d3 | bellard | return bs->device_name;
|
497 | ea2384d3 | bellard | } |
498 | ea2384d3 | bellard | |
499 | b338082b | bellard | void bdrv_info(void) |
500 | b338082b | bellard | { |
501 | b338082b | bellard | BlockDriverState *bs; |
502 | b338082b | bellard | |
503 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
504 | b338082b | bellard | term_printf("%s:", bs->device_name);
|
505 | b338082b | bellard | term_printf(" type=");
|
506 | b338082b | bellard | switch(bs->type) {
|
507 | b338082b | bellard | case BDRV_TYPE_HD:
|
508 | b338082b | bellard | term_printf("hd");
|
509 | b338082b | bellard | break;
|
510 | b338082b | bellard | case BDRV_TYPE_CDROM:
|
511 | b338082b | bellard | term_printf("cdrom");
|
512 | b338082b | bellard | break;
|
513 | b338082b | bellard | case BDRV_TYPE_FLOPPY:
|
514 | b338082b | bellard | term_printf("floppy");
|
515 | b338082b | bellard | break;
|
516 | b338082b | bellard | } |
517 | b338082b | bellard | term_printf(" removable=%d", bs->removable);
|
518 | b338082b | bellard | if (bs->removable) {
|
519 | b338082b | bellard | term_printf(" locked=%d", bs->locked);
|
520 | b338082b | bellard | } |
521 | b338082b | bellard | if (bs->inserted) {
|
522 | b338082b | bellard | term_printf(" file=%s", bs->filename);
|
523 | ea2384d3 | bellard | if (bs->backing_file[0] != '\0') |
524 | ea2384d3 | bellard | term_printf(" backing_file=%s", bs->backing_file);
|
525 | b338082b | bellard | term_printf(" ro=%d", bs->read_only);
|
526 | ea2384d3 | bellard | term_printf(" drv=%s", bs->drv->format_name);
|
527 | ea2384d3 | bellard | if (bs->encrypted)
|
528 | ea2384d3 | bellard | term_printf(" encrypted");
|
529 | b338082b | bellard | } else {
|
530 | b338082b | bellard | term_printf(" [not inserted]");
|
531 | b338082b | bellard | } |
532 | b338082b | bellard | term_printf("\n");
|
533 | b338082b | bellard | } |
534 | b338082b | bellard | } |
535 | ea2384d3 | bellard | |
536 | ea2384d3 | bellard | |
537 | ea2384d3 | bellard | /**************************************************************/
|
538 | ea2384d3 | bellard | /* RAW block driver */
|
539 | ea2384d3 | bellard | |
540 | ea2384d3 | bellard | typedef struct BDRVRawState { |
541 | ea2384d3 | bellard | int fd;
|
542 | ea2384d3 | bellard | } BDRVRawState; |
543 | ea2384d3 | bellard | |
544 | ea2384d3 | bellard | static int raw_probe(const uint8_t *buf, int buf_size, const char *filename) |
545 | ea2384d3 | bellard | { |
546 | ea2384d3 | bellard | return 1; /* maybe */ |
547 | ea2384d3 | bellard | } |
548 | ea2384d3 | bellard | |
549 | ea2384d3 | bellard | static int raw_open(BlockDriverState *bs, const char *filename) |
550 | ea2384d3 | bellard | { |
551 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
552 | ea2384d3 | bellard | int fd;
|
553 | ea2384d3 | bellard | int64_t size; |
554 | e5484d33 | bellard | #ifdef _BSD
|
555 | e5484d33 | bellard | struct stat sb;
|
556 | e5484d33 | bellard | #endif
|
557 | ea2384d3 | bellard | |
558 | ea2384d3 | bellard | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
559 | ea2384d3 | bellard | if (fd < 0) { |
560 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
561 | ea2384d3 | bellard | if (fd < 0) |
562 | ea2384d3 | bellard | return -1; |
563 | ea2384d3 | bellard | bs->read_only = 1;
|
564 | ea2384d3 | bellard | } |
565 | 7674e7bf | bellard | #ifdef _BSD
|
566 | e5484d33 | bellard | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
|
567 | 7674e7bf | bellard | #ifdef DIOCGMEDIASIZE
|
568 | e5484d33 | bellard | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
|
569 | 7674e7bf | bellard | #endif
|
570 | e5484d33 | bellard | size = lseek(fd, 0LL, SEEK_END);
|
571 | 7674e7bf | bellard | } else
|
572 | 7674e7bf | bellard | #endif
|
573 | 7674e7bf | bellard | { |
574 | 7674e7bf | bellard | size = lseek(fd, 0, SEEK_END);
|
575 | 7674e7bf | bellard | } |
576 | c747cd1f | bellard | #ifdef _WIN32
|
577 | c747cd1f | bellard | /* On Windows hosts it can happen that we're unable to get file size
|
578 | c747cd1f | bellard | for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
|
579 | c747cd1f | bellard | if (size == -1) |
580 | c747cd1f | bellard | size = LONG_LONG_MAX; |
581 | c747cd1f | bellard | #endif
|
582 | ea2384d3 | bellard | bs->total_sectors = size / 512;
|
583 | ea2384d3 | bellard | s->fd = fd; |
584 | ea2384d3 | bellard | return 0; |
585 | ea2384d3 | bellard | } |
586 | ea2384d3 | bellard | |
587 | ea2384d3 | bellard | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
588 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
589 | ea2384d3 | bellard | { |
590 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
591 | ea2384d3 | bellard | int ret;
|
592 | ea2384d3 | bellard | |
593 | d5249393 | bellard | lseek(s->fd, sector_num * 512, SEEK_SET);
|
594 | ea2384d3 | bellard | ret = read(s->fd, buf, nb_sectors * 512);
|
595 | ea2384d3 | bellard | if (ret != nb_sectors * 512) |
596 | ea2384d3 | bellard | return -1; |
597 | ea2384d3 | bellard | return 0; |
598 | ea2384d3 | bellard | } |
599 | ea2384d3 | bellard | |
600 | ea2384d3 | bellard | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
601 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
602 | ea2384d3 | bellard | { |
603 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
604 | ea2384d3 | bellard | int ret;
|
605 | ea2384d3 | bellard | |
606 | d5249393 | bellard | lseek(s->fd, sector_num * 512, SEEK_SET);
|
607 | ea2384d3 | bellard | ret = write(s->fd, buf, nb_sectors * 512);
|
608 | ea2384d3 | bellard | if (ret != nb_sectors * 512) |
609 | ea2384d3 | bellard | return -1; |
610 | ea2384d3 | bellard | return 0; |
611 | ea2384d3 | bellard | } |
612 | ea2384d3 | bellard | |
613 | e2731add | bellard | static void raw_close(BlockDriverState *bs) |
614 | ea2384d3 | bellard | { |
615 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
616 | ea2384d3 | bellard | close(s->fd); |
617 | ea2384d3 | bellard | } |
618 | ea2384d3 | bellard | |
619 | ea2384d3 | bellard | static int raw_create(const char *filename, int64_t total_size, |
620 | ea2384d3 | bellard | const char *backing_file, int flags) |
621 | ea2384d3 | bellard | { |
622 | ea2384d3 | bellard | int fd;
|
623 | ea2384d3 | bellard | |
624 | ea2384d3 | bellard | if (flags || backing_file)
|
625 | ea2384d3 | bellard | return -ENOTSUP;
|
626 | ea2384d3 | bellard | |
627 | ea2384d3 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
628 | ea2384d3 | bellard | 0644);
|
629 | ea2384d3 | bellard | if (fd < 0) |
630 | ea2384d3 | bellard | return -EIO;
|
631 | d5249393 | bellard | ftruncate(fd, total_size * 512);
|
632 | ea2384d3 | bellard | close(fd); |
633 | ea2384d3 | bellard | return 0; |
634 | ea2384d3 | bellard | } |
635 | ea2384d3 | bellard | |
636 | ea2384d3 | bellard | BlockDriver bdrv_raw = { |
637 | ea2384d3 | bellard | "raw",
|
638 | ea2384d3 | bellard | sizeof(BDRVRawState),
|
639 | ea2384d3 | bellard | raw_probe, |
640 | ea2384d3 | bellard | raw_open, |
641 | ea2384d3 | bellard | raw_read, |
642 | ea2384d3 | bellard | raw_write, |
643 | ea2384d3 | bellard | raw_close, |
644 | ea2384d3 | bellard | raw_create, |
645 | ea2384d3 | bellard | }; |
646 | ea2384d3 | bellard | |
647 | ea2384d3 | bellard | void bdrv_init(void) |
648 | ea2384d3 | bellard | { |
649 | ea2384d3 | bellard | bdrv_register(&bdrv_raw); |
650 | ea2384d3 | bellard | #ifndef _WIN32
|
651 | ea2384d3 | bellard | bdrv_register(&bdrv_cow); |
652 | ea2384d3 | bellard | #endif
|
653 | ea2384d3 | bellard | bdrv_register(&bdrv_qcow); |
654 | ea2384d3 | bellard | bdrv_register(&bdrv_vmdk); |
655 | 3c56521b | bellard | bdrv_register(&bdrv_cloop); |
656 | 585d0ed9 | bellard | bdrv_register(&bdrv_dmg); |
657 | a8753c34 | bellard | bdrv_register(&bdrv_bochs); |
658 | 6a0f9e82 | bellard | bdrv_register(&bdrv_vpc); |
659 | 712e7874 | bellard | bdrv_register(&bdrv_vvfat); |
660 | ea2384d3 | bellard | } |