root / block.c @ d796321b
History | View | Annotate | Download (20.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 | 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 | 3b0d4f61 | bellard | #ifdef CONFIG_COCOA
|
36 | 3b0d4f61 | bellard | #include <paths.h> |
37 | 3b0d4f61 | bellard | #include <sys/param.h> |
38 | 3b0d4f61 | bellard | #include <IOKit/IOKitLib.h> |
39 | 3b0d4f61 | bellard | #include <IOKit/IOBSD.h> |
40 | 3b0d4f61 | bellard | #include <IOKit/storage/IOMediaBSDClient.h> |
41 | 3b0d4f61 | bellard | #include <IOKit/storage/IOMedia.h> |
42 | 3b0d4f61 | bellard | #include <IOKit/storage/IOCDMedia.h> |
43 | 3b0d4f61 | bellard | //#include <IOKit/storage/IOCDTypes.h>
|
44 | 3b0d4f61 | bellard | #include <CoreFoundation/CoreFoundation.h> |
45 | 3b0d4f61 | bellard | #endif
|
46 | 3b0d4f61 | bellard | |
47 | ec530c81 | bellard | #ifdef __sun__
|
48 | ec530c81 | bellard | #include <sys/dkio.h> |
49 | ec530c81 | bellard | #endif
|
50 | ec530c81 | bellard | |
51 | b338082b | bellard | static BlockDriverState *bdrv_first;
|
52 | ea2384d3 | bellard | static BlockDriver *first_drv;
|
53 | ea2384d3 | bellard | |
54 | 3b0d4f61 | bellard | #ifdef CONFIG_COCOA
|
55 | 3b0d4f61 | bellard | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
|
56 | 3b0d4f61 | bellard | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); |
57 | 3b0d4f61 | bellard | |
58 | 3b0d4f61 | bellard | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) |
59 | 3b0d4f61 | bellard | { |
60 | 3b0d4f61 | bellard | kern_return_t kernResult; |
61 | 3b0d4f61 | bellard | mach_port_t masterPort; |
62 | 3b0d4f61 | bellard | CFMutableDictionaryRef classesToMatch; |
63 | 3b0d4f61 | bellard | |
64 | 3b0d4f61 | bellard | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); |
65 | 3b0d4f61 | bellard | if ( KERN_SUCCESS != kernResult ) {
|
66 | 3b0d4f61 | bellard | printf( "IOMasterPort returned %d\n", kernResult );
|
67 | 3b0d4f61 | bellard | } |
68 | 3b0d4f61 | bellard | |
69 | 3b0d4f61 | bellard | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
70 | 3b0d4f61 | bellard | if ( classesToMatch == NULL ) { |
71 | 3b0d4f61 | bellard | printf( "IOServiceMatching returned a NULL dictionary.\n" );
|
72 | 3b0d4f61 | bellard | } else {
|
73 | 3b0d4f61 | bellard | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); |
74 | 3b0d4f61 | bellard | } |
75 | 3b0d4f61 | bellard | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); |
76 | 3b0d4f61 | bellard | if ( KERN_SUCCESS != kernResult )
|
77 | 3b0d4f61 | bellard | { |
78 | 3b0d4f61 | bellard | printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
|
79 | 3b0d4f61 | bellard | } |
80 | 3b0d4f61 | bellard | |
81 | 3b0d4f61 | bellard | return kernResult;
|
82 | 3b0d4f61 | bellard | } |
83 | 3b0d4f61 | bellard | |
84 | 3b0d4f61 | bellard | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
|
85 | 3b0d4f61 | bellard | { |
86 | 3b0d4f61 | bellard | io_object_t nextMedia; |
87 | 3b0d4f61 | bellard | kern_return_t kernResult = KERN_FAILURE; |
88 | 3b0d4f61 | bellard | *bsdPath = '\0';
|
89 | 3b0d4f61 | bellard | nextMedia = IOIteratorNext( mediaIterator ); |
90 | 3b0d4f61 | bellard | if ( nextMedia )
|
91 | 3b0d4f61 | bellard | { |
92 | 3b0d4f61 | bellard | CFTypeRef bsdPathAsCFString; |
93 | 3b0d4f61 | bellard | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
|
94 | 3b0d4f61 | bellard | if ( bsdPathAsCFString ) {
|
95 | 3b0d4f61 | bellard | size_t devPathLength; |
96 | 3b0d4f61 | bellard | strcpy( bsdPath, _PATH_DEV ); |
97 | 3b0d4f61 | bellard | strcat( bsdPath, "r" );
|
98 | 3b0d4f61 | bellard | devPathLength = strlen( bsdPath ); |
99 | 3b0d4f61 | bellard | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
|
100 | 3b0d4f61 | bellard | kernResult = KERN_SUCCESS; |
101 | 3b0d4f61 | bellard | } |
102 | 3b0d4f61 | bellard | CFRelease( bsdPathAsCFString ); |
103 | 3b0d4f61 | bellard | } |
104 | 3b0d4f61 | bellard | IOObjectRelease( nextMedia ); |
105 | 3b0d4f61 | bellard | } |
106 | 3b0d4f61 | bellard | |
107 | 3b0d4f61 | bellard | return kernResult;
|
108 | 3b0d4f61 | bellard | } |
109 | 3b0d4f61 | bellard | |
110 | 3b0d4f61 | bellard | #endif
|
111 | 3b0d4f61 | bellard | |
112 | ea2384d3 | bellard | void bdrv_register(BlockDriver *bdrv)
|
113 | ea2384d3 | bellard | { |
114 | ea2384d3 | bellard | bdrv->next = first_drv; |
115 | ea2384d3 | bellard | first_drv = bdrv; |
116 | ea2384d3 | bellard | } |
117 | b338082b | bellard | |
118 | b338082b | bellard | /* create a new block device (by default it is empty) */
|
119 | b338082b | bellard | BlockDriverState *bdrv_new(const char *device_name) |
120 | b338082b | bellard | { |
121 | b338082b | bellard | BlockDriverState **pbs, *bs; |
122 | b338082b | bellard | |
123 | b338082b | bellard | bs = qemu_mallocz(sizeof(BlockDriverState));
|
124 | b338082b | bellard | if(!bs)
|
125 | b338082b | bellard | return NULL; |
126 | b338082b | bellard | pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
|
127 | ea2384d3 | bellard | if (device_name[0] != '\0') { |
128 | ea2384d3 | bellard | /* insert at the end */
|
129 | ea2384d3 | bellard | pbs = &bdrv_first; |
130 | ea2384d3 | bellard | while (*pbs != NULL) |
131 | ea2384d3 | bellard | pbs = &(*pbs)->next; |
132 | ea2384d3 | bellard | *pbs = bs; |
133 | ea2384d3 | bellard | } |
134 | b338082b | bellard | return bs;
|
135 | b338082b | bellard | } |
136 | b338082b | bellard | |
137 | ea2384d3 | bellard | BlockDriver *bdrv_find_format(const char *format_name) |
138 | ea2384d3 | bellard | { |
139 | ea2384d3 | bellard | BlockDriver *drv1; |
140 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
141 | ea2384d3 | bellard | if (!strcmp(drv1->format_name, format_name))
|
142 | ea2384d3 | bellard | return drv1;
|
143 | ea2384d3 | bellard | } |
144 | ea2384d3 | bellard | return NULL; |
145 | ea2384d3 | bellard | } |
146 | ea2384d3 | bellard | |
147 | ea2384d3 | bellard | int bdrv_create(BlockDriver *drv,
|
148 | ea2384d3 | bellard | const char *filename, int64_t size_in_sectors, |
149 | ea2384d3 | bellard | const char *backing_file, int flags) |
150 | ea2384d3 | bellard | { |
151 | ea2384d3 | bellard | if (!drv->bdrv_create)
|
152 | ea2384d3 | bellard | return -ENOTSUP;
|
153 | ea2384d3 | bellard | return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
|
154 | ea2384d3 | bellard | } |
155 | ea2384d3 | bellard | |
156 | d5249393 | bellard | #ifdef _WIN32
|
157 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
158 | d5249393 | bellard | { |
159 | 95389c86 | bellard | char* p = strrchr(filename, '/'); |
160 | 95389c86 | bellard | |
161 | 95389c86 | bellard | if (p == NULL) |
162 | 95389c86 | bellard | return;
|
163 | 95389c86 | bellard | |
164 | d5249393 | bellard | /* XXX: find a better function */
|
165 | 95389c86 | bellard | tmpnam(p); |
166 | 95389c86 | bellard | *p = '/';
|
167 | d5249393 | bellard | } |
168 | d5249393 | bellard | #else
|
169 | 95389c86 | bellard | void get_tmp_filename(char *filename, int size) |
170 | fc01f7e7 | bellard | { |
171 | 67b915a5 | bellard | int fd;
|
172 | d5249393 | bellard | /* XXX: race condition possible */
|
173 | ea2384d3 | bellard | pstrcpy(filename, size, "/tmp/vl.XXXXXX");
|
174 | ea2384d3 | bellard | fd = mkstemp(filename); |
175 | ea2384d3 | bellard | close(fd); |
176 | ea2384d3 | bellard | } |
177 | d5249393 | bellard | #endif
|
178 | fc01f7e7 | bellard | |
179 | 7674e7bf | bellard | /* XXX: force raw format if block or character device ? It would
|
180 | 7674e7bf | bellard | simplify the BSD case */
|
181 | ea2384d3 | bellard | static BlockDriver *find_image_format(const char *filename) |
182 | ea2384d3 | bellard | { |
183 | ea2384d3 | bellard | int fd, ret, score, score_max;
|
184 | ea2384d3 | bellard | BlockDriver *drv1, *drv; |
185 | 7674e7bf | bellard | uint8_t *buf; |
186 | 7674e7bf | bellard | size_t bufsize = 1024;
|
187 | 33e3963e | bellard | |
188 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
189 | 712e7874 | bellard | if (fd < 0) { |
190 | 712e7874 | bellard | buf = NULL;
|
191 | 712e7874 | bellard | ret = 0;
|
192 | 712e7874 | bellard | } else {
|
193 | 7674e7bf | bellard | #ifdef DIOCGSECTORSIZE
|
194 | 712e7874 | bellard | { |
195 | 712e7874 | bellard | unsigned int sectorsize = 512; |
196 | 712e7874 | bellard | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
|
197 | 712e7874 | bellard | sectorsize > bufsize) |
198 | 712e7874 | bellard | bufsize = sectorsize; |
199 | 712e7874 | bellard | } |
200 | 7674e7bf | bellard | #endif
|
201 | 3b0d4f61 | bellard | #ifdef CONFIG_COCOA
|
202 | 3b0d4f61 | bellard | u_int32_t blockSize = 512;
|
203 | 3b0d4f61 | bellard | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
|
204 | 3b0d4f61 | bellard | bufsize = blockSize; |
205 | 3b0d4f61 | bellard | } |
206 | 3b0d4f61 | bellard | #endif
|
207 | 712e7874 | bellard | buf = qemu_malloc(bufsize); |
208 | 712e7874 | bellard | if (!buf)
|
209 | 712e7874 | bellard | return NULL; |
210 | 712e7874 | bellard | ret = read(fd, buf, bufsize); |
211 | 712e7874 | bellard | if (ret < 0) { |
212 | 712e7874 | bellard | close(fd); |
213 | 712e7874 | bellard | qemu_free(buf); |
214 | 712e7874 | bellard | return NULL; |
215 | 712e7874 | bellard | } |
216 | ea2384d3 | bellard | close(fd); |
217 | ea2384d3 | bellard | } |
218 | ea2384d3 | bellard | |
219 | ea2384d3 | bellard | drv = NULL;
|
220 | ea2384d3 | bellard | score_max = 0;
|
221 | ea2384d3 | bellard | for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { |
222 | ea2384d3 | bellard | score = drv1->bdrv_probe(buf, ret, filename); |
223 | ea2384d3 | bellard | if (score > score_max) {
|
224 | ea2384d3 | bellard | score_max = score; |
225 | ea2384d3 | bellard | drv = drv1; |
226 | 0849bf08 | bellard | } |
227 | fc01f7e7 | bellard | } |
228 | 712e7874 | bellard | qemu_free(buf); |
229 | ea2384d3 | bellard | return drv;
|
230 | ea2384d3 | bellard | } |
231 | ea2384d3 | bellard | |
232 | ea2384d3 | bellard | int bdrv_open(BlockDriverState *bs, const char *filename, int snapshot) |
233 | ea2384d3 | bellard | { |
234 | 3b0d4f61 | bellard | #ifdef CONFIG_COCOA
|
235 | 3b0d4f61 | bellard | if ( strncmp( filename, "/dev/cdrom", 10 ) == 0 ) { |
236 | 3b0d4f61 | bellard | kern_return_t kernResult; |
237 | 3b0d4f61 | bellard | io_iterator_t mediaIterator; |
238 | 3b0d4f61 | bellard | char bsdPath[ MAXPATHLEN ];
|
239 | 3b0d4f61 | bellard | int fd;
|
240 | 3b0d4f61 | bellard | |
241 | 3b0d4f61 | bellard | kernResult = FindEjectableCDMedia( &mediaIterator ); |
242 | 3b0d4f61 | bellard | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
|
243 | 3b0d4f61 | bellard | |
244 | 3b0d4f61 | bellard | if ( bsdPath[ 0 ] != '\0' ) { |
245 | 3b0d4f61 | bellard | strcat(bsdPath,"s0");
|
246 | 3b0d4f61 | bellard | /* some CDs don't have a partition 0 */
|
247 | 3b0d4f61 | bellard | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); |
248 | 3b0d4f61 | bellard | if (fd < 0) { |
249 | 3b0d4f61 | bellard | bsdPath[strlen(bsdPath)-1] = '1'; |
250 | 3b0d4f61 | bellard | } else {
|
251 | 3b0d4f61 | bellard | close(fd); |
252 | 3b0d4f61 | bellard | } |
253 | 3b0d4f61 | bellard | filename = bsdPath; |
254 | 3b0d4f61 | bellard | } |
255 | 3b0d4f61 | bellard | |
256 | 3b0d4f61 | bellard | if ( mediaIterator )
|
257 | 3b0d4f61 | bellard | IOObjectRelease( mediaIterator ); |
258 | 3b0d4f61 | bellard | } |
259 | 3b0d4f61 | bellard | #endif
|
260 | ea2384d3 | bellard | return bdrv_open2(bs, filename, snapshot, NULL); |
261 | ea2384d3 | bellard | } |
262 | ea2384d3 | bellard | |
263 | ea2384d3 | bellard | int bdrv_open2(BlockDriverState *bs, const char *filename, int snapshot, |
264 | ea2384d3 | bellard | BlockDriver *drv) |
265 | ea2384d3 | bellard | { |
266 | ea2384d3 | bellard | int ret;
|
267 | ea2384d3 | bellard | char tmp_filename[1024]; |
268 | ea2384d3 | bellard | |
269 | ea2384d3 | bellard | bs->read_only = 0;
|
270 | ea2384d3 | bellard | bs->is_temporary = 0;
|
271 | ea2384d3 | bellard | bs->encrypted = 0;
|
272 | 712e7874 | bellard | |
273 | ea2384d3 | bellard | if (snapshot) {
|
274 | ea2384d3 | bellard | BlockDriverState *bs1; |
275 | ea2384d3 | bellard | int64_t total_size; |
276 | ea2384d3 | bellard | |
277 | ea2384d3 | bellard | /* if snapshot, we create a temporary backing file and open it
|
278 | ea2384d3 | bellard | instead of opening 'filename' directly */
|
279 | 33e3963e | bellard | |
280 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
281 | ea2384d3 | bellard | bs1 = bdrv_new("");
|
282 | ea2384d3 | bellard | if (!bs1) {
|
283 | ea2384d3 | bellard | return -1; |
284 | ea2384d3 | bellard | } |
285 | ea2384d3 | bellard | if (bdrv_open(bs1, filename, 0) < 0) { |
286 | ea2384d3 | bellard | bdrv_delete(bs1); |
287 | ea2384d3 | bellard | return -1; |
288 | ea2384d3 | bellard | } |
289 | ea2384d3 | bellard | total_size = bs1->total_sectors; |
290 | ea2384d3 | bellard | bdrv_delete(bs1); |
291 | ea2384d3 | bellard | |
292 | ea2384d3 | bellard | get_tmp_filename(tmp_filename, sizeof(tmp_filename));
|
293 | ea2384d3 | bellard | /* XXX: use cow for linux as it is more efficient ? */
|
294 | ea2384d3 | bellard | if (bdrv_create(&bdrv_qcow, tmp_filename,
|
295 | ea2384d3 | bellard | total_size, filename, 0) < 0) { |
296 | ea2384d3 | bellard | return -1; |
297 | ea2384d3 | bellard | } |
298 | ea2384d3 | bellard | filename = tmp_filename; |
299 | ea2384d3 | bellard | bs->is_temporary = 1;
|
300 | ea2384d3 | bellard | } |
301 | 712e7874 | bellard | |
302 | ea2384d3 | bellard | pstrcpy(bs->filename, sizeof(bs->filename), filename);
|
303 | ea2384d3 | bellard | if (!drv) {
|
304 | ea2384d3 | bellard | drv = find_image_format(filename); |
305 | ea2384d3 | bellard | if (!drv)
|
306 | ea2384d3 | bellard | return -1; |
307 | ea2384d3 | bellard | } |
308 | ea2384d3 | bellard | bs->drv = drv; |
309 | ea2384d3 | bellard | bs->opaque = qemu_mallocz(drv->instance_size); |
310 | ea2384d3 | bellard | if (bs->opaque == NULL && drv->instance_size > 0) |
311 | ea2384d3 | bellard | return -1; |
312 | ea2384d3 | bellard | |
313 | ea2384d3 | bellard | ret = drv->bdrv_open(bs, filename); |
314 | ea2384d3 | bellard | if (ret < 0) { |
315 | ea2384d3 | bellard | qemu_free(bs->opaque); |
316 | ea2384d3 | bellard | return -1; |
317 | 33e3963e | bellard | } |
318 | 67b915a5 | bellard | #ifndef _WIN32
|
319 | ea2384d3 | bellard | if (bs->is_temporary) {
|
320 | ea2384d3 | bellard | unlink(filename); |
321 | ea2384d3 | bellard | } |
322 | ea2384d3 | bellard | #endif
|
323 | ea2384d3 | bellard | if (bs->backing_file[0] != '\0' && drv->bdrv_is_allocated) { |
324 | ea2384d3 | bellard | /* if there is a backing file, use it */
|
325 | ea2384d3 | bellard | bs->backing_hd = bdrv_new("");
|
326 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
327 | ea2384d3 | bellard | fail:
|
328 | ea2384d3 | bellard | bdrv_close(bs); |
329 | ea2384d3 | bellard | return -1; |
330 | 33e3963e | bellard | } |
331 | ea2384d3 | bellard | if (bdrv_open(bs->backing_hd, bs->backing_file, 0) < 0) |
332 | 33e3963e | bellard | goto fail;
|
333 | 33e3963e | bellard | } |
334 | 33e3963e | bellard | |
335 | b338082b | bellard | bs->inserted = 1;
|
336 | b338082b | bellard | |
337 | b338082b | bellard | /* call the change callback */
|
338 | b338082b | bellard | if (bs->change_cb)
|
339 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
340 | b338082b | bellard | |
341 | b338082b | bellard | return 0; |
342 | fc01f7e7 | bellard | } |
343 | fc01f7e7 | bellard | |
344 | fc01f7e7 | bellard | void bdrv_close(BlockDriverState *bs)
|
345 | fc01f7e7 | bellard | { |
346 | b338082b | bellard | if (bs->inserted) {
|
347 | ea2384d3 | bellard | if (bs->backing_hd)
|
348 | ea2384d3 | bellard | bdrv_delete(bs->backing_hd); |
349 | ea2384d3 | bellard | bs->drv->bdrv_close(bs); |
350 | ea2384d3 | bellard | qemu_free(bs->opaque); |
351 | ea2384d3 | bellard | #ifdef _WIN32
|
352 | ea2384d3 | bellard | if (bs->is_temporary) {
|
353 | ea2384d3 | bellard | unlink(bs->filename); |
354 | ea2384d3 | bellard | } |
355 | 67b915a5 | bellard | #endif
|
356 | ea2384d3 | bellard | bs->opaque = NULL;
|
357 | ea2384d3 | bellard | bs->drv = NULL;
|
358 | b338082b | bellard | bs->inserted = 0;
|
359 | b338082b | bellard | |
360 | b338082b | bellard | /* call the change callback */
|
361 | b338082b | bellard | if (bs->change_cb)
|
362 | b338082b | bellard | bs->change_cb(bs->change_opaque); |
363 | b338082b | bellard | } |
364 | b338082b | bellard | } |
365 | b338082b | bellard | |
366 | b338082b | bellard | void bdrv_delete(BlockDriverState *bs)
|
367 | b338082b | bellard | { |
368 | ea2384d3 | bellard | /* XXX: remove the driver list */
|
369 | b338082b | bellard | bdrv_close(bs); |
370 | b338082b | bellard | qemu_free(bs); |
371 | fc01f7e7 | bellard | } |
372 | fc01f7e7 | bellard | |
373 | 33e3963e | bellard | /* commit COW file into the raw image */
|
374 | 33e3963e | bellard | int bdrv_commit(BlockDriverState *bs)
|
375 | 33e3963e | bellard | { |
376 | 33e3963e | bellard | int64_t i; |
377 | ea2384d3 | bellard | int n, j;
|
378 | ea2384d3 | bellard | unsigned char sector[512]; |
379 | 33e3963e | bellard | |
380 | b338082b | bellard | if (!bs->inserted)
|
381 | ea2384d3 | bellard | return -ENOENT;
|
382 | 33e3963e | bellard | |
383 | 33e3963e | bellard | if (bs->read_only) {
|
384 | ea2384d3 | bellard | return -EACCES;
|
385 | 33e3963e | bellard | } |
386 | 33e3963e | bellard | |
387 | ea2384d3 | bellard | if (!bs->backing_hd) {
|
388 | ea2384d3 | bellard | return -ENOTSUP;
|
389 | ea2384d3 | bellard | } |
390 | 33e3963e | bellard | |
391 | ea2384d3 | bellard | for (i = 0; i < bs->total_sectors;) { |
392 | ea2384d3 | bellard | if (bs->drv->bdrv_is_allocated(bs, i, 65536, &n)) { |
393 | ea2384d3 | bellard | for(j = 0; j < n; j++) { |
394 | ea2384d3 | bellard | if (bdrv_read(bs, i, sector, 1) != 0) { |
395 | ea2384d3 | bellard | return -EIO;
|
396 | ea2384d3 | bellard | } |
397 | ea2384d3 | bellard | |
398 | ea2384d3 | bellard | if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { |
399 | ea2384d3 | bellard | return -EIO;
|
400 | ea2384d3 | bellard | } |
401 | ea2384d3 | bellard | i++; |
402 | 33e3963e | bellard | } |
403 | ea2384d3 | bellard | } else {
|
404 | ea2384d3 | bellard | i += n; |
405 | ea2384d3 | bellard | } |
406 | 33e3963e | bellard | } |
407 | 95389c86 | bellard | |
408 | 95389c86 | bellard | if (bs->drv->bdrv_make_empty)
|
409 | 95389c86 | bellard | return bs->drv->bdrv_make_empty(bs);
|
410 | 95389c86 | bellard | |
411 | 33e3963e | bellard | return 0; |
412 | 33e3963e | bellard | } |
413 | 33e3963e | bellard | |
414 | fc01f7e7 | bellard | /* return -1 if error */
|
415 | fc01f7e7 | bellard | int bdrv_read(BlockDriverState *bs, int64_t sector_num,
|
416 | fc01f7e7 | bellard | uint8_t *buf, int nb_sectors)
|
417 | fc01f7e7 | bellard | { |
418 | ea2384d3 | bellard | int ret, n;
|
419 | ea2384d3 | bellard | BlockDriver *drv = bs->drv; |
420 | ea2384d3 | bellard | |
421 | b338082b | bellard | if (!bs->inserted)
|
422 | b338082b | bellard | return -1; |
423 | b338082b | bellard | |
424 | 33e3963e | bellard | while (nb_sectors > 0) { |
425 | ea2384d3 | bellard | if (sector_num == 0 && bs->boot_sector_enabled) { |
426 | cf98951b | bellard | memcpy(buf, bs->boot_sector_data, 512);
|
427 | cf98951b | bellard | n = 1;
|
428 | ea2384d3 | bellard | } else if (bs->backing_hd) { |
429 | ea2384d3 | bellard | if (drv->bdrv_is_allocated(bs, sector_num, nb_sectors, &n)) {
|
430 | ea2384d3 | bellard | ret = drv->bdrv_read(bs, sector_num, buf, n); |
431 | ea2384d3 | bellard | if (ret < 0) |
432 | ea2384d3 | bellard | return -1; |
433 | ea2384d3 | bellard | } else {
|
434 | ea2384d3 | bellard | /* read from the base image */
|
435 | ea2384d3 | bellard | ret = bdrv_read(bs->backing_hd, sector_num, buf, n); |
436 | ea2384d3 | bellard | if (ret < 0) |
437 | ea2384d3 | bellard | return -1; |
438 | ea2384d3 | bellard | } |
439 | 33e3963e | bellard | } else {
|
440 | ea2384d3 | bellard | ret = drv->bdrv_read(bs, sector_num, buf, nb_sectors); |
441 | ea2384d3 | bellard | if (ret < 0) |
442 | 33e3963e | bellard | return -1; |
443 | ea2384d3 | bellard | /* no need to loop */
|
444 | ea2384d3 | bellard | break;
|
445 | 33e3963e | bellard | } |
446 | 33e3963e | bellard | nb_sectors -= n; |
447 | 33e3963e | bellard | sector_num += n; |
448 | 33e3963e | bellard | buf += n * 512;
|
449 | 33e3963e | bellard | } |
450 | 33e3963e | bellard | return 0; |
451 | fc01f7e7 | bellard | } |
452 | fc01f7e7 | bellard | |
453 | fc01f7e7 | bellard | /* return -1 if error */
|
454 | fc01f7e7 | bellard | int bdrv_write(BlockDriverState *bs, int64_t sector_num,
|
455 | fc01f7e7 | bellard | const uint8_t *buf, int nb_sectors) |
456 | fc01f7e7 | bellard | { |
457 | b338082b | bellard | if (!bs->inserted)
|
458 | b338082b | bellard | return -1; |
459 | 0849bf08 | bellard | if (bs->read_only)
|
460 | 0849bf08 | bellard | return -1; |
461 | 79639d42 | bellard | if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { |
462 | 79639d42 | bellard | memcpy(bs->boot_sector_data, buf, 512);
|
463 | 79639d42 | bellard | } |
464 | ea2384d3 | bellard | return bs->drv->bdrv_write(bs, sector_num, buf, nb_sectors);
|
465 | fc01f7e7 | bellard | } |
466 | fc01f7e7 | bellard | |
467 | fc01f7e7 | bellard | void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
|
468 | fc01f7e7 | bellard | { |
469 | fc01f7e7 | bellard | *nb_sectors_ptr = bs->total_sectors; |
470 | fc01f7e7 | bellard | } |
471 | cf98951b | bellard | |
472 | cf98951b | bellard | /* force a given boot sector. */
|
473 | cf98951b | bellard | void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) |
474 | cf98951b | bellard | { |
475 | cf98951b | bellard | bs->boot_sector_enabled = 1;
|
476 | cf98951b | bellard | if (size > 512) |
477 | cf98951b | bellard | size = 512;
|
478 | cf98951b | bellard | memcpy(bs->boot_sector_data, data, size); |
479 | cf98951b | bellard | memset(bs->boot_sector_data + size, 0, 512 - size); |
480 | cf98951b | bellard | } |
481 | b338082b | bellard | |
482 | b338082b | bellard | void bdrv_set_geometry_hint(BlockDriverState *bs,
|
483 | b338082b | bellard | int cyls, int heads, int secs) |
484 | b338082b | bellard | { |
485 | b338082b | bellard | bs->cyls = cyls; |
486 | b338082b | bellard | bs->heads = heads; |
487 | b338082b | bellard | bs->secs = secs; |
488 | b338082b | bellard | } |
489 | b338082b | bellard | |
490 | b338082b | bellard | void bdrv_set_type_hint(BlockDriverState *bs, int type) |
491 | b338082b | bellard | { |
492 | b338082b | bellard | bs->type = type; |
493 | b338082b | bellard | bs->removable = ((type == BDRV_TYPE_CDROM || |
494 | b338082b | bellard | type == BDRV_TYPE_FLOPPY)); |
495 | b338082b | bellard | } |
496 | b338082b | bellard | |
497 | 46d4767d | bellard | void bdrv_set_translation_hint(BlockDriverState *bs, int translation) |
498 | 46d4767d | bellard | { |
499 | 46d4767d | bellard | bs->translation = translation; |
500 | 46d4767d | bellard | } |
501 | 46d4767d | bellard | |
502 | b338082b | bellard | void bdrv_get_geometry_hint(BlockDriverState *bs,
|
503 | b338082b | bellard | int *pcyls, int *pheads, int *psecs) |
504 | b338082b | bellard | { |
505 | b338082b | bellard | *pcyls = bs->cyls; |
506 | b338082b | bellard | *pheads = bs->heads; |
507 | b338082b | bellard | *psecs = bs->secs; |
508 | b338082b | bellard | } |
509 | b338082b | bellard | |
510 | b338082b | bellard | int bdrv_get_type_hint(BlockDriverState *bs)
|
511 | b338082b | bellard | { |
512 | b338082b | bellard | return bs->type;
|
513 | b338082b | bellard | } |
514 | b338082b | bellard | |
515 | 46d4767d | bellard | int bdrv_get_translation_hint(BlockDriverState *bs)
|
516 | 46d4767d | bellard | { |
517 | 46d4767d | bellard | return bs->translation;
|
518 | 46d4767d | bellard | } |
519 | 46d4767d | bellard | |
520 | b338082b | bellard | int bdrv_is_removable(BlockDriverState *bs)
|
521 | b338082b | bellard | { |
522 | b338082b | bellard | return bs->removable;
|
523 | b338082b | bellard | } |
524 | b338082b | bellard | |
525 | b338082b | bellard | int bdrv_is_read_only(BlockDriverState *bs)
|
526 | b338082b | bellard | { |
527 | b338082b | bellard | return bs->read_only;
|
528 | b338082b | bellard | } |
529 | b338082b | bellard | |
530 | b338082b | bellard | int bdrv_is_inserted(BlockDriverState *bs)
|
531 | b338082b | bellard | { |
532 | b338082b | bellard | return bs->inserted;
|
533 | b338082b | bellard | } |
534 | b338082b | bellard | |
535 | b338082b | bellard | int bdrv_is_locked(BlockDriverState *bs)
|
536 | b338082b | bellard | { |
537 | b338082b | bellard | return bs->locked;
|
538 | b338082b | bellard | } |
539 | b338082b | bellard | |
540 | b338082b | bellard | void bdrv_set_locked(BlockDriverState *bs, int locked) |
541 | b338082b | bellard | { |
542 | b338082b | bellard | bs->locked = locked; |
543 | b338082b | bellard | } |
544 | b338082b | bellard | |
545 | b338082b | bellard | void bdrv_set_change_cb(BlockDriverState *bs,
|
546 | b338082b | bellard | void (*change_cb)(void *opaque), void *opaque) |
547 | b338082b | bellard | { |
548 | b338082b | bellard | bs->change_cb = change_cb; |
549 | b338082b | bellard | bs->change_opaque = opaque; |
550 | b338082b | bellard | } |
551 | b338082b | bellard | |
552 | ea2384d3 | bellard | int bdrv_is_encrypted(BlockDriverState *bs)
|
553 | ea2384d3 | bellard | { |
554 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted)
|
555 | ea2384d3 | bellard | return 1; |
556 | ea2384d3 | bellard | return bs->encrypted;
|
557 | ea2384d3 | bellard | } |
558 | ea2384d3 | bellard | |
559 | ea2384d3 | bellard | int bdrv_set_key(BlockDriverState *bs, const char *key) |
560 | ea2384d3 | bellard | { |
561 | ea2384d3 | bellard | int ret;
|
562 | ea2384d3 | bellard | if (bs->backing_hd && bs->backing_hd->encrypted) {
|
563 | ea2384d3 | bellard | ret = bdrv_set_key(bs->backing_hd, key); |
564 | ea2384d3 | bellard | if (ret < 0) |
565 | ea2384d3 | bellard | return ret;
|
566 | ea2384d3 | bellard | if (!bs->encrypted)
|
567 | ea2384d3 | bellard | return 0; |
568 | ea2384d3 | bellard | } |
569 | ea2384d3 | bellard | if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
|
570 | ea2384d3 | bellard | return -1; |
571 | ea2384d3 | bellard | return bs->drv->bdrv_set_key(bs, key);
|
572 | ea2384d3 | bellard | } |
573 | ea2384d3 | bellard | |
574 | ea2384d3 | bellard | void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) |
575 | ea2384d3 | bellard | { |
576 | ea2384d3 | bellard | if (!bs->inserted || !bs->drv) {
|
577 | ea2384d3 | bellard | buf[0] = '\0'; |
578 | ea2384d3 | bellard | } else {
|
579 | ea2384d3 | bellard | pstrcpy(buf, buf_size, bs->drv->format_name); |
580 | ea2384d3 | bellard | } |
581 | ea2384d3 | bellard | } |
582 | ea2384d3 | bellard | |
583 | ea2384d3 | bellard | void bdrv_iterate_format(void (*it)(void *opaque, const char *name), |
584 | ea2384d3 | bellard | void *opaque)
|
585 | ea2384d3 | bellard | { |
586 | ea2384d3 | bellard | BlockDriver *drv; |
587 | ea2384d3 | bellard | |
588 | ea2384d3 | bellard | for (drv = first_drv; drv != NULL; drv = drv->next) { |
589 | ea2384d3 | bellard | it(opaque, drv->format_name); |
590 | ea2384d3 | bellard | } |
591 | ea2384d3 | bellard | } |
592 | ea2384d3 | bellard | |
593 | b338082b | bellard | BlockDriverState *bdrv_find(const char *name) |
594 | b338082b | bellard | { |
595 | b338082b | bellard | BlockDriverState *bs; |
596 | b338082b | bellard | |
597 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
598 | b338082b | bellard | if (!strcmp(name, bs->device_name))
|
599 | b338082b | bellard | return bs;
|
600 | b338082b | bellard | } |
601 | b338082b | bellard | return NULL; |
602 | b338082b | bellard | } |
603 | b338082b | bellard | |
604 | 81d0912d | bellard | void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) |
605 | 81d0912d | bellard | { |
606 | 81d0912d | bellard | BlockDriverState *bs; |
607 | 81d0912d | bellard | |
608 | 81d0912d | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
609 | 81d0912d | bellard | it(opaque, bs->device_name); |
610 | 81d0912d | bellard | } |
611 | 81d0912d | bellard | } |
612 | 81d0912d | bellard | |
613 | ea2384d3 | bellard | const char *bdrv_get_device_name(BlockDriverState *bs) |
614 | ea2384d3 | bellard | { |
615 | ea2384d3 | bellard | return bs->device_name;
|
616 | ea2384d3 | bellard | } |
617 | ea2384d3 | bellard | |
618 | 7a6cba61 | pbrook | void bdrv_flush(BlockDriverState *bs)
|
619 | 7a6cba61 | pbrook | { |
620 | 7a6cba61 | pbrook | if (bs->drv->bdrv_flush)
|
621 | 7a6cba61 | pbrook | bs->drv->bdrv_flush(bs); |
622 | 7a6cba61 | pbrook | if (bs->backing_hd)
|
623 | 7a6cba61 | pbrook | bdrv_flush(bs->backing_hd); |
624 | 7a6cba61 | pbrook | } |
625 | 7a6cba61 | pbrook | |
626 | b338082b | bellard | void bdrv_info(void) |
627 | b338082b | bellard | { |
628 | b338082b | bellard | BlockDriverState *bs; |
629 | b338082b | bellard | |
630 | b338082b | bellard | for (bs = bdrv_first; bs != NULL; bs = bs->next) { |
631 | b338082b | bellard | term_printf("%s:", bs->device_name);
|
632 | b338082b | bellard | term_printf(" type=");
|
633 | b338082b | bellard | switch(bs->type) {
|
634 | b338082b | bellard | case BDRV_TYPE_HD:
|
635 | b338082b | bellard | term_printf("hd");
|
636 | b338082b | bellard | break;
|
637 | b338082b | bellard | case BDRV_TYPE_CDROM:
|
638 | b338082b | bellard | term_printf("cdrom");
|
639 | b338082b | bellard | break;
|
640 | b338082b | bellard | case BDRV_TYPE_FLOPPY:
|
641 | b338082b | bellard | term_printf("floppy");
|
642 | b338082b | bellard | break;
|
643 | b338082b | bellard | } |
644 | b338082b | bellard | term_printf(" removable=%d", bs->removable);
|
645 | b338082b | bellard | if (bs->removable) {
|
646 | b338082b | bellard | term_printf(" locked=%d", bs->locked);
|
647 | b338082b | bellard | } |
648 | b338082b | bellard | if (bs->inserted) {
|
649 | b338082b | bellard | term_printf(" file=%s", bs->filename);
|
650 | ea2384d3 | bellard | if (bs->backing_file[0] != '\0') |
651 | ea2384d3 | bellard | term_printf(" backing_file=%s", bs->backing_file);
|
652 | b338082b | bellard | term_printf(" ro=%d", bs->read_only);
|
653 | ea2384d3 | bellard | term_printf(" drv=%s", bs->drv->format_name);
|
654 | ea2384d3 | bellard | if (bs->encrypted)
|
655 | ea2384d3 | bellard | term_printf(" encrypted");
|
656 | b338082b | bellard | } else {
|
657 | b338082b | bellard | term_printf(" [not inserted]");
|
658 | b338082b | bellard | } |
659 | b338082b | bellard | term_printf("\n");
|
660 | b338082b | bellard | } |
661 | b338082b | bellard | } |
662 | ea2384d3 | bellard | |
663 | ea2384d3 | bellard | /**************************************************************/
|
664 | ea2384d3 | bellard | /* RAW block driver */
|
665 | ea2384d3 | bellard | |
666 | ea2384d3 | bellard | typedef struct BDRVRawState { |
667 | ea2384d3 | bellard | int fd;
|
668 | ea2384d3 | bellard | } BDRVRawState; |
669 | ea2384d3 | bellard | |
670 | ea2384d3 | bellard | static int raw_probe(const uint8_t *buf, int buf_size, const char *filename) |
671 | ea2384d3 | bellard | { |
672 | ea2384d3 | bellard | return 1; /* maybe */ |
673 | ea2384d3 | bellard | } |
674 | ea2384d3 | bellard | |
675 | ea2384d3 | bellard | static int raw_open(BlockDriverState *bs, const char *filename) |
676 | ea2384d3 | bellard | { |
677 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
678 | ea2384d3 | bellard | int fd;
|
679 | ea2384d3 | bellard | int64_t size; |
680 | e5484d33 | bellard | #ifdef _BSD
|
681 | e5484d33 | bellard | struct stat sb;
|
682 | e5484d33 | bellard | #endif
|
683 | ec530c81 | bellard | #ifdef __sun__
|
684 | ec530c81 | bellard | struct dk_minfo minfo;
|
685 | ec530c81 | bellard | int rv;
|
686 | ec530c81 | bellard | #endif
|
687 | ea2384d3 | bellard | |
688 | ea2384d3 | bellard | fd = open(filename, O_RDWR | O_BINARY | O_LARGEFILE); |
689 | ea2384d3 | bellard | if (fd < 0) { |
690 | ea2384d3 | bellard | fd = open(filename, O_RDONLY | O_BINARY | O_LARGEFILE); |
691 | ea2384d3 | bellard | if (fd < 0) |
692 | ea2384d3 | bellard | return -1; |
693 | ea2384d3 | bellard | bs->read_only = 1;
|
694 | ea2384d3 | bellard | } |
695 | 7674e7bf | bellard | #ifdef _BSD
|
696 | e5484d33 | bellard | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
|
697 | 7674e7bf | bellard | #ifdef DIOCGMEDIASIZE
|
698 | e5484d33 | bellard | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
|
699 | 7674e7bf | bellard | #endif
|
700 | 3b0d4f61 | bellard | #ifdef CONFIG_COCOA
|
701 | 3b0d4f61 | bellard | size = LONG_LONG_MAX; |
702 | 3b0d4f61 | bellard | #else
|
703 | 3b0d4f61 | bellard | size = lseek(fd, 0LL, SEEK_END);
|
704 | 3b0d4f61 | bellard | #endif
|
705 | 7674e7bf | bellard | } else
|
706 | 7674e7bf | bellard | #endif
|
707 | ec530c81 | bellard | #ifdef __sun__
|
708 | ec530c81 | bellard | /*
|
709 | ec530c81 | bellard | * use the DKIOCGMEDIAINFO ioctl to read the size.
|
710 | ec530c81 | bellard | */
|
711 | ec530c81 | bellard | rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); |
712 | ec530c81 | bellard | if ( rv != -1 ) { |
713 | ec530c81 | bellard | size = minfo.dki_lbsize * minfo.dki_capacity; |
714 | ec530c81 | bellard | } else /* there are reports that lseek on some devices |
715 | ec530c81 | bellard | fails, but irc discussion said that contingency
|
716 | ec530c81 | bellard | on contingency was overkill */
|
717 | ec530c81 | bellard | #endif
|
718 | 7674e7bf | bellard | { |
719 | 7674e7bf | bellard | size = lseek(fd, 0, SEEK_END);
|
720 | 7674e7bf | bellard | } |
721 | c747cd1f | bellard | #ifdef _WIN32
|
722 | c747cd1f | bellard | /* On Windows hosts it can happen that we're unable to get file size
|
723 | c747cd1f | bellard | for CD-ROM raw device (it's inherent limitation of the CDFS driver). */
|
724 | c747cd1f | bellard | if (size == -1) |
725 | c747cd1f | bellard | size = LONG_LONG_MAX; |
726 | c747cd1f | bellard | #endif
|
727 | ea2384d3 | bellard | bs->total_sectors = size / 512;
|
728 | ea2384d3 | bellard | s->fd = fd; |
729 | ea2384d3 | bellard | return 0; |
730 | ea2384d3 | bellard | } |
731 | ea2384d3 | bellard | |
732 | ea2384d3 | bellard | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
733 | ea2384d3 | bellard | uint8_t *buf, int nb_sectors)
|
734 | ea2384d3 | bellard | { |
735 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
736 | ea2384d3 | bellard | int ret;
|
737 | ea2384d3 | bellard | |
738 | d5249393 | bellard | lseek(s->fd, sector_num * 512, SEEK_SET);
|
739 | ea2384d3 | bellard | ret = read(s->fd, buf, nb_sectors * 512);
|
740 | ea2384d3 | bellard | if (ret != nb_sectors * 512) |
741 | ea2384d3 | bellard | return -1; |
742 | ea2384d3 | bellard | return 0; |
743 | ea2384d3 | bellard | } |
744 | ea2384d3 | bellard | |
745 | ea2384d3 | bellard | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
746 | ea2384d3 | bellard | const uint8_t *buf, int nb_sectors) |
747 | ea2384d3 | bellard | { |
748 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
749 | ea2384d3 | bellard | int ret;
|
750 | ea2384d3 | bellard | |
751 | d5249393 | bellard | lseek(s->fd, sector_num * 512, SEEK_SET);
|
752 | ea2384d3 | bellard | ret = write(s->fd, buf, nb_sectors * 512);
|
753 | ea2384d3 | bellard | if (ret != nb_sectors * 512) |
754 | ea2384d3 | bellard | return -1; |
755 | ea2384d3 | bellard | return 0; |
756 | ea2384d3 | bellard | } |
757 | ea2384d3 | bellard | |
758 | e2731add | bellard | static void raw_close(BlockDriverState *bs) |
759 | ea2384d3 | bellard | { |
760 | ea2384d3 | bellard | BDRVRawState *s = bs->opaque; |
761 | ea2384d3 | bellard | close(s->fd); |
762 | ea2384d3 | bellard | } |
763 | ea2384d3 | bellard | |
764 | ea2384d3 | bellard | static int raw_create(const char *filename, int64_t total_size, |
765 | ea2384d3 | bellard | const char *backing_file, int flags) |
766 | ea2384d3 | bellard | { |
767 | ea2384d3 | bellard | int fd;
|
768 | ea2384d3 | bellard | |
769 | ea2384d3 | bellard | if (flags || backing_file)
|
770 | ea2384d3 | bellard | return -ENOTSUP;
|
771 | ea2384d3 | bellard | |
772 | ea2384d3 | bellard | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, |
773 | ea2384d3 | bellard | 0644);
|
774 | ea2384d3 | bellard | if (fd < 0) |
775 | ea2384d3 | bellard | return -EIO;
|
776 | d5249393 | bellard | ftruncate(fd, total_size * 512);
|
777 | ea2384d3 | bellard | close(fd); |
778 | ea2384d3 | bellard | return 0; |
779 | ea2384d3 | bellard | } |
780 | ea2384d3 | bellard | |
781 | 7a6cba61 | pbrook | static void raw_flush(BlockDriverState *bs) |
782 | 7a6cba61 | pbrook | { |
783 | 7a6cba61 | pbrook | BDRVRawState *s = bs->opaque; |
784 | 7a6cba61 | pbrook | fsync(s->fd); |
785 | 7a6cba61 | pbrook | } |
786 | 7a6cba61 | pbrook | |
787 | ea2384d3 | bellard | BlockDriver bdrv_raw = { |
788 | ea2384d3 | bellard | "raw",
|
789 | ea2384d3 | bellard | sizeof(BDRVRawState),
|
790 | ea2384d3 | bellard | raw_probe, |
791 | ea2384d3 | bellard | raw_open, |
792 | ea2384d3 | bellard | raw_read, |
793 | ea2384d3 | bellard | raw_write, |
794 | ea2384d3 | bellard | raw_close, |
795 | ea2384d3 | bellard | raw_create, |
796 | 7a6cba61 | pbrook | raw_flush, |
797 | ea2384d3 | bellard | }; |
798 | ea2384d3 | bellard | |
799 | ea2384d3 | bellard | void bdrv_init(void) |
800 | ea2384d3 | bellard | { |
801 | ea2384d3 | bellard | bdrv_register(&bdrv_raw); |
802 | ea2384d3 | bellard | #ifndef _WIN32
|
803 | ea2384d3 | bellard | bdrv_register(&bdrv_cow); |
804 | ea2384d3 | bellard | #endif
|
805 | ea2384d3 | bellard | bdrv_register(&bdrv_qcow); |
806 | ea2384d3 | bellard | bdrv_register(&bdrv_vmdk); |
807 | 3c56521b | bellard | bdrv_register(&bdrv_cloop); |
808 | 585d0ed9 | bellard | bdrv_register(&bdrv_dmg); |
809 | a8753c34 | bellard | bdrv_register(&bdrv_bochs); |
810 | 6a0f9e82 | bellard | bdrv_register(&bdrv_vpc); |
811 | 712e7874 | bellard | bdrv_register(&bdrv_vvfat); |
812 | ea2384d3 | bellard | } |