Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[linux-drm-fsl-dcu.git] / arch / um / os-Linux / aio.c
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"
13 #include "aio.h"
14 #include "init.h"
15 #include "user.h"
16 #include "mode.h"
17
18 struct aio_thread_req {
19         enum aio_type type;
20         int io_fd;
21         unsigned long long offset;
22         char *buf;
23         int len;
24         struct aio_context *aio;
25 };
26
27 #if defined(HAVE_AIO_ABI)
28 #include <linux/aio_abi.h>
29
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.
32  */
33
34 #if !defined(HAVE_AIO_LIBC)
35
36 static long io_setup(int n, aio_context_t *ctxp)
37 {
38         return syscall(__NR_io_setup, n, ctxp);
39 }
40
41 static long io_submit(aio_context_t ctx, long nr, struct iocb **iocbpp)
42 {
43         return syscall(__NR_io_submit, ctx, nr, iocbpp);
44 }
45
46 static long io_getevents(aio_context_t ctx_id, long min_nr, long nr,
47                          struct io_event *events, struct timespec *timeout)
48 {
49         return syscall(__NR_io_getevents, ctx_id, min_nr, nr, events, timeout);
50 }
51
52 #endif
53
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.
63  */
64
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)
67 {
68         struct iocb iocb, *iocbp = &iocb;
69         char c;
70         int err;
71
72         iocb = ((struct iocb) { .aio_data       = (unsigned long) aio,
73                                 .aio_reqprio    = 0,
74                                 .aio_fildes     = fd,
75                                 .aio_buf        = (unsigned long) buf,
76                                 .aio_nbytes     = len,
77                                 .aio_offset     = offset,
78                                 .aio_reserved1  = 0,
79                                 .aio_reserved2  = 0,
80                                 .aio_reserved3  = 0 });
81
82         switch(type){
83         case AIO_READ:
84                 iocb.aio_lio_opcode = IOCB_CMD_PREAD;
85                 err = io_submit(ctx, 1, &iocbp);
86                 break;
87         case AIO_WRITE:
88                 iocb.aio_lio_opcode = IOCB_CMD_PWRITE;
89                 err = io_submit(ctx, 1, &iocbp);
90                 break;
91         case AIO_MMAP:
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);
96                 break;
97         default:
98                 printk("Bogus op in do_aio - %d\n", type);
99                 err = -EINVAL;
100                 break;
101         }
102
103         if(err > 0)
104                 err = 0;
105         else
106                 err = -errno;
107
108         return err;
109 }
110
111 /* Initialized in an initcall and unchanged thereafter */
112 static aio_context_t ctx = 0;
113
114 static int aio_thread(void *arg)
115 {
116         struct aio_thread_reply reply;
117         struct io_event event;
118         int err, n, reply_fd;
119
120         signal(SIGWINCH, SIG_IGN);
121
122         while(1){
123                 n = io_getevents(ctx, 1, 1, &event, NULL);
124                 if(n < 0){
125                         if(errno == EINTR)
126                                 continue;
127                         printk("aio_thread - io_getevents failed, "
128                                "errno = %d\n", errno);
129                 }
130                 else {
131                         reply = ((struct aio_thread_reply)
132                                 { .data = (void *) (long) event.data,
133                                                 .err    = event.res });
134                         reply_fd = ((struct aio_context *) reply.data)->reply_fd;
135                         err = os_write_file(reply_fd, &reply, sizeof(reply));
136                         if(err != sizeof(reply))
137                                 printk("aio_thread - write failed, fd = %d, "
138                                        "err = %d\n", reply_fd, -err);
139                 }
140         }
141         return 0;
142 }
143
144 #endif
145
146 static int do_not_aio(struct aio_thread_req *req)
147 {
148         char c;
149         int err;
150
151         switch(req->type){
152         case AIO_READ:
153                 err = os_seek_file(req->io_fd, req->offset);
154                 if(err)
155                         goto out;
156
157                 err = os_read_file(req->io_fd, req->buf, req->len);
158                 break;
159         case AIO_WRITE:
160                 err = os_seek_file(req->io_fd, req->offset);
161                 if(err)
162                         goto out;
163
164                 err = os_write_file(req->io_fd, req->buf, req->len);
165                 break;
166         case AIO_MMAP:
167                 err = os_seek_file(req->io_fd, req->offset);
168                 if(err)
169                         goto out;
170
171                 err = os_read_file(req->io_fd, &c, sizeof(c));
172                 break;
173         default:
174                 printk("do_not_aio - bad request type : %d\n", req->type);
175                 err = -EINVAL;
176                 break;
177         }
178
179 out:
180         return err;
181 }
182
183 /* These are initialized in initcalls and not changed */
184 static int aio_req_fd_r = -1;
185 static int aio_req_fd_w = -1;
186 static int aio_pid = -1;
187
188 static int not_aio_thread(void *arg)
189 {
190         struct aio_thread_req req;
191         struct aio_thread_reply reply;
192         int err;
193
194         signal(SIGWINCH, SIG_IGN);
195         while(1){
196                 err = os_read_file(aio_req_fd_r, &req, sizeof(req));
197                 if(err != sizeof(req)){
198                         if(err < 0)
199                                 printk("not_aio_thread - read failed, "
200                                        "fd = %d, err = %d\n", aio_req_fd_r,
201                                        -err);
202                         else {
203                                 printk("not_aio_thread - short read, fd = %d, "
204                                        "length = %d\n", aio_req_fd_r, err);
205                         }
206                         continue;
207                 }
208                 err = do_not_aio(&req);
209                 reply = ((struct aio_thread_reply) { .data      = req.aio,
210                                          .err   = err });
211                 err = os_write_file(req.aio->reply_fd, &reply, sizeof(reply));
212                 if(err != sizeof(reply))
213                         printk("not_aio_thread - write failed, fd = %d, "
214                                "err = %d\n", req.aio->reply_fd, -err);
215         }
216
217         return 0;
218 }
219
220 static int init_aio_24(void)
221 {
222         unsigned long stack;
223         int fds[2], err;
224
225         err = os_pipe(fds, 1, 1);
226         if(err)
227                 goto out;
228
229         aio_req_fd_w = fds[0];
230         aio_req_fd_r = fds[1];
231         err = run_helper_thread(not_aio_thread, NULL,
232                                 CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
233         if(err < 0)
234                 goto out_close_pipe;
235
236         aio_pid = err;
237         goto out;
238
239 out_close_pipe:
240         os_close_file(fds[0]);
241         os_close_file(fds[1]);
242         aio_req_fd_w = -1;
243         aio_req_fd_r = -1;
244 out:
245 #ifndef HAVE_AIO_ABI
246         printk("/usr/include/linux/aio_abi.h not present during build\n");
247 #endif
248         printk("2.6 host AIO support not used - falling back to I/O "
249                "thread\n");
250         return 0;
251 }
252
253 #ifdef HAVE_AIO_ABI
254 #define DEFAULT_24_AIO 0
255 static int init_aio_26(void)
256 {
257         unsigned long stack;
258         int err;
259
260         if(io_setup(256, &ctx)){
261                 err = -errno;
262                 printk("aio_thread failed to initialize context, err = %d\n",
263                        errno);
264                 return err;
265         }
266
267         err = run_helper_thread(aio_thread, NULL,
268                                 CLONE_FILES | CLONE_VM | SIGCHLD, &stack, 0);
269         if(err < 0)
270                 return err;
271
272         aio_pid = err;
273
274         printk("Using 2.6 host AIO\n");
275         return 0;
276 }
277
278 static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
279                          unsigned long long offset, struct aio_context *aio)
280 {
281         struct aio_thread_reply reply;
282         int err;
283
284         err = do_aio(ctx, type, io_fd, buf, len, offset, aio);
285         if(err){
286                 reply = ((struct aio_thread_reply) { .data = aio,
287                                          .err  = err });
288                 err = os_write_file(aio->reply_fd, &reply, sizeof(reply));
289                 if(err != sizeof(reply))
290                         printk("submit_aio_26 - write failed, "
291                                "fd = %d, err = %d\n", aio->reply_fd, -err);
292                 else err = 0;
293         }
294
295         return err;
296 }
297
298 #else
299 #define DEFAULT_24_AIO 1
300 static int init_aio_26(void)
301 {
302         return -ENOSYS;
303 }
304
305 static int submit_aio_26(enum aio_type type, int io_fd, char *buf, int len,
306                          unsigned long long offset, struct aio_context *aio)
307 {
308         return -ENOSYS;
309 }
310 #endif
311
312 /* Initialized in an initcall and unchanged thereafter */
313 static int aio_24 = DEFAULT_24_AIO;
314
315 static int __init set_aio_24(char *name, int *add)
316 {
317         aio_24 = 1;
318         return 0;
319 }
320
321 __uml_setup("aio=2.4", set_aio_24,
322 "aio=2.4\n"
323 "    This is used to force UML to use 2.4-style AIO even when 2.6 AIO is\n"
324 "    available.  2.4 AIO is a single thread that handles one request at a\n"
325 "    time, synchronously.  2.6 AIO is a thread which uses the 2.6 AIO \n"
326 "    interface to handle an arbitrary number of pending requests.  2.6 AIO \n"
327 "    is not available in tt mode, on 2.4 hosts, or when UML is built with\n"
328 "    /usr/include/linux/aio_abi.h not available.  Many distributions don't\n"
329 "    include aio_abi.h, so you will need to copy it from a kernel tree to\n"
330 "    your /usr/include/linux in order to build an AIO-capable UML\n\n"
331 );
332
333 static int init_aio(void)
334 {
335         int err;
336
337         CHOOSE_MODE(({ if(!aio_24){
338                             printk("Disabling 2.6 AIO in tt mode\n");
339                             aio_24 = 1;
340                     } }), (void) 0);
341
342         if(!aio_24){
343                 err = init_aio_26();
344                 if(err && (errno == ENOSYS)){
345                         printk("2.6 AIO not supported on the host - "
346                                "reverting to 2.4 AIO\n");
347                         aio_24 = 1;
348                 }
349                 else return err;
350         }
351
352         if(aio_24)
353                 return init_aio_24();
354
355         return 0;
356 }
357
358 /* The reason for the __initcall/__uml_exitcall asymmetry is that init_aio
359  * needs to be called when the kernel is running because it calls run_helper,
360  * which needs get_free_page.  exit_aio is a __uml_exitcall because the generic
361  * kernel does not run __exitcalls on shutdown, and can't because many of them
362  * break when called outside of module unloading.
363  */
364 __initcall(init_aio);
365
366 static void exit_aio(void)
367 {
368         if(aio_pid != -1)
369                 os_kill_process(aio_pid, 1);
370 }
371
372 __uml_exitcall(exit_aio);
373
374 static int submit_aio_24(enum aio_type type, int io_fd, char *buf, int len,
375                          unsigned long long offset, struct aio_context *aio)
376 {
377         struct aio_thread_req req = { .type             = type,
378                                       .io_fd            = io_fd,
379                                       .offset           = offset,
380                                       .buf              = buf,
381                                       .len              = len,
382                                       .aio              = aio,
383         };
384         int err;
385
386         err = os_write_file(aio_req_fd_w, &req, sizeof(req));
387         if(err == sizeof(req))
388                 err = 0;
389
390         return err;
391 }
392
393 int submit_aio(enum aio_type type, int io_fd, char *buf, int len,
394                unsigned long long offset, int reply_fd,
395                struct aio_context *aio)
396 {
397         aio->reply_fd = reply_fd;
398         if(aio_24)
399                 return submit_aio_24(type, io_fd, buf, len, offset, aio);
400         else {
401                 return submit_aio_26(type, io_fd, buf, len, offset, aio);
402         }
403 }