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