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>
20 #include <linux/sched.h>
22 #include <linux/file.h>
24 #include <linux/mman.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/aio.h>
28 #include <linux/highmem.h>
29 #include <linux/workqueue.h>
31 #include <asm/kmap_types.h>
32 #include <asm/uaccess.h>
33 #include <asm/mmu_context.h>
36 #define dprintk printk
38 #define dprintk(x...) do { ; } while (0)
41 /*------ sysctl variables----*/
42 atomic_t aio_nr = ATOMIC_INIT(0); /* current system wide number of aio requests */
43 unsigned aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
44 /*----end sysctl variables---*/
46 static kmem_cache_t *kiocb_cachep;
47 static kmem_cache_t *kioctx_cachep;
49 static struct workqueue_struct *aio_wq;
51 /* Used for rare fput completion. */
52 static void aio_fput_routine(void *);
53 static DECLARE_WORK(fput_work, aio_fput_routine, NULL);
55 static spinlock_t fput_lock = SPIN_LOCK_UNLOCKED;
58 static void aio_kick_handler(void *);
61 * Creates the slab caches used by the aio routines, panic on
62 * failure as this is done early during the boot sequence.
64 static int __init aio_setup(void)
66 kiocb_cachep = kmem_cache_create("kiocb", sizeof(struct kiocb),
67 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
68 kioctx_cachep = kmem_cache_create("kioctx", sizeof(struct kioctx),
69 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
71 aio_wq = create_workqueue("aio");
73 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
78 static void aio_free_ring(struct kioctx *ctx)
80 struct aio_ring_info *info = &ctx->ring_info;
83 for (i=0; i<info->nr_pages; i++)
84 put_page(info->ring_pages[i]);
86 if (info->mmap_size) {
87 down_write(&ctx->mm->mmap_sem);
88 do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
89 up_write(&ctx->mm->mmap_sem);
92 if (info->ring_pages && info->ring_pages != info->internal_pages)
93 kfree(info->ring_pages);
94 info->ring_pages = NULL;
98 static int aio_setup_ring(struct kioctx *ctx)
100 struct aio_ring *ring;
101 struct aio_ring_info *info = &ctx->ring_info;
102 unsigned nr_events = ctx->max_reqs;
106 /* Compensate for the ring buffer's head/tail overlap entry */
107 nr_events += 2; /* 1 is required, 2 for good luck */
109 size = sizeof(struct aio_ring);
110 size += sizeof(struct io_event) * nr_events;
111 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
116 info->nr_pages = nr_pages;
118 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
121 info->ring_pages = info->internal_pages;
122 if (nr_pages > AIO_RING_PAGES) {
123 info->ring_pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_KERNEL);
124 if (!info->ring_pages)
126 memset(info->ring_pages, 0, sizeof(struct page *) * nr_pages);
129 info->mmap_size = nr_pages * PAGE_SIZE;
130 dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
131 down_write(&ctx->mm->mmap_sem);
132 info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
133 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
135 if (IS_ERR((void *)info->mmap_base)) {
136 up_write(&ctx->mm->mmap_sem);
137 printk("mmap err: %ld\n", -info->mmap_base);
143 dprintk("mmap address: 0x%08lx\n", info->mmap_base);
144 info->nr_pages = get_user_pages(current, ctx->mm,
145 info->mmap_base, nr_pages,
146 1, 0, info->ring_pages, NULL);
147 up_write(&ctx->mm->mmap_sem);
149 if (unlikely(info->nr_pages != nr_pages)) {
154 ctx->user_id = info->mmap_base;
156 info->nr = nr_events; /* trusted copy */
158 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
159 ring->nr = nr_events; /* user copy */
160 ring->id = ctx->user_id;
161 ring->head = ring->tail = 0;
162 ring->magic = AIO_RING_MAGIC;
163 ring->compat_features = AIO_RING_COMPAT_FEATURES;
164 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
165 ring->header_length = sizeof(struct aio_ring);
166 kunmap_atomic(ring, KM_USER0);
172 /* aio_ring_event: returns a pointer to the event at the given index from
173 * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
175 #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
176 #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
177 #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
179 #define aio_ring_event(info, nr, km) ({ \
180 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
181 struct io_event *__event; \
182 __event = kmap_atomic( \
183 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
184 __event += pos % AIO_EVENTS_PER_PAGE; \
188 #define put_aio_ring_event(event, km) do { \
189 struct io_event *__event = (event); \
191 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
195 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
197 static struct kioctx *ioctx_alloc(unsigned nr_events)
199 struct mm_struct *mm;
202 /* Prevent overflows */
203 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
204 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
205 pr_debug("ENOMEM: nr_events too high\n");
206 return ERR_PTR(-EINVAL);
209 if (nr_events > aio_max_nr)
210 return ERR_PTR(-EAGAIN);
212 ctx = kmem_cache_alloc(kioctx_cachep, GFP_KERNEL);
214 return ERR_PTR(-ENOMEM);
216 memset(ctx, 0, sizeof(*ctx));
217 ctx->max_reqs = nr_events;
218 mm = ctx->mm = current->mm;
219 atomic_inc(&mm->mm_count);
221 atomic_set(&ctx->users, 1);
222 spin_lock_init(&ctx->ctx_lock);
223 spin_lock_init(&ctx->ring_info.ring_lock);
224 init_waitqueue_head(&ctx->wait);
226 INIT_LIST_HEAD(&ctx->active_reqs);
227 INIT_LIST_HEAD(&ctx->run_list);
228 INIT_WORK(&ctx->wq, aio_kick_handler, ctx);
230 if (aio_setup_ring(ctx) < 0)
233 /* limit the number of system wide aios */
234 atomic_add(ctx->max_reqs, &aio_nr); /* undone by __put_ioctx */
235 if (unlikely(atomic_read(&aio_nr) > aio_max_nr))
238 /* now link into global list. kludge. FIXME */
239 write_lock(&mm->ioctx_list_lock);
240 ctx->next = mm->ioctx_list;
241 mm->ioctx_list = ctx;
242 write_unlock(&mm->ioctx_list_lock);
244 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
245 ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
249 atomic_sub(ctx->max_reqs, &aio_nr);
250 ctx->max_reqs = 0; /* prevent __put_ioctx from sub'ing aio_nr */
252 return ERR_PTR(-EAGAIN);
256 kmem_cache_free(kioctx_cachep, ctx);
257 ctx = ERR_PTR(-ENOMEM);
259 dprintk("aio: error allocating ioctx %p\n", ctx);
264 * Cancels all outstanding aio requests on an aio context. Used
265 * when the processes owning a context have all exited to encourage
266 * the rapid destruction of the kioctx.
268 static void aio_cancel_all(struct kioctx *ctx)
270 int (*cancel)(struct kiocb *, struct io_event *);
272 spin_lock_irq(&ctx->ctx_lock);
274 while (!list_empty(&ctx->active_reqs)) {
275 struct list_head *pos = ctx->active_reqs.next;
276 struct kiocb *iocb = list_kiocb(pos);
277 list_del_init(&iocb->ki_list);
278 cancel = iocb->ki_cancel;
281 spin_unlock_irq(&ctx->ctx_lock);
283 spin_lock_irq(&ctx->ctx_lock);
286 spin_unlock_irq(&ctx->ctx_lock);
289 void wait_for_all_aios(struct kioctx *ctx)
291 struct task_struct *tsk = current;
292 DECLARE_WAITQUEUE(wait, tsk);
294 if (!ctx->reqs_active)
297 add_wait_queue(&ctx->wait, &wait);
298 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
299 while (ctx->reqs_active) {
301 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
303 __set_task_state(tsk, TASK_RUNNING);
304 remove_wait_queue(&ctx->wait, &wait);
307 /* wait_on_sync_kiocb:
308 * Waits on the given sync kiocb to complete.
310 ssize_t fastcall wait_on_sync_kiocb(struct kiocb *iocb)
312 while (iocb->ki_users) {
313 set_current_state(TASK_UNINTERRUPTIBLE);
318 __set_current_state(TASK_RUNNING);
319 return iocb->ki_user_data;
322 /* exit_aio: called when the last user of mm goes away. At this point,
323 * there is no way for any new requests to be submited or any of the
324 * io_* syscalls to be called on the context. However, there may be
325 * outstanding requests which hold references to the context; as they
326 * go away, they will call put_ioctx and release any pinned memory
327 * associated with the request (held via struct page * references).
329 void fastcall exit_aio(struct mm_struct *mm)
331 struct kioctx *ctx = mm->ioctx_list;
332 mm->ioctx_list = NULL;
334 struct kioctx *next = ctx->next;
338 wait_for_all_aios(ctx);
340 if (1 != atomic_read(&ctx->users))
342 "exit_aio:ioctx still alive: %d %d %d\n",
343 atomic_read(&ctx->users), ctx->dead,
351 * Called when the last user of an aio context has gone away,
352 * and the struct needs to be freed.
354 void fastcall __put_ioctx(struct kioctx *ctx)
356 unsigned nr_events = ctx->max_reqs;
358 if (unlikely(ctx->reqs_active))
364 pr_debug("__put_ioctx: freeing %p\n", ctx);
365 kmem_cache_free(kioctx_cachep, ctx);
367 atomic_sub(nr_events, &aio_nr);
371 * Allocate a slot for an aio request. Increments the users count
372 * of the kioctx so that the kioctx stays around until all requests are
373 * complete. Returns NULL if no requests are free.
375 * Returns with kiocb->users set to 2. The io submit code path holds
376 * an extra reference while submitting the i/o.
377 * This prevents races between the aio code path referencing the
378 * req (after submitting it) and aio_complete() freeing the req.
380 static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
381 static struct kiocb fastcall *__aio_get_req(struct kioctx *ctx)
383 struct kiocb *req = NULL;
384 struct aio_ring *ring;
387 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
391 req->ki_flags = 1 << KIF_LOCKED;
395 req->ki_cancel = NULL;
396 req->ki_retry = NULL;
397 req->ki_obj.user = NULL;
399 /* Check if the completion queue has enough free space to
400 * accept an event from this io.
402 spin_lock_irq(&ctx->ctx_lock);
403 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
404 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
405 list_add(&req->ki_list, &ctx->active_reqs);
410 kunmap_atomic(ring, KM_USER0);
411 spin_unlock_irq(&ctx->ctx_lock);
414 kmem_cache_free(kiocb_cachep, req);
421 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
424 /* Handle a potential starvation case -- should be exceedingly rare as
425 * requests will be stuck on fput_head only if the aio_fput_routine is
426 * delayed and the requests were the last user of the struct file.
428 req = __aio_get_req(ctx);
429 if (unlikely(NULL == req)) {
430 aio_fput_routine(NULL);
431 req = __aio_get_req(ctx);
436 static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
440 req->ki_obj.user = NULL;
441 kmem_cache_free(kiocb_cachep, req);
444 if (unlikely(!ctx->reqs_active && ctx->dead))
448 static void aio_fput_routine(void *data)
450 spin_lock_irq(&fput_lock);
451 while (likely(!list_empty(&fput_head))) {
452 struct kiocb *req = list_kiocb(fput_head.next);
453 struct kioctx *ctx = req->ki_ctx;
455 list_del(&req->ki_list);
456 spin_unlock_irq(&fput_lock);
458 /* Complete the fput */
459 __fput(req->ki_filp);
461 /* Link the iocb into the context's free list */
462 spin_lock_irq(&ctx->ctx_lock);
463 really_put_req(ctx, req);
464 spin_unlock_irq(&ctx->ctx_lock);
467 spin_lock_irq(&fput_lock);
469 spin_unlock_irq(&fput_lock);
473 * Returns true if this put was the last user of the request.
475 static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
477 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
478 req, atomic_read(&req->ki_filp->f_count));
481 if (unlikely(req->ki_users < 0))
483 if (likely(req->ki_users))
485 list_del(&req->ki_list); /* remove from active_reqs */
486 req->ki_cancel = NULL;
487 req->ki_retry = NULL;
489 /* Must be done under the lock to serialise against cancellation.
490 * Call this aio_fput as it duplicates fput via the fput_work.
492 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
494 spin_lock(&fput_lock);
495 list_add(&req->ki_list, &fput_head);
496 spin_unlock(&fput_lock);
497 queue_work(aio_wq, &fput_work);
499 really_put_req(ctx, req);
504 * Returns true if this put was the last user of the kiocb,
505 * false if the request is still in use.
507 int fastcall aio_put_req(struct kiocb *req)
509 struct kioctx *ctx = req->ki_ctx;
511 spin_lock_irq(&ctx->ctx_lock);
512 ret = __aio_put_req(ctx, req);
513 spin_unlock_irq(&ctx->ctx_lock);
519 /* Lookup an ioctx id. ioctx_list is lockless for reads.
520 * FIXME: this is O(n) and is only suitable for development.
522 struct kioctx *lookup_ioctx(unsigned long ctx_id)
524 struct kioctx *ioctx;
525 struct mm_struct *mm;
528 read_lock(&mm->ioctx_list_lock);
529 for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
530 if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
534 read_unlock(&mm->ioctx_list_lock);
539 static void use_mm(struct mm_struct *mm)
541 struct mm_struct *active_mm;
543 atomic_inc(&mm->mm_count);
545 active_mm = current->active_mm;
547 if (mm != active_mm) {
548 current->active_mm = mm;
549 activate_mm(active_mm, mm);
551 task_unlock(current);
555 static void unuse_mm(struct mm_struct *mm)
559 task_unlock(current);
560 /* active_mm is still 'mm' */
561 enter_lazy_tlb(mm, current);
564 /* Run on kevent's context. FIXME: needs to be per-cpu and warn if an
567 static void aio_kick_handler(void *data)
569 struct kioctx *ctx = data;
573 spin_lock_irq(&ctx->ctx_lock);
574 while (!list_empty(&ctx->run_list)) {
578 iocb = list_entry(ctx->run_list.next, struct kiocb,
580 list_del(&iocb->ki_run_list);
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);
591 spin_lock_irq(&ctx->ctx_lock);
593 __aio_put_req(ctx, iocb);
595 spin_unlock_irq(&ctx->ctx_lock);
600 void fastcall 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
606 if (is_sync_kiocb(iocb)) {
607 kiocbSetKicked(iocb);
608 wake_up_process(iocb->ki_obj.tsk);
612 if (!kiocbTryKick(iocb)) {
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 queue_work(aio_wq, &ctx->wq);
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 fastcall 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;
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
642 if (is_sync_kiocb(iocb)) {
645 iocb->ki_user_data = res;
646 if (iocb->ki_users == 1) {
650 spin_lock_irq(&ctx->ctx_lock);
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_obj.tsk);
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
668 spin_lock_irqsave(&ctx->ctx_lock, flags);
670 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
673 event = aio_ring_event(info, tail, KM_IRQ0);
674 tail = (tail + 1) % info->nr;
676 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
677 event->data = iocb->ki_user_data;
681 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
682 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
685 /* after flagging the request as done, we
686 * must never even look at it again
688 smp_wmb(); /* make event visible before updating tail */
693 put_aio_ring_event(event, KM_IRQ0);
694 kunmap_atomic(ring, KM_IRQ1);
696 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
698 /* everything turned out well, dispose of the aiocb. */
699 ret = __aio_put_req(ctx, iocb);
701 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
703 if (waitqueue_active(&ctx->wait))
713 * Pull an event off of the ioctx's event ring. Returns the number of
714 * events fetched (0 or 1 ;-)
715 * FIXME: make this use cmpxchg.
716 * TODO: make the ringbuffer user mmap()able (requires FIXME).
718 static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
720 struct aio_ring_info *info = &ioctx->ring_info;
721 struct aio_ring *ring;
725 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
726 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
727 (unsigned long)ring->head, (unsigned long)ring->tail,
728 (unsigned long)ring->nr);
730 if (ring->head == ring->tail)
733 spin_lock(&info->ring_lock);
735 head = ring->head % info->nr;
736 if (head != ring->tail) {
737 struct io_event *evp = aio_ring_event(info, head, KM_USER1);
739 head = (head + 1) % info->nr;
740 smp_mb(); /* finish reading the event before updatng the head */
743 put_aio_ring_event(evp, KM_USER1);
745 spin_unlock(&info->ring_lock);
748 kunmap_atomic(ring, KM_USER0);
749 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
750 (unsigned long)ring->head, (unsigned long)ring->tail);
755 struct timer_list timer;
757 struct task_struct *p;
760 static void timeout_func(unsigned long data)
762 struct timeout *to = (struct timeout *)data;
765 wake_up_process(to->p);
768 static inline void init_timeout(struct timeout *to)
770 init_timer(&to->timer);
771 to->timer.data = (unsigned long)to;
772 to->timer.function = timeout_func;
777 static inline void set_timeout(long start_jiffies, struct timeout *to,
778 const struct timespec *ts)
780 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
781 if (time_after(to->timer.expires, jiffies))
782 add_timer(&to->timer);
787 static inline void clear_timeout(struct timeout *to)
789 del_singleshot_timer_sync(&to->timer);
792 static int read_events(struct kioctx *ctx,
793 long min_nr, long nr,
794 struct io_event __user *event,
795 struct timespec __user *timeout)
797 long start_jiffies = jiffies;
798 struct task_struct *tsk = current;
799 DECLARE_WAITQUEUE(wait, tsk);
805 /* needed to zero any padding within an entry (there shouldn't be
808 memset(&ent, 0, sizeof(ent));
811 while (likely(i < nr)) {
812 ret = aio_read_evt(ctx, &ent);
813 if (unlikely(ret <= 0))
816 dprintk("read event: %Lx %Lx %Lx %Lx\n",
817 ent.data, ent.obj, ent.res, ent.res2);
819 /* Could we split the check in two? */
821 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
822 dprintk("aio: lost an event due to EFAULT.\n");
827 /* Good, event copied to userland, update counts. */
843 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
846 set_timeout(start_jiffies, &to, &ts);
849 while (likely(i < nr)) {
850 add_wait_queue_exclusive(&ctx->wait, &wait);
852 set_task_state(tsk, TASK_INTERRUPTIBLE);
854 ret = aio_read_evt(ctx, &ent);
860 if (to.timed_out) /* Only check after read evt */
863 if (signal_pending(tsk)) {
867 /*ret = aio_read_evt(ctx, &ent);*/
870 set_task_state(tsk, TASK_RUNNING);
871 remove_wait_queue(&ctx->wait, &wait);
873 if (unlikely(ret <= 0))
877 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
878 dprintk("aio: lost an event due to EFAULT.\n");
882 /* Good, event copied to userland, update counts. */
893 /* Take an ioctx and remove it from the list of ioctx's. Protects
894 * against races with itself via ->dead.
896 static void io_destroy(struct kioctx *ioctx)
898 struct mm_struct *mm = current->mm;
902 /* delete the entry from the list is someone else hasn't already */
903 write_lock(&mm->ioctx_list_lock);
904 was_dead = ioctx->dead;
906 for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
911 write_unlock(&mm->ioctx_list_lock);
913 dprintk("aio_release(%p)\n", ioctx);
914 if (likely(!was_dead))
915 put_ioctx(ioctx); /* twice for the list */
917 aio_cancel_all(ioctx);
918 wait_for_all_aios(ioctx);
919 put_ioctx(ioctx); /* once for the lookup */
923 * Create an aio_context capable of receiving at least nr_events.
924 * ctxp must not point to an aio_context that already exists, and
925 * must be initialized to 0 prior to the call. On successful
926 * creation of the aio_context, *ctxp is filled in with the resulting
927 * handle. May fail with -EINVAL if *ctxp is not initialized,
928 * if the specified nr_events exceeds internal limits. May fail
929 * with -EAGAIN if the specified nr_events exceeds the user's limit
930 * of available events. May fail with -ENOMEM if insufficient kernel
931 * resources are available. May fail with -EFAULT if an invalid
932 * pointer is passed for ctxp. Will fail with -ENOSYS if not
935 asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
937 struct kioctx *ioctx = NULL;
941 ret = get_user(ctx, ctxp);
946 if (unlikely(ctx || (int)nr_events <= 0)) {
947 pr_debug("EINVAL: io_setup: ctx or nr_events > max\n");
951 ioctx = ioctx_alloc(nr_events);
952 ret = PTR_ERR(ioctx);
953 if (!IS_ERR(ioctx)) {
954 ret = put_user(ioctx->user_id, ctxp);
966 * Destroy the aio_context specified. May cancel any outstanding
967 * AIOs and block on completion. Will fail with -ENOSYS if not
968 * implemented. May fail with -EFAULT if the context pointed to
971 asmlinkage long sys_io_destroy(aio_context_t ctx)
973 struct kioctx *ioctx = lookup_ioctx(ctx);
974 if (likely(NULL != ioctx)) {
978 pr_debug("EINVAL: io_destroy: invalid context id\n");
982 int fastcall io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
990 /* enforce forwards compatibility on users */
991 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2 ||
992 iocb->aio_reserved3)) {
993 pr_debug("EINVAL: io_submit: reserve field set\n");
997 /* prevent overflows */
999 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1000 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1001 ((ssize_t)iocb->aio_nbytes < 0)
1003 pr_debug("EINVAL: io_submit: overflow check\n");
1007 file = fget(iocb->aio_fildes);
1008 if (unlikely(!file))
1011 req = aio_get_req(ctx); /* returns with 2 references to req */
1012 if (unlikely(!req)) {
1017 req->ki_filp = file;
1018 iocb->aio_key = req->ki_key;
1019 ret = put_user(iocb->aio_key, &user_iocb->aio_key);
1020 if (unlikely(ret)) {
1021 dprintk("EFAULT: aio_key\n");
1025 req->ki_obj.user = user_iocb;
1026 req->ki_user_data = iocb->aio_data;
1027 req->ki_pos = iocb->aio_offset;
1029 buf = (char __user *)(unsigned long)iocb->aio_buf;
1031 switch (iocb->aio_lio_opcode) {
1032 case IOCB_CMD_PREAD:
1034 if (unlikely(!(file->f_mode & FMODE_READ)))
1037 if (unlikely(!access_ok(VERIFY_WRITE, buf, iocb->aio_nbytes)))
1040 if (file->f_op->aio_read)
1041 ret = file->f_op->aio_read(req, buf,
1042 iocb->aio_nbytes, req->ki_pos);
1044 case IOCB_CMD_PWRITE:
1046 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1049 if (unlikely(!access_ok(VERIFY_READ, buf, iocb->aio_nbytes)))
1052 if (file->f_op->aio_write)
1053 ret = file->f_op->aio_write(req, buf,
1054 iocb->aio_nbytes, req->ki_pos);
1056 case IOCB_CMD_FDSYNC:
1058 if (file->f_op->aio_fsync)
1059 ret = file->f_op->aio_fsync(req, 1);
1061 case IOCB_CMD_FSYNC:
1063 if (file->f_op->aio_fsync)
1064 ret = file->f_op->aio_fsync(req, 0);
1067 dprintk("EINVAL: io_submit: no operation provided\n");
1071 aio_put_req(req); /* drop extra ref to req */
1072 if (likely(-EIOCBQUEUED == ret))
1074 aio_complete(req, ret, 0); /* will drop i/o ref to req */
1078 aio_put_req(req); /* drop extra ref to req */
1079 aio_put_req(req); /* drop i/o ref to req */
1084 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1085 * the number of iocbs queued. May return -EINVAL if the aio_context
1086 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1087 * *iocbpp[0] is not properly initialized, if the operation specified
1088 * is invalid for the file descriptor in the iocb. May fail with
1089 * -EFAULT if any of the data structures point to invalid data. May
1090 * fail with -EBADF if the file descriptor specified in the first
1091 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1092 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1093 * fail with -ENOSYS if not implemented.
1095 asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1096 struct iocb __user * __user *iocbpp)
1102 if (unlikely(nr < 0))
1105 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1108 ctx = lookup_ioctx(ctx_id);
1109 if (unlikely(!ctx)) {
1110 pr_debug("EINVAL: io_submit: invalid context id\n");
1115 * AKPM: should this return a partial result if some of the IOs were
1116 * successfully submitted?
1118 for (i=0; i<nr; i++) {
1119 struct iocb __user *user_iocb;
1122 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1127 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1132 ret = io_submit_one(ctx, user_iocb, &tmp);
1142 * Finds a given iocb for cancellation.
1143 * MUST be called with ctx->ctx_lock held.
1145 struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb, u32 key)
1147 struct list_head *pos;
1148 /* TODO: use a hash or array, this sucks. */
1149 list_for_each(pos, &ctx->active_reqs) {
1150 struct kiocb *kiocb = list_kiocb(pos);
1151 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1158 * Attempts to cancel an iocb previously passed to io_submit. If
1159 * the operation is successfully cancelled, the resulting event is
1160 * copied into the memory pointed to by result without being placed
1161 * into the completion queue and 0 is returned. May fail with
1162 * -EFAULT if any of the data structures pointed to are invalid.
1163 * May fail with -EINVAL if aio_context specified by ctx_id is
1164 * invalid. May fail with -EAGAIN if the iocb specified was not
1165 * cancelled. Will fail with -ENOSYS if not implemented.
1167 asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
1168 struct io_event __user *result)
1170 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1172 struct kiocb *kiocb;
1176 ret = get_user(key, &iocb->aio_key);
1180 ctx = lookup_ioctx(ctx_id);
1184 spin_lock_irq(&ctx->ctx_lock);
1186 kiocb = lookup_kiocb(ctx, iocb, key);
1187 if (kiocb && kiocb->ki_cancel) {
1188 cancel = kiocb->ki_cancel;
1192 spin_unlock_irq(&ctx->ctx_lock);
1194 if (NULL != cancel) {
1195 struct io_event tmp;
1196 pr_debug("calling cancel\n");
1197 memset(&tmp, 0, sizeof(tmp));
1198 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1199 tmp.data = kiocb->ki_user_data;
1200 ret = cancel(kiocb, &tmp);
1202 /* Cancellation succeeded -- copy the result
1203 * into the user's buffer.
1205 if (copy_to_user(result, &tmp, sizeof(tmp)))
1209 printk(KERN_DEBUG "iocb has no cancel operation\n");
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,
1231 struct io_event __user *events,
1232 struct timespec __user *timeout)
1234 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1237 if (likely(ioctx)) {
1238 if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
1239 ret = read_events(ioctx, min_nr, nr, events, timeout);
1246 __initcall(aio_setup);
1248 EXPORT_SYMBOL(aio_complete);
1249 EXPORT_SYMBOL(aio_put_req);
1250 EXPORT_SYMBOL(wait_on_sync_kiocb);