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/mmu_context.h>
28 #include <linux/slab.h>
29 #include <linux/timer.h>
30 #include <linux/aio.h>
31 #include <linux/highmem.h>
32 #include <linux/workqueue.h>
33 #include <linux/security.h>
34 #include <linux/eventfd.h>
35 #include <linux/blkdev.h>
36 #include <linux/mempool.h>
37 #include <linux/hash.h>
39 #include <asm/kmap_types.h>
40 #include <asm/uaccess.h>
43 #define dprintk printk
45 #define dprintk(x...) do { ; } while (0)
48 /*------ sysctl variables----*/
49 static DEFINE_SPINLOCK(aio_nr_lock);
50 unsigned long aio_nr; /* current system wide number of aio requests */
51 unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
52 /*----end sysctl variables---*/
54 static struct kmem_cache *kiocb_cachep;
55 static struct kmem_cache *kioctx_cachep;
57 static struct workqueue_struct *aio_wq;
59 /* Used for rare fput completion. */
60 static void aio_fput_routine(struct work_struct *);
61 static DECLARE_WORK(fput_work, aio_fput_routine);
63 static DEFINE_SPINLOCK(fput_lock);
64 static LIST_HEAD(fput_head);
66 #define AIO_BATCH_HASH_BITS 3 /* allocated on-stack, so don't go crazy */
67 #define AIO_BATCH_HASH_SIZE (1 << AIO_BATCH_HASH_BITS)
68 struct aio_batch_entry {
69 struct hlist_node list;
70 struct address_space *mapping;
74 static void aio_kick_handler(struct work_struct *);
75 static void aio_queue_work(struct kioctx *);
78 * Creates the slab caches used by the aio routines, panic on
79 * failure as this is done early during the boot sequence.
81 static int __init aio_setup(void)
83 kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
84 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
86 aio_wq = create_workqueue("aio");
87 abe_pool = mempool_create_kmalloc_pool(1, sizeof(struct aio_batch_entry));
90 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
94 __initcall(aio_setup);
96 static void aio_free_ring(struct kioctx *ctx)
98 struct aio_ring_info *info = &ctx->ring_info;
101 for (i=0; i<info->nr_pages; i++)
102 put_page(info->ring_pages[i]);
104 if (info->mmap_size) {
105 down_write(&ctx->mm->mmap_sem);
106 do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
107 up_write(&ctx->mm->mmap_sem);
110 if (info->ring_pages && info->ring_pages != info->internal_pages)
111 kfree(info->ring_pages);
112 info->ring_pages = NULL;
116 static int aio_setup_ring(struct kioctx *ctx)
118 struct aio_ring *ring;
119 struct aio_ring_info *info = &ctx->ring_info;
120 unsigned nr_events = ctx->max_reqs;
124 /* Compensate for the ring buffer's head/tail overlap entry */
125 nr_events += 2; /* 1 is required, 2 for good luck */
127 size = sizeof(struct aio_ring);
128 size += sizeof(struct io_event) * nr_events;
129 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
134 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
137 info->ring_pages = info->internal_pages;
138 if (nr_pages > AIO_RING_PAGES) {
139 info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
140 if (!info->ring_pages)
144 info->mmap_size = nr_pages * PAGE_SIZE;
145 dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
146 down_write(&ctx->mm->mmap_sem);
147 info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
148 PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
150 if (IS_ERR((void *)info->mmap_base)) {
151 up_write(&ctx->mm->mmap_sem);
157 dprintk("mmap address: 0x%08lx\n", info->mmap_base);
158 info->nr_pages = get_user_pages(current, ctx->mm,
159 info->mmap_base, nr_pages,
160 1, 0, info->ring_pages, NULL);
161 up_write(&ctx->mm->mmap_sem);
163 if (unlikely(info->nr_pages != nr_pages)) {
168 ctx->user_id = info->mmap_base;
170 info->nr = nr_events; /* trusted copy */
172 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
173 ring->nr = nr_events; /* user copy */
174 ring->id = ctx->user_id;
175 ring->head = ring->tail = 0;
176 ring->magic = AIO_RING_MAGIC;
177 ring->compat_features = AIO_RING_COMPAT_FEATURES;
178 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
179 ring->header_length = sizeof(struct aio_ring);
180 kunmap_atomic(ring, KM_USER0);
186 /* aio_ring_event: returns a pointer to the event at the given index from
187 * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
189 #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
190 #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
191 #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
193 #define aio_ring_event(info, nr, km) ({ \
194 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
195 struct io_event *__event; \
196 __event = kmap_atomic( \
197 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
198 __event += pos % AIO_EVENTS_PER_PAGE; \
202 #define put_aio_ring_event(event, km) do { \
203 struct io_event *__event = (event); \
205 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
208 static void ctx_rcu_free(struct rcu_head *head)
210 struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
211 unsigned nr_events = ctx->max_reqs;
213 kmem_cache_free(kioctx_cachep, ctx);
216 spin_lock(&aio_nr_lock);
217 BUG_ON(aio_nr - nr_events > aio_nr);
219 spin_unlock(&aio_nr_lock);
224 * Called when the last user of an aio context has gone away,
225 * and the struct needs to be freed.
227 static void __put_ioctx(struct kioctx *ctx)
229 BUG_ON(ctx->reqs_active);
231 cancel_delayed_work(&ctx->wq);
232 cancel_work_sync(&ctx->wq.work);
236 pr_debug("__put_ioctx: freeing %p\n", ctx);
237 call_rcu(&ctx->rcu_head, ctx_rcu_free);
240 #define get_ioctx(kioctx) do { \
241 BUG_ON(atomic_read(&(kioctx)->users) <= 0); \
242 atomic_inc(&(kioctx)->users); \
244 #define put_ioctx(kioctx) do { \
245 BUG_ON(atomic_read(&(kioctx)->users) <= 0); \
246 if (unlikely(atomic_dec_and_test(&(kioctx)->users))) \
247 __put_ioctx(kioctx); \
251 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
253 static struct kioctx *ioctx_alloc(unsigned nr_events)
255 struct mm_struct *mm;
259 /* Prevent overflows */
260 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
261 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
262 pr_debug("ENOMEM: nr_events too high\n");
263 return ERR_PTR(-EINVAL);
266 if ((unsigned long)nr_events > aio_max_nr)
267 return ERR_PTR(-EAGAIN);
269 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
271 return ERR_PTR(-ENOMEM);
273 ctx->max_reqs = nr_events;
274 mm = ctx->mm = current->mm;
275 atomic_inc(&mm->mm_count);
277 atomic_set(&ctx->users, 1);
278 spin_lock_init(&ctx->ctx_lock);
279 spin_lock_init(&ctx->ring_info.ring_lock);
280 init_waitqueue_head(&ctx->wait);
282 INIT_LIST_HEAD(&ctx->active_reqs);
283 INIT_LIST_HEAD(&ctx->run_list);
284 INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
286 if (aio_setup_ring(ctx) < 0)
289 /* limit the number of system wide aios */
291 spin_lock_bh(&aio_nr_lock);
292 if (aio_nr + nr_events > aio_max_nr ||
293 aio_nr + nr_events < aio_nr)
296 aio_nr += ctx->max_reqs;
297 spin_unlock_bh(&aio_nr_lock);
298 if (ctx->max_reqs || did_sync)
301 /* wait for rcu callbacks to have completed before giving up */
304 ctx->max_reqs = nr_events;
307 if (ctx->max_reqs == 0)
310 /* now link into global list. */
311 spin_lock(&mm->ioctx_lock);
312 hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
313 spin_unlock(&mm->ioctx_lock);
315 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
316 ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
321 return ERR_PTR(-EAGAIN);
325 kmem_cache_free(kioctx_cachep, ctx);
326 ctx = ERR_PTR(-ENOMEM);
328 dprintk("aio: error allocating ioctx %p\n", ctx);
333 * Cancels all outstanding aio requests on an aio context. Used
334 * when the processes owning a context have all exited to encourage
335 * the rapid destruction of the kioctx.
337 static void aio_cancel_all(struct kioctx *ctx)
339 int (*cancel)(struct kiocb *, struct io_event *);
341 spin_lock_irq(&ctx->ctx_lock);
343 while (!list_empty(&ctx->active_reqs)) {
344 struct list_head *pos = ctx->active_reqs.next;
345 struct kiocb *iocb = list_kiocb(pos);
346 list_del_init(&iocb->ki_list);
347 cancel = iocb->ki_cancel;
348 kiocbSetCancelled(iocb);
351 spin_unlock_irq(&ctx->ctx_lock);
353 spin_lock_irq(&ctx->ctx_lock);
356 spin_unlock_irq(&ctx->ctx_lock);
359 static void wait_for_all_aios(struct kioctx *ctx)
361 struct task_struct *tsk = current;
362 DECLARE_WAITQUEUE(wait, tsk);
364 spin_lock_irq(&ctx->ctx_lock);
365 if (!ctx->reqs_active)
368 add_wait_queue(&ctx->wait, &wait);
369 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
370 while (ctx->reqs_active) {
371 spin_unlock_irq(&ctx->ctx_lock);
373 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
374 spin_lock_irq(&ctx->ctx_lock);
376 __set_task_state(tsk, TASK_RUNNING);
377 remove_wait_queue(&ctx->wait, &wait);
380 spin_unlock_irq(&ctx->ctx_lock);
383 /* wait_on_sync_kiocb:
384 * Waits on the given sync kiocb to complete.
386 ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
388 while (iocb->ki_users) {
389 set_current_state(TASK_UNINTERRUPTIBLE);
394 __set_current_state(TASK_RUNNING);
395 return iocb->ki_user_data;
397 EXPORT_SYMBOL(wait_on_sync_kiocb);
399 /* exit_aio: called when the last user of mm goes away. At this point,
400 * there is no way for any new requests to be submited or any of the
401 * io_* syscalls to be called on the context. However, there may be
402 * outstanding requests which hold references to the context; as they
403 * go away, they will call put_ioctx and release any pinned memory
404 * associated with the request (held via struct page * references).
406 void exit_aio(struct mm_struct *mm)
410 while (!hlist_empty(&mm->ioctx_list)) {
411 ctx = hlist_entry(mm->ioctx_list.first, struct kioctx, list);
412 hlist_del_rcu(&ctx->list);
416 wait_for_all_aios(ctx);
418 * Ensure we don't leave the ctx on the aio_wq
420 cancel_work_sync(&ctx->wq.work);
422 if (1 != atomic_read(&ctx->users))
424 "exit_aio:ioctx still alive: %d %d %d\n",
425 atomic_read(&ctx->users), ctx->dead,
432 * Allocate a slot for an aio request. Increments the users count
433 * of the kioctx so that the kioctx stays around until all requests are
434 * complete. Returns NULL if no requests are free.
436 * Returns with kiocb->users set to 2. The io submit code path holds
437 * an extra reference while submitting the i/o.
438 * This prevents races between the aio code path referencing the
439 * req (after submitting it) and aio_complete() freeing the req.
441 static struct kiocb *__aio_get_req(struct kioctx *ctx)
443 struct kiocb *req = NULL;
444 struct aio_ring *ring;
447 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
455 req->ki_cancel = NULL;
456 req->ki_retry = NULL;
459 req->ki_iovec = NULL;
460 INIT_LIST_HEAD(&req->ki_run_list);
461 req->ki_eventfd = NULL;
463 /* Check if the completion queue has enough free space to
464 * accept an event from this io.
466 spin_lock_irq(&ctx->ctx_lock);
467 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
468 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
469 list_add(&req->ki_list, &ctx->active_reqs);
473 kunmap_atomic(ring, KM_USER0);
474 spin_unlock_irq(&ctx->ctx_lock);
477 kmem_cache_free(kiocb_cachep, req);
484 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
487 /* Handle a potential starvation case -- should be exceedingly rare as
488 * requests will be stuck on fput_head only if the aio_fput_routine is
489 * delayed and the requests were the last user of the struct file.
491 req = __aio_get_req(ctx);
492 if (unlikely(NULL == req)) {
493 aio_fput_routine(NULL);
494 req = __aio_get_req(ctx);
499 static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
501 assert_spin_locked(&ctx->ctx_lock);
503 if (req->ki_eventfd != NULL)
504 eventfd_ctx_put(req->ki_eventfd);
507 if (req->ki_iovec != &req->ki_inline_vec)
508 kfree(req->ki_iovec);
509 kmem_cache_free(kiocb_cachep, req);
512 if (unlikely(!ctx->reqs_active && ctx->dead))
516 static void aio_fput_routine(struct work_struct *data)
518 spin_lock_irq(&fput_lock);
519 while (likely(!list_empty(&fput_head))) {
520 struct kiocb *req = list_kiocb(fput_head.next);
521 struct kioctx *ctx = req->ki_ctx;
523 list_del(&req->ki_list);
524 spin_unlock_irq(&fput_lock);
526 /* Complete the fput(s) */
527 if (req->ki_filp != NULL)
528 __fput(req->ki_filp);
530 /* Link the iocb into the context's free list */
531 spin_lock_irq(&ctx->ctx_lock);
532 really_put_req(ctx, req);
533 spin_unlock_irq(&ctx->ctx_lock);
536 spin_lock_irq(&fput_lock);
538 spin_unlock_irq(&fput_lock);
542 * Returns true if this put was the last user of the request.
544 static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
546 dprintk(KERN_DEBUG "aio_put(%p): f_count=%ld\n",
547 req, atomic_long_read(&req->ki_filp->f_count));
549 assert_spin_locked(&ctx->ctx_lock);
552 BUG_ON(req->ki_users < 0);
553 if (likely(req->ki_users))
555 list_del(&req->ki_list); /* remove from active_reqs */
556 req->ki_cancel = NULL;
557 req->ki_retry = NULL;
560 * Try to optimize the aio and eventfd file* puts, by avoiding to
561 * schedule work in case it is not __fput() time. In normal cases,
562 * we would not be holding the last reference to the file*, so
563 * this function will be executed w/out any aio kthread wakeup.
565 if (unlikely(atomic_long_dec_and_test(&req->ki_filp->f_count))) {
567 spin_lock(&fput_lock);
568 list_add(&req->ki_list, &fput_head);
569 spin_unlock(&fput_lock);
570 queue_work(aio_wq, &fput_work);
573 really_put_req(ctx, req);
579 * Returns true if this put was the last user of the kiocb,
580 * false if the request is still in use.
582 int aio_put_req(struct kiocb *req)
584 struct kioctx *ctx = req->ki_ctx;
586 spin_lock_irq(&ctx->ctx_lock);
587 ret = __aio_put_req(ctx, req);
588 spin_unlock_irq(&ctx->ctx_lock);
591 EXPORT_SYMBOL(aio_put_req);
593 static struct kioctx *lookup_ioctx(unsigned long ctx_id)
595 struct mm_struct *mm = current->mm;
596 struct kioctx *ctx, *ret = NULL;
597 struct hlist_node *n;
601 hlist_for_each_entry_rcu(ctx, n, &mm->ioctx_list, list) {
602 if (ctx->user_id == ctx_id && !ctx->dead) {
614 * Queue up a kiocb to be retried. Assumes that the kiocb
615 * has already been marked as kicked, and places it on
616 * the retry run list for the corresponding ioctx, if it
617 * isn't already queued. Returns 1 if it actually queued
618 * the kiocb (to tell the caller to activate the work
619 * queue to process it), or 0, if it found that it was
622 static inline int __queue_kicked_iocb(struct kiocb *iocb)
624 struct kioctx *ctx = iocb->ki_ctx;
626 assert_spin_locked(&ctx->ctx_lock);
628 if (list_empty(&iocb->ki_run_list)) {
629 list_add_tail(&iocb->ki_run_list,
637 * This is the core aio execution routine. It is
638 * invoked both for initial i/o submission and
639 * subsequent retries via the aio_kick_handler.
640 * Expects to be invoked with iocb->ki_ctx->lock
641 * already held. The lock is released and reacquired
642 * as needed during processing.
644 * Calls the iocb retry method (already setup for the
645 * iocb on initial submission) for operation specific
646 * handling, but takes care of most of common retry
647 * execution details for a given iocb. The retry method
648 * needs to be non-blocking as far as possible, to avoid
649 * holding up other iocbs waiting to be serviced by the
650 * retry kernel thread.
652 * The trickier parts in this code have to do with
653 * ensuring that only one retry instance is in progress
654 * for a given iocb at any time. Providing that guarantee
655 * simplifies the coding of individual aio operations as
656 * it avoids various potential races.
658 static ssize_t aio_run_iocb(struct kiocb *iocb)
660 struct kioctx *ctx = iocb->ki_ctx;
661 ssize_t (*retry)(struct kiocb *);
664 if (!(retry = iocb->ki_retry)) {
665 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
670 * We don't want the next retry iteration for this
671 * operation to start until this one has returned and
672 * updated the iocb state. However, wait_queue functions
673 * can trigger a kick_iocb from interrupt context in the
674 * meantime, indicating that data is available for the next
675 * iteration. We want to remember that and enable the
676 * next retry iteration _after_ we are through with
679 * So, in order to be able to register a "kick", but
680 * prevent it from being queued now, we clear the kick
681 * flag, but make the kick code *think* that the iocb is
682 * still on the run list until we are actually done.
683 * When we are done with this iteration, we check if
684 * the iocb was kicked in the meantime and if so, queue
688 kiocbClearKicked(iocb);
691 * This is so that aio_complete knows it doesn't need to
692 * pull the iocb off the run list (We can't just call
693 * INIT_LIST_HEAD because we don't want a kick_iocb to
694 * queue this on the run list yet)
696 iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
697 spin_unlock_irq(&ctx->ctx_lock);
699 /* Quit retrying if the i/o has been cancelled */
700 if (kiocbIsCancelled(iocb)) {
702 aio_complete(iocb, ret, 0);
703 /* must not access the iocb after this */
708 * Now we are all set to call the retry method in async
713 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
714 BUG_ON(!list_empty(&iocb->ki_wait.task_list));
715 aio_complete(iocb, ret, 0);
718 spin_lock_irq(&ctx->ctx_lock);
720 if (-EIOCBRETRY == ret) {
722 * OK, now that we are done with this iteration
723 * and know that there is more left to go,
724 * this is where we let go so that a subsequent
725 * "kick" can start the next iteration
728 /* will make __queue_kicked_iocb succeed from here on */
729 INIT_LIST_HEAD(&iocb->ki_run_list);
730 /* we must queue the next iteration ourselves, if it
731 * has already been kicked */
732 if (kiocbIsKicked(iocb)) {
733 __queue_kicked_iocb(iocb);
736 * __queue_kicked_iocb will always return 1 here, because
737 * iocb->ki_run_list is empty at this point so it should
738 * be safe to unconditionally queue the context into the
749 * Process all pending retries queued on the ioctx
751 * Assumes it is operating within the aio issuer's mm
754 static int __aio_run_iocbs(struct kioctx *ctx)
757 struct list_head run_list;
759 assert_spin_locked(&ctx->ctx_lock);
761 list_replace_init(&ctx->run_list, &run_list);
762 while (!list_empty(&run_list)) {
763 iocb = list_entry(run_list.next, struct kiocb,
765 list_del(&iocb->ki_run_list);
767 * Hold an extra reference while retrying i/o.
769 iocb->ki_users++; /* grab extra reference */
771 __aio_put_req(ctx, iocb);
773 if (!list_empty(&ctx->run_list))
778 static void aio_queue_work(struct kioctx * ctx)
780 unsigned long timeout;
782 * if someone is waiting, get the work started right
783 * away, otherwise, use a longer delay
786 if (waitqueue_active(&ctx->wait))
790 queue_delayed_work(aio_wq, &ctx->wq, timeout);
796 * Process all pending retries queued on the ioctx
798 * Assumes it is operating within the aio issuer's mm
801 static inline void aio_run_iocbs(struct kioctx *ctx)
805 spin_lock_irq(&ctx->ctx_lock);
807 requeue = __aio_run_iocbs(ctx);
808 spin_unlock_irq(&ctx->ctx_lock);
814 * just like aio_run_iocbs, but keeps running them until
815 * the list stays empty
817 static inline void aio_run_all_iocbs(struct kioctx *ctx)
819 spin_lock_irq(&ctx->ctx_lock);
820 while (__aio_run_iocbs(ctx))
822 spin_unlock_irq(&ctx->ctx_lock);
827 * Work queue handler triggered to process pending
828 * retries on an ioctx. Takes on the aio issuer's
829 * mm context before running the iocbs, so that
830 * copy_xxx_user operates on the issuer's address
832 * Run on aiod's context.
834 static void aio_kick_handler(struct work_struct *work)
836 struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
837 mm_segment_t oldfs = get_fs();
838 struct mm_struct *mm;
843 spin_lock_irq(&ctx->ctx_lock);
844 requeue =__aio_run_iocbs(ctx);
846 spin_unlock_irq(&ctx->ctx_lock);
850 * we're in a worker thread already, don't use queue_delayed_work,
853 queue_delayed_work(aio_wq, &ctx->wq, 0);
858 * Called by kick_iocb to queue the kiocb for retry
859 * and if required activate the aio work queue to process
862 static void try_queue_kicked_iocb(struct kiocb *iocb)
864 struct kioctx *ctx = iocb->ki_ctx;
868 /* We're supposed to be the only path putting the iocb back on the run
869 * list. If we find that the iocb is *back* on a wait queue already
870 * than retry has happened before we could queue the iocb. This also
871 * means that the retry could have completed and freed our iocb, no
873 BUG_ON((!list_empty(&iocb->ki_wait.task_list)));
875 spin_lock_irqsave(&ctx->ctx_lock, flags);
876 /* set this inside the lock so that we can't race with aio_run_iocb()
877 * testing it and putting the iocb on the run list under the lock */
878 if (!kiocbTryKick(iocb))
879 run = __queue_kicked_iocb(iocb);
880 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
887 * Called typically from a wait queue callback context
888 * (aio_wake_function) to trigger a retry of the iocb.
889 * The retry is usually executed by aio workqueue
890 * threads (See aio_kick_handler).
892 void kick_iocb(struct kiocb *iocb)
894 /* sync iocbs are easy: they can only ever be executing from a
896 if (is_sync_kiocb(iocb)) {
897 kiocbSetKicked(iocb);
898 wake_up_process(iocb->ki_obj.tsk);
902 try_queue_kicked_iocb(iocb);
904 EXPORT_SYMBOL(kick_iocb);
907 * Called when the io request on the given iocb is complete.
908 * Returns true if this is the last user of the request. The
909 * only other user of the request can be the cancellation code.
911 int aio_complete(struct kiocb *iocb, long res, long res2)
913 struct kioctx *ctx = iocb->ki_ctx;
914 struct aio_ring_info *info;
915 struct aio_ring *ring;
916 struct io_event *event;
922 * Special case handling for sync iocbs:
923 * - events go directly into the iocb for fast handling
924 * - the sync task with the iocb in its stack holds the single iocb
925 * ref, no other paths have a way to get another ref
926 * - the sync task helpfully left a reference to itself in the iocb
928 if (is_sync_kiocb(iocb)) {
929 BUG_ON(iocb->ki_users != 1);
930 iocb->ki_user_data = res;
932 wake_up_process(iocb->ki_obj.tsk);
936 info = &ctx->ring_info;
938 /* add a completion event to the ring buffer.
939 * must be done holding ctx->ctx_lock to prevent
940 * other code from messing with the tail
941 * pointer since we might be called from irq
944 spin_lock_irqsave(&ctx->ctx_lock, flags);
946 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
947 list_del_init(&iocb->ki_run_list);
950 * cancelled requests don't get events, userland was given one
951 * when the event got cancelled.
953 if (kiocbIsCancelled(iocb))
956 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
959 event = aio_ring_event(info, tail, KM_IRQ0);
960 if (++tail >= info->nr)
963 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
964 event->data = iocb->ki_user_data;
968 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
969 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
972 /* after flagging the request as done, we
973 * must never even look at it again
975 smp_wmb(); /* make event visible before updating tail */
980 put_aio_ring_event(event, KM_IRQ0);
981 kunmap_atomic(ring, KM_IRQ1);
983 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
986 * Check if the user asked us to deliver the result through an
987 * eventfd. The eventfd_signal() function is safe to be called
990 if (iocb->ki_eventfd != NULL)
991 eventfd_signal(iocb->ki_eventfd, 1);
994 /* everything turned out well, dispose of the aiocb. */
995 ret = __aio_put_req(ctx, iocb);
998 * We have to order our ring_info tail store above and test
999 * of the wait list below outside the wait lock. This is
1000 * like in wake_up_bit() where clearing a bit has to be
1001 * ordered with the unlocked test.
1005 if (waitqueue_active(&ctx->wait))
1006 wake_up(&ctx->wait);
1008 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
1011 EXPORT_SYMBOL(aio_complete);
1014 * Pull an event off of the ioctx's event ring. Returns the number of
1015 * events fetched (0 or 1 ;-)
1016 * FIXME: make this use cmpxchg.
1017 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1019 static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1021 struct aio_ring_info *info = &ioctx->ring_info;
1022 struct aio_ring *ring;
1026 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
1027 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1028 (unsigned long)ring->head, (unsigned long)ring->tail,
1029 (unsigned long)ring->nr);
1031 if (ring->head == ring->tail)
1034 spin_lock(&info->ring_lock);
1036 head = ring->head % info->nr;
1037 if (head != ring->tail) {
1038 struct io_event *evp = aio_ring_event(info, head, KM_USER1);
1040 head = (head + 1) % info->nr;
1041 smp_mb(); /* finish reading the event before updatng the head */
1044 put_aio_ring_event(evp, KM_USER1);
1046 spin_unlock(&info->ring_lock);
1049 kunmap_atomic(ring, KM_USER0);
1050 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
1051 (unsigned long)ring->head, (unsigned long)ring->tail);
1055 struct aio_timeout {
1056 struct timer_list timer;
1058 struct task_struct *p;
1061 static void timeout_func(unsigned long data)
1063 struct aio_timeout *to = (struct aio_timeout *)data;
1066 wake_up_process(to->p);
1069 static inline void init_timeout(struct aio_timeout *to)
1071 setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
1076 static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1077 const struct timespec *ts)
1079 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
1080 if (time_after(to->timer.expires, jiffies))
1081 add_timer(&to->timer);
1086 static inline void clear_timeout(struct aio_timeout *to)
1088 del_singleshot_timer_sync(&to->timer);
1091 static int read_events(struct kioctx *ctx,
1092 long min_nr, long nr,
1093 struct io_event __user *event,
1094 struct timespec __user *timeout)
1096 long start_jiffies = jiffies;
1097 struct task_struct *tsk = current;
1098 DECLARE_WAITQUEUE(wait, tsk);
1101 struct io_event ent;
1102 struct aio_timeout to;
1105 /* needed to zero any padding within an entry (there shouldn't be
1106 * any, but C is fun!
1108 memset(&ent, 0, sizeof(ent));
1111 while (likely(i < nr)) {
1112 ret = aio_read_evt(ctx, &ent);
1113 if (unlikely(ret <= 0))
1116 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1117 ent.data, ent.obj, ent.res, ent.res2);
1119 /* Could we split the check in two? */
1121 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1122 dprintk("aio: lost an event due to EFAULT.\n");
1127 /* Good, event copied to userland, update counts. */
1139 /* racey check, but it gets redone */
1140 if (!retry && unlikely(!list_empty(&ctx->run_list))) {
1142 aio_run_all_iocbs(ctx);
1150 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
1153 set_timeout(start_jiffies, &to, &ts);
1156 while (likely(i < nr)) {
1157 add_wait_queue_exclusive(&ctx->wait, &wait);
1159 set_task_state(tsk, TASK_INTERRUPTIBLE);
1160 ret = aio_read_evt(ctx, &ent);
1165 if (unlikely(ctx->dead)) {
1169 if (to.timed_out) /* Only check after read evt */
1171 /* Try to only show up in io wait if there are ops
1173 if (ctx->reqs_active)
1177 if (signal_pending(tsk)) {
1181 /*ret = aio_read_evt(ctx, &ent);*/
1184 set_task_state(tsk, TASK_RUNNING);
1185 remove_wait_queue(&ctx->wait, &wait);
1187 if (unlikely(ret <= 0))
1191 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1192 dprintk("aio: lost an event due to EFAULT.\n");
1196 /* Good, event copied to userland, update counts. */
1204 destroy_timer_on_stack(&to.timer);
1208 /* Take an ioctx and remove it from the list of ioctx's. Protects
1209 * against races with itself via ->dead.
1211 static void io_destroy(struct kioctx *ioctx)
1213 struct mm_struct *mm = current->mm;
1216 /* delete the entry from the list is someone else hasn't already */
1217 spin_lock(&mm->ioctx_lock);
1218 was_dead = ioctx->dead;
1220 hlist_del_rcu(&ioctx->list);
1221 spin_unlock(&mm->ioctx_lock);
1223 dprintk("aio_release(%p)\n", ioctx);
1224 if (likely(!was_dead))
1225 put_ioctx(ioctx); /* twice for the list */
1227 aio_cancel_all(ioctx);
1228 wait_for_all_aios(ioctx);
1231 * Wake up any waiters. The setting of ctx->dead must be seen
1232 * by other CPUs at this point. Right now, we rely on the
1233 * locking done by the above calls to ensure this consistency.
1235 wake_up(&ioctx->wait);
1236 put_ioctx(ioctx); /* once for the lookup */
1240 * Create an aio_context capable of receiving at least nr_events.
1241 * ctxp must not point to an aio_context that already exists, and
1242 * must be initialized to 0 prior to the call. On successful
1243 * creation of the aio_context, *ctxp is filled in with the resulting
1244 * handle. May fail with -EINVAL if *ctxp is not initialized,
1245 * if the specified nr_events exceeds internal limits. May fail
1246 * with -EAGAIN if the specified nr_events exceeds the user's limit
1247 * of available events. May fail with -ENOMEM if insufficient kernel
1248 * resources are available. May fail with -EFAULT if an invalid
1249 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1252 SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
1254 struct kioctx *ioctx = NULL;
1258 ret = get_user(ctx, ctxp);
1263 if (unlikely(ctx || nr_events == 0)) {
1264 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1269 ioctx = ioctx_alloc(nr_events);
1270 ret = PTR_ERR(ioctx);
1271 if (!IS_ERR(ioctx)) {
1272 ret = put_user(ioctx->user_id, ctxp);
1276 get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
1285 * Destroy the aio_context specified. May cancel any outstanding
1286 * AIOs and block on completion. Will fail with -ENOSYS if not
1287 * implemented. May fail with -EFAULT if the context pointed to
1290 SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
1292 struct kioctx *ioctx = lookup_ioctx(ctx);
1293 if (likely(NULL != ioctx)) {
1297 pr_debug("EINVAL: io_destroy: invalid context id\n");
1301 static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
1303 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
1307 while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
1308 ssize_t this = min((ssize_t)iov->iov_len, ret);
1309 iov->iov_base += this;
1310 iov->iov_len -= this;
1311 iocb->ki_left -= this;
1313 if (iov->iov_len == 0) {
1319 /* the caller should not have done more io than what fit in
1320 * the remaining iovecs */
1321 BUG_ON(ret > 0 && iocb->ki_left == 0);
1324 static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
1326 struct file *file = iocb->ki_filp;
1327 struct address_space *mapping = file->f_mapping;
1328 struct inode *inode = mapping->host;
1329 ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
1330 unsigned long, loff_t);
1332 unsigned short opcode;
1334 if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
1335 (iocb->ki_opcode == IOCB_CMD_PREAD)) {
1336 rw_op = file->f_op->aio_read;
1337 opcode = IOCB_CMD_PREADV;
1339 rw_op = file->f_op->aio_write;
1340 opcode = IOCB_CMD_PWRITEV;
1343 /* This matches the pread()/pwrite() logic */
1344 if (iocb->ki_pos < 0)
1348 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
1349 iocb->ki_nr_segs - iocb->ki_cur_seg,
1352 aio_advance_iovec(iocb, ret);
1354 /* retry all partial writes. retry partial reads as long as its a
1356 } while (ret > 0 && iocb->ki_left > 0 &&
1357 (opcode == IOCB_CMD_PWRITEV ||
1358 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
1360 /* This means we must have transferred all that we could */
1361 /* No need to retry anymore */
1362 if ((ret == 0) || (iocb->ki_left == 0))
1363 ret = iocb->ki_nbytes - iocb->ki_left;
1365 /* If we managed to write some out we return that, rather than
1366 * the eventual error. */
1367 if (opcode == IOCB_CMD_PWRITEV
1368 && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
1369 && iocb->ki_nbytes - iocb->ki_left)
1370 ret = iocb->ki_nbytes - iocb->ki_left;
1375 static ssize_t aio_fdsync(struct kiocb *iocb)
1377 struct file *file = iocb->ki_filp;
1378 ssize_t ret = -EINVAL;
1380 if (file->f_op->aio_fsync)
1381 ret = file->f_op->aio_fsync(iocb, 1);
1385 static ssize_t aio_fsync(struct kiocb *iocb)
1387 struct file *file = iocb->ki_filp;
1388 ssize_t ret = -EINVAL;
1390 if (file->f_op->aio_fsync)
1391 ret = file->f_op->aio_fsync(iocb, 0);
1395 static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
1399 ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
1400 kiocb->ki_nbytes, 1,
1401 &kiocb->ki_inline_vec, &kiocb->ki_iovec);
1405 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1406 kiocb->ki_cur_seg = 0;
1407 /* ki_nbytes/left now reflect bytes instead of segs */
1408 kiocb->ki_nbytes = ret;
1409 kiocb->ki_left = ret;
1416 static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
1418 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1419 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
1420 kiocb->ki_iovec->iov_len = kiocb->ki_left;
1421 kiocb->ki_nr_segs = 1;
1422 kiocb->ki_cur_seg = 0;
1428 * Performs the initial checks and aio retry method
1429 * setup for the kiocb at the time of io submission.
1431 static ssize_t aio_setup_iocb(struct kiocb *kiocb)
1433 struct file *file = kiocb->ki_filp;
1436 switch (kiocb->ki_opcode) {
1437 case IOCB_CMD_PREAD:
1439 if (unlikely(!(file->f_mode & FMODE_READ)))
1442 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1445 ret = security_file_permission(file, MAY_READ);
1448 ret = aio_setup_single_vector(kiocb);
1452 if (file->f_op->aio_read)
1453 kiocb->ki_retry = aio_rw_vect_retry;
1455 case IOCB_CMD_PWRITE:
1457 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1460 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1463 ret = security_file_permission(file, MAY_WRITE);
1466 ret = aio_setup_single_vector(kiocb);
1470 if (file->f_op->aio_write)
1471 kiocb->ki_retry = aio_rw_vect_retry;
1473 case IOCB_CMD_PREADV:
1475 if (unlikely(!(file->f_mode & FMODE_READ)))
1477 ret = security_file_permission(file, MAY_READ);
1480 ret = aio_setup_vectored_rw(READ, kiocb);
1484 if (file->f_op->aio_read)
1485 kiocb->ki_retry = aio_rw_vect_retry;
1487 case IOCB_CMD_PWRITEV:
1489 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1491 ret = security_file_permission(file, MAY_WRITE);
1494 ret = aio_setup_vectored_rw(WRITE, kiocb);
1498 if (file->f_op->aio_write)
1499 kiocb->ki_retry = aio_rw_vect_retry;
1501 case IOCB_CMD_FDSYNC:
1503 if (file->f_op->aio_fsync)
1504 kiocb->ki_retry = aio_fdsync;
1506 case IOCB_CMD_FSYNC:
1508 if (file->f_op->aio_fsync)
1509 kiocb->ki_retry = aio_fsync;
1512 dprintk("EINVAL: io_submit: no operation provided\n");
1516 if (!kiocb->ki_retry)
1523 * aio_wake_function:
1524 * wait queue callback function for aio notification,
1525 * Simply triggers a retry of the operation via kick_iocb.
1527 * This callback is specified in the wait queue entry in
1531 * This routine is executed with the wait queue lock held.
1532 * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
1533 * the ioctx lock inside the wait queue lock. This is safe
1534 * because this callback isn't used for wait queues which
1535 * are nested inside ioctx lock (i.e. ctx->wait)
1537 static int aio_wake_function(wait_queue_t *wait, unsigned mode,
1538 int sync, void *key)
1540 struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
1542 list_del_init(&wait->task_list);
1547 static void aio_batch_add(struct address_space *mapping,
1548 struct hlist_head *batch_hash)
1550 struct aio_batch_entry *abe;
1551 struct hlist_node *pos;
1554 bucket = hash_ptr(mapping, AIO_BATCH_HASH_BITS);
1555 hlist_for_each_entry(abe, pos, &batch_hash[bucket], list) {
1556 if (abe->mapping == mapping)
1560 abe = mempool_alloc(abe_pool, GFP_KERNEL);
1561 BUG_ON(!igrab(mapping->host));
1562 abe->mapping = mapping;
1563 hlist_add_head(&abe->list, &batch_hash[bucket]);
1567 static void aio_batch_free(struct hlist_head *batch_hash)
1569 struct aio_batch_entry *abe;
1570 struct hlist_node *pos, *n;
1573 for (i = 0; i < AIO_BATCH_HASH_SIZE; i++) {
1574 hlist_for_each_entry_safe(abe, pos, n, &batch_hash[i], list) {
1575 blk_run_address_space(abe->mapping);
1576 iput(abe->mapping->host);
1577 hlist_del(&abe->list);
1578 mempool_free(abe, abe_pool);
1583 static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1584 struct iocb *iocb, struct hlist_head *batch_hash)
1590 /* enforce forwards compatibility on users */
1591 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
1592 pr_debug("EINVAL: io_submit: reserve field set\n");
1596 /* prevent overflows */
1598 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1599 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1600 ((ssize_t)iocb->aio_nbytes < 0)
1602 pr_debug("EINVAL: io_submit: overflow check\n");
1606 file = fget(iocb->aio_fildes);
1607 if (unlikely(!file))
1610 req = aio_get_req(ctx); /* returns with 2 references to req */
1611 if (unlikely(!req)) {
1615 req->ki_filp = file;
1616 if (iocb->aio_flags & IOCB_FLAG_RESFD) {
1618 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1619 * instance of the file* now. The file descriptor must be
1620 * an eventfd() fd, and will be signaled for each completed
1621 * event using the eventfd_signal() function.
1623 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
1624 if (IS_ERR(req->ki_eventfd)) {
1625 ret = PTR_ERR(req->ki_eventfd);
1626 req->ki_eventfd = NULL;
1631 ret = put_user(req->ki_key, &user_iocb->aio_key);
1632 if (unlikely(ret)) {
1633 dprintk("EFAULT: aio_key\n");
1637 req->ki_obj.user = user_iocb;
1638 req->ki_user_data = iocb->aio_data;
1639 req->ki_pos = iocb->aio_offset;
1641 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1642 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1643 req->ki_opcode = iocb->aio_lio_opcode;
1644 init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
1645 INIT_LIST_HEAD(&req->ki_wait.task_list);
1647 ret = aio_setup_iocb(req);
1652 spin_lock_irq(&ctx->ctx_lock);
1654 if (!list_empty(&ctx->run_list)) {
1655 /* drain the run list */
1656 while (__aio_run_iocbs(ctx))
1659 spin_unlock_irq(&ctx->ctx_lock);
1660 if (req->ki_opcode == IOCB_CMD_PREAD ||
1661 req->ki_opcode == IOCB_CMD_PREADV ||
1662 req->ki_opcode == IOCB_CMD_PWRITE ||
1663 req->ki_opcode == IOCB_CMD_PWRITEV)
1664 aio_batch_add(file->f_mapping, batch_hash);
1666 aio_put_req(req); /* drop extra ref to req */
1670 aio_put_req(req); /* drop extra ref to req */
1671 aio_put_req(req); /* drop i/o ref to req */
1676 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1677 * the number of iocbs queued. May return -EINVAL if the aio_context
1678 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1679 * *iocbpp[0] is not properly initialized, if the operation specified
1680 * is invalid for the file descriptor in the iocb. May fail with
1681 * -EFAULT if any of the data structures point to invalid data. May
1682 * fail with -EBADF if the file descriptor specified in the first
1683 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1684 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1685 * fail with -ENOSYS if not implemented.
1687 SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1688 struct iocb __user * __user *, iocbpp)
1693 struct hlist_head batch_hash[AIO_BATCH_HASH_SIZE] = { { 0, }, };
1695 if (unlikely(nr < 0))
1698 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1701 ctx = lookup_ioctx(ctx_id);
1702 if (unlikely(!ctx)) {
1703 pr_debug("EINVAL: io_submit: invalid context id\n");
1708 * AKPM: should this return a partial result if some of the IOs were
1709 * successfully submitted?
1711 for (i=0; i<nr; i++) {
1712 struct iocb __user *user_iocb;
1715 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1720 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1725 ret = io_submit_one(ctx, user_iocb, &tmp, batch_hash);
1729 aio_batch_free(batch_hash);
1736 * Finds a given iocb for cancellation.
1738 static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1741 struct list_head *pos;
1743 assert_spin_locked(&ctx->ctx_lock);
1745 /* TODO: use a hash or array, this sucks. */
1746 list_for_each(pos, &ctx->active_reqs) {
1747 struct kiocb *kiocb = list_kiocb(pos);
1748 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1755 * Attempts to cancel an iocb previously passed to io_submit. If
1756 * the operation is successfully cancelled, the resulting event is
1757 * copied into the memory pointed to by result without being placed
1758 * into the completion queue and 0 is returned. May fail with
1759 * -EFAULT if any of the data structures pointed to are invalid.
1760 * May fail with -EINVAL if aio_context specified by ctx_id is
1761 * invalid. May fail with -EAGAIN if the iocb specified was not
1762 * cancelled. Will fail with -ENOSYS if not implemented.
1764 SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1765 struct io_event __user *, result)
1767 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1769 struct kiocb *kiocb;
1773 ret = get_user(key, &iocb->aio_key);
1777 ctx = lookup_ioctx(ctx_id);
1781 spin_lock_irq(&ctx->ctx_lock);
1783 kiocb = lookup_kiocb(ctx, iocb, key);
1784 if (kiocb && kiocb->ki_cancel) {
1785 cancel = kiocb->ki_cancel;
1787 kiocbSetCancelled(kiocb);
1790 spin_unlock_irq(&ctx->ctx_lock);
1792 if (NULL != cancel) {
1793 struct io_event tmp;
1794 pr_debug("calling cancel\n");
1795 memset(&tmp, 0, sizeof(tmp));
1796 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1797 tmp.data = kiocb->ki_user_data;
1798 ret = cancel(kiocb, &tmp);
1800 /* Cancellation succeeded -- copy the result
1801 * into the user's buffer.
1803 if (copy_to_user(result, &tmp, sizeof(tmp)))
1815 * Attempts to read at least min_nr events and up to nr events from
1816 * the completion queue for the aio_context specified by ctx_id. May
1817 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1818 * if nr is out of range, if when is out of range. May fail with
1819 * -EFAULT if any of the memory specified to is invalid. May return
1820 * 0 or < min_nr if no events are available and the timeout specified
1821 * by when has elapsed, where when == NULL specifies an infinite
1822 * timeout. Note that the timeout pointed to by when is relative and
1823 * will be updated if not NULL and the operation blocks. Will fail
1824 * with -ENOSYS if not implemented.
1826 SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
1829 struct io_event __user *, events,
1830 struct timespec __user *, timeout)
1832 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1835 if (likely(ioctx)) {
1836 if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
1837 ret = read_events(ioctx, min_nr, nr, events, timeout);
1841 asmlinkage_protect(5, ret, ctx_id, min_nr, nr, events, timeout);