uml: redo host capability detection and disabling
[safe/jmp/linux-2.6] / arch / um / os-Linux / aio.c
index f6e6402..57e3d46 100644 (file)
@@ -1,51 +1,35 @@
 /*
- * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com)
+ * Copyright (C) 2004 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  * Licensed under the GPL
  */
 
-#include <stdlib.h>
 #include <unistd.h>
+#include <sched.h>
 #include <signal.h>
-#include <string.h>
 #include <errno.h>
-#include <sched.h>
-#include <sys/syscall.h>
-#include "os.h"
-#include "helper.h"
+#include <sys/time.h>
+#include <asm/unistd.h>
 #include "aio.h"
 #include "init.h"
+#include "kern_constants.h"
+#include "kern_util.h"
+#include "os.h"
 #include "user.h"
-#include "mode.h"
-
-static int aio_req_fd_r = -1;
-static int aio_req_fd_w = -1;
 
-static int update_aio(struct aio_context *aio, int res)
-{
-        if(res < 0)
-                aio->len = res;
-        else if((res == 0) && (aio->type == AIO_READ)){
-                /* This is the EOF case - we have hit the end of the file
-                 * and it ends in a partial block, so we fill the end of
-                 * the block with zeros and claim success.
-                 */
-                memset(aio->data, 0, aio->len);
-                aio->len = 0;
-        }
-        else if(res > 0){
-                aio->len -= res;
-                aio->data += res;
-                aio->offset += res;
-                return aio->len;
-        }
-
-        return 0;
-}
+struct aio_thread_req {
+       enum aio_type type;
+       int io_fd;
+       unsigned long long offset;
+       char *buf;
+       int len;
+       struct aio_context *aio;
+};
 
 #if defined(HAVE_AIO_ABI)
 #include <linux/aio_abi.h>
 
-/* If we have the headers, we are going to build with AIO enabled.
+/*
+ * If we have the headers, we are going to build with AIO enabled.
  * If we don't have aio in libc, we define the necessary stubs here.
  */
 
@@ -53,23 +37,24 @@ static int update_aio(struct aio_context *aio, int res)
 
 static long io_setup(int n, aio_context_t *ctxp)
 {
-        return syscall(__NR_io_setup, n, ctxp);
+       return syscall(__NR_io_setup, n, ctxp);
 }
 
 static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp)
 {
-        return syscall(__NR_io_submit, ctx, nr, iocbpp);
+       return syscall(__NR_io_submit, ctx, nr, iocbpp);
 }
 
 static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
-                         struct io_event *events, struct timespec *timeout)
+                        struct io_event *events, struct timespec *timeout)
 {
-        return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
+       return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
 }
 
 #endif
 
-/* The AIO_MMAP cases force the mmapped page into memory here
+/*
+ * The AIO_MMAP cases force the mmapped page into memory here
  * rather than in whatever place first touches the data.  I used
  * to do this by touching the page, but that's delicate because
  * gcc is prone to optimizing that away.  So, what's done here
@@ -80,282 +65,249 @@ static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
  * that it now backs the mmapped area.
  */
 
