root / block-raw-posix.c @ bbeea539
History | View | Annotate | Download (35.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 | 9f23011a | blueswir1 | #include <sys/cdio.h> |
59 | 1cb6c3fd | ths | #endif
|
60 | 83f64091 | bellard | |
61 | 128ab2ff | blueswir1 | #ifdef __OpenBSD__
|
62 | 128ab2ff | blueswir1 | #include <sys/ioctl.h> |
63 | 128ab2ff | blueswir1 | #include <sys/disklabel.h> |
64 | 128ab2ff | blueswir1 | #include <sys/dkio.h> |
65 | 128ab2ff | blueswir1 | #endif
|
66 | 128ab2ff | blueswir1 | |
67 | c5e97233 | blueswir1 | #ifdef __DragonFly__
|
68 | c5e97233 | blueswir1 | #include <sys/ioctl.h> |
69 | c5e97233 | blueswir1 | #include <sys/diskslice.h> |
70 | c5e97233 | blueswir1 | #endif
|
71 | c5e97233 | blueswir1 | |
72 | 19cb3738 | bellard | //#define DEBUG_FLOPPY
|
73 | 83f64091 | bellard | |
74 | faf07963 | pbrook | //#define DEBUG_BLOCK
|
75 | 03ff3ca3 | aliguori | #if defined(DEBUG_BLOCK)
|
76 | 93fcfe39 | aliguori | #define DEBUG_BLOCK_PRINT(formatCstr, args...) do { if (qemu_log_enabled()) \ |
77 | 31b1a7b4 | aliguori | { qemu_log(formatCstr, ##args); qemu_log_flush(); } } while (0) |
78 | 8c05dbf9 | ths | #else
|
79 | 8c05dbf9 | ths | #define DEBUG_BLOCK_PRINT(formatCstr, args...)
|
80 | 8c05dbf9 | ths | #endif
|
81 | 8c05dbf9 | ths | |
82 | f6465578 | aliguori | /* OS X does not have O_DSYNC */
|
83 | f6465578 | aliguori | #ifndef O_DSYNC
|
84 | 7ab064d2 | aliguori | #define O_DSYNC O_SYNC
|
85 | f6465578 | aliguori | #endif
|
86 | f6465578 | aliguori | |
87 | 9f7965c7 | aliguori | /* Approximate O_DIRECT with O_DSYNC if O_DIRECT isn't available */
|
88 | 9f7965c7 | aliguori | #ifndef O_DIRECT
|
89 | 9f7965c7 | aliguori | #define O_DIRECT O_DSYNC
|
90 | 9f7965c7 | aliguori | #endif
|
91 | 9f7965c7 | aliguori | |
92 | 19cb3738 | bellard | #define FTYPE_FILE 0 |
93 | 19cb3738 | bellard | #define FTYPE_CD 1 |
94 | 19cb3738 | bellard | #define FTYPE_FD 2 |
95 | 83f64091 | bellard | |
96 | bed5cc52 | bellard | #define ALIGNED_BUFFER_SIZE (32 * 512) |
97 | bed5cc52 | bellard | |
98 | 19cb3738 | bellard | /* if the FD is not accessed during that time (in ms), we try to
|
99 | 19cb3738 | bellard | reopen it to see if the disk has been changed */
|
100 | 19cb3738 | bellard | #define FD_OPEN_TIMEOUT 1000 |
101 | 83f64091 | bellard | |
102 | 19cb3738 | bellard | typedef struct BDRVRawState { |
103 | 19cb3738 | bellard | int fd;
|
104 | 19cb3738 | bellard | int type;
|
105 | 8c05dbf9 | ths | unsigned int lseek_err_cnt; |
106 | 19cb3738 | bellard | #if defined(__linux__)
|
107 | 19cb3738 | bellard | /* linux floppy specific */
|
108 | 6dd2db52 | blueswir1 | int fd_open_flags;
|
109 | 19cb3738 | bellard | int64_t fd_open_time; |
110 | 19cb3738 | bellard | int64_t fd_error_time; |
111 | 19cb3738 | bellard | int fd_got_error;
|
112 | 19cb3738 | bellard | int fd_media_changed;
|
113 | 83f64091 | bellard | #endif
|
114 | 9f23011a | blueswir1 | #if defined(__FreeBSD__)
|
115 | 9f23011a | blueswir1 | int cd_open_flags;
|
116 | 9f23011a | blueswir1 | #endif
|
117 | bed5cc52 | bellard | uint8_t* aligned_buf; |
118 | 19cb3738 | bellard | } BDRVRawState; |
119 | 19cb3738 | bellard | |
120 | a76bab49 | aliguori | static int posix_aio_init(void); |
121 | a76bab49 | aliguori | |
122 | 19cb3738 | bellard | static int fd_open(BlockDriverState *bs); |
123 | 83f64091 | bellard | |
124 | 9f23011a | blueswir1 | #if defined(__FreeBSD__)
|
125 | 9f23011a | blueswir1 | static int cd_open(BlockDriverState *bs); |
126 | 9f23011a | blueswir1 | #endif
|
127 | 9f23011a | blueswir1 | |
128 | 9f23011a | blueswir1 | static int raw_is_inserted(BlockDriverState *bs); |
129 | 9f23011a | blueswir1 | |
130 | 83f64091 | bellard | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
131 | 83f64091 | bellard | { |
132 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
133 | 19cb3738 | bellard | int fd, open_flags, ret;
|
134 | 83f64091 | bellard | |
135 | a76bab49 | aliguori | posix_aio_init(); |
136 | a76bab49 | aliguori | |
137 | 8c05dbf9 | ths | s->lseek_err_cnt = 0;
|
138 | 8c05dbf9 | ths | |
139 | 83f64091 | bellard | open_flags = O_BINARY; |
140 | 83f64091 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
141 | 83f64091 | bellard | open_flags |= O_RDWR; |
142 | 83f64091 | bellard | } else {
|
143 | 83f64091 | bellard | open_flags |= O_RDONLY; |
144 | 83f64091 | bellard | bs->read_only = 1;
|
145 | 83f64091 | bellard | } |
146 | 83f64091 | bellard | if (flags & BDRV_O_CREAT)
|
147 | 83f64091 | bellard | open_flags |= O_CREAT | O_TRUNC; |
148 | 9f7965c7 | aliguori | |
149 | 9f7965c7 | aliguori | /* Use O_DSYNC for write-through caching, no flags for write-back caching,
|
150 | 9f7965c7 | aliguori | * and O_DIRECT for no caching. */
|
151 | 9f7965c7 | aliguori | if ((flags & BDRV_O_NOCACHE))
|
152 | 33f00271 | balrog | open_flags |= O_DIRECT; |
153 | 9f7965c7 | aliguori | else if (!(flags & BDRV_O_CACHE_WB)) |
154 | 9f7965c7 | aliguori | open_flags |= O_DSYNC; |
155 | 83f64091 | bellard | |
156 | 19cb3738 | bellard | s->type = FTYPE_FILE; |
157 | 19cb3738 | bellard | |
158 | 83f64091 | bellard | fd = open(filename, open_flags, 0644);
|
159 | 19cb3738 | bellard | if (fd < 0) { |
160 | 19cb3738 | bellard | ret = -errno; |
161 | 19cb3738 | bellard | if (ret == -EROFS)
|
162 | 19cb3738 | bellard | ret = -EACCES; |
163 | 19cb3738 | bellard | return ret;
|
164 | 19cb3738 | bellard | } |
165 | 83f64091 | bellard | s->fd = fd; |
166 | bed5cc52 | bellard | s->aligned_buf = NULL;
|
167 | 9f7965c7 | aliguori | if ((flags & BDRV_O_NOCACHE)) {
|
168 | bed5cc52 | bellard | s->aligned_buf = qemu_memalign(512, ALIGNED_BUFFER_SIZE);
|
169 | bed5cc52 | bellard | if (s->aligned_buf == NULL) { |
170 | bed5cc52 | bellard | ret = -errno; |
171 | bed5cc52 | bellard | close(fd); |
172 | bed5cc52 | bellard | return ret;
|
173 | bed5cc52 | bellard | } |
174 | bed5cc52 | bellard | } |
175 | 83f64091 | bellard | return 0; |
176 | 83f64091 | bellard | } |
177 | 83f64091 | bellard | |
178 | 83f64091 | bellard | /* XXX: use host sector size if necessary with:
|
179 | 83f64091 | bellard | #ifdef DIOCGSECTORSIZE
|
180 | 83f64091 | bellard | {
|
181 | 83f64091 | bellard | unsigned int sectorsize = 512;
|
182 | 83f64091 | bellard | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
|
183 | 83f64091 | bellard | sectorsize > bufsize)
|
184 | 83f64091 | bellard | bufsize = sectorsize;
|
185 | 83f64091 | bellard | }
|
186 | 83f64091 | bellard | #endif
|
187 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
188 | 83f64091 | bellard | u_int32_t blockSize = 512;
|
189 | 83f64091 | bellard | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
|
190 | 83f64091 | bellard | bufsize = blockSize;
|
191 | 83f64091 | bellard | }
|
192 | 83f64091 | bellard | #endif
|
193 | 83f64091 | bellard | */
|
194 | 83f64091 | bellard | |
195 | bed5cc52 | bellard | /*
|
196 | bed5cc52 | bellard | * offset and count are in bytes, but must be multiples of 512 for files
|
197 | bed5cc52 | bellard | * opened with O_DIRECT. buf must be aligned to 512 bytes then.
|
198 | bed5cc52 | bellard | *
|
199 | bed5cc52 | bellard | * This function may be called without alignment if the caller ensures
|
200 | bed5cc52 | bellard | * that O_DIRECT is not in effect.
|
201 | bed5cc52 | bellard | */
|
202 | bed5cc52 | bellard | static int raw_pread_aligned(BlockDriverState *bs, int64_t offset, |
203 | 83f64091 | bellard | uint8_t *buf, int count)
|
204 | 83f64091 | bellard | { |
205 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
206 | 83f64091 | bellard | int ret;
|
207 | 3b46e624 | ths | |
208 | 19cb3738 | bellard | ret = fd_open(bs); |
209 | 19cb3738 | bellard | if (ret < 0) |
210 | 19cb3738 | bellard | return ret;
|
211 | 19cb3738 | bellard | |
212 | 985a03b0 | ths | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
213 | 8c05dbf9 | ths | ++(s->lseek_err_cnt); |
214 | 8c05dbf9 | ths | if(s->lseek_err_cnt <= 10) { |
215 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
216 | 92868412 | j_mayer | "] lseek failed : %d = %s\n",
|
217 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
218 | 8c05dbf9 | ths | bs->total_sectors, errno, strerror(errno)); |
219 | 8c05dbf9 | ths | } |
220 | 8c05dbf9 | ths | return -1; |
221 | 8c05dbf9 | ths | } |
222 | 8c05dbf9 | ths | s->lseek_err_cnt=0;
|
223 | 8c05dbf9 | ths | |
224 | 83f64091 | bellard | ret = read(s->fd, buf, count); |
225 | 8c05dbf9 | ths | if (ret == count)
|
226 | 8c05dbf9 | ths | goto label__raw_read__success;
|
227 | 8c05dbf9 | ths | |
228 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
229 | 92868412 | j_mayer | "] read failed %d : %d = %s\n",
|
230 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
231 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
232 | 8c05dbf9 | ths | |
233 | 8c05dbf9 | ths | /* Try harder for CDrom. */
|
234 | 8c05dbf9 | ths | if (bs->type == BDRV_TYPE_CDROM) {
|
235 | 8c05dbf9 | ths | lseek(s->fd, offset, SEEK_SET); |
236 | 8c05dbf9 | ths | ret = read(s->fd, buf, count); |
237 | 8c05dbf9 | ths | if (ret == count)
|
238 | 8c05dbf9 | ths | goto label__raw_read__success;
|
239 | 8c05dbf9 | ths | lseek(s->fd, offset, SEEK_SET); |
240 | 8c05dbf9 | ths | ret = read(s->fd, buf, count); |
241 | 8c05dbf9 | ths | if (ret == count)
|
242 | 8c05dbf9 | ths | goto label__raw_read__success;
|
243 | 8c05dbf9 | ths | |
244 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
245 | 92868412 | j_mayer | "] retry read failed %d : %d = %s\n",
|
246 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
247 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
248 | 8c05dbf9 | ths | } |
249 | 8c05dbf9 | ths | |
250 | 8c05dbf9 | ths | label__raw_read__success:
|
251 | 8c05dbf9 | ths | |
252 | 83f64091 | bellard | return ret;
|
253 | 83f64091 | bellard | } |
254 | 83f64091 | bellard | |
255 | bed5cc52 | bellard | /*
|
256 | bed5cc52 | bellard | * offset and count are in bytes, but must be multiples of 512 for files
|
257 | bed5cc52 | bellard | * opened with O_DIRECT. buf must be aligned to 512 bytes then.
|
258 | bed5cc52 | bellard | *
|
259 | bed5cc52 | bellard | * This function may be called without alignment if the caller ensures
|
260 | bed5cc52 | bellard | * that O_DIRECT is not in effect.
|
261 | bed5cc52 | bellard | */
|
262 | bed5cc52 | bellard | static int raw_pwrite_aligned(BlockDriverState *bs, int64_t offset, |
263 | 83f64091 | bellard | const uint8_t *buf, int count) |
264 | 83f64091 | bellard | { |
265 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
266 | 83f64091 | bellard | int ret;
|
267 | 3b46e624 | ths | |
268 | 19cb3738 | bellard | ret = fd_open(bs); |
269 | 19cb3738 | bellard | if (ret < 0) |
270 | 4141d4c2 | aliguori | return -errno;
|
271 | 19cb3738 | bellard | |
272 | 985a03b0 | ths | if (offset >= 0 && lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
273 | 8c05dbf9 | ths | ++(s->lseek_err_cnt); |
274 | 8c05dbf9 | ths | if(s->lseek_err_cnt) {
|
275 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" |
276 | 92868412 | j_mayer | PRId64 "] lseek failed : %d = %s\n",
|
277 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
278 | 8c05dbf9 | ths | bs->total_sectors, errno, strerror(errno)); |
279 | 8c05dbf9 | ths | } |
280 | 4141d4c2 | aliguori | return -EIO;
|
281 | 8c05dbf9 | ths | } |
282 | 8c05dbf9 | ths | s->lseek_err_cnt = 0;
|
283 | 8c05dbf9 | ths | |
284 | 83f64091 | bellard | ret = write(s->fd, buf, count); |
285 | 8c05dbf9 | ths | if (ret == count)
|
286 | 8c05dbf9 | ths | goto label__raw_write__success;
|
287 | 8c05dbf9 | ths | |
288 | 92868412 | j_mayer | DEBUG_BLOCK_PRINT("raw_pwrite(%d:%s, %" PRId64 ", %p, %d) [%" PRId64 |
289 | 92868412 | j_mayer | "] write failed %d : %d = %s\n",
|
290 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
291 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
292 | 8c05dbf9 | ths | |
293 | 8c05dbf9 | ths | label__raw_write__success:
|
294 | 8c05dbf9 | ths | |
295 | 4141d4c2 | aliguori | return (ret < 0) ? -errno : ret; |
296 | 83f64091 | bellard | } |
297 | 83f64091 | bellard | |
298 | bed5cc52 | bellard | |
299 | bed5cc52 | bellard | /*
|
300 | bed5cc52 | bellard | * offset and count are in bytes and possibly not aligned. For files opened
|
301 | bed5cc52 | bellard | * with O_DIRECT, necessary alignments are ensured before calling
|
302 | bed5cc52 | bellard | * raw_pread_aligned to do the actual read.
|
303 | bed5cc52 | bellard | */
|
304 | bed5cc52 | bellard | static int raw_pread(BlockDriverState *bs, int64_t offset, |
305 | bed5cc52 | bellard | uint8_t *buf, int count)
|
306 | bed5cc52 | bellard | { |
307 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
308 | bed5cc52 | bellard | int size, ret, shift, sum;
|
309 | bed5cc52 | bellard | |
310 | bed5cc52 | bellard | sum = 0;
|
311 | bed5cc52 | bellard | |
312 | bed5cc52 | bellard | if (s->aligned_buf != NULL) { |
313 | bed5cc52 | bellard | |
314 | bed5cc52 | bellard | if (offset & 0x1ff) { |
315 | bed5cc52 | bellard | /* align offset on a 512 bytes boundary */
|
316 | bed5cc52 | bellard | |
317 | bed5cc52 | bellard | shift = offset & 0x1ff;
|
318 | bed5cc52 | bellard | size = (shift + count + 0x1ff) & ~0x1ff; |
319 | bed5cc52 | bellard | if (size > ALIGNED_BUFFER_SIZE)
|
320 | bed5cc52 | bellard | size = ALIGNED_BUFFER_SIZE; |
321 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, size); |
322 | bed5cc52 | bellard | if (ret < 0) |
323 | bed5cc52 | bellard | return ret;
|
324 | bed5cc52 | bellard | |
325 | bed5cc52 | bellard | size = 512 - shift;
|
326 | bed5cc52 | bellard | if (size > count)
|
327 | bed5cc52 | bellard | size = count; |
328 | bed5cc52 | bellard | memcpy(buf, s->aligned_buf + shift, size); |
329 | bed5cc52 | bellard | |
330 | bed5cc52 | bellard | buf += size; |
331 | bed5cc52 | bellard | offset += size; |
332 | bed5cc52 | bellard | count -= size; |
333 | bed5cc52 | bellard | sum += size; |
334 | bed5cc52 | bellard | |
335 | bed5cc52 | bellard | if (count == 0) |
336 | bed5cc52 | bellard | return sum;
|
337 | bed5cc52 | bellard | } |
338 | bed5cc52 | bellard | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
339 | bed5cc52 | bellard | |
340 | bed5cc52 | bellard | /* read on aligned buffer */
|
341 | bed5cc52 | bellard | |
342 | bed5cc52 | bellard | while (count) {
|
343 | bed5cc52 | bellard | |
344 | bed5cc52 | bellard | size = (count + 0x1ff) & ~0x1ff; |
345 | bed5cc52 | bellard | if (size > ALIGNED_BUFFER_SIZE)
|
346 | bed5cc52 | bellard | size = ALIGNED_BUFFER_SIZE; |
347 | bed5cc52 | bellard | |
348 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset, s->aligned_buf, size); |
349 | bed5cc52 | bellard | if (ret < 0) |
350 | bed5cc52 | bellard | return ret;
|
351 | bed5cc52 | bellard | |
352 | bed5cc52 | bellard | size = ret; |
353 | bed5cc52 | bellard | if (size > count)
|
354 | bed5cc52 | bellard | size = count; |
355 | bed5cc52 | bellard | |
356 | bed5cc52 | bellard | memcpy(buf, s->aligned_buf, size); |
357 | bed5cc52 | bellard | |
358 | bed5cc52 | bellard | buf += size; |
359 | bed5cc52 | bellard | offset += size; |
360 | bed5cc52 | bellard | count -= size; |
361 | bed5cc52 | bellard | sum += size; |
362 | bed5cc52 | bellard | } |
363 | bed5cc52 | bellard | |
364 | bed5cc52 | bellard | return sum;
|
365 | bed5cc52 | bellard | } |
366 | bed5cc52 | bellard | } |
367 | bed5cc52 | bellard | |
368 | bed5cc52 | bellard | return raw_pread_aligned(bs, offset, buf, count) + sum;
|
369 | bed5cc52 | bellard | } |
370 | bed5cc52 | bellard | |
371 | eda578e5 | aliguori | static int raw_read(BlockDriverState *bs, int64_t sector_num, |
372 | eda578e5 | aliguori | uint8_t *buf, int nb_sectors)
|
373 | eda578e5 | aliguori | { |
374 | 537a1d4b | aliguori | int ret;
|
375 | 537a1d4b | aliguori | |
376 | 537a1d4b | aliguori | ret = raw_pread(bs, sector_num * 512, buf, nb_sectors * 512); |
377 | 537a1d4b | aliguori | if (ret == (nb_sectors * 512)) |
378 | 537a1d4b | aliguori | ret = 0;
|
379 | 537a1d4b | aliguori | return ret;
|
380 | eda578e5 | aliguori | } |
381 | eda578e5 | aliguori | |
382 | bed5cc52 | bellard | /*
|
383 | bed5cc52 | bellard | * offset and count are in bytes and possibly not aligned. For files opened
|
384 | bed5cc52 | bellard | * with O_DIRECT, necessary alignments are ensured before calling
|
385 | bed5cc52 | bellard | * raw_pwrite_aligned to do the actual write.
|
386 | bed5cc52 | bellard | */
|
387 | bed5cc52 | bellard | static int raw_pwrite(BlockDriverState *bs, int64_t offset, |
388 | bed5cc52 | bellard | const uint8_t *buf, int count) |
389 | bed5cc52 | bellard | { |
390 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
391 | bed5cc52 | bellard | int size, ret, shift, sum;
|
392 | bed5cc52 | bellard | |
393 | bed5cc52 | bellard | sum = 0;
|
394 | bed5cc52 | bellard | |
395 | bed5cc52 | bellard | if (s->aligned_buf != NULL) { |
396 | bed5cc52 | bellard | |
397 | bed5cc52 | bellard | if (offset & 0x1ff) { |
398 | bed5cc52 | bellard | /* align offset on a 512 bytes boundary */
|
399 | bed5cc52 | bellard | shift = offset & 0x1ff;
|
400 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset - shift, s->aligned_buf, 512);
|
401 | bed5cc52 | bellard | if (ret < 0) |
402 | bed5cc52 | bellard | return ret;
|
403 | bed5cc52 | bellard | |
404 | bed5cc52 | bellard | size = 512 - shift;
|
405 | bed5cc52 | bellard | if (size > count)
|
406 | bed5cc52 | bellard | size = count; |
407 | bed5cc52 | bellard | memcpy(s->aligned_buf + shift, buf, size); |
408 | bed5cc52 | bellard | |
409 | bed5cc52 | bellard | ret = raw_pwrite_aligned(bs, offset - shift, s->aligned_buf, 512);
|
410 | bed5cc52 | bellard | if (ret < 0) |
411 | bed5cc52 | bellard | return ret;
|
412 | bed5cc52 | bellard | |
413 | bed5cc52 | bellard | buf += size; |
414 | bed5cc52 | bellard | offset += size; |
415 | bed5cc52 | bellard | count -= size; |
416 | bed5cc52 | bellard | sum += size; |
417 | bed5cc52 | bellard | |
418 | bed5cc52 | bellard | if (count == 0) |
419 | bed5cc52 | bellard | return sum;
|
420 | bed5cc52 | bellard | } |
421 | bed5cc52 | bellard | if (count & 0x1ff || (uintptr_t) buf & 0x1ff) { |
422 | bed5cc52 | bellard | |
423 | bed5cc52 | bellard | while ((size = (count & ~0x1ff)) != 0) { |
424 | bed5cc52 | bellard | |
425 | bed5cc52 | bellard | if (size > ALIGNED_BUFFER_SIZE)
|
426 | bed5cc52 | bellard | size = ALIGNED_BUFFER_SIZE; |
427 | bed5cc52 | bellard | |
428 | bed5cc52 | bellard | memcpy(s->aligned_buf, buf, size); |
429 | bed5cc52 | bellard | |
430 | bed5cc52 | bellard | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, size); |
431 | bed5cc52 | bellard | if (ret < 0) |
432 | bed5cc52 | bellard | return ret;
|
433 | bed5cc52 | bellard | |
434 | bed5cc52 | bellard | buf += ret; |
435 | bed5cc52 | bellard | offset += ret; |
436 | bed5cc52 | bellard | count -= ret; |
437 | bed5cc52 | bellard | sum += ret; |
438 | bed5cc52 | bellard | } |
439 | bed5cc52 | bellard | /* here, count < 512 because (count & ~0x1ff) == 0 */
|
440 | bed5cc52 | bellard | if (count) {
|
441 | bed5cc52 | bellard | ret = raw_pread_aligned(bs, offset, s->aligned_buf, 512);
|
442 | bed5cc52 | bellard | if (ret < 0) |
443 | bed5cc52 | bellard | return ret;
|
444 | bed5cc52 | bellard | memcpy(s->aligned_buf, buf, count); |
445 | bed5cc52 | bellard | |
446 | bed5cc52 | bellard | ret = raw_pwrite_aligned(bs, offset, s->aligned_buf, 512);
|
447 | bed5cc52 | bellard | if (ret < 0) |
448 | bed5cc52 | bellard | return ret;
|
449 | bed5cc52 | bellard | if (count < ret)
|
450 | bed5cc52 | bellard | ret = count; |
451 | bed5cc52 | bellard | |
452 | bed5cc52 | bellard | sum += ret; |
453 | bed5cc52 | bellard | } |
454 | bed5cc52 | bellard | return sum;
|
455 | bed5cc52 | bellard | } |
456 | bed5cc52 | bellard | } |
457 | bed5cc52 | bellard | return raw_pwrite_aligned(bs, offset, buf, count) + sum;
|
458 | bed5cc52 | bellard | } |
459 | bed5cc52 | bellard | |
460 | eda578e5 | aliguori | static int raw_write(BlockDriverState *bs, int64_t sector_num, |
461 | eda578e5 | aliguori | const uint8_t *buf, int nb_sectors) |
462 | eda578e5 | aliguori | { |
463 | 537a1d4b | aliguori | int ret;
|
464 | 537a1d4b | aliguori | ret = raw_pwrite(bs, sector_num * 512, buf, nb_sectors * 512); |
465 | 537a1d4b | aliguori | if (ret == (nb_sectors * 512)) |
466 | 537a1d4b | aliguori | ret = 0;
|
467 | 537a1d4b | aliguori | return ret;
|
468 | eda578e5 | aliguori | } |
469 | eda578e5 | aliguori | |
470 | 414f0dab | blueswir1 | #ifdef CONFIG_AIO
|
471 | 83f64091 | bellard | /***********************************************************/
|
472 | 19cb3738 | bellard | /* Unix AIO using POSIX AIO */
|
473 | 83f64091 | bellard | |
474 | 83f64091 | bellard | typedef struct RawAIOCB { |
475 | ce1a14dc | pbrook | BlockDriverAIOCB common; |
476 | 3c529d93 | aliguori | struct qemu_paiocb aiocb;
|
477 | ce1a14dc | pbrook | struct RawAIOCB *next;
|
478 | bed5cc52 | bellard | int ret;
|
479 | 83f64091 | bellard | } RawAIOCB; |
480 | 83f64091 | bellard | |
481 | a76bab49 | aliguori | typedef struct PosixAioState |
482 | a76bab49 | aliguori | { |
483 | 9e472e10 | aliguori | int rfd, wfd;
|
484 | a76bab49 | aliguori | RawAIOCB *first_aio; |
485 | a76bab49 | aliguori | } PosixAioState; |
486 | 83f64091 | bellard | |
487 | a76bab49 | aliguori | static void posix_aio_read(void *opaque) |
488 | 83f64091 | bellard | { |
489 | a76bab49 | aliguori | PosixAioState *s = opaque; |
490 | ce1a14dc | pbrook | RawAIOCB *acb, **pacb; |
491 | 83f64091 | bellard | int ret;
|
492 | 9e472e10 | aliguori | ssize_t len; |
493 | 9e472e10 | aliguori | |
494 | e20e830b | aliguori | /* read all bytes from signal pipe */
|
495 | e20e830b | aliguori | for (;;) {
|
496 | e20e830b | aliguori | char bytes[16]; |
497 | 9e472e10 | aliguori | |
498 | e20e830b | aliguori | len = read(s->rfd, bytes, sizeof(bytes));
|
499 | 2c41a5f9 | aliguori | if (len == -1 && errno == EINTR) |
500 | e20e830b | aliguori | continue; /* try again */ |
501 | e20e830b | aliguori | if (len == sizeof(bytes)) |
502 | e20e830b | aliguori | continue; /* more to read */ |
503 | e20e830b | aliguori | break;
|
504 | e20e830b | aliguori | } |
505 | 83f64091 | bellard | |
506 | 83f64091 | bellard | for(;;) {
|
507 | a76bab49 | aliguori | pacb = &s->first_aio; |
508 | 83f64091 | bellard | for(;;) {
|
509 | 83f64091 | bellard | acb = *pacb; |
510 | 83f64091 | bellard | if (!acb)
|
511 | 83f64091 | bellard | goto the_end;
|
512 | 3c529d93 | aliguori | ret = qemu_paio_error(&acb->aiocb); |
513 | 83f64091 | bellard | if (ret == ECANCELED) {
|
514 | 83f64091 | bellard | /* remove the request */
|
515 | ce1a14dc | pbrook | *pacb = acb->next; |
516 | ce1a14dc | pbrook | qemu_aio_release(acb); |
517 | 83f64091 | bellard | } else if (ret != EINPROGRESS) { |
518 | 83f64091 | bellard | /* end of aio */
|
519 | 83f64091 | bellard | if (ret == 0) { |
520 | 3c529d93 | aliguori | ret = qemu_paio_return(&acb->aiocb); |
521 | ce1a14dc | pbrook | if (ret == acb->aiocb.aio_nbytes)
|
522 | 83f64091 | bellard | ret = 0;
|
523 | 83f64091 | bellard | else
|
524 | 19cb3738 | bellard | ret = -EINVAL; |
525 | 83f64091 | bellard | } else {
|
526 | 83f64091 | bellard | ret = -ret; |
527 | 83f64091 | bellard | } |
528 | 83f64091 | bellard | /* remove the request */
|
529 | ce1a14dc | pbrook | *pacb = acb->next; |
530 | 83f64091 | bellard | /* call the callback */
|
531 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, ret); |
532 | ce1a14dc | pbrook | qemu_aio_release(acb); |
533 | 83f64091 | bellard | break;
|
534 | 83f64091 | bellard | } else {
|
535 | ce1a14dc | pbrook | pacb = &acb->next; |
536 | 83f64091 | bellard | } |
537 | 83f64091 | bellard | } |
538 | 83f64091 | bellard | } |
539 | 83f64091 | bellard | the_end: ;
|
540 | 83f64091 | bellard | } |
541 | 83f64091 | bellard | |
542 | a76bab49 | aliguori | static int posix_aio_flush(void *opaque) |
543 | baf35cb9 | aliguori | { |
544 | a76bab49 | aliguori | PosixAioState *s = opaque; |
545 | a76bab49 | aliguori | return !!s->first_aio;
|
546 | a76bab49 | aliguori | } |
547 | a76bab49 | aliguori | |
548 | a76bab49 | aliguori | static PosixAioState *posix_aio_state;
|
549 | baf35cb9 | aliguori | |
550 | 9e472e10 | aliguori | static void aio_signal_handler(int signum) |
551 | 9e472e10 | aliguori | { |
552 | 9e472e10 | aliguori | if (posix_aio_state) {
|
553 | 9e472e10 | aliguori | char byte = 0; |
554 | 9e472e10 | aliguori | |
555 | 9e472e10 | aliguori | write(posix_aio_state->wfd, &byte, sizeof(byte));
|
556 | 9e472e10 | aliguori | } |
557 | 9e472e10 | aliguori | |
558 | 9e472e10 | aliguori | qemu_service_io(); |
559 | 9e472e10 | aliguori | } |
560 | 9e472e10 | aliguori | |
561 | a76bab49 | aliguori | static int posix_aio_init(void) |
562 | a76bab49 | aliguori | { |
563 | 9e472e10 | aliguori | struct sigaction act;
|
564 | a76bab49 | aliguori | PosixAioState *s; |
565 | 9e472e10 | aliguori | int fds[2]; |
566 | 3c529d93 | aliguori | struct qemu_paioinit ai;
|
567 | a76bab49 | aliguori | |
568 | a76bab49 | aliguori | if (posix_aio_state)
|
569 | a76bab49 | aliguori | return 0; |
570 | ad02ad6f | aliguori | |
571 | a76bab49 | aliguori | s = qemu_malloc(sizeof(PosixAioState));
|
572 | baf35cb9 | aliguori | |
573 | 9e472e10 | aliguori | sigfillset(&act.sa_mask); |
574 | 9e472e10 | aliguori | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ |
575 | 9e472e10 | aliguori | act.sa_handler = aio_signal_handler; |
576 | 9e472e10 | aliguori | sigaction(SIGUSR2, &act, NULL);
|
577 | 9e472e10 | aliguori | |
578 | a76bab49 | aliguori | s->first_aio = NULL;
|
579 | 9e472e10 | aliguori | if (pipe(fds) == -1) { |
580 | 9e472e10 | aliguori | fprintf(stderr, "failed to create pipe\n");
|
581 | 27463101 | aliguori | return -errno;
|
582 | 27463101 | aliguori | } |
583 | 2c41a5f9 | aliguori | |
584 | 9e472e10 | aliguori | s->rfd = fds[0];
|
585 | 9e472e10 | aliguori | s->wfd = fds[1];
|
586 | 9e472e10 | aliguori | |
587 | e20e830b | aliguori | fcntl(s->rfd, F_SETFL, O_NONBLOCK); |
588 | 9e472e10 | aliguori | fcntl(s->wfd, F_SETFL, O_NONBLOCK); |
589 | 2c41a5f9 | aliguori | |
590 | 9e472e10 | aliguori | qemu_aio_set_fd_handler(s->rfd, posix_aio_read, NULL, posix_aio_flush, s);
|
591 | baf35cb9 | aliguori | |
592 | 3c529d93 | aliguori | memset(&ai, 0, sizeof(ai)); |
593 | 3c529d93 | aliguori | ai.aio_threads = 64;
|
594 | 3c529d93 | aliguori | ai.aio_num = 64;
|
595 | 3c529d93 | aliguori | qemu_paio_init(&ai); |
596 | acce87f9 | aliguori | |
597 | a76bab49 | aliguori | posix_aio_state = s; |
598 | 6eb5733a | bellard | |
599 | a76bab49 | aliguori | return 0; |
600 | 83f64091 | bellard | } |
601 | 83f64091 | bellard | |
602 | ce1a14dc | pbrook | static RawAIOCB *raw_aio_setup(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 | BDRVRawState *s = bs->opaque; |
607 | ce1a14dc | pbrook | RawAIOCB *acb; |
608 | ce1a14dc | pbrook | |
609 | 19cb3738 | bellard | if (fd_open(bs) < 0) |
610 | 19cb3738 | bellard | return NULL; |
611 | 19cb3738 | bellard | |
612 | ce1a14dc | pbrook | acb = qemu_aio_get(bs, cb, opaque); |
613 | ce1a14dc | pbrook | if (!acb)
|
614 | ce1a14dc | pbrook | return NULL; |
615 | 3c529d93 | aliguori | acb->aiocb.aio_fildes = s->fd; |
616 | 55f11ca3 | blueswir1 | acb->aiocb.ev_signo = SIGUSR2; |
617 | ce1a14dc | pbrook | acb->aiocb.aio_buf = buf; |
618 | 985a03b0 | ths | if (nb_sectors < 0) |
619 | 985a03b0 | ths | acb->aiocb.aio_nbytes = -nb_sectors; |
620 | 985a03b0 | ths | else
|
621 | 985a03b0 | ths | acb->aiocb.aio_nbytes = nb_sectors * 512;
|
622 | ce1a14dc | pbrook | acb->aiocb.aio_offset = sector_num * 512;
|
623 | a76bab49 | aliguori | acb->next = posix_aio_state->first_aio; |
624 | a76bab49 | aliguori | posix_aio_state->first_aio = acb; |
625 | ce1a14dc | pbrook | return acb;
|
626 | 83f64091 | bellard | } |
627 | 83f64091 | bellard | |
628 | bed5cc52 | bellard | static void raw_aio_em_cb(void* opaque) |
629 | bed5cc52 | bellard | { |
630 | bed5cc52 | bellard | RawAIOCB *acb = opaque; |
631 | bed5cc52 | bellard | acb->common.cb(acb->common.opaque, acb->ret); |
632 | bed5cc52 | bellard | qemu_aio_release(acb); |
633 | bed5cc52 | bellard | } |
634 | bed5cc52 | bellard | |
635 | 22bf1458 | aliguori | static void raw_aio_remove(RawAIOCB *acb) |
636 | 22bf1458 | aliguori | { |
637 | 22bf1458 | aliguori | RawAIOCB **pacb; |
638 | 22bf1458 | aliguori | |
639 | 22bf1458 | aliguori | /* remove the callback from the queue */
|
640 | 22bf1458 | aliguori | pacb = &posix_aio_state->first_aio; |
641 | 22bf1458 | aliguori | for(;;) {
|
642 | 22bf1458 | aliguori | if (*pacb == NULL) { |
643 | 7a11b22e | aliguori | fprintf(stderr, "raw_aio_remove: aio request not found!\n");
|
644 | 22bf1458 | aliguori | break;
|
645 | 22bf1458 | aliguori | } else if (*pacb == acb) { |
646 | 22bf1458 | aliguori | *pacb = acb->next; |
647 | 22bf1458 | aliguori | qemu_aio_release(acb); |
648 | 22bf1458 | aliguori | break;
|
649 | 22bf1458 | aliguori | } |
650 | 7a11b22e | aliguori | pacb = &(*pacb)->next; |
651 | 22bf1458 | aliguori | } |
652 | 22bf1458 | aliguori | } |
653 | 22bf1458 | aliguori | |
654 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
|
655 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
656 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
657 | 83f64091 | bellard | { |
658 | ce1a14dc | pbrook | RawAIOCB *acb; |
659 | 83f64091 | bellard | |
660 | bed5cc52 | bellard | /*
|
661 | bed5cc52 | bellard | * If O_DIRECT is used and the buffer is not aligned fall back
|
662 | bed5cc52 | bellard | * to synchronous IO.
|
663 | bed5cc52 | bellard | */
|
664 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
665 | bed5cc52 | bellard | |
666 | bed5cc52 | bellard | if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) { |
667 | bed5cc52 | bellard | QEMUBH *bh; |
668 | bed5cc52 | bellard | acb = qemu_aio_get(bs, cb, opaque); |
669 | bed5cc52 | bellard | acb->ret = raw_pread(bs, 512 * sector_num, buf, 512 * nb_sectors); |
670 | bed5cc52 | bellard | bh = qemu_bh_new(raw_aio_em_cb, acb); |
671 | bed5cc52 | bellard | qemu_bh_schedule(bh); |
672 | bed5cc52 | bellard | return &acb->common;
|
673 | bed5cc52 | bellard | } |
674 | bed5cc52 | bellard | |
675 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
676 | ce1a14dc | pbrook | if (!acb)
|
677 | ce1a14dc | pbrook | return NULL; |
678 | 3c529d93 | aliguori | if (qemu_paio_read(&acb->aiocb) < 0) { |
679 | 22bf1458 | aliguori | raw_aio_remove(acb); |
680 | ce1a14dc | pbrook | return NULL; |
681 | 5fafdf24 | ths | } |
682 | ce1a14dc | pbrook | return &acb->common;
|
683 | 83f64091 | bellard | } |
684 | 83f64091 | bellard | |
685 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
|
686 | ce1a14dc | pbrook | int64_t sector_num, const uint8_t *buf, int nb_sectors, |
687 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
688 | 83f64091 | bellard | { |
689 | ce1a14dc | pbrook | RawAIOCB *acb; |
690 | 83f64091 | bellard | |
691 | bed5cc52 | bellard | /*
|
692 | bed5cc52 | bellard | * If O_DIRECT is used and the buffer is not aligned fall back
|
693 | bed5cc52 | bellard | * to synchronous IO.
|
694 | bed5cc52 | bellard | */
|
695 | bed5cc52 | bellard | BDRVRawState *s = bs->opaque; |
696 | bed5cc52 | bellard | |
697 | bed5cc52 | bellard | if (unlikely(s->aligned_buf != NULL && ((uintptr_t) buf % 512))) { |
698 | bed5cc52 | bellard | QEMUBH *bh; |
699 | bed5cc52 | bellard | acb = qemu_aio_get(bs, cb, opaque); |
700 | bed5cc52 | bellard | acb->ret = raw_pwrite(bs, 512 * sector_num, buf, 512 * nb_sectors); |
701 | bed5cc52 | bellard | bh = qemu_bh_new(raw_aio_em_cb, acb); |
702 | bed5cc52 | bellard | qemu_bh_schedule(bh); |
703 | bed5cc52 | bellard | return &acb->common;
|
704 | bed5cc52 | bellard | } |
705 | bed5cc52 | bellard | |
706 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque); |
707 | ce1a14dc | pbrook | if (!acb)
|
708 | ce1a14dc | pbrook | return NULL; |
709 | 3c529d93 | aliguori | if (qemu_paio_write(&acb->aiocb) < 0) { |
710 | 22bf1458 | aliguori | raw_aio_remove(acb); |
711 | ce1a14dc | pbrook | return NULL; |
712 | 5fafdf24 | ths | } |
713 | ce1a14dc | pbrook | return &acb->common;
|
714 | 83f64091 | bellard | } |
715 | 83f64091 | bellard | |
716 | ce1a14dc | pbrook | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) |
717 | 83f64091 | bellard | { |
718 | 83f64091 | bellard | int ret;
|
719 | ce1a14dc | pbrook | RawAIOCB *acb = (RawAIOCB *)blockacb; |
720 | 83f64091 | bellard | |
721 | 3c529d93 | aliguori | ret = qemu_paio_cancel(acb->aiocb.aio_fildes, &acb->aiocb); |
722 | 3c529d93 | aliguori | if (ret == QEMU_PAIO_NOTCANCELED) {
|
723 | 83f64091 | bellard | /* fail safe: if the aio could not be canceled, we wait for
|
724 | 83f64091 | bellard | it */
|
725 | 3c529d93 | aliguori | while (qemu_paio_error(&acb->aiocb) == EINPROGRESS);
|
726 | 83f64091 | bellard | } |
727 | 83f64091 | bellard | |
728 | 22bf1458 | aliguori | raw_aio_remove(acb); |
729 | 83f64091 | bellard | } |
730 | a76bab49 | aliguori | #else /* CONFIG_AIO */ |
731 | a76bab49 | aliguori | static int posix_aio_init(void) |
732 | 414f0dab | blueswir1 | { |
733 | 674a24ac | blueswir1 | return 0; |
734 | 414f0dab | blueswir1 | } |
735 | 414f0dab | blueswir1 | #endif /* CONFIG_AIO */ |
736 | 414f0dab | blueswir1 | |
737 | 53538725 | aliguori | |
738 | 83f64091 | bellard | static void raw_close(BlockDriverState *bs) |
739 | 83f64091 | bellard | { |
740 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
741 | 19cb3738 | bellard | if (s->fd >= 0) { |
742 | 19cb3738 | bellard | close(s->fd); |
743 | 19cb3738 | bellard | s->fd = -1;
|
744 | bed5cc52 | bellard | if (s->aligned_buf != NULL) |
745 | bed5cc52 | bellard | qemu_free(s->aligned_buf); |
746 | 19cb3738 | bellard | } |
747 | 83f64091 | bellard | } |
748 | 83f64091 | bellard | |
749 | 83f64091 | bellard | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
750 | 83f64091 | bellard | { |
751 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
752 | 19cb3738 | bellard | if (s->type != FTYPE_FILE)
|
753 | 19cb3738 | bellard | return -ENOTSUP;
|
754 | 83f64091 | bellard | if (ftruncate(s->fd, offset) < 0) |
755 | 83f64091 | bellard | return -errno;
|
756 | 83f64091 | bellard | return 0; |
757 | 83f64091 | bellard | } |
758 | 83f64091 | bellard | |
759 | 128ab2ff | blueswir1 | #ifdef __OpenBSD__
|
760 | 128ab2ff | blueswir1 | static int64_t raw_getlength(BlockDriverState *bs)
|
761 | 128ab2ff | blueswir1 | { |
762 | 128ab2ff | blueswir1 | BDRVRawState *s = bs->opaque; |
763 | 128ab2ff | blueswir1 | int fd = s->fd;
|
764 | 128ab2ff | blueswir1 | struct stat st;
|
765 | 128ab2ff | blueswir1 | |
766 | 128ab2ff | blueswir1 | if (fstat(fd, &st))
|
767 | 128ab2ff | blueswir1 | return -1; |
768 | 128ab2ff | blueswir1 | if (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode)) {
|
769 | 128ab2ff | blueswir1 | struct disklabel dl;
|
770 | 128ab2ff | blueswir1 | |
771 | 128ab2ff | blueswir1 | if (ioctl(fd, DIOCGDINFO, &dl))
|
772 | 128ab2ff | blueswir1 | return -1; |
773 | 128ab2ff | blueswir1 | return (uint64_t)dl.d_secsize *
|
774 | 128ab2ff | blueswir1 | dl.d_partitions[DISKPART(st.st_rdev)].p_size; |
775 | 128ab2ff | blueswir1 | } else
|
776 | 128ab2ff | blueswir1 | return st.st_size;
|
777 | 128ab2ff | blueswir1 | } |
778 | 128ab2ff | blueswir1 | #else /* !__OpenBSD__ */ |
779 | 83f64091 | bellard | static int64_t raw_getlength(BlockDriverState *bs)
|
780 | 83f64091 | bellard | { |
781 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
782 | 83f64091 | bellard | int fd = s->fd;
|
783 | 83f64091 | bellard | int64_t size; |
784 | 179a2c19 | blueswir1 | #ifdef HOST_BSD
|
785 | 83f64091 | bellard | struct stat sb;
|
786 | 9f23011a | blueswir1 | #ifdef __FreeBSD__
|
787 | 9f23011a | blueswir1 | int reopened = 0; |
788 | 9f23011a | blueswir1 | #endif
|
789 | 83f64091 | bellard | #endif
|
790 | 83f64091 | bellard | #ifdef __sun__
|
791 | 83f64091 | bellard | struct dk_minfo minfo;
|
792 | 83f64091 | bellard | int rv;
|
793 | 83f64091 | bellard | #endif
|
794 | 19cb3738 | bellard | int ret;
|
795 | 19cb3738 | bellard | |
796 | 19cb3738 | bellard | ret = fd_open(bs); |
797 | 19cb3738 | bellard | if (ret < 0) |
798 | 19cb3738 | bellard | return ret;
|
799 | 83f64091 | bellard | |
800 | 179a2c19 | blueswir1 | #ifdef HOST_BSD
|
801 | 9f23011a | blueswir1 | #ifdef __FreeBSD__
|
802 | 9f23011a | blueswir1 | again:
|
803 | 9f23011a | blueswir1 | #endif
|
804 | 83f64091 | bellard | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
|
805 | 83f64091 | bellard | #ifdef DIOCGMEDIASIZE
|
806 | 83f64091 | bellard | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
|
807 | c5e97233 | blueswir1 | #elif defined(DIOCGPART)
|
808 | c5e97233 | blueswir1 | { |
809 | c5e97233 | blueswir1 | struct partinfo pi;
|
810 | c5e97233 | blueswir1 | if (ioctl(fd, DIOCGPART, &pi) == 0) |
811 | c5e97233 | blueswir1 | size = pi.media_size; |
812 | c5e97233 | blueswir1 | else
|
813 | c5e97233 | blueswir1 | size = 0;
|
814 | c5e97233 | blueswir1 | } |
815 | c5e97233 | blueswir1 | if (size == 0) |
816 | 83f64091 | bellard | #endif
|
817 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
818 | 83f64091 | bellard | size = LONG_LONG_MAX; |
819 | 83f64091 | bellard | #else
|
820 | 83f64091 | bellard | size = lseek(fd, 0LL, SEEK_END);
|
821 | 83f64091 | bellard | #endif
|
822 | 9f23011a | blueswir1 | #ifdef __FreeBSD__
|
823 | 9f23011a | blueswir1 | switch(s->type) {
|
824 | 9f23011a | blueswir1 | case FTYPE_CD:
|
825 | 9f23011a | blueswir1 | /* XXX FreeBSD acd returns UINT_MAX sectors for an empty drive */
|
826 | 9f23011a | blueswir1 | if (size == 2048LL * (unsigned)-1) |
827 | 9f23011a | blueswir1 | size = 0;
|
828 | 9f23011a | blueswir1 | /* XXX no disc? maybe we need to reopen... */
|
829 | 9f23011a | blueswir1 | if (size <= 0 && !reopened && cd_open(bs) >= 0) { |
830 | 9f23011a | blueswir1 | reopened = 1;
|
831 | 9f23011a | blueswir1 | goto again;
|
832 | 9f23011a | blueswir1 | } |
833 | 9f23011a | blueswir1 | } |
834 | 9f23011a | blueswir1 | #endif
|
835 | 83f64091 | bellard | } else
|
836 | 83f64091 | bellard | #endif
|
837 | 83f64091 | bellard | #ifdef __sun__
|
838 | 83f64091 | bellard | /*
|
839 | 83f64091 | bellard | * use the DKIOCGMEDIAINFO ioctl to read the size.
|
840 | 83f64091 | bellard | */
|
841 | 83f64091 | bellard | rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); |
842 | 83f64091 | bellard | if ( rv != -1 ) { |
843 | 83f64091 | bellard | size = minfo.dki_lbsize * minfo.dki_capacity; |
844 | 83f64091 | bellard | } else /* there are reports that lseek on some devices |
845 | 83f64091 | bellard | fails, but irc discussion said that contingency
|
846 | 83f64091 | bellard | on contingency was overkill */
|
847 | 83f64091 | bellard | #endif
|
848 | 83f64091 | bellard | { |
849 | 83f64091 | bellard | size = lseek(fd, 0, SEEK_END);
|
850 | 83f64091 | bellard | } |
851 | 83f64091 | bellard | return size;
|
852 | 83f64091 | bellard | } |
853 | 128ab2ff | blueswir1 | #endif
|
854 | 83f64091 | bellard | |
855 | 83f64091 | bellard | static int raw_create(const char *filename, int64_t total_size, |
856 | 83f64091 | bellard | const char *backing_file, int flags) |
857 | 83f64091 | bellard | { |
858 | 83f64091 | bellard | int fd;
|
859 | 83f64091 | bellard | |
860 | 83f64091 | bellard | if (flags || backing_file)
|
861 | 83f64091 | bellard | return -ENOTSUP;
|
862 | 83f64091 | bellard | |
863 | 5fafdf24 | ths | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
864 | 83f64091 | bellard | 0644);
|
865 | 83f64091 | bellard | if (fd < 0) |
866 | 83f64091 | bellard | return -EIO;
|
867 | 83f64091 | bellard | ftruncate(fd, total_size * 512);
|
868 | 83f64091 | bellard | close(fd); |
869 | 83f64091 | bellard | return 0; |
870 | 83f64091 | bellard | } |
871 | 83f64091 | bellard | |
872 | 83f64091 | bellard | static void raw_flush(BlockDriverState *bs) |
873 | 83f64091 | bellard | { |
874 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
875 | 83f64091 | bellard | fsync(s->fd); |
876 | 83f64091 | bellard | } |
877 | 83f64091 | bellard | |
878 | 83f64091 | bellard | BlockDriver bdrv_raw = { |
879 | 83f64091 | bellard | "raw",
|
880 | 83f64091 | bellard | sizeof(BDRVRawState),
|
881 | 83f64091 | bellard | NULL, /* no probe for protocols */ |
882 | 83f64091 | bellard | raw_open, |
883 | 83f64091 | bellard | NULL,
|
884 | 83f64091 | bellard | NULL,
|
885 | 83f64091 | bellard | raw_close, |
886 | 83f64091 | bellard | raw_create, |
887 | 83f64091 | bellard | raw_flush, |
888 | 3b46e624 | ths | |
889 | 414f0dab | blueswir1 | #ifdef CONFIG_AIO
|
890 | 83f64091 | bellard | .bdrv_aio_read = raw_aio_read, |
891 | 83f64091 | bellard | .bdrv_aio_write = raw_aio_write, |
892 | 83f64091 | bellard | .bdrv_aio_cancel = raw_aio_cancel, |
893 | ce1a14dc | pbrook | .aiocb_size = sizeof(RawAIOCB),
|
894 | 414f0dab | blueswir1 | #endif
|
895 | 3c529d93 | aliguori | |
896 | eda578e5 | aliguori | .bdrv_read = raw_read, |
897 | eda578e5 | aliguori | .bdrv_write = raw_write, |
898 | 83f64091 | bellard | .bdrv_truncate = raw_truncate, |
899 | 83f64091 | bellard | .bdrv_getlength = raw_getlength, |
900 | 83f64091 | bellard | }; |
901 | 83f64091 | bellard | |
902 | 19cb3738 | bellard | /***********************************************/
|
903 | 19cb3738 | bellard | /* host device */
|
904 | 19cb3738 | bellard | |
905 | 19cb3738 | bellard | #ifdef CONFIG_COCOA
|
906 | 19cb3738 | bellard | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
|
907 | 19cb3738 | bellard | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); |
908 | 19cb3738 | bellard | |
909 | 19cb3738 | bellard | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) |
910 | 19cb3738 | bellard | { |
911 | 5fafdf24 | ths | kern_return_t kernResult; |
912 | 19cb3738 | bellard | mach_port_t masterPort; |
913 | 19cb3738 | bellard | CFMutableDictionaryRef classesToMatch; |
914 | 19cb3738 | bellard | |
915 | 19cb3738 | bellard | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); |
916 | 19cb3738 | bellard | if ( KERN_SUCCESS != kernResult ) {
|
917 | 19cb3738 | bellard | printf( "IOMasterPort returned %d\n", kernResult );
|
918 | 19cb3738 | bellard | } |
919 | 3b46e624 | ths | |
920 | 5fafdf24 | ths | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
921 | 19cb3738 | bellard | if ( classesToMatch == NULL ) { |
922 | 19cb3738 | bellard | printf( "IOServiceMatching returned a NULL dictionary.\n" );
|
923 | 19cb3738 | bellard | } else {
|
924 | 19cb3738 | bellard | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); |
925 | 19cb3738 | bellard | } |
926 | 19cb3738 | bellard | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); |
927 | 19cb3738 | bellard | if ( KERN_SUCCESS != kernResult )
|
928 | 19cb3738 | bellard | { |
929 | 19cb3738 | bellard | printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
|
930 | 19cb3738 | bellard | } |
931 | 3b46e624 | ths | |
932 | 19cb3738 | bellard | return kernResult;
|
933 | 19cb3738 | bellard | } |
934 | 19cb3738 | bellard | |
935 | 19cb3738 | bellard | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
|
936 | 19cb3738 | bellard | { |
937 | 19cb3738 | bellard | io_object_t nextMedia; |
938 | 19cb3738 | bellard | kern_return_t kernResult = KERN_FAILURE; |
939 | 19cb3738 | bellard | *bsdPath = '\0';
|
940 | 19cb3738 | bellard | nextMedia = IOIteratorNext( mediaIterator ); |
941 | 19cb3738 | bellard | if ( nextMedia )
|
942 | 19cb3738 | bellard | { |
943 | 19cb3738 | bellard | CFTypeRef bsdPathAsCFString; |
944 | 19cb3738 | bellard | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
|
945 | 19cb3738 | bellard | if ( bsdPathAsCFString ) {
|
946 | 19cb3738 | bellard | size_t devPathLength; |
947 | 19cb3738 | bellard | strcpy( bsdPath, _PATH_DEV ); |
948 | 19cb3738 | bellard | strcat( bsdPath, "r" );
|
949 | 19cb3738 | bellard | devPathLength = strlen( bsdPath ); |
950 | 19cb3738 | bellard | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
|
951 | 19cb3738 | bellard | kernResult = KERN_SUCCESS; |
952 | 19cb3738 | bellard | } |
953 | 19cb3738 | bellard | CFRelease( bsdPathAsCFString ); |
954 | 19cb3738 | bellard | } |
955 | 19cb3738 | bellard | IOObjectRelease( nextMedia ); |
956 | 19cb3738 | bellard | } |
957 | 3b46e624 | ths | |
958 | 19cb3738 | bellard | return kernResult;
|
959 | 19cb3738 | bellard | } |
960 | 19cb3738 | bellard | |
961 | 19cb3738 | bellard | #endif
|
962 | 19cb3738 | bellard | |
963 | 19cb3738 | bellard | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
964 | 19cb3738 | bellard | { |
965 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
966 | 3c529d93 | aliguori | int fd, open_flags, ret;
|
967 | 19cb3738 | bellard | |
968 | a76bab49 | aliguori | posix_aio_init(); |
969 | a76bab49 | aliguori | |
970 | 19cb3738 | bellard | #ifdef CONFIG_COCOA
|
971 | 19cb3738 | bellard | if (strstart(filename, "/dev/cdrom", NULL)) { |
972 | 19cb3738 | bellard | kern_return_t kernResult; |
973 | 19cb3738 | bellard | io_iterator_t mediaIterator; |
974 | 19cb3738 | bellard | char bsdPath[ MAXPATHLEN ];
|
975 | 19cb3738 | bellard | int fd;
|
976 | 5fafdf24 | ths | |
977 | 19cb3738 | bellard | kernResult = FindEjectableCDMedia( &mediaIterator ); |
978 | 19cb3738 | bellard | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
|
979 | 3b46e624 | ths | |
980 | 19cb3738 | bellard | if ( bsdPath[ 0 ] != '\0' ) { |
981 | 19cb3738 | bellard | strcat(bsdPath,"s0");
|
982 | 19cb3738 | bellard | /* some CDs don't have a partition 0 */
|
983 | 19cb3738 | bellard | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); |
984 | 19cb3738 | bellard | if (fd < 0) { |
985 | 19cb3738 | bellard | bsdPath[strlen(bsdPath)-1] = '1'; |
986 | 19cb3738 | bellard | } else {
|
987 | 19cb3738 | bellard | close(fd); |
988 | 19cb3738 | bellard | } |
989 | 19cb3738 | bellard | filename = bsdPath; |
990 | 19cb3738 | bellard | } |
991 | 3b46e624 | ths | |
992 | 19cb3738 | bellard | if ( mediaIterator )
|
993 | 19cb3738 | bellard | IOObjectRelease( mediaIterator ); |
994 | 19cb3738 | bellard | } |
995 | 19cb3738 | bellard | #endif
|
996 | 19cb3738 | bellard | open_flags = O_BINARY; |
997 | 19cb3738 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
998 | 19cb3738 | bellard | open_flags |= O_RDWR; |
999 | 19cb3738 | bellard | } else {
|
1000 | 19cb3738 | bellard | open_flags |= O_RDONLY; |
1001 | 19cb3738 | bellard | bs->read_only = 1;
|
1002 | 19cb3738 | bellard | } |
1003 | 9f7965c7 | aliguori | /* Use O_DSYNC for write-through caching, no flags for write-back caching,
|
1004 | 9f7965c7 | aliguori | * and O_DIRECT for no caching. */
|
1005 | 9f7965c7 | aliguori | if ((flags & BDRV_O_NOCACHE))
|
1006 | 33f00271 | balrog | open_flags |= O_DIRECT; |
1007 | 9f7965c7 | aliguori | else if (!(flags & BDRV_O_CACHE_WB)) |
1008 | 9f7965c7 | aliguori | open_flags |= O_DSYNC; |
1009 | 19cb3738 | bellard | |
1010 | 19cb3738 | bellard | s->type = FTYPE_FILE; |
1011 | 19cb3738 | bellard | #if defined(__linux__)
|
1012 | 19cb3738 | bellard | if (strstart(filename, "/dev/cd", NULL)) { |
1013 | 19cb3738 | bellard | /* open will not fail even if no CD is inserted */
|
1014 | 19cb3738 | bellard | open_flags |= O_NONBLOCK; |
1015 | 19cb3738 | bellard | s->type = FTYPE_CD; |
1016 | 19cb3738 | bellard | } else if (strstart(filename, "/dev/fd", NULL)) { |
1017 | 19cb3738 | bellard | s->type = FTYPE_FD; |
1018 | 6dd2db52 | blueswir1 | s->fd_open_flags = open_flags; |
1019 | 19cb3738 | bellard | /* open will not fail even if no floppy is inserted */
|
1020 | 19cb3738 | bellard | open_flags |= O_NONBLOCK; |
1021 | 985a03b0 | ths | } else if (strstart(filename, "/dev/sg", NULL)) { |
1022 | 985a03b0 | ths | bs->sg = 1;
|
1023 | 19cb3738 | bellard | } |
1024 | 19cb3738 | bellard | #endif
|
1025 | 9f23011a | blueswir1 | #if defined(__FreeBSD__)
|
1026 | 9f23011a | blueswir1 | if (strstart(filename, "/dev/cd", NULL) || |
1027 | 9f23011a | blueswir1 | strstart(filename, "/dev/acd", NULL)) { |
1028 | 9f23011a | blueswir1 | s->type = FTYPE_CD; |
1029 | 9f23011a | blueswir1 | s->cd_open_flags = open_flags; |
1030 | 9f23011a | blueswir1 | } |
1031 | 9f23011a | blueswir1 | #endif
|
1032 | 9f23011a | blueswir1 | s->fd = -1;
|
1033 | 19cb3738 | bellard | fd = open(filename, open_flags, 0644);
|
1034 | 19cb3738 | bellard | if (fd < 0) { |
1035 | 19cb3738 | bellard | ret = -errno; |
1036 | 19cb3738 | bellard | if (ret == -EROFS)
|
1037 | 19cb3738 | bellard | ret = -EACCES; |
1038 | 19cb3738 | bellard | return ret;
|
1039 | 19cb3738 | bellard | } |
1040 | 19cb3738 | bellard | s->fd = fd; |
1041 | 9f23011a | blueswir1 | #if defined(__FreeBSD__)
|
1042 | 9f23011a | blueswir1 | /* make sure the door isnt locked at this time */
|
1043 | 9f23011a | blueswir1 | if (s->type == FTYPE_CD)
|
1044 | 9f23011a | blueswir1 | ioctl (s->fd, CDIOCALLOW); |
1045 | 9f23011a | blueswir1 | #endif
|
1046 | 19cb3738 | bellard | #if defined(__linux__)
|
1047 | 19cb3738 | bellard | /* close fd so that we can reopen it as needed */
|
1048 | 19cb3738 | bellard | if (s->type == FTYPE_FD) {
|
1049 | 19cb3738 | bellard | close(s->fd); |
1050 | 19cb3738 | bellard | s->fd = -1;
|
1051 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
1052 | 19cb3738 | bellard | } |
1053 | 19cb3738 | bellard | #endif
|
1054 | 19cb3738 | bellard | return 0; |
1055 | 19cb3738 | bellard | } |
1056 | 19cb3738 | bellard | |
1057 | 03ff3ca3 | aliguori | #if defined(__linux__)
|
1058 | 19cb3738 | bellard | /* Note: we do not have a reliable method to detect if the floppy is
|
1059 | 19cb3738 | bellard | present. The current method is to try to open the floppy at every
|
1060 | 19cb3738 | bellard | I/O and to keep it opened during a few hundreds of ms. */
|
1061 | 19cb3738 | bellard | static int fd_open(BlockDriverState *bs) |
1062 | 19cb3738 | bellard | { |
1063 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1064 | 19cb3738 | bellard | int last_media_present;
|
1065 | 19cb3738 | bellard | |
1066 | 19cb3738 | bellard | if (s->type != FTYPE_FD)
|
1067 | 19cb3738 | bellard | return 0; |
1068 | 19cb3738 | bellard | last_media_present = (s->fd >= 0);
|
1069 | 5fafdf24 | ths | if (s->fd >= 0 && |
1070 | 19cb3738 | bellard | (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
1071 | 19cb3738 | bellard | close(s->fd); |
1072 | 19cb3738 | bellard | s->fd = -1;
|
1073 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1074 | 19cb3738 | bellard | printf("Floppy closed\n");
|
1075 | 19cb3738 | bellard | #endif
|
1076 | 19cb3738 | bellard | } |
1077 | 19cb3738 | bellard | if (s->fd < 0) { |
1078 | 5fafdf24 | ths | if (s->fd_got_error &&
|
1079 | 19cb3738 | bellard | (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
1080 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1081 | 19cb3738 | bellard | printf("No floppy (open delayed)\n");
|
1082 | 19cb3738 | bellard | #endif
|
1083 | 19cb3738 | bellard | return -EIO;
|
1084 | 19cb3738 | bellard | } |
1085 | 6dd2db52 | blueswir1 | s->fd = open(bs->filename, s->fd_open_flags); |
1086 | 19cb3738 | bellard | if (s->fd < 0) { |
1087 | 19cb3738 | bellard | s->fd_error_time = qemu_get_clock(rt_clock); |
1088 | 19cb3738 | bellard | s->fd_got_error = 1;
|
1089 | 19cb3738 | bellard | if (last_media_present)
|
1090 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
1091 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1092 | 19cb3738 | bellard | printf("No floppy\n");
|
1093 | 19cb3738 | bellard | #endif
|
1094 | 19cb3738 | bellard | return -EIO;
|
1095 | 19cb3738 | bellard | } |
1096 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1097 | 19cb3738 | bellard | printf("Floppy opened\n");
|
1098 | 19cb3738 | bellard | #endif
|
1099 | 19cb3738 | bellard | } |
1100 | 19cb3738 | bellard | if (!last_media_present)
|
1101 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
1102 | 19cb3738 | bellard | s->fd_open_time = qemu_get_clock(rt_clock); |
1103 | 19cb3738 | bellard | s->fd_got_error = 0;
|
1104 | 19cb3738 | bellard | return 0; |
1105 | 19cb3738 | bellard | } |
1106 | 19cb3738 | bellard | |
1107 | 19cb3738 | bellard | static int raw_is_inserted(BlockDriverState *bs) |
1108 | 19cb3738 | bellard | { |
1109 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1110 | 19cb3738 | bellard | int ret;
|
1111 | 19cb3738 | bellard | |
1112 | 19cb3738 | bellard | switch(s->type) {
|
1113 | 19cb3738 | bellard | case FTYPE_CD:
|
1114 | 19cb3738 | bellard | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); |
1115 | 19cb3738 | bellard | if (ret == CDS_DISC_OK)
|
1116 | 19cb3738 | bellard | return 1; |
1117 | 19cb3738 | bellard | else
|
1118 | 19cb3738 | bellard | return 0; |
1119 | 19cb3738 | bellard | break;
|
1120 | 19cb3738 | bellard | case FTYPE_FD:
|
1121 | 19cb3738 | bellard | ret = fd_open(bs); |
1122 | 19cb3738 | bellard | return (ret >= 0); |
1123 | 19cb3738 | bellard | default:
|
1124 | 19cb3738 | bellard | return 1; |
1125 | 19cb3738 | bellard | } |
1126 | 19cb3738 | bellard | } |
1127 | 19cb3738 | bellard | |
1128 | 19cb3738 | bellard | /* currently only used by fdc.c, but a CD version would be good too */
|
1129 | 19cb3738 | bellard | static int raw_media_changed(BlockDriverState *bs) |
1130 | 19cb3738 | bellard | { |
1131 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1132 | 19cb3738 | bellard | |
1133 | 19cb3738 | bellard | switch(s->type) {
|
1134 | 19cb3738 | bellard | case FTYPE_FD:
|
1135 | 19cb3738 | bellard | { |
1136 | 19cb3738 | bellard | int ret;
|
1137 | 19cb3738 | bellard | /* XXX: we do not have a true media changed indication. It
|
1138 | 19cb3738 | bellard | does not work if the floppy is changed without trying
|
1139 | 19cb3738 | bellard | to read it */
|
1140 | 19cb3738 | bellard | fd_open(bs); |
1141 | 19cb3738 | bellard | ret = s->fd_media_changed; |
1142 | 19cb3738 | bellard | s->fd_media_changed = 0;
|
1143 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
1144 | 19cb3738 | bellard | printf("Floppy changed=%d\n", ret);
|
1145 | 19cb3738 | bellard | #endif
|
1146 | 19cb3738 | bellard | return ret;
|
1147 | 19cb3738 | bellard | } |
1148 | 19cb3738 | bellard | default:
|
1149 | 19cb3738 | bellard | return -ENOTSUP;
|
1150 | 19cb3738 | bellard | } |
1151 | 19cb3738 | bellard | } |
1152 | 19cb3738 | bellard | |
1153 | 19cb3738 | bellard | static int raw_eject(BlockDriverState *bs, int eject_flag) |
1154 | 19cb3738 | bellard | { |
1155 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1156 | 19cb3738 | bellard | |
1157 | 19cb3738 | bellard | switch(s->type) {
|
1158 | 19cb3738 | bellard | case FTYPE_CD:
|
1159 | 19cb3738 | bellard | if (eject_flag) {
|
1160 | 19cb3738 | bellard | if (ioctl (s->fd, CDROMEJECT, NULL) < 0) |
1161 | 19cb3738 | bellard | perror("CDROMEJECT");
|
1162 | 19cb3738 | bellard | } else {
|
1163 | 19cb3738 | bellard | if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0) |
1164 | 19cb3738 | bellard | perror("CDROMEJECT");
|
1165 | 19cb3738 | bellard | } |
1166 | 19cb3738 | bellard | break;
|
1167 | 19cb3738 | bellard | case FTYPE_FD:
|
1168 | 19cb3738 | bellard | { |
1169 | 19cb3738 | bellard | int fd;
|
1170 | 19cb3738 | bellard | if (s->fd >= 0) { |
1171 | 19cb3738 | bellard | close(s->fd); |
1172 | 19cb3738 | bellard | s->fd = -1;
|
1173 | 19cb3738 | bellard | } |
1174 | 6dd2db52 | blueswir1 | fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK); |
1175 | 19cb3738 | bellard | if (fd >= 0) { |
1176 | 19cb3738 | bellard | if (ioctl(fd, FDEJECT, 0) < 0) |
1177 | 19cb3738 | bellard | perror("FDEJECT");
|
1178 | 19cb3738 | bellard | close(fd); |
1179 | 19cb3738 | bellard | } |
1180 | 19cb3738 | bellard | } |
1181 | 19cb3738 | bellard | break;
|
1182 | 19cb3738 | bellard | default:
|
1183 | 19cb3738 | bellard | return -ENOTSUP;
|
1184 | 19cb3738 | bellard | } |
1185 | 19cb3738 | bellard | return 0; |
1186 | 19cb3738 | bellard | } |
1187 | 19cb3738 | bellard | |
1188 | 19cb3738 | bellard | static int raw_set_locked(BlockDriverState *bs, int locked) |
1189 | 19cb3738 | bellard | { |
1190 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1191 | 19cb3738 | bellard | |
1192 | 19cb3738 | bellard | switch(s->type) {
|
1193 | 19cb3738 | bellard | case FTYPE_CD:
|
1194 | 19cb3738 | bellard | if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) { |
1195 | 19cb3738 | bellard | /* Note: an error can happen if the distribution automatically
|
1196 | 19cb3738 | bellard | mounts the CD-ROM */
|
1197 | 19cb3738 | bellard | // perror("CDROM_LOCKDOOR");
|
1198 | 19cb3738 | bellard | } |
1199 | 19cb3738 | bellard | break;
|
1200 | 19cb3738 | bellard | default:
|
1201 | 19cb3738 | bellard | return -ENOTSUP;
|
1202 | 19cb3738 | bellard | } |
1203 | 19cb3738 | bellard | return 0; |
1204 | 19cb3738 | bellard | } |
1205 | 19cb3738 | bellard | |
1206 | 985a03b0 | ths | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1207 | 985a03b0 | ths | { |
1208 | 985a03b0 | ths | BDRVRawState *s = bs->opaque; |
1209 | 985a03b0 | ths | |
1210 | 985a03b0 | ths | return ioctl(s->fd, req, buf);
|
1211 | 985a03b0 | ths | } |
1212 | 221f715d | aliguori | |
1213 | 221f715d | aliguori | static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
|
1214 | 221f715d | aliguori | unsigned long int req, void *buf, |
1215 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1216 | 221f715d | aliguori | { |
1217 | 221f715d | aliguori | RawAIOCB *acb; |
1218 | 221f715d | aliguori | |
1219 | 221f715d | aliguori | acb = raw_aio_setup(bs, 0, buf, 0, cb, opaque); |
1220 | 221f715d | aliguori | if (!acb)
|
1221 | 221f715d | aliguori | return NULL; |
1222 | 221f715d | aliguori | |
1223 | 221f715d | aliguori | acb->aiocb.aio_ioctl_cmd = req; |
1224 | 221f715d | aliguori | if (qemu_paio_ioctl(&acb->aiocb) < 0) { |
1225 | 221f715d | aliguori | raw_aio_remove(acb); |
1226 | 221f715d | aliguori | return NULL; |
1227 | 221f715d | aliguori | } |
1228 | 221f715d | aliguori | |
1229 | 221f715d | aliguori | return &acb->common;
|
1230 | 221f715d | aliguori | } |
1231 | 221f715d | aliguori | |
1232 | 9f23011a | blueswir1 | #elif defined(__FreeBSD__)
|
1233 | 9f23011a | blueswir1 | |
1234 | 9f23011a | blueswir1 | static int fd_open(BlockDriverState *bs) |
1235 | 9f23011a | blueswir1 | { |
1236 | 9f23011a | blueswir1 | BDRVRawState *s = bs->opaque; |
1237 | 9f23011a | blueswir1 | |
1238 | 9f23011a | blueswir1 | /* this is just to ensure s->fd is sane (its called by io ops) */
|
1239 | 9f23011a | blueswir1 | if (s->fd >= 0) |
1240 | 9f23011a | blueswir1 | return 0; |
1241 | 9f23011a | blueswir1 | return -EIO;
|
1242 | 9f23011a | blueswir1 | } |
1243 | 9f23011a | blueswir1 | |
1244 | 9f23011a | blueswir1 | static int cd_open(BlockDriverState *bs) |
1245 | 9f23011a | blueswir1 | { |
1246 | 9f23011a | blueswir1 | #if defined(__FreeBSD__)
|
1247 | 9f23011a | blueswir1 | BDRVRawState *s = bs->opaque; |
1248 | 9f23011a | blueswir1 | int fd;
|
1249 | 9f23011a | blueswir1 | |
1250 | 9f23011a | blueswir1 | switch(s->type) {
|
1251 | 9f23011a | blueswir1 | case FTYPE_CD:
|
1252 | 9f23011a | blueswir1 | /* XXX force reread of possibly changed/newly loaded disc,
|
1253 | 9f23011a | blueswir1 | * FreeBSD seems to not notice sometimes... */
|
1254 | 9f23011a | blueswir1 | if (s->fd >= 0) |
1255 | 9f23011a | blueswir1 | close (s->fd); |
1256 | 9f23011a | blueswir1 | fd = open(bs->filename, s->cd_open_flags, 0644);
|
1257 | 9f23011a | blueswir1 | if (fd < 0) { |
1258 | 9f23011a | blueswir1 | s->fd = -1;
|
1259 | 9f23011a | blueswir1 | return -EIO;
|
1260 | 9f23011a | blueswir1 | } |
1261 | 9f23011a | blueswir1 | s->fd = fd; |
1262 | 9f23011a | blueswir1 | /* make sure the door isnt locked at this time */
|
1263 | 9f23011a | blueswir1 | ioctl (s->fd, CDIOCALLOW); |
1264 | 9f23011a | blueswir1 | } |
1265 | 9f23011a | blueswir1 | #endif
|
1266 | 9f23011a | blueswir1 | return 0; |
1267 | 9f23011a | blueswir1 | } |
1268 | 9f23011a | blueswir1 | |
1269 | 9f23011a | blueswir1 | static int raw_is_inserted(BlockDriverState *bs) |
1270 | 9f23011a | blueswir1 | { |
1271 | 9f23011a | blueswir1 | BDRVRawState *s = bs->opaque; |
1272 | 9f23011a | blueswir1 | |
1273 | 9f23011a | blueswir1 | switch(s->type) {
|
1274 | 9f23011a | blueswir1 | case FTYPE_CD:
|
1275 | 9f23011a | blueswir1 | return (raw_getlength(bs) > 0); |
1276 | 9f23011a | blueswir1 | case FTYPE_FD:
|
1277 | 9f23011a | blueswir1 | /* XXX handle this */
|
1278 | 9f23011a | blueswir1 | /* FALLTHRU */
|
1279 | 9f23011a | blueswir1 | default:
|
1280 | 9f23011a | blueswir1 | return 1; |
1281 | 9f23011a | blueswir1 | } |
1282 | 9f23011a | blueswir1 | } |
1283 | 9f23011a | blueswir1 | |
1284 | 9f23011a | blueswir1 | static int raw_media_changed(BlockDriverState *bs) |
1285 | 9f23011a | blueswir1 | { |
1286 | 9f23011a | blueswir1 | return -ENOTSUP;
|
1287 | 9f23011a | blueswir1 | } |
1288 | 9f23011a | blueswir1 | |
1289 | 9f23011a | blueswir1 | static int raw_eject(BlockDriverState *bs, int eject_flag) |
1290 | 9f23011a | blueswir1 | { |
1291 | 9f23011a | blueswir1 | BDRVRawState *s = bs->opaque; |
1292 | 9f23011a | blueswir1 | |
1293 | 9f23011a | blueswir1 | switch(s->type) {
|
1294 | 9f23011a | blueswir1 | case FTYPE_CD:
|
1295 | 9f23011a | blueswir1 | if (s->fd < 0) |
1296 | 9f23011a | blueswir1 | return -ENOTSUP;
|
1297 | 9f23011a | blueswir1 | (void) ioctl (s->fd, CDIOCALLOW);
|
1298 | 9f23011a | blueswir1 | if (eject_flag) {
|
1299 | 9f23011a | blueswir1 | if (ioctl (s->fd, CDIOCEJECT) < 0) |
1300 | 9f23011a | blueswir1 | perror("CDIOCEJECT");
|
1301 | 9f23011a | blueswir1 | } else {
|
1302 | 9f23011a | blueswir1 | if (ioctl (s->fd, CDIOCCLOSE) < 0) |
1303 | 9f23011a | blueswir1 | perror("CDIOCCLOSE");
|
1304 | 9f23011a | blueswir1 | } |
1305 | 9f23011a | blueswir1 | if (cd_open(bs) < 0) |
1306 | 9f23011a | blueswir1 | return -ENOTSUP;
|
1307 | 9f23011a | blueswir1 | break;
|
1308 | 9f23011a | blueswir1 | case FTYPE_FD:
|
1309 | 9f23011a | blueswir1 | /* XXX handle this */
|
1310 | 9f23011a | blueswir1 | /* FALLTHRU */
|
1311 | 9f23011a | blueswir1 | default:
|
1312 | 9f23011a | blueswir1 | return -ENOTSUP;
|
1313 | 9f23011a | blueswir1 | } |
1314 | 9f23011a | blueswir1 | return 0; |
1315 | 9f23011a | blueswir1 | } |
1316 | 9f23011a | blueswir1 | |
1317 | 9f23011a | blueswir1 | static int raw_set_locked(BlockDriverState *bs, int locked) |
1318 | 9f23011a | blueswir1 | { |
1319 | 9f23011a | blueswir1 | BDRVRawState *s = bs->opaque; |
1320 | 9f23011a | blueswir1 | |
1321 | 9f23011a | blueswir1 | switch(s->type) {
|
1322 | 9f23011a | blueswir1 | case FTYPE_CD:
|
1323 | 9f23011a | blueswir1 | if (s->fd < 0) |
1324 | 9f23011a | blueswir1 | return -ENOTSUP;
|
1325 | 9f23011a | blueswir1 | if (ioctl (s->fd, (locked ? CDIOCPREVENT : CDIOCALLOW)) < 0) { |
1326 | 9f23011a | blueswir1 | /* Note: an error can happen if the distribution automatically
|
1327 | 9f23011a | blueswir1 | mounts the CD-ROM */
|
1328 | 9f23011a | blueswir1 | // perror("CDROM_LOCKDOOR");
|
1329 | 9f23011a | blueswir1 | } |
1330 | 9f23011a | blueswir1 | break;
|
1331 | 9f23011a | blueswir1 | default:
|
1332 | 9f23011a | blueswir1 | return -ENOTSUP;
|
1333 | 9f23011a | blueswir1 | } |
1334 | 9f23011a | blueswir1 | return 0; |
1335 | 9f23011a | blueswir1 | } |
1336 | 9f23011a | blueswir1 | |
1337 | 9f23011a | blueswir1 | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1338 | 9f23011a | blueswir1 | { |
1339 | 9f23011a | blueswir1 | return -ENOTSUP;
|
1340 | 9f23011a | blueswir1 | } |
1341 | 9f23011a | blueswir1 | #else /* !linux && !FreeBSD */ |
1342 | 19cb3738 | bellard | |
1343 | 08af02e2 | aliguori | static int fd_open(BlockDriverState *bs) |
1344 | 08af02e2 | aliguori | { |
1345 | 08af02e2 | aliguori | return 0; |
1346 | 08af02e2 | aliguori | } |
1347 | 08af02e2 | aliguori | |
1348 | 19cb3738 | bellard | static int raw_is_inserted(BlockDriverState *bs) |
1349 | 19cb3738 | bellard | { |
1350 | 19cb3738 | bellard | return 1; |
1351 | 19cb3738 | bellard | } |
1352 | 19cb3738 | bellard | |
1353 | 19cb3738 | bellard | static int raw_media_changed(BlockDriverState *bs) |
1354 | 19cb3738 | bellard | { |
1355 | 19cb3738 | bellard | return -ENOTSUP;
|
1356 | 19cb3738 | bellard | } |
1357 | 19cb3738 | bellard | |
1358 | 19cb3738 | bellard | static int raw_eject(BlockDriverState *bs, int eject_flag) |
1359 | 19cb3738 | bellard | { |
1360 | 19cb3738 | bellard | return -ENOTSUP;
|
1361 | 19cb3738 | bellard | } |
1362 | 19cb3738 | bellard | |
1363 | 19cb3738 | bellard | static int raw_set_locked(BlockDriverState *bs, int locked) |
1364 | 19cb3738 | bellard | { |
1365 | 19cb3738 | bellard | return -ENOTSUP;
|
1366 | 19cb3738 | bellard | } |
1367 | 19cb3738 | bellard | |
1368 | 985a03b0 | ths | static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) |
1369 | 985a03b0 | ths | { |
1370 | 985a03b0 | ths | return -ENOTSUP;
|
1371 | 985a03b0 | ths | } |
1372 | 04eeb8b6 | aliguori | |
1373 | 221f715d | aliguori | static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
|
1374 | 221f715d | aliguori | unsigned long int req, void *buf, |
1375 | 221f715d | aliguori | BlockDriverCompletionFunc *cb, void *opaque)
|
1376 | 04eeb8b6 | aliguori | { |
1377 | bbeea539 | blueswir1 | return NULL; |
1378 | 04eeb8b6 | aliguori | } |
1379 | 221f715d | aliguori | #endif /* !linux && !FreeBSD */ |
1380 | 04eeb8b6 | aliguori | |
1381 | 19cb3738 | bellard | BlockDriver bdrv_host_device = { |
1382 | e60f469c | aurel32 | .format_name = "host_device",
|
1383 | e60f469c | aurel32 | .instance_size = sizeof(BDRVRawState),
|
1384 | e60f469c | aurel32 | .bdrv_open = hdev_open, |
1385 | e60f469c | aurel32 | .bdrv_close = raw_close, |
1386 | e60f469c | aurel32 | .bdrv_flush = raw_flush, |
1387 | 3b46e624 | ths | |
1388 | 414f0dab | blueswir1 | #ifdef CONFIG_AIO
|
1389 | e60f469c | aurel32 | .bdrv_aio_read = raw_aio_read, |
1390 | e60f469c | aurel32 | .bdrv_aio_write = raw_aio_write, |
1391 | e60f469c | aurel32 | .bdrv_aio_cancel = raw_aio_cancel, |
1392 | e60f469c | aurel32 | .aiocb_size = sizeof(RawAIOCB),
|
1393 | 414f0dab | blueswir1 | #endif
|
1394 | 3c529d93 | aliguori | |
1395 | eda578e5 | aliguori | .bdrv_read = raw_read, |
1396 | eda578e5 | aliguori | .bdrv_write = raw_write, |
1397 | e60f469c | aurel32 | .bdrv_getlength = raw_getlength, |
1398 | 19cb3738 | bellard | |
1399 | 19cb3738 | bellard | /* removable device support */
|
1400 | e60f469c | aurel32 | .bdrv_is_inserted = raw_is_inserted, |
1401 | e60f469c | aurel32 | .bdrv_media_changed = raw_media_changed, |
1402 | e60f469c | aurel32 | .bdrv_eject = raw_eject, |
1403 | e60f469c | aurel32 | .bdrv_set_locked = raw_set_locked, |
1404 | 985a03b0 | ths | /* generic scsi device */
|
1405 | e60f469c | aurel32 | .bdrv_ioctl = raw_ioctl, |
1406 | 221f715d | aliguori | .bdrv_aio_ioctl = raw_aio_ioctl, |
1407 | 19cb3738 | bellard | }; |