Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include <stdlib.h> |
| 7 | #include <unistd.h> |
| 8 | #include <signal.h> |
| 9 | #include <errno.h> |
| 10 | #include <sched.h> |
| 11 | #include <sys/syscall.h> |
| 12 | #include "os.h" |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 13 | #include "aio.h" |
| 14 | #include "init.h" |
| 15 | #include "user.h" |
| 16 | #include "mode.h" |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 17 | #include "kern_constants.h" |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 18 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 19 | struct aio_thread_req { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 20 | enum aio_type type; |
| 21 | int io_fd; |
| 22 | unsigned long long offset; |
| 23 | char *buf; |
| 24 | int len; |
| 25 | struct aio_context *aio; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 26 | }; |
| 27 | |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 28 | #if defined(HAVE_AIO_ABI) |
| 29 | #include <linux/aio_abi.h> |
| 30 | |
| 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. |
| 33 | */ |
| 34 | |
| 35 | #if !defined(HAVE_AIO_LIBC) |
| 36 | |
| 37 | static long io_setup(int n, aio_context_t *ctxp) |
| 38 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 39 | return syscall(__NR_io_setup, n, ctxp); |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp) |
| 43 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 44 | return syscall(__NR_io_submit, ctx, nr, iocbpp); |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static long io_getevents(aio_context_t ctx_id, long min_nr, long nr, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 48 | struct io_event *events, struct timespec *timeout) |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 49 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 50 | return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout); |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | #endif |
| 54 | |
| 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. |
| 64 | */ |
| 65 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 66 | static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 67 | int len, unsigned long long offset, struct aio_context *aio) |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 68 | { |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 69 | struct iocb *iocbp = & ((struct iocb) { |
| 70 | .aio_data = (unsigned long) aio, |
| 71 | .aio_fildes = fd, |
| 72 | .aio_buf = (unsigned long) buf, |
| 73 | .aio_nbytes = len, |
| 74 | .aio_offset = offset |
| 75 | }); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 76 | char c; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 77 | |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 78 | switch (type) { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 79 | case AIO_READ: |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 80 | iocbp->aio_lio_opcode = IOCB_CMD_PREAD; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 81 | break; |
| 82 | case AIO_WRITE: |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 83 | iocbp->aio_lio_opcode = IOCB_CMD_PWRITE; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 84 | break; |
| 85 | case AIO_MMAP: |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 86 | iocbp->aio_lio_opcode = IOCB_CMD_PREAD; |
| 87 | iocbp->aio_buf = (unsigned long) &c; |
| 88 | iocbp->aio_nbytes = sizeof(c); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 89 | break; |
| 90 | default: |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 91 | printk(UM_KERN_ERR "Bogus op in do_aio - %d\n", type); |
| 92 | return -EINVAL; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 93 | } |
Jeff Dike | 09ace81 | 2005-09-03 15:57:46 -0700 | [diff] [blame] | 94 | |
Jeff Dike | da3e30e | 2007-07-23 18:43:47 -0700 | [diff] [blame^] | 95 | return (io_submit(ctx, 1, &iocbp) > 0) ? 0 : -errno; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Jeff Dike | 9683da9 | 2007-02-10 01:44:27 -0800 | [diff] [blame] | 98 | /* Initialized in an initcall and unchanged thereafter */ |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 99 | static aio_context_t ctx = 0; |
| 100 | |
| 101 | static int aio_thread(void *arg) |
| 102 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 103 | struct aio_thread_reply reply; |
| 104 | struct io_event event; |
| 105 | int err, n, reply_fd; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 106 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 107 | signal(SIGWINCH, SIG_IGN); |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 108 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 109 | while(1){ |
| 110 | n = io_getevents(ctx, 1, 1, &event, NULL); |
| 111 | if(n < 0){ |
| 112 | if(errno == EINTR) |
| 113 | continue; |
| 114 | printk("aio_thread - io_getevents failed, " |
| 115 | "errno = %d\n", errno); |
| 116 | } |
| 117 | else { |
| 118 | reply = ((struct aio_thread_reply) |
| 119 | { .data = (void *) (long) event.data, |
| 120 | .err = event.res }); |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 121 | reply_fd = ((struct aio_context *) reply.data)->reply_fd; |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 122 | err = write(reply_fd, &reply, sizeof(reply)); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 123 | if(err != sizeof(reply)) |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 124 | printk("aio_thread - write failed, fd = %d, " |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 125 | "err = %d\n", reply_fd, errno); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | return 0; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | #endif |
| 132 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 133 | static int do_not_aio(struct aio_thread_req *req) |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 134 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 135 | char c; |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 136 | unsigned long long actual; |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 137 | int n; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 138 | |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 139 | actual = lseek64(req->io_fd, req->offset, SEEK_SET); |
| 140 | if(actual != req->offset) |
| 141 | return -errno; |
| 142 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 143 | switch(req->type){ |
| 144 | case AIO_READ: |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 145 | n = read(req->io_fd, req->buf, req->len); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 146 | break; |
| 147 | case AIO_WRITE: |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 148 | n = write(req->io_fd, req->buf, req->len); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 149 | break; |
| 150 | case AIO_MMAP: |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 151 | n = read(req->io_fd, &c, sizeof(c)); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 152 | break; |
| 153 | default: |
| 154 | printk("do_not_aio - bad request type : %d\n", req->type); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 155 | return -EINVAL; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 156 | } |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 157 | |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 158 | if(n < 0) |
| 159 | return -errno; |
| 160 | return 0; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Jeff Dike | 9683da9 | 2007-02-10 01:44:27 -0800 | [diff] [blame] | 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; |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 167 | static unsigned long aio_stack; |
Jeff Dike | 9683da9 | 2007-02-10 01:44:27 -0800 | [diff] [blame] | 168 | |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 169 | static int not_aio_thread(void *arg) |
| 170 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 171 | struct aio_thread_req req; |
| 172 | struct aio_thread_reply reply; |
| 173 | int err; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 174 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 175 | signal(SIGWINCH, SIG_IGN); |
| 176 | while(1){ |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 177 | err = read(aio_req_fd_r, &req, sizeof(req)); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 178 | if(err != sizeof(req)){ |
| 179 | if(err < 0) |
| 180 | printk("not_aio_thread - read failed, " |
| 181 | "fd = %d, err = %d\n", aio_req_fd_r, |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 182 | errno); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 183 | else { |
| 184 | printk("not_aio_thread - short read, fd = %d, " |
| 185 | "length = %d\n", aio_req_fd_r, err); |
| 186 | } |
| 187 | continue; |
| 188 | } |
| 189 | err = do_not_aio(&req); |
| 190 | reply = ((struct aio_thread_reply) { .data = req.aio, |
Jeff Dike | ef0470c | 2007-05-06 14:51:33 -0700 | [diff] [blame] | 191 | .err = err }); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 192 | err = write(req.aio->reply_fd, &reply, sizeof(reply)); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 193 | if(err != sizeof(reply)) |
| 194 | printk("not_aio_thread - write failed, fd = %d, " |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 195 | "err = %d\n", req.aio->reply_fd, errno); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 196 | } |
Jeff Dike | 1b57e9c | 2006-01-06 00:18:49 -0800 | [diff] [blame] | 197 | |
| 198 | return 0; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 201 | static int init_aio_24(void) |
| 202 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 203 | int fds[2], err; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 204 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 205 | err = os_pipe(fds, 1, 1); |
| 206 | if(err) |
| 207 | goto out; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 208 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 209 | aio_req_fd_w = fds[0]; |
| 210 | aio_req_fd_r = fds[1]; |
Jeff Dike | 8603ec8 | 2007-05-06 14:51:44 -0700 | [diff] [blame] | 211 | |
| 212 | err = os_set_fd_block(aio_req_fd_w, 0); |
| 213 | if(err) |
| 214 | goto out_close_pipe; |
| 215 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 216 | err = run_helper_thread(not_aio_thread, NULL, |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 217 | CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 218 | if(err < 0) |
| 219 | goto out_close_pipe; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 220 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 221 | aio_pid = err; |
| 222 | goto out; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 223 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 224 | out_close_pipe: |
| 225 | os_close_file(fds[0]); |
| 226 | os_close_file(fds[1]); |
| 227 | aio_req_fd_w = -1; |
| 228 | aio_req_fd_r = -1; |
| 229 | out: |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 230 | #ifndef HAVE_AIO_ABI |
| 231 | printk("/usr/include/linux/aio_abi.h not present during build\n"); |
| 232 | #endif |
| 233 | printk("2.6 host AIO support not used - falling back to I/O " |
| 234 | "thread\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 235 | return 0; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | #ifdef HAVE_AIO_ABI |
| 239 | #define DEFAULT_24_AIO 0 |
| 240 | static int init_aio_26(void) |
| 241 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 242 | int err; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 243 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 244 | if(io_setup(256, &ctx)){ |
Jeff Dike | b4fd310 | 2005-09-16 19:27:49 -0700 | [diff] [blame] | 245 | err = -errno; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 246 | printk("aio_thread failed to initialize context, err = %d\n", |
| 247 | errno); |
| 248 | return err; |
| 249 | } |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 250 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 251 | err = run_helper_thread(aio_thread, NULL, |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 252 | CLONE_FILES | CLONE_VM | SIGCHLD, &aio_stack); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 253 | if(err < 0) |
| 254 | return err; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 255 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 256 | aio_pid = err; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 257 | |
| 258 | printk("Using 2.6 host AIO\n"); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 259 | return 0; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 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) |
| 264 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 265 | struct aio_thread_reply reply; |
| 266 | int err; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 267 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 268 | err = do_aio(ctx, type, io_fd, buf, len, offset, aio); |
| 269 | if(err){ |
| 270 | reply = ((struct aio_thread_reply) { .data = aio, |
| 271 | .err = err }); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 272 | err = write(aio->reply_fd, &reply, sizeof(reply)); |
| 273 | if(err != sizeof(reply)){ |
| 274 | err = -errno; |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 275 | printk("submit_aio_26 - write failed, " |
| 276 | "fd = %d, err = %d\n", aio->reply_fd, -err); |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 277 | } |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 278 | else err = 0; |
| 279 | } |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 280 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 281 | return err; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 282 | } |
| 283 | |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 284 | #else |
| 285 | #define DEFAULT_24_AIO 1 |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 286 | static int init_aio_26(void) |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 287 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 288 | return -ENOSYS; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 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) |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 293 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 294 | return -ENOSYS; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 295 | } |
| 296 | #endif |
| 297 | |
Jeff Dike | 9683da9 | 2007-02-10 01:44:27 -0800 | [diff] [blame] | 298 | /* Initialized in an initcall and unchanged thereafter */ |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 299 | static int aio_24 = DEFAULT_24_AIO; |
| 300 | |
| 301 | static int __init set_aio_24(char *name, int *add) |
| 302 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 303 | aio_24 = 1; |
| 304 | return 0; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | __uml_setup("aio=2.4", set_aio_24, |
| 308 | "aio=2.4\n" |
| 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" |
| 317 | ); |
| 318 | |
| 319 | static int init_aio(void) |
| 320 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 321 | int err; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 322 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 323 | CHOOSE_MODE(({ if(!aio_24){ |
| 324 | printk("Disabling 2.6 AIO in tt mode\n"); |
| 325 | aio_24 = 1; |
| 326 | } }), (void) 0); |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 327 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 328 | if(!aio_24){ |
| 329 | err = init_aio_26(); |
| 330 | if(err && (errno == ENOSYS)){ |
| 331 | printk("2.6 AIO not supported on the host - " |
| 332 | "reverting to 2.4 AIO\n"); |
| 333 | aio_24 = 1; |
| 334 | } |
| 335 | else return err; |
| 336 | } |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 337 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 338 | if(aio_24) |
| 339 | return init_aio_24(); |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 340 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 341 | return 0; |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 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. |
| 349 | */ |
| 350 | __initcall(init_aio); |
| 351 | |
| 352 | static void exit_aio(void) |
| 353 | { |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 354 | if (aio_pid != -1) { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 355 | os_kill_process(aio_pid, 1); |
Jeff Dike | c439901 | 2007-07-15 23:38:56 -0700 | [diff] [blame] | 356 | free_stack(aio_stack, 0); |
| 357 | } |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | __uml_exitcall(exit_aio); |
| 361 | |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 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) |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 364 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 365 | struct aio_thread_req req = { .type = type, |
| 366 | .io_fd = io_fd, |
| 367 | .offset = offset, |
| 368 | .buf = buf, |
| 369 | .len = len, |
| 370 | .aio = aio, |
| 371 | }; |
| 372 | int err; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 373 | |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 374 | err = write(aio_req_fd_w, &req, sizeof(req)); |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 375 | if(err == sizeof(req)) |
| 376 | err = 0; |
Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 377 | else err = -errno; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 378 | |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 379 | return err; |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | int submit_aio(enum aio_type type, int io_fd, char *buf, int len, |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 383 | unsigned long long offset, int reply_fd, |
| 384 | struct aio_context *aio) |
Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 385 | { |
Jeff Dike | d50084a | 2006-01-06 00:18:50 -0800 | [diff] [blame] | 386 | aio->reply_fd = reply_fd; |
| 387 | if(aio_24) |
| 388 | return submit_aio_24(type, io_fd, buf, len, offset, aio); |
| 389 | else { |
| 390 | return submit_aio_26(type, io_fd, buf, len, offset, aio); |
| 391 | } |
Jeff Dike | 75e5584 | 2005-09-03 15:57:45 -0700 | [diff] [blame] | 392 | } |