[PATCH] remove kiobufs
[linux-2.6/history.git] / fs / aio.c
blob6b51c1316ab234ab17c01727f9d24747f448d501
1 /*
2 * An async IO implementation for Linux
3 * Written by Benjamin LaHaise <bcrl@redhat.com>
5 * Implements an efficient asynchronous io interface.
7 * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
9 * See ../COPYING for licensing terms.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/time.h>
15 #include <linux/aio_abi.h>
16 #include <linux/module.h>
18 //#define DEBUG 1
20 #include <linux/sched.h>
21 #include <linux/fs.h>
22 #include <linux/file.h>
23 #include <linux/mm.h>
24 #include <linux/mman.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/aio.h>
28 #include <linux/module.h>
29 #include <linux/highmem.h>
30 #include <linux/workqueue.h>
32 #include <asm/kmap_types.h>
33 #include <asm/uaccess.h>
34 #include <asm/mmu_context.h>
36 #if DEBUG > 1
37 #define dprintk printk
38 #else
39 #define dprintk(x...) do { ; } while (0)
40 #endif
42 /*------ sysctl variables----*/
43 atomic_t aio_nr = ATOMIC_INIT(0); /* current system wide number of aio requests */
44 unsigned aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
45 /*----end sysctl variables---*/
47 static kmem_cache_t *kiocb_cachep;
48 static kmem_cache_t *kioctx_cachep;
50 static struct workqueue_struct *aio_wq;
52 /* Used for rare fput completion. */
53 static void aio_fput_routine(void *);
54 static DECLARE_WORK(fput_work, aio_fput_routine, NULL);
56 static spinlock_t fput_lock = SPIN_LOCK_UNLOCKED;
57 LIST_HEAD(fput_head);
59 static void aio_kick_handler(void *);
61 /* aio_setup
62 * Creates the slab caches used by the aio routines, panic on
63 * failure as this is done early during the boot sequence.
65 static int __init aio_setup(void)
67 kiocb_cachep = kmem_cache_create("kiocb", sizeof(struct kiocb),
68 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
69 if (!kiocb_cachep)
70 panic("unable to create kiocb cache\n");
72 kioctx_cachep = kmem_cache_create("kioctx", sizeof(struct kioctx),
73 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
74 if (!kioctx_cachep)
75 panic("unable to create kioctx cache");
77 aio_wq = create_workqueue("aio");
79 printk(KERN_NOTICE "aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
81 return 0;
84 static void aio_free_ring(struct kioctx *ctx)
86 struct aio_ring_info *info = &ctx->ring_info;
87 long i;
89 for (i=0; i<info->nr_pages; i++)
90 put_page(info->ring_pages[i]);
92 if (info->mmap_size) {
93 down_write(&ctx->mm->mmap_sem);
94 do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
95 up_write(&ctx->mm->mmap_sem);
98 if (info->ring_pages && info->ring_pages != info->internal_pages)
99 kfree(info->ring_pages);
100 info->ring_pages = NULL;
101 info->nr = 0;
104 static int aio_setup_ring(struct kioctx *ctx)
106 struct aio_ring *ring;
107 struct aio_ring_info *info = &ctx->ring_info;
108 unsigned nr_events = ctx->max_reqs;
109 unsigned long size;
110 int nr_pages;
112 /* Compensate for the ring buffer's head/tail overlap entry */
113 nr_events += 2; /* 1 is required, 2 for good luck */
115 size = sizeof(struct aio_ring);
116 size += sizeof(struct io_event) * nr_events;
117 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
119 if (nr_pages < 0)
120 return -EINVAL;
122 info->nr_pages = nr_pages;
124 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
126 info->nr = 0;
127 info->ring_pages = info->internal_pages;
128 if (nr_pages > AIO_RING_PAGES) {
129 info->ring_pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_KERNEL);
130 if (!info->ring_pages)
131 return -ENOMEM;
132 memset(info->ring_pages, 0, sizeof(struct page *) * nr_pages);
135 info->mmap_size = nr_pages * PAGE_SIZE;
136 dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
137 down_write(&ctx->mm->mmap_sem);
138 info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
139 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
141 if (IS_ERR((void *)info->mmap_base)) {
142 up_write(&ctx->mm->mmap_sem);
143 printk("mmap err: %ld\n", -info->mmap_base);
144 info->mmap_size = 0;
145 aio_free_ring(ctx);
146 return -EAGAIN;
149 dprintk("mmap address: 0x%08lx\n", info->mmap_base);
150 info->nr_pages = get_user_pages(current, ctx->mm,
151 info->mmap_base, info->mmap_size,
152 1, 0, info->ring_pages, NULL);
153 up_write(&ctx->mm->mmap_sem);
155 if (unlikely(info->nr_pages != nr_pages)) {
156 aio_free_ring(ctx);
157 return -EAGAIN;
160 ctx->user_id = info->mmap_base;
162 info->nr = nr_events; /* trusted copy */
164 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
165 ring->nr = nr_events; /* user copy */
166 ring->id = ctx->user_id;
167 ring->head = ring->tail = 0;
168 ring->magic = AIO_RING_MAGIC;
169 ring->compat_features = AIO_RING_COMPAT_FEATURES;
170 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
171 ring->header_length = sizeof(struct aio_ring);
172 kunmap_atomic(ring, KM_USER0);
174 return 0;
178 /* aio_ring_event: returns a pointer to the event at the given index from
179 * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
181 #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
182 #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
183 #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
185 #define aio_ring_event(info, nr, km) ({ \
186 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
187 struct io_event *__event; \
188 __event = kmap_atomic( \
189 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
190 __event += pos % AIO_EVENTS_PER_PAGE; \
191 __event; \
194 #define put_aio_ring_event(event, km) do { \
195 struct io_event *__event = (event); \
196 (void)__event; \
197 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
198 } while(0)
200 /* ioctx_alloc
201 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
203 static struct kioctx *ioctx_alloc(unsigned nr_events)
205 struct mm_struct *mm;
206 struct kioctx *ctx;
208 /* Prevent overflows */
209 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
210 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
211 pr_debug("ENOMEM: nr_events too high\n");
212 return ERR_PTR(-EINVAL);
215 if (nr_events > aio_max_nr)
216 return ERR_PTR(-EAGAIN);
218 ctx = kmem_cache_alloc(kioctx_cachep, GFP_KERNEL);
219 if (!ctx)
220 return ERR_PTR(-ENOMEM);
222 memset(ctx, 0, sizeof(*ctx));
223 ctx->max_reqs = nr_events;
224 mm = ctx->mm = current->mm;
225 atomic_inc(&mm->mm_count);
227 atomic_set(&ctx->users, 1);
228 spin_lock_init(&ctx->ctx_lock);
229 spin_lock_init(&ctx->ring_info.ring_lock);
230 init_waitqueue_head(&ctx->wait);
232 INIT_LIST_HEAD(&ctx->active_reqs);
233 INIT_LIST_HEAD(&ctx->run_list);
234 INIT_WORK(&ctx->wq, aio_kick_handler, ctx);
236 if (aio_setup_ring(ctx) < 0)
237 goto out_freectx;
239 /* limit the number of system wide aios */
240 atomic_add(ctx->max_reqs, &aio_nr); /* undone by __put_ioctx */
241 if (unlikely(atomic_read(&aio_nr) > aio_max_nr))
242 goto out_cleanup;
244 /* now link into global list. kludge. FIXME */
245 write_lock(&mm->ioctx_list_lock);
246 ctx->next = mm->ioctx_list;
247 mm->ioctx_list = ctx;
248 write_unlock(&mm->ioctx_list_lock);
250 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
251 ctx, ctx->user_id, current->mm, ctx->ring_info.ring->nr);
252 return ctx;
254 out_cleanup:
255 atomic_sub(ctx->max_reqs, &aio_nr); /* undone by __put_ioctx */
256 ctx->max_reqs = 0; /* prevent __put_ioctx from sub'ing aio_nr */
257 __put_ioctx(ctx);
258 return ERR_PTR(-EAGAIN);
260 out_freectx:
261 kmem_cache_free(kioctx_cachep, ctx);
262 ctx = ERR_PTR(-ENOMEM);
264 dprintk("aio: error allocating ioctx %p\n", ctx);
265 return ctx;
268 /* aio_cancel_all
269 * Cancels all outstanding aio requests on an aio context. Used
270 * when the processes owning a context have all exited to encourage
271 * the rapid destruction of the kioctx.
273 static void aio_cancel_all(struct kioctx *ctx)
275 int (*cancel)(struct kiocb *, struct io_event *);
276 struct io_event res;
277 spin_lock_irq(&ctx->ctx_lock);
278 ctx->dead = 1;
279 while (!list_empty(&ctx->active_reqs)) {
280 struct list_head *pos = ctx->active_reqs.next;
281 struct kiocb *iocb = list_kiocb(pos);
282 list_del_init(&iocb->ki_list);
283 cancel = iocb->ki_cancel;
284 if (cancel)
285 iocb->ki_users++;
286 spin_unlock_irq(&ctx->ctx_lock);
287 if (cancel)
288 cancel(iocb, &res);
289 spin_lock_irq(&ctx->ctx_lock);
291 spin_unlock_irq(&ctx->ctx_lock);
294 void wait_for_all_aios(struct kioctx *ctx)
296 struct task_struct *tsk = current;
297 DECLARE_WAITQUEUE(wait, tsk);
299 if (!ctx->reqs_active)
300 return;
302 add_wait_queue(&ctx->wait, &wait);
303 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
304 while (ctx->reqs_active) {
305 printk("ctx->reqs_active = %d\n", ctx->reqs_active);
306 schedule();
307 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
309 __set_task_state(tsk, TASK_RUNNING);
310 remove_wait_queue(&ctx->wait, &wait);
313 /* wait_on_sync_kiocb:
314 * Waits on the given sync kiocb to complete.
316 ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
318 while (iocb->ki_users) {
319 set_current_state(TASK_UNINTERRUPTIBLE);
320 if (!iocb->ki_users)
321 break;
322 schedule();
324 __set_current_state(TASK_RUNNING);
325 return iocb->ki_user_data;
328 /* exit_aio: called when the last user of mm goes away. At this point,
329 * there is no way for any new requests to be submited or any of the
330 * io_* syscalls to be called on the context. However, there may be
331 * outstanding requests which hold references to the context; as they
332 * go away, they will call put_ioctx and release any pinned memory
333 * associated with the request (held via struct page * references).
335 void exit_aio(struct mm_struct *mm)
337 struct kioctx *ctx = mm->ioctx_list;
338 mm->ioctx_list = NULL;
339 while (ctx) {
340 struct kioctx *next = ctx->next;
341 ctx->next = NULL;
342 aio_cancel_all(ctx);
344 wait_for_all_aios(ctx);
346 if (1 != atomic_read(&ctx->users))
347 printk(KERN_DEBUG
348 "exit_aio:ioctx still alive: %d %d %d\n",
349 atomic_read(&ctx->users), ctx->dead,
350 ctx->reqs_active);
351 put_ioctx(ctx);
352 ctx = next;
356 /* __put_ioctx
357 * Called when the last user of an aio context has gone away,
358 * and the struct needs to be freed.
360 void __put_ioctx(struct kioctx *ctx)
362 unsigned nr_events = ctx->max_reqs;
364 if (unlikely(ctx->reqs_active))
365 BUG();
367 aio_free_ring(ctx);
368 mmdrop(ctx->mm);
369 ctx->mm = NULL;
370 pr_debug("__put_ioctx: freeing %p\n", ctx);
371 kmem_cache_free(kioctx_cachep, ctx);
373 atomic_sub(nr_events, &aio_nr);
376 /* aio_get_req
377 * Allocate a slot for an aio request. Increments the users count
378 * of the kioctx so that the kioctx stays around until all requests are
379 * complete. Returns -EAGAIN if no requests are free.
381 static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
382 static struct kiocb *__aio_get_req(struct kioctx *ctx)
384 struct kiocb *req = NULL;
385 struct aio_ring *ring;
386 int okay = 0;
388 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
389 if (unlikely(!req))
390 return NULL;
392 req->ki_flags = 1 << KIF_LOCKED;
393 req->ki_users = 1;
394 req->ki_key = 0;
395 req->ki_ctx = ctx;
396 req->ki_cancel = NULL;
397 req->ki_retry = NULL;
398 req->ki_user_obj = NULL;
400 /* Check if the completion queue has enough free space to
401 * accept an event from this io.
403 spin_lock_irq(&ctx->ctx_lock);
404 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
405 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
406 list_add(&req->ki_list, &ctx->active_reqs);
407 get_ioctx(ctx);
408 ctx->reqs_active++;
409 req->ki_user_obj = NULL;
410 req->ki_ctx = ctx;
411 req->ki_users = 1;
412 } else {
413 kmem_cache_free(kiocb_cachep, req);
414 okay = 1;
416 kunmap_atomic(ring, KM_USER0);
417 spin_unlock_irq(&ctx->ctx_lock);
419 if (!okay) {
420 kmem_cache_free(kiocb_cachep, req);
421 req = NULL;
424 return req;
427 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
429 struct kiocb *req;
430 /* Handle a potential starvation case -- should be exceedingly rare as
431 * requests will be stuck on fput_head only if the aio_fput_routine is
432 * delayed and the requests were the last user of the struct file.
434 req = __aio_get_req(ctx);
435 if (unlikely(NULL == req)) {
436 aio_fput_routine(NULL);
437 req = __aio_get_req(ctx);
439 return req;
442 static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
444 req->ki_ctx = NULL;
445 req->ki_filp = NULL;
446 req->ki_user_obj = NULL;
447 kmem_cache_free(kiocb_cachep, req);
448 ctx->reqs_active--;
450 if (unlikely(!ctx->reqs_active && ctx->dead))
451 wake_up(&ctx->wait);
454 static void aio_fput_routine(void *data)
456 spin_lock_irq(&fput_lock);
457 while (likely(!list_empty(&fput_head))) {
458 struct kiocb *req = list_kiocb(fput_head.next);
459 struct kioctx *ctx = req->ki_ctx;
461 list_del(&req->ki_list);
462 spin_unlock_irq(&fput_lock);
464 /* Complete the fput */
465 __fput(req->ki_filp);
467 /* Link the iocb into the context's free list */
468 spin_lock_irq(&ctx->ctx_lock);
469 really_put_req(ctx, req);
470 spin_unlock_irq(&ctx->ctx_lock);
472 put_ioctx(ctx);
473 spin_lock_irq(&fput_lock);
475 spin_unlock_irq(&fput_lock);
478 /* __aio_put_req
479 * Returns true if this put was the last user of the request.
481 static inline int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
483 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
484 req, atomic_read(&req->ki_filp->f_count));
486 req->ki_users --;
487 if (unlikely(req->ki_users < 0))
488 BUG();
489 if (likely(req->ki_users))
490 return 0;
491 list_del(&req->ki_list); /* remove from active_reqs */
492 req->ki_cancel = NULL;
493 req->ki_retry = NULL;
495 /* Must be done under the lock to serialise against cancellation.
496 * Call this aio_fput as it duplicates fput via the fput_work.
498 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
499 get_ioctx(ctx);
500 spin_lock(&fput_lock);
501 list_add(&req->ki_list, &fput_head);
502 spin_unlock(&fput_lock);
503 queue_work(aio_wq, &fput_work);
504 } else
505 really_put_req(ctx, req);
506 return 1;
509 /* aio_put_req
510 * Returns true if this put was the last user of the kiocb,
511 * false if the request is still in use.
513 int aio_put_req(struct kiocb *req)
515 struct kioctx *ctx = req->ki_ctx;
516 int ret;
517 spin_lock_irq(&ctx->ctx_lock);
518 ret = __aio_put_req(ctx, req);
519 spin_unlock_irq(&ctx->ctx_lock);
520 if (ret)
521 put_ioctx(ctx);
522 return ret;
525 /* Lookup an ioctx id. ioctx_list is lockless for reads.
526 * FIXME: this is O(n) and is only suitable for development.
528 static inline struct kioctx *lookup_ioctx(unsigned long ctx_id)
530 struct kioctx *ioctx;
531 struct mm_struct *mm;
533 mm = current->mm;
534 read_lock(&mm->ioctx_list_lock);
535 for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
536 if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
537 get_ioctx(ioctx);
538 break;
540 read_unlock(&mm->ioctx_list_lock);
542 return ioctx;
545 static void use_mm(struct mm_struct *mm)
547 struct mm_struct *active_mm = current->active_mm;
548 atomic_inc(&mm->mm_count);
549 current->mm = mm;
550 if (mm != active_mm) {
551 current->active_mm = mm;
552 activate_mm(active_mm, mm);
554 mmdrop(active_mm);
557 static void unuse_mm(struct mm_struct *mm)
559 current->mm = NULL;
560 /* active_mm is still 'mm' */
561 enter_lazy_tlb(mm, current, smp_processor_id());
564 /* Run on kevent's context. FIXME: needs to be per-cpu and warn if an
565 * operation blocks.
567 static void aio_kick_handler(void *data)
569 struct kioctx *ctx = data;
571 use_mm(ctx->mm);
573 spin_lock_irq(&ctx->ctx_lock);
574 while (!list_empty(&ctx->run_list)) {
575 struct kiocb *iocb;
576 long ret;
578 iocb = list_entry(ctx->run_list.next, struct kiocb,
579 ki_run_list);
580 list_del(&iocb->ki_run_list);
581 iocb->ki_users ++;
582 spin_unlock_irq(&ctx->ctx_lock);
584 kiocbClearKicked(iocb);
585 ret = iocb->ki_retry(iocb);
586 if (-EIOCBQUEUED != ret) {
587 aio_complete(iocb, ret, 0);
588 iocb = NULL;
591 spin_lock_irq(&ctx->ctx_lock);
592 if (NULL != iocb)
593 __aio_put_req(ctx, iocb);
595 spin_unlock_irq(&ctx->ctx_lock);
597 unuse_mm(ctx->mm);
600 void kick_iocb(struct kiocb *iocb)
602 struct kioctx *ctx = iocb->ki_ctx;
604 /* sync iocbs are easy: they can only ever be executing from a
605 * single context. */
606 if (is_sync_kiocb(iocb)) {
607 kiocbSetKicked(iocb);
608 wake_up_process(iocb->ki_user_obj);
609 return;
612 if (!kiocbTryKick(iocb)) {
613 long flags;
614 spin_lock_irqsave(&ctx->ctx_lock, flags);
615 list_add_tail(&iocb->ki_run_list, &ctx->run_list);
616 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
617 schedule_work(&ctx->wq);
621 /* aio_complete
622 * Called when the io request on the given iocb is complete.
623 * Returns true if this is the last user of the request. The
624 * only other user of the request can be the cancellation code.
626 int aio_complete(struct kiocb *iocb, long res, long res2)
628 struct kioctx *ctx = iocb->ki_ctx;
629 struct aio_ring_info *info;
630 struct aio_ring *ring;
631 struct io_event *event;
632 unsigned long flags;
633 unsigned long tail;
634 int ret;
636 /* Special case handling for sync iocbs: events go directly
637 * into the iocb for fast handling. Note that this will not
638 * work if we allow sync kiocbs to be cancelled. in which
639 * case the usage count checks will have to move under ctx_lock
640 * for all cases.
642 if (is_sync_kiocb(iocb)) {
643 int ret;
645 iocb->ki_user_data = res;
646 if (iocb->ki_users == 1) {
647 iocb->ki_users = 0;
648 return 1;
650 spin_lock_irq(&ctx->ctx_lock);
651 iocb->ki_users--;
652 ret = (0 == iocb->ki_users);
653 spin_unlock_irq(&ctx->ctx_lock);
655 /* sync iocbs put the task here for us */
656 wake_up_process(iocb->ki_user_obj);
657 return ret;
660 info = &ctx->ring_info;
662 /* add a completion event to the ring buffer.
663 * must be done holding ctx->ctx_lock to prevent
664 * other code from messing with the tail
665 * pointer since we might be called from irq
666 * context.
668 spin_lock_irqsave(&ctx->ctx_lock, flags);
670 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
672 tail = info->tail;
673 event = aio_ring_event(info, tail, KM_IRQ0);
674 tail = (tail + 1) % info->nr;
676 event->obj = (u64)(unsigned long)iocb->ki_user_obj;
677 event->data = iocb->ki_user_data;
678 event->res = res;
679 event->res2 = res2;
681 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
682 ctx, tail, iocb, iocb->ki_user_obj, iocb->ki_user_data,
683 res, res2);
685 /* after flagging the request as done, we
686 * must never even look at it again
688 barrier();
690 info->tail = tail;
691 ring->tail = tail;
693 wmb();
694 put_aio_ring_event(event, KM_IRQ0);
695 kunmap_atomic(ring, KM_IRQ1);
697 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
699 /* everything turned out well, dispose of the aiocb. */
700 ret = __aio_put_req(ctx, iocb);
702 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
704 if (waitqueue_active(&ctx->wait))
705 wake_up(&ctx->wait);
707 if (ret)
708 put_ioctx(ctx);
710 return ret;
713 /* aio_read_evt
714 * Pull an event off of the ioctx's event ring. Returns the number of
715 * events fetched (0 or 1 ;-)
716 * FIXME: make this use cmpxchg.
717 * TODO: make the ringbuffer user mmap()able (requires FIXME).
719 static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
721 struct aio_ring_info *info = &ioctx->ring_info;
722 struct aio_ring *ring;
723 unsigned long head;
724 int ret = 0;
726 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
727 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
728 (unsigned long)ring->head, (unsigned long)ring->tail,
729 (unsigned long)ring->nr);
730 barrier();
731 if (ring->head == ring->tail)
732 goto out;
734 spin_lock(&info->ring_lock);
736 head = ring->head % info->nr;
737 if (head != ring->tail) {
738 struct io_event *evp = aio_ring_event(info, head, KM_USER1);
739 *ent = *evp;
740 head = (head + 1) % info->nr;
741 barrier();
742 ring->head = head;
743 ret = 1;
744 put_aio_ring_event(evp, KM_USER1);
746 spin_unlock(&info->ring_lock);
748 out:
749 kunmap_atomic(ring, KM_USER0);
750 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
751 (unsigned long)ring->head, (unsigned long)ring->tail);
752 return ret;
755 struct timeout {
756 struct timer_list timer;
757 int timed_out;
758 struct task_struct *p;
761 static void timeout_func(unsigned long data)
763 struct timeout *to = (struct timeout *)data;
765 to->timed_out = 1;
766 wake_up_process(to->p);
769 static inline void init_timeout(struct timeout *to)
771 init_timer(&to->timer);
772 to->timer.data = (unsigned long)to;
773 to->timer.function = timeout_func;
774 to->timed_out = 0;
775 to->p = current;
778 static inline void set_timeout(long start_jiffies, struct timeout *to,
779 const struct timespec *ts)
781 unsigned long how_long;
783 if (ts->tv_sec < 0 || (!ts->tv_sec && !ts->tv_nsec)) {
784 to->timed_out = 1;
785 return;
788 how_long = ts->tv_sec * HZ;
789 #define HZ_NS (1000000000 / HZ)
790 how_long += (ts->tv_nsec + HZ_NS - 1) / HZ_NS;
792 to->timer.expires = jiffies + how_long;
793 add_timer(&to->timer);
796 static inline void clear_timeout(struct timeout *to)
798 del_timer_sync(&to->timer);
801 static int read_events(struct kioctx *ctx,
802 long min_nr, long nr,
803 struct io_event *event,
804 struct timespec *timeout)
806 long start_jiffies = jiffies;
807 struct task_struct *tsk = current;
808 DECLARE_WAITQUEUE(wait, tsk);
809 int ret;
810 int i = 0;
811 struct io_event ent;
812 struct timeout to;
814 /* needed to zero any padding within an entry (there shouldn't be
815 * any, but C is fun!
817 memset(&ent, 0, sizeof(ent));
818 ret = 0;
820 while (likely(i < nr)) {
821 ret = aio_read_evt(ctx, &ent);
822 if (unlikely(ret <= 0))
823 break;
825 dprintk("read event: %Lx %Lx %Lx %Lx\n",
826 ent.data, ent.obj, ent.res, ent.res2);
828 /* Could we split the check in two? */
829 ret = -EFAULT;
830 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
831 dprintk("aio: lost an event due to EFAULT.\n");
832 break;
834 ret = 0;
836 /* Good, event copied to userland, update counts. */
837 event ++;
838 i ++;
841 if (min_nr <= i)
842 return i;
843 if (ret)
844 return ret;
846 /* End fast path */
848 if (timeout) {
849 struct timespec ts;
850 ret = -EFAULT;
851 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
852 goto out;
854 init_timeout(&to);
855 set_timeout(start_jiffies, &to, &ts);
858 while (likely(i < nr)) {
859 add_wait_queue_exclusive(&ctx->wait, &wait);
860 do {
861 set_task_state(tsk, TASK_INTERRUPTIBLE);
863 ret = aio_read_evt(ctx, &ent);
864 if (ret)
865 break;
866 if (min_nr <= i)
867 break;
868 ret = 0;
869 if (to.timed_out) /* Only check after read evt */
870 break;
871 schedule();
872 if (signal_pending(tsk)) {
873 ret = -EINTR;
874 break;
876 /*ret = aio_read_evt(ctx, &ent);*/
877 } while (1) ;
879 set_task_state(tsk, TASK_RUNNING);
880 remove_wait_queue(&ctx->wait, &wait);
882 if (unlikely(ret <= 0))
883 break;
885 ret = -EFAULT;
886 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
887 dprintk("aio: lost an event due to EFAULT.\n");
888 break;
891 /* Good, event copied to userland, update counts. */
892 event ++;
893 i ++;
896 if (timeout)
897 clear_timeout(&to);
898 out:
899 return i ? i : ret;
902 /* Take an ioctx and remove it from the list of ioctx's. Protects
903 * against races with itself via ->dead.
905 static void io_destroy(struct kioctx *ioctx)
907 struct mm_struct *mm = current->mm;
908 struct kioctx **tmp;
909 int was_dead;
911 /* delete the entry from the list is someone else hasn't already */
912 write_lock(&mm->ioctx_list_lock);
913 was_dead = ioctx->dead;
914 ioctx->dead = 1;
915 for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
916 tmp = &(*tmp)->next)
918 if (*tmp)
919 *tmp = ioctx->next;
920 write_unlock(&mm->ioctx_list_lock);
922 dprintk("aio_release(%p)\n", ioctx);
923 if (likely(!was_dead))
924 put_ioctx(ioctx); /* twice for the list */
926 aio_cancel_all(ioctx);
927 wait_for_all_aios(ioctx);
928 put_ioctx(ioctx); /* once for the lookup */
931 /* sys_io_setup:
932 * Create an aio_context capable of receiving at least nr_events.
933 * ctxp must not point to an aio_context that already exists, and
934 * must be initialized to 0 prior to the call. On successful
935 * creation of the aio_context, *ctxp is filled in with the resulting
936 * handle. May fail with -EINVAL if *ctxp is not initialized,
937 * if the specified nr_events exceeds internal limits. May fail
938 * with -EAGAIN if the specified nr_events exceeds the user's limit
939 * of available events. May fail with -ENOMEM if insufficient kernel
940 * resources are available. May fail with -EFAULT if an invalid
941 * pointer is passed for ctxp. Will fail with -ENOSYS if not
942 * implemented.
944 asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t *ctxp)
946 struct kioctx *ioctx = NULL;
947 unsigned long ctx;
948 long ret;
950 ret = get_user(ctx, ctxp);
951 if (unlikely(ret))
952 goto out;
954 ret = -EINVAL;
955 if (unlikely(ctx || !nr_events || (int)nr_events < 0)) {
956 pr_debug("EINVAL: io_setup: ctx or nr_events > max\n");
957 goto out;
960 ioctx = ioctx_alloc(nr_events);
961 ret = PTR_ERR(ioctx);
962 if (!IS_ERR(ioctx)) {
963 ret = put_user(ioctx->user_id, ctxp);
964 if (!ret)
965 return 0;
966 io_destroy(ioctx);
969 out:
970 return ret;
973 /* sys_io_destroy:
974 * Destroy the aio_context specified. May cancel any outstanding
975 * AIOs and block on completion. Will fail with -ENOSYS if not
976 * implemented. May fail with -EFAULT if the context pointed to
977 * is invalid.
979 asmlinkage long sys_io_destroy(aio_context_t ctx)
981 struct kioctx *ioctx = lookup_ioctx(ctx);
982 if (likely(NULL != ioctx)) {
983 io_destroy(ioctx);
984 return 0;
986 pr_debug("EINVAL: io_destroy: invalid context id\n");
987 return -EINVAL;
990 static int FASTCALL(io_submit_one(struct kioctx *ctx, struct iocb *user_iocb,
991 struct iocb *iocb));
992 static int io_submit_one(struct kioctx *ctx, struct iocb *user_iocb,
993 struct iocb *iocb)
995 struct kiocb *req;
996 struct file *file;
997 ssize_t ret;
998 char *buf;
1000 /* enforce forwards compatibility on users */
1001 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2 ||
1002 iocb->aio_reserved3)) {
1003 pr_debug("EINVAL: io_submit: reserve field set\n");
1004 return -EINVAL;
1007 /* prevent overflows */
1008 if (unlikely(
1009 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1010 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1011 ((ssize_t)iocb->aio_nbytes < 0)
1012 )) {
1013 pr_debug("EINVAL: io_submit: overflow check\n");
1014 return -EINVAL;
1017 file = fget(iocb->aio_fildes);
1018 if (unlikely(!file))
1019 return -EBADF;
1021 req = aio_get_req(ctx);
1022 if (unlikely(!req)) {
1023 fput(file);
1024 return -EAGAIN;
1027 req->ki_filp = file;
1028 iocb->aio_key = req->ki_key;
1029 ret = put_user(iocb->aio_key, &user_iocb->aio_key);
1030 if (unlikely(ret)) {
1031 dprintk("EFAULT: aio_key\n");
1032 goto out_put_req;
1035 req->ki_user_obj = user_iocb;
1036 req->ki_user_data = iocb->aio_data;
1037 req->ki_pos = iocb->aio_offset;
1039 buf = (char *)(unsigned long)iocb->aio_buf;
1041 switch (iocb->aio_lio_opcode) {
1042 case IOCB_CMD_PREAD:
1043 ret = -EBADF;
1044 if (unlikely(!(file->f_mode & FMODE_READ)))
1045 goto out_put_req;
1046 ret = -EFAULT;
1047 if (unlikely(!access_ok(VERIFY_WRITE, buf, iocb->aio_nbytes)))
1048 goto out_put_req;
1049 ret = -EINVAL;
1050 if (file->f_op->aio_read)
1051 ret = file->f_op->aio_read(req, buf,
1052 iocb->aio_nbytes, req->ki_pos);
1053 break;
1054 case IOCB_CMD_PWRITE:
1055 ret = -EBADF;
1056 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1057 goto out_put_req;
1058 ret = -EFAULT;
1059 if (unlikely(!access_ok(VERIFY_READ, buf, iocb->aio_nbytes)))
1060 goto out_put_req;
1061 ret = -EINVAL;
1062 if (file->f_op->aio_write)
1063 ret = file->f_op->aio_write(req, buf,
1064 iocb->aio_nbytes, req->ki_pos);
1065 break;
1066 case IOCB_CMD_FDSYNC:
1067 ret = -EINVAL;
1068 if (file->f_op->aio_fsync)
1069 ret = file->f_op->aio_fsync(req, 1);
1070 break;
1071 case IOCB_CMD_FSYNC:
1072 ret = -EINVAL;
1073 if (file->f_op->aio_fsync)
1074 ret = file->f_op->aio_fsync(req, 0);
1075 break;
1076 default:
1077 dprintk("EINVAL: io_submit: no operation provided\n");
1078 ret = -EINVAL;
1081 if (likely(-EIOCBQUEUED == ret))
1082 return 0;
1083 aio_complete(req, ret, 0);
1084 return 0;
1086 out_put_req:
1087 aio_put_req(req);
1088 return ret;
1091 /* sys_io_submit:
1092 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1093 * the number of iocbs queued. May return -EINVAL if the aio_context
1094 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1095 * *iocbpp[0] is not properly initialized, if the operation specified
1096 * is invalid for the file descriptor in the iocb. May fail with
1097 * -EFAULT if any of the data structures point to invalid data. May
1098 * fail with -EBADF if the file descriptor specified in the first
1099 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1100 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1101 * fail with -ENOSYS if not implemented.
1103 asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1104 struct iocb **iocbpp)
1106 struct kioctx *ctx;
1107 long ret = 0;
1108 int i;
1110 if (unlikely(nr < 0))
1111 return -EINVAL;
1113 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1114 return -EFAULT;
1116 ctx = lookup_ioctx(ctx_id);
1117 if (unlikely(!ctx)) {
1118 pr_debug("EINVAL: io_submit: invalid context id\n");
1119 return -EINVAL;
1122 for (i=0; i<nr; i++) {
1123 struct iocb *user_iocb, tmp;
1125 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1126 ret = -EFAULT;
1127 break;
1130 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1131 ret = -EFAULT;
1132 break;
1135 ret = io_submit_one(ctx, user_iocb, &tmp);
1136 if (ret)
1137 break;
1140 put_ioctx(ctx);
1141 return i ? i : ret;
1144 /* lookup_kiocb
1145 * Finds a given iocb for cancellation.
1146 * MUST be called with ctx->ctx_lock held.
1148 struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb *iocb, u32 key)
1150 struct list_head *pos;
1151 /* TODO: use a hash or array, this sucks. */
1152 list_for_each(pos, &ctx->active_reqs) {
1153 struct kiocb *kiocb = list_kiocb(pos);
1154 if (kiocb->ki_user_obj == iocb && kiocb->ki_key == key)
1155 return kiocb;
1157 return NULL;
1160 /* sys_io_cancel:
1161 * Attempts to cancel an iocb previously passed to io_submit. If
1162 * the operation is successfully cancelled, the resulting event is
1163 * copied into the memory pointed to by result without being placed
1164 * into the completion queue and 0 is returned. May fail with
1165 * -EFAULT if any of the data structures pointed to are invalid.
1166 * May fail with -EINVAL if aio_context specified by ctx_id is
1167 * invalid. May fail with -EAGAIN if the iocb specified was not
1168 * cancelled. Will fail with -ENOSYS if not implemented.
1170 asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb *iocb,
1171 struct io_event *result)
1173 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1174 struct kioctx *ctx;
1175 struct kiocb *kiocb;
1176 u32 key;
1177 int ret;
1179 ret = get_user(key, &iocb->aio_key);
1180 if (unlikely(ret))
1181 return -EFAULT;
1183 ctx = lookup_ioctx(ctx_id);
1184 if (unlikely(!ctx))
1185 return -EINVAL;
1187 spin_lock_irq(&ctx->ctx_lock);
1188 ret = -EAGAIN;
1189 kiocb = lookup_kiocb(ctx, iocb, key);
1190 if (kiocb && kiocb->ki_cancel) {
1191 cancel = kiocb->ki_cancel;
1192 kiocb->ki_users ++;
1193 } else
1194 cancel = NULL;
1195 spin_unlock_irq(&ctx->ctx_lock);
1197 if (NULL != cancel) {
1198 struct io_event tmp;
1199 printk("calling cancel\n");
1200 ret = cancel(kiocb, &tmp);
1201 if (!ret) {
1202 /* Cancellation succeeded -- copy the result
1203 * into the user's buffer.
1205 if (copy_to_user(result, &tmp, sizeof(tmp)))
1206 ret = -EFAULT;
1208 } else
1209 printk(KERN_DEBUG "iocb has no cancel operation\n");
1211 put_ioctx(ctx);
1213 return ret;
1216 /* io_getevents:
1217 * Attempts to read at least min_nr events and up to nr events from
1218 * the completion queue for the aio_context specified by ctx_id. May
1219 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1220 * if nr is out of range, if when is out of range. May fail with
1221 * -EFAULT if any of the memory specified to is invalid. May return
1222 * 0 or < min_nr if no events are available and the timeout specified
1223 * by when has elapsed, where when == NULL specifies an infinite
1224 * timeout. Note that the timeout pointed to by when is relative and
1225 * will be updated if not NULL and the operation blocks. Will fail
1226 * with -ENOSYS if not implemented.
1228 asmlinkage long sys_io_getevents(aio_context_t ctx_id,
1229 long min_nr,
1230 long nr,
1231 struct io_event *events,
1232 struct timespec *timeout)
1234 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1235 long ret = -EINVAL;
1237 if (unlikely(min_nr > nr || min_nr < 0 || nr < 0))
1238 return ret;
1240 if (likely(NULL != ioctx)) {
1241 ret = read_events(ioctx, min_nr, nr, events, timeout);
1242 put_ioctx(ioctx);
1245 return ret;
1248 __initcall(aio_setup);
1250 EXPORT_SYMBOL(aio_complete);
1251 EXPORT_SYMBOL(aio_put_req);
1252 EXPORT_SYMBOL(wait_on_sync_kiocb);