root / block.c @ fcc941fe
History | View | Annotate | Download (13.2 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 | fc01f7e7 | bellard | |
26 | 67b915a5 | bellard | #ifndef _WIN32
|
27 | 67b915a5 | bellard | #include <sys/mman.h> |
28 | 67b915a5 | bellard | #endif
|
29 | 33e3963e | bellard | |
30 | 00af2b26 | bellard | #include "cow.h" |
31 | 00af2b26 | bellard | |
32 | fc01f7e7 | bellard | struct BlockDriverState {
|
33 | 33e3963e | bellard | int fd; /* if -1, only COW mappings */ |
34 | fc01f7e7 | bellard | int64_t total_sectors; |
35 | b338082b | bellard | int read_only; /* if true, the media is read only */ |
36 | b338082b | bellard | int inserted; /* if true, the media is present */ |
37 | b338082b | bellard | int removable; /* if true, the media can be removed */ |
38 | b338082b | bellard | int locked; /* if true, the media cannot temporarily be ejected */ |
39 | b338082b | bellard | /* event callback when inserting/removing */
|
40 | b338082b | bellard | void (*change_cb)(void *opaque); |
41 | b338082b | bellard | void *change_opaque;
|
42 | 33e3963e | bellard | |
43 | 33e3963e | bellard | uint8_t *cow_bitmap; /* if non NULL, COW mappings are used first */
|
44 | 33e3963e | bellard | uint8_t *cow_bitmap_addr; /* mmap address of cow_bitmap */
|
45 | 33e3963e | bellard | int cow_bitmap_size;
|
46 | 33e3963e | bellard | int cow_fd;
|
47 | 33e3963e | bellard | int64_t cow_sectors_offset; |
48 | cf98951b | bellard | int boot_sector_enabled;
|
49 | cf98951b | bellard | uint8_t boot_sector_data[512];
|
50 | cf98951b | bellard | |
51 | 33e3963e | bellard | char filename[1024]; |
52 | b338082b | bellard | |
53 | b338082b | bellard | /* NOTE: the following infos are only hints for real hardware
|
54 | b338082b | bellard | drivers. They are not used by the block driver */
|
55 | b338082b | bellard | int cyls, heads, secs;
|
56 | b338082b | bellard | int type;
|
57 | b338082b | bellard | char device_name[32]; |
58 | b338082b | bellard | BlockDriverState *next; |
59 | fc01f7e7 | bellard | }; |
60 | fc01f7e7 | bellard | |
61 | b338082b | bellard | static BlockDriverState *bdrv_first;
|
62 | b338082b | bellard | |
63 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
64 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
65 | b338082b | bellard | { |
66 | b338082b | bellard | BlockDriverState **pbs, *bs; |
67 | b338082b | bellard | |
68 | b338082b | bellard | bs = qemu_mallocz(sizeof(BlockDriverState));
|
69 | b338082b | bellard | if(!bs)
|
70 | b338082b | bellard | return NULL; |
71 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
72 | b338082b | bellard | /* insert at the end */
|
73 | b338082b | bellard | pbs = &bdrv_first; |
74 | b338082b | bellard | while (*pbs != NULL) |
75 | b338082b | bellard | pbs = &(*pbs)->next; |
76 | b338082b | bellard | *pbs = bs; |
77 | b338082b | bellard | return bs;
|
78 | b338082b | bellard | } |
79 | b338082b | bellard | |
80 | b338082b | bellard | int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
81 | fc01f7e7 | bellard | { |
82 | 67b915a5 | bellard | int fd;
|
83 | fc01f7e7 | bellard | int64_t size; |
84 | 33e3963e | bellard | struct cow_header_v2 cow_header;
|
85 | 67b915a5 | bellard | #ifndef _WIN32
|
86 | 67b915a5 | bellard | char template[] = "/tmp/vl.XXXXXX"; |
87 | 67b915a5 | bellard | int cow_fd;
|
88 | 33e3963e | bellard | struct stat st;
|
89 | 67b915a5 | bellard | #endif
|
90 | fc01f7e7 | bellard | |
91 | 0849bf08 | bellard | bs->read_only = 0;
|
92 | 33e3963e | bellard | bs->fd = -1;
|
93 | 33e3963e | bellard | bs->cow_fd = -1;
|
94 | 33e3963e | bellard | bs->cow_bitmap = NULL;
|
95 | eba2af63 | bellard | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
96 | 33e3963e | bellard | |
97 | 33e3963e | bellard | /* open standard HD image */
|
98 | 67b915a5 | bellard | #ifdef _WIN32
|
99 | 67b915a5 | bellard | fd = open(filename, O_RDWR | O_BINARY); |
100 | 67b915a5 | bellard | #else
|
101 | 33e3963e | bellard | fd = open(filename, O_RDWR | O_LARGEFILE); |
102 | 67b915a5 | bellard | #endif
|
103 | fc01f7e7 | bellard | if (fd < 0) { |
104 | 33e3963e | bellard | /* read only image on disk */
|
105 | 67b915a5 | bellard | #ifdef _WIN32
|
106 | 67b915a5 | bellard | fd = open(filename, O_RDONLY | O_BINARY); |
107 | 67b915a5 | bellard | #else
|
108 | 33e3963e | bellard | fd = open(filename, O_RDONLY | O_LARGEFILE); |
109 | 67b915a5 | bellard | #endif
|
110 | 0849bf08 | bellard | if (fd < 0) { |
111 | 33e3963e | bellard | perror(filename); |
112 | 33e3963e | bellard | goto fail;
|
113 | 0849bf08 | bellard | } |
114 | 33e3963e | bellard | if (!snapshot)
|
115 | 33e3963e | bellard | bs->read_only = 1;
|
116 | fc01f7e7 | bellard | } |
117 | fc01f7e7 | bellard | bs->fd = fd; |
118 | 33e3963e | bellard | |
119 | 33e3963e | bellard | /* see if it is a cow image */
|
120 | 33e3963e | bellard | if (read(fd, &cow_header, sizeof(cow_header)) != sizeof(cow_header)) { |
121 | 33e3963e | bellard | fprintf(stderr, "%s: could not read header\n", filename);
|
122 | 33e3963e | bellard | goto fail;
|
123 | 33e3963e | bellard | } |
124 | 67b915a5 | bellard | #ifndef _WIN32
|
125 | 67b915a5 | bellard | if (be32_to_cpu(cow_header.magic) == COW_MAGIC &&
|
126 | 67b915a5 | bellard | be32_to_cpu(cow_header.version) == COW_VERSION) { |
127 | 33e3963e | bellard | /* cow image found */
|
128 | 33e3963e | bellard | size = cow_header.size; |
129 | 33e3963e | bellard | #ifndef WORDS_BIGENDIAN
|
130 | 33e3963e | bellard | size = bswap64(size); |
131 | 33e3963e | bellard | #endif
|
132 | 33e3963e | bellard | bs->total_sectors = size / 512;
|
133 | 33e3963e | bellard | |
134 | 33e3963e | bellard | bs->cow_fd = fd; |
135 | 33e3963e | bellard | bs->fd = -1;
|
136 | 33e3963e | bellard | if (cow_header.backing_file[0] != '\0') { |
137 | 33e3963e | bellard | if (stat(cow_header.backing_file, &st) != 0) { |
138 | 33e3963e | bellard | fprintf(stderr, "%s: could not find original disk image '%s'\n", filename, cow_header.backing_file);
|
139 | 33e3963e | bellard | goto fail;
|
140 | 33e3963e | bellard | } |
141 | 67b915a5 | bellard | if (st.st_mtime != be32_to_cpu(cow_header.mtime)) {
|
142 | 33e3963e | bellard | fprintf(stderr, "%s: original raw disk image '%s' does not match saved timestamp\n", filename, cow_header.backing_file);
|
143 | 33e3963e | bellard | goto fail;
|
144 | 33e3963e | bellard | } |
145 | 33e3963e | bellard | fd = open(cow_header.backing_file, O_RDONLY | O_LARGEFILE); |
146 | 33e3963e | bellard | if (fd < 0) |
147 | 33e3963e | bellard | goto fail;
|
148 | 33e3963e | bellard | bs->fd = fd; |
149 | 33e3963e | bellard | } |
150 | 33e3963e | bellard | /* mmap the bitmap */
|
151 | 33e3963e | bellard | bs->cow_bitmap_size = ((bs->total_sectors + 7) >> 3) + sizeof(cow_header); |
152 | 33e3963e | bellard | bs->cow_bitmap_addr = mmap(get_mmap_addr(bs->cow_bitmap_size), |
153 | 33e3963e | bellard | bs->cow_bitmap_size, |
154 | 33e3963e | bellard | PROT_READ | PROT_WRITE, |
155 | 33e3963e | bellard | MAP_SHARED, bs->cow_fd, 0);
|
156 | 33e3963e | bellard | if (bs->cow_bitmap_addr == MAP_FAILED)
|
157 | 33e3963e | bellard | goto fail;
|
158 | 33e3963e | bellard | bs->cow_bitmap = bs->cow_bitmap_addr + sizeof(cow_header);
|
159 | 33e3963e | bellard | bs->cow_sectors_offset = (bs->cow_bitmap_size + 511) & ~511; |
160 | 33e3963e | bellard | snapshot = 0;
|
161 | 67b915a5 | bellard | } else
|
162 | 67b915a5 | bellard | #endif
|
163 | 67b915a5 | bellard | { |
164 | 33e3963e | bellard | /* standard raw image */
|
165 | 33e3963e | bellard | size = lseek64(fd, 0, SEEK_END);
|
166 | 33e3963e | bellard | bs->total_sectors = size / 512;
|
167 | 33e3963e | bellard | bs->fd = fd; |
168 | 33e3963e | bellard | } |
169 | 33e3963e | bellard | |
170 | 67b915a5 | bellard | #ifndef _WIN32
|
171 | 33e3963e | bellard | if (snapshot) {
|
172 | 33e3963e | bellard | /* create a temporary COW file */
|
173 | 2b64948e | bellard | cow_fd = mkstemp64(template); |
174 | 33e3963e | bellard | if (cow_fd < 0) |
175 | 33e3963e | bellard | goto fail;
|
176 | 33e3963e | bellard | bs->cow_fd = cow_fd; |
177 | 33e3963e | bellard | unlink(template); |
178 | 33e3963e | bellard | |
179 | 33e3963e | bellard | /* just need to allocate bitmap */
|
180 | 33e3963e | bellard | bs->cow_bitmap_size = (bs->total_sectors + 7) >> 3; |
181 | 33e3963e | bellard | bs->cow_bitmap_addr = mmap(get_mmap_addr(bs->cow_bitmap_size), |
182 | 33e3963e | bellard | bs->cow_bitmap_size, |
183 | 33e3963e | bellard | PROT_READ | PROT_WRITE, |
184 | 33e3963e | bellard | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
185 | 33e3963e | bellard | if (bs->cow_bitmap_addr == MAP_FAILED)
|
186 | 33e3963e | bellard | goto fail;
|
187 | 33e3963e | bellard | bs->cow_bitmap = bs->cow_bitmap_addr; |
188 | 33e3963e | bellard | bs->cow_sectors_offset = 0;
|
189 | 33e3963e | bellard | } |
190 | 67b915a5 | bellard | #endif
|
191 | 33e3963e | bellard | |
192 | b338082b | bellard | bs->inserted = 1;
|
193 | b338082b | bellard | |
194 | b338082b | bellard | /* call the change callback */
|
195 | b338082b | bellard | if (bs->change_cb)
|
196 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
197 | b338082b | bellard | |
198 | b338082b | bellard | return 0; |
199 | 33e3963e | bellard | fail:
|
200 | 33e3963e | bellard | bdrv_close(bs); |
201 | b338082b | bellard | return -1; |
202 | fc01f7e7 | bellard | } |
203 | fc01f7e7 | bellard | |
204 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
205 | fc01f7e7 | bellard | { |
206 | b338082b | bellard | if (bs->inserted) {
|
207 | 67b915a5 | bellard | #ifndef _WIN32
|
208 | b338082b | bellard | /* we unmap the mapping so that it is written to the COW file */
|
209 | b338082b | bellard | if (bs->cow_bitmap_addr)
|
210 | b338082b | bellard | munmap(bs->cow_bitmap_addr, bs->cow_bitmap_size); |
211 | 67b915a5 | bellard | #endif
|
212 | b338082b | bellard | if (bs->cow_fd >= 0) |
213 | b338082b | bellard | close(bs->cow_fd); |
214 | b338082b | bellard | if (bs->fd >= 0) |
215 | b338082b | bellard | close(bs->fd); |
216 | b338082b | bellard | bs->inserted = 0;
|
217 | b338082b | bellard | |
218 | b338082b | bellard | /* call the change callback */
|
219 | b338082b | bellard | if (bs->change_cb)
|
220 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
221 | b338082b | bellard | } |
222 | b338082b | bellard | } |
223 | b338082b | bellard | |
224 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
225 | b338082b | bellard | { |
226 | b338082b | bellard | bdrv_close(bs); |
227 | b338082b | bellard | qemu_free(bs); |
228 | fc01f7e7 | bellard | } |
229 | fc01f7e7 | bellard | |
230 | 33e3963e | bellard | static inline void set_bit(uint8_t *bitmap, int64_t bitnum) |
231 | 33e3963e | bellard | { |
232 | 33e3963e | bellard | bitmap[bitnum / 8] |= (1 << (bitnum%8)); |
233 | 33e3963e | bellard | } |
234 | 33e3963e | bellard | |
235 | 33e3963e | bellard | static inline int is_bit_set(const uint8_t *bitmap, int64_t bitnum) |
236 | 33e3963e | bellard | { |
237 | 33e3963e | bellard | return !!(bitmap[bitnum / 8] & (1 << (bitnum%8))); |
238 | 33e3963e | bellard | } |
239 | 33e3963e | bellard | |
240 | 33e3963e | bellard | |
241 | 33e3963e | bellard | /* Return true if first block has been changed (ie. current version is
|
242 | 33e3963e | bellard | * in COW file). Set the number of continuous blocks for which that
|
243 | 33e3963e | bellard | * is true. */
|
244 | 33e3963e | bellard | static int is_changed(uint8_t *bitmap, |
245 | 33e3963e | bellard | int64_t sector_num, int nb_sectors,
|
246 | 33e3963e | bellard | int *num_same)
|
247 | 33e3963e | bellard | { |
248 | 33e3963e | bellard | int changed;
|
249 | 33e3963e | bellard | |
250 | 33e3963e | bellard | if (!bitmap || nb_sectors == 0) { |
251 | 33e3963e | bellard | *num_same = nb_sectors; |
252 | 33e3963e | bellard | return 0; |
253 | 33e3963e | bellard | } |
254 | 33e3963e | bellard | |
255 | 33e3963e | bellard | changed = is_bit_set(bitmap, sector_num); |
256 | 33e3963e | bellard | for (*num_same = 1; *num_same < nb_sectors; (*num_same)++) { |
257 | 33e3963e | bellard | if (is_bit_set(bitmap, sector_num + *num_same) != changed)
|
258 | 33e3963e | bellard | break;
|
259 | 33e3963e | bellard | } |
260 | 33e3963e | bellard | |
261 | 33e3963e | bellard | return changed;
|
262 | 33e3963e | bellard | } |
263 | 33e3963e | bellard | |
264 | 33e3963e | bellard | /* commit COW file into the raw image */
|
265 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
266 | 33e3963e | bellard | { |
267 | 33e3963e | bellard | int64_t i; |
268 | 33e3963e | bellard | uint8_t *cow_bitmap; |
269 | 33e3963e | bellard | |
270 | b338082b | bellard | if (!bs->inserted)
|
271 | b338082b | bellard | return -1; |
272 | b338082b | bellard | |
273 | 33e3963e | bellard | if (!bs->cow_bitmap) {
|
274 | 33e3963e | bellard | fprintf(stderr, "Already committed to %s\n", bs->filename);
|
275 | 33e3963e | bellard | return 0; |
276 | 33e3963e | bellard | } |
277 | 33e3963e | bellard | |
278 | 33e3963e | bellard | if (bs->read_only) {
|
279 | 33e3963e | bellard | fprintf(stderr, "Can't commit to %s: read-only\n", bs->filename);
|
280 | 33e3963e | bellard | return -1; |
281 | 33e3963e | bellard | } |
282 | 33e3963e | bellard | |
283 | 33e3963e | bellard | cow_bitmap = bs->cow_bitmap; |
284 | 33e3963e | bellard | for (i = 0; i < bs->total_sectors; i++) { |
285 | 33e3963e | bellard | if (is_bit_set(cow_bitmap, i)) {
|
286 | 33e3963e | bellard | unsigned char sector[512]; |
287 | 33e3963e | bellard | if (bdrv_read(bs, i, sector, 1) != 0) { |
288 | 33e3963e | bellard | fprintf(stderr, "Error reading sector %lli: aborting commit\n",
|
289 | 33e3963e | bellard | (long long)i); |
290 | 33e3963e | bellard | return -1; |
291 | 33e3963e | bellard | } |
292 | 33e3963e | bellard | |
293 | 33e3963e | bellard | /* Make bdrv_write write to real file for a moment. */
|
294 | 33e3963e | bellard | bs->cow_bitmap = NULL;
|
295 | 33e3963e | bellard | if (bdrv_write(bs, i, sector, 1) != 0) { |
296 | 33e3963e | bellard | fprintf(stderr, "Error writing sector %lli: aborting commit\n",
|
297 | 33e3963e | bellard | (long long)i); |
298 | 33e3963e | bellard | bs->cow_bitmap = cow_bitmap; |
299 | 33e3963e | bellard | return -1; |
300 | 33e3963e | bellard | } |
301 | 33e3963e | bellard | bs->cow_bitmap = cow_bitmap; |
302 | 33e3963e | bellard | } |
303 | 33e3963e | bellard | } |
304 | 33e3963e | bellard | fprintf(stderr, "Committed snapshot to %s\n", bs->filename);
|
305 | 33e3963e | bellard | return 0; |
306 | 33e3963e | bellard | } |
307 | 33e3963e | bellard | |
308 | fc01f7e7 | bellard | /* return -1 if error */
|
309 | fc01f7e7 | bellard | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
310 | fc01f7e7 | bellard | uint8_t *buf, int nb_sectors)
|
311 | fc01f7e7 | bellard | { |
312 | 33e3963e | bellard | int ret, n, fd;
|
313 | 33e3963e | bellard | int64_t offset; |
314 | 33e3963e | bellard | |
315 | b338082b | bellard | if (!bs->inserted)
|
316 | b338082b | bellard | return -1; |
317 | b338082b | bellard | |
318 | 33e3963e | bellard | while (nb_sectors > 0) { |
319 | 33e3963e | bellard | if (is_changed(bs->cow_bitmap, sector_num, nb_sectors, &n)) {
|
320 | 33e3963e | bellard | fd = bs->cow_fd; |
321 | 33e3963e | bellard | offset = bs->cow_sectors_offset; |
322 | cf98951b | bellard | } else if (sector_num == 0 && bs->boot_sector_enabled) { |
323 | cf98951b | bellard | memcpy(buf, bs->boot_sector_data, 512);
|
324 | cf98951b | bellard | n = 1;
|
325 | cf98951b | bellard | goto next;
|
326 | 33e3963e | bellard | } else {
|
327 | 33e3963e | bellard | fd = bs->fd; |
328 | 33e3963e | bellard | offset = 0;
|
329 | 33e3963e | bellard | } |
330 | fc01f7e7 | bellard | |
331 | 33e3963e | bellard | if (fd < 0) { |
332 | 33e3963e | bellard | /* no file, just return empty sectors */
|
333 | 33e3963e | bellard | memset(buf, 0, n * 512); |
334 | 33e3963e | bellard | } else {
|
335 | 33e3963e | bellard | offset += sector_num * 512;
|
336 | 33e3963e | bellard | lseek64(fd, offset, SEEK_SET); |
337 | 33e3963e | bellard | ret = read(fd, buf, n * 512);
|
338 | 33e3963e | bellard | if (ret != n * 512) { |
339 | 33e3963e | bellard | return -1; |
340 | 33e3963e | bellard | } |
341 | 33e3963e | bellard | } |
342 | cf98951b | bellard | next:
|
343 | 33e3963e | bellard | nb_sectors -= n; |
344 | 33e3963e | bellard | sector_num += n; |
345 | 33e3963e | bellard | buf += n * 512;
|
346 | 33e3963e | bellard | } |
347 | 33e3963e | bellard | return 0; |
348 | fc01f7e7 | bellard | } |
349 | fc01f7e7 | bellard | |
350 | fc01f7e7 | bellard | /* return -1 if error */
|
351 | fc01f7e7 | bellard | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
352 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
353 | fc01f7e7 | bellard | { |
354 | 33e3963e | bellard | int ret, fd, i;
|
355 | 33e3963e | bellard | int64_t offset, retl; |
356 | cf98951b | bellard | |
357 | b338082b | bellard | if (!bs->inserted)
|
358 | b338082b | bellard | return -1; |
359 | 0849bf08 | bellard | if (bs->read_only)
|
360 | 0849bf08 | bellard | return -1; |
361 | 0849bf08 | bellard | |
362 | 33e3963e | bellard | if (bs->cow_bitmap) {
|
363 | 33e3963e | bellard | fd = bs->cow_fd; |
364 | 33e3963e | bellard | offset = bs->cow_sectors_offset; |
365 | 33e3963e | bellard | } else {
|
366 | 33e3963e | bellard | fd = bs->fd; |
367 | 33e3963e | bellard | offset = 0;
|
368 | 33e3963e | bellard | } |
369 | 33e3963e | bellard | |
370 | 33e3963e | bellard | offset += sector_num * 512;
|
371 | 33e3963e | bellard | retl = lseek64(fd, offset, SEEK_SET); |
372 | 33e3963e | bellard | if (retl == -1) { |
373 | fc01f7e7 | bellard | return -1; |
374 | 33e3963e | bellard | } |
375 | 33e3963e | bellard | ret = write(fd, buf, nb_sectors * 512);
|
376 | 33e3963e | bellard | if (ret != nb_sectors * 512) { |
377 | 33e3963e | bellard | return -1; |
378 | 33e3963e | bellard | } |
379 | 33e3963e | bellard | |
380 | 33e3963e | bellard | if (bs->cow_bitmap) {
|
381 | 33e3963e | bellard | for (i = 0; i < nb_sectors; i++) |
382 | 33e3963e | bellard | set_bit(bs->cow_bitmap, sector_num + i); |
383 | 33e3963e | bellard | } |
384 | 33e3963e | bellard | return 0; |
385 | fc01f7e7 | bellard | } |
386 | fc01f7e7 | bellard | |
387 | fc01f7e7 | bellard | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
|
388 | fc01f7e7 | bellard | { |
389 | fc01f7e7 | bellard | *nb_sectors_ptr = bs->total_sectors; |
390 | fc01f7e7 | bellard | } |
391 | cf98951b | bellard | |
392 | cf98951b | bellard | /* force a given boot sector. */
|
393 | cf98951b | bellard | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) |
394 | cf98951b | bellard | { |
395 | cf98951b | bellard | bs->boot_sector_enabled = 1;
|
396 | cf98951b | bellard | if (size > 512) |
397 | cf98951b | bellard | size = 512;
|
398 | cf98951b | bellard | memcpy(bs->boot_sector_data, data, size); |
399 | cf98951b | bellard | memset(bs->boot_sector_data + size, 0, 512 - size); |
400 | cf98951b | bellard | } |
401 | b338082b | bellard | |
402 | b338082b | bellard | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
403 | b338082b | bellard | int cyls, int heads, int secs) |
404 | b338082b | bellard | { |
405 | b338082b | bellard | bs->cyls = cyls; |
406 | b338082b | bellard | bs->heads = heads; |
407 | b338082b | bellard | bs->secs = secs; |
408 | b338082b | bellard | } |
409 | b338082b | bellard | |
410 | b338082b | bellard | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
411 | b338082b | bellard | { |
412 | b338082b | bellard | bs->type = type; |
413 | b338082b | bellard | bs->removable = ((type == BDRV_TYPE_CDROM || |
414 | b338082b | bellard | type == BDRV_TYPE_FLOPPY)); |
415 | b338082b | bellard | } |
416 | b338082b | bellard | |
417 | b338082b | bellard | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
418 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
419 | b338082b | bellard | { |
420 | b338082b | bellard | *pcyls = bs->cyls; |
421 | b338082b | bellard | *pheads = bs->heads; |
422 | b338082b | bellard | *psecs = bs->secs; |
423 | b338082b | bellard | } |
424 | b338082b | bellard | |
425 | b338082b | bellard | int bdrv_get_type_hint(BlockDriverState *bs)
|
426 | b338082b | bellard | { |
427 | b338082b | bellard | return bs->type;
|
428 | b338082b | bellard | } |
429 | b338082b | bellard | |
430 | b338082b | bellard | int bdrv_is_removable(BlockDriverState *bs)
|
431 | b338082b | bellard | { |
432 | b338082b | bellard | return bs->removable;
|
433 | b338082b | bellard | } |
434 | b338082b | bellard | |
435 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
436 | b338082b | bellard | { |
437 | b338082b | bellard | return bs->read_only;
|
438 | b338082b | bellard | } |
439 | b338082b | bellard | |
440 | b338082b | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
441 | b338082b | bellard | { |
442 | b338082b | bellard | return bs->inserted;
|
443 | b338082b | bellard | } |
444 | b338082b | bellard | |
445 | b338082b | bellard | int bdrv_is_locked(BlockDriverState *bs)
|
446 | b338082b | bellard | { |
447 | b338082b | bellard | return bs->locked;
|
448 | b338082b | bellard | } |
449 | b338082b | bellard | |
450 | b338082b | bellard | void bdrv_set_locked(BlockDriverState *bs, int locked) |
451 | b338082b | bellard | { |
452 | b338082b | bellard | bs->locked = locked; |
453 | b338082b | bellard | } |
454 | b338082b | bellard | |
455 | b338082b | bellard | void bdrv_set_change_cb(BlockDriverState *bs,
|
456 | b338082b | bellard | void (*change_cb)(void *opaque), void *opaque) |
457 | b338082b | bellard | { |
458 | b338082b | bellard | bs->change_cb = change_cb; |
459 | b338082b | bellard | bs->change_opaque = opaque; |
460 | b338082b | bellard | } |
461 | b338082b | bellard | |
462 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
463 | b338082b | bellard | { |
464 | b338082b | bellard | BlockDriverState *bs; |
465 | b338082b | bellard | |
466 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
467 | b338082b | bellard | if (!strcmp(name, bs->device_name))
|
468 | b338082b | bellard | return bs;
|
469 | b338082b | bellard | } |
470 | b338082b | bellard | return NULL; |
471 | b338082b | bellard | } |
472 | b338082b | bellard | |
473 | b338082b | bellard | void bdrv_info(void) |
474 | b338082b | bellard | { |
475 | b338082b | bellard | BlockDriverState *bs; |
476 | b338082b | bellard | |
477 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
478 | b338082b | bellard | term_printf("%s:", bs->device_name);
|
479 | b338082b | bellard | term_printf(" type=");
|
480 | b338082b | bellard | switch(bs->type) {
|
481 | b338082b | bellard | case BDRV_TYPE_HD:
|
482 | b338082b | bellard | term_printf("hd");
|
483 | b338082b | bellard | break;
|
484 | b338082b | bellard | case BDRV_TYPE_CDROM:
|
485 | b338082b | bellard | term_printf("cdrom");
|
486 | b338082b | bellard | break;
|
487 | b338082b | bellard | case BDRV_TYPE_FLOPPY:
|
488 | b338082b | bellard | term_printf("floppy");
|
489 | b338082b | bellard | break;
|
490 | b338082b | bellard | } |
491 | b338082b | bellard | term_printf(" removable=%d", bs->removable);
|
492 | b338082b | bellard | if (bs->removable) {
|
493 | b338082b | bellard | term_printf(" locked=%d", bs->locked);
|
494 | b338082b | bellard | } |
495 | b338082b | bellard | if (bs->inserted) {
|
496 | b338082b | bellard | term_printf(" file=%s", bs->filename);
|
497 | b338082b | bellard | term_printf(" ro=%d", bs->read_only);
|
498 | b338082b | bellard | } else {
|
499 | b338082b | bellard | term_printf(" [not inserted]");
|
500 | b338082b | bellard | } |
501 | b338082b | bellard | term_printf("\n");
|
502 | b338082b | bellard | } |
503 | b338082b | bellard | } |