root / block-raw.c @ 5fafdf24
History | View | Annotate | Download (35 kB)
1 | 83f64091 | bellard | /*
|
---|---|---|---|
2 | 83f64091 | bellard | * Block driver for RAW files
|
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 | 83f64091 | bellard | #include "vl.h" |
25 | 83f64091 | bellard | #include "block_int.h" |
26 | 83f64091 | bellard | #include <assert.h> |
27 | 83f64091 | bellard | #ifndef _WIN32
|
28 | 83f64091 | bellard | #include <aio.h> |
29 | 83f64091 | bellard | |
30 | 83f64091 | bellard | #ifndef QEMU_TOOL
|
31 | 83f64091 | bellard | #include "exec-all.h" |
32 | 83f64091 | bellard | #endif
|
33 | 83f64091 | bellard | |
34 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
35 | 83f64091 | bellard | #include <paths.h> |
36 | 83f64091 | bellard | #include <sys/param.h> |
37 | 83f64091 | bellard | #include <IOKit/IOKitLib.h> |
38 | 83f64091 | bellard | #include <IOKit/IOBSD.h> |
39 | 83f64091 | bellard | #include <IOKit/storage/IOMediaBSDClient.h> |
40 | 83f64091 | bellard | #include <IOKit/storage/IOMedia.h> |
41 | 83f64091 | bellard | #include <IOKit/storage/IOCDMedia.h> |
42 | 83f64091 | bellard | //#include <IOKit/storage/IOCDTypes.h>
|
43 | 83f64091 | bellard | #include <CoreFoundation/CoreFoundation.h> |
44 | 83f64091 | bellard | #endif
|
45 | 83f64091 | bellard | |
46 | 83f64091 | bellard | #ifdef __sun__
|
47 | 2e9671da | ths | #define _POSIX_PTHREAD_SEMANTICS 1 |
48 | 2e9671da | ths | #include <signal.h> |
49 | 83f64091 | bellard | #include <sys/dkio.h> |
50 | 83f64091 | bellard | #endif
|
51 | 19cb3738 | bellard | #ifdef __linux__
|
52 | 19cb3738 | bellard | #include <sys/ioctl.h> |
53 | 19cb3738 | bellard | #include <linux/cdrom.h> |
54 | 19cb3738 | bellard | #include <linux/fd.h> |
55 | 19cb3738 | bellard | #endif
|
56 | 1cb6c3fd | ths | #ifdef __FreeBSD__
|
57 | 1cb6c3fd | ths | #include <sys/disk.h> |
58 | 1cb6c3fd | ths | #endif
|
59 | 83f64091 | bellard | |
60 | 19cb3738 | bellard | //#define DEBUG_FLOPPY
|
61 | 83f64091 | bellard | |
62 | 8c05dbf9 | ths | #define DEBUG_BLOCK
|
63 | 8c05dbf9 | ths | #if defined(DEBUG_BLOCK) && !defined(QEMU_TOOL)
|
64 | 8c05dbf9 | ths | #define DEBUG_BLOCK_PRINT(formatCstr, args...) fprintf(logfile, formatCstr, ##args); fflush(logfile) |
65 | 8c05dbf9 | ths | #else
|
66 | 8c05dbf9 | ths | #define DEBUG_BLOCK_PRINT(formatCstr, args...)
|
67 | 8c05dbf9 | ths | #endif
|
68 | 8c05dbf9 | ths | |
69 | 19cb3738 | bellard | #define FTYPE_FILE 0 |
70 | 19cb3738 | bellard | #define FTYPE_CD 1 |
71 | 19cb3738 | bellard | #define FTYPE_FD 2 |
72 | 83f64091 | bellard | |
73 | 19cb3738 | bellard | /* if the FD is not accessed during that time (in ms), we try to
|
74 | 19cb3738 | bellard | reopen it to see if the disk has been changed */
|
75 | 19cb3738 | bellard | #define FD_OPEN_TIMEOUT 1000 |
76 | 83f64091 | bellard | |
77 | 19cb3738 | bellard | typedef struct BDRVRawState { |
78 | 19cb3738 | bellard | int fd;
|
79 | 19cb3738 | bellard | int type;
|
80 | 8c05dbf9 | ths | unsigned int lseek_err_cnt; |
81 | 19cb3738 | bellard | #if defined(__linux__)
|
82 | 19cb3738 | bellard | /* linux floppy specific */
|
83 | 19cb3738 | bellard | int fd_open_flags;
|
84 | 19cb3738 | bellard | int64_t fd_open_time; |
85 | 19cb3738 | bellard | int64_t fd_error_time; |
86 | 19cb3738 | bellard | int fd_got_error;
|
87 | 19cb3738 | bellard | int fd_media_changed;
|
88 | 83f64091 | bellard | #endif
|
89 | 19cb3738 | bellard | } BDRVRawState; |
90 | 19cb3738 | bellard | |
91 | 19cb3738 | bellard | static int fd_open(BlockDriverState *bs); |
92 | 83f64091 | bellard | |
93 | 83f64091 | bellard | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
94 | 83f64091 | bellard | { |
95 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
96 | 19cb3738 | bellard | int fd, open_flags, ret;
|
97 | 83f64091 | bellard | |
98 | 8c05dbf9 | ths | s->lseek_err_cnt = 0;
|
99 | 8c05dbf9 | ths | |
100 | 83f64091 | bellard | open_flags = O_BINARY; |
101 | 83f64091 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
102 | 83f64091 | bellard | open_flags |= O_RDWR; |
103 | 83f64091 | bellard | } else {
|
104 | 83f64091 | bellard | open_flags |= O_RDONLY; |
105 | 83f64091 | bellard | bs->read_only = 1;
|
106 | 83f64091 | bellard | } |
107 | 83f64091 | bellard | if (flags & BDRV_O_CREAT)
|
108 | 83f64091 | bellard | open_flags |= O_CREAT | O_TRUNC; |
109 | 83f64091 | bellard | |
110 | 19cb3738 | bellard | s->type = FTYPE_FILE; |
111 | 19cb3738 | bellard | |
112 | 83f64091 | bellard | fd = open(filename, open_flags, 0644);
|
113 | 19cb3738 | bellard | if (fd < 0) { |
114 | 19cb3738 | bellard | ret = -errno; |
115 | 19cb3738 | bellard | if (ret == -EROFS)
|
116 | 19cb3738 | bellard | ret = -EACCES; |
117 | 19cb3738 | bellard | return ret;
|
118 | 19cb3738 | bellard | } |
119 | 83f64091 | bellard | s->fd = fd; |
120 | 83f64091 | bellard | return 0; |
121 | 83f64091 | bellard | } |
122 | 83f64091 | bellard | |
123 | 83f64091 | bellard | /* XXX: use host sector size if necessary with:
|
124 | 83f64091 | bellard | #ifdef DIOCGSECTORSIZE
|
125 | 83f64091 | bellard | {
|
126 | 83f64091 | bellard | unsigned int sectorsize = 512;
|
127 | 83f64091 | bellard | if (!ioctl(fd, DIOCGSECTORSIZE, §orsize) &&
|
128 | 83f64091 | bellard | sectorsize > bufsize)
|
129 | 83f64091 | bellard | bufsize = sectorsize;
|
130 | 83f64091 | bellard | }
|
131 | 83f64091 | bellard | #endif
|
132 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
133 | 83f64091 | bellard | u_int32_t blockSize = 512;
|
134 | 83f64091 | bellard | if ( !ioctl( fd, DKIOCGETBLOCKSIZE, &blockSize ) && blockSize > bufsize) {
|
135 | 83f64091 | bellard | bufsize = blockSize;
|
136 | 83f64091 | bellard | }
|
137 | 83f64091 | bellard | #endif
|
138 | 83f64091 | bellard | */
|
139 | 83f64091 | bellard | |
140 | 5fafdf24 | ths | static int raw_pread(BlockDriverState *bs, int64_t offset, |
141 | 83f64091 | bellard | uint8_t *buf, int count)
|
142 | 83f64091 | bellard | { |
143 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
144 | 83f64091 | bellard | int ret;
|
145 | 5fafdf24 | ths | |
146 | 19cb3738 | bellard | ret = fd_open(bs); |
147 | 19cb3738 | bellard | if (ret < 0) |
148 | 19cb3738 | bellard | return ret;
|
149 | 19cb3738 | bellard | |
150 | 8c05dbf9 | ths | if (lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
151 | 8c05dbf9 | ths | ++(s->lseek_err_cnt); |
152 | 8c05dbf9 | ths | if(s->lseek_err_cnt <= 10) { |
153 | 8c05dbf9 | ths | DEBUG_BLOCK_PRINT("raw_pread(%d:%s, %lld, %p, %d) [%lld] lseek failed : %d = %s\n",
|
154 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
155 | 8c05dbf9 | ths | bs->total_sectors, errno, strerror(errno)); |
156 | 8c05dbf9 | ths | } |
157 | 8c05dbf9 | ths | return -1; |
158 | 8c05dbf9 | ths | } |
159 | 8c05dbf9 | ths | s->lseek_err_cnt=0;
|
160 | 8c05dbf9 | ths | |
161 | 83f64091 | bellard | ret = read(s->fd, buf, count); |
162 | 8c05dbf9 | ths | if (ret == count)
|
163 | 8c05dbf9 | ths | goto label__raw_read__success;
|
164 | 8c05dbf9 | ths | |
165 | 8c05dbf9 | ths | DEBUG_BLOCK_PRINT("raw_read(%d:%s, %lld, %p, %d) [%lld] read failed %d : %d = %s\n",
|
166 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
167 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
168 | 8c05dbf9 | ths | |
169 | 8c05dbf9 | ths | /* Try harder for CDrom. */
|
170 | 8c05dbf9 | ths | if (bs->type == BDRV_TYPE_CDROM) {
|
171 | 8c05dbf9 | ths | lseek(s->fd, offset, SEEK_SET); |
172 | 8c05dbf9 | ths | ret = read(s->fd, buf, count); |
173 | 8c05dbf9 | ths | if (ret == count)
|
174 | 8c05dbf9 | ths | goto label__raw_read__success;
|
175 | 8c05dbf9 | ths | lseek(s->fd, offset, SEEK_SET); |
176 | 8c05dbf9 | ths | ret = read(s->fd, buf, count); |
177 | 8c05dbf9 | ths | if (ret == count)
|
178 | 8c05dbf9 | ths | goto label__raw_read__success;
|
179 | 8c05dbf9 | ths | |
180 | 8c05dbf9 | ths | DEBUG_BLOCK_PRINT("raw_read(%d:%s, %lld, %p, %d) [%lld] retry read failed %d : %d = %s\n",
|
181 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
182 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
183 | 8c05dbf9 | ths | } |
184 | 8c05dbf9 | ths | |
185 | 8c05dbf9 | ths | return -1; |
186 | 8c05dbf9 | ths | |
187 | 8c05dbf9 | ths | label__raw_read__success:
|
188 | 8c05dbf9 | ths | |
189 | 83f64091 | bellard | return ret;
|
190 | 83f64091 | bellard | } |
191 | 83f64091 | bellard | |
192 | 5fafdf24 | ths | static int raw_pwrite(BlockDriverState *bs, int64_t offset, |
193 | 83f64091 | bellard | const uint8_t *buf, int count) |
194 | 83f64091 | bellard | { |
195 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
196 | 83f64091 | bellard | int ret;
|
197 | 5fafdf24 | ths | |
198 | 19cb3738 | bellard | ret = fd_open(bs); |
199 | 19cb3738 | bellard | if (ret < 0) |
200 | 19cb3738 | bellard | return ret;
|
201 | 19cb3738 | bellard | |
202 | 8c05dbf9 | ths | if (lseek(s->fd, offset, SEEK_SET) == (off_t)-1) { |
203 | 8c05dbf9 | ths | ++(s->lseek_err_cnt); |
204 | 8c05dbf9 | ths | if(s->lseek_err_cnt) {
|
205 | 8c05dbf9 | ths | DEBUG_BLOCK_PRINT("raw_write(%d:%s, %lld, %p, %d) [%lld] lseek failed : %d = %s\n",
|
206 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
207 | 8c05dbf9 | ths | bs->total_sectors, errno, strerror(errno)); |
208 | 8c05dbf9 | ths | } |
209 | 8c05dbf9 | ths | return -1; |
210 | 8c05dbf9 | ths | } |
211 | 8c05dbf9 | ths | s->lseek_err_cnt = 0;
|
212 | 8c05dbf9 | ths | |
213 | 83f64091 | bellard | ret = write(s->fd, buf, count); |
214 | 8c05dbf9 | ths | if (ret == count)
|
215 | 8c05dbf9 | ths | goto label__raw_write__success;
|
216 | 8c05dbf9 | ths | |
217 | 8c05dbf9 | ths | DEBUG_BLOCK_PRINT("raw_write(%d:%s, %lld, %p, %d) [%lld] write failed %d : %d = %s\n",
|
218 | 8c05dbf9 | ths | s->fd, bs->filename, offset, buf, count, |
219 | 8c05dbf9 | ths | bs->total_sectors, ret, errno, strerror(errno)); |
220 | 8c05dbf9 | ths | |
221 | 8c05dbf9 | ths | return -1; |
222 | 8c05dbf9 | ths | |
223 | 8c05dbf9 | ths | label__raw_write__success:
|
224 | 8c05dbf9 | ths | |
225 | 83f64091 | bellard | return ret;
|
226 | 83f64091 | bellard | } |
227 | 83f64091 | bellard | |
228 | 83f64091 | bellard | /***********************************************************/
|
229 | 19cb3738 | bellard | /* Unix AIO using POSIX AIO */
|
230 | 83f64091 | bellard | |
231 | 83f64091 | bellard | typedef struct RawAIOCB { |
232 | ce1a14dc | pbrook | BlockDriverAIOCB common; |
233 | 83f64091 | bellard | struct aiocb aiocb;
|
234 | ce1a14dc | pbrook | struct RawAIOCB *next;
|
235 | 83f64091 | bellard | } RawAIOCB; |
236 | 83f64091 | bellard | |
237 | 83f64091 | bellard | static int aio_sig_num = SIGUSR2; |
238 | ce1a14dc | pbrook | static RawAIOCB *first_aio; /* AIO issued */ |
239 | 979b67ad | bellard | static int aio_initialized = 0; |
240 | 83f64091 | bellard | |
241 | 83f64091 | bellard | static void aio_signal_handler(int signum) |
242 | 83f64091 | bellard | { |
243 | 979b67ad | bellard | #ifndef QEMU_TOOL
|
244 | 83f64091 | bellard | CPUState *env = cpu_single_env; |
245 | 83f64091 | bellard | if (env) {
|
246 | 83f64091 | bellard | /* stop the currently executing cpu because a timer occured */
|
247 | 83f64091 | bellard | cpu_interrupt(env, CPU_INTERRUPT_EXIT); |
248 | 83f64091 | bellard | #ifdef USE_KQEMU
|
249 | 83f64091 | bellard | if (env->kqemu_enabled) {
|
250 | 83f64091 | bellard | kqemu_cpu_interrupt(env); |
251 | 83f64091 | bellard | } |
252 | 83f64091 | bellard | #endif
|
253 | 83f64091 | bellard | } |
254 | 979b67ad | bellard | #endif
|
255 | 83f64091 | bellard | } |
256 | 83f64091 | bellard | |
257 | 83f64091 | bellard | void qemu_aio_init(void) |
258 | 83f64091 | bellard | { |
259 | 83f64091 | bellard | struct sigaction act;
|
260 | 979b67ad | bellard | |
261 | 979b67ad | bellard | aio_initialized = 1;
|
262 | 5fafdf24 | ths | |
263 | 83f64091 | bellard | sigfillset(&act.sa_mask); |
264 | 83f64091 | bellard | act.sa_flags = 0; /* do not restart syscalls to interrupt select() */ |
265 | 83f64091 | bellard | act.sa_handler = aio_signal_handler; |
266 | 83f64091 | bellard | sigaction(aio_sig_num, &act, NULL);
|
267 | 83f64091 | bellard | |
268 | 19cb3738 | bellard | #if defined(__GLIBC__) && defined(__linux__)
|
269 | 83f64091 | bellard | { |
270 | 19cb3738 | bellard | /* XXX: aio thread exit seems to hang on RedHat 9 and this init
|
271 | 19cb3738 | bellard | seems to fix the problem. */
|
272 | 83f64091 | bellard | struct aioinit ai;
|
273 | 83f64091 | bellard | memset(&ai, 0, sizeof(ai)); |
274 | 19cb3738 | bellard | ai.aio_threads = 1;
|
275 | 83f64091 | bellard | ai.aio_num = 1;
|
276 | 83f64091 | bellard | ai.aio_idle_time = 365 * 100000; |
277 | 83f64091 | bellard | aio_init(&ai); |
278 | 83f64091 | bellard | } |
279 | 19cb3738 | bellard | #endif
|
280 | 83f64091 | bellard | } |
281 | 83f64091 | bellard | |
282 | 83f64091 | bellard | void qemu_aio_poll(void) |
283 | 83f64091 | bellard | { |
284 | ce1a14dc | pbrook | RawAIOCB *acb, **pacb; |
285 | 83f64091 | bellard | int ret;
|
286 | 83f64091 | bellard | |
287 | 83f64091 | bellard | for(;;) {
|
288 | 83f64091 | bellard | pacb = &first_aio; |
289 | 83f64091 | bellard | for(;;) {
|
290 | 83f64091 | bellard | acb = *pacb; |
291 | 83f64091 | bellard | if (!acb)
|
292 | 83f64091 | bellard | goto the_end;
|
293 | ce1a14dc | pbrook | ret = aio_error(&acb->aiocb); |
294 | 83f64091 | bellard | if (ret == ECANCELED) {
|
295 | 83f64091 | bellard | /* remove the request */
|
296 | ce1a14dc | pbrook | *pacb = acb->next; |
297 | ce1a14dc | pbrook | qemu_aio_release(acb); |
298 | 83f64091 | bellard | } else if (ret != EINPROGRESS) { |
299 | 83f64091 | bellard | /* end of aio */
|
300 | 83f64091 | bellard | if (ret == 0) { |
301 | ce1a14dc | pbrook | ret = aio_return(&acb->aiocb); |
302 | ce1a14dc | pbrook | if (ret == acb->aiocb.aio_nbytes)
|
303 | 83f64091 | bellard | ret = 0;
|
304 | 83f64091 | bellard | else
|
305 | 19cb3738 | bellard | ret = -EINVAL; |
306 | 83f64091 | bellard | } else {
|
307 | 83f64091 | bellard | ret = -ret; |
308 | 83f64091 | bellard | } |
309 | 83f64091 | bellard | /* remove the request */
|
310 | ce1a14dc | pbrook | *pacb = acb->next; |
311 | 83f64091 | bellard | /* call the callback */
|
312 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, ret); |
313 | ce1a14dc | pbrook | qemu_aio_release(acb); |
314 | 83f64091 | bellard | break;
|
315 | 83f64091 | bellard | } else {
|
316 | ce1a14dc | pbrook | pacb = &acb->next; |
317 | 83f64091 | bellard | } |
318 | 83f64091 | bellard | } |
319 | 83f64091 | bellard | } |
320 | 83f64091 | bellard | the_end: ;
|
321 | 83f64091 | bellard | } |
322 | 83f64091 | bellard | |
323 | 6192bc37 | pbrook | /* Wait for all IO requests to complete. */
|
324 | 6192bc37 | pbrook | void qemu_aio_flush(void) |
325 | 6192bc37 | pbrook | { |
326 | 6192bc37 | pbrook | qemu_aio_wait_start(); |
327 | 6192bc37 | pbrook | qemu_aio_poll(); |
328 | 6192bc37 | pbrook | while (first_aio) {
|
329 | 6192bc37 | pbrook | qemu_aio_wait(); |
330 | 6192bc37 | pbrook | } |
331 | 6192bc37 | pbrook | qemu_aio_wait_end(); |
332 | 6192bc37 | pbrook | } |
333 | 6192bc37 | pbrook | |
334 | 83f64091 | bellard | /* wait until at least one AIO was handled */
|
335 | 83f64091 | bellard | static sigset_t wait_oset;
|
336 | 83f64091 | bellard | |
337 | 83f64091 | bellard | void qemu_aio_wait_start(void) |
338 | 83f64091 | bellard | { |
339 | 83f64091 | bellard | sigset_t set; |
340 | 979b67ad | bellard | |
341 | 979b67ad | bellard | if (!aio_initialized)
|
342 | 979b67ad | bellard | qemu_aio_init(); |
343 | 83f64091 | bellard | sigemptyset(&set); |
344 | 83f64091 | bellard | sigaddset(&set, aio_sig_num); |
345 | 83f64091 | bellard | sigprocmask(SIG_BLOCK, &set, &wait_oset); |
346 | 83f64091 | bellard | } |
347 | 83f64091 | bellard | |
348 | 83f64091 | bellard | void qemu_aio_wait(void) |
349 | 83f64091 | bellard | { |
350 | 83f64091 | bellard | sigset_t set; |
351 | 83f64091 | bellard | int nb_sigs;
|
352 | 6eb5733a | bellard | |
353 | 6eb5733a | bellard | #ifndef QEMU_TOOL
|
354 | 6eb5733a | bellard | if (qemu_bh_poll())
|
355 | 6eb5733a | bellard | return;
|
356 | 6eb5733a | bellard | #endif
|
357 | 83f64091 | bellard | sigemptyset(&set); |
358 | 83f64091 | bellard | sigaddset(&set, aio_sig_num); |
359 | 83f64091 | bellard | sigwait(&set, &nb_sigs); |
360 | 83f64091 | bellard | qemu_aio_poll(); |
361 | 83f64091 | bellard | } |
362 | 83f64091 | bellard | |
363 | 83f64091 | bellard | void qemu_aio_wait_end(void) |
364 | 83f64091 | bellard | { |
365 | 83f64091 | bellard | sigprocmask(SIG_SETMASK, &wait_oset, NULL);
|
366 | 83f64091 | bellard | } |
367 | 83f64091 | bellard | |
368 | ce1a14dc | pbrook | static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
|
369 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
370 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
371 | 83f64091 | bellard | { |
372 | ce1a14dc | pbrook | BDRVRawState *s = bs->opaque; |
373 | ce1a14dc | pbrook | RawAIOCB *acb; |
374 | ce1a14dc | pbrook | |
375 | 19cb3738 | bellard | if (fd_open(bs) < 0) |
376 | 19cb3738 | bellard | return NULL; |
377 | 19cb3738 | bellard | |
378 | ce1a14dc | pbrook | acb = qemu_aio_get(bs, cb, opaque); |
379 | ce1a14dc | pbrook | if (!acb)
|
380 | ce1a14dc | pbrook | return NULL; |
381 | ce1a14dc | pbrook | acb->aiocb.aio_fildes = s->fd; |
382 | ce1a14dc | pbrook | acb->aiocb.aio_sigevent.sigev_signo = aio_sig_num; |
383 | ce1a14dc | pbrook | acb->aiocb.aio_sigevent.sigev_notify = SIGEV_SIGNAL; |
384 | ce1a14dc | pbrook | acb->aiocb.aio_buf = buf; |
385 | ce1a14dc | pbrook | acb->aiocb.aio_nbytes = nb_sectors * 512;
|
386 | ce1a14dc | pbrook | acb->aiocb.aio_offset = sector_num * 512;
|
387 | ce1a14dc | pbrook | acb->next = first_aio; |
388 | ce1a14dc | pbrook | first_aio = acb; |
389 | ce1a14dc | pbrook | return acb;
|
390 | 83f64091 | bellard | } |
391 | 83f64091 | bellard | |
392 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
|
393 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
394 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
395 | 83f64091 | bellard | { |
396 | ce1a14dc | pbrook | RawAIOCB *acb; |
397 | 83f64091 | bellard | |
398 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
399 | ce1a14dc | pbrook | if (!acb)
|
400 | ce1a14dc | pbrook | return NULL; |
401 | ce1a14dc | pbrook | if (aio_read(&acb->aiocb) < 0) { |
402 | ce1a14dc | pbrook | qemu_aio_release(acb); |
403 | ce1a14dc | pbrook | return NULL; |
404 | 5fafdf24 | ths | } |
405 | ce1a14dc | pbrook | return &acb->common;
|
406 | 83f64091 | bellard | } |
407 | 83f64091 | bellard | |
408 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
|
409 | ce1a14dc | pbrook | int64_t sector_num, const uint8_t *buf, int nb_sectors, |
410 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
411 | 83f64091 | bellard | { |
412 | ce1a14dc | pbrook | RawAIOCB *acb; |
413 | 83f64091 | bellard | |
414 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, (uint8_t*)buf, nb_sectors, cb, opaque); |
415 | ce1a14dc | pbrook | if (!acb)
|
416 | ce1a14dc | pbrook | return NULL; |
417 | ce1a14dc | pbrook | if (aio_write(&acb->aiocb) < 0) { |
418 | ce1a14dc | pbrook | qemu_aio_release(acb); |
419 | ce1a14dc | pbrook | return NULL; |
420 | 5fafdf24 | ths | } |
421 | ce1a14dc | pbrook | return &acb->common;
|
422 | 83f64091 | bellard | } |
423 | 83f64091 | bellard | |
424 | ce1a14dc | pbrook | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) |
425 | 83f64091 | bellard | { |
426 | 83f64091 | bellard | int ret;
|
427 | ce1a14dc | pbrook | RawAIOCB *acb = (RawAIOCB *)blockacb; |
428 | ce1a14dc | pbrook | RawAIOCB **pacb; |
429 | 83f64091 | bellard | |
430 | ce1a14dc | pbrook | ret = aio_cancel(acb->aiocb.aio_fildes, &acb->aiocb); |
431 | 83f64091 | bellard | if (ret == AIO_NOTCANCELED) {
|
432 | 83f64091 | bellard | /* fail safe: if the aio could not be canceled, we wait for
|
433 | 83f64091 | bellard | it */
|
434 | ce1a14dc | pbrook | while (aio_error(&acb->aiocb) == EINPROGRESS);
|
435 | 83f64091 | bellard | } |
436 | 83f64091 | bellard | |
437 | 83f64091 | bellard | /* remove the callback from the queue */
|
438 | 83f64091 | bellard | pacb = &first_aio; |
439 | 83f64091 | bellard | for(;;) {
|
440 | 83f64091 | bellard | if (*pacb == NULL) { |
441 | 83f64091 | bellard | break;
|
442 | 83f64091 | bellard | } else if (*pacb == acb) { |
443 | ce1a14dc | pbrook | *pacb = acb->next; |
444 | ce1a14dc | pbrook | qemu_aio_release(acb); |
445 | 83f64091 | bellard | break;
|
446 | 83f64091 | bellard | } |
447 | ce1a14dc | pbrook | pacb = &acb->next; |
448 | 83f64091 | bellard | } |
449 | 83f64091 | bellard | } |
450 | 83f64091 | bellard | |
451 | 83f64091 | bellard | static void raw_close(BlockDriverState *bs) |
452 | 83f64091 | bellard | { |
453 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
454 | 19cb3738 | bellard | if (s->fd >= 0) { |
455 | 19cb3738 | bellard | close(s->fd); |
456 | 19cb3738 | bellard | s->fd = -1;
|
457 | 19cb3738 | bellard | } |
458 | 83f64091 | bellard | } |
459 | 83f64091 | bellard | |
460 | 83f64091 | bellard | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
461 | 83f64091 | bellard | { |
462 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
463 | 19cb3738 | bellard | if (s->type != FTYPE_FILE)
|
464 | 19cb3738 | bellard | return -ENOTSUP;
|
465 | 83f64091 | bellard | if (ftruncate(s->fd, offset) < 0) |
466 | 83f64091 | bellard | return -errno;
|
467 | 83f64091 | bellard | return 0; |
468 | 83f64091 | bellard | } |
469 | 83f64091 | bellard | |
470 | 83f64091 | bellard | static int64_t raw_getlength(BlockDriverState *bs)
|
471 | 83f64091 | bellard | { |
472 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
473 | 83f64091 | bellard | int fd = s->fd;
|
474 | 83f64091 | bellard | int64_t size; |
475 | 83f64091 | bellard | #ifdef _BSD
|
476 | 83f64091 | bellard | struct stat sb;
|
477 | 83f64091 | bellard | #endif
|
478 | 83f64091 | bellard | #ifdef __sun__
|
479 | 83f64091 | bellard | struct dk_minfo minfo;
|
480 | 83f64091 | bellard | int rv;
|
481 | 83f64091 | bellard | #endif
|
482 | 19cb3738 | bellard | int ret;
|
483 | 19cb3738 | bellard | |
484 | 19cb3738 | bellard | ret = fd_open(bs); |
485 | 19cb3738 | bellard | if (ret < 0) |
486 | 19cb3738 | bellard | return ret;
|
487 | 83f64091 | bellard | |
488 | 83f64091 | bellard | #ifdef _BSD
|
489 | 83f64091 | bellard | if (!fstat(fd, &sb) && (S_IFCHR & sb.st_mode)) {
|
490 | 83f64091 | bellard | #ifdef DIOCGMEDIASIZE
|
491 | 83f64091 | bellard | if (ioctl(fd, DIOCGMEDIASIZE, (off_t *)&size))
|
492 | 83f64091 | bellard | #endif
|
493 | 83f64091 | bellard | #ifdef CONFIG_COCOA
|
494 | 83f64091 | bellard | size = LONG_LONG_MAX; |
495 | 83f64091 | bellard | #else
|
496 | 83f64091 | bellard | size = lseek(fd, 0LL, SEEK_END);
|
497 | 83f64091 | bellard | #endif
|
498 | 83f64091 | bellard | } else
|
499 | 83f64091 | bellard | #endif
|
500 | 83f64091 | bellard | #ifdef __sun__
|
501 | 83f64091 | bellard | /*
|
502 | 83f64091 | bellard | * use the DKIOCGMEDIAINFO ioctl to read the size.
|
503 | 83f64091 | bellard | */
|
504 | 83f64091 | bellard | rv = ioctl ( fd, DKIOCGMEDIAINFO, &minfo ); |
505 | 83f64091 | bellard | if ( rv != -1 ) { |
506 | 83f64091 | bellard | size = minfo.dki_lbsize * minfo.dki_capacity; |
507 | 83f64091 | bellard | } else /* there are reports that lseek on some devices |
508 | 83f64091 | bellard | fails, but irc discussion said that contingency
|
509 | 83f64091 | bellard | on contingency was overkill */
|
510 | 83f64091 | bellard | #endif
|
511 | 83f64091 | bellard | { |
512 | 83f64091 | bellard | size = lseek(fd, 0, SEEK_END);
|
513 | 83f64091 | bellard | } |
514 | 83f64091 | bellard | return size;
|
515 | 83f64091 | bellard | } |
516 | 83f64091 | bellard | |
517 | 83f64091 | bellard | static int raw_create(const char *filename, int64_t total_size, |
518 | 83f64091 | bellard | const char *backing_file, int flags) |
519 | 83f64091 | bellard | { |
520 | 83f64091 | bellard | int fd;
|
521 | 83f64091 | bellard | |
522 | 83f64091 | bellard | if (flags || backing_file)
|
523 | 83f64091 | bellard | return -ENOTSUP;
|
524 | 83f64091 | bellard | |
525 | 5fafdf24 | ths | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
526 | 83f64091 | bellard | 0644);
|
527 | 83f64091 | bellard | if (fd < 0) |
528 | 83f64091 | bellard | return -EIO;
|
529 | 83f64091 | bellard | ftruncate(fd, total_size * 512);
|
530 | 83f64091 | bellard | close(fd); |
531 | 83f64091 | bellard | return 0; |
532 | 83f64091 | bellard | } |
533 | 83f64091 | bellard | |
534 | 83f64091 | bellard | static void raw_flush(BlockDriverState *bs) |
535 | 83f64091 | bellard | { |
536 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
537 | 83f64091 | bellard | fsync(s->fd); |
538 | 83f64091 | bellard | } |
539 | 83f64091 | bellard | |
540 | 83f64091 | bellard | BlockDriver bdrv_raw = { |
541 | 83f64091 | bellard | "raw",
|
542 | 83f64091 | bellard | sizeof(BDRVRawState),
|
543 | 83f64091 | bellard | NULL, /* no probe for protocols */ |
544 | 83f64091 | bellard | raw_open, |
545 | 83f64091 | bellard | NULL,
|
546 | 83f64091 | bellard | NULL,
|
547 | 83f64091 | bellard | raw_close, |
548 | 83f64091 | bellard | raw_create, |
549 | 83f64091 | bellard | raw_flush, |
550 | 5fafdf24 | ths | |
551 | 83f64091 | bellard | .bdrv_aio_read = raw_aio_read, |
552 | 83f64091 | bellard | .bdrv_aio_write = raw_aio_write, |
553 | 83f64091 | bellard | .bdrv_aio_cancel = raw_aio_cancel, |
554 | ce1a14dc | pbrook | .aiocb_size = sizeof(RawAIOCB),
|
555 | 83f64091 | bellard | .protocol_name = "file",
|
556 | 83f64091 | bellard | .bdrv_pread = raw_pread, |
557 | 83f64091 | bellard | .bdrv_pwrite = raw_pwrite, |
558 | 83f64091 | bellard | .bdrv_truncate = raw_truncate, |
559 | 83f64091 | bellard | .bdrv_getlength = raw_getlength, |
560 | 83f64091 | bellard | }; |
561 | 83f64091 | bellard | |
562 | 19cb3738 | bellard | /***********************************************/
|
563 | 19cb3738 | bellard | /* host device */
|
564 | 19cb3738 | bellard | |
565 | 19cb3738 | bellard | #ifdef CONFIG_COCOA
|
566 | 19cb3738 | bellard | static kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator );
|
567 | 19cb3738 | bellard | static kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize ); |
568 | 19cb3738 | bellard | |
569 | 19cb3738 | bellard | kern_return_t FindEjectableCDMedia( io_iterator_t *mediaIterator ) |
570 | 19cb3738 | bellard | { |
571 | 5fafdf24 | ths | kern_return_t kernResult; |
572 | 19cb3738 | bellard | mach_port_t masterPort; |
573 | 19cb3738 | bellard | CFMutableDictionaryRef classesToMatch; |
574 | 19cb3738 | bellard | |
575 | 19cb3738 | bellard | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); |
576 | 19cb3738 | bellard | if ( KERN_SUCCESS != kernResult ) {
|
577 | 19cb3738 | bellard | printf( "IOMasterPort returned %d\n", kernResult );
|
578 | 19cb3738 | bellard | } |
579 | 5fafdf24 | ths | |
580 | 5fafdf24 | ths | classesToMatch = IOServiceMatching( kIOCDMediaClass ); |
581 | 19cb3738 | bellard | if ( classesToMatch == NULL ) { |
582 | 19cb3738 | bellard | printf( "IOServiceMatching returned a NULL dictionary.\n" );
|
583 | 19cb3738 | bellard | } else {
|
584 | 19cb3738 | bellard | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaEjectableKey ), kCFBooleanTrue ); |
585 | 19cb3738 | bellard | } |
586 | 19cb3738 | bellard | kernResult = IOServiceGetMatchingServices( masterPort, classesToMatch, mediaIterator ); |
587 | 19cb3738 | bellard | if ( KERN_SUCCESS != kernResult )
|
588 | 19cb3738 | bellard | { |
589 | 19cb3738 | bellard | printf( "IOServiceGetMatchingServices returned %d\n", kernResult );
|
590 | 19cb3738 | bellard | } |
591 | 5fafdf24 | ths | |
592 | 19cb3738 | bellard | return kernResult;
|
593 | 19cb3738 | bellard | } |
594 | 19cb3738 | bellard | |
595 | 19cb3738 | bellard | kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex maxPathSize )
|
596 | 19cb3738 | bellard | { |
597 | 19cb3738 | bellard | io_object_t nextMedia; |
598 | 19cb3738 | bellard | kern_return_t kernResult = KERN_FAILURE; |
599 | 19cb3738 | bellard | *bsdPath = '\0';
|
600 | 19cb3738 | bellard | nextMedia = IOIteratorNext( mediaIterator ); |
601 | 19cb3738 | bellard | if ( nextMedia )
|
602 | 19cb3738 | bellard | { |
603 | 19cb3738 | bellard | CFTypeRef bsdPathAsCFString; |
604 | 19cb3738 | bellard | bsdPathAsCFString = IORegistryEntryCreateCFProperty( nextMedia, CFSTR( kIOBSDNameKey ), kCFAllocatorDefault, 0 );
|
605 | 19cb3738 | bellard | if ( bsdPathAsCFString ) {
|
606 | 19cb3738 | bellard | size_t devPathLength; |
607 | 19cb3738 | bellard | strcpy( bsdPath, _PATH_DEV ); |
608 | 19cb3738 | bellard | strcat( bsdPath, "r" );
|
609 | 19cb3738 | bellard | devPathLength = strlen( bsdPath ); |
610 | 19cb3738 | bellard | if ( CFStringGetCString( bsdPathAsCFString, bsdPath + devPathLength, maxPathSize - devPathLength, kCFStringEncodingASCII ) ) {
|
611 | 19cb3738 | bellard | kernResult = KERN_SUCCESS; |
612 | 19cb3738 | bellard | } |
613 | 19cb3738 | bellard | CFRelease( bsdPathAsCFString ); |
614 | 19cb3738 | bellard | } |
615 | 19cb3738 | bellard | IOObjectRelease( nextMedia ); |
616 | 19cb3738 | bellard | } |
617 | 5fafdf24 | ths | |
618 | 19cb3738 | bellard | return kernResult;
|
619 | 19cb3738 | bellard | } |
620 | 19cb3738 | bellard | |
621 | 19cb3738 | bellard | #endif
|
622 | 19cb3738 | bellard | |
623 | 19cb3738 | bellard | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
624 | 19cb3738 | bellard | { |
625 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
626 | 19cb3738 | bellard | int fd, open_flags, ret;
|
627 | 19cb3738 | bellard | |
628 | 19cb3738 | bellard | #ifdef CONFIG_COCOA
|
629 | 19cb3738 | bellard | if (strstart(filename, "/dev/cdrom", NULL)) { |
630 | 19cb3738 | bellard | kern_return_t kernResult; |
631 | 19cb3738 | bellard | io_iterator_t mediaIterator; |
632 | 19cb3738 | bellard | char bsdPath[ MAXPATHLEN ];
|
633 | 19cb3738 | bellard | int fd;
|
634 | 5fafdf24 | ths | |
635 | 19cb3738 | bellard | kernResult = FindEjectableCDMedia( &mediaIterator ); |
636 | 19cb3738 | bellard | kernResult = GetBSDPath( mediaIterator, bsdPath, sizeof( bsdPath ) );
|
637 | 5fafdf24 | ths | |
638 | 19cb3738 | bellard | if ( bsdPath[ 0 ] != '\0' ) { |
639 | 19cb3738 | bellard | strcat(bsdPath,"s0");
|
640 | 19cb3738 | bellard | /* some CDs don't have a partition 0 */
|
641 | 19cb3738 | bellard | fd = open(bsdPath, O_RDONLY | O_BINARY | O_LARGEFILE); |
642 | 19cb3738 | bellard | if (fd < 0) { |
643 | 19cb3738 | bellard | bsdPath[strlen(bsdPath)-1] = '1'; |
644 | 19cb3738 | bellard | } else {
|
645 | 19cb3738 | bellard | close(fd); |
646 | 19cb3738 | bellard | } |
647 | 19cb3738 | bellard | filename = bsdPath; |
648 | 19cb3738 | bellard | } |
649 | 5fafdf24 | ths | |
650 | 19cb3738 | bellard | if ( mediaIterator )
|
651 | 19cb3738 | bellard | IOObjectRelease( mediaIterator ); |
652 | 19cb3738 | bellard | } |
653 | 19cb3738 | bellard | #endif
|
654 | 19cb3738 | bellard | open_flags = O_BINARY; |
655 | 19cb3738 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
656 | 19cb3738 | bellard | open_flags |= O_RDWR; |
657 | 19cb3738 | bellard | } else {
|
658 | 19cb3738 | bellard | open_flags |= O_RDONLY; |
659 | 19cb3738 | bellard | bs->read_only = 1;
|
660 | 19cb3738 | bellard | } |
661 | 19cb3738 | bellard | |
662 | 19cb3738 | bellard | s->type = FTYPE_FILE; |
663 | 19cb3738 | bellard | #if defined(__linux__)
|
664 | 19cb3738 | bellard | if (strstart(filename, "/dev/cd", NULL)) { |
665 | 19cb3738 | bellard | /* open will not fail even if no CD is inserted */
|
666 | 19cb3738 | bellard | open_flags |= O_NONBLOCK; |
667 | 19cb3738 | bellard | s->type = FTYPE_CD; |
668 | 19cb3738 | bellard | } else if (strstart(filename, "/dev/fd", NULL)) { |
669 | 19cb3738 | bellard | s->type = FTYPE_FD; |
670 | 19cb3738 | bellard | s->fd_open_flags = open_flags; |
671 | 19cb3738 | bellard | /* open will not fail even if no floppy is inserted */
|
672 | 19cb3738 | bellard | open_flags |= O_NONBLOCK; |
673 | 19cb3738 | bellard | } |
674 | 19cb3738 | bellard | #endif
|
675 | 19cb3738 | bellard | fd = open(filename, open_flags, 0644);
|
676 | 19cb3738 | bellard | if (fd < 0) { |
677 | 19cb3738 | bellard | ret = -errno; |
678 | 19cb3738 | bellard | if (ret == -EROFS)
|
679 | 19cb3738 | bellard | ret = -EACCES; |
680 | 19cb3738 | bellard | return ret;
|
681 | 19cb3738 | bellard | } |
682 | 19cb3738 | bellard | s->fd = fd; |
683 | 19cb3738 | bellard | #if defined(__linux__)
|
684 | 19cb3738 | bellard | /* close fd so that we can reopen it as needed */
|
685 | 19cb3738 | bellard | if (s->type == FTYPE_FD) {
|
686 | 19cb3738 | bellard | close(s->fd); |
687 | 19cb3738 | bellard | s->fd = -1;
|
688 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
689 | 19cb3738 | bellard | } |
690 | 19cb3738 | bellard | #endif
|
691 | 19cb3738 | bellard | return 0; |
692 | 19cb3738 | bellard | } |
693 | 19cb3738 | bellard | |
694 | 19cb3738 | bellard | #if defined(__linux__) && !defined(QEMU_TOOL)
|
695 | 19cb3738 | bellard | |
696 | 19cb3738 | bellard | /* Note: we do not have a reliable method to detect if the floppy is
|
697 | 19cb3738 | bellard | present. The current method is to try to open the floppy at every
|
698 | 19cb3738 | bellard | I/O and to keep it opened during a few hundreds of ms. */
|
699 | 19cb3738 | bellard | static int fd_open(BlockDriverState *bs) |
700 | 19cb3738 | bellard | { |
701 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
702 | 19cb3738 | bellard | int last_media_present;
|
703 | 19cb3738 | bellard | |
704 | 19cb3738 | bellard | if (s->type != FTYPE_FD)
|
705 | 19cb3738 | bellard | return 0; |
706 | 19cb3738 | bellard | last_media_present = (s->fd >= 0);
|
707 | 5fafdf24 | ths | if (s->fd >= 0 && |
708 | 19cb3738 | bellard | (qemu_get_clock(rt_clock) - s->fd_open_time) >= FD_OPEN_TIMEOUT) { |
709 | 19cb3738 | bellard | close(s->fd); |
710 | 19cb3738 | bellard | s->fd = -1;
|
711 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
712 | 19cb3738 | bellard | printf("Floppy closed\n");
|
713 | 19cb3738 | bellard | #endif
|
714 | 19cb3738 | bellard | } |
715 | 19cb3738 | bellard | if (s->fd < 0) { |
716 | 5fafdf24 | ths | if (s->fd_got_error &&
|
717 | 19cb3738 | bellard | (qemu_get_clock(rt_clock) - s->fd_error_time) < FD_OPEN_TIMEOUT) { |
718 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
719 | 19cb3738 | bellard | printf("No floppy (open delayed)\n");
|
720 | 19cb3738 | bellard | #endif
|
721 | 19cb3738 | bellard | return -EIO;
|
722 | 19cb3738 | bellard | } |
723 | 19cb3738 | bellard | s->fd = open(bs->filename, s->fd_open_flags); |
724 | 19cb3738 | bellard | if (s->fd < 0) { |
725 | 19cb3738 | bellard | s->fd_error_time = qemu_get_clock(rt_clock); |
726 | 19cb3738 | bellard | s->fd_got_error = 1;
|
727 | 19cb3738 | bellard | if (last_media_present)
|
728 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
729 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
730 | 19cb3738 | bellard | printf("No floppy\n");
|
731 | 19cb3738 | bellard | #endif
|
732 | 19cb3738 | bellard | return -EIO;
|
733 | 19cb3738 | bellard | } |
734 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
735 | 19cb3738 | bellard | printf("Floppy opened\n");
|
736 | 19cb3738 | bellard | #endif
|
737 | 19cb3738 | bellard | } |
738 | 19cb3738 | bellard | if (!last_media_present)
|
739 | 19cb3738 | bellard | s->fd_media_changed = 1;
|
740 | 19cb3738 | bellard | s->fd_open_time = qemu_get_clock(rt_clock); |
741 | 19cb3738 | bellard | s->fd_got_error = 0;
|
742 | 19cb3738 | bellard | return 0; |
743 | 19cb3738 | bellard | } |
744 | 19cb3738 | bellard | #else
|
745 | 19cb3738 | bellard | static int fd_open(BlockDriverState *bs) |
746 | 19cb3738 | bellard | { |
747 | 19cb3738 | bellard | return 0; |
748 | 19cb3738 | bellard | } |
749 | 19cb3738 | bellard | #endif
|
750 | 19cb3738 | bellard | |
751 | 19cb3738 | bellard | #if defined(__linux__)
|
752 | 19cb3738 | bellard | |
753 | 19cb3738 | bellard | static int raw_is_inserted(BlockDriverState *bs) |
754 | 19cb3738 | bellard | { |
755 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
756 | 19cb3738 | bellard | int ret;
|
757 | 19cb3738 | bellard | |
758 | 19cb3738 | bellard | switch(s->type) {
|
759 | 19cb3738 | bellard | case FTYPE_CD:
|
760 | 19cb3738 | bellard | ret = ioctl(s->fd, CDROM_DRIVE_STATUS, CDSL_CURRENT); |
761 | 19cb3738 | bellard | if (ret == CDS_DISC_OK)
|
762 | 19cb3738 | bellard | return 1; |
763 | 19cb3738 | bellard | else
|
764 | 19cb3738 | bellard | return 0; |
765 | 19cb3738 | bellard | break;
|
766 | 19cb3738 | bellard | case FTYPE_FD:
|
767 | 19cb3738 | bellard | ret = fd_open(bs); |
768 | 19cb3738 | bellard | return (ret >= 0); |
769 | 19cb3738 | bellard | default:
|
770 | 19cb3738 | bellard | return 1; |
771 | 19cb3738 | bellard | } |
772 | 19cb3738 | bellard | } |
773 | 19cb3738 | bellard | |
774 | 19cb3738 | bellard | /* currently only used by fdc.c, but a CD version would be good too */
|
775 | 19cb3738 | bellard | static int raw_media_changed(BlockDriverState *bs) |
776 | 19cb3738 | bellard | { |
777 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
778 | 19cb3738 | bellard | |
779 | 19cb3738 | bellard | switch(s->type) {
|
780 | 19cb3738 | bellard | case FTYPE_FD:
|
781 | 19cb3738 | bellard | { |
782 | 19cb3738 | bellard | int ret;
|
783 | 19cb3738 | bellard | /* XXX: we do not have a true media changed indication. It
|
784 | 19cb3738 | bellard | does not work if the floppy is changed without trying
|
785 | 19cb3738 | bellard | to read it */
|
786 | 19cb3738 | bellard | fd_open(bs); |
787 | 19cb3738 | bellard | ret = s->fd_media_changed; |
788 | 19cb3738 | bellard | s->fd_media_changed = 0;
|
789 | 19cb3738 | bellard | #ifdef DEBUG_FLOPPY
|
790 | 19cb3738 | bellard | printf("Floppy changed=%d\n", ret);
|
791 | 19cb3738 | bellard | #endif
|
792 | 19cb3738 | bellard | return ret;
|
793 | 19cb3738 | bellard | } |
794 | 19cb3738 | bellard | default:
|
795 | 19cb3738 | bellard | return -ENOTSUP;
|
796 | 19cb3738 | bellard | } |
797 | 19cb3738 | bellard | } |
798 | 19cb3738 | bellard | |
799 | 19cb3738 | bellard | static int raw_eject(BlockDriverState *bs, int eject_flag) |
800 | 19cb3738 | bellard | { |
801 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
802 | 19cb3738 | bellard | |
803 | 19cb3738 | bellard | switch(s->type) {
|
804 | 19cb3738 | bellard | case FTYPE_CD:
|
805 | 19cb3738 | bellard | if (eject_flag) {
|
806 | 19cb3738 | bellard | if (ioctl (s->fd, CDROMEJECT, NULL) < 0) |
807 | 19cb3738 | bellard | perror("CDROMEJECT");
|
808 | 19cb3738 | bellard | } else {
|
809 | 19cb3738 | bellard | if (ioctl (s->fd, CDROMCLOSETRAY, NULL) < 0) |
810 | 19cb3738 | bellard | perror("CDROMEJECT");
|
811 | 19cb3738 | bellard | } |
812 | 19cb3738 | bellard | break;
|
813 | 19cb3738 | bellard | case FTYPE_FD:
|
814 | 19cb3738 | bellard | { |
815 | 19cb3738 | bellard | int fd;
|
816 | 19cb3738 | bellard | if (s->fd >= 0) { |
817 | 19cb3738 | bellard | close(s->fd); |
818 | 19cb3738 | bellard | s->fd = -1;
|
819 | 19cb3738 | bellard | } |
820 | 19cb3738 | bellard | fd = open(bs->filename, s->fd_open_flags | O_NONBLOCK); |
821 | 19cb3738 | bellard | if (fd >= 0) { |
822 | 19cb3738 | bellard | if (ioctl(fd, FDEJECT, 0) < 0) |
823 | 19cb3738 | bellard | perror("FDEJECT");
|
824 | 19cb3738 | bellard | close(fd); |
825 | 19cb3738 | bellard | } |
826 | 19cb3738 | bellard | } |
827 | 19cb3738 | bellard | break;
|
828 | 19cb3738 | bellard | default:
|
829 | 19cb3738 | bellard | return -ENOTSUP;
|
830 | 19cb3738 | bellard | } |
831 | 19cb3738 | bellard | return 0; |
832 | 19cb3738 | bellard | } |
833 | 19cb3738 | bellard | |
834 | 19cb3738 | bellard | static int raw_set_locked(BlockDriverState *bs, int locked) |
835 | 19cb3738 | bellard | { |
836 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
837 | 19cb3738 | bellard | |
838 | 19cb3738 | bellard | switch(s->type) {
|
839 | 19cb3738 | bellard | case FTYPE_CD:
|
840 | 19cb3738 | bellard | if (ioctl (s->fd, CDROM_LOCKDOOR, locked) < 0) { |
841 | 19cb3738 | bellard | /* Note: an error can happen if the distribution automatically
|
842 | 19cb3738 | bellard | mounts the CD-ROM */
|
843 | 19cb3738 | bellard | // perror("CDROM_LOCKDOOR");
|
844 | 19cb3738 | bellard | } |
845 | 19cb3738 | bellard | break;
|
846 | 19cb3738 | bellard | default:
|
847 | 19cb3738 | bellard | return -ENOTSUP;
|
848 | 19cb3738 | bellard | } |
849 | 19cb3738 | bellard | return 0; |
850 | 19cb3738 | bellard | } |
851 | 19cb3738 | bellard | |
852 | 19cb3738 | bellard | #else
|
853 | 19cb3738 | bellard | |
854 | 19cb3738 | bellard | static int raw_is_inserted(BlockDriverState *bs) |
855 | 19cb3738 | bellard | { |
856 | 19cb3738 | bellard | return 1; |
857 | 19cb3738 | bellard | } |
858 | 19cb3738 | bellard | |
859 | 19cb3738 | bellard | static int raw_media_changed(BlockDriverState *bs) |
860 | 19cb3738 | bellard | { |
861 | 19cb3738 | bellard | return -ENOTSUP;
|
862 | 19cb3738 | bellard | } |
863 | 19cb3738 | bellard | |
864 | 19cb3738 | bellard | static int raw_eject(BlockDriverState *bs, int eject_flag) |
865 | 19cb3738 | bellard | { |
866 | 19cb3738 | bellard | return -ENOTSUP;
|
867 | 19cb3738 | bellard | } |
868 | 19cb3738 | bellard | |
869 | 19cb3738 | bellard | static int raw_set_locked(BlockDriverState *bs, int locked) |
870 | 19cb3738 | bellard | { |
871 | 19cb3738 | bellard | return -ENOTSUP;
|
872 | 19cb3738 | bellard | } |
873 | 19cb3738 | bellard | |
874 | 19cb3738 | bellard | #endif /* !linux */ |
875 | 19cb3738 | bellard | |
876 | 19cb3738 | bellard | BlockDriver bdrv_host_device = { |
877 | 19cb3738 | bellard | "host_device",
|
878 | 19cb3738 | bellard | sizeof(BDRVRawState),
|
879 | 19cb3738 | bellard | NULL, /* no probe for protocols */ |
880 | 19cb3738 | bellard | hdev_open, |
881 | 19cb3738 | bellard | NULL,
|
882 | 19cb3738 | bellard | NULL,
|
883 | 19cb3738 | bellard | raw_close, |
884 | 19cb3738 | bellard | NULL,
|
885 | 19cb3738 | bellard | raw_flush, |
886 | 5fafdf24 | ths | |
887 | 19cb3738 | bellard | .bdrv_aio_read = raw_aio_read, |
888 | 19cb3738 | bellard | .bdrv_aio_write = raw_aio_write, |
889 | 19cb3738 | bellard | .bdrv_aio_cancel = raw_aio_cancel, |
890 | 19cb3738 | bellard | .aiocb_size = sizeof(RawAIOCB),
|
891 | 19cb3738 | bellard | .bdrv_pread = raw_pread, |
892 | 19cb3738 | bellard | .bdrv_pwrite = raw_pwrite, |
893 | 19cb3738 | bellard | .bdrv_getlength = raw_getlength, |
894 | 19cb3738 | bellard | |
895 | 19cb3738 | bellard | /* removable device support */
|
896 | 19cb3738 | bellard | .bdrv_is_inserted = raw_is_inserted, |
897 | 19cb3738 | bellard | .bdrv_media_changed = raw_media_changed, |
898 | 19cb3738 | bellard | .bdrv_eject = raw_eject, |
899 | 19cb3738 | bellard | .bdrv_set_locked = raw_set_locked, |
900 | 19cb3738 | bellard | }; |
901 | 19cb3738 | bellard | |
902 | 83f64091 | bellard | #else /* _WIN32 */ |
903 | 83f64091 | bellard | |
904 | 83f64091 | bellard | /* XXX: use another file ? */
|
905 | 83f64091 | bellard | #include <winioctl.h> |
906 | 83f64091 | bellard | |
907 | 19cb3738 | bellard | #define FTYPE_FILE 0 |
908 | 19cb3738 | bellard | #define FTYPE_CD 1 |
909 | 01781963 | bellard | #define FTYPE_HARDDISK 2 |
910 | 19cb3738 | bellard | |
911 | 83f64091 | bellard | typedef struct BDRVRawState { |
912 | 83f64091 | bellard | HANDLE hfile; |
913 | 19cb3738 | bellard | int type;
|
914 | f45512fe | bellard | char drive_path[16]; /* format: "d:\" */ |
915 | 83f64091 | bellard | } BDRVRawState; |
916 | 83f64091 | bellard | |
917 | 83f64091 | bellard | typedef struct RawAIOCB { |
918 | ce1a14dc | pbrook | BlockDriverAIOCB common; |
919 | 83f64091 | bellard | HANDLE hEvent; |
920 | 83f64091 | bellard | OVERLAPPED ov; |
921 | 83f64091 | bellard | int count;
|
922 | 83f64091 | bellard | } RawAIOCB; |
923 | 83f64091 | bellard | |
924 | 83f64091 | bellard | int qemu_ftruncate64(int fd, int64_t length) |
925 | 83f64091 | bellard | { |
926 | 83f64091 | bellard | LARGE_INTEGER li; |
927 | 83f64091 | bellard | LONG high; |
928 | 83f64091 | bellard | HANDLE h; |
929 | 83f64091 | bellard | BOOL res; |
930 | 83f64091 | bellard | |
931 | 83f64091 | bellard | if ((GetVersion() & 0x80000000UL) && (length >> 32) != 0) |
932 | 83f64091 | bellard | return -1; |
933 | 83f64091 | bellard | |
934 | 83f64091 | bellard | h = (HANDLE)_get_osfhandle(fd); |
935 | 83f64091 | bellard | |
936 | 83f64091 | bellard | /* get current position, ftruncate do not change position */
|
937 | 83f64091 | bellard | li.HighPart = 0;
|
938 | 83f64091 | bellard | li.LowPart = SetFilePointer (h, 0, &li.HighPart, FILE_CURRENT);
|
939 | 83f64091 | bellard | if (li.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
940 | 83f64091 | bellard | return -1; |
941 | 83f64091 | bellard | |
942 | 83f64091 | bellard | high = length >> 32;
|
943 | 83f64091 | bellard | if (!SetFilePointer(h, (DWORD) length, &high, FILE_BEGIN))
|
944 | 83f64091 | bellard | return -1; |
945 | 83f64091 | bellard | res = SetEndOfFile(h); |
946 | 83f64091 | bellard | |
947 | 83f64091 | bellard | /* back to old position */
|
948 | 83f64091 | bellard | SetFilePointer(h, li.LowPart, &li.HighPart, FILE_BEGIN); |
949 | 83f64091 | bellard | return res ? 0 : -1; |
950 | 83f64091 | bellard | } |
951 | 83f64091 | bellard | |
952 | 83f64091 | bellard | static int set_sparse(int fd) |
953 | 83f64091 | bellard | { |
954 | 83f64091 | bellard | DWORD returned; |
955 | 83f64091 | bellard | return (int) DeviceIoControl((HANDLE)_get_osfhandle(fd), FSCTL_SET_SPARSE, |
956 | 83f64091 | bellard | NULL, 0, NULL, 0, &returned, NULL); |
957 | 83f64091 | bellard | } |
958 | 83f64091 | bellard | |
959 | 83f64091 | bellard | static int raw_open(BlockDriverState *bs, const char *filename, int flags) |
960 | 83f64091 | bellard | { |
961 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
962 | 83f64091 | bellard | int access_flags, create_flags;
|
963 | ce1a14dc | pbrook | DWORD overlapped; |
964 | 19cb3738 | bellard | |
965 | f45512fe | bellard | s->type = FTYPE_FILE; |
966 | 83f64091 | bellard | |
967 | 83f64091 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
968 | 83f64091 | bellard | access_flags = GENERIC_READ | GENERIC_WRITE; |
969 | 83f64091 | bellard | } else {
|
970 | 83f64091 | bellard | access_flags = GENERIC_READ; |
971 | 83f64091 | bellard | } |
972 | 979b67ad | bellard | if (flags & BDRV_O_CREAT) {
|
973 | 83f64091 | bellard | create_flags = CREATE_ALWAYS; |
974 | 83f64091 | bellard | } else {
|
975 | 83f64091 | bellard | create_flags = OPEN_EXISTING; |
976 | 83f64091 | bellard | } |
977 | ce1a14dc | pbrook | #ifdef QEMU_TOOL
|
978 | 3b9f94e1 | bellard | overlapped = FILE_ATTRIBUTE_NORMAL; |
979 | ce1a14dc | pbrook | #else
|
980 | ce1a14dc | pbrook | overlapped = FILE_FLAG_OVERLAPPED; |
981 | ce1a14dc | pbrook | #endif
|
982 | 5fafdf24 | ths | s->hfile = CreateFile(filename, access_flags, |
983 | 83f64091 | bellard | FILE_SHARE_READ, NULL,
|
984 | 3b9f94e1 | bellard | create_flags, overlapped, NULL);
|
985 | 54421cb1 | ths | if (s->hfile == INVALID_HANDLE_VALUE) {
|
986 | 54421cb1 | ths | int err = GetLastError();
|
987 | 54421cb1 | ths | |
988 | 54421cb1 | ths | if (err == ERROR_ACCESS_DENIED)
|
989 | 54421cb1 | ths | return -EACCES;
|
990 | 83f64091 | bellard | return -1; |
991 | 54421cb1 | ths | } |
992 | 83f64091 | bellard | return 0; |
993 | 83f64091 | bellard | } |
994 | 83f64091 | bellard | |
995 | 5fafdf24 | ths | static int raw_pread(BlockDriverState *bs, int64_t offset, |
996 | 83f64091 | bellard | uint8_t *buf, int count)
|
997 | 83f64091 | bellard | { |
998 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
999 | 83f64091 | bellard | OVERLAPPED ov; |
1000 | 83f64091 | bellard | DWORD ret_count; |
1001 | 83f64091 | bellard | int ret;
|
1002 | 5fafdf24 | ths | |
1003 | 83f64091 | bellard | memset(&ov, 0, sizeof(ov)); |
1004 | 83f64091 | bellard | ov.Offset = offset; |
1005 | 83f64091 | bellard | ov.OffsetHigh = offset >> 32;
|
1006 | 436e15b8 | bellard | ret = ReadFile(s->hfile, buf, count, &ret_count, &ov); |
1007 | 436e15b8 | bellard | if (!ret) {
|
1008 | 436e15b8 | bellard | ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); |
1009 | 436e15b8 | bellard | if (!ret)
|
1010 | 436e15b8 | bellard | return -EIO;
|
1011 | 436e15b8 | bellard | else
|
1012 | 436e15b8 | bellard | return ret_count;
|
1013 | 436e15b8 | bellard | } |
1014 | 83f64091 | bellard | return ret_count;
|
1015 | 83f64091 | bellard | } |
1016 | 83f64091 | bellard | |
1017 | 5fafdf24 | ths | static int raw_pwrite(BlockDriverState *bs, int64_t offset, |
1018 | 83f64091 | bellard | const uint8_t *buf, int count) |
1019 | 83f64091 | bellard | { |
1020 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
1021 | 83f64091 | bellard | OVERLAPPED ov; |
1022 | 83f64091 | bellard | DWORD ret_count; |
1023 | 83f64091 | bellard | int ret;
|
1024 | 5fafdf24 | ths | |
1025 | 83f64091 | bellard | memset(&ov, 0, sizeof(ov)); |
1026 | 83f64091 | bellard | ov.Offset = offset; |
1027 | 83f64091 | bellard | ov.OffsetHigh = offset >> 32;
|
1028 | 436e15b8 | bellard | ret = WriteFile(s->hfile, buf, count, &ret_count, &ov); |
1029 | 436e15b8 | bellard | if (!ret) {
|
1030 | 436e15b8 | bellard | ret = GetOverlappedResult(s->hfile, &ov, &ret_count, TRUE); |
1031 | 436e15b8 | bellard | if (!ret)
|
1032 | 436e15b8 | bellard | return -EIO;
|
1033 | 436e15b8 | bellard | else
|
1034 | 436e15b8 | bellard | return ret_count;
|
1035 | 436e15b8 | bellard | } |
1036 | 83f64091 | bellard | return ret_count;
|
1037 | 83f64091 | bellard | } |
1038 | 83f64091 | bellard | |
1039 | 3b9f94e1 | bellard | #if 0
|
1040 | 436e15b8 | bellard | #ifndef QEMU_TOOL
|
1041 | 83f64091 | bellard | static void raw_aio_cb(void *opaque)
|
1042 | 83f64091 | bellard | {
|
1043 | ce1a14dc | pbrook | RawAIOCB *acb = opaque;
|
1044 | ce1a14dc | pbrook | BlockDriverState *bs = acb->common.bs;
|
1045 | 979b67ad | bellard | BDRVRawState *s = bs->opaque;
|
1046 | 83f64091 | bellard | DWORD ret_count;
|
1047 | 83f64091 | bellard | int ret;
|
1048 | 83f64091 | bellard | |
1049 | ce1a14dc | pbrook | ret = GetOverlappedResult(s->hfile, &acb->ov, &ret_count, TRUE);
|
1050 | ce1a14dc | pbrook | if (!ret || ret_count != acb->count) {
|
1051 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, -EIO);
|
1052 | 83f64091 | bellard | } else {
|
1053 | ce1a14dc | pbrook | acb->common.cb(acb->common.opaque, 0);
|
1054 | 83f64091 | bellard | }
|
1055 | 83f64091 | bellard | }
|
1056 | 436e15b8 | bellard | #endif
|
1057 | ce1a14dc | pbrook | |
1058 | ce1a14dc | pbrook | static RawAIOCB *raw_aio_setup(BlockDriverState *bs,
|
1059 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
1060 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1061 | 83f64091 | bellard | { |
1062 | ce1a14dc | pbrook | RawAIOCB *acb; |
1063 | 83f64091 | bellard | int64_t offset; |
1064 | 83f64091 | bellard | |
1065 | ce1a14dc | pbrook | acb = qemu_aio_get(bs, cb, opaque); |
1066 | ce1a14dc | pbrook | if (acb->hEvent) {
|
1067 | ce1a14dc | pbrook | acb->hEvent = CreateEvent(NULL, TRUE, FALSE, NULL); |
1068 | ce1a14dc | pbrook | if (!acb->hEvent) {
|
1069 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1070 | ce1a14dc | pbrook | return NULL; |
1071 | ce1a14dc | pbrook | } |
1072 | ce1a14dc | pbrook | } |
1073 | ce1a14dc | pbrook | memset(&acb->ov, 0, sizeof(acb->ov)); |
1074 | 83f64091 | bellard | offset = sector_num * 512;
|
1075 | ce1a14dc | pbrook | acb->ov.Offset = offset; |
1076 | ce1a14dc | pbrook | acb->ov.OffsetHigh = offset >> 32;
|
1077 | ce1a14dc | pbrook | acb->ov.hEvent = acb->hEvent; |
1078 | ce1a14dc | pbrook | acb->count = nb_sectors * 512;
|
1079 | 436e15b8 | bellard | #ifndef QEMU_TOOL
|
1080 | ce1a14dc | pbrook | qemu_add_wait_object(acb->ov.hEvent, raw_aio_cb, acb); |
1081 | 436e15b8 | bellard | #endif
|
1082 | ce1a14dc | pbrook | return acb;
|
1083 | 83f64091 | bellard | } |
1084 | 83f64091 | bellard | |
1085 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_read(BlockDriverState *bs,
|
1086 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
1087 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1088 | 83f64091 | bellard | { |
1089 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
1090 | ce1a14dc | pbrook | RawAIOCB *acb; |
1091 | 83f64091 | bellard | int ret;
|
1092 | 83f64091 | bellard | |
1093 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
1094 | ce1a14dc | pbrook | if (!acb)
|
1095 | ce1a14dc | pbrook | return NULL; |
1096 | ce1a14dc | pbrook | ret = ReadFile(s->hfile, buf, acb->count, NULL, &acb->ov);
|
1097 | ce1a14dc | pbrook | if (!ret) {
|
1098 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1099 | ce1a14dc | pbrook | return NULL; |
1100 | ce1a14dc | pbrook | } |
1101 | ce1a14dc | pbrook | #ifdef QEMU_TOOL
|
1102 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1103 | 436e15b8 | bellard | #endif
|
1104 | ce1a14dc | pbrook | return (BlockDriverAIOCB *)acb;
|
1105 | 83f64091 | bellard | } |
1106 | 83f64091 | bellard | |
1107 | ce1a14dc | pbrook | static BlockDriverAIOCB *raw_aio_write(BlockDriverState *bs,
|
1108 | ce1a14dc | pbrook | int64_t sector_num, uint8_t *buf, int nb_sectors,
|
1109 | ce1a14dc | pbrook | BlockDriverCompletionFunc *cb, void *opaque)
|
1110 | 83f64091 | bellard | { |
1111 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
1112 | ce1a14dc | pbrook | RawAIOCB *acb; |
1113 | ce1a14dc | pbrook | int ret;
|
1114 | 83f64091 | bellard | |
1115 | ce1a14dc | pbrook | acb = raw_aio_setup(bs, sector_num, buf, nb_sectors, cb, opaque); |
1116 | ce1a14dc | pbrook | if (!acb)
|
1117 | ce1a14dc | pbrook | return NULL; |
1118 | ce1a14dc | pbrook | ret = WriteFile(s->hfile, buf, acb->count, NULL, &acb->ov);
|
1119 | ce1a14dc | pbrook | if (!ret) {
|
1120 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1121 | ce1a14dc | pbrook | return NULL; |
1122 | ce1a14dc | pbrook | } |
1123 | ce1a14dc | pbrook | #ifdef QEMU_TOOL
|
1124 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1125 | 436e15b8 | bellard | #endif
|
1126 | ce1a14dc | pbrook | return (BlockDriverAIOCB *)acb;
|
1127 | 83f64091 | bellard | } |
1128 | 83f64091 | bellard | |
1129 | ce1a14dc | pbrook | static void raw_aio_cancel(BlockDriverAIOCB *blockacb) |
1130 | 83f64091 | bellard | { |
1131 | ce1a14dc | pbrook | #ifndef QEMU_TOOL
|
1132 | ce1a14dc | pbrook | RawAIOCB *acb = (RawAIOCB *)blockacb; |
1133 | ce1a14dc | pbrook | BlockDriverState *bs = acb->common.bs; |
1134 | ce1a14dc | pbrook | BDRVRawState *s = bs->opaque; |
1135 | ce1a14dc | pbrook | |
1136 | ce1a14dc | pbrook | qemu_del_wait_object(acb->ov.hEvent, raw_aio_cb, acb); |
1137 | ce1a14dc | pbrook | /* XXX: if more than one async I/O it is not correct */
|
1138 | ce1a14dc | pbrook | CancelIo(s->hfile); |
1139 | ce1a14dc | pbrook | qemu_aio_release(acb); |
1140 | ce1a14dc | pbrook | #endif
|
1141 | 83f64091 | bellard | } |
1142 | 3b9f94e1 | bellard | #endif /* #if 0 */ |
1143 | 83f64091 | bellard | |
1144 | 83f64091 | bellard | static void raw_flush(BlockDriverState *bs) |
1145 | 83f64091 | bellard | { |
1146 | 3b9f94e1 | bellard | BDRVRawState *s = bs->opaque; |
1147 | 3b9f94e1 | bellard | FlushFileBuffers(s->hfile); |
1148 | 83f64091 | bellard | } |
1149 | 83f64091 | bellard | |
1150 | 83f64091 | bellard | static void raw_close(BlockDriverState *bs) |
1151 | 83f64091 | bellard | { |
1152 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
1153 | 83f64091 | bellard | CloseHandle(s->hfile); |
1154 | 83f64091 | bellard | } |
1155 | 83f64091 | bellard | |
1156 | 83f64091 | bellard | static int raw_truncate(BlockDriverState *bs, int64_t offset) |
1157 | 83f64091 | bellard | { |
1158 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
1159 | 83f64091 | bellard | DWORD low, high; |
1160 | 83f64091 | bellard | |
1161 | 979b67ad | bellard | low = offset; |
1162 | 979b67ad | bellard | high = offset >> 32;
|
1163 | 83f64091 | bellard | if (!SetFilePointer(s->hfile, low, &high, FILE_BEGIN))
|
1164 | 83f64091 | bellard | return -EIO;
|
1165 | 83f64091 | bellard | if (!SetEndOfFile(s->hfile))
|
1166 | 83f64091 | bellard | return -EIO;
|
1167 | 83f64091 | bellard | return 0; |
1168 | 83f64091 | bellard | } |
1169 | 83f64091 | bellard | |
1170 | f45512fe | bellard | static int64_t raw_getlength(BlockDriverState *bs)
|
1171 | 83f64091 | bellard | { |
1172 | 83f64091 | bellard | BDRVRawState *s = bs->opaque; |
1173 | 83f64091 | bellard | LARGE_INTEGER l; |
1174 | 5fafdf24 | ths | ULARGE_INTEGER available, total, total_free; |
1175 | 01781963 | bellard | DISK_GEOMETRY dg; |
1176 | 01781963 | bellard | DWORD count; |
1177 | 01781963 | bellard | BOOL status; |
1178 | 436e15b8 | bellard | |
1179 | f45512fe | bellard | switch(s->type) {
|
1180 | 19cb3738 | bellard | case FTYPE_FILE:
|
1181 | 19cb3738 | bellard | l.LowPart = GetFileSize(s->hfile, &l.HighPart); |
1182 | 19cb3738 | bellard | if (l.LowPart == 0xffffffffUL && GetLastError() != NO_ERROR) |
1183 | 19cb3738 | bellard | return -EIO;
|
1184 | 19cb3738 | bellard | break;
|
1185 | 19cb3738 | bellard | case FTYPE_CD:
|
1186 | f45512fe | bellard | if (!GetDiskFreeSpaceEx(s->drive_path, &available, &total, &total_free))
|
1187 | 19cb3738 | bellard | return -EIO;
|
1188 | f45512fe | bellard | l.QuadPart = total.QuadPart; |
1189 | 19cb3738 | bellard | break;
|
1190 | 01781963 | bellard | case FTYPE_HARDDISK:
|
1191 | 01781963 | bellard | status = DeviceIoControl(s->hfile, IOCTL_DISK_GET_DRIVE_GEOMETRY, |
1192 | 01781963 | bellard | NULL, 0, &dg, sizeof(dg), &count, NULL); |
1193 | 01781963 | bellard | if (status != FALSE) {
|
1194 | 01781963 | bellard | l.QuadPart = dg.Cylinders.QuadPart * dg.TracksPerCylinder |
1195 | 01781963 | bellard | * dg.SectorsPerTrack * dg.BytesPerSector; |
1196 | 01781963 | bellard | } |
1197 | 01781963 | bellard | break;
|
1198 | 19cb3738 | bellard | default:
|
1199 | 19cb3738 | bellard | return -EIO;
|
1200 | 19cb3738 | bellard | } |
1201 | 83f64091 | bellard | return l.QuadPart;
|
1202 | 83f64091 | bellard | } |
1203 | 83f64091 | bellard | |
1204 | 83f64091 | bellard | static int raw_create(const char *filename, int64_t total_size, |
1205 | 83f64091 | bellard | const char *backing_file, int flags) |
1206 | 83f64091 | bellard | { |
1207 | 83f64091 | bellard | int fd;
|
1208 | 83f64091 | bellard | |
1209 | 83f64091 | bellard | if (flags || backing_file)
|
1210 | 83f64091 | bellard | return -ENOTSUP;
|
1211 | 83f64091 | bellard | |
1212 | 5fafdf24 | ths | fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, |
1213 | 83f64091 | bellard | 0644);
|
1214 | 83f64091 | bellard | if (fd < 0) |
1215 | 83f64091 | bellard | return -EIO;
|
1216 | 83f64091 | bellard | set_sparse(fd); |
1217 | 83f64091 | bellard | ftruncate(fd, total_size * 512);
|
1218 | 83f64091 | bellard | close(fd); |
1219 | 83f64091 | bellard | return 0; |
1220 | 83f64091 | bellard | } |
1221 | 83f64091 | bellard | |
1222 | 83f64091 | bellard | void qemu_aio_init(void) |
1223 | 83f64091 | bellard | { |
1224 | 83f64091 | bellard | } |
1225 | 83f64091 | bellard | |
1226 | 83f64091 | bellard | void qemu_aio_poll(void) |
1227 | 83f64091 | bellard | { |
1228 | 83f64091 | bellard | } |
1229 | 83f64091 | bellard | |
1230 | 62ee0211 | ths | void qemu_aio_flush(void) |
1231 | 62ee0211 | ths | { |
1232 | 62ee0211 | ths | } |
1233 | 62ee0211 | ths | |
1234 | 83f64091 | bellard | void qemu_aio_wait_start(void) |
1235 | 83f64091 | bellard | { |
1236 | 83f64091 | bellard | } |
1237 | 83f64091 | bellard | |
1238 | 83f64091 | bellard | void qemu_aio_wait(void) |
1239 | 83f64091 | bellard | { |
1240 | fd44d818 | bellard | #ifndef QEMU_TOOL
|
1241 | fd44d818 | bellard | qemu_bh_poll(); |
1242 | fd44d818 | bellard | #endif
|
1243 | 83f64091 | bellard | } |
1244 | 83f64091 | bellard | |
1245 | 83f64091 | bellard | void qemu_aio_wait_end(void) |
1246 | 83f64091 | bellard | { |
1247 | 83f64091 | bellard | } |
1248 | 83f64091 | bellard | |
1249 | 83f64091 | bellard | BlockDriver bdrv_raw = { |
1250 | 83f64091 | bellard | "raw",
|
1251 | 83f64091 | bellard | sizeof(BDRVRawState),
|
1252 | 83f64091 | bellard | NULL, /* no probe for protocols */ |
1253 | 83f64091 | bellard | raw_open, |
1254 | 83f64091 | bellard | NULL,
|
1255 | 83f64091 | bellard | NULL,
|
1256 | 83f64091 | bellard | raw_close, |
1257 | 83f64091 | bellard | raw_create, |
1258 | 83f64091 | bellard | raw_flush, |
1259 | 5fafdf24 | ths | |
1260 | 83f64091 | bellard | #if 0
|
1261 | 83f64091 | bellard | .bdrv_aio_read = raw_aio_read,
|
1262 | 83f64091 | bellard | .bdrv_aio_write = raw_aio_write,
|
1263 | 83f64091 | bellard | .bdrv_aio_cancel = raw_aio_cancel,
|
1264 | ce1a14dc | pbrook | .aiocb_size = sizeof(RawAIOCB);
|
1265 | 83f64091 | bellard | #endif
|
1266 | 83f64091 | bellard | .protocol_name = "file",
|
1267 | 83f64091 | bellard | .bdrv_pread = raw_pread, |
1268 | 83f64091 | bellard | .bdrv_pwrite = raw_pwrite, |
1269 | 83f64091 | bellard | .bdrv_truncate = raw_truncate, |
1270 | 83f64091 | bellard | .bdrv_getlength = raw_getlength, |
1271 | 83f64091 | bellard | }; |
1272 | 19cb3738 | bellard | |
1273 | 19cb3738 | bellard | /***********************************************/
|
1274 | 19cb3738 | bellard | /* host device */
|
1275 | 19cb3738 | bellard | |
1276 | 19cb3738 | bellard | static int find_cdrom(char *cdrom_name, int cdrom_name_size) |
1277 | 19cb3738 | bellard | { |
1278 | 19cb3738 | bellard | char drives[256], *pdrv = drives; |
1279 | 19cb3738 | bellard | UINT type; |
1280 | 19cb3738 | bellard | |
1281 | f45512fe | bellard | memset(drives, 0, sizeof(drives)); |
1282 | 19cb3738 | bellard | GetLogicalDriveStrings(sizeof(drives), drives);
|
1283 | 19cb3738 | bellard | while(pdrv[0] != '\0') { |
1284 | 19cb3738 | bellard | type = GetDriveType(pdrv); |
1285 | 19cb3738 | bellard | switch(type) {
|
1286 | 19cb3738 | bellard | case DRIVE_CDROM:
|
1287 | 19cb3738 | bellard | snprintf(cdrom_name, cdrom_name_size, "\\\\.\\%c:", pdrv[0]); |
1288 | 19cb3738 | bellard | return 0; |
1289 | 19cb3738 | bellard | break;
|
1290 | 19cb3738 | bellard | } |
1291 | 19cb3738 | bellard | pdrv += lstrlen(pdrv) + 1;
|
1292 | 19cb3738 | bellard | } |
1293 | 19cb3738 | bellard | return -1; |
1294 | 19cb3738 | bellard | } |
1295 | 19cb3738 | bellard | |
1296 | f45512fe | bellard | static int find_device_type(BlockDriverState *bs, const char *filename) |
1297 | 19cb3738 | bellard | { |
1298 | f45512fe | bellard | BDRVRawState *s = bs->opaque; |
1299 | 19cb3738 | bellard | UINT type; |
1300 | 19cb3738 | bellard | const char *p; |
1301 | 19cb3738 | bellard | |
1302 | 19cb3738 | bellard | if (strstart(filename, "\\\\.\\", &p) || |
1303 | 19cb3738 | bellard | strstart(filename, "//./", &p)) {
|
1304 | 01781963 | bellard | if (stristart(p, "PhysicalDrive", NULL)) |
1305 | 01781963 | bellard | return FTYPE_HARDDISK;
|
1306 | f45512fe | bellard | snprintf(s->drive_path, sizeof(s->drive_path), "%c:\\", p[0]); |
1307 | f45512fe | bellard | type = GetDriveType(s->drive_path); |
1308 | 19cb3738 | bellard | if (type == DRIVE_CDROM)
|
1309 | 19cb3738 | bellard | return FTYPE_CD;
|
1310 | 19cb3738 | bellard | else
|
1311 | 19cb3738 | bellard | return FTYPE_FILE;
|
1312 | 19cb3738 | bellard | } else {
|
1313 | 19cb3738 | bellard | return FTYPE_FILE;
|
1314 | 19cb3738 | bellard | } |
1315 | 19cb3738 | bellard | } |
1316 | 19cb3738 | bellard | |
1317 | 19cb3738 | bellard | static int hdev_open(BlockDriverState *bs, const char *filename, int flags) |
1318 | 19cb3738 | bellard | { |
1319 | 19cb3738 | bellard | BDRVRawState *s = bs->opaque; |
1320 | 19cb3738 | bellard | int access_flags, create_flags;
|
1321 | 19cb3738 | bellard | DWORD overlapped; |
1322 | 19cb3738 | bellard | char device_name[64]; |
1323 | 19cb3738 | bellard | |
1324 | 19cb3738 | bellard | if (strstart(filename, "/dev/cdrom", NULL)) { |
1325 | 19cb3738 | bellard | if (find_cdrom(device_name, sizeof(device_name)) < 0) |
1326 | 19cb3738 | bellard | return -ENOENT;
|
1327 | 19cb3738 | bellard | filename = device_name; |
1328 | 19cb3738 | bellard | } else {
|
1329 | 19cb3738 | bellard | /* transform drive letters into device name */
|
1330 | 19cb3738 | bellard | if (((filename[0] >= 'a' && filename[0] <= 'z') || |
1331 | 19cb3738 | bellard | (filename[0] >= 'A' && filename[0] <= 'Z')) && |
1332 | 19cb3738 | bellard | filename[1] == ':' && filename[2] == '\0') { |
1333 | 19cb3738 | bellard | snprintf(device_name, sizeof(device_name), "\\\\.\\%c:", filename[0]); |
1334 | 19cb3738 | bellard | filename = device_name; |
1335 | 19cb3738 | bellard | } |
1336 | 19cb3738 | bellard | } |
1337 | f45512fe | bellard | s->type = find_device_type(bs, filename); |
1338 | 5fafdf24 | ths | |
1339 | 19cb3738 | bellard | if ((flags & BDRV_O_ACCESS) == O_RDWR) {
|
1340 | 19cb3738 | bellard | access_flags = GENERIC_READ | GENERIC_WRITE; |
1341 | 19cb3738 | bellard | } else {
|
1342 | 19cb3738 | bellard | access_flags = GENERIC_READ; |
1343 | 19cb3738 | bellard | } |
1344 | 19cb3738 | bellard | create_flags = OPEN_EXISTING; |
1345 | 19cb3738 | bellard | |
1346 | 19cb3738 | bellard | #ifdef QEMU_TOOL
|
1347 | 3b9f94e1 | bellard | overlapped = FILE_ATTRIBUTE_NORMAL; |
1348 | 19cb3738 | bellard | #else
|
1349 | 19cb3738 | bellard | overlapped = FILE_FLAG_OVERLAPPED; |
1350 | 19cb3738 | bellard | #endif
|
1351 | 5fafdf24 | ths | s->hfile = CreateFile(filename, access_flags, |
1352 | 19cb3738 | bellard | FILE_SHARE_READ, NULL,
|
1353 | 3b9f94e1 | bellard | create_flags, overlapped, NULL);
|
1354 | 54421cb1 | ths | if (s->hfile == INVALID_HANDLE_VALUE) {
|
1355 | 54421cb1 | ths | int err = GetLastError();
|
1356 | 54421cb1 | ths | |
1357 | 54421cb1 | ths | if (err == ERROR_ACCESS_DENIED)
|
1358 | 54421cb1 | ths | return -EACCES;
|
1359 | 19cb3738 | bellard | return -1; |
1360 | 54421cb1 | ths | } |
1361 | 19cb3738 | bellard | return 0; |
1362 | 19cb3738 | bellard | } |
1363 | 19cb3738 | bellard | |
1364 | 19cb3738 | bellard | #if 0
|
1365 | 19cb3738 | bellard | /***********************************************/
|
1366 | aa1f17c1 | ths | /* removable device additional commands */
|
1367 | 19cb3738 | bellard | |
1368 | 19cb3738 | bellard | static int raw_is_inserted(BlockDriverState *bs)
|
1369 | 19cb3738 | bellard | {
|
1370 | 19cb3738 | bellard | return 1;
|
1371 | 19cb3738 | bellard | }
|
1372 | 19cb3738 | bellard | |
1373 | 19cb3738 | bellard | static int raw_media_changed(BlockDriverState *bs)
|
1374 | 19cb3738 | bellard | {
|
1375 | 19cb3738 | bellard | return -ENOTSUP;
|
1376 | 19cb3738 | bellard | }
|
1377 | 19cb3738 | bellard | |
1378 | 19cb3738 | bellard | static int raw_eject(BlockDriverState *bs, int eject_flag)
|
1379 | 19cb3738 | bellard | {
|
1380 | 19cb3738 | bellard | DWORD ret_count;
|
1381 | 19cb3738 | bellard | |
1382 | 19cb3738 | bellard | if (s->type == FTYPE_FILE)
|
1383 | 19cb3738 | bellard | return -ENOTSUP;
|
1384 | 19cb3738 | bellard | if (eject_flag) {
|
1385 | 5fafdf24 | ths | DeviceIoControl(s->hfile, IOCTL_STORAGE_EJECT_MEDIA,
|
1386 | 19cb3738 | bellard | NULL, 0, NULL, 0, &lpBytesReturned, NULL);
|
1387 | 19cb3738 | bellard | } else {
|
1388 | 5fafdf24 | ths | DeviceIoControl(s->hfile, IOCTL_STORAGE_LOAD_MEDIA,
|
1389 | 19cb3738 | bellard | NULL, 0, NULL, 0, &lpBytesReturned, NULL);
|
1390 | 19cb3738 | bellard | }
|
1391 | 19cb3738 | bellard | }
|
1392 | 19cb3738 | bellard | |
1393 | 19cb3738 | bellard | static int raw_set_locked(BlockDriverState *bs, int locked)
|
1394 | 19cb3738 | bellard | {
|
1395 | 19cb3738 | bellard | return -ENOTSUP;
|
1396 | 19cb3738 | bellard | }
|
1397 | 19cb3738 | bellard | #endif
|
1398 | 19cb3738 | bellard | |
1399 | 19cb3738 | bellard | BlockDriver bdrv_host_device = { |
1400 | 19cb3738 | bellard | "host_device",
|
1401 | 19cb3738 | bellard | sizeof(BDRVRawState),
|
1402 | 19cb3738 | bellard | NULL, /* no probe for protocols */ |
1403 | 19cb3738 | bellard | hdev_open, |
1404 | 19cb3738 | bellard | NULL,
|
1405 | 19cb3738 | bellard | NULL,
|
1406 | 19cb3738 | bellard | raw_close, |
1407 | 19cb3738 | bellard | NULL,
|
1408 | 19cb3738 | bellard | raw_flush, |
1409 | 5fafdf24 | ths | |
1410 | 19cb3738 | bellard | #if 0
|
1411 | 19cb3738 | bellard | .bdrv_aio_read = raw_aio_read,
|
1412 | 19cb3738 | bellard | .bdrv_aio_write = raw_aio_write,
|
1413 | 19cb3738 | bellard | .bdrv_aio_cancel = raw_aio_cancel,
|
1414 | 19cb3738 | bellard | .aiocb_size = sizeof(RawAIOCB);
|
1415 | 19cb3738 | bellard | #endif
|
1416 | 19cb3738 | bellard | .bdrv_pread = raw_pread, |
1417 | 19cb3738 | bellard | .bdrv_pwrite = raw_pwrite, |
1418 | 19cb3738 | bellard | .bdrv_getlength = raw_getlength, |
1419 | 19cb3738 | bellard | }; |
1420 | 83f64091 | bellard | #endif /* _WIN32 */ |