Add to mips64 also for symmetry.
[linux-2.6/linux-mips.git] / fs / aio.c
blobd006eba2335064fc6503877551db9d4d7e31d4e5
1 /*
2 * An async IO implementation for Linux
3 * Written by Benjamin LaHaise <bcrl@redhat.com>
5 * Implements an efficient asynchronous io interface.
7 * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
9 * See ../COPYING for licensing terms.
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/time.h>
15 #include <linux/aio_abi.h>
16 #include <linux/module.h>
18 //#define DEBUG 1
20 #include <linux/sched.h>
21 #include <linux/fs.h>
22 #include <linux/file.h>
23 #include <linux/mm.h>
24 #include <linux/mman.h>
25 #include <linux/slab.h>
26 #include <linux/timer.h>
27 #include <linux/aio.h>
28 #include <linux/module.h>
29 #include <linux/highmem.h>
30 #include <linux/workqueue.h>
32 #include <asm/uaccess.h>
33 #include <asm/mmu_context.h>
35 #if DEBUG > 1
36 #define dprintk printk
37 #else
38 #define dprintk(x...) do { ; } while (0)
39 #endif
41 /*------ sysctl variables----*/
42 atomic_t aio_nr = ATOMIC_INIT(0); /* current system wide number of aio requests */
43 unsigned aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
44 /*----end sysctl variables---*/
46 static kmem_cache_t *kiocb_cachep;
47 static kmem_cache_t *kioctx_cachep;
49 static struct workqueue_struct *aio_wq;
51 /* Used for rare fput completion. */
52 static void aio_fput_routine(void *);
53 static DECLARE_WORK(fput_work, aio_fput_routine, NULL);
55 static spinlock_t fput_lock = SPIN_LOCK_UNLOCKED;
56 LIST_HEAD(fput_head);
58 static void aio_kick_handler(void *);
60 /* aio_setup
61 * Creates the slab caches used by the aio routines, panic on
62 * failure as this is done early during the boot sequence.
64 static int __init aio_setup(void)
66 kiocb_cachep = kmem_cache_create("kiocb", sizeof(struct kiocb),
67 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
68 if (!kiocb_cachep)
69 panic("unable to create kiocb cache\n");
71 kioctx_cachep = kmem_cache_create("kioctx", sizeof(struct kioctx),
72 0, SLAB_HWCACHE_ALIGN, NULL, NULL);
73 if (!kioctx_cachep)
74 panic("unable to create kioctx cache");
76 aio_wq = create_workqueue("aio");
78 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
80 return 0;
83 static void aio_free_ring(struct kioctx *ctx)
85 struct aio_ring_info *info = &ctx->ring_info;
86 long i;
88 for (i=0; i<info->nr_pages; i++)
89 put_page(info->ring_pages[i]);
91 if (info->mmap_size) {
92 down_write(&ctx->mm->mmap_sem);
93 do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
94 up_write(&ctx->mm->mmap_sem);
97 if (info->ring_pages && info->ring_pages != info->internal_pages)
98 kfree(info->ring_pages);
99 info->ring_pages = NULL;
100 info->nr = 0;
103 static int aio_setup_ring(struct kioctx *ctx)
105 struct aio_ring *ring;
106 struct aio_ring_info *info = &ctx->ring_info;
107 unsigned nr_events = ctx->max_reqs;
108 unsigned long size;
109 int nr_pages;
111 /* Compensate for the ring buffer's head/tail overlap entry */
112 nr_events += 2; /* 1 is required, 2 for good luck */
114 size = sizeof(struct aio_ring);
115 size += sizeof(struct io_event) * nr_events;
116 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
118 if (nr_pages < 0)
119 return -EINVAL;
121 info->nr_pages = nr_pages;
123 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
125 info->nr = 0;
126 info->ring_pages = info->internal_pages;
127 if (nr_pages > AIO_RING_PAGES) {
128 info->ring_pages = kmalloc(sizeof(struct page *) * nr_pages, GFP_KERNEL);
129 if (!info->ring_pages)
130 return -ENOMEM;
131 memset(info->ring_pages, 0, sizeof(struct page *) * nr_pages);
134 info->mmap_size = nr_pages * PAGE_SIZE;
135 dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
136 down_write(&ctx->mm->mmap_sem);
137 info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
138 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
140 if (IS_ERR((void *)info->mmap_base)) {
141 up_write(&ctx->mm->mmap_sem);
142 printk("mmap err: %ld\n", -info->mmap_base);
143 info->mmap_size = 0;
144 aio_free_ring(ctx);
145 return -EAGAIN;
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)) {
155 aio_free_ring(ctx);
156 return -EAGAIN;
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);
173 return 0;
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; \
190 __event; \
193 #define put_aio_ring_event(event, km) do { \
194 struct io_event *__event = (event); \
195 (void)__event; \
196 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
197 } while(0)
199 /* ioctx_alloc
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;
205 struct kioctx *ctx;
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 (nr_events > aio_max_nr)
215 return ERR_PTR(-EAGAIN);
217 ctx = kmem_cache_alloc(kioctx_cachep, GFP_KERNEL);
218 if (!ctx)
219 return ERR_PTR(-ENOMEM);
221 memset(ctx, 0, sizeof(*ctx));
222 ctx->max_reqs = nr_events;
223 mm = ctx->mm = current->mm;
224 atomic_inc(&mm->mm_count);
226 atomic_set(&ctx->users, 1);
227 spin_lock_init(&ctx->ctx_lock);
228 spin_lock_init(&ctx->ring_info.ring_lock);
229 init_waitqueue_head(&ctx->wait);
231 INIT_LIST_HEAD(&ctx->active_reqs);
232 INIT_LIST_HEAD(&ctx->run_list);
233 INIT_WORK(&ctx->wq, aio_kick_handler, ctx);
235 if (aio_setup_ring(ctx) < 0)
236 goto out_freectx;
238 /* limit the number of system wide aios */
239 atomic_add(ctx->max_reqs, &aio_nr); /* undone by __put_ioctx */
240 if (unlikely(atomic_read(&aio_nr) > aio_max_nr))
241 goto out_cleanup;
243 /* now link into global list. kludge. FIXME */
244 write_lock(&mm->ioctx_list_lock);
245 ctx->next = mm->ioctx_list;
246 mm->ioctx_list = ctx;
247 write_unlock(&mm->ioctx_list_lock);
249 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
250 ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
251 return ctx;
253 out_cleanup:
254 atomic_sub(ctx->max_reqs, &aio_nr);
255 ctx->max_reqs = 0; /* prevent __put_ioctx from sub'ing aio_nr */
256 __put_ioctx(ctx);
257 return ERR_PTR(-EAGAIN);
259 out_freectx:
260 kmem_cache_free(kioctx_cachep, ctx);
261 ctx = ERR_PTR(-ENOMEM);
263 dprintk("aio: error allocating ioctx %p\n", ctx);
264 return ctx;
267 /* aio_cancel_all
268 * Cancels all outstanding aio requests on an aio context. Used
269 * when the processes owning a context have all exited to encourage
270 * the rapid destruction of the kioctx.
272 static void aio_cancel_all(struct kioctx *ctx)
274 int (*cancel)(struct kiocb *, struct io_event *);
275 struct io_event res;
276 spin_lock_irq(&ctx->ctx_lock);
277 ctx->dead = 1;
278 while (!list_empty(&ctx->active_reqs)) {
279 struct list_head *pos = ctx->active_reqs.next;
280 struct kiocb *iocb = list_kiocb(pos);
281 list_del_init(&iocb->ki_list);
282 cancel = iocb->ki_cancel;
283 if (cancel) {
284 iocb->ki_users++;
285 spin_unlock_irq(&ctx->ctx_lock);
286 cancel(iocb, &res);
287 spin_lock_irq(&ctx->ctx_lock);
290 spin_unlock_irq(&ctx->ctx_lock);
293 void wait_for_all_aios(struct kioctx *ctx)
295 struct task_struct *tsk = current;
296 DECLARE_WAITQUEUE(wait, tsk);
298 if (!ctx->reqs_active)
299 return;
301 add_wait_queue(&ctx->wait, &wait);
302 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
303 while (ctx->reqs_active) {
304 schedule();
305 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
307 __set_task_state(tsk, TASK_RUNNING);
308 remove_wait_queue(&ctx->wait, &wait);
311 /* wait_on_sync_kiocb:
312 * Waits on the given sync kiocb to complete.
314 ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
316 while (iocb->ki_users) {
317 set_current_state(TASK_UNINTERRUPTIBLE);
318 if (!iocb->ki_users)
319 break;
320 schedule();
322 __set_current_state(TASK_RUNNING);
323 return iocb->ki_user_data;
326 /* exit_aio: called when the last user of mm goes away. At this point,
327 * there is no way for any new requests to be submited or any of the
328 * io_* syscalls to be called on the context. However, there may be
329 * outstanding requests which hold references to the context; as they
330 * go away, they will call put_ioctx and release any pinned memory
331 * associated with the request (held via struct page * references).
333 void exit_aio(struct mm_struct *mm)
335 struct kioctx *ctx = mm->ioctx_list;
336 mm->ioctx_list = NULL;
337 while (ctx) {
338 struct kioctx *next = ctx->next;
339 ctx->next = NULL;
340 aio_cancel_all(ctx);
342 wait_for_all_aios(ctx);
344 if (1 != atomic_read(&ctx->users))
345 printk(KERN_DEBUG
346 "exit_aio:ioctx still alive: %d %d %d\n",
347 atomic_read(&ctx->users), ctx->dead,
348 ctx->reqs_active);
349 put_ioctx(ctx);
350 ctx = next;
354 /* __put_ioctx
355 * Called when the last user of an aio context has gone away,
356 * and the struct needs to be freed.
358 void __put_ioctx(struct kioctx *ctx)
360 unsigned nr_events = ctx->max_reqs;
362 if (unlikely(ctx->reqs_active))
363 BUG();
365 aio_free_ring(ctx);
366 mmdrop(ctx->mm);
367 ctx->mm = NULL;
368 pr_debug("__put_ioctx: freeing %p\n", ctx);
369 kmem_cache_free(kioctx_cachep, ctx);
371 atomic_sub(nr_events, &aio_nr);
374 /* aio_get_req
375 * Allocate a slot for an aio request. Increments the users count
376 * of the kioctx so that the kioctx stays around until all requests are
377 * complete. Returns NULL if no requests are free.
379 static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
380 static struct kiocb *__aio_get_req(struct kioctx *ctx)
382 struct kiocb *req = NULL;
383 struct aio_ring *ring;
384 int okay = 0;
386 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
387 if (unlikely(!req))
388 return NULL;
390 req->ki_flags = 1 << KIF_LOCKED;
391 req->ki_users = 1;
392 req->ki_key = 0;
393 req->ki_ctx = ctx;
394 req->ki_cancel = NULL;
395 req->ki_retry = NULL;
396 req->ki_user_obj = NULL;
398 /* Check if the completion queue has enough free space to
399 * accept an event from this io.
401 spin_lock_irq(&ctx->ctx_lock);
402 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
403 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
404 list_add(&req->ki_list, &ctx->active_reqs);
405 get_ioctx(ctx);
406 ctx->reqs_active++;
407 okay = 1;
409 kunmap_atomic(ring, KM_USER0);
410 spin_unlock_irq(&ctx->ctx_lock);
412 if (!okay) {
413 kmem_cache_free(kiocb_cachep, req);
414 req = NULL;
417 return req;
420 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
422 struct kiocb *req;
423 /* Handle a potential starvation case -- should be exceedingly rare as
424 * requests will be stuck on fput_head only if the aio_fput_routine is
425 * delayed and the requests were the last user of the struct file.
427 req = __aio_get_req(ctx);
428 if (unlikely(NULL == req)) {
429 aio_fput_routine(NULL);
430 req = __aio_get_req(ctx);
432 return req;
435 static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
437 req->ki_ctx = NULL;
438 req->ki_filp = NULL;
439 req->ki_user_obj = NULL;
440 kmem_cache_free(kiocb_cachep, req);
441 ctx->reqs_active--;
443 if (unlikely(!ctx->reqs_active && ctx->dead))
444 wake_up(&ctx->wait);
447 static void aio_fput_routine(void *data)
449 spin_lock_irq(&fput_lock);
450 while (likely(!list_empty(&fput_head))) {
451 struct kiocb *req = list_kiocb(fput_head.next);
452 struct kioctx *ctx = req->ki_ctx;
454 list_del(&req->ki_list);
455 spin_unlock_irq(&fput_lock);
457 /* Complete the fput */
458 __fput(req->ki_filp);
460 /* Link the iocb into the context's free list */
461 spin_lock_irq(&ctx->ctx_lock);
462 really_put_req(ctx, req);
463 spin_unlock_irq(&ctx->ctx_lock);
465 put_ioctx(ctx);
466 spin_lock_irq(&fput_lock);
468 spin_unlock_irq(&fput_lock);
471 /* __aio_put_req
472 * Returns true if this put was the last user of the request.
474 static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
476 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
477 req, atomic_read(&req->ki_filp->f_count));
479 req->ki_users --;
480 if (unlikely(req->ki_users < 0))
481 BUG();
482 if (likely(req->ki_users))
483 return 0;
484 list_del(&req->ki_list); /* remove from active_reqs */
485 req->ki_cancel = NULL;
486 req->ki_retry = NULL;
488 /* Must be done under the lock to serialise against cancellation.
489 * Call this aio_fput as it duplicates fput via the fput_work.
491 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
492 get_ioctx(ctx);
493 spin_lock(&fput_lock);
494 list_add(&req->ki_list, &fput_head);
495 spin_unlock(&fput_lock);
496 queue_work(aio_wq, &fput_work);
497 } else
498 really_put_req(ctx, req);
499 return 1;
502 /* aio_put_req
503 * Returns true if this put was the last user of the kiocb,
504 * false if the request is still in use.
506 int aio_put_req(struct kiocb *req)
508 struct kioctx *ctx = req->ki_ctx;
509 int ret;
510 spin_lock_irq(&ctx->ctx_lock);
511 ret = __aio_put_req(ctx, req);
512 spin_unlock_irq(&ctx->ctx_lock);
513 if (ret)
514 put_ioctx(ctx);
515 return ret;
518 /* Lookup an ioctx id. ioctx_list is lockless for reads.
519 * FIXME: this is O(n) and is only suitable for development.
521 struct kioctx *lookup_ioctx(unsigned long ctx_id)
523 struct kioctx *ioctx;
524 struct mm_struct *mm;
526 mm = current->mm;
527 read_lock(&mm->ioctx_list_lock);
528 for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
529 if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
530 get_ioctx(ioctx);
531 break;
533 read_unlock(&mm->ioctx_list_lock);
535 return ioctx;
538 static void use_mm(struct mm_struct *mm)
540 struct mm_struct *active_mm = current->active_mm;
541 atomic_inc(&mm->mm_count);
542 current->mm = mm;
543 if (mm != active_mm) {
544 current->active_mm = mm;
545 activate_mm(active_mm, mm);
547 mmdrop(active_mm);
550 static void unuse_mm(struct mm_struct *mm)
552 current->mm = NULL;
553 /* active_mm is still 'mm' */
554 enter_lazy_tlb(mm, current);
557 /* Run on kevent's context. FIXME: needs to be per-cpu and warn if an
558 * operation blocks.
560 static void aio_kick_handler(void *data)
562 struct kioctx *ctx = data;
564 use_mm(ctx->mm);
566 spin_lock_irq(&ctx->ctx_lock);
567 while (!list_empty(&ctx->run_list)) {
568 struct kiocb *iocb;
569 long ret;
571 iocb = list_entry(ctx->run_list.next, struct kiocb,
572 ki_run_list);
573 list_del(&iocb->ki_run_list);
574 iocb->ki_users ++;
575 spin_unlock_irq(&ctx->ctx_lock);
577 kiocbClearKicked(iocb);
578 ret = iocb->ki_retry(iocb);
579 if (-EIOCBQUEUED != ret) {
580 aio_complete(iocb, ret, 0);
581 iocb = NULL;
584 spin_lock_irq(&ctx->ctx_lock);
585 if (NULL != iocb)
586 __aio_put_req(ctx, iocb);
588 spin_unlock_irq(&ctx->ctx_lock);
590 unuse_mm(ctx->mm);
593 void kick_iocb(struct kiocb *iocb)
595 struct kioctx *ctx = iocb->ki_ctx;
597 /* sync iocbs are easy: they can only ever be executing from a
598 * single context. */
599 if (is_sync_kiocb(iocb)) {
600 kiocbSetKicked(iocb);
601 wake_up_process(iocb->ki_user_obj);
602 return;
605 if (!kiocbTryKick(iocb)) {
606 unsigned long flags;
607 spin_lock_irqsave(&ctx->ctx_lock, flags);
608 list_add_tail(&iocb->ki_run_list, &ctx->run_list);
609 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
610 schedule_work(&ctx->wq);
614 /* aio_complete
615 * Called when the io request on the given iocb is complete.
616 * Returns true if this is the last user of the request. The
617 * only other user of the request can be the cancellation code.
619 int aio_complete(struct kiocb *iocb, long res, long res2)
621 struct kioctx *ctx = iocb->ki_ctx;
622 struct aio_ring_info *info;
623 struct aio_ring *ring;
624 struct io_event *event;
625 unsigned long flags;
626 unsigned long tail;
627 int ret;
629 /* Special case handling for sync iocbs: events go directly
630 * into the iocb for fast handling. Note that this will not
631 * work if we allow sync kiocbs to be cancelled. in which
632 * case the usage count checks will have to move under ctx_lock
633 * for all cases.
635 if (is_sync_kiocb(iocb)) {
636 int ret;
638 iocb->ki_user_data = res;
639 if (iocb->ki_users == 1) {
640 iocb->ki_users = 0;
641 return 1;
643 spin_lock_irq(&ctx->ctx_lock);
644 iocb->ki_users--;
645 ret = (0 == iocb->ki_users);
646 spin_unlock_irq(&ctx->ctx_lock);
648 /* sync iocbs put the task here for us */
649 wake_up_process(iocb->ki_user_obj);
650 return ret;
653 info = &ctx->ring_info;
655 /* add a completion event to the ring buffer.
656 * must be done holding ctx->ctx_lock to prevent
657 * other code from messing with the tail
658 * pointer since we might be called from irq
659 * context.
661 spin_lock_irqsave(&ctx->ctx_lock, flags);
663 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
665 tail = info->tail;
666 event = aio_ring_event(info, tail, KM_IRQ0);
667 tail = (tail + 1) % info->nr;
669 event->obj = (u64)(unsigned long)iocb->ki_user_obj;
670 event->data = iocb->ki_user_data;
671 event->res = res;
672 event->res2 = res2;
674 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
675 ctx, tail, iocb, iocb->ki_user_obj, iocb->ki_user_data,
676 res, res2);
678 /* after flagging the request as done, we
679 * must never even look at it again
681 smp_wmb(); /* make event visible before updating tail */
683 info->tail = tail;
684 ring->tail = tail;
686 put_aio_ring_event(event, KM_IRQ0);
687 kunmap_atomic(ring, KM_IRQ1);
689 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
691 /* everything turned out well, dispose of the aiocb. */
692 ret = __aio_put_req(ctx, iocb);
694 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
696 if (waitqueue_active(&ctx->wait))
697 wake_up(&ctx->wait);
699 if (ret)
700 put_ioctx(ctx);
702 return ret;
705 /* aio_read_evt
706 * Pull an event off of the ioctx's event ring. Returns the number of
707 * events fetched (0 or 1 ;-)
708 * FIXME: make this use cmpxchg.
709 * TODO: make the ringbuffer user mmap()able (requires FIXME).
711 static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
713 struct aio_ring_info *info = &ioctx->ring_info;
714 struct aio_ring *ring;
715 unsigned long head;
716 int ret = 0;
718 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
719 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
720 (unsigned long)ring->head, (unsigned long)ring->tail,
721 (unsigned long)ring->nr);
723 if (ring->head == ring->tail)
724 goto out;
726 spin_lock(&info->ring_lock);
728 head = ring->head % info->nr;
729 if (head != ring->tail) {
730 struct io_event *evp = aio_ring_event(info, head, KM_USER1);
731 *ent = *evp;
732 head = (head + 1) % info->nr;
733 smp_mb(); /* finish reading the event before updatng the head */
734 ring->head = head;
735 ret = 1;
736 put_aio_ring_event(evp, KM_USER1);
738 spin_unlock(&info->ring_lock);
740 out:
741 kunmap_atomic(ring, KM_USER0);
742 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
743 (unsigned long)ring->head, (unsigned long)ring->tail);
744 return ret;
747 struct timeout {
748 struct timer_list timer;
749 int timed_out;
750 struct task_struct *p;
753 static void timeout_func(unsigned long data)
755 struct timeout *to = (struct timeout *)data;
757 to->timed_out = 1;
758 wake_up_process(to->p);
761 static inline void init_timeout(struct timeout *to)
763 init_timer(&to->timer);
764 to->timer.data = (unsigned long)to;
765 to->timer.function = timeout_func;
766 to->timed_out = 0;
767 to->p = current;
770 static inline void set_timeout(long start_jiffies, struct timeout *to,
771 const struct timespec *ts)
773 unsigned long how_long;
775 if (ts->tv_sec < 0 || (!ts->tv_sec && !ts->tv_nsec)) {
776 to->timed_out = 1;
777 return;
780 how_long = ts->tv_sec * HZ;
781 #define HZ_NS (1000000000 / HZ)
782 how_long += (ts->tv_nsec + HZ_NS - 1) / HZ_NS;
784 to->timer.expires = jiffies + how_long;
785 add_timer(&to->timer);
788 static inline void clear_timeout(struct timeout *to)
790 del_timer_sync(&to->timer);
793 static int read_events(struct kioctx *ctx,
794 long min_nr, long nr,
795 struct io_event *event,
796 struct timespec *timeout)
798 long start_jiffies = jiffies;
799 struct task_struct *tsk = current;
800 DECLARE_WAITQUEUE(wait, tsk);
801 int ret;
802 int i = 0;
803 struct io_event ent;
804 struct timeout to;
806 /* needed to zero any padding within an entry (there shouldn't be
807 * any, but C is fun!
809 memset(&ent, 0, sizeof(ent));
810 ret = 0;
812 while (likely(i < nr)) {
813 ret = aio_read_evt(ctx, &ent);
814 if (unlikely(ret <= 0))
815 break;
817 dprintk("read event: %Lx %Lx %Lx %Lx\n",
818 ent.data, ent.obj, ent.res, ent.res2);
820 /* Could we split the check in two? */
821 ret = -EFAULT;
822 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
823 dprintk("aio: lost an event due to EFAULT.\n");
824 break;
826 ret = 0;
828 /* Good, event copied to userland, update counts. */
829 event ++;
830 i ++;
833 if (min_nr <= i)
834 return i;
835 if (ret)
836 return ret;
838 /* End fast path */
840 init_timeout(&to);
841 if (timeout) {
842 struct timespec ts;
843 ret = -EFAULT;
844 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
845 goto out;
847 set_timeout(start_jiffies, &to, &ts);
850 while (likely(i < nr)) {
851 add_wait_queue_exclusive(&ctx->wait, &wait);
852 do {
853 set_task_state(tsk, TASK_INTERRUPTIBLE);
855 ret = aio_read_evt(ctx, &ent);
856 if (ret)
857 break;
858 if (min_nr <= i)
859 break;
860 ret = 0;
861 if (to.timed_out) /* Only check after read evt */
862 break;
863 schedule();
864 if (signal_pending(tsk)) {
865 ret = -EINTR;
866 break;
868 /*ret = aio_read_evt(ctx, &ent);*/
869 } while (1) ;
871 set_task_state(tsk, TASK_RUNNING);
872 remove_wait_queue(&ctx->wait, &wait);
874 if (unlikely(ret <= 0))
875 break;
877 ret = -EFAULT;
878 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
879 dprintk("aio: lost an event due to EFAULT.\n");
880 break;
883 /* Good, event copied to userland, update counts. */
884 event ++;
885 i ++;
888 if (timeout)
889 clear_timeout(&to);
890 out:
891 return i ? i : ret;
894 /* Take an ioctx and remove it from the list of ioctx's. Protects
895 * against races with itself via ->dead.
897 static void io_destroy(struct kioctx *ioctx)
899 struct mm_struct *mm = current->mm;
900 struct kioctx **tmp;
901 int was_dead;
903 /* delete the entry from the list is someone else hasn't already */
904 write_lock(&mm->ioctx_list_lock);
905 was_dead = ioctx->dead;
906 ioctx->dead = 1;
907 for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
908 tmp = &(*tmp)->next)
910 if (*tmp)
911 *tmp = ioctx->next;
912 write_unlock(&mm->ioctx_list_lock);
914 dprintk("aio_release(%p)\n", ioctx);
915 if (likely(!was_dead))
916 put_ioctx(ioctx); /* twice for the list */
918 aio_cancel_all(ioctx);
919 wait_for_all_aios(ioctx);
920 put_ioctx(ioctx); /* once for the lookup */
923 /* sys_io_setup:
924 * Create an aio_context capable of receiving at least nr_events.
925 * ctxp must not point to an aio_context that already exists, and
926 * must be initialized to 0 prior to the call. On successful
927 * creation of the aio_context, *ctxp is filled in with the resulting
928 * handle. May fail with -EINVAL if *ctxp is not initialized,
929 * if the specified nr_events exceeds internal limits. May fail
930 * with -EAGAIN if the specified nr_events exceeds the user's limit
931 * of available events. May fail with -ENOMEM if insufficient kernel
932 * resources are available. May fail with -EFAULT if an invalid
933 * pointer is passed for ctxp. Will fail with -ENOSYS if not
934 * implemented.
936 asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t *ctxp)
938 struct kioctx *ioctx = NULL;
939 unsigned long ctx;
940 long ret;
942 ret = get_user(ctx, ctxp);
943 if (unlikely(ret))
944 goto out;
946 ret = -EINVAL;
947 if (unlikely(ctx || (int)nr_events <= 0)) {
948 pr_debug("EINVAL: io_setup: ctx or nr_events > max\n");
949 goto out;
952 ioctx = ioctx_alloc(nr_events);
953 ret = PTR_ERR(ioctx);
954 if (!IS_ERR(ioctx)) {
955 ret = put_user(ioctx->user_id, ctxp);
956 if (!ret)
957 return 0;
958 io_destroy(ioctx);
961 out:
962 return ret;
965 /* sys_io_destroy:
966 * Destroy the aio_context specified. May cancel any outstanding
967 * AIOs and block on completion. Will fail with -ENOSYS if not
968 * implemented. May fail with -EFAULT if the context pointed to
969 * is invalid.
971 asmlinkage long sys_io_destroy(aio_context_t ctx)
973 struct kioctx *ioctx = lookup_ioctx(ctx);
974 if (likely(NULL != ioctx)) {
975 io_destroy(ioctx);
976 return 0;
978 pr_debug("EINVAL: io_destroy: invalid context id\n");
979 return -EINVAL;
982 int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
983 struct iocb *iocb)
985 struct kiocb *req;
986 struct file *file;
987 ssize_t ret;
988 char *buf;
990 /* enforce forwards compatibility on users */
991 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2 ||
992 iocb->aio_reserved3)) {
993 pr_debug("EINVAL: io_submit: reserve field set\n");
994 return -EINVAL;
997 /* prevent overflows */
998 if (unlikely(
999 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1000 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1001 ((ssize_t)iocb->aio_nbytes < 0)
1002 )) {
1003 pr_debug("EINVAL: io_submit: overflow check\n");
1004 return -EINVAL;
1007 file = fget(iocb->aio_fildes);
1008 if (unlikely(!file))
1009 return -EBADF;
1011 req = aio_get_req(ctx);
1012 if (unlikely(!req)) {
1013 fput(file);
1014 return -EAGAIN;
1017 req->ki_filp = file;
1018 iocb->aio_key = req->ki_key;
1019 ret = put_user(iocb->aio_key, &user_iocb->aio_key);
1020 if (unlikely(ret)) {
1021 dprintk("EFAULT: aio_key\n");
1022 goto out_put_req;
1025 req->ki_user_obj = user_iocb;
1026 req->ki_user_data = iocb->aio_data;
1027 req->ki_pos = iocb->aio_offset;
1029 buf = (char *)(unsigned long)iocb->aio_buf;
1031 switch (iocb->aio_lio_opcode) {
1032 case IOCB_CMD_PREAD:
1033 ret = -EBADF;
1034 if (unlikely(!(file->f_mode & FMODE_READ)))
1035 goto out_put_req;
1036 ret = -EFAULT;
1037 if (unlikely(!access_ok(VERIFY_WRITE, buf, iocb->aio_nbytes)))
1038 goto out_put_req;
1039 ret = -EINVAL;
1040 if (file->f_op->aio_read)
1041 ret = file->f_op->aio_read(req, buf,
1042 iocb->aio_nbytes, req->ki_pos);
1043 break;
1044 case IOCB_CMD_PWRITE:
1045 ret = -EBADF;
1046 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1047 goto out_put_req;
1048 ret = -EFAULT;
1049 if (unlikely(!access_ok(VERIFY_READ, buf, iocb->aio_nbytes)))
1050 goto out_put_req;
1051 ret = -EINVAL;
1052 if (file->f_op->aio_write)
1053 ret = file->f_op->aio_write(req, buf,
1054 iocb->aio_nbytes, req->ki_pos);
1055 break;
1056 case IOCB_CMD_FDSYNC:
1057 ret = -EINVAL;
1058 if (file->f_op->aio_fsync)
1059 ret = file->f_op->aio_fsync(req, 1);
1060 break;
1061 case IOCB_CMD_FSYNC:
1062 ret = -EINVAL;
1063 if (file->f_op->aio_fsync)
1064 ret = file->f_op->aio_fsync(req, 0);
1065 break;
1066 default:
1067 dprintk("EINVAL: io_submit: no operation provided\n");
1068 ret = -EINVAL;
1071 if (likely(-EIOCBQUEUED == ret))
1072 return 0;
1073 aio_complete(req, ret, 0);
1074 return 0;
1076 out_put_req:
1077 aio_put_req(req);
1078 return ret;
1081 /* sys_io_submit:
1082 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1083 * the number of iocbs queued. May return -EINVAL if the aio_context
1084 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1085 * *iocbpp[0] is not properly initialized, if the operation specified
1086 * is invalid for the file descriptor in the iocb. May fail with
1087 * -EFAULT if any of the data structures point to invalid data. May
1088 * fail with -EBADF if the file descriptor specified in the first
1089 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1090 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1091 * fail with -ENOSYS if not implemented.
1093 asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1094 struct iocb __user **iocbpp)
1096 struct kioctx *ctx;
1097 long ret = 0;
1098 int i;
1100 if (unlikely(nr < 0))
1101 return -EINVAL;
1103 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1104 return -EFAULT;
1106 ctx = lookup_ioctx(ctx_id);
1107 if (unlikely(!ctx)) {
1108 pr_debug("EINVAL: io_submit: invalid context id\n");
1109 return -EINVAL;
1113 * AKPM: should this return a partial result if some of the IOs were
1114 * successfully submitted?
1116 for (i=0; i<nr; i++) {
1117 struct iocb __user *user_iocb;
1118 struct iocb tmp;
1120 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1121 ret = -EFAULT;
1122 break;
1125 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1126 ret = -EFAULT;
1127 break;
1130 ret = io_submit_one(ctx, user_iocb, &tmp);
1131 if (ret)
1132 break;
1135 put_ioctx(ctx);
1136 return i ? i : ret;
1139 /* lookup_kiocb
1140 * Finds a given iocb for cancellation.
1141 * MUST be called with ctx->ctx_lock held.
1143 struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb *iocb, u32 key)
1145 struct list_head *pos;
1146 /* TODO: use a hash or array, this sucks. */
1147 list_for_each(pos, &ctx->active_reqs) {
1148 struct kiocb *kiocb = list_kiocb(pos);
1149 if (kiocb->ki_user_obj == iocb && kiocb->ki_key == key)
1150 return kiocb;
1152 return NULL;
1155 /* sys_io_cancel:
1156 * Attempts to cancel an iocb previously passed to io_submit. If
1157 * the operation is successfully cancelled, the resulting event is
1158 * copied into the memory pointed to by result without being placed
1159 * into the completion queue and 0 is returned. May fail with
1160 * -EFAULT if any of the data structures pointed to are invalid.
1161 * May fail with -EINVAL if aio_context specified by ctx_id is
1162 * invalid. May fail with -EAGAIN if the iocb specified was not
1163 * cancelled. Will fail with -ENOSYS if not implemented.
1165 asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb *iocb,
1166 struct io_event *result)
1168 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1169 struct kioctx *ctx;
1170 struct kiocb *kiocb;
1171 u32 key;
1172 int ret;
1174 ret = get_user(key, &iocb->aio_key);
1175 if (unlikely(ret))
1176 return -EFAULT;
1178 ctx = lookup_ioctx(ctx_id);
1179 if (unlikely(!ctx))
1180 return -EINVAL;
1182 spin_lock_irq(&ctx->ctx_lock);
1183 ret = -EAGAIN;
1184 kiocb = lookup_kiocb(ctx, iocb, key);
1185 if (kiocb && kiocb->ki_cancel) {
1186 cancel = kiocb->ki_cancel;
1187 kiocb->ki_users ++;
1188 } else
1189 cancel = NULL;
1190 spin_unlock_irq(&ctx->ctx_lock);
1192 if (NULL != cancel) {
1193 struct io_event tmp;
1194 pr_debug("calling cancel\n");
1195 memset(&tmp, 0, sizeof(tmp));
1196 tmp.obj = (u64)(unsigned long)kiocb->ki_user_obj;
1197 tmp.data = kiocb->ki_user_data;
1198 ret = cancel(kiocb, &tmp);
1199 if (!ret) {
1200 /* Cancellation succeeded -- copy the result
1201 * into the user's buffer.
1203 if (copy_to_user(result, &tmp, sizeof(tmp)))
1204 ret = -EFAULT;
1206 } else
1207 printk(KERN_DEBUG "iocb has no cancel operation\n");
1209 put_ioctx(ctx);
1211 return ret;
1214 /* io_getevents:
1215 * Attempts to read at least min_nr events and up to nr events from
1216 * the completion queue for the aio_context specified by ctx_id. May
1217 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1218 * if nr is out of range, if when is out of range. May fail with
1219 * -EFAULT if any of the memory specified to is invalid. May return
1220 * 0 or < min_nr if no events are available and the timeout specified
1221 * by when has elapsed, where when == NULL specifies an infinite
1222 * timeout. Note that the timeout pointed to by when is relative and
1223 * will be updated if not NULL and the operation blocks. Will fail
1224 * with -ENOSYS if not implemented.
1226 asmlinkage long sys_io_getevents(aio_context_t ctx_id,
1227 long min_nr,
1228 long nr,
1229 struct io_event *events,
1230 struct timespec *timeout)
1232 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1233 long ret = -EINVAL;
1235 if (unlikely(min_nr > nr || min_nr < 0 || nr < 0))
1236 return ret;
1238 if (likely(NULL != ioctx)) {
1239 ret = read_events(ioctx, min_nr, nr, events, timeout);
1240 put_ioctx(ioctx);
1243 return ret;
1246 __initcall(aio_setup);
1248 EXPORT_SYMBOL(aio_complete);
1249 EXPORT_SYMBOL(aio_put_req);
1250 EXPORT_SYMBOL(wait_on_sync_kiocb);