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