2 * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
11 #include <sys/syscall.h>
18 struct aio_thread_req {
21 unsigned long long offset;
24 struct aio_context *aio;
27 #if defined(HAVE_AIO_ABI)
28 #include <linux/aio_abi.h>
30 /* If we have the headers, we are going to build with AIO enabled.
31 * If we don't have aio in libc, we define the necessary stubs here.
34 #if !defined(HAVE_AIO_LIBC)
36 static long io_setup(int n, aio_context_t *ctxp)
38 return syscall(__NR_io_setup, n, ctxp);
41 static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp)
43 return syscall(__NR_io_submit, ctx, nr, iocbpp);
46 static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
47 struct io_event *events, struct timespec *timeout)
49 return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
54 /* The AIO_MMAP cases force the mmapped page into memory here
55 * rather than in whatever place first touches the data. I used
56 * to do this by touching the page, but that's delicate because
57 * gcc is prone to optimizing that away. So, what's done here
58 * is we read from the descriptor from which the page was
59 * mapped. The caller is required to pass an offset which is
60 * inside the page that was mapped. Thus, when the read
61 * returns, we know that the page is in the page cache, and
62 * that it now backs the mmapped area.
65 static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf,
66 int len, unsigned long long offset, struct aio_context *aio)
68 struct iocb iocb, *iocbp = &iocb;
72 iocb = ((struct iocb) { .aio_data = (unsigned long) aio,
75 .aio_buf = (unsigned long) buf,
80 .aio_reserved3 = 0 });
84 iocb.aio_lio_opcode = IOCB_CMD_PREAD;
85 err = io_submit(ctx, 1, &iocbp);
88 iocb.aio_lio_opcode = IOCB_CMD_PWRITE;
89 err = io_submit(ctx, 1, &iocbp);
92 iocb.aio_lio_opcode = IOCB_CMD_PREAD;
93 iocb.aio_buf = (unsigned long) &c;
94 iocb.aio_nbytes = sizeof(c);
95 err = io_submit(ctx, 1, &iocbp);
98 printk("Bogus op in do_aio - %d\n", type);
111 /* Initialized in an initcall and unchanged thereafter */
112 static aio_context_t ctx = 0;
114 static int aio_thread(void *arg)
116 struct aio_thread_reply reply;
117 struct io_event event;
118 int err, n, reply_fd;
120 signal(SIGWINCH, SIG_IGN);
123 n = io_getevents(ctx, 1, 1, &event, NULL);
127 printk("aio_thread - io_getevents failed, "
128 "errno = %d\n", errno);
131 reply = ((struct aio_thread_reply)
132 { .data = (void *) (long) event.data,
134 reply_fd = ((struct aio_context *) reply.data)->reply_fd;
135 err = write(reply_fd, &reply, sizeof(reply));
136 if(err != sizeof(reply))
137 printk("aio_thread - write failed, fd = %d, "
138 "err = %d\n", reply_fd, errno);
146 static int do_not_aio(struct aio_thread_req *req)
149 unsigned long long actual;
152 actual = lseek64(req->io_fd, req->offset, SEEK_SET);
153 if(actual != req->offset)
158 n = read(req->io_fd, req->buf, req->len);
161 n = write(req->io_fd, req->buf, req->len);
164 n = read(req->io_fd, &c, sizeof(c));
167 printk("do_not_aio - bad request type : %d\n", req->type);
176 /* These are initialized in initcalls and not changed */
177 static int aio_req_fd_r = -1;
178 static int aio_req_fd_w = -1;
179 static int aio_pid = -1;
180 static unsigned long aio_stack;
182 static int not_aio_thread(void *arg)
184 struct aio_thread_req req;
185 struct aio_thread_reply reply;
188 signal(SIGWINCH, SIG_IGN);
190 err = read(aio_req_fd_r, &req, sizeof(req));
191 if(err != sizeof(req)){
193 printk("not_aio_thread - read failed, "
194 "fd = %d, err = %d\n", aio_req_fd_r,
197 printk("not_aio_thread - short read, fd = %d, "
198 "length = %d\n", aio_req_fd_r, err);
202 err = do_not_aio(&req);
203 reply = ((struct aio_thread_reply) { .data = req.aio,
205 err = write(req.aio->reply_fd, &reply, sizeof(reply));
206 if(err != sizeof(reply))
207 printk("not_aio_thread - write failed, fd = %d, "
208 "err = %d\n", req.aio->reply_fd, errno);
214 static int init_aio_24(void)
218 err = os_pipe(fds, 1, 1);
222 aio_req_fd_w = fds[0];
223 aio_req_fd_r = fds[1];
225 err = os_set_fd_block(aio_req_fd_w, 0);
229 err = run_helper_thread(not_aio_thread, NULL,
230 CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
238 os_close_file(fds[0]);
239 os_close_file(fds[1]);
244 printk("/usr/include/linux/aio_abi.h not present during build\n");
246 printk("2.6 host AIO support not used - falling back to I/O "
252 #define DEFAULT_24_AIO 0
253 static int init_aio_26(void)
257 if(io_setup(256, &ctx)){
259 printk("aio_thread failed to initialize context, err = %d\n",
264 err = run_helper_thread(aio_thread, NULL,
265 CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
271 printk("Using 2.6 host AIO\n");
275 static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
276 unsigned long long offset, struct aio_context *aio)
278 struct aio_thread_reply reply;
281 err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
283 reply = ((struct aio_thread_reply) { .data = aio,
285 err = write(aio->reply_fd, &reply, sizeof(reply));
286 if(err != sizeof(reply)){
288 printk("submit_aio_26 - write failed, "
289 "fd = %d, err = %d\n", aio->reply_fd, -err);
298 #define DEFAULT_24_AIO 1
299 static int init_aio_26(void)
304 static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
305 unsigned long long offset, struct aio_context *aio)
311 /* Initialized in an initcall and unchanged thereafter */
312 static int aio_24 = DEFAULT_24_AIO;
314 static int __init set_aio_24(char *name, int *add)
320 __uml_setup("aio=2.4", set_aio_24,
322 " This is used to force UML to use 2.4-style AIO even when 2.6 AIO is\n"
323 " available. 2.4 AIO is a single thread that handles one request at a\n"
324 " time, synchronously. 2.6 AIO is a thread which uses the 2.6 AIO \n"
325 " interface to handle an arbitrary number of pending requests. 2.6 AIO \n"
326 " is not available in tt mode, on 2.4 hosts, or when UML is built with\n"
327 " /usr/include/linux/aio_abi.h not available. Many distributions don't\n"
328 " include aio_abi.h, so you will need to copy it from a kernel tree to\n"
329 " your /usr/include/linux in order to build an AIO-capable UML\n\n"
332 static int init_aio(void)
336 CHOOSE_MODE(({ if(!aio_24){
337 printk("Disabling 2.6 AIO in tt mode\n");
343 if(err && (errno == ENOSYS)){
344 printk("2.6 AIO not supported on the host - "
345 "reverting to 2.4 AIO\n");
352 return init_aio_24();
357 /* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
358 * needs to be called when the kernel is running because it calls run_helper,
359 * which needs get_free_page. exit_aio is a __uml_exitcall because the generic
360 * kernel does not run __exitcalls on shutdown, and can't because many of them
361 * break when called outside of module unloading.
363 __initcall(init_aio);
365 static void exit_aio(void)
368 os_kill_process(aio_pid, 1);
369 free_stack(aio_stack, 0);
373 __uml_exitcall(exit_aio);
375 static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
376 unsigned long long offset, struct aio_context *aio)
378 struct aio_thread_req req = { .type = type,
387 err = write(aio_req_fd_w, &req, sizeof(req));
388 if(err == sizeof(req))
395 int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
396 unsigned long long offset, int reply_fd,
397 struct aio_context *aio)
399 aio->reply_fd = reply_fd;
401 return submit_aio_24(type, io_fd, buf, len, offset, aio);
403 return submit_aio_26(type, io_fd, buf, len, offset, aio);