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>
34 #include <asm/kmap_types.h>
35 #include <asm/uaccess.h>
36 #include <asm/mmu_context.h>
39 #define dprintk printk
41 #define dprintk(x...) do { ; } while (0)
44 /*------ sysctl variables----*/
45 static DEFINE_SPINLOCK(aio_nr_lock
);
46 unsigned long aio_nr
; /* current system wide number of aio requests */
47 unsigned long aio_max_nr
= 0x10000; /* system wide maximum number of aio requests */
48 /*----end sysctl variables---*/
50 static struct kmem_cache
*kiocb_cachep
;
51 static struct kmem_cache
*kioctx_cachep
;
53 static struct workqueue_struct
*aio_wq
;
55 /* Used for rare fput completion. */
56 static void aio_fput_routine(struct work_struct
*);
57 static DECLARE_WORK(fput_work
, aio_fput_routine
);
59 static DEFINE_SPINLOCK(fput_lock
);
60 static LIST_HEAD(fput_head
);
62 static void aio_kick_handler(struct work_struct
*);
63 static void aio_queue_work(struct kioctx
*);
66 * Creates the slab caches used by the aio routines, panic on
67 * failure as this is done early during the boot sequence.
69 static int __init
aio_setup(void)
71 kiocb_cachep
= KMEM_CACHE(kiocb
, SLAB_HWCACHE_ALIGN
|SLAB_PANIC
);
72 kioctx_cachep
= KMEM_CACHE(kioctx
,SLAB_HWCACHE_ALIGN
|SLAB_PANIC
);
74 aio_wq
= create_workqueue("aio");
76 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page
));
81 static void aio_free_ring(struct kioctx
*ctx
)
83 struct aio_ring_info
*info
= &ctx
->ring_info
;
86 for (i
=0; i
<info
->nr_pages
; i
++)
87 put_page(info
->ring_pages
[i
]);
89 if (info
->mmap_size
) {
90 down_write(&ctx
->mm
->mmap_sem
);
91 do_munmap(ctx
->mm
, info
->mmap_base
, info
->mmap_size
);
92 up_write(&ctx
->mm
->mmap_sem
);
95 if (info
->ring_pages
&& info
->ring_pages
!= info
->internal_pages
)
96 kfree(info
->ring_pages
);
97 info
->ring_pages
= NULL
;
101 static int aio_setup_ring(struct kioctx
*ctx
)
103 struct aio_ring
*ring
;
104 struct aio_ring_info
*info
= &ctx
->ring_info
;
105 unsigned nr_events
= ctx
->max_reqs
;
109 /* Compensate for the ring buffer's head/tail overlap entry */
110 nr_events
+= 2; /* 1 is required, 2 for good luck */
112 size
= sizeof(struct aio_ring
);
113 size
+= sizeof(struct io_event
) * nr_events
;
114 nr_pages
= (size
+ PAGE_SIZE
-1) >> PAGE_SHIFT
;
119 nr_events
= (PAGE_SIZE
* nr_pages
- sizeof(struct aio_ring
)) / sizeof(struct io_event
);
122 info
->ring_pages
= info
->internal_pages
;
123 if (nr_pages
> AIO_RING_PAGES
) {
124 info
->ring_pages
= kcalloc(nr_pages
, sizeof(struct page
*), GFP_KERNEL
);
125 if (!info
->ring_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_ANONYMOUS
|MAP_PRIVATE
,
135 if (IS_ERR((void *)info
->mmap_base
)) {
136 up_write(&ctx
->mm
->mmap_sem
);
142 dprintk("mmap address: 0x%08lx\n", info
->mmap_base
);
143 info
->nr_pages
= get_user_pages(current
, ctx
->mm
,
144 info
->mmap_base
, nr_pages
,
145 1, 0, info
->ring_pages
, NULL
);
146 up_write(&ctx
->mm
->mmap_sem
);
148 if (unlikely(info
->nr_pages
!= nr_pages
)) {
153 ctx
->user_id
= info
->mmap_base
;
155 info
->nr
= nr_events
; /* trusted copy */
157 ring
= kmap_atomic(info
->ring_pages
[0], KM_USER0
);
158 ring
->nr
= nr_events
; /* user copy */
159 ring
->id
= ctx
->user_id
;
160 ring
->head
= ring
->tail
= 0;
161 ring
->magic
= AIO_RING_MAGIC
;
162 ring
->compat_features
= AIO_RING_COMPAT_FEATURES
;
163 ring
->incompat_features
= AIO_RING_INCOMPAT_FEATURES
;
164 ring
->header_length
= sizeof(struct aio_ring
);
165 kunmap_atomic(ring
, KM_USER0
);
171 /* aio_ring_event: returns a pointer to the event at the given index from
172 * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
174 #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
175 #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
176 #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
178 #define aio_ring_event(info, nr, km) ({ \
179 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
180 struct io_event *__event; \
181 __event = kmap_atomic( \
182 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
183 __event += pos % AIO_EVENTS_PER_PAGE; \
187 #define put_aio_ring_event(event, km) do { \
188 struct io_event *__event = (event); \
190 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
194 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
196 static struct kioctx
*ioctx_alloc(unsigned nr_events
)
198 struct mm_struct
*mm
;
201 /* Prevent overflows */
202 if ((nr_events
> (0x10000000U
/ sizeof(struct io_event
))) ||
203 (nr_events
> (0x10000000U
/ sizeof(struct kiocb
)))) {
204 pr_debug("ENOMEM: nr_events too high\n");
205 return ERR_PTR(-EINVAL
);
208 if ((unsigned long)nr_events
> aio_max_nr
)
209 return ERR_PTR(-EAGAIN
);
211 ctx
= kmem_cache_zalloc(kioctx_cachep
, GFP_KERNEL
);
213 return ERR_PTR(-ENOMEM
);
215 ctx
->max_reqs
= nr_events
;
216 mm
= ctx
->mm
= current
->mm
;
217 atomic_inc(&mm
->mm_count
);
219 atomic_set(&ctx
->users
, 1);
220 spin_lock_init(&ctx
->ctx_lock
);
221 spin_lock_init(&ctx
->ring_info
.ring_lock
);
222 init_waitqueue_head(&ctx
->wait
);
224 INIT_LIST_HEAD(&ctx
->active_reqs
);
225 INIT_LIST_HEAD(&ctx
->run_list
);
226 INIT_DELAYED_WORK(&ctx
->wq
, aio_kick_handler
);
228 if (aio_setup_ring(ctx
) < 0)
231 /* limit the number of system wide aios */
232 spin_lock(&aio_nr_lock
);
233 if (aio_nr
+ ctx
->max_reqs
> aio_max_nr
||
234 aio_nr
+ ctx
->max_reqs
< aio_nr
)
237 aio_nr
+= ctx
->max_reqs
;
238 spin_unlock(&aio_nr_lock
);
239 if (ctx
->max_reqs
== 0)
242 /* now link into global list. kludge. FIXME */
243 write_lock(&mm
->ioctx_list_lock
);
244 ctx
->next
= mm
->ioctx_list
;
245 mm
->ioctx_list
= ctx
;
246 write_unlock(&mm
->ioctx_list_lock
);
248 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
249 ctx
, ctx
->user_id
, current
->mm
, ctx
->ring_info
.nr
);
254 return ERR_PTR(-EAGAIN
);
258 kmem_cache_free(kioctx_cachep
, ctx
);
259 ctx
= ERR_PTR(-ENOMEM
);
261 dprintk("aio: error allocating ioctx %p\n", ctx
);
266 * Cancels all outstanding aio requests on an aio context. Used
267 * when the processes owning a context have all exited to encourage
268 * the rapid destruction of the kioctx.
270 static void aio_cancel_all(struct kioctx
*ctx
)
272 int (*cancel
)(struct kiocb
*, struct io_event
*);
274 spin_lock_irq(&ctx
->ctx_lock
);
276 while (!list_empty(&ctx
->active_reqs
)) {
277 struct list_head
*pos
= ctx
->active_reqs
.next
;
278 struct kiocb
*iocb
= list_kiocb(pos
);
279 list_del_init(&iocb
->ki_list
);
280 cancel
= iocb
->ki_cancel
;
281 kiocbSetCancelled(iocb
);
284 spin_unlock_irq(&ctx
->ctx_lock
);
286 spin_lock_irq(&ctx
->ctx_lock
);
289 spin_unlock_irq(&ctx
->ctx_lock
);
292 static void wait_for_all_aios(struct kioctx
*ctx
)
294 struct task_struct
*tsk
= current
;
295 DECLARE_WAITQUEUE(wait
, tsk
);
297 spin_lock_irq(&ctx
->ctx_lock
);
298 if (!ctx
->reqs_active
)
301 add_wait_queue(&ctx
->wait
, &wait
);
302 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
303 while (ctx
->reqs_active
) {
304 spin_unlock_irq(&ctx
->ctx_lock
);
306 set_task_state(tsk
, TASK_UNINTERRUPTIBLE
);
307 spin_lock_irq(&ctx
->ctx_lock
);
309 __set_task_state(tsk
, TASK_RUNNING
);
310 remove_wait_queue(&ctx
->wait
, &wait
);
313 spin_unlock_irq(&ctx
->ctx_lock
);
316 /* wait_on_sync_kiocb:
317 * Waits on the given sync kiocb to complete.
319 ssize_t fastcall
wait_on_sync_kiocb(struct kiocb
*iocb
)
321 while (iocb
->ki_users
) {
322 set_current_state(TASK_UNINTERRUPTIBLE
);
327 __set_current_state(TASK_RUNNING
);
328 return iocb
->ki_user_data
;
331 /* exit_aio: called when the last user of mm goes away. At this point,
332 * there is no way for any new requests to be submited or any of the
333 * io_* syscalls to be called on the context. However, there may be
334 * outstanding requests which hold references to the context; as they
335 * go away, they will call put_ioctx and release any pinned memory
336 * associated with the request (held via struct page * references).
338 void fastcall
exit_aio(struct mm_struct
*mm
)
340 struct kioctx
*ctx
= mm
->ioctx_list
;
341 mm
->ioctx_list
= NULL
;
343 struct kioctx
*next
= ctx
->next
;
347 wait_for_all_aios(ctx
);
349 * this is an overkill, but ensures we don't leave
350 * the ctx on the aio_wq
352 flush_workqueue(aio_wq
);
354 if (1 != atomic_read(&ctx
->users
))
356 "exit_aio:ioctx still alive: %d %d %d\n",
357 atomic_read(&ctx
->users
), ctx
->dead
,
365 * Called when the last user of an aio context has gone away,
366 * and the struct needs to be freed.
368 void fastcall
__put_ioctx(struct kioctx
*ctx
)
370 unsigned nr_events
= ctx
->max_reqs
;
372 BUG_ON(ctx
->reqs_active
);
374 cancel_delayed_work(&ctx
->wq
);
375 flush_workqueue(aio_wq
);
379 pr_debug("__put_ioctx: freeing %p\n", ctx
);
380 kmem_cache_free(kioctx_cachep
, ctx
);
383 spin_lock(&aio_nr_lock
);
384 BUG_ON(aio_nr
- nr_events
> aio_nr
);
386 spin_unlock(&aio_nr_lock
);
391 * Allocate a slot for an aio request. Increments the users count
392 * of the kioctx so that the kioctx stays around until all requests are
393 * complete. Returns NULL if no requests are free.
395 * Returns with kiocb->users set to 2. The io submit code path holds
396 * an extra reference while submitting the i/o.
397 * This prevents races between the aio code path referencing the
398 * req (after submitting it) and aio_complete() freeing the req.
400 static struct kiocb
*FASTCALL(__aio_get_req(struct kioctx
*ctx
));
401 static struct kiocb fastcall
*__aio_get_req(struct kioctx
*ctx
)
403 struct kiocb
*req
= NULL
;
404 struct aio_ring
*ring
;
407 req
= kmem_cache_alloc(kiocb_cachep
, GFP_KERNEL
);
415 req
->ki_cancel
= NULL
;
416 req
->ki_retry
= NULL
;
419 req
->ki_iovec
= NULL
;
420 INIT_LIST_HEAD(&req
->ki_run_list
);
422 /* Check if the completion queue has enough free space to
423 * accept an event from this io.
425 spin_lock_irq(&ctx
->ctx_lock
);
426 ring
= kmap_atomic(ctx
->ring_info
.ring_pages
[0], KM_USER0
);
427 if (ctx
->reqs_active
< aio_ring_avail(&ctx
->ring_info
, ring
)) {
428 list_add(&req
->ki_list
, &ctx
->active_reqs
);
432 kunmap_atomic(ring
, KM_USER0
);
433 spin_unlock_irq(&ctx
->ctx_lock
);
436 kmem_cache_free(kiocb_cachep
, req
);
443 static inline struct kiocb
*aio_get_req(struct kioctx
*ctx
)
446 /* Handle a potential starvation case -- should be exceedingly rare as
447 * requests will be stuck on fput_head only if the aio_fput_routine is
448 * delayed and the requests were the last user of the struct file.
450 req
= __aio_get_req(ctx
);
451 if (unlikely(NULL
== req
)) {
452 aio_fput_routine(NULL
);
453 req
= __aio_get_req(ctx
);
458 static inline void really_put_req(struct kioctx
*ctx
, struct kiocb
*req
)
460 assert_spin_locked(&ctx
->ctx_lock
);
464 if (req
->ki_iovec
!= &req
->ki_inline_vec
)
465 kfree(req
->ki_iovec
);
466 kmem_cache_free(kiocb_cachep
, req
);
469 if (unlikely(!ctx
->reqs_active
&& ctx
->dead
))
473 static void aio_fput_routine(struct work_struct
*data
)
475 spin_lock_irq(&fput_lock
);
476 while (likely(!list_empty(&fput_head
))) {
477 struct kiocb
*req
= list_kiocb(fput_head
.next
);
478 struct kioctx
*ctx
= req
->ki_ctx
;
480 list_del(&req
->ki_list
);
481 spin_unlock_irq(&fput_lock
);
483 /* Complete the fput */
484 __fput(req
->ki_filp
);
486 /* Link the iocb into the context's free list */
487 spin_lock_irq(&ctx
->ctx_lock
);
488 really_put_req(ctx
, req
);
489 spin_unlock_irq(&ctx
->ctx_lock
);
492 spin_lock_irq(&fput_lock
);
494 spin_unlock_irq(&fput_lock
);
498 * Returns true if this put was the last user of the request.
500 static int __aio_put_req(struct kioctx
*ctx
, struct kiocb
*req
)
502 dprintk(KERN_DEBUG
"aio_put(%p): f_count=%d\n",
503 req
, atomic_read(&req
->ki_filp
->f_count
));
505 assert_spin_locked(&ctx
->ctx_lock
);
508 BUG_ON(req
->ki_users
< 0);
509 if (likely(req
->ki_users
))
511 list_del(&req
->ki_list
); /* remove from active_reqs */
512 req
->ki_cancel
= NULL
;
513 req
->ki_retry
= NULL
;
515 /* Must be done under the lock to serialise against cancellation.
516 * Call this aio_fput as it duplicates fput via the fput_work.
518 if (unlikely(atomic_dec_and_test(&req
->ki_filp
->f_count
))) {
520 spin_lock(&fput_lock
);
521 list_add(&req
->ki_list
, &fput_head
);
522 spin_unlock(&fput_lock
);
523 queue_work(aio_wq
, &fput_work
);
525 really_put_req(ctx
, req
);
530 * Returns true if this put was the last user of the kiocb,
531 * false if the request is still in use.
533 int fastcall
aio_put_req(struct kiocb
*req
)
535 struct kioctx
*ctx
= req
->ki_ctx
;
537 spin_lock_irq(&ctx
->ctx_lock
);
538 ret
= __aio_put_req(ctx
, req
);
539 spin_unlock_irq(&ctx
->ctx_lock
);
543 /* Lookup an ioctx id. ioctx_list is lockless for reads.
544 * FIXME: this is O(n) and is only suitable for development.
546 struct kioctx
*lookup_ioctx(unsigned long ctx_id
)
548 struct kioctx
*ioctx
;
549 struct mm_struct
*mm
;
552 read_lock(&mm
->ioctx_list_lock
);
553 for (ioctx
= mm
->ioctx_list
; ioctx
; ioctx
= ioctx
->next
)
554 if (likely(ioctx
->user_id
== ctx_id
&& !ioctx
->dead
)) {
558 read_unlock(&mm
->ioctx_list_lock
);
565 * Makes the calling kernel thread take on the specified
567 * Called by the retry thread execute retries within the
568 * iocb issuer's mm context, so that copy_from/to_user
569 * operations work seamlessly for aio.
570 * (Note: this routine is intended to be called only
571 * from a kernel thread context)
573 static void use_mm(struct mm_struct
*mm
)
575 struct mm_struct
*active_mm
;
576 struct task_struct
*tsk
= current
;
579 tsk
->flags
|= PF_BORROWED_MM
;
580 active_mm
= tsk
->active_mm
;
581 atomic_inc(&mm
->mm_count
);
585 * Note that on UML this *requires* PF_BORROWED_MM to be set, otherwise
586 * it won't work. Update it accordingly if you change it here
588 switch_mm(active_mm
, mm
, tsk
);
596 * Reverses the effect of use_mm, i.e. releases the
597 * specified mm context which was earlier taken on
598 * by the calling kernel thread
599 * (Note: this routine is intended to be called only
600 * from a kernel thread context)
602 static void unuse_mm(struct mm_struct
*mm
)
604 struct task_struct
*tsk
= current
;
607 tsk
->flags
&= ~PF_BORROWED_MM
;
609 /* active_mm is still 'mm' */
610 enter_lazy_tlb(mm
, tsk
);
615 * Queue up a kiocb to be retried. Assumes that the kiocb
616 * has already been marked as kicked, and places it on
617 * the retry run list for the corresponding ioctx, if it
618 * isn't already queued. Returns 1 if it actually queued
619 * the kiocb (to tell the caller to activate the work
620 * queue to process it), or 0, if it found that it was
623 static inline int __queue_kicked_iocb(struct kiocb
*iocb
)
625 struct kioctx
*ctx
= iocb
->ki_ctx
;
627 assert_spin_locked(&ctx
->ctx_lock
);
629 if (list_empty(&iocb
->ki_run_list
)) {
630 list_add_tail(&iocb
->ki_run_list
,
638 * This is the core aio execution routine. It is
639 * invoked both for initial i/o submission and
640 * subsequent retries via the aio_kick_handler.
641 * Expects to be invoked with iocb->ki_ctx->lock
642 * already held. The lock is released and reacquired
643 * as needed during processing.
645 * Calls the iocb retry method (already setup for the
646 * iocb on initial submission) for operation specific
647 * handling, but takes care of most of common retry
648 * execution details for a given iocb. The retry method
649 * needs to be non-blocking as far as possible, to avoid
650 * holding up other iocbs waiting to be serviced by the
651 * retry kernel thread.
653 * The trickier parts in this code have to do with
654 * ensuring that only one retry instance is in progress
655 * for a given iocb at any time. Providing that guarantee
656 * simplifies the coding of individual aio operations as
657 * it avoids various potential races.
659 static ssize_t
aio_run_iocb(struct kiocb
*iocb
)
661 struct kioctx
*ctx
= iocb
->ki_ctx
;
662 ssize_t (*retry
)(struct kiocb
*);
665 if (!(retry
= iocb
->ki_retry
)) {
666 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
671 * We don't want the next retry iteration for this
672 * operation to start until this one has returned and
673 * updated the iocb state. However, wait_queue functions
674 * can trigger a kick_iocb from interrupt context in the
675 * meantime, indicating that data is available for the next
676 * iteration. We want to remember that and enable the
677 * next retry iteration _after_ we are through with
680 * So, in order to be able to register a "kick", but
681 * prevent it from being queued now, we clear the kick
682 * flag, but make the kick code *think* that the iocb is
683 * still on the run list until we are actually done.
684 * When we are done with this iteration, we check if
685 * the iocb was kicked in the meantime and if so, queue
689 kiocbClearKicked(iocb
);
692 * This is so that aio_complete knows it doesn't need to
693 * pull the iocb off the run list (We can't just call
694 * INIT_LIST_HEAD because we don't want a kick_iocb to
695 * queue this on the run list yet)
697 iocb
->ki_run_list
.next
= iocb
->ki_run_list
.prev
= NULL
;
698 spin_unlock_irq(&ctx
->ctx_lock
);
700 /* Quit retrying if the i/o has been cancelled */
701 if (kiocbIsCancelled(iocb
)) {
703 aio_complete(iocb
, ret
, 0);
704 /* must not access the iocb after this */
709 * Now we are all set to call the retry method in async
710 * context. By setting this thread's io_wait context
711 * to point to the wait queue entry inside the currently
712 * running iocb for the duration of the retry, we ensure
713 * that async notification wakeups are queued by the
714 * operation instead of blocking waits, and when notified,
715 * cause the iocb to be kicked for continuation (through
716 * the aio_wake_function callback).
718 BUG_ON(current
->io_wait
!= NULL
);
719 current
->io_wait
= &iocb
->ki_wait
;
721 current
->io_wait
= NULL
;
723 if (ret
!= -EIOCBRETRY
&& ret
!= -EIOCBQUEUED
) {
724 BUG_ON(!list_empty(&iocb
->ki_wait
.task_list
));
725 aio_complete(iocb
, ret
, 0);
728 spin_lock_irq(&ctx
->ctx_lock
);
730 if (-EIOCBRETRY
== ret
) {
732 * OK, now that we are done with this iteration
733 * and know that there is more left to go,
734 * this is where we let go so that a subsequent
735 * "kick" can start the next iteration
738 /* will make __queue_kicked_iocb succeed from here on */
739 INIT_LIST_HEAD(&iocb
->ki_run_list
);
740 /* we must queue the next iteration ourselves, if it
741 * has already been kicked */
742 if (kiocbIsKicked(iocb
)) {
743 __queue_kicked_iocb(iocb
);
746 * __queue_kicked_iocb will always return 1 here, because
747 * iocb->ki_run_list is empty at this point so it should
748 * be safe to unconditionally queue the context into the
759 * Process all pending retries queued on the ioctx
761 * Assumes it is operating within the aio issuer's mm
764 static int __aio_run_iocbs(struct kioctx
*ctx
)
767 struct list_head run_list
;
769 assert_spin_locked(&ctx
->ctx_lock
);
771 list_replace_init(&ctx
->run_list
, &run_list
);
772 while (!list_empty(&run_list
)) {
773 iocb
= list_entry(run_list
.next
, struct kiocb
,
775 list_del(&iocb
->ki_run_list
);
777 * Hold an extra reference while retrying i/o.
779 iocb
->ki_users
++; /* grab extra reference */
781 __aio_put_req(ctx
, iocb
);
783 if (!list_empty(&ctx
->run_list
))
788 static void aio_queue_work(struct kioctx
* ctx
)
790 unsigned long timeout
;
792 * if someone is waiting, get the work started right
793 * away, otherwise, use a longer delay
796 if (waitqueue_active(&ctx
->wait
))
800 queue_delayed_work(aio_wq
, &ctx
->wq
, timeout
);
806 * Process all pending retries queued on the ioctx
808 * Assumes it is operating within the aio issuer's mm
811 static inline void aio_run_iocbs(struct kioctx
*ctx
)
815 spin_lock_irq(&ctx
->ctx_lock
);
817 requeue
= __aio_run_iocbs(ctx
);
818 spin_unlock_irq(&ctx
->ctx_lock
);
824 * just like aio_run_iocbs, but keeps running them until
825 * the list stays empty
827 static inline void aio_run_all_iocbs(struct kioctx
*ctx
)
829 spin_lock_irq(&ctx
->ctx_lock
);
830 while (__aio_run_iocbs(ctx
))
832 spin_unlock_irq(&ctx
->ctx_lock
);
837 * Work queue handler triggered to process pending
838 * retries on an ioctx. Takes on the aio issuer's
839 * mm context before running the iocbs, so that
840 * copy_xxx_user operates on the issuer's address
842 * Run on aiod's context.
844 static void aio_kick_handler(struct work_struct
*work
)
846 struct kioctx
*ctx
= container_of(work
, struct kioctx
, wq
.work
);
847 mm_segment_t oldfs
= get_fs();
848 struct mm_struct
*mm
;
853 spin_lock_irq(&ctx
->ctx_lock
);
854 requeue
=__aio_run_iocbs(ctx
);
856 spin_unlock_irq(&ctx
->ctx_lock
);
860 * we're in a worker thread already, don't use queue_delayed_work,
863 queue_delayed_work(aio_wq
, &ctx
->wq
, 0);
868 * Called by kick_iocb to queue the kiocb for retry
869 * and if required activate the aio work queue to process
872 static void try_queue_kicked_iocb(struct kiocb
*iocb
)
874 struct kioctx
*ctx
= iocb
->ki_ctx
;
878 /* We're supposed to be the only path putting the iocb back on the run
879 * list. If we find that the iocb is *back* on a wait queue already
880 * than retry has happened before we could queue the iocb. This also
881 * means that the retry could have completed and freed our iocb, no
883 BUG_ON((!list_empty(&iocb
->ki_wait
.task_list
)));
885 spin_lock_irqsave(&ctx
->ctx_lock
, flags
);
886 /* set this inside the lock so that we can't race with aio_run_iocb()
887 * testing it and putting the iocb on the run list under the lock */
888 if (!kiocbTryKick(iocb
))
889 run
= __queue_kicked_iocb(iocb
);
890 spin_unlock_irqrestore(&ctx
->ctx_lock
, flags
);
897 * Called typically from a wait queue callback context
898 * (aio_wake_function) to trigger a retry of the iocb.
899 * The retry is usually executed by aio workqueue
900 * threads (See aio_kick_handler).
902 void fastcall
kick_iocb(struct kiocb
*iocb
)
904 /* sync iocbs are easy: they can only ever be executing from a
906 if (is_sync_kiocb(iocb
)) {
907 kiocbSetKicked(iocb
);
908 wake_up_process(iocb
->ki_obj
.tsk
);
912 try_queue_kicked_iocb(iocb
);
914 EXPORT_SYMBOL(kick_iocb
);
917 * Called when the io request on the given iocb is complete.
918 * Returns true if this is the last user of the request. The
919 * only other user of the request can be the cancellation code.
921 int fastcall
aio_complete(struct kiocb
*iocb
, long res
, long res2
)
923 struct kioctx
*ctx
= iocb
->ki_ctx
;
924 struct aio_ring_info
*info
;
925 struct aio_ring
*ring
;
926 struct io_event
*event
;
932 * Special case handling for sync iocbs:
933 * - events go directly into the iocb for fast handling
934 * - the sync task with the iocb in its stack holds the single iocb
935 * ref, no other paths have a way to get another ref
936 * - the sync task helpfully left a reference to itself in the iocb
938 if (is_sync_kiocb(iocb
)) {
939 BUG_ON(iocb
->ki_users
!= 1);
940 iocb
->ki_user_data
= res
;
942 wake_up_process(iocb
->ki_obj
.tsk
);
946 info
= &ctx
->ring_info
;
948 /* add a completion event to the ring buffer.
949 * must be done holding ctx->ctx_lock to prevent
950 * other code from messing with the tail
951 * pointer since we might be called from irq
954 spin_lock_irqsave(&ctx
->ctx_lock
, flags
);
956 if (iocb
->ki_run_list
.prev
&& !list_empty(&iocb
->ki_run_list
))
957 list_del_init(&iocb
->ki_run_list
);
960 * cancelled requests don't get events, userland was given one
961 * when the event got cancelled.
963 if (kiocbIsCancelled(iocb
))
966 ring
= kmap_atomic(info
->ring_pages
[0], KM_IRQ1
);
969 event
= aio_ring_event(info
, tail
, KM_IRQ0
);
970 if (++tail
>= info
->nr
)
973 event
->obj
= (u64
)(unsigned long)iocb
->ki_obj
.user
;
974 event
->data
= iocb
->ki_user_data
;
978 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
979 ctx
, tail
, iocb
, iocb
->ki_obj
.user
, iocb
->ki_user_data
,
982 /* after flagging the request as done, we
983 * must never even look at it again
985 smp_wmb(); /* make event visible before updating tail */
990 put_aio_ring_event(event
, KM_IRQ0
);
991 kunmap_atomic(ring
, KM_IRQ1
);
993 pr_debug("added to ring %p at [%lu]\n", iocb
, tail
);
995 /* everything turned out well, dispose of the aiocb. */
996 ret
= __aio_put_req(ctx
, iocb
);
998 if (waitqueue_active(&ctx
->wait
))
1001 spin_unlock_irqrestore(&ctx
->ctx_lock
, flags
);
1006 * Pull an event off of the ioctx's event ring. Returns the number of
1007 * events fetched (0 or 1 ;-)
1008 * FIXME: make this use cmpxchg.
1009 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1011 static int aio_read_evt(struct kioctx
*ioctx
, struct io_event
*ent
)
1013 struct aio_ring_info
*info
= &ioctx
->ring_info
;
1014 struct aio_ring
*ring
;
1018 ring
= kmap_atomic(info
->ring_pages
[0], KM_USER0
);
1019 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1020 (unsigned long)ring
->head
, (unsigned long)ring
->tail
,
1021 (unsigned long)ring
->nr
);
1023 if (ring
->head
== ring
->tail
)
1026 spin_lock(&info
->ring_lock
);
1028 head
= ring
->head
% info
->nr
;
1029 if (head
!= ring
->tail
) {
1030 struct io_event
*evp
= aio_ring_event(info
, head
, KM_USER1
);
1032 head
= (head
+ 1) % info
->nr
;
1033 smp_mb(); /* finish reading the event before updatng the head */
1036 put_aio_ring_event(evp
, KM_USER1
);
1038 spin_unlock(&info
->ring_lock
);
1041 kunmap_atomic(ring
, KM_USER0
);
1042 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret
,
1043 (unsigned long)ring
->head
, (unsigned long)ring
->tail
);
1047 struct aio_timeout
{
1048 struct timer_list timer
;
1050 struct task_struct
*p
;
1053 static void timeout_func(unsigned long data
)
1055 struct aio_timeout
*to
= (struct aio_timeout
*)data
;
1058 wake_up_process(to
->p
);
1061 static inline void init_timeout(struct aio_timeout
*to
)
1063 init_timer(&to
->timer
);
1064 to
->timer
.data
= (unsigned long)to
;
1065 to
->timer
.function
= timeout_func
;
1070 static inline void set_timeout(long start_jiffies
, struct aio_timeout
*to
,
1071 const struct timespec
*ts
)
1073 to
->timer
.expires
= start_jiffies
+ timespec_to_jiffies(ts
);
1074 if (time_after(to
->timer
.expires
, jiffies
))
1075 add_timer(&to
->timer
);
1080 static inline void clear_timeout(struct aio_timeout
*to
)
1082 del_singleshot_timer_sync(&to
->timer
);
1085 static int read_events(struct kioctx
*ctx
,
1086 long min_nr
, long nr
,
1087 struct io_event __user
*event
,
1088 struct timespec __user
*timeout
)
1090 long start_jiffies
= jiffies
;
1091 struct task_struct
*tsk
= current
;
1092 DECLARE_WAITQUEUE(wait
, tsk
);
1095 struct io_event ent
;
1096 struct aio_timeout to
;
1099 /* needed to zero any padding within an entry (there shouldn't be
1100 * any, but C is fun!
1102 memset(&ent
, 0, sizeof(ent
));
1105 while (likely(i
< nr
)) {
1106 ret
= aio_read_evt(ctx
, &ent
);
1107 if (unlikely(ret
<= 0))
1110 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1111 ent
.data
, ent
.obj
, ent
.res
, ent
.res2
);
1113 /* Could we split the check in two? */
1115 if (unlikely(copy_to_user(event
, &ent
, sizeof(ent
)))) {
1116 dprintk("aio: lost an event due to EFAULT.\n");
1121 /* Good, event copied to userland, update counts. */
1133 /* racey check, but it gets redone */
1134 if (!retry
&& unlikely(!list_empty(&ctx
->run_list
))) {
1136 aio_run_all_iocbs(ctx
);
1144 if (unlikely(copy_from_user(&ts
, timeout
, sizeof(ts
))))
1147 set_timeout(start_jiffies
, &to
, &ts
);
1150 while (likely(i
< nr
)) {
1151 add_wait_queue_exclusive(&ctx
->wait
, &wait
);
1153 set_task_state(tsk
, TASK_INTERRUPTIBLE
);
1154 ret
= aio_read_evt(ctx
, &ent
);
1160 if (to
.timed_out
) /* Only check after read evt */
1163 if (signal_pending(tsk
)) {
1167 /*ret = aio_read_evt(ctx, &ent);*/
1170 set_task_state(tsk
, TASK_RUNNING
);
1171 remove_wait_queue(&ctx
->wait
, &wait
);
1173 if (unlikely(ret
<= 0))
1177 if (unlikely(copy_to_user(event
, &ent
, sizeof(ent
)))) {
1178 dprintk("aio: lost an event due to EFAULT.\n");
1182 /* Good, event copied to userland, update counts. */
1193 /* Take an ioctx and remove it from the list of ioctx's. Protects
1194 * against races with itself via ->dead.
1196 static void io_destroy(struct kioctx
*ioctx
)
1198 struct mm_struct
*mm
= current
->mm
;
1199 struct kioctx
**tmp
;
1202 /* delete the entry from the list is someone else hasn't already */
1203 write_lock(&mm
->ioctx_list_lock
);
1204 was_dead
= ioctx
->dead
;
1206 for (tmp
= &mm
->ioctx_list
; *tmp
&& *tmp
!= ioctx
;
1207 tmp
= &(*tmp
)->next
)
1211 write_unlock(&mm
->ioctx_list_lock
);
1213 dprintk("aio_release(%p)\n", ioctx
);
1214 if (likely(!was_dead
))
1215 put_ioctx(ioctx
); /* twice for the list */
1217 aio_cancel_all(ioctx
);
1218 wait_for_all_aios(ioctx
);
1219 put_ioctx(ioctx
); /* once for the lookup */
1223 * Create an aio_context capable of receiving at least nr_events.
1224 * ctxp must not point to an aio_context that already exists, and
1225 * must be initialized to 0 prior to the call. On successful
1226 * creation of the aio_context, *ctxp is filled in with the resulting
1227 * handle. May fail with -EINVAL if *ctxp is not initialized,
1228 * if the specified nr_events exceeds internal limits. May fail
1229 * with -EAGAIN if the specified nr_events exceeds the user's limit
1230 * of available events. May fail with -ENOMEM if insufficient kernel
1231 * resources are available. May fail with -EFAULT if an invalid
1232 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1235 asmlinkage
long sys_io_setup(unsigned nr_events
, aio_context_t __user
*ctxp
)
1237 struct kioctx
*ioctx
= NULL
;
1241 ret
= get_user(ctx
, ctxp
);
1246 if (unlikely(ctx
|| nr_events
== 0)) {
1247 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1252 ioctx
= ioctx_alloc(nr_events
);
1253 ret
= PTR_ERR(ioctx
);
1254 if (!IS_ERR(ioctx
)) {
1255 ret
= put_user(ioctx
->user_id
, ctxp
);
1259 get_ioctx(ioctx
); /* io_destroy() expects us to hold a ref */
1268 * Destroy the aio_context specified. May cancel any outstanding
1269 * AIOs and block on completion. Will fail with -ENOSYS if not
1270 * implemented. May fail with -EFAULT if the context pointed to
1273 asmlinkage
long sys_io_destroy(aio_context_t ctx
)
1275 struct kioctx
*ioctx
= lookup_ioctx(ctx
);
1276 if (likely(NULL
!= ioctx
)) {
1280 pr_debug("EINVAL: io_destroy: invalid context id\n");
1284 static void aio_advance_iovec(struct kiocb
*iocb
, ssize_t ret
)
1286 struct iovec
*iov
= &iocb
->ki_iovec
[iocb
->ki_cur_seg
];
1290 while (iocb
->ki_cur_seg
< iocb
->ki_nr_segs
&& ret
> 0) {
1291 ssize_t
this = min((ssize_t
)iov
->iov_len
, ret
);
1292 iov
->iov_base
+= this;
1293 iov
->iov_len
-= this;
1294 iocb
->ki_left
-= this;
1296 if (iov
->iov_len
== 0) {
1302 /* the caller should not have done more io than what fit in
1303 * the remaining iovecs */
1304 BUG_ON(ret
> 0 && iocb
->ki_left
== 0);
1307 static ssize_t
aio_rw_vect_retry(struct kiocb
*iocb
)
1309 struct file
*file
= iocb
->ki_filp
;
1310 struct address_space
*mapping
= file
->f_mapping
;
1311 struct inode
*inode
= mapping
->host
;
1312 ssize_t (*rw_op
)(struct kiocb
*, const struct iovec
*,
1313 unsigned long, loff_t
);
1315 unsigned short opcode
;
1317 if ((iocb
->ki_opcode
== IOCB_CMD_PREADV
) ||
1318 (iocb
->ki_opcode
== IOCB_CMD_PREAD
)) {
1319 rw_op
= file
->f_op
->aio_read
;
1320 opcode
= IOCB_CMD_PREADV
;
1322 rw_op
= file
->f_op
->aio_write
;
1323 opcode
= IOCB_CMD_PWRITEV
;
1327 ret
= rw_op(iocb
, &iocb
->ki_iovec
[iocb
->ki_cur_seg
],
1328 iocb
->ki_nr_segs
- iocb
->ki_cur_seg
,
1331 aio_advance_iovec(iocb
, ret
);
1333 /* retry all partial writes. retry partial reads as long as its a
1335 } while (ret
> 0 && iocb
->ki_left
> 0 &&
1336 (opcode
== IOCB_CMD_PWRITEV
||
1337 (!S_ISFIFO(inode
->i_mode
) && !S_ISSOCK(inode
->i_mode
))));
1339 /* This means we must have transferred all that we could */
1340 /* No need to retry anymore */
1341 if ((ret
== 0) || (iocb
->ki_left
== 0))
1342 ret
= iocb
->ki_nbytes
- iocb
->ki_left
;
1347 static ssize_t
aio_fdsync(struct kiocb
*iocb
)
1349 struct file
*file
= iocb
->ki_filp
;
1350 ssize_t ret
= -EINVAL
;
1352 if (file
->f_op
->aio_fsync
)
1353 ret
= file
->f_op
->aio_fsync(iocb
, 1);
1357 static ssize_t
aio_fsync(struct kiocb
*iocb
)
1359 struct file
*file
= iocb
->ki_filp
;
1360 ssize_t ret
= -EINVAL
;
1362 if (file
->f_op
->aio_fsync
)
1363 ret
= file
->f_op
->aio_fsync(iocb
, 0);
1367 static ssize_t
aio_setup_vectored_rw(int type
, struct kiocb
*kiocb
)
1371 ret
= rw_copy_check_uvector(type
, (struct iovec __user
*)kiocb
->ki_buf
,
1372 kiocb
->ki_nbytes
, 1,
1373 &kiocb
->ki_inline_vec
, &kiocb
->ki_iovec
);
1377 kiocb
->ki_nr_segs
= kiocb
->ki_nbytes
;
1378 kiocb
->ki_cur_seg
= 0;
1379 /* ki_nbytes/left now reflect bytes instead of segs */
1380 kiocb
->ki_nbytes
= ret
;
1381 kiocb
->ki_left
= ret
;
1388 static ssize_t
aio_setup_single_vector(struct kiocb
*kiocb
)
1390 kiocb
->ki_iovec
= &kiocb
->ki_inline_vec
;
1391 kiocb
->ki_iovec
->iov_base
= kiocb
->ki_buf
;
1392 kiocb
->ki_iovec
->iov_len
= kiocb
->ki_left
;
1393 kiocb
->ki_nr_segs
= 1;
1394 kiocb
->ki_cur_seg
= 0;
1400 * Performs the initial checks and aio retry method
1401 * setup for the kiocb at the time of io submission.
1403 static ssize_t
aio_setup_iocb(struct kiocb
*kiocb
)
1405 struct file
*file
= kiocb
->ki_filp
;
1408 switch (kiocb
->ki_opcode
) {
1409 case IOCB_CMD_PREAD
:
1411 if (unlikely(!(file
->f_mode
& FMODE_READ
)))
1414 if (unlikely(!access_ok(VERIFY_WRITE
, kiocb
->ki_buf
,
1417 ret
= security_file_permission(file
, MAY_READ
);
1420 ret
= aio_setup_single_vector(kiocb
);
1424 if (file
->f_op
->aio_read
)
1425 kiocb
->ki_retry
= aio_rw_vect_retry
;
1427 case IOCB_CMD_PWRITE
:
1429 if (unlikely(!(file
->f_mode
& FMODE_WRITE
)))
1432 if (unlikely(!access_ok(VERIFY_READ
, kiocb
->ki_buf
,
1435 ret
= security_file_permission(file
, MAY_WRITE
);
1438 ret
= aio_setup_single_vector(kiocb
);
1442 if (file
->f_op
->aio_write
)
1443 kiocb
->ki_retry
= aio_rw_vect_retry
;
1445 case IOCB_CMD_PREADV
:
1447 if (unlikely(!(file
->f_mode
& FMODE_READ
)))
1449 ret
= security_file_permission(file
, MAY_READ
);
1452 ret
= aio_setup_vectored_rw(READ
, kiocb
);
1456 if (file
->f_op
->aio_read
)
1457 kiocb
->ki_retry
= aio_rw_vect_retry
;
1459 case IOCB_CMD_PWRITEV
:
1461 if (unlikely(!(file
->f_mode
& FMODE_WRITE
)))
1463 ret
= security_file_permission(file
, MAY_WRITE
);
1466 ret
= aio_setup_vectored_rw(WRITE
, kiocb
);
1470 if (file
->f_op
->aio_write
)
1471 kiocb
->ki_retry
= aio_rw_vect_retry
;
1473 case IOCB_CMD_FDSYNC
:
1475 if (file
->f_op
->aio_fsync
)
1476 kiocb
->ki_retry
= aio_fdsync
;
1478 case IOCB_CMD_FSYNC
:
1480 if (file
->f_op
->aio_fsync
)
1481 kiocb
->ki_retry
= aio_fsync
;
1484 dprintk("EINVAL: io_submit: no operation provided\n");
1488 if (!kiocb
->ki_retry
)
1495 * aio_wake_function:
1496 * wait queue callback function for aio notification,
1497 * Simply triggers a retry of the operation via kick_iocb.
1499 * This callback is specified in the wait queue entry in
1500 * a kiocb (current->io_wait points to this wait queue
1501 * entry when an aio operation executes; it is used
1502 * instead of a synchronous wait when an i/o blocking
1503 * condition is encountered during aio).
1506 * This routine is executed with the wait queue lock held.
1507 * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
1508 * the ioctx lock inside the wait queue lock. This is safe
1509 * because this callback isn't used for wait queues which
1510 * are nested inside ioctx lock (i.e. ctx->wait)
1512 static int aio_wake_function(wait_queue_t
*wait
, unsigned mode
,
1513 int sync
, void *key
)
1515 struct kiocb
*iocb
= container_of(wait
, struct kiocb
, ki_wait
);
1517 list_del_init(&wait
->task_list
);
1522 int fastcall
io_submit_one(struct kioctx
*ctx
, struct iocb __user
*user_iocb
,
1529 /* enforce forwards compatibility on users */
1530 if (unlikely(iocb
->aio_reserved1
|| iocb
->aio_reserved2
||
1531 iocb
->aio_reserved3
)) {
1532 pr_debug("EINVAL: io_submit: reserve field set\n");
1536 /* prevent overflows */
1538 (iocb
->aio_buf
!= (unsigned long)iocb
->aio_buf
) ||
1539 (iocb
->aio_nbytes
!= (size_t)iocb
->aio_nbytes
) ||
1540 ((ssize_t
)iocb
->aio_nbytes
< 0)
1542 pr_debug("EINVAL: io_submit: overflow check\n");
1546 file
= fget(iocb
->aio_fildes
);
1547 if (unlikely(!file
))
1550 req
= aio_get_req(ctx
); /* returns with 2 references to req */
1551 if (unlikely(!req
)) {
1556 req
->ki_filp
= file
;
1557 ret
= put_user(req
->ki_key
, &user_iocb
->aio_key
);
1558 if (unlikely(ret
)) {
1559 dprintk("EFAULT: aio_key\n");
1563 req
->ki_obj
.user
= user_iocb
;
1564 req
->ki_user_data
= iocb
->aio_data
;
1565 req
->ki_pos
= iocb
->aio_offset
;
1567 req
->ki_buf
= (char __user
*)(unsigned long)iocb
->aio_buf
;
1568 req
->ki_left
= req
->ki_nbytes
= iocb
->aio_nbytes
;
1569 req
->ki_opcode
= iocb
->aio_lio_opcode
;
1570 init_waitqueue_func_entry(&req
->ki_wait
, aio_wake_function
);
1571 INIT_LIST_HEAD(&req
->ki_wait
.task_list
);
1573 ret
= aio_setup_iocb(req
);
1578 spin_lock_irq(&ctx
->ctx_lock
);
1580 if (!list_empty(&ctx
->run_list
)) {
1581 /* drain the run list */
1582 while (__aio_run_iocbs(ctx
))
1585 spin_unlock_irq(&ctx
->ctx_lock
);
1586 aio_put_req(req
); /* drop extra ref to req */
1590 aio_put_req(req
); /* drop extra ref to req */
1591 aio_put_req(req
); /* drop i/o ref to req */
1596 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1597 * the number of iocbs queued. May return -EINVAL if the aio_context
1598 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1599 * *iocbpp[0] is not properly initialized, if the operation specified
1600 * is invalid for the file descriptor in the iocb. May fail with
1601 * -EFAULT if any of the data structures point to invalid data. May
1602 * fail with -EBADF if the file descriptor specified in the first
1603 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1604 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1605 * fail with -ENOSYS if not implemented.
1607 asmlinkage
long sys_io_submit(aio_context_t ctx_id
, long nr
,
1608 struct iocb __user
* __user
*iocbpp
)
1614 if (unlikely(nr
< 0))
1617 if (unlikely(!access_ok(VERIFY_READ
, iocbpp
, (nr
*sizeof(*iocbpp
)))))
1620 ctx
= lookup_ioctx(ctx_id
);
1621 if (unlikely(!ctx
)) {
1622 pr_debug("EINVAL: io_submit: invalid context id\n");
1627 * AKPM: should this return a partial result if some of the IOs were
1628 * successfully submitted?
1630 for (i
=0; i
<nr
; i
++) {
1631 struct iocb __user
*user_iocb
;
1634 if (unlikely(__get_user(user_iocb
, iocbpp
+ i
))) {
1639 if (unlikely(copy_from_user(&tmp
, user_iocb
, sizeof(tmp
)))) {
1644 ret
= io_submit_one(ctx
, user_iocb
, &tmp
);
1654 * Finds a given iocb for cancellation.
1656 static struct kiocb
*lookup_kiocb(struct kioctx
*ctx
, struct iocb __user
*iocb
,
1659 struct list_head
*pos
;
1661 assert_spin_locked(&ctx
->ctx_lock
);
1663 /* TODO: use a hash or array, this sucks. */
1664 list_for_each(pos
, &ctx
->active_reqs
) {
1665 struct kiocb
*kiocb
= list_kiocb(pos
);
1666 if (kiocb
->ki_obj
.user
== iocb
&& kiocb
->ki_key
== key
)
1673 * Attempts to cancel an iocb previously passed to io_submit. If
1674 * the operation is successfully cancelled, the resulting event is
1675 * copied into the memory pointed to by result without being placed
1676 * into the completion queue and 0 is returned. May fail with
1677 * -EFAULT if any of the data structures pointed to are invalid.
1678 * May fail with -EINVAL if aio_context specified by ctx_id is
1679 * invalid. May fail with -EAGAIN if the iocb specified was not
1680 * cancelled. Will fail with -ENOSYS if not implemented.
1682 asmlinkage
long sys_io_cancel(aio_context_t ctx_id
, struct iocb __user
*iocb
,
1683 struct io_event __user
*result
)
1685 int (*cancel
)(struct kiocb
*iocb
, struct io_event
*res
);
1687 struct kiocb
*kiocb
;
1691 ret
= get_user(key
, &iocb
->aio_key
);
1695 ctx
= lookup_ioctx(ctx_id
);
1699 spin_lock_irq(&ctx
->ctx_lock
);
1701 kiocb
= lookup_kiocb(ctx
, iocb
, key
);
1702 if (kiocb
&& kiocb
->ki_cancel
) {
1703 cancel
= kiocb
->ki_cancel
;
1705 kiocbSetCancelled(kiocb
);
1708 spin_unlock_irq(&ctx
->ctx_lock
);
1710 if (NULL
!= cancel
) {
1711 struct io_event tmp
;
1712 pr_debug("calling cancel\n");
1713 memset(&tmp
, 0, sizeof(tmp
));
1714 tmp
.obj
= (u64
)(unsigned long)kiocb
->ki_obj
.user
;
1715 tmp
.data
= kiocb
->ki_user_data
;
1716 ret
= cancel(kiocb
, &tmp
);
1718 /* Cancellation succeeded -- copy the result
1719 * into the user's buffer.
1721 if (copy_to_user(result
, &tmp
, sizeof(tmp
)))
1733 * Attempts to read at least min_nr events and up to nr events from
1734 * the completion queue for the aio_context specified by ctx_id. May
1735 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1736 * if nr is out of range, if when is out of range. May fail with
1737 * -EFAULT if any of the memory specified to is invalid. May return
1738 * 0 or < min_nr if no events are available and the timeout specified
1739 * by when has elapsed, where when == NULL specifies an infinite
1740 * timeout. Note that the timeout pointed to by when is relative and
1741 * will be updated if not NULL and the operation blocks. Will fail
1742 * with -ENOSYS if not implemented.
1744 asmlinkage
long sys_io_getevents(aio_context_t ctx_id
,
1747 struct io_event __user
*events
,
1748 struct timespec __user
*timeout
)
1750 struct kioctx
*ioctx
= lookup_ioctx(ctx_id
);
1753 if (likely(ioctx
)) {
1754 if (likely(min_nr
<= nr
&& min_nr
>= 0 && nr
>= 0))
1755 ret
= read_events(ioctx
, min_nr
, nr
, events
, timeout
);
1762 __initcall(aio_setup
);
1764 EXPORT_SYMBOL(aio_complete
);
1765 EXPORT_SYMBOL(aio_put_req
);
1766 EXPORT_SYMBOL(wait_on_sync_kiocb
);