-static int do_aio(aio_context_t ctx, struct aio_context *aio)
+static int do_aio(aio_context_t ctx, enum aio_type type, int fd, char *buf,
+                 int len, unsigned long long offset, struct aio_context *aio)
 {
-        struct iocb iocb, *iocbp = &iocb;
-        char c;
-        int err;
-
-        iocb = ((struct iocb) { .aio_data      = (unsigned long) aio,
-                                .aio_reqprio   = 0,
-                                .aio_fildes    = aio->fd,
-                                .aio_buf       = (unsigned long) aio->data,
-                                .aio_nbytes    = aio->len,
-                                .aio_offset    = aio->offset,
-                                .aio_reserved1 = 0,
-                                .aio_reserved2 = 0,
-                                .aio_reserved3 = 0 });
-
-        switch(aio->type){
-        case AIO_READ:
-                iocb.aio_lio_opcode = IOCB_CMD_PREAD;
-                break;
-        case AIO_WRITE:
-                iocb.aio_lio_opcode = IOCB_CMD_PWRITE;
-                break;
-        case AIO_MMAP:
-                iocb.aio_lio_opcode = IOCB_CMD_PREAD;
-                iocb.aio_buf = (unsigned long) &c;
-                iocb.aio_nbytes = sizeof(c);
-                break;
-        default:
-                printk("Bogus op in do_aio - %d\n", aio->type);
-                err = -EINVAL;
-                goto out;
-        }
-
-        err = io_submit(ctx, 1, &iocbp);
-        if(err > 0)
-                err = 0;
-       else
-               err = -errno;
+       struct iocb *iocbp = & ((struct iocb) {
+                                   .aio_data       = (unsigned long) aio,
+                                   .aio_fildes     = fd,
+                                   .aio_buf        = (unsigned long) buf,
+                                   .aio_nbytes     = len,
+                                   .aio_offset     = offset
+                            });
+       char c;
+
+       switch (type) {
+       case AIO_READ:
+               iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
+               break;
+       case AIO_WRITE:
+               iocbp->aio_lio_opcode = IOCB_CMD_PWRITE;
+               break;
+       case AIO_MMAP:
+               iocbp->aio_lio_opcode = IOCB_CMD_PREAD;
+               iocbp->aio_buf = (unsigned long) &c;
+               iocbp->aio_nbytes = sizeof(c);
+               break;
+       default:
+               printk(UM_KERN_ERR "Bogus op in do_aio - %d\n", type);
+               return -EINVAL;
+       }
 
- out:
-        return err;
+       return (io_submit(ctx, 1, &iocbp) > 0) ? 0 : -errno;
 }
 
+/* Initialized in an initcall and unchanged thereafter */
 static aio_context_t ctx = 0;
 
 static int aio_thread(void *arg)
 {
-        struct aio_thread_reply reply;
-        struct aio_context *aio;
-        struct io_event event;
-        int err, n;
-
-        signal(SIGWINCH, SIG_IGN);
-
-        while(1){
-                n = io_getevents(ctx, 1, 1, &event, NULL);
-                if(n < 0){
-                        if(errno == EINTR)
-                                continue;
-                        printk("aio_thread - io_getevents failed, "
-                               "errno = %d\n", errno);
-                }
-                else {
-                       /* This is safe as we've just a pointer here. */
-                       aio = (struct aio_context *) (long) event.data;
-                       if(update_aio(aio, event.res)){
-                               do_aio(ctx, aio);
-                               continue;
-                       }
+       struct aio_thread_reply reply;
+       struct io_event event;
+       int err, n, reply_fd;
+
+       signal(SIGWINCH, SIG_IGN);
 
-                        reply = ((struct aio_thread_reply)
-                               { .data = aio,
-                                 .err  = aio->len });
-                       err = os_write_file(aio->reply_fd, &reply,
-                                           sizeof(reply));
-                        if(err != sizeof(reply))
-                               printk("aio_thread - write failed, "
-                                      "fd = %d, err = %d\n", aio->reply_fd,
-                                      -err);
-                }
-        }
-        return 0;
+       while (1) {
+               n = io_getevents(ctx, 1, 1, &event, NULL);
+               if (n < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       printk(UM_KERN_ERR "aio_thread - io_getevents failed, "
+                              "errno = %d\n", errno);
+               }
+               else {
+                       reply = ((struct aio_thread_reply)
+                               { .data = (void *) (long) event.data,
+                                               .err    = event.res });
+                       reply_fd = ((struct aio_context *) reply.data)->reply_fd;
+                       err = write(reply_fd, &reply, sizeof(reply));
+                       if (err != sizeof(reply))
+                               printk(UM_KERN_ERR "aio_thread - write failed, "
+                                      "fd = %d, err = %d\n", reply_fd, errno);
+               }
+       }
+       return 0;
 }
 
 #endif
 
