2 * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
11 #include <sys/syscall.h>
17 #include "kern_constants.h"
19 struct aio_thread_req {
22 unsigned long long offset;
25 struct aio_context *aio;
28 #if defined(HAVE_AIO_ABI)
29 #include <linux/aio_abi.h>
31 /* If we have the headers, we are going to build with AIO enabled.
32 * If we don't have aio in libc, we define the necessary stubs here.
35 #if !defined(HAVE_AIO_LIBC)
37 static long io_setup(int n, aio_context_t *ctxp)
39 return syscall(__NR_io_setup, n, ctxp);
42 static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp)
44 return syscall(__NR_io_submit, ctx, nr, iocbpp);
47 static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
48 struct io_event *events, struct timespec *timeout)
50 return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
55 /* The AIO_MMAP cases force the mmapped page into memory here
56 * rather than in whatever place first touches the data. I used
57 * to do this by touching the page, but that's delicate because
58 * gcc is prone to optimizing that away. So, what's done here
59 * is we read from the descriptor from which the page was
60 * mapped. The caller is required to pass an offset which is
61 * inside the page that was mapped. Thus, when the read
62 * returns, we know that the page is in the page cache, and
63 * that it now backs the mmapped area.
66 static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf,
67 int len, unsigned long long offset, struct aio_context *aio)
69 struct iocb *iocbp = & ((struct iocb) {
70 .aio_data = (unsigned long) aio,
72 .aio_buf = (unsigned long) buf,
80 iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
83 iocbp->aio_lio_opcode = IOCB_CMD_PWRITE;
86 iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
87 iocbp->aio_buf = (unsigned long) &c;
88 iocbp->aio_nbytes = sizeof(c);
91 printk(UM_KERN_ERR "Bogus op in do_aio - %d\n", type);
95 return (io_submit(ctx, 1, &iocbp) > 0) ? 0 : -errno;
98 /* Initialized in an initcall and unchanged thereafter */
99 static aio_context_t ctx = 0;
101 static int aio_thread(void *arg)
103 struct aio_thread_reply reply;
104 struct io_event event;
105 int err, n, reply_fd;
107 signal(SIGWINCH, SIG_IGN);
110 n = io_getevents(ctx, 1, 1, &event, NULL);
114 printk("aio_thread - io_getevents failed, "
115 "errno = %d\n", errno);
118 reply = ((struct aio_thread_reply)
119 { .data = (void *) (long) event.data,
121 reply_fd = ((struct aio_context *) reply.data)->reply_fd;
122 err = write(reply_fd, &reply, sizeof(reply));
123 if(err != sizeof(reply))
124 printk("aio_thread - write failed, fd = %d, "
125 "err = %d\n", reply_fd, errno);
133 static int do_not_aio(struct aio_thread_req *req)
136 unsigned long long actual;
139 actual = lseek64(req->io_fd, req->offset, SEEK_SET);
140 if(actual != req->offset)
145 n = read(req->io_fd, req->buf, req->len);
148 n = write(req->io_fd, req->buf, req->len);
151 n = read(req->io_fd, &c, sizeof(c));
154 printk("do_not_aio - bad request type : %d\n", req->type);
163 /* These are initialized in initcalls and not changed */
164 static int aio_req_fd_r = -1;
165 static int aio_req_fd_w = -1;
166 static int aio_pid = -1;
167 static unsigned long aio_stack;
169 static int not_aio_thread(void *arg)
171 struct aio_thread_req req;
172 struct aio_thread_reply reply;
175 signal(SIGWINCH, SIG_IGN);
177 err = read(aio_req_fd_r, &req, sizeof(req));
178 if(err != sizeof(req)){
180 printk("not_aio_thread - read failed, "
181 "fd = %d, err = %d\n", aio_req_fd_r,
184 printk("not_aio_thread - short read, fd = %d, "
185 "length = %d\n", aio_req_fd_r, err);
189 err = do_not_aio(&req);
190 reply = ((struct aio_thread_reply) { .data = req.aio,
192 err = write(req.aio->reply_fd, &reply, sizeof(reply));
193 if(err != sizeof(reply))
194 printk("not_aio_thread - write failed, fd = %d, "
195 "err = %d\n", req.aio->reply_fd, errno);
201 static int init_aio_24(void)
205 err = os_pipe(fds, 1, 1);
209 aio_req_fd_w = fds[0];
210 aio_req_fd_r = fds[1];
212 err = os_set_fd_block(aio_req_fd_w, 0);
216 err = run_helper_thread(not_aio_thread, NULL,
217 CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
225 os_close_file(fds[0]);
226 os_close_file(fds[1]);
231 printk("/usr/include/linux/aio_abi.h not present during build\n");
233 printk("2.6 host AIO support not used - falling back to I/O "
239 #define DEFAULT_24_AIO 0
240 static int init_aio_26(void)
244 if(io_setup(256, &ctx)){
246 printk("aio_thread failed to initialize context, err = %d\n",
251 err = run_helper_thread(aio_thread, NULL,
252 CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack);
258 printk("Using 2.6 host AIO\n");
262 static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
263 unsigned long long offset, struct aio_context *aio)
265 struct aio_thread_reply reply;
268 err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
270 reply = ((struct aio_thread_reply) { .data = aio,
272 err = write(aio->reply_fd, &reply, sizeof(reply));
273 if(err != sizeof(reply)){
275 printk("submit_aio_26 - write failed, "
276 "fd = %d, err = %d\n", aio->reply_fd, -err);
285 #define DEFAULT_24_AIO 1
286 static int init_aio_26(void)
291 static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
292 unsigned long long offset, struct aio_context *aio)
298 /* Initialized in an initcall and unchanged thereafter */
299 static int aio_24 = DEFAULT_24_AIO;
301 static int __init set_aio_24(char *name, int *add)
307 __uml_setup("aio=2.4", set_aio_24,
309 " This is used to force UML to use 2.4-style AIO even when 2.6 AIO is\n"
310 " available. 2.4 AIO is a single thread that handles one request at a\n"
311 " time, synchronously. 2.6 AIO is a thread which uses the 2.6 AIO \n"
312 " interface to handle an arbitrary number of pending requests. 2.6 AIO \n"
313 " is not available in tt mode, on 2.4 hosts, or when UML is built with\n"
314 " /usr/include/linux/aio_abi.h not available. Many distributions don't\n"
315 " include aio_abi.h, so you will need to copy it from a kernel tree to\n"
316 " your /usr/include/linux in order to build an AIO-capable UML\n\n"
319 static int init_aio(void)
323 CHOOSE_MODE(({ if(!aio_24){
324 printk("Disabling 2.6 AIO in tt mode\n");
330 if(err && (errno == ENOSYS)){
331 printk("2.6 AIO not supported on the host - "
332 "reverting to 2.4 AIO\n");
339 return init_aio_24();
344 /* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
345 * needs to be called when the kernel is running because it calls run_helper,
346 * which needs get_free_page. exit_aio is a __uml_exitcall because the generic
347 * kernel does not run __exitcalls on shutdown, and can't because many of them
348 * break when called outside of module unloading.
350 __initcall(init_aio);
352 static void exit_aio(void)
355 os_kill_process(aio_pid, 1);
356 free_stack(aio_stack, 0);
360 __uml_exitcall(exit_aio);
362 static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
363 unsigned long long offset, struct aio_context *aio)
365 struct aio_thread_req req = { .type = type,
374 err = write(aio_req_fd_w, &req, sizeof(req));
375 if(err == sizeof(req))
382 int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
383 unsigned long long offset, int reply_fd,
384 struct aio_context *aio)
386 aio->reply_fd = reply_fd;
388 return submit_aio_24(type, io_fd, buf, len, offset, aio);
390 return submit_aio_26(type, io_fd, buf, len, offset, aio);