2 * An async IO implementation for Linux
3 * Written by Benjamin LaHaise <bcrl@kvack.org>
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>
17 #include <linux/syscalls.h>
18 #include <linux/uio.h>
22 #include <linux/sched.h>
24 #include <linux/file.h>
26 #include <linux/mman.h>
27 #include <linux/slab.h>
28 #include <linux/timer.h>
29 #include <linux/aio.h>
30 #include <linux/highmem.h>
31 #include <linux/workqueue.h>
32 #include <linux/security.h>
33 #include <linux/eventfd.h>
35 #include <asm/kmap_types.h>
36 #include <asm/uaccess.h>
37 #include <asm/mmu_context.h>
40 #include <linux/poll.h>
41 #include <linux/anon_inodes.h>
45 #define dprintk printk
47 #define dprintk(x...) do { ; } while (0)
50 /*------ sysctl variables----*/
51 static DEFINE_SPINLOCK(aio_nr_lock);
52 unsigned long aio_nr; /* current system wide number of aio requests */
53 unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
54 /*----end sysctl variables---*/
56 static struct kmem_cache *kiocb_cachep;
57 static struct kmem_cache *kioctx_cachep;
59 static struct workqueue_struct *aio_wq;
61 /* Used for rare fput completion. */
62 static void aio_fput_routine(struct work_struct *);
63 static DECLARE_WORK(fput_work, aio_fput_routine);
65 static DEFINE_SPINLOCK(fput_lock);
66 static LIST_HEAD(fput_head);
68 static void aio_kick_handler(struct work_struct *);
69 static void aio_queue_work(struct kioctx *);
72 * Creates the slab caches used by the aio routines, panic on
73 * failure as this is done early during the boot sequence.
75 static int __init aio_setup(void)
77 kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
78 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
80 aio_wq = create_workqueue("aio");
82 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
87 static void aio_free_ring(struct kioctx *ctx)
89 struct aio_ring_info *info = &ctx->ring_info;
92 for (i=0; i<info->nr_pages; i++)
93 put_page(info->ring_pages[i]);
95 if (info->mmap_size) {
96 down_write(&ctx->mm->mmap_sem);
97 do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
98 up_write(&ctx->mm->mmap_sem);
101 if (info->ring_pages && info->ring_pages != info->internal_pages)
102 kfree(info->ring_pages);
103 info->ring_pages = NULL;
107 static int aio_setup_ring(struct kioctx *ctx)
109 struct aio_ring *ring;
110 struct aio_ring_info *info = &ctx->ring_info;
111 unsigned nr_events = ctx->max_reqs;
115 /* Compensate for the ring buffer's head/tail overlap entry */
116 nr_events += 2; /* 1 is required, 2 for good luck */
118 size = sizeof(struct aio_ring);
119 size += sizeof(struct io_event) * nr_events;
120 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
125 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
128 info->ring_pages = info->internal_pages;
129 if (nr_pages > AIO_RING_PAGES) {
130 info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
131 if (!info->ring_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_ANONYMOUS|MAP_PRIVATE,
141 if (IS_ERR((void *)info->mmap_base)) {
142 up_write(&ctx->mm->mmap_sem);
148 dprintk("mmap address: 0x%08lx\n", info->mmap_base);
149 info->nr_pages = get_user_pages(current, ctx->mm,
150 info->mmap_base, nr_pages,
151 1, 0, info->ring_pages, NULL);
152 up_write(&ctx->mm->mmap_sem);
154 if (unlikely(info->nr_pages != nr_pages)) {
159 ctx->user_id = info->mmap_base;
161 info->nr = nr_events; /* trusted copy */
163 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
164 ring->nr = nr_events; /* user copy */
165 ring->id = ctx->user_id;
166 ring->head = ring->tail = 0;
167 ring->magic = AIO_RING_MAGIC;
168 ring->compat_features = AIO_RING_COMPAT_FEATURES;
169 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
170 ring->header_length = sizeof(struct aio_ring);
171 kunmap_atomic(ring, KM_USER0);
177 /* aio_ring_event: returns a pointer to the event at the given index from
178 * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
180 #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
181 #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
182 #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
184 #define aio_ring_event(info, nr, km) ({ \
185 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
186 struct io_event *__event; \
187 __event = kmap_atomic( \
188 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
189 __event += pos % AIO_EVENTS_PER_PAGE; \
193 #define put_aio_ring_event(event, km) do { \
194 struct io_event *__event = (event); \
196 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
200 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
202 static struct kioctx *ioctx_alloc(unsigned nr_events)
204 struct mm_struct *mm;
207 /* Prevent overflows */
208 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
209 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
210 pr_debug("ENOMEM: nr_events too high\n");
211 return ERR_PTR(-EINVAL);
214 if ((unsigned long)nr_events > aio_max_nr)
215 return ERR_PTR(-EAGAIN);
217 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
219 return ERR_PTR(-ENOMEM);
221 ctx->max_reqs = nr_events;
222 mm = ctx->mm = current->mm;
223 atomic_inc(&mm->mm_count);
225 atomic_set(&ctx->users, 1);
226 spin_lock_init(&ctx->ctx_lock);
227 spin_lock_init(&ctx->ring_info.ring_lock);
228 init_waitqueue_head(&ctx->wait);
230 INIT_LIST_HEAD(&ctx->active_reqs);
231 INIT_LIST_HEAD(&ctx->run_list);
232 INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
234 if (aio_setup_ring(ctx) < 0)
237 /* limit the number of system wide aios */
238 spin_lock(&aio_nr_lock);
239 if (aio_nr + ctx->max_reqs > aio_max_nr ||
240 aio_nr + ctx->max_reqs < aio_nr)
243 aio_nr += ctx->max_reqs;
244 spin_unlock(&aio_nr_lock);
245 if (ctx->max_reqs == 0)
248 /* now link into global list. kludge. FIXME */
249 write_lock(&mm->ioctx_list_lock);
250 ctx->next = mm->ioctx_list;
251 mm->ioctx_list = ctx;
252 write_unlock(&mm->ioctx_list_lock);
254 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
255 ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
260 return ERR_PTR(-EAGAIN);
264 kmem_cache_free(kioctx_cachep, ctx);
265 ctx = ERR_PTR(-ENOMEM);
267 dprintk("aio: error allocating ioctx %p\n", ctx);
272 * Cancels all outstanding aio requests on an aio context. Used
273 * when the processes owning a context have all exited to encourage
274 * the rapid destruction of the kioctx.
276 static void aio_cancel_all(struct kioctx *ctx)
278 int (*cancel)(struct kiocb *, struct io_event *);
280 spin_lock_irq(&ctx->ctx_lock);
282 while (!list_empty(&ctx->active_reqs)) {
283 struct list_head *pos = ctx->active_reqs.next;
284 struct kiocb *iocb = list_kiocb(pos);
285 list_del_init(&iocb->ki_list);
286 cancel = iocb->ki_cancel;
287 kiocbSetCancelled(iocb);
290 spin_unlock_irq(&ctx->ctx_lock);
292 spin_lock_irq(&ctx->ctx_lock);
295 spin_unlock_irq(&ctx->ctx_lock);
298 static void wait_for_all_aios(struct kioctx *ctx)
300 struct task_struct *tsk = current;
301 DECLARE_WAITQUEUE(wait, tsk);
303 spin_lock_irq(&ctx->ctx_lock);
304 if (!ctx->reqs_active)
307 add_wait_queue(&ctx->wait, &wait);
308 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
309 while (ctx->reqs_active) {
310 spin_unlock_irq(&ctx->ctx_lock);
312 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
313 spin_lock_irq(&ctx->ctx_lock);
315 __set_task_state(tsk, TASK_RUNNING);
316 remove_wait_queue(&ctx->wait, &wait);
319 spin_unlock_irq(&ctx->ctx_lock);
322 /* wait_on_sync_kiocb:
323 * Waits on the given sync kiocb to complete.
325 ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
327 while (iocb->ki_users) {
328 set_current_state(TASK_UNINTERRUPTIBLE);
333 __set_current_state(TASK_RUNNING);
334 return iocb->ki_user_data;
337 /* exit_aio: called when the last user of mm goes away. At this point,
338 * there is no way for any new requests to be submited or any of the
339 * io_* syscalls to be called on the context. However, there may be
340 * outstanding requests which hold references to the context; as they
341 * go away, they will call put_ioctx and release any pinned memory
342 * associated with the request (held via struct page * references).
344 void exit_aio(struct mm_struct *mm)
346 struct kioctx *ctx = mm->ioctx_list;
347 mm->ioctx_list = NULL;
349 struct kioctx *next = ctx->next;
353 wait_for_all_aios(ctx);
355 * Ensure we don't leave the ctx on the aio_wq
357 cancel_work_sync(&ctx->wq.work);
359 if (1 != atomic_read(&ctx->users))
361 "exit_aio:ioctx still alive: %d %d %d\n",
362 atomic_read(&ctx->users), ctx->dead,
370 * Called when the last user of an aio context has gone away,
371 * and the struct needs to be freed.
373 void __put_ioctx(struct kioctx *ctx)
375 unsigned nr_events = ctx->max_reqs;
377 BUG_ON(ctx->reqs_active);
379 cancel_delayed_work(&ctx->wq);
380 cancel_work_sync(&ctx->wq.work);
384 pr_debug("__put_ioctx: freeing %p\n", ctx);
385 kmem_cache_free(kioctx_cachep, ctx);
388 spin_lock(&aio_nr_lock);
389 BUG_ON(aio_nr - nr_events > aio_nr);
391 spin_unlock(&aio_nr_lock);
396 * Allocate a slot for an aio request. Increments the users count
397 * of the kioctx so that the kioctx stays around until all requests are
398 * complete. Returns NULL if no requests are free.
400 * Returns with kiocb->users set to 2. The io submit code path holds
401 * an extra reference while submitting the i/o.
402 * This prevents races between the aio code path referencing the
403 * req (after submitting it) and aio_complete() freeing the req.
405 static struct kiocb *__aio_get_req(struct kioctx *ctx)
407 struct kiocb *req = NULL;
408 struct aio_ring *ring;
411 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
419 req->ki_cancel = NULL;
420 req->ki_retry = NULL;
423 req->ki_iovec = NULL;
424 INIT_LIST_HEAD(&req->ki_run_list);
425 req->ki_eventfd = ERR_PTR(-EINVAL);
427 /* Check if the completion queue has enough free space to
428 * accept an event from this io.
430 spin_lock_irq(&ctx->ctx_lock);
431 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
432 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
433 list_add(&req->ki_list, &ctx->active_reqs);
437 kunmap_atomic(ring, KM_USER0);
438 spin_unlock_irq(&ctx->ctx_lock);
441 kmem_cache_free(kiocb_cachep, req);
448 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
451 /* Handle a potential starvation case -- should be exceedingly rare as
452 * requests will be stuck on fput_head only if the aio_fput_routine is
453 * delayed and the requests were the last user of the struct file.
455 req = __aio_get_req(ctx);
456 if (unlikely(NULL == req)) {
457 aio_fput_routine(NULL);
458 req = __aio_get_req(ctx);
463 static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
465 assert_spin_locked(&ctx->ctx_lock);
467 if (!IS_ERR(req->ki_eventfd))
468 fput(req->ki_eventfd);
471 if (req->ki_iovec != &req->ki_inline_vec)
472 kfree(req->ki_iovec);
473 kmem_cache_free(kiocb_cachep, req);
476 if (unlikely(!ctx->reqs_active && ctx->dead))
480 static void aio_fput_routine(struct work_struct *data)
482 spin_lock_irq(&fput_lock);
483 while (likely(!list_empty(&fput_head))) {
484 struct kiocb *req = list_kiocb(fput_head.next);
485 struct kioctx *ctx = req->ki_ctx;
487 list_del(&req->ki_list);
488 spin_unlock_irq(&fput_lock);
490 /* Complete the fput */
491 __fput(req->ki_filp);
493 /* Link the iocb into the context's free list */
494 spin_lock_irq(&ctx->ctx_lock);
495 really_put_req(ctx, req);
496 spin_unlock_irq(&ctx->ctx_lock);
499 spin_lock_irq(&fput_lock);
501 spin_unlock_irq(&fput_lock);
505 * Returns true if this put was the last user of the request.
507 static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
509 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
510 req, atomic_read(&req->ki_filp->f_count));
512 assert_spin_locked(&ctx->ctx_lock);
515 BUG_ON(req->ki_users < 0);
516 if (likely(req->ki_users))
518 list_del(&req->ki_list); /* remove from active_reqs */
519 req->ki_cancel = NULL;
520 req->ki_retry = NULL;
522 /* Must be done under the lock to serialise against cancellation.
523 * Call this aio_fput as it duplicates fput via the fput_work.
525 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
527 spin_lock(&fput_lock);
528 list_add(&req->ki_list, &fput_head);
529 spin_unlock(&fput_lock);
530 queue_work(aio_wq, &fput_work);
532 really_put_req(ctx, req);
537 * Returns true if this put was the last user of the kiocb,
538 * false if the request is still in use.
540 int aio_put_req(struct kiocb *req)
542 struct kioctx *ctx = req->ki_ctx;
544 spin_lock_irq(&ctx->ctx_lock);
545 ret = __aio_put_req(ctx, req);
546 spin_unlock_irq(&ctx->ctx_lock);
550 /* Lookup an ioctx id. ioctx_list is lockless for reads.
551 * FIXME: this is O(n) and is only suitable for development.
553 struct kioctx *lookup_ioctx(unsigned long ctx_id)
555 struct kioctx *ioctx;
556 struct mm_struct *mm;
559 read_lock(&mm->ioctx_list_lock);
560 for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
561 if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
565 read_unlock(&mm->ioctx_list_lock);
572 * Makes the calling kernel thread take on the specified
574 * Called by the retry thread execute retries within the
575 * iocb issuer's mm context, so that copy_from/to_user
576 * operations work seamlessly for aio.
577 * (Note: this routine is intended to be called only
578 * from a kernel thread context)
580 static void use_mm(struct mm_struct *mm)
582 struct mm_struct *active_mm;
583 struct task_struct *tsk = current;
586 tsk->flags |= PF_BORROWED_MM;
587 active_mm = tsk->active_mm;
588 atomic_inc(&mm->mm_count);
592 * Note that on UML this *requires* PF_BORROWED_MM to be set, otherwise
593 * it won't work. Update it accordingly if you change it here
595 switch_mm(active_mm, mm, tsk);
603 * Reverses the effect of use_mm, i.e. releases the
604 * specified mm context which was earlier taken on
605 * by the calling kernel thread
606 * (Note: this routine is intended to be called only
607 * from a kernel thread context)
609 static void unuse_mm(struct mm_struct *mm)
611 struct task_struct *tsk = current;
614 tsk->flags &= ~PF_BORROWED_MM;
616 /* active_mm is still 'mm' */
617 enter_lazy_tlb(mm, tsk);
622 * Queue up a kiocb to be retried. Assumes that the kiocb
623 * has already been marked as kicked, and places it on
624 * the retry run list for the corresponding ioctx, if it
625 * isn't already queued. Returns 1 if it actually queued
626 * the kiocb (to tell the caller to activate the work
627 * queue to process it), or 0, if it found that it was
630 static inline int __queue_kicked_iocb(struct kiocb *iocb)
632 struct kioctx *ctx = iocb->ki_ctx;
634 assert_spin_locked(&ctx->ctx_lock);
636 if (list_empty(&iocb->ki_run_list)) {
637 list_add_tail(&iocb->ki_run_list,
645 * This is the core aio execution routine. It is
646 * invoked both for initial i/o submission and
647 * subsequent retries via the aio_kick_handler.
648 * Expects to be invoked with iocb->ki_ctx->lock
649 * already held. The lock is released and reacquired
650 * as needed during processing.
652 * Calls the iocb retry method (already setup for the
653 * iocb on initial submission) for operation specific
654 * handling, but takes care of most of common retry
655 * execution details for a given iocb. The retry method
656 * needs to be non-blocking as far as possible, to avoid
657 * holding up other iocbs waiting to be serviced by the
658 * retry kernel thread.
660 * The trickier parts in this code have to do with
661 * ensuring that only one retry instance is in progress
662 * for a given iocb at any time. Providing that guarantee
663 * simplifies the coding of individual aio operations as
664 * it avoids various potential races.
666 static ssize_t aio_run_iocb(struct kiocb *iocb)
668 struct kioctx *ctx = iocb->ki_ctx;
669 ssize_t (*retry)(struct kiocb *);
672 if (!(retry = iocb->ki_retry)) {
673 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
678 * We don't want the next retry iteration for this
679 * operation to start until this one has returned and
680 * updated the iocb state. However, wait_queue functions
681 * can trigger a kick_iocb from interrupt context in the
682 * meantime, indicating that data is available for the next
683 * iteration. We want to remember that and enable the
684 * next retry iteration _after_ we are through with
687 * So, in order to be able to register a "kick", but
688 * prevent it from being queued now, we clear the kick
689 * flag, but make the kick code *think* that the iocb is
690 * still on the run list until we are actually done.
691 * When we are done with this iteration, we check if
692 * the iocb was kicked in the meantime and if so, queue
696 kiocbClearKicked(iocb);
699 * This is so that aio_complete knows it doesn't need to
700 * pull the iocb off the run list (We can't just call
701 * INIT_LIST_HEAD because we don't want a kick_iocb to
702 * queue this on the run list yet)
704 iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
705 spin_unlock_irq(&ctx->ctx_lock);
707 /* Quit retrying if the i/o has been cancelled */
708 if (kiocbIsCancelled(iocb)) {
710 aio_complete(iocb, ret, 0);
711 /* must not access the iocb after this */
716 * Now we are all set to call the retry method in async
721 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
722 BUG_ON(!list_empty(&iocb->ki_wait.task_list));
723 aio_complete(iocb, ret, 0);
726 spin_lock_irq(&ctx->ctx_lock);
728 if (-EIOCBRETRY == ret) {
730 * OK, now that we are done with this iteration
731 * and know that there is more left to go,
732 * this is where we let go so that a subsequent
733 * "kick" can start the next iteration
736 /* will make __queue_kicked_iocb succeed from here on */
737 INIT_LIST_HEAD(&iocb->ki_run_list);
738 /* we must queue the next iteration ourselves, if it
739 * has already been kicked */
740 if (kiocbIsKicked(iocb)) {
741 __queue_kicked_iocb(iocb);
744 * __queue_kicked_iocb will always return 1 here, because
745 * iocb->ki_run_list is empty at this point so it should
746 * be safe to unconditionally queue the context into the
757 * Process all pending retries queued on the ioctx
759 * Assumes it is operating within the aio issuer's mm
762 static int __aio_run_iocbs(struct kioctx *ctx)
765 struct list_head run_list;
767 assert_spin_locked(&ctx->ctx_lock);
769 list_replace_init(&ctx->run_list, &run_list);
770 while (!list_empty(&run_list)) {
771 iocb = list_entry(run_list.next, struct kiocb,
773 list_del(&iocb->ki_run_list);
775 * Hold an extra reference while retrying i/o.
777 iocb->ki_users++; /* grab extra reference */
779 __aio_put_req(ctx, iocb);
781 if (!list_empty(&ctx->run_list))
786 static void aio_queue_work(struct kioctx * ctx)
788 unsigned long timeout;
790 * if someone is waiting, get the work started right
791 * away, otherwise, use a longer delay
794 if (waitqueue_active(&ctx->wait))
798 queue_delayed_work(aio_wq, &ctx->wq, timeout);
804 * Process all pending retries queued on the ioctx
806 * Assumes it is operating within the aio issuer's mm
809 static inline void aio_run_iocbs(struct kioctx *ctx)
813 spin_lock_irq(&ctx->ctx_lock);
815 requeue = __aio_run_iocbs(ctx);
816 spin_unlock_irq(&ctx->ctx_lock);
822 * just like aio_run_iocbs, but keeps running them until
823 * the list stays empty
825 static inline void aio_run_all_iocbs(struct kioctx *ctx)
827 spin_lock_irq(&ctx->ctx_lock);
828 while (__aio_run_iocbs(ctx))
830 spin_unlock_irq(&ctx->ctx_lock);
835 * Work queue handler triggered to process pending
836 * retries on an ioctx. Takes on the aio issuer's
837 * mm context before running the iocbs, so that
838 * copy_xxx_user operates on the issuer's address
840 * Run on aiod's context.
842 static void aio_kick_handler(struct work_struct *work)
844 struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
845 mm_segment_t oldfs = get_fs();
846 struct mm_struct *mm;
851 spin_lock_irq(&ctx->ctx_lock);
852 requeue =__aio_run_iocbs(ctx);
854 spin_unlock_irq(&ctx->ctx_lock);
858 * we're in a worker thread already, don't use queue_delayed_work,
861 queue_delayed_work(aio_wq, &ctx->wq, 0);
866 * Called by kick_iocb to queue the kiocb for retry
867 * and if required activate the aio work queue to process
870 static void try_queue_kicked_iocb(struct kiocb *iocb)
872 struct kioctx *ctx = iocb->ki_ctx;
876 /* We're supposed to be the only path putting the iocb back on the run
877 * list. If we find that the iocb is *back* on a wait queue already
878 * than retry has happened before we could queue the iocb. This also
879 * means that the retry could have completed and freed our iocb, no
881 BUG_ON((!list_empty(&iocb->ki_wait.task_list)));
883 spin_lock_irqsave(&ctx->ctx_lock, flags);
884 /* set this inside the lock so that we can't race with aio_run_iocb()
885 * testing it and putting the iocb on the run list under the lock */
886 if (!kiocbTryKick(iocb))
887 run = __queue_kicked_iocb(iocb);
888 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
895 * Called typically from a wait queue callback context
896 * (aio_wake_function) to trigger a retry of the iocb.
897 * The retry is usually executed by aio workqueue
898 * threads (See aio_kick_handler).
900 void kick_iocb(struct kiocb *iocb)
902 /* sync iocbs are easy: they can only ever be executing from a
904 if (is_sync_kiocb(iocb)) {
905 kiocbSetKicked(iocb);
906 wake_up_process(iocb->ki_obj.tsk);
910 try_queue_kicked_iocb(iocb);
912 EXPORT_SYMBOL(kick_iocb);
915 * Called when the io request on the given iocb is complete.
916 * Returns true if this is the last user of the request. The
917 * only other user of the request can be the cancellation code.
919 int aio_complete(struct kiocb *iocb, long res, long res2)
921 struct kioctx *ctx = iocb->ki_ctx;
922 struct aio_ring_info *info;
923 struct aio_ring *ring;
924 struct io_event *event;
930 * Special case handling for sync iocbs:
931 * - events go directly into the iocb for fast handling
932 * - the sync task with the iocb in its stack holds the single iocb
933 * ref, no other paths have a way to get another ref
934 * - the sync task helpfully left a reference to itself in the iocb
936 if (is_sync_kiocb(iocb)) {
937 BUG_ON(iocb->ki_users != 1);
938 iocb->ki_user_data = res;
940 wake_up_process(iocb->ki_obj.tsk);
945 * Check if the user asked us to deliver the result through an
946 * eventfd. The eventfd_signal() function is safe to be called
949 if (!IS_ERR(iocb->ki_eventfd))
950 eventfd_signal(iocb->ki_eventfd, 1);
952 info = &ctx->ring_info;
954 /* add a completion event to the ring buffer.
955 * must be done holding ctx->ctx_lock to prevent
956 * other code from messing with the tail
957 * pointer since we might be called from irq
960 spin_lock_irqsave(&ctx->ctx_lock, flags);
962 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
963 list_del_init(&iocb->ki_run_list);
966 * cancelled requests don't get events, userland was given one
967 * when the event got cancelled.
969 if (kiocbIsCancelled(iocb))
972 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
975 event = aio_ring_event(info, tail, KM_IRQ0);
976 if (++tail >= info->nr)
979 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
980 event->data = iocb->ki_user_data;
984 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
985 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
988 /* after flagging the request as done, we
989 * must never even look at it again
991 smp_wmb(); /* make event visible before updating tail */
996 put_aio_ring_event(event, KM_IRQ0);
997 kunmap_atomic(ring, KM_IRQ1);
999 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
1001 /* everything turned out well, dispose of the aiocb. */
1002 ret = __aio_put_req(ctx, iocb);
1005 * We have to order our ring_info tail store above and test
1006 * of the wait list below outside the wait lock. This is
1007 * like in wake_up_bit() where clearing a bit has to be
1008 * ordered with the unlocked test.
1012 if (waitqueue_active(&ctx->wait))
1013 wake_up(&ctx->wait);
1016 if (ctx->file && waitqueue_active(&ctx->poll_wait))
1017 wake_up(&ctx->poll_wait);
1020 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
1025 * Pull an event off of the ioctx's event ring. Returns the number of
1026 * events fetched (0 or 1 ;-)
1027 * If ent parameter is 0, just returns the number of events that would
1029 * FIXME: make this use cmpxchg.
1030 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1032 static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1034 struct aio_ring_info *info = &ioctx->ring_info;
1035 struct aio_ring *ring;
1039 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
1040 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1041 (unsigned long)ring->head, (unsigned long)ring->tail,
1042 (unsigned long)ring->nr);
1044 if (ring->head == ring->tail)
1047 spin_lock(&info->ring_lock);
1049 head = ring->head % info->nr;
1050 if (head != ring->tail) {
1051 if (ent) { /* event requested */
1052 struct io_event *evp =
1053 aio_ring_event(info, head, KM_USER1);
1055 head = (head + 1) % info->nr;
1056 /* finish reading the event before updatng the head */
1060 put_aio_ring_event(evp, KM_USER1);
1061 } else /* only need to know availability */
1064 spin_unlock(&info->ring_lock);
1067 kunmap_atomic(ring, KM_USER0);
1068 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
1069 (unsigned long)ring->head, (unsigned long)ring->tail);
1073 struct aio_timeout {
1074 struct timer_list timer;
1076 struct task_struct *p;
1079 static void timeout_func(unsigned long data)
1081 struct aio_timeout *to = (struct aio_timeout *)data;
1084 wake_up_process(to->p);
1087 static inline void init_timeout(struct aio_timeout *to)
1089 init_timer(&to->timer);
1090 to->timer.data = (unsigned long)to;
1091 to->timer.function = timeout_func;
1096 static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1097 const struct timespec *ts)
1099 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
1100 if (time_after(to->timer.expires, jiffies))
1101 add_timer(&to->timer);
1106 static inline void clear_timeout(struct aio_timeout *to)
1108 del_singleshot_timer_sync(&to->timer);
1111 static int read_events(struct kioctx *ctx,
1112 long min_nr, long nr,
1113 struct io_event __user *event,
1114 struct timespec __user *timeout)
1116 long start_jiffies = jiffies;
1117 struct task_struct *tsk = current;
1118 DECLARE_WAITQUEUE(wait, tsk);
1121 struct io_event ent;
1122 struct aio_timeout to;
1125 /* needed to zero any padding within an entry (there shouldn't be
1126 * any, but C is fun!
1128 memset(&ent, 0, sizeof(ent));
1131 while (likely(i < nr)) {
1132 ret = aio_read_evt(ctx, &ent);
1133 if (unlikely(ret <= 0))
1136 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1137 ent.data, ent.obj, ent.res, ent.res2);
1139 /* Could we split the check in two? */
1141 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1142 dprintk("aio: lost an event due to EFAULT.\n");
1147 /* Good, event copied to userland, update counts. */
1159 /* racey check, but it gets redone */
1160 if (!retry && unlikely(!list_empty(&ctx->run_list))) {
1162 aio_run_all_iocbs(ctx);
1170 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
1173 set_timeout(start_jiffies, &to, &ts);
1176 while (likely(i < nr)) {
1177 add_wait_queue_exclusive(&ctx->wait, &wait);
1179 set_task_state(tsk, TASK_INTERRUPTIBLE);
1180 ret = aio_read_evt(ctx, &ent);
1186 if (to.timed_out) /* Only check after read evt */
1188 /* Try to only show up in io wait if there are ops
1190 if (ctx->reqs_active)
1194 if (signal_pending(tsk)) {
1198 /*ret = aio_read_evt(ctx, &ent);*/
1201 set_task_state(tsk, TASK_RUNNING);
1202 remove_wait_queue(&ctx->wait, &wait);
1204 if (unlikely(ret <= 0))
1208 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1209 dprintk("aio: lost an event due to EFAULT.\n");
1213 /* Good, event copied to userland, update counts. */
1224 /* Take an ioctx and remove it from the list of ioctx's. Protects
1225 * against races with itself via ->dead.
1227 static void io_destroy(struct kioctx *ioctx)
1229 struct mm_struct *mm = current->mm;
1230 struct kioctx **tmp;
1233 /* delete the entry from the list is someone else hasn't already */
1234 write_lock(&mm->ioctx_list_lock);
1235 was_dead = ioctx->dead;
1237 for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
1238 tmp = &(*tmp)->next)
1242 write_unlock(&mm->ioctx_list_lock);
1244 dprintk("aio_release(%p)\n", ioctx);
1245 if (likely(!was_dead))
1246 put_ioctx(ioctx); /* twice for the list */
1248 aio_cancel_all(ioctx);
1249 wait_for_all_aios(ioctx);
1251 /* forget the poll file, but it's up to the user to close it */
1253 ioctx->file->private_data = 0;
1257 put_ioctx(ioctx); /* once for the lookup */
1262 static int aio_queue_fd_close(struct inode *inode, struct file *file)
1264 struct kioctx *ioctx = file->private_data;
1266 file->private_data = 0;
1267 spin_lock_irq(&ioctx->ctx_lock);
1269 spin_unlock_irq(&ioctx->ctx_lock);
1274 static unsigned int aio_queue_fd_poll(struct file *file, poll_table *wait)
1275 { unsigned int pollflags = 0;
1276 struct kioctx *ioctx = file->private_data;
1280 spin_lock_irq(&ioctx->ctx_lock);
1281 /* Insert inside our poll wait queue */
1282 poll_wait(file, &ioctx->poll_wait, wait);
1284 /* Check our condition */
1285 if (aio_read_evt(ioctx, 0))
1286 pollflags = POLLIN | POLLRDNORM;
1287 spin_unlock_irq(&ioctx->ctx_lock);
1293 static const struct file_operations aioq_fops = {
1294 .release = aio_queue_fd_close,
1295 .poll = aio_queue_fd_poll
1299 * Create a file descriptor that can be used to poll the event queue.
1300 * Based on the excellent epoll code.
1303 static int make_aio_fd(struct kioctx *ioctx)
1306 struct inode *inode;
1309 error = anon_inode_getfd(&fd, &inode, &file, "[aioq]",
1314 /* associate the file with the IO context */
1315 file->private_data = ioctx;
1317 init_waitqueue_head(&ioctx->poll_wait);
1324 * Create an aio_context capable of receiving at least nr_events.
1325 * ctxp must not point to an aio_context that already exists, and
1326 * must be initialized to 0 prior to the call. On successful
1327 * creation of the aio_context, *ctxp is filled in with the resulting
1328 * handle. May fail with -EINVAL if *ctxp is not initialized,
1329 * if the specified nr_events exceeds internal limits. May fail
1330 * with -EAGAIN if the specified nr_events exceeds the user's limit
1331 * of available events. May fail with -ENOMEM if insufficient kernel
1332 * resources are available. May fail with -EFAULT if an invalid
1333 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1336 * To request a selectable fd, the user context has to be initialized
1337 * to 1, instead of 0, and the return value is the fd.
1338 * This keeps the system call compatible, since a non-zero value
1339 * was not allowed so far.
1341 asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
1343 struct kioctx *ioctx = NULL;
1348 ret = get_user(ctx, ctxp);
1359 if (unlikely(ctx || nr_events == 0)) {
1360 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1365 ioctx = ioctx_alloc(nr_events);
1366 ret = PTR_ERR(ioctx);
1367 if (!IS_ERR(ioctx)) {
1368 ret = put_user(ioctx->user_id, ctxp);
1370 if (make_fd && ret >= 0)
1371 ret = make_aio_fd(ioctx);
1376 get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
1385 * Destroy the aio_context specified. May cancel any outstanding
1386 * AIOs and block on completion. Will fail with -ENOSYS if not
1387 * implemented. May fail with -EFAULT if the context pointed to
1390 asmlinkage long sys_io_destroy(aio_context_t ctx)
1392 struct kioctx *ioctx = lookup_ioctx(ctx);
1393 if (likely(NULL != ioctx)) {
1397 pr_debug("EINVAL: io_destroy: invalid context id\n");
1401 static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
1403 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
1407 while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
1408 ssize_t this = min((ssize_t)iov->iov_len, ret);
1409 iov->iov_base += this;
1410 iov->iov_len -= this;
1411 iocb->ki_left -= this;
1413 if (iov->iov_len == 0) {
1419 /* the caller should not have done more io than what fit in
1420 * the remaining iovecs */
1421 BUG_ON(ret > 0 && iocb->ki_left == 0);
1424 static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
1426 struct file *file = iocb->ki_filp;
1427 struct address_space *mapping = file->f_mapping;
1428 struct inode *inode = mapping->host;
1429 ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
1430 unsigned long, loff_t);
1432 unsigned short opcode;
1434 if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
1435 (iocb->ki_opcode == IOCB_CMD_PREAD)) {
1436 rw_op = file->f_op->aio_read;
1437 opcode = IOCB_CMD_PREADV;
1439 rw_op = file->f_op->aio_write;
1440 opcode = IOCB_CMD_PWRITEV;
1443 /* This matches the pread()/pwrite() logic */
1444 if (iocb->ki_pos < 0)
1448 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
1449 iocb->ki_nr_segs - iocb->ki_cur_seg,
1452 aio_advance_iovec(iocb, ret);
1454 /* retry all partial writes. retry partial reads as long as its a
1456 } while (ret > 0 && iocb->ki_left > 0 &&
1457 (opcode == IOCB_CMD_PWRITEV ||
1458 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
1460 /* This means we must have transferred all that we could */
1461 /* No need to retry anymore */
1462 if ((ret == 0) || (iocb->ki_left == 0))
1463 ret = iocb->ki_nbytes - iocb->ki_left;
1465 /* If we managed to write some out we return that, rather than
1466 * the eventual error. */
1467 if (opcode == IOCB_CMD_PWRITEV
1468 && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
1469 && iocb->ki_nbytes - iocb->ki_left)
1470 ret = iocb->ki_nbytes - iocb->ki_left;
1475 static ssize_t aio_fdsync(struct kiocb *iocb)
1477 struct file *file = iocb->ki_filp;
1478 ssize_t ret = -EINVAL;
1480 if (file->f_op->aio_fsync)
1481 ret = file->f_op->aio_fsync(iocb, 1);
1485 static ssize_t aio_fsync(struct kiocb *iocb)
1487 struct file *file = iocb->ki_filp;
1488 ssize_t ret = -EINVAL;
1490 if (file->f_op->aio_fsync)
1491 ret = file->f_op->aio_fsync(iocb, 0);
1495 static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
1499 ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
1500 kiocb->ki_nbytes, 1,
1501 &kiocb->ki_inline_vec, &kiocb->ki_iovec);
1505 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1506 kiocb->ki_cur_seg = 0;
1507 /* ki_nbytes/left now reflect bytes instead of segs */
1508 kiocb->ki_nbytes = ret;
1509 kiocb->ki_left = ret;
1516 static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
1518 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1519 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
1520 kiocb->ki_iovec->iov_len = kiocb->ki_left;
1521 kiocb->ki_nr_segs = 1;
1522 kiocb->ki_cur_seg = 0;
1528 * Performs the initial checks and aio retry method
1529 * setup for the kiocb at the time of io submission.
1531 static ssize_t aio_setup_iocb(struct kiocb *kiocb)
1533 struct file *file = kiocb->ki_filp;
1536 switch (kiocb->ki_opcode) {
1537 case IOCB_CMD_PREAD:
1539 if (unlikely(!(file->f_mode & FMODE_READ)))
1542 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1545 ret = security_file_permission(file, MAY_READ);
1548 ret = aio_setup_single_vector(kiocb);
1552 if (file->f_op->aio_read)
1553 kiocb->ki_retry = aio_rw_vect_retry;
1555 case IOCB_CMD_PWRITE:
1557 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1560 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1563 ret = security_file_permission(file, MAY_WRITE);
1566 ret = aio_setup_single_vector(kiocb);
1570 if (file->f_op->aio_write)
1571 kiocb->ki_retry = aio_rw_vect_retry;
1573 case IOCB_CMD_PREADV:
1575 if (unlikely(!(file->f_mode & FMODE_READ)))
1577 ret = security_file_permission(file, MAY_READ);
1580 ret = aio_setup_vectored_rw(READ, kiocb);
1584 if (file->f_op->aio_read)
1585 kiocb->ki_retry = aio_rw_vect_retry;
1587 case IOCB_CMD_PWRITEV:
1589 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1591 ret = security_file_permission(file, MAY_WRITE);
1594 ret = aio_setup_vectored_rw(WRITE, kiocb);
1598 if (file->f_op->aio_write)
1599 kiocb->ki_retry = aio_rw_vect_retry;
1601 case IOCB_CMD_FDSYNC:
1603 if (file->f_op->aio_fsync)
1604 kiocb->ki_retry = aio_fdsync;
1606 case IOCB_CMD_FSYNC:
1608 if (file->f_op->aio_fsync)
1609 kiocb->ki_retry = aio_fsync;
1612 dprintk("EINVAL: io_submit: no operation provided\n");
1616 if (!kiocb->ki_retry)
1623 * aio_wake_function:
1624 * wait queue callback function for aio notification,
1625 * Simply triggers a retry of the operation via kick_iocb.
1627 * This callback is specified in the wait queue entry in
1631 * This routine is executed with the wait queue lock held.
1632 * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
1633 * the ioctx lock inside the wait queue lock. This is safe
1634 * because this callback isn't used for wait queues which
1635 * are nested inside ioctx lock (i.e. ctx->wait)
1637 static int aio_wake_function(wait_queue_t *wait, unsigned mode,
1638 int sync, void *key)
1640 struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
1642 list_del_init(&wait->task_list);
1647 int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1654 /* enforce forwards compatibility on users */
1655 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
1656 pr_debug("EINVAL: io_submit: reserve field set\n");
1660 /* prevent overflows */
1662 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1663 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1664 ((ssize_t)iocb->aio_nbytes < 0)
1666 pr_debug("EINVAL: io_submit: overflow check\n");
1670 file = fget(iocb->aio_fildes);
1671 if (unlikely(!file))
1674 req = aio_get_req(ctx); /* returns with 2 references to req */
1675 if (unlikely(!req)) {
1679 req->ki_filp = file;
1680 if (iocb->aio_flags & IOCB_FLAG_RESFD) {
1682 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1683 * instance of the file* now. The file descriptor must be
1684 * an eventfd() fd, and will be signaled for each completed
1685 * event using the eventfd_signal() function.
1687 req->ki_eventfd = eventfd_fget((int) iocb->aio_resfd);
1688 if (unlikely(IS_ERR(req->ki_eventfd))) {
1689 ret = PTR_ERR(req->ki_eventfd);
1694 ret = put_user(req->ki_key, &user_iocb->aio_key);
1695 if (unlikely(ret)) {
1696 dprintk("EFAULT: aio_key\n");
1700 req->ki_obj.user = user_iocb;
1701 req->ki_user_data = iocb->aio_data;
1702 req->ki_pos = iocb->aio_offset;
1704 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1705 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1706 req->ki_opcode = iocb->aio_lio_opcode;
1707 init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
1708 INIT_LIST_HEAD(&req->ki_wait.task_list);
1710 ret = aio_setup_iocb(req);
1715 spin_lock_irq(&ctx->ctx_lock);
1717 if (!list_empty(&ctx->run_list)) {
1718 /* drain the run list */
1719 while (__aio_run_iocbs(ctx))
1722 spin_unlock_irq(&ctx->ctx_lock);
1723 aio_put_req(req); /* drop extra ref to req */
1727 aio_put_req(req); /* drop extra ref to req */
1728 aio_put_req(req); /* drop i/o ref to req */
1733 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1734 * the number of iocbs queued. May return -EINVAL if the aio_context
1735 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1736 * *iocbpp[0] is not properly initialized, if the operation specified
1737 * is invalid for the file descriptor in the iocb. May fail with
1738 * -EFAULT if any of the data structures point to invalid data. May
1739 * fail with -EBADF if the file descriptor specified in the first
1740 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1741 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1742 * fail with -ENOSYS if not implemented.
1744 asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1745 struct iocb __user * __user *iocbpp)
1751 if (unlikely(nr < 0))
1754 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1757 ctx = lookup_ioctx(ctx_id);
1758 if (unlikely(!ctx)) {
1759 pr_debug("EINVAL: io_submit: invalid context id\n");
1764 * AKPM: should this return a partial result if some of the IOs were
1765 * successfully submitted?
1767 for (i=0; i<nr; i++) {
1768 struct iocb __user *user_iocb;
1771 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1776 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1781 ret = io_submit_one(ctx, user_iocb, &tmp);
1791 * Finds a given iocb for cancellation.
1793 static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1796 struct list_head *pos;
1798 assert_spin_locked(&ctx->ctx_lock);
1800 /* TODO: use a hash or array, this sucks. */
1801 list_for_each(pos, &ctx->active_reqs) {
1802 struct kiocb *kiocb = list_kiocb(pos);
1803 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1810 * Attempts to cancel an iocb previously passed to io_submit. If
1811 * the operation is successfully cancelled, the resulting event is
1812 * copied into the memory pointed to by result without being placed
1813 * into the completion queue and 0 is returned. May fail with
1814 * -EFAULT if any of the data structures pointed to are invalid.
1815 * May fail with -EINVAL if aio_context specified by ctx_id is
1816 * invalid. May fail with -EAGAIN if the iocb specified was not
1817 * cancelled. Will fail with -ENOSYS if not implemented.
1819 asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
1820 struct io_event __user *result)
1822 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1824 struct kiocb *kiocb;
1828 ret = get_user(key, &iocb->aio_key);
1832 ctx = lookup_ioctx(ctx_id);
1836 spin_lock_irq(&ctx->ctx_lock);
1838 kiocb = lookup_kiocb(ctx, iocb, key);
1839 if (kiocb && kiocb->ki_cancel) {
1840 cancel = kiocb->ki_cancel;
1842 kiocbSetCancelled(kiocb);
1845 spin_unlock_irq(&ctx->ctx_lock);
1847 if (NULL != cancel) {
1848 struct io_event tmp;
1849 pr_debug("calling cancel\n");
1850 memset(&tmp, 0, sizeof(tmp));
1851 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1852 tmp.data = kiocb->ki_user_data;
1853 ret = cancel(kiocb, &tmp);
1855 /* Cancellation succeeded -- copy the result
1856 * into the user's buffer.
1858 if (copy_to_user(result, &tmp, sizeof(tmp)))
1870 * Attempts to read at least min_nr events and up to nr events from
1871 * the completion queue for the aio_context specified by ctx_id. May
1872 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1873 * if nr is out of range, if when is out of range. May fail with
1874 * -EFAULT if any of the memory specified to is invalid. May return
1875 * 0 or < min_nr if no events are available and the timeout specified
1876 * by when has elapsed, where when == NULL specifies an infinite
1877 * timeout. Note that the timeout pointed to by when is relative and
1878 * will be updated if not NULL and the operation blocks. Will fail
1879 * with -ENOSYS if not implemented.
1881 asmlinkage long sys_io_getevents(aio_context_t ctx_id,
1884 struct io_event __user *events,
1885 struct timespec __user *timeout)
1887 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1890 if (likely(ioctx)) {
1891 if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
1892 ret = read_events(ioctx, min_nr, nr, events, timeout);
1899 __initcall(aio_setup);
1901 EXPORT_SYMBOL(aio_complete);
1902 EXPORT_SYMBOL(aio_put_req);
1903 EXPORT_SYMBOL(wait_on_sync_kiocb);