-static int do_not_aio(struct aio_context *aio)
+static int do_not_aio(struct aio_thread_req *req)
 {
-        char c;
-        int err;
-
-        switch(aio->type){
-        case AIO_READ:
-                err = os_seek_file(aio->fd, aio->offset);
-                if(err)
-                        goto out;
-
-                err = os_read_file(aio->fd, aio->data, aio->len);
-                break;
-        case AIO_WRITE:
-                err = os_seek_file(aio->fd, aio->offset);
-                if(err)
-                        goto out;
-
-                err = os_write_file(aio->fd, aio->data, aio->len);
-                break;
-        case AIO_MMAP:
-                err = os_seek_file(aio->fd, aio->offset);
-                if(err)
-                        goto out;
-
-                err = os_read_file(aio->fd, &c, sizeof(c));
-                break;
-        default:
-                printk("do_not_aio - bad request type : %d\n", aio->type);
-                err = -EINVAL;
-                break;
-        }
-
- out:
-        return err;
+       char c;
+       unsigned long long actual;
+       int n;
+
+       actual = lseek64(req->io_fd, req->offset, SEEK_SET);
+       if (actual != req->offset)
+               return -errno;
+
+       switch (req->type) {
+       case AIO_READ:
+               n = read(req->io_fd, req->buf, req->len);
+               break;
+       case AIO_WRITE:
+               n = write(req->io_fd, req->buf, req->len);
+               break;
+       case AIO_MMAP:
+               n = read(req->io_fd, &c, sizeof(c));
+               break;
+       default:
+               printk(UM_KERN_ERR "do_not_aio - bad request type : %d\n",
+                      req->type);
+               return -EINVAL;
+       }
+
+       if (n < 0)
+               return -errno;
+       return 0;
 }
 
+/* These are initialized in initcalls and not changed */
+static int aio_req_fd_r = -1;
+static int aio_req_fd_w = -1;
+static int aio_pid = -1;
+static unsigned long aio_stack;
+
 static int not_aio_thread(void *arg)
 {
-        struct aio_context *aio;
-        struct aio_thread_reply reply;
-        int err;
-
-        signal(SIGWINCH, SIG_IGN);
-        while(1){
-                err = os_read_file(aio_req_fd_r, &aio, sizeof(aio));
-                if(err != sizeof(aio)){
-                        if(err < 0)
-                                printk("not_aio_thread - read failed, "
-                                       "fd = %d, err = %d\n", aio_req_fd_r,
-                                       -err);
-                        else {
-                                printk("not_aio_thread - short read, fd = %d, "
-                                       "length = %d\n", aio_req_fd_r, err);
-                        }
-                        continue;
-                }
- again:
-                err = do_not_aio(aio);
-
-                if(update_aio(aio, err))
-                        goto again;
-
-                reply = ((struct aio_thread_reply) { .data     = aio,
-                                                     .err      = aio->len });
-                err = os_write_file(aio->reply_fd, &reply, sizeof(reply));
-                if(err != sizeof(reply))
-                        printk("not_aio_thread - write failed, fd = %d, "
-                               "err = %d\n", aio_req_fd_r, -err);
-        }
+       struct aio_thread_req req;
+       struct aio_thread_reply reply;
+       int err;
+
+       signal(SIGWINCH, SIG_IGN);
+       while (1) {
+               err = read(aio_req_fd_r, &req, sizeof(req));
+               if (err != sizeof(req)) {
+                       if (err < 0)
+                               printk(UM_KERN_ERR "not_aio_thread - "
+                                      "read failed, fd = %d, err = %d\n",
+                                      aio_req_fd_r,
+                                      errno);
+                       else {
+                               printk(UM_KERN_ERR "not_aio_thread - short "
+                                      "read, fd = %d, length = %d\n",
+                                      aio_req_fd_r, err);
+                       }
+                       continue;
+               }
+               err = do_not_aio(&req);
+               reply = ((struct aio_thread_reply) { .data      = req.aio,
+                                                    .err       = err });
+               err = write(req.aio->reply_fd, &reply, sizeof(reply));
+               if (err != sizeof(reply))
+                       printk(UM_KERN_ERR "not_aio_thread - write failed, "
+                              "fd = %d, err = %d\n", req.aio->reply_fd, errno);
+       }
+
+       return 0;
 }
 
