root / block-raw-posix.c @ 395f153d
History | View | Annotate | Download (30.9 kB)
1 | 83f64091 | bellard | /*
|
---|---|---|---|
2 | 223d4670 | ths | * Block driver for RAW files (posix)
|
3 | 5fafdf24 | ths | *
|
4 | 83f64091 | bellard | * Copyright (c) 2006 Fabrice Bellard
|
5 | 5fafdf24 | ths | *
|
6 | 83f64091 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 83f64091 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 83f64091 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 83f64091 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 83f64091 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 83f64091 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 83f64091 | bellard | *
|
13 | 83f64091 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 83f64091 | bellard | * all copies or substantial portions of the Software.
|
15 | 83f64091 | bellard | *
|
16 | 83f64091 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 83f64091 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 83f64091 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 83f64091 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 83f64091 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 83f64091 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 83f64091 | bellard | * THE SOFTWARE.
|
23 | 83f64091 | bellard | */
|
24 | faf07963 | pbrook | #include "qemu-common.h" |
25 | 87ecb68b | pbrook | #include "qemu-timer.h" |
26 | baf35cb9 | aliguori | #include "qemu-char.h" |
27 | 83f64091 | bellard | #include "block_int.h" |
28 | 83f64091 | bellard | #include <assert.h> |
29 | 414f0dab | blueswir1 | #ifdef CONFIG_AIO
|
30 | 3c529d93 | aliguori | #include "posix-aio-compat.h" |
31 | 414f0dab | blueswir1 | #endif
|
32 | 83f64091 | bellard | |
33 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
34 | 83f64091 | bellard | #include <paths.h> |
35 | 83f64091 | bellard | #include <sys/param.h> |
36 | 83f64091 | bellard | #include <IOKit/IOKitLib.h> |
37 | 83f64091 | bellard | #include <IOKit/IOBSD.h> |
38 | 83f64091 | bellard | #include <IOKit/storage/IOMediaBSDClient.h> |
39 | 83f64091 | bellard | #include <IOKit/storage/IOMedia.h> |
40 | 83f64091 | bellard | #include <IOKit/storage/IOCDMedia.h> |
41 | 83f64091 | bellard | //#include <IOKit/storage/IOCDTypes.h>
|
42 | 83f64091 | bellard | #include <CoreFoundation/CoreFoundation.h> |
43 | 83f64091 | bellard | #endif
|
44 | 83f64091 | bellard | |
45 | 83f64091 | bellard | #ifdef __sun__
|
46 | 2e9671da | ths | #define _POSIX_PTHREAD_SEMANTICS 1 |
47 | 2e9671da | ths | #include <signal.h> |
48 | 83f64091 | bellard | #include <sys/dkio.h> |
49 | 83f64091 | bellard | #endif
|
50 | 19cb3738 | bellard | #ifdef __linux__
|
51 | 19cb3738 | bellard | #include <sys/ioctl.h> |
52 | 19cb3738 | bellard | #include <linux/cdrom.h> |
53 | 19cb3738 | bellard | #include <linux/fd.h> |
54 | 19cb3738 | bellard | #endif
|
55 | 1cb6c3fd | ths | #ifdef __FreeBSD__
|
56 | 543952ca | blueswir1 | #include <signal.h> |
57 | 1cb6c3fd | ths | #include <sys/disk.h> |
58 | 1cb6c3fd | ths | #endif
|
59 | 83f64091 | bellard | |
60 | 128ab2ff | blueswir1 | #ifdef __OpenBSD__
|
61 | 128ab2ff | blueswir1 | #include <sys/ioctl.h> |
62 | 128ab2ff | blueswir1 | #include <sys/disklabel.h> |
63 | 128ab2ff | blueswir1 | #include <sys/dkio.h> |
64 | 128ab2ff | blueswir1 | #endif
|
65 | 128ab2ff | blueswir1 | |
66 | 19cb3738 | bellard | //#define DEBUG_FLOPPY
|
67 | 83f64091 | bellard | |
68 | faf07963 | pbrook | //#define DEBUG_BLOCK
|
69 | 03ff3ca3 | aliguori | #if defined(DEBUG_BLOCK)
|
70 | a50a6282 | balrog | #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (loglevel != 0) \ |
71 | 2e03286b | balrog | { fprintf(logfile, formatCstr, ##args); fflush(logfile); } } while (0) |
72 | 8c05dbf9 | ths | #else
|
73 | 8c05dbf9 | ths | #define DEBUG_BLOCK_PRINT(formatCstr, args...)
|
74 | 8c05dbf9 | ths | #endif
|
75 | 8c05dbf9 | ths | |
76 | f6465578 | aliguori | /* OS X does not have O_DSYNC */
|
77 | f6465578 | aliguori | #ifndef O_DSYNC
|
78 | 7ab064d2 | aliguori | #define O_DSYNC O_SYNC
|
79 | f6465578 | aliguori | #endif
|
80 | f6465578 | aliguori | |
81 | 9f7965c7 | aliguori | /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
|
82 | 9f7965c7 | aliguori | #ifndef O_DIRECT
|
83 | 9f7965c7 | aliguori | #define O_DIRECT O_DSYNC
|
84 | 9f7965c7 | aliguori | #endif
|
85 | 9f7965c7 | aliguori | |
86 | 19cb3738 | bellard | #define FTYPE_FILE 0 |
87 | 19cb3738 | bellard | #define FTYPE_CD 1 |
88 | 19cb3738 | bellard | #define FTYPE_FD 2 |
89 | 83f64091 | bellard | |
90 | bed5cc52 | bellard | #define ALIGNED_BUFFER_SIZE (32 * 512) |
91 | bed5cc52 | bellard | |
92 | 19cb3738 | bellard | /* if the FD is not accessed during that time (in ms), we try to
|
93 | 19cb3738 | bellard | reopen it to see if the disk has been changed */
|
94 | 19cb3738 | bellard | #define FD_OPEN_TIMEOUT 1000 |
95 | 83f64091 | bellard | |
96 | 19cb3738 | bellard | typedef struct BDRVRawState { |
97 | 19cb3738 | bellard | int fd;
|
98 | 19cb3738 | bellard | int type;
|
99 | 8c05dbf9 | ths | unsigned int lseek_err_cnt; |
100 | 19cb3738 | bellard | #if defined(__linux__)
|
101 | 19cb3738 | bellard | /* linux floppy specific */
|
102 | 6dd2db52 | blueswir1 | int fd_open_flags;
|
103 | 19cb3738 | bellard | int64_t fd_open_time; |
104 | 19cb3738 | bellard | int64_t fd_error_time; |
105 | 19cb3738 | bellard | int fd_got_error;
|
106 | 19cb3738 | bellard | int fd_media_changed;
|
107 | 83f64091 | bellard | #endif
|
108 | bed5cc52 | bellard | uint8_t* aligned_buf; |
109 | 19cb3738 | bellard | } BDRVRawState; |
110 | 19cb3738 | bellard | |
111 | a76bab49 | aliguori | static int posix_aio_init(void); |
112 | a76bab49 | aliguori | |
113 | 19cb3738 | bellard | static int fd_open(BlockDriverState *bs); |
114 | 83f64091 | bellard | |
115 | 83f64091 | bellard | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
116 | 83f64091 | bellard | { |
117 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
118 | 19cb3738 | bellard | int fd, open_flags, ret;
|
119 | 83f64091 | bellard | |
120 | a76bab49 | aliguori | posix_aio_init(); |
121 | a76bab49 | aliguori | |
122 | 8c05dbf9 | ths | s->lseek_err_cnt = 0;
|
123 | 8c05dbf9 | ths | |
124 | 83f64091 | bellard | open_flags = O_BINARY; |
125 | 83f64091 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
126 | 83f64091 | bellard | open_flags |= O_RDWR; |
127 | 83f64091 | bellard | } else {
|
128 | 83f64091 | bellard | open_flags |= O_RDONLY; |
129 | 83f64091 | bellard | bs->read_only = 1;
|
130 | 83f64091 | bellard | } |
131 | 83f64091 | bellard | if (flags & BDRV_O_CREAT)
|
132 | 83f64091 | bellard | open_flags |= O_CREAT | O_TRUNC; |
133 | 9f7965c7 | aliguori | |
134 | 9f7965c7 | aliguori | /* Use O_DSYNC for write-through caching, no flags for write-back caching,
|
135 | 9f7965c7 | aliguori | * and O_DIRECT for no caching. */
|
136 | 9f7965c7 | aliguori | if ((flags & BDRV_O_NOCACHE))
|
137 | 33f00271 | balrog | open_flags |= O_DIRECT; |
138 | 9f7965c7 | aliguori | else if (!(flags & BDRV_O_CACHE_WB)) |
139 | 9f7965c7 | aliguori | open_flags |= O_DSYNC; |
140 | 83f64091 | bellard | |
141 | 19cb3738 | bellard | s->type = FTYPE_FILE; |
142 | 19cb3738 | bellard | |
143 | 83f64091 | bellard | fd = open(filename, open_flags, 0644);
|
144 | 19cb3738 | bellard | if (fd < 0) { |
145 | 19cb3738 | bellard | ret = -errno; |
146 | 19cb3738 | bellard | if (ret == -EROFS)
|
147 | 19cb3738 | bellard | ret = -EACCES; |
148 | 19cb3738 | bellard | return ret;
|
149 | 19cb3738 | bellard | } |
150 | 83f64091 | bellard | s->fd = fd; |
151 | bed5cc52 | bellard | s->aligned_buf = NULL;
|
152 | 9f7965c7 | aliguori | if ((flags & BDRV_O_NOCACHE)) {
|
153 | bed5cc52 | bellard | s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
|
154 | bed5cc52 | bellard | if (s->aligned_buf == NULL) { |
155 | bed5cc52 | bellard | ret = -errno; |
156 | bed5cc52 | bellard | close(fd); |
157 | bed5cc52 | bellard | return ret;
|
158 | bed5cc52 | bellard | } |
159 | bed5cc52 | bellard | } |
160 | 83f64091 | bellard | return 0; |
161 | 83f64091 | bellard | } |
162 | 83f64091 | bellard | |
163 | 83f64091 | bellard | /* XXX: use host sector size if necessary with:
|
164 | 83f64091 | bellard | #ifdef DIOCGSECTORSIZE
|
165 | 83f64091 | bellard | {
|
166 | 83f64091 | bellard | unsigned int sectorsize = 512;
|
167 | 83f64091 | bellard | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
|
168 | 83f64091 | bellard | sectorsize > bufsize)
|
169 | 83f64091 | bellard | bufsize = sectorsize;
|
170 | 83f64091 | bellard | }
|
171 | 83f64091 | bellard | #endif
|
172 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
173 | 83f64091 | bellard | u_int32_t blockSize = 512;
|
174 | 83f64091 | bellard | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
|
175 | 83f64091 | bellard | bufsize = blockSize;
|
176 | 83f64091 | bellard | }
|
177 | 83f64091 | bellard | #endif
|
178 | 83f64091 | bellard | */
|
179 | 83f64091 | bellard | |
180 | bed5cc52 | bellard | /*
|
181 | bed5cc52 | bellard | * offset and count are in bytes, but must be multiples of 512 for files
|
182 | bed5cc52 | bellard | * opened with O_DIRECT. buf must be aligned to 512 bytes then.
|
183 | bed5cc52 | bellard | *
|
184 | bed5cc52 | bellard | * This function may be called without alignment if the caller ensures
|
185 | bed5cc52 | bellard | * that O_DIRECT is not in effect.
|
186 | bed5cc52 | bellard | */
|
187 | bed5cc52 | bellard | static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, |
188 | 83f64091 | bellard | uint8_t *buf, int count)
|
189 | 83f64091 | bellard | { |
190 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
191 | 83f64091 | bellard | int ret;
|
192 | 3b46e624 | ths | |
193 | 19cb3738 | bellard | ret = fd_open(bs); |
194 | 19cb3738 | bellard | if (ret < 0) |
195 | 19cb3738 | bellard | return ret;
|
196 | 19cb3738 | bellard | |
197 | 985a03b0 | ths | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
198 | 8c05dbf9 | ths | ++(s->lseek_err_cnt); |
199 | 8c05dbf9 | ths | if(s->lseek_err_cnt <= 10) { |
200 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
201 | 92868412 | j_mayer | "] lseek failed : %d = %s\n",
|
202 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
203 | 8c05dbf9 | ths | bs->total_sectors, errno, strerror(errno)); |
204 | 8c05dbf9 | ths | } |
205 | 8c05dbf9 | ths | return -1; |
206 | 8c05dbf9 | ths | } |
207 | 8c05dbf9 | ths | s->lseek_err_cnt=0;
|
208 | 8c05dbf9 | ths | |
209 | 83f64091 | bellard | ret = read(s->fd, buf, count); |
210 | 8c05dbf9 | ths | if (ret == count)
|
211 | 8c05dbf9 | ths | goto label__raw_read__success;
|
212 | 8c05dbf9 | ths | |
213 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
214 | 92868412 | j_mayer | "] read failed %d : %d = %s\n",
|
215 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
216 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
217 | 8c05dbf9 | ths | |
218 | 8c05dbf9 | ths | /* Try harder for CDrom. */
|
219 | 8c05dbf9 | ths | if (bs->type == BDRV_TYPE_CDROM) {
|
220 | 8c05dbf9 | ths | lseek(s->fd, offset, SEEK_SET); |
221 | 8c05dbf9 | ths | ret = read(s->fd, buf, count); |
222 | 8c05dbf9 | ths | if (ret == count)
|
223 | 8c05dbf9 | ths | goto label__raw_read__success;
|
224 | 8c05dbf9 | ths | lseek(s->fd, offset, SEEK_SET); |
225 | 8c05dbf9 | ths | ret = read(s->fd, buf, count); |
226 | 8c05dbf9 | ths | if (ret == count)
|
227 | 8c05dbf9 | ths | goto label__raw_read__success;
|
228 | 8c05dbf9 | ths | |
229 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
230 | 92868412 | j_mayer | "] retry read failed %d : %d = %s\n",
|
231 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
232 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
233 | 8c05dbf9 | ths | } |
234 | 8c05dbf9 | ths | |
235 | 8c05dbf9 | ths | label__raw_read__success:
|
236 | 8c05dbf9 | ths | |
237 | 83f64091 | bellard | return ret;
|
238 | 83f64091 | bellard | } |
239 | 83f64091 | bellard | |
240 | bed5cc52 | bellard | /*
|
241 | bed5cc52 | bellard | * offset and count are in bytes, but must be multiples of 512 for files
|
242 | bed5cc52 | bellard | * opened with O_DIRECT. buf must be aligned to 512 bytes then.
|
243 | bed5cc52 | bellard | *
|
244 | bed5cc52 | bellard | * This function may be called without alignment if the caller ensures
|
245 | bed5cc52 | bellard | * that O_DIRECT is not in effect.
|
246 | bed5cc52 | bellard | */
|
247 | bed5cc52 | bellard | static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset, |
248 | 83f64091 | bellard | const uint8_t *buf, int count) |
249 | 83f64091 | bellard | { |
250 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
251 | 83f64091 | bellard | int ret;
|
252 | 3b46e624 | ths | |
253 | 19cb3738 | bellard | ret = fd_open(bs); |
254 | 19cb3738 | bellard | if (ret < 0) |
255 | 19cb3738 | bellard | return ret;
|
256 | 19cb3738 | bellard | |
257 | 985a03b0 | ths | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
258 | 8c05dbf9 | ths | ++(s->lseek_err_cnt); |
259 | 8c05dbf9 | ths | if(s->lseek_err_cnt) {
|
260 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" |
261 | 92868412 | j_mayer | PRId64 "] lseek failed : %d = %s\n",
|
262 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
263 | 8c05dbf9 | ths | bs->total_sectors, errno, strerror(errno)); |
264 | 8c05dbf9 | ths | } |
265 | 8c05dbf9 | ths | return -1; |
266 | 8c05dbf9 | ths | } |
267 | 8c05dbf9 | ths | s->lseek_err_cnt = 0;
|
268 | 8c05dbf9 | ths | |
269 | 83f64091 | bellard | ret = write(s->fd, buf, count); |
270 | 8c05dbf9 | ths | if (ret == count)
|
271 | 8c05dbf9 | ths | goto label__raw_write__success;
|
272 | 8c05dbf9 | ths | |
273 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
274 | 92868412 | j_mayer | "] write failed %d : %d = %s\n",
|
275 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
276 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
277 | 8c05dbf9 | ths | |
278 | 8c05dbf9 | ths | label__raw_write__success:
|
279 | 8c05dbf9 | ths | |
280 | 83f64091 | bellard | return ret;
|
281 | 83f64091 | bellard | } |
282 | 83f64091 | bellard | |
283 | bed5cc52 | bellard | |
284 | bed5cc52 | bellard | /*
|
285 | bed5cc52 | bellard | * offset and count are in bytes and possibly not aligned. For files opened
|
286 | bed5cc52 | bellard | * with O_DIRECT, necessary alignments are ensured before calling
|
287 | bed5cc52 | bellard | * raw_pread_aligned to do the actual read.
|
288 | bed5cc52 | bellard | */
|
289 | bed5cc52 | bellard | static int raw_pread(BlockDriverState *bs, int64_t offset, |
290 | bed5cc52 | bellard | uint8_t *buf, int count)
|
291 | bed5cc52 | bellard | { |
292 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
293 | bed5cc52 | bellard | int size, ret, shift, sum;
|
294 | bed5cc52 | bellard | |
295 | bed5cc52 | bellard | sum = 0;
|
296 | bed5cc52 | bellard | |
297 | bed5cc52 | bellard | if (s->aligned_buf != NULL) { |
298 | bed5cc52 | bellard | |
299 | bed5cc52 | bellard | if (offset & 0x1ff) { |
300 | bed5cc52 | bellard | /* align offset on a 512 bytes boundary */
|
301 | bed5cc52 | bellard | |
302 | bed5cc52 | bellard | shift = offset & 0x1ff;
|
303 | bed5cc52 | bellard | size = (shift + count + 0x1ff) & ~0x1ff; |
304 | bed5cc52 | bellard | if (size > ALIGNED_BUFFER_SIZE)
|
305 | bed5cc52 | bellard | size = ALIGNED_BUFFER_SIZE; |
306 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size); |
307 | bed5cc52 | bellard | if (ret < 0) |
308 | bed5cc52 | bellard | return ret;
|
309 | bed5cc52 | bellard | |
310 | bed5cc52 | bellard | size = 512 - shift;
|
311 | bed5cc52 | bellard | if (size > count)
|
312 | bed5cc52 | bellard | size = count; |
313 | bed5cc52 | bellard | memcpy(buf, s->aligned_buf + shift, size); |
314 | bed5cc52 | bellard | |
315 | bed5cc52 | bellard | buf += size; |
316 | bed5cc52 | bellard | offset += size; |
317 | bed5cc52 | bellard | count -= size; |
318 | bed5cc52 | bellard | sum += size; |
319 | bed5cc52 | bellard | |
320 | bed5cc52 | bellard | if (count == 0) |
321 | bed5cc52 | bellard | return sum;
|
322 | bed5cc52 | bellard | } |
323 | bed5cc52 | bellard | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
324 | bed5cc52 | bellard | |
325 | bed5cc52 | bellard | /* read on aligned buffer */
|
326 | bed5cc52 | bellard | |
327 | bed5cc52 | bellard | while (count) {
|
328 | bed5cc52 | bellard | |
329 | bed5cc52 | bellard | size = (count + 0x1ff) & ~0x1ff; |
330 | bed5cc52 | bellard | if (size > ALIGNED_BUFFER_SIZE)
|
331 | bed5cc52 | bellard | size = ALIGNED_BUFFER_SIZE; |
332 | bed5cc52 | bellard | |
333 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset, s->aligned_buf, size); |
334 | bed5cc52 | bellard | if (ret < 0) |
335 | bed5cc52 | bellard | return ret;
|
336 | bed5cc52 | bellard | |
337 | bed5cc52 | bellard | size = ret; |
338 | bed5cc52 | bellard | if (size > count)
|
339 | bed5cc52 | bellard | size = count; |
340 | bed5cc52 | bellard | |
341 | bed5cc52 | bellard | memcpy(buf, s->aligned_buf, size); |
342 | bed5cc52 | bellard | |
343 | bed5cc52 | bellard | buf += size; |
344 | bed5cc52 | bellard | offset += size; |
345 | bed5cc52 | bellard | count -= size; |
346 | bed5cc52 | bellard | sum += size; |
347 | bed5cc52 | bellard | } |
348 | bed5cc52 | bellard | |
349 | bed5cc52 | bellard | return sum;
|
350 | bed5cc52 | bellard | } |
351 | bed5cc52 | bellard | } |
352 | bed5cc52 | bellard | |
353 | bed5cc52 | bellard | return raw_pread_aligned(bs, offset, buf, count) + sum;
|
354 | bed5cc52 | bellard | } |
355 | bed5cc52 | bellard | |
356 | bed5cc52 | bellard | /*
|
357 | bed5cc52 | bellard | * offset and count are in bytes and possibly not aligned. For files opened
|
358 | bed5cc52 | bellard | * with O_DIRECT, necessary alignments are ensured before calling
|
359 | bed5cc52 | bellard | * raw_pwrite_aligned to do the actual write.
|
360 | bed5cc52 | bellard | */
|
361 | bed5cc52 | bellard | static int raw_pwrite(BlockDriverState *bs, int64_t offset, |
362 | bed5cc52 | bellard | const uint8_t *buf, int count) |
363 | bed5cc52 | bellard | { |
364 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
365 | bed5cc52 | bellard | int size, ret, shift, sum;
|
366 | bed5cc52 | bellard | |
367 | bed5cc52 | bellard | sum = 0;
|
368 | bed5cc52 | bellard | |
369 | bed5cc52 | bellard | if (s->aligned_buf != NULL) { |
370 | bed5cc52 | bellard | |
371 | bed5cc52 | bellard | if (offset & 0x1ff) { |
372 | bed5cc52 | bellard | /* align offset on a 512 bytes boundary */
|
373 | bed5cc52 | bellard | shift = offset & 0x1ff;
|
374 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
|
375 | bed5cc52 | bellard | if (ret < 0) |
376 | bed5cc52 | bellard | return ret;
|
377 | bed5cc52 | bellard | |
378 | bed5cc52 | bellard | size = 512 - shift;
|
379 | bed5cc52 | bellard | if (size > count)
|
380 | bed5cc52 | bellard | size = count; |
381 | bed5cc52 | bellard | memcpy(s->aligned_buf + shift, buf, size); |
382 | bed5cc52 | bellard | |
383 | bed5cc52 | bellard | ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
|
384 | bed5cc52 | bellard | if (ret < 0) |
385 | bed5cc52 | bellard | return ret;
|
386 | bed5cc52 | bellard | |
387 | bed5cc52 | bellard | buf += size; |
388 | bed5cc52 | bellard | offset += size; |
389 | bed5cc52 | bellard | count -= size; |
390 | bed5cc52 | bellard | sum += size; |
391 | bed5cc52 | bellard | |
392 | bed5cc52 | bellard | if (count == 0) |
393 | bed5cc52 | bellard | return sum;
|
394 | bed5cc52 | bellard | } |
395 | bed5cc52 | bellard | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
396 | bed5cc52 | bellard | |
397 | bed5cc52 | bellard | while ((size = (count & ~0x1ff)) != 0) { |
398 | bed5cc52 | bellard | |
399 | bed5cc52 | bellard | if (size > ALIGNED_BUFFER_SIZE)
|
400 | bed5cc52 | bellard | size = ALIGNED_BUFFER_SIZE; |
401 | bed5cc52 | bellard | |
402 | bed5cc52 | bellard | memcpy(s->aligned_buf, buf, size); |
403 | bed5cc52 | bellard | |
404 | bed5cc52 | bellard | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size); |
405 | bed5cc52 | bellard | if (ret < 0) |
406 | bed5cc52 | bellard | return ret;
|
407 | bed5cc52 | bellard | |
408 | bed5cc52 | bellard | buf += ret; |
409 | bed5cc52 | bellard | offset += ret; |
410 | bed5cc52 | bellard | count -= ret; |
411 | bed5cc52 | bellard | sum += ret; |
412 | bed5cc52 | bellard | } |
413 | bed5cc52 | bellard | /* here, count < 512 because (count & ~0x1ff) == 0 */
|
414 | bed5cc52 | bellard | if (count) {
|
415 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
|
416 | bed5cc52 | bellard | if (ret < 0) |
417 | bed5cc52 | bellard | return ret;
|
418 | bed5cc52 | bellard | memcpy(s->aligned_buf, buf, count); |
419 | bed5cc52 | bellard | |
420 | bed5cc52 | bellard | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
|
421 | bed5cc52 | bellard | if (ret < 0) |
422 | bed5cc52 | bellard | return ret;
|
423 | bed5cc52 | bellard | if (count < ret)
|
424 | bed5cc52 | bellard | ret = count; |
425 | bed5cc52 | bellard | |
426 | bed5cc52 | bellard | sum += ret; |
427 | bed5cc52 | bellard | } |
428 | bed5cc52 | bellard | return sum;
|
429 | bed5cc52 | bellard | } |
430 | bed5cc52 | bellard | } |
431 | bed5cc52 | bellard | return raw_pwrite_aligned(bs, offset, buf, count) + sum;
|
432 | bed5cc52 | bellard | } |
433 | bed5cc52 | bellard | |
434 | 414f0dab | blueswir1 | #ifdef CONFIG_AIO
|
435 | 83f64091 | bellard | /***********************************************************/
|
436 | 19cb3738 | bellard | /* Unix AIO using POSIX AIO */
|
437 | 83f64091 | bellard | |
438 | 83f64091 | bellard | typedef struct RawAIOCB { |
439 | ce1a14dc | pbrook | BlockDriverAIOCB common; |
440 | 3c529d93 | aliguori | struct qemu_paiocb aiocb;
|
441 | ce1a14dc | pbrook | struct RawAIOCB *next;
|
442 | bed5cc52 | bellard | int ret;
|
443 | 83f64091 | bellard | } RawAIOCB; |
444 | 83f64091 | bellard | |
445 | a76bab49 | aliguori | typedef struct PosixAioState |
446 | a76bab49 | aliguori | { |
447 | 9e472e10 | aliguori | int rfd, wfd;
|
448 | a76bab49 | aliguori | RawAIOCB *first_aio; |
449 | a76bab49 | aliguori | } PosixAioState; |
450 | 83f64091 | bellard | |
451 | a76bab49 | aliguori | static void posix_aio_read(void *opaque) |
452 | 83f64091 | bellard | { |
453 | a76bab49 | aliguori | PosixAioState *s = opaque; |
454 | ce1a14dc | pbrook | RawAIOCB *acb, **pacb; |
455 | 83f64091 | bellard | int ret;
|
456 | 9e472e10 | aliguori | ssize_t len; |
457 | 9e472e10 | aliguori | |
458 | e20e830b | aliguori | /* read all bytes from signal pipe */
|
459 | e20e830b | aliguori | for (;;) {
|
460 | e20e830b | aliguori | char bytes[16]; |
461 | 9e472e10 | aliguori | |
462 | e20e830b | aliguori | len = read(s->rfd, bytes, sizeof(bytes));
|
463 | 2c41a5f9 | aliguori | if (len == -1 && errno == EINTR) |
464 | e20e830b | aliguori | continue; /* try again */ |
465 | e20e830b | aliguori | if (len == sizeof(bytes)) |
466 | e20e830b | aliguori | continue; /* more to read */ |
467 | e20e830b | aliguori | break;
|
468 | e20e830b | aliguori | } |
469 | 83f64091 | bellard | |
470 | 83f64091 | bellard | for(;;) {
|
471 | a76bab49 | aliguori | pacb = &s->first_aio; |
472 | 83f64091 | bellard | for(;;) {
|
473 | 83f64091 | bellard | acb = *pacb; |
474 | 83f64091 | bellard | if (!acb)
|
475 | 83f64091 | bellard | goto the_end;
|
476 | 3c529d93 | aliguori | ret = qemu_paio_error(&acb->aiocb); |
477 | 83f64091 | bellard | if (ret == ECANCELED) {
|
478 | 83f64091 | bellard | /* remove the request */
|
479 | ce1a14dc | pbrook | *pacb = acb->next; |
480 | ce1a14dc | pbrook | qemu_aio_release(acb); |
481 | 83f64091 | bellard | } else if (ret != EINPROGRESS) { |
482 | 83f64091 | bellard | /* end of aio */
|
483 | 83f64091 | bellard | if (ret == 0) { |
484 | 3c529d93 | aliguori | ret = qemu_paio_return(&acb->aiocb); |
485 | ce1a14dc | pbrook | if (ret == acb->aiocb.aio_nbytes)
|
486 | 83f64091 | bellard | ret = 0;
|
487 | 83f64091 | bellard | else
|
488 | 19cb3738 | bellard | ret = -EINVAL; |
489 | 83f64091 | bellard | } else {
|
490 | 83f64091 | bellard | ret = -ret; |
491 | 83f64091 | bellard | } |
492 | 83f64091 | bellard | /* remove the request */
|
493 | ce1a14dc | pbrook | *pacb = acb->next; |
494 | 83f64091 | bellard | /* call the callback */
|
495 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, ret); |
496 | ce1a14dc | pbrook | qemu_aio_release(acb); |
497 | 83f64091 | bellard | break;
|
498 | 83f64091 | bellard | } else {
|
499 | ce1a14dc | pbrook | pacb = &acb->next; |
500 | 83f64091 | bellard | } |
501 | 83f64091 | bellard | } |
502 | 83f64091 | bellard | } |
503 | 83f64091 | bellard | the_end: ;
|
504 | 83f64091 | bellard | } |
505 | 83f64091 | bellard | |
506 | a76bab49 | aliguori | static int posix_aio_flush(void *opaque) |
507 | baf35cb9 | aliguori | { |
508 | a76bab49 | aliguori | PosixAioState *s = opaque; |
509 | a76bab49 | aliguori | return !!s->first_aio;
|
510 | a76bab49 | aliguori | } |
511 | a76bab49 | aliguori | |
512 | a76bab49 | aliguori | static PosixAioState *posix_aio_state;
|
513 | baf35cb9 | aliguori | |
514 | 9e472e10 | aliguori | static void aio_signal_handler(int signum) |
515 | 9e472e10 | aliguori | { |
516 | 9e472e10 | aliguori | if (posix_aio_state) {
|
517 | 9e472e10 | aliguori | char byte = 0; |
518 | 9e472e10 | aliguori | |
519 | 9e472e10 | aliguori | write(posix_aio_state->wfd, &byte, sizeof(byte));
|
520 | 9e472e10 | aliguori | } |
521 | 9e472e10 | aliguori | |
522 | 9e472e10 | aliguori | qemu_service_io(); |
523 | 9e472e10 | aliguori | } |
524 | 9e472e10 | aliguori | |
525 | a76bab49 | aliguori | static int posix_aio_init(void) |
526 | a76bab49 | aliguori | { |
527 | 9e472e10 | aliguori | struct sigaction act;
|
528 | a76bab49 | aliguori | PosixAioState *s; |
529 | 9e472e10 | aliguori | int fds[2]; |
530 | 3c529d93 | aliguori | struct qemu_paioinit ai;
|
531 | a76bab49 | aliguori | |
532 | a76bab49 | aliguori | if (posix_aio_state)
|
533 | a76bab49 | aliguori | return 0; |
534 | ad02ad6f | aliguori | |
535 | a76bab49 | aliguori | s = qemu_malloc(sizeof(PosixAioState));
|
536 | a76bab49 | aliguori | if (s == NULL) |
537 | a76bab49 | aliguori | return -ENOMEM;
|
538 | baf35cb9 | aliguori | |
539 | 9e472e10 | aliguori | sigfillset(&act.sa_mask); |
540 | 9e472e10 | aliguori | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ |
541 | 9e472e10 | aliguori | act.sa_handler = aio_signal_handler; |
542 | 9e472e10 | aliguori | sigaction(SIGUSR2, &act, NULL);
|
543 | 9e472e10 | aliguori | |
544 | a76bab49 | aliguori | s->first_aio = NULL;
|
545 | 9e472e10 | aliguori | if (pipe(fds) == -1) { |
546 | 9e472e10 | aliguori | fprintf(stderr, "failed to create pipe\n");
|
547 | 27463101 | aliguori | return -errno;
|
548 | 27463101 | aliguori | } |
549 | 2c41a5f9 | aliguori | |
550 | 9e472e10 | aliguori | s->rfd = fds[0];
|
551 | 9e472e10 | aliguori | s->wfd = fds[1];
|
552 | 9e472e10 | aliguori | |
553 | e20e830b | aliguori | fcntl(s->rfd, F_SETFL, O_NONBLOCK); |
554 | 9e472e10 | aliguori | fcntl(s->wfd, F_SETFL, O_NONBLOCK); |
555 | 2c41a5f9 | aliguori | |
556 | 9e472e10 | aliguori | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
|
557 | baf35cb9 | aliguori | |
558 | 3c529d93 | aliguori | memset(&ai, 0, sizeof(ai)); |
559 | 3c529d93 | aliguori | ai.aio_threads = 64;
|
560 | 3c529d93 | aliguori | ai.aio_num = 64;
|
561 | 3c529d93 | aliguori | qemu_paio_init(&ai); |
562 | acce87f9 | aliguori | |
563 | a76bab49 | aliguori | posix_aio_state = s; |
564 | 6eb5733a | bellard | |
565 | a76bab49 | aliguori | return 0; |
566 | 83f64091 | bellard | } |
567 | 83f64091 | bellard | |
568 | ce1a14dc | pbrook | static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
|
569 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
570 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
571 | 83f64091 | bellard | { |
572 | ce1a14dc | pbrook | BDRVRawState *s = bs->opaque; |
573 | ce1a14dc | pbrook | RawAIOCB *acb; |
574 | ce1a14dc | pbrook | |
575 | 19cb3738 | bellard | if (fd_open(bs) < 0) |
576 | 19cb3738 | bellard | return NULL; |
577 | 19cb3738 | bellard | |
578 | ce1a14dc | pbrook | acb = qemu_aio_get(bs, cb, opaque); |
579 | ce1a14dc | pbrook | if (!acb)
|
580 | ce1a14dc | pbrook | return NULL; |
581 | 3c529d93 | aliguori | acb->aiocb.aio_fildes = s->fd; |
582 | a76bab49 | aliguori | acb->aiocb.aio_sigevent.sigev_signo = SIGUSR2; |
583 | ce1a14dc | pbrook | acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL; |
584 | ce1a14dc | pbrook | acb->aiocb.aio_buf = buf; |
585 | 985a03b0 | ths | if (nb_sectors < 0) |
586 | 985a03b0 | ths | acb->aiocb.aio_nbytes = -nb_sectors; |
587 | 985a03b0 | ths | else
|
588 | 985a03b0 | ths | acb->aiocb.aio_nbytes = nb_sectors * 512;
|
589 | ce1a14dc | pbrook | acb->aiocb.aio_offset = sector_num * 512;
|
590 | a76bab49 | aliguori | acb->next = posix_aio_state->first_aio; |
591 | a76bab49 | aliguori | posix_aio_state->first_aio = acb; |
592 | ce1a14dc | pbrook | return acb;
|
593 | 83f64091 | bellard | } |
594 | 83f64091 | bellard | |
595 | bed5cc52 | bellard | static void raw_aio_em_cb(void* opaque) |
596 | bed5cc52 | bellard | { |
597 | bed5cc52 | bellard | RawAIOCB *acb = opaque; |
598 | bed5cc52 | bellard | acb->common.cb(acb->common.opaque, acb->ret); |
599 | bed5cc52 | bellard | qemu_aio_release(acb); |
600 | bed5cc52 | bellard | } |
601 | bed5cc52 | bellard | |
602 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
|
603 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
604 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
605 | 83f64091 | bellard | { |
606 | ce1a14dc | pbrook | RawAIOCB *acb; |
607 | 83f64091 | bellard | |
608 | bed5cc52 | bellard | /*
|
609 | bed5cc52 | bellard | * If O_DIRECT is used and the buffer is not aligned fall back
|
610 | bed5cc52 | bellard | * to synchronous IO.
|
611 | bed5cc52 | bellard | */
|
612 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
613 | bed5cc52 | bellard | |
614 | bed5cc52 | bellard | if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) { |
615 | bed5cc52 | bellard | QEMUBH *bh; |
616 | bed5cc52 | bellard | acb = qemu_aio_get(bs, cb, opaque); |
617 | bed5cc52 | bellard | acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors); |
618 | bed5cc52 | bellard | bh = qemu_bh_new(raw_aio_em_cb, acb); |
619 | bed5cc52 | bellard | qemu_bh_schedule(bh); |
620 | bed5cc52 | bellard | return &acb->common;
|
621 | bed5cc52 | bellard | } |
622 | bed5cc52 | bellard | |
623 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
624 | ce1a14dc | pbrook | if (!acb)
|
625 | ce1a14dc | pbrook | return NULL; |
626 | 3c529d93 | aliguori | if (qemu_paio_read(&acb->aiocb) < 0) { |
627 | ce1a14dc | pbrook | qemu_aio_release(acb); |
628 | ce1a14dc | pbrook | return NULL; |
629 | 5fafdf24 | ths | } |
630 | ce1a14dc | pbrook | return &acb->common;
|
631 | 83f64091 | bellard | } |
632 | 83f64091 | bellard | |
633 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
|
634 | ce1a14dc | pbrook | int64_t sector_num, const uint8_t *buf, int nb_sectors, |
635 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
636 | 83f64091 | bellard | { |
637 | ce1a14dc | pbrook | RawAIOCB *acb; |
638 | 83f64091 | bellard | |
639 | bed5cc52 | bellard | /*
|
640 | bed5cc52 | bellard | * If O_DIRECT is used and the buffer is not aligned fall back
|
641 | bed5cc52 | bellard | * to synchronous IO.
|
642 | bed5cc52 | bellard | */
|
643 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
644 | bed5cc52 | bellard | |
645 | bed5cc52 | bellard | if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) { |
646 | bed5cc52 | bellard | QEMUBH *bh; |
647 | bed5cc52 | bellard | acb = qemu_aio_get(bs, cb, opaque); |
648 | bed5cc52 | bellard | acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors); |
649 | bed5cc52 | bellard | bh = qemu_bh_new(raw_aio_em_cb, acb); |
650 | bed5cc52 | bellard | qemu_bh_schedule(bh); |
651 | bed5cc52 | bellard | return &acb->common;
|
652 | bed5cc52 | bellard | } |
653 | bed5cc52 | bellard | |
654 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque); |
655 | ce1a14dc | pbrook | if (!acb)
|
656 | ce1a14dc | pbrook | return NULL; |
657 | 3c529d93 | aliguori | if (qemu_paio_write(&acb->aiocb) < 0) { |
658 | ce1a14dc | pbrook | qemu_aio_release(acb); |
659 | ce1a14dc | pbrook | return NULL; |
660 | 5fafdf24 | ths | } |
661 | ce1a14dc | pbrook | return &acb->common;
|
662 | 83f64091 | bellard | } |
663 | 83f64091 | bellard | |
664 | ce1a14dc | pbrook | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) |
665 | 83f64091 | bellard | { |
666 | 83f64091 | bellard | int ret;
|
667 | ce1a14dc | pbrook | RawAIOCB *acb = (RawAIOCB *)blockacb; |
668 | ce1a14dc | pbrook | RawAIOCB **pacb; |
669 | 83f64091 | bellard | |
670 | 3c529d93 | aliguori | ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb); |
671 | 3c529d93 | aliguori | if (ret == QEMU_PAIO_NOTCANCELED) {
|
672 | 83f64091 | bellard | /* fail safe: if the aio could not be canceled, we wait for
|
673 | 83f64091 | bellard | it */
|
674 | 3c529d93 | aliguori | while (qemu_paio_error(&acb->aiocb) == EINPROGRESS);
|
675 | 83f64091 | bellard | } |
676 | 83f64091 | bellard | |
677 | 83f64091 | bellard | /* remove the callback from the queue */
|
678 | a76bab49 | aliguori | pacb = &posix_aio_state->first_aio; |
679 | 83f64091 | bellard | for(;;) {
|
680 | 83f64091 | bellard | if (*pacb == NULL) { |
681 | 83f64091 | bellard | break;
|
682 | 83f64091 | bellard | } else if (*pacb == acb) { |
683 | ce1a14dc | pbrook | *pacb = acb->next; |
684 | ce1a14dc | pbrook | qemu_aio_release(acb); |
685 | 83f64091 | bellard | break;
|
686 | 83f64091 | bellard | } |
687 | ce1a14dc | pbrook | pacb = &acb->next; |
688 | 83f64091 | bellard | } |
689 | 83f64091 | bellard | } |
690 | a76bab49 | aliguori | #else /* CONFIG_AIO */ |
691 | a76bab49 | aliguori | static int posix_aio_init(void) |
692 | 414f0dab | blueswir1 | { |
693 | 674a24ac | blueswir1 | return 0; |
694 | 414f0dab | blueswir1 | } |
695 | 414f0dab | blueswir1 | #endif /* CONFIG_AIO */ |
696 | 414f0dab | blueswir1 | |
697 | 53538725 | aliguori | |
698 | 83f64091 | bellard | static void raw_close(BlockDriverState *bs) |
699 | 83f64091 | bellard | { |
700 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
701 | 19cb3738 | bellard | if (s->fd >= 0) { |
702 | 19cb3738 | bellard | close(s->fd); |
703 | 19cb3738 | bellard | s->fd = -1;
|
704 | bed5cc52 | bellard | if (s->aligned_buf != NULL) |
705 | bed5cc52 | bellard | qemu_free(s->aligned_buf); |
706 | 19cb3738 | bellard | } |
707 | 83f64091 | bellard | } |
708 | 83f64091 | bellard | |
709 | 83f64091 | bellard | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
710 | 83f64091 | bellard | { |
711 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
712 | 19cb3738 | bellard | if (s->type != FTYPE_FILE)
|
713 | 19cb3738 | bellard | return -ENOTSUP;
|
714 | 83f64091 | bellard | if (ftruncate(s->fd, offset) < 0) |
715 | 83f64091 | bellard | return -errno;
|
716 | 83f64091 | bellard | return 0; |
717 | 83f64091 | bellard | } |
718 | 83f64091 | bellard | |
719 | 128ab2ff | blueswir1 | #ifdef __OpenBSD__
|
720 | 128ab2ff | blueswir1 | static int64_t raw_getlength(BlockDriverState *bs)
|
721 | 128ab2ff | blueswir1 | { |
722 | 128ab2ff | blueswir1 | BDRVRawState *s = bs->opaque; |
723 | 128ab2ff | blueswir1 | int fd = s->fd;
|
724 | 128ab2ff | blueswir1 | struct stat st;
|
725 | 128ab2ff | blueswir1 | |
726 | 128ab2ff | blueswir1 | if (fstat(fd, &st))
|
727 | 128ab2ff | blueswir1 | return -1; |
728 | 128ab2ff | blueswir1 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
|
729 | 128ab2ff | blueswir1 | struct disklabel dl;
|
730 | 128ab2ff | blueswir1 | |
731 | 128ab2ff | blueswir1 | if (ioctl(fd, DIOCGDINFO, &dl))
|
732 | 128ab2ff | blueswir1 | return -1; |
733 | 128ab2ff | blueswir1 | return (uint64_t)dl.d_secsize *
|
734 | 128ab2ff | blueswir1 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; |
735 | 128ab2ff | blueswir1 | } else
|
736 | 128ab2ff | blueswir1 | return st.st_size;
|
737 | 128ab2ff | blueswir1 | } |
738 | 128ab2ff | blueswir1 | #else /* !__OpenBSD__ */ |
739 | 83f64091 | bellard | static int64_t raw_getlength(BlockDriverState *bs)
|
740 | 83f64091 | bellard | { |
741 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
742 | 83f64091 | bellard | int fd = s->fd;
|
743 | 83f64091 | bellard | int64_t size; |
744 | 83f64091 | bellard | #ifdef _BSD
|
745 | 83f64091 | bellard | struct stat sb;
|
746 | 83f64091 | bellard | #endif
|
747 | 83f64091 | bellard | #ifdef __sun__
|
748 | 83f64091 | bellard | struct dk_minfo minfo;
|
749 | 83f64091 | bellard | int rv;
|
750 | 83f64091 | bellard | #endif
|
751 | 19cb3738 | bellard | int ret;
|
752 | 19cb3738 | bellard | |
753 | 19cb3738 | bellard | ret = fd_open(bs); |
754 | 19cb3738 | bellard | if (ret < 0) |
755 | 19cb3738 | bellard | return ret;
|
756 | 83f64091 | bellard | |
757 | 83f64091 | bellard | #ifdef _BSD
|
758 | 83f64091 | bellard | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
|
759 | 83f64091 | bellard | #ifdef DIOCGMEDIASIZE
|
760 | 83f64091 | bellard | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
|
761 | 83f64091 | bellard | #endif
|
762 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
763 | 83f64091 | bellard | size = LONG_LONG_MAX; |
764 | 83f64091 | bellard | #else
|
765 | 83f64091 | bellard | size = lseek(fd, 0LL, SEEK_END);
|
766 | 83f64091 | bellard | #endif
|
767 | 83f64091 | bellard | } else
|
768 | 83f64091 | bellard | #endif
|
769 | 83f64091 | bellard | #ifdef __sun__
|
770 | 83f64091 | bellard | /*
|
771 | 83f64091 | bellard | * use the DKIOCGMEDIAINFO ioctl to read the size.
|
772 | 83f64091 | bellard | */
|
773 | 83f64091 | bellard | rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); |
774 | 83f64091 | bellard | if ( rv != -1 ) { |
775 | 83f64091 | bellard | size = minfo.dki_lbsize * minfo.dki_capacity; |
776 | 83f64091 | bellard | } else /* there are reports that lseek on some devices |
777 | 83f64091 | bellard | fails, but irc discussion said that contingency
|
778 | 83f64091 | bellard | on contingency was overkill */
|
779 | 83f64091 | bellard | #endif
|
780 | 83f64091 | bellard | { |
781 | 83f64091 | bellard | size = lseek(fd, 0, SEEK_END);
|
782 | 83f64091 | bellard | } |
783 | 83f64091 | bellard | return size;
|
784 | 83f64091 | bellard | } |
785 | 128ab2ff | blueswir1 | #endif
|
786 | 83f64091 | bellard | |
787 | 83f64091 | bellard | static int raw_create(const char *filename, int64_t total_size, |
788 | 83f64091 | bellard | const char *backing_file, int flags) |
789 | 83f64091 | bellard | { |
790 | 83f64091 | bellard | int fd;
|
791 | 83f64091 | bellard | |
792 | 83f64091 | bellard | if (flags || backing_file)
|
793 | 83f64091 | bellard | return -ENOTSUP;
|
794 | 83f64091 | bellard | |
795 | 5fafdf24 | ths | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
796 | 83f64091 | bellard | 0644);
|
797 | 83f64091 | bellard | if (fd < 0) |
798 | 83f64091 | bellard | return -EIO;
|
799 | 83f64091 | bellard | ftruncate(fd, total_size * 512);
|
800 | 83f64091 | bellard | close(fd); |
801 | 83f64091 | bellard | return 0; |
802 | 83f64091 | bellard | } |
803 | 83f64091 | bellard | |
804 | 83f64091 | bellard | static void raw_flush(BlockDriverState *bs) |
805 | 83f64091 | bellard | { |
806 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
807 | 83f64091 | bellard | fsync(s->fd); |
808 | 83f64091 | bellard | } |
809 | 83f64091 | bellard | |
810 | 83f64091 | bellard | BlockDriver bdrv_raw = { |
811 | 83f64091 | bellard | "raw",
|
812 | 83f64091 | bellard | sizeof(BDRVRawState),
|
813 | 83f64091 | bellard | NULL, /* no probe for protocols */ |
814 | 83f64091 | bellard | raw_open, |
815 | 83f64091 | bellard | NULL,
|
816 | 83f64091 | bellard | NULL,
|
817 | 83f64091 | bellard | raw_close, |
818 | 83f64091 | bellard | raw_create, |
819 | 83f64091 | bellard | raw_flush, |
820 | 3b46e624 | ths | |
821 | 414f0dab | blueswir1 | #ifdef CONFIG_AIO
|
822 | 83f64091 | bellard | .bdrv_aio_read = raw_aio_read, |
823 | 83f64091 | bellard | .bdrv_aio_write = raw_aio_write, |
824 | 83f64091 | bellard | .bdrv_aio_cancel = raw_aio_cancel, |
825 | ce1a14dc | pbrook | .aiocb_size = sizeof(RawAIOCB),
|
826 | 414f0dab | blueswir1 | #endif
|
827 | 3c529d93 | aliguori | |
828 | 83f64091 | bellard | .bdrv_pread = raw_pread, |
829 | 83f64091 | bellard | .bdrv_pwrite = raw_pwrite, |
830 | 83f64091 | bellard | .bdrv_truncate = raw_truncate, |
831 | 83f64091 | bellard | .bdrv_getlength = raw_getlength, |
832 | 83f64091 | bellard | }; |
833 | 83f64091 | bellard | |
834 | 19cb3738 | bellard | /***********************************************/
|
835 | 19cb3738 | bellard | /* host device */
|
836 | 19cb3738 | bellard | |
837 | 19cb3738 | bellard | #ifdef CONFIG_COCOA
|
838 | 19cb3738 | bellard | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
|
839 | 19cb3738 | bellard | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); |
840 | 19cb3738 | bellard | |
841 | 19cb3738 | bellard | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) |
842 | 19cb3738 | bellard | { |
843 | 5fafdf24 | ths | kern_return_t kernResult; |
844 | 19cb3738 | bellard | mach_port_t masterPort; |
845 | 19cb3738 | bellard | CFMutableDictionaryRef classesToMatch; |
846 | 19cb3738 | bellard | |
847 | 19cb3738 | bellard | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); |
848 | 19cb3738 | bellard | if ( KERN_SUCCESS != kernResult ) {
|
849 | 19cb3738 | bellard | printf( "IOMasterPort returned %d\n", kernResult );
|
850 | 19cb3738 | bellard | } |
851 | 3b46e624 | ths | |
852 | 5fafdf24 | ths | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
853 | 19cb3738 | bellard | if ( classesToMatch == NULL ) { |
854 | 19cb3738 | bellard | printf( "IOServiceMatching returned a NULL dictionary.\n" );
|
855 | 19cb3738 | bellard | } else {
|
856 | 19cb3738 | bellard | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); |
857 | 19cb3738 | bellard | } |
858 | 19cb3738 | bellard | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); |
859 | 19cb3738 | bellard | if ( KERN_SUCCESS != kernResult )
|
860 | 19cb3738 | bellard | { |
861 | 19cb3738 | bellard | printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
|
862 | 19cb3738 | bellard | } |
863 | 3b46e624 | ths | |
864 | 19cb3738 | bellard | return kernResult;
|
865 | 19cb3738 | bellard | } |
866 | 19cb3738 | bellard | |
867 | 19cb3738 | bellard | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
|
868 | 19cb3738 | bellard | { |
869 | 19cb3738 | bellard | io_object_t nextMedia; |
870 | 19cb3738 | bellard | kern_return_t kernResult = KERN_FAILURE; |
871 | 19cb3738 | bellard | *bsdPath = '\0';
|
872 | 19cb3738 | bellard | nextMedia = IOIteratorNext( mediaIterator ); |
873 | 19cb3738 | bellard | if ( nextMedia )
|
874 | 19cb3738 | bellard | { |
875 | 19cb3738 | bellard | CFTypeRef bsdPathAsCFString; |
876 | 19cb3738 | bellard | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
|
877 | 19cb3738 | bellard | if ( bsdPathAsCFString ) {
|
878 | 19cb3738 | bellard | size_t devPathLength; |
879 | 19cb3738 | bellard | strcpy( bsdPath, _PATH_DEV ); |
880 | 19cb3738 | bellard | strcat( bsdPath, "r" );
|
881 | 19cb3738 | bellard | devPathLength = strlen( bsdPath ); |
882 | 19cb3738 | bellard | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
|
883 | 19cb3738 | bellard | kernResult = KERN_SUCCESS; |
884 | 19cb3738 | bellard | } |
885 | 19cb3738 | bellard | CFRelease( bsdPathAsCFString ); |
886 | 19cb3738 | bellard | } |
887 | 19cb3738 | bellard | IOObjectRelease( nextMedia ); |
888 | 19cb3738 | bellard | } |
889 | 3b46e624 | ths | |
890 | 19cb3738 | bellard | return kernResult;
|
891 | 19cb3738 | bellard | } |
892 | 19cb3738 | bellard | |
893 | 19cb3738 | bellard | #endif
|
894 | 19cb3738 | bellard | |
895 | 19cb3738 | bellard | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
896 | 19cb3738 | bellard | { |
897 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
898 | 3c529d93 | aliguori | int fd, open_flags, ret;
|
899 | 19cb3738 | bellard | |
900 | a76bab49 | aliguori | posix_aio_init(); |
901 | a76bab49 | aliguori | |
902 | 19cb3738 | bellard | #ifdef CONFIG_COCOA
|
903 | 19cb3738 | bellard | if (strstart(filename, "/dev/cdrom", NULL)) { |
904 | 19cb3738 | bellard | kern_return_t kernResult; |
905 | 19cb3738 | bellard | io_iterator_t mediaIterator; |
906 | 19cb3738 | bellard | char bsdPath[ MAXPATHLEN ];
|
907 | 19cb3738 | bellard | int fd;
|
908 | 5fafdf24 | ths | |
909 | 19cb3738 | bellard | kernResult = FindEjectableCDMedia( &mediaIterator ); |
910 | 19cb3738 | bellard | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
|
911 | 3b46e624 | ths | |
912 | 19cb3738 | bellard | if ( bsdPath[ 0 ] != '\0' ) { |
913 | 19cb3738 | bellard | strcat(bsdPath,"s0");
|
914 | 19cb3738 | bellard | /* some CDs don't have a partition 0 */
|
915 | 19cb3738 | bellard | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); |
916 | 19cb3738 | bellard | if (fd < 0) { |
917 | 19cb3738 | bellard | bsdPath[strlen(bsdPath)-1] = '1'; |
918 | 19cb3738 | bellard | } else {
|
919 | 19cb3738 | bellard | close(fd); |
920 | 19cb3738 | bellard | } |
921 | 19cb3738 | bellard | filename = bsdPath; |
922 | 19cb3738 | bellard | } |
923 | 3b46e624 | ths | |
924 | 19cb3738 | bellard | if ( mediaIterator )
|
925 | 19cb3738 | bellard | IOObjectRelease( mediaIterator ); |
926 | 19cb3738 | bellard | } |
927 | 19cb3738 | bellard | #endif
|
928 | 19cb3738 | bellard | open_flags = O_BINARY; |
929 | 19cb3738 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
930 | 19cb3738 | bellard | open_flags |= O_RDWR; |
931 | 19cb3738 | bellard | } else {
|
932 | 19cb3738 | bellard | open_flags |= O_RDONLY; |
933 | 19cb3738 | bellard | bs->read_only = 1;
|
934 | 19cb3738 | bellard | } |
935 | 9f7965c7 | aliguori | /* Use O_DSYNC for write-through caching, no flags for write-back caching,
|
936 | 9f7965c7 | aliguori | * and O_DIRECT for no caching. */
|
937 | 9f7965c7 | aliguori | if ((flags & BDRV_O_NOCACHE))
|
938 | 33f00271 | balrog | open_flags |= O_DIRECT; |
939 | 9f7965c7 | aliguori | else if (!(flags & BDRV_O_CACHE_WB)) |
940 | 9f7965c7 | aliguori | open_flags |= O_DSYNC; |
941 | 19cb3738 | bellard | |
942 | 19cb3738 | bellard | s->type = FTYPE_FILE; |
943 | 19cb3738 | bellard | #if defined(__linux__)
|
944 | 19cb3738 | bellard | if (strstart(filename, "/dev/cd", NULL)) { |
945 | 19cb3738 | bellard | /* open will not fail even if no CD is inserted */
|
946 | 19cb3738 | bellard | open_flags |= O_NONBLOCK; |
947 | 19cb3738 | bellard | s->type = FTYPE_CD; |
948 | 19cb3738 | bellard | } else if (strstart(filename, "/dev/fd", NULL)) { |
949 | 19cb3738 | bellard | s->type = FTYPE_FD; |
950 | 6dd2db52 | blueswir1 | s->fd_open_flags = open_flags; |
951 | 19cb3738 | bellard | /* open will not fail even if no floppy is inserted */
|
952 | 19cb3738 | bellard | open_flags |= O_NONBLOCK; |
953 | 985a03b0 | ths | } else if (strstart(filename, "/dev/sg", NULL)) { |
954 | 985a03b0 | ths | bs->sg = 1;
|
955 | 19cb3738 | bellard | } |
956 | 19cb3738 | bellard | #endif
|
957 | 19cb3738 | bellard | fd = open(filename, open_flags, 0644);
|
958 | 19cb3738 | bellard | if (fd < 0) { |
959 | 19cb3738 | bellard | ret = -errno; |
960 | 19cb3738 | bellard | if (ret == -EROFS)
|
961 | 19cb3738 | bellard | ret = -EACCES; |
962 | 19cb3738 | bellard | return ret;
|
963 | 19cb3738 | bellard | } |
964 | 19cb3738 | bellard | s->fd = fd; |
965 | 19cb3738 | bellard | #if defined(__linux__)
|
966 | 19cb3738 | bellard | /* close fd so that we can reopen it as needed */
|
967 | 19cb3738 | bellard | if (s->type == FTYPE_FD) {
|
968 | 19cb3738 | bellard | close(s->fd); |
969 | 19cb3738 | bellard | s->fd = -1;
|
970 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
971 | 19cb3738 | bellard | } |
972 | 19cb3738 | bellard | #endif
|
973 | 19cb3738 | bellard | return 0; |
974 | 19cb3738 | bellard | } |
975 | 19cb3738 | bellard | |
976 | 03ff3ca3 | aliguori | #if defined(__linux__)
|
977 | 19cb3738 | bellard | /* Note: we do not have a reliable method to detect if the floppy is
|
978 | 19cb3738 | bellard | present. The current method is to try to open the floppy at every
|
979 | 19cb3738 | bellard | I/O and to keep it opened during a few hundreds of ms. */
|
980 | 19cb3738 | bellard | static int fd_open(BlockDriverState *bs) |
981 | 19cb3738 | bellard | { |
982 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
983 | 19cb3738 | bellard | int last_media_present;
|
984 | 19cb3738 | bellard | |
985 | 19cb3738 | bellard | if (s->type != FTYPE_FD)
|
986 | 19cb3738 | bellard | return 0; |
987 | 19cb3738 | bellard | last_media_present = (s->fd >= 0);
|
988 | 5fafdf24 | ths | if (s->fd >= 0 && |
989 | 19cb3738 | bellard | (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
990 | 19cb3738 | bellard | close(s->fd); |
991 | 19cb3738 | bellard | s->fd = -1;
|
992 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
993 | 19cb3738 | bellard | printf("Floppy closed\n");
|
994 | 19cb3738 | bellard | #endif
|
995 | 19cb3738 | bellard | } |
996 | 19cb3738 | bellard | if (s->fd < 0) { |
997 | 5fafdf24 | ths | if (s->fd_got_error &&
|
998 | 19cb3738 | bellard | (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
999 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1000 | 19cb3738 | bellard | printf("No floppy (open delayed)\n");
|
1001 | 19cb3738 | bellard | #endif
|
1002 | 19cb3738 | bellard | return -EIO;
|
1003 | 19cb3738 | bellard | } |
1004 | 6dd2db52 | blueswir1 | s->fd = open(bs->filename, s->fd_open_flags); |
1005 | 19cb3738 | bellard | if (s->fd < 0) { |
1006 | 19cb3738 | bellard | s->fd_error_time = qemu_get_clock(rt_clock); |
1007 | 19cb3738 | bellard | s->fd_got_error = 1;
|
1008 | 19cb3738 | bellard | if (last_media_present)
|
1009 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
1010 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1011 | 19cb3738 | bellard | printf("No floppy\n");
|
1012 | 19cb3738 | bellard | #endif
|
1013 | 19cb3738 | bellard | return -EIO;
|
1014 | 19cb3738 | bellard | } |
1015 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1016 | 19cb3738 | bellard | printf("Floppy opened\n");
|
1017 | 19cb3738 | bellard | #endif
|
1018 | 19cb3738 | bellard | } |
1019 | 19cb3738 | bellard | if (!last_media_present)
|
1020 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
1021 | 19cb3738 | bellard | s->fd_open_time = qemu_get_clock(rt_clock); |
1022 | 19cb3738 | bellard | s->fd_got_error = 0;
|
1023 | 19cb3738 | bellard | return 0; |
1024 | 19cb3738 | bellard | } |
1025 | 19cb3738 | bellard | |
1026 | 19cb3738 | bellard | static int raw_is_inserted(BlockDriverState *bs) |
1027 | 19cb3738 | bellard | { |
1028 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1029 | 19cb3738 | bellard | int ret;
|
1030 | 19cb3738 | bellard | |
1031 | 19cb3738 | bellard | switch(s->type) {
|
1032 | 19cb3738 | bellard | case FTYPE_CD:
|
1033 | 19cb3738 | bellard | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); |
1034 | 19cb3738 | bellard | if (ret == CDS_DISC_OK)
|
1035 | 19cb3738 | bellard | return 1; |
1036 | 19cb3738 | bellard | else
|
1037 | 19cb3738 | bellard | return 0; |
1038 | 19cb3738 | bellard | break;
|
1039 | 19cb3738 | bellard | case FTYPE_FD:
|
1040 | 19cb3738 | bellard | ret = fd_open(bs); |
1041 | 19cb3738 | bellard | return (ret >= 0); |
1042 | 19cb3738 | bellard | default:
|
1043 | 19cb3738 | bellard | return 1; |
1044 | 19cb3738 | bellard | } |
1045 | 19cb3738 | bellard | } |
1046 | 19cb3738 | bellard | |
1047 | 19cb3738 | bellard | /* currently only used by fdc.c, but a CD version would be good too */
|
1048 | 19cb3738 | bellard | static int raw_media_changed(BlockDriverState *bs) |
1049 | 19cb3738 | bellard | { |
1050 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1051 | 19cb3738 | bellard | |
1052 | 19cb3738 | bellard | switch(s->type) {
|
1053 | 19cb3738 | bellard | case FTYPE_FD:
|
1054 | 19cb3738 | bellard | { |
1055 | 19cb3738 | bellard | int ret;
|
1056 | 19cb3738 | bellard | /* XXX: we do not have a true media changed indication. It
|
1057 | 19cb3738 | bellard | does not work if the floppy is changed without trying
|
1058 | 19cb3738 | bellard | to read it */
|
1059 | 19cb3738 | bellard | fd_open(bs); |
1060 | 19cb3738 | bellard | ret = s->fd_media_changed; |
1061 | 19cb3738 | bellard | s->fd_media_changed = 0;
|
1062 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1063 | 19cb3738 | bellard | printf("Floppy changed=%d\n", ret);
|
1064 | 19cb3738 | bellard | #endif
|
1065 | 19cb3738 | bellard | return ret;
|
1066 | 19cb3738 | bellard | } |
1067 | 19cb3738 | bellard | default:
|
1068 | 19cb3738 | bellard | return -ENOTSUP;
|
1069 | 19cb3738 | bellard | } |
1070 | 19cb3738 | bellard | } |
1071 | 19cb3738 | bellard | |
1072 | 19cb3738 | bellard | static int raw_eject(BlockDriverState *bs, int eject_flag) |
1073 | 19cb3738 | bellard | { |
1074 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1075 | 19cb3738 | bellard | |
1076 | 19cb3738 | bellard | switch(s->type) {
|
1077 | 19cb3738 | bellard | case FTYPE_CD:
|
1078 | 19cb3738 | bellard | if (eject_flag) {
|
1079 | 19cb3738 | bellard | if (ioctl (s->fd, CDROMEJECT, NULL) < 0) |
1080 | 19cb3738 | bellard | perror("CDROMEJECT");
|
1081 | 19cb3738 | bellard | } else {
|
1082 | 19cb3738 | bellard | if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0) |
1083 | 19cb3738 | bellard | perror("CDROMEJECT");
|
1084 | 19cb3738 | bellard | } |
1085 | 19cb3738 | bellard | break;
|
1086 | 19cb3738 | bellard | case FTYPE_FD:
|
1087 | 19cb3738 | bellard | { |
1088 | 19cb3738 | bellard | int fd;
|
1089 | 19cb3738 | bellard | if (s->fd >= 0) { |
1090 | 19cb3738 | bellard | close(s->fd); |
1091 | 19cb3738 | bellard | s->fd = -1;
|
1092 | 19cb3738 | bellard | } |
1093 | 6dd2db52 | blueswir1 | fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK); |
1094 | 19cb3738 | bellard | if (fd >= 0) { |
1095 | 19cb3738 | bellard | if (ioctl(fd, FDEJECT, 0) < 0) |
1096 | 19cb3738 | bellard | perror("FDEJECT");
|
1097 | 19cb3738 | bellard | close(fd); |
1098 | 19cb3738 | bellard | } |
1099 | 19cb3738 | bellard | } |
1100 | 19cb3738 | bellard | break;
|
1101 | 19cb3738 | bellard | default:
|
1102 | 19cb3738 | bellard | return -ENOTSUP;
|
1103 | 19cb3738 | bellard | } |
1104 | 19cb3738 | bellard | return 0; |
1105 | 19cb3738 | bellard | } |
1106 | 19cb3738 | bellard | |
1107 | 19cb3738 | bellard | static int raw_set_locked(BlockDriverState *bs, int locked) |
1108 | 19cb3738 | bellard | { |
1109 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1110 | 19cb3738 | bellard | |
1111 | 19cb3738 | bellard | switch(s->type) {
|
1112 | 19cb3738 | bellard | case FTYPE_CD:
|
1113 | 19cb3738 | bellard | if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) { |
1114 | 19cb3738 | bellard | /* Note: an error can happen if the distribution automatically
|
1115 | 19cb3738 | bellard | mounts the CD-ROM */
|
1116 | 19cb3738 | bellard | // perror("CDROM_LOCKDOOR");
|
1117 | 19cb3738 | bellard | } |
1118 | 19cb3738 | bellard | break;
|
1119 | 19cb3738 | bellard | default:
|
1120 | 19cb3738 | bellard | return -ENOTSUP;
|
1121 | 19cb3738 | bellard | } |
1122 | 19cb3738 | bellard | return 0; |
1123 | 19cb3738 | bellard | } |
1124 | 19cb3738 | bellard | |
1125 | 985a03b0 | ths | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1126 | 985a03b0 | ths | { |
1127 | 985a03b0 | ths | BDRVRawState *s = bs->opaque; |
1128 | 985a03b0 | ths | |
1129 | 985a03b0 | ths | return ioctl(s->fd, req, buf);
|
1130 | 985a03b0 | ths | } |
1131 | 19cb3738 | bellard | #else
|
1132 | 19cb3738 | bellard | |
1133 | 08af02e2 | aliguori | static int fd_open(BlockDriverState *bs) |
1134 | 08af02e2 | aliguori | { |
1135 | 08af02e2 | aliguori | return 0; |
1136 | 08af02e2 | aliguori | } |
1137 | 08af02e2 | aliguori | |
1138 | 19cb3738 | bellard | static int raw_is_inserted(BlockDriverState *bs) |
1139 | 19cb3738 | bellard | { |
1140 | 19cb3738 | bellard | return 1; |
1141 | 19cb3738 | bellard | } |
1142 | 19cb3738 | bellard | |
1143 | 19cb3738 | bellard | static int raw_media_changed(BlockDriverState *bs) |
1144 | 19cb3738 | bellard | { |
1145 | 19cb3738 | bellard | return -ENOTSUP;
|
1146 | 19cb3738 | bellard | } |
1147 | 19cb3738 | bellard | |
1148 | 19cb3738 | bellard | static int raw_eject(BlockDriverState *bs, int eject_flag) |
1149 | 19cb3738 | bellard | { |
1150 | 19cb3738 | bellard | return -ENOTSUP;
|
1151 | 19cb3738 | bellard | } |
1152 | 19cb3738 | bellard | |
1153 | 19cb3738 | bellard | static int raw_set_locked(BlockDriverState *bs, int locked) |
1154 | 19cb3738 | bellard | { |
1155 | 19cb3738 | bellard | return -ENOTSUP;
|
1156 | 19cb3738 | bellard | } |
1157 | 19cb3738 | bellard | |
1158 | 985a03b0 | ths | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1159 | 985a03b0 | ths | { |
1160 | 985a03b0 | ths | return -ENOTSUP;
|
1161 | 985a03b0 | ths | } |
1162 | 19cb3738 | bellard | #endif /* !linux */ |
1163 | 19cb3738 | bellard | |
1164 | 19cb3738 | bellard | BlockDriver bdrv_host_device = { |
1165 | 19cb3738 | bellard | "host_device",
|
1166 | 19cb3738 | bellard | sizeof(BDRVRawState),
|
1167 | 19cb3738 | bellard | NULL, /* no probe for protocols */ |
1168 | 19cb3738 | bellard | hdev_open, |
1169 | 19cb3738 | bellard | NULL,
|
1170 | 19cb3738 | bellard | NULL,
|
1171 | 19cb3738 | bellard | raw_close, |
1172 | 19cb3738 | bellard | NULL,
|
1173 | 19cb3738 | bellard | raw_flush, |
1174 | 3b46e624 | ths | |
1175 | 414f0dab | blueswir1 | #ifdef CONFIG_AIO
|
1176 | 19cb3738 | bellard | .bdrv_aio_read = raw_aio_read, |
1177 | 19cb3738 | bellard | .bdrv_aio_write = raw_aio_write, |
1178 | 19cb3738 | bellard | .bdrv_aio_cancel = raw_aio_cancel, |
1179 | 19cb3738 | bellard | .aiocb_size = sizeof(RawAIOCB),
|
1180 | 414f0dab | blueswir1 | #endif
|
1181 | 3c529d93 | aliguori | |
1182 | 19cb3738 | bellard | .bdrv_pread = raw_pread, |
1183 | 19cb3738 | bellard | .bdrv_pwrite = raw_pwrite, |
1184 | 19cb3738 | bellard | .bdrv_getlength = raw_getlength, |
1185 | 19cb3738 | bellard | |
1186 | 19cb3738 | bellard | /* removable device support */
|
1187 | 19cb3738 | bellard | .bdrv_is_inserted = raw_is_inserted, |
1188 | 19cb3738 | bellard | .bdrv_media_changed = raw_media_changed, |
1189 | 19cb3738 | bellard | .bdrv_eject = raw_eject, |
1190 | 19cb3738 | bellard | .bdrv_set_locked = raw_set_locked, |
1191 | 985a03b0 | ths | /* generic scsi device */
|
1192 | 985a03b0 | ths | .bdrv_ioctl = raw_ioctl, |
1193 | 19cb3738 | bellard | }; |