-static int submit_aio_24(struct aio_context *aio)
+static int init_aio_24(void)
 {
-        int err;
+       int fds[2], err;
 
-        err = os_write_file(aio_req_fd_w, &aio, sizeof(aio));
-        if(err == sizeof(aio))
-                err = 0;
+       err = os_pipe(fds, 1, 1);
+       if (err)
+               goto out;
 
-        return err;
-}
+       aio_req_fd_w = fds[0];
+       aio_req_fd_r = fds[1];
 
-static int aio_pid = -1;
-static int (*submit_proc)(struct aio_context *aio);
+       err = os_set_fd_block(aio_req_fd_w, 0);
+       if (err)
+               goto out_close_pipe;
 
-static int init_aio_24(void)
-{
-        unsigned long stack;
-        int fds[2], err;
-
-        err = os_pipe(fds, 1, 1);
-        if(err)
-                goto out;
-
-        aio_req_fd_w = fds[0];
-        aio_req_fd_r = fds[1];
-        err = run_helper_thread(not_aio_thread, NULL,
-                                CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
-        if(err < 0)
-                goto out_close_pipe;
-
-        aio_pid = err;
-        goto out;
-
- out_close_pipe:
-        os_close_file(fds[0]);
-        os_close_file(fds[1]);
-        aio_req_fd_w = -1;
-        aio_req_fd_r = -1;
- out:
-#ifndef HAVE_AIO_ABI
-       printk("/usr/include/linux/aio_abi.h not present during build\n");
-#endif
-       printk("2.6 host AIO support not used - falling back to I/O "
-              "thread\n");
+       err = run_helper_thread(not_aio_thread, NULL,
+                               CLONE_FILES | CLONE_VM, &aio_stack);
+       if (err < 0)
+               goto out_close_pipe;
 
-       submit_proc = submit_aio_24;
+       aio_pid = err;
+       goto out;
 
-        return 0;
+out_close_pipe:
+       close(fds[0]);
+       close(fds[1]);
+       aio_req_fd_w = -1;
+       aio_req_fd_r = -1;
+out:
+#ifndef HAVE_AIO_ABI
+       printk(UM_KERN_INFO "/usr/include/linux/aio_abi.h not present during "
+              "build\n");
+#endif
+       printk(UM_KERN_INFO "2.6 host AIO support not used - falling back to "
+              "I/O thread\n");
+       return 0;
 }
 
 #ifdef HAVE_AIO_ABI
 #define DEFAULT_24_AIO 0
-static int submit_aio_26(struct aio_context *aio)
+static int init_aio_26(void)
 {
-       struct aio_thread_reply reply;
        int err;
 
-       err = do_aio(ctx, aio);
-       if(err){
-               reply = ((struct aio_thread_reply) { .data = aio,
-                                                    .err  = err });
-               err = os_write_file(aio->reply_fd, &reply, sizeof(reply));
-               if(err != sizeof(reply))
-                       printk("submit_aio_26 - write failed, "
-                              "fd = %d, err = %d\n", aio->reply_fd, -err);
-               else err = 0;
+       if (io_setup(256, &ctx)) {
+               err = -errno;
+               printk(UM_KERN_ERR "aio_thread failed to initialize context, "
+                      "err = %d\n", errno);
+               return err;
        }
 
-       return err;
-}
-
-static int init_aio_26(void)
-{
-        unsigned long stack;
-        int err;
+       err = run_helper_thread(aio_thread, NULL,
+                               CLONE_FILES | CLONE_VM, &aio_stack);
+       if (err < 0)
+               return err;
 
-        if(io_setup(256, &ctx)){
-               err = -errno;
-                printk("aio_thread failed to initialize context, err = %d\n",
-                       errno);
-                return err;
-        }
+       aio_pid = err;
 
-        err = run_helper_thread(aio_thread, NULL,
-                                CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
-        if(err < 0)
-                return err;
-
-        aio_pid = err;
+       printk(UM_KERN_INFO "Using 2.6 host AIO\n");
+       return 0;
+}
 
-       printk("Using 2.6 host AIO\n");
+static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
+                        unsigned long long offset, struct aio_context *aio)
+{
+       struct aio_thread_reply reply;
+       int err;
 
-       submit_proc = submit_aio_26;
+       err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
+       if (err) {
+               reply = ((struct aio_thread_reply) { .data = aio,
+                                        .err  = err });
+               err = write(aio->reply_fd, &reply, sizeof(reply));
+               if (err != sizeof(reply)) {
+                       err = -errno;
+                       printk(UM_KERN_ERR "submit_aio_26 - write failed, "
+                              "fd = %d, err = %d\n", aio->reply_fd, -err);
+               }
+               else err = 0;
+       }
 
-        return 0;
+       return err;
 }
 
 #else
 #define DEFAULT_24_AIO 1
-static int submit_aio_26(struct aio_context *aio)
+static int init_aio_26(void)
 {
-        return -ENOSYS;
+       return -ENOSYS;
 }
 
-static int init_aio_26(void)
+static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
+                        unsigned long long offset, struct aio_context *aio)
 {
-       submit_proc = submit_aio_26;
-        return -ENOSYS;
+       return -ENOSYS;
 }
 #endif
 
+/* Initialized in an initcall and unchanged thereafter */
 static int aio_24 = DEFAULT_24_AIO;
 
 static int __init set_aio_24(char *name, int *add)
 {
-        aio_24 = 1;
-        return 0;
+       aio_24 = 1;
+       return 0;
 }
 
 __uml_setup("aio=2.4", set_aio_24,
@@ -372,31 +324,26 @@ __uml_setup("aio=2.4", set_aio_24,
 
 static int init_aio(void)
 {
-        int err;
-
-        CHOOSE_MODE(({
-                if(!aio_24){
-                        printk("Disabling 2.6 AIO in tt mode\n");
-                        aio_24 = 1;
-                } }), (void) 0);
-
-        if(!aio_24){
-                err = init_aio_26();
-                if(err && (errno == ENOSYS)){
-                        printk("2.6 AIO not supported on the host - "
-                               "reverting to 2.4 AIO\n");
-                        aio_24 = 1;
-                }
-                else return err;
-        }
-
-        if(aio_24)
-                return init_aio_24();
-
-        return 0;
+       int err;
+
+       if (!aio_24) {
+               err = init_aio_26();
+               if (err && (errno == ENOSYS)) {
+                       printk(UM_KERN_INFO "2.6 AIO not supported on the "
+                              "host - reverting to 2.4 AIO\n");
+                       aio_24 = 1;
+               }
+               else return err;
+       }
+
+       if (aio_24)
+               return init_aio_24();
+
+       return 0;
 }
 
-/* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
+/*
+ * The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
  * needs to be called when the kernel is running because it calls run_helper,
  * which needs get_free_page.  exit_aio is a __uml_exitcall because the generic
  * kernel does not run __exitcalls on shutdown, and can't because many of them
@@ -406,13 +353,41 @@ __initcall(init_aio);
 
 static void exit_aio(void)
 {
-        if(aio_pid != -1)
-                os_kill_process(aio_pid, 1);
+       if (aio_pid != -1) {
+               os_kill_process(aio_pid, 1);
+               free_stack(aio_stack, 0);
+       }
 }
 
 __uml_exitcall(exit_aio);
 
-int submit_aio(struct aio_context *aio)
+static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
+                        unsigned long long offset, struct aio_context *aio)
+{
+       struct aio_thread_req req = { .type             = type,
+                                     .io_fd            = io_fd,
+                                     .offset           = offset,
+                                     .buf              = buf,
+                                     .len              = len,
+                                     .aio              = aio,
+       };
+       int err;
+
+       err = write(aio_req_fd_w, &req, sizeof(req));
+       if (err == sizeof(req))
+               err = 0;
+       else err = -errno;
+
+       return err;
+}
+
+int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
+              unsigned long long offset, int reply_fd,
+              struct aio_context *aio)
 {
-       return (*submit_proc)(aio);
+       aio->reply_fd = reply_fd;
+       if (aio_24)
+               return submit_aio_24(type, io_fd, buf, len, offset, aio);
+       else
+               return submit_aio_26(type, io_fd, buf, len, offset, aio);
 }