[MIPS] Use platform_device for Cobalt UART
[linux-2.6/linux-2.6-openrd.git] / fs / aio.c
blobac1c1587aa02dad4583b7a394cf1261e79a0c680
1 /*
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>
20 #define DEBUG 0
22 #include <linux/sched.h>
23 #include <linux/fs.h>
24 #include <linux/file.h>
25 #include <linux/mm.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>
38 #if DEBUG > 1
39 #define dprintk printk
40 #else
41 #define dprintk(x...) do { ; } while (0)
42 #endif
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 *);
65 /* aio_setup
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));
78 return 0;
81 static void aio_free_ring(struct kioctx *ctx)
83 struct aio_ring_info *info = &ctx->ring_info;
84 long i;
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;
98 info->nr = 0;
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;
106 unsigned long size;
107 int nr_pages;
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;
116 if (nr_pages < 0)
117 return -EINVAL;
119 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
121 info->nr = 0;
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)
126 return -ENOMEM;
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);
137 info->mmap_size = 0;
138 aio_free_ring(ctx);
139 return -EAGAIN;
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)) {
149 aio_free_ring(ctx);
150 return -EAGAIN;
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);
167 return 0;
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; \
184 __event; \
187 #define put_aio_ring_event(event, km) do { \
188 struct io_event *__event = (event); \
189 (void)__event; \
190 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
191 } while(0)
193 /* ioctx_alloc
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;
199 struct kioctx *ctx;
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);
212 if (!ctx)
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)
229 goto out_freectx;
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)
235 ctx->max_reqs = 0;
236 else
237 aio_nr += ctx->max_reqs;
238 spin_unlock(&aio_nr_lock);
239 if (ctx->max_reqs == 0)
240 goto out_cleanup;
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);
250 return ctx;
252 out_cleanup:
253 __put_ioctx(ctx);
254 return ERR_PTR(-EAGAIN);
256 out_freectx:
257 mmdrop(mm);
258 kmem_cache_free(kioctx_cachep, ctx);
259 ctx = ERR_PTR(-ENOMEM);
261 dprintk("aio: error allocating ioctx %p\n", ctx);
262 return ctx;
265 /* aio_cancel_all
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 *);
273 struct io_event res;
274 spin_lock_irq(&ctx->ctx_lock);
275 ctx->dead = 1;
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);
282 if (cancel) {
283 iocb->ki_users++;
284 spin_unlock_irq(&ctx->ctx_lock);
285 cancel(iocb, &res);
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)
299 goto out;
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);
305 schedule();
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);
312 out:
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);
323 if (!iocb->ki_users)
324 break;
325 schedule();
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;
342 while (ctx) {
343 struct kioctx *next = ctx->next;
344 ctx->next = NULL;
345 aio_cancel_all(ctx);
347 wait_for_all_aios(ctx);
349 * Ensure we don't leave the ctx on the aio_wq
351 cancel_work_sync(&ctx->wq.work);
353 if (1 != atomic_read(&ctx->users))
354 printk(KERN_DEBUG
355 "exit_aio:ioctx still alive: %d %d %d\n",
356 atomic_read(&ctx->users), ctx->dead,
357 ctx->reqs_active);
358 put_ioctx(ctx);
359 ctx = next;
363 /* __put_ioctx
364 * Called when the last user of an aio context has gone away,
365 * and the struct needs to be freed.
367 void fastcall __put_ioctx(struct kioctx *ctx)
369 unsigned nr_events = ctx->max_reqs;
371 BUG_ON(ctx->reqs_active);
373 cancel_delayed_work(&ctx->wq);
374 cancel_work_sync(&ctx->wq.work);
375 aio_free_ring(ctx);
376 mmdrop(ctx->mm);
377 ctx->mm = NULL;
378 pr_debug("__put_ioctx: freeing %p\n", ctx);
379 kmem_cache_free(kioctx_cachep, ctx);
381 if (nr_events) {
382 spin_lock(&aio_nr_lock);
383 BUG_ON(aio_nr - nr_events > aio_nr);
384 aio_nr -= nr_events;
385 spin_unlock(&aio_nr_lock);
389 /* aio_get_req
390 * Allocate a slot for an aio request. Increments the users count
391 * of the kioctx so that the kioctx stays around until all requests are
392 * complete. Returns NULL if no requests are free.
394 * Returns with kiocb->users set to 2. The io submit code path holds
395 * an extra reference while submitting the i/o.
396 * This prevents races between the aio code path referencing the
397 * req (after submitting it) and aio_complete() freeing the req.
399 static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
400 static struct kiocb fastcall *__aio_get_req(struct kioctx *ctx)
402 struct kiocb *req = NULL;
403 struct aio_ring *ring;
404 int okay = 0;
406 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
407 if (unlikely(!req))
408 return NULL;
410 req->ki_flags = 0;
411 req->ki_users = 2;
412 req->ki_key = 0;
413 req->ki_ctx = ctx;
414 req->ki_cancel = NULL;
415 req->ki_retry = NULL;
416 req->ki_dtor = NULL;
417 req->private = NULL;
418 req->ki_iovec = NULL;
419 INIT_LIST_HEAD(&req->ki_run_list);
421 /* Check if the completion queue has enough free space to
422 * accept an event from this io.
424 spin_lock_irq(&ctx->ctx_lock);
425 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
426 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
427 list_add(&req->ki_list, &ctx->active_reqs);
428 ctx->reqs_active++;
429 okay = 1;
431 kunmap_atomic(ring, KM_USER0);
432 spin_unlock_irq(&ctx->ctx_lock);
434 if (!okay) {
435 kmem_cache_free(kiocb_cachep, req);
436 req = NULL;
439 return req;
442 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
444 struct kiocb *req;
445 /* Handle a potential starvation case -- should be exceedingly rare as
446 * requests will be stuck on fput_head only if the aio_fput_routine is
447 * delayed and the requests were the last user of the struct file.
449 req = __aio_get_req(ctx);
450 if (unlikely(NULL == req)) {
451 aio_fput_routine(NULL);
452 req = __aio_get_req(ctx);
454 return req;
457 static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
459 assert_spin_locked(&ctx->ctx_lock);
461 if (req->ki_dtor)
462 req->ki_dtor(req);
463 if (req->ki_iovec != &req->ki_inline_vec)
464 kfree(req->ki_iovec);
465 kmem_cache_free(kiocb_cachep, req);
466 ctx->reqs_active--;
468 if (unlikely(!ctx->reqs_active && ctx->dead))
469 wake_up(&ctx->wait);
472 static void aio_fput_routine(struct work_struct *data)
474 spin_lock_irq(&fput_lock);
475 while (likely(!list_empty(&fput_head))) {
476 struct kiocb *req = list_kiocb(fput_head.next);
477 struct kioctx *ctx = req->ki_ctx;
479 list_del(&req->ki_list);
480 spin_unlock_irq(&fput_lock);
482 /* Complete the fput */
483 __fput(req->ki_filp);
485 /* Link the iocb into the context's free list */
486 spin_lock_irq(&ctx->ctx_lock);
487 really_put_req(ctx, req);
488 spin_unlock_irq(&ctx->ctx_lock);
490 put_ioctx(ctx);
491 spin_lock_irq(&fput_lock);
493 spin_unlock_irq(&fput_lock);
496 /* __aio_put_req
497 * Returns true if this put was the last user of the request.
499 static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
501 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
502 req, atomic_read(&req->ki_filp->f_count));
504 assert_spin_locked(&ctx->ctx_lock);
506 req->ki_users --;
507 BUG_ON(req->ki_users < 0);
508 if (likely(req->ki_users))
509 return 0;
510 list_del(&req->ki_list); /* remove from active_reqs */
511 req->ki_cancel = NULL;
512 req->ki_retry = NULL;
514 /* Must be done under the lock to serialise against cancellation.
515 * Call this aio_fput as it duplicates fput via the fput_work.
517 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
518 get_ioctx(ctx);
519 spin_lock(&fput_lock);
520 list_add(&req->ki_list, &fput_head);
521 spin_unlock(&fput_lock);
522 queue_work(aio_wq, &fput_work);
523 } else
524 really_put_req(ctx, req);
525 return 1;
528 /* aio_put_req
529 * Returns true if this put was the last user of the kiocb,
530 * false if the request is still in use.
532 int fastcall aio_put_req(struct kiocb *req)
534 struct kioctx *ctx = req->ki_ctx;
535 int ret;
536 spin_lock_irq(&ctx->ctx_lock);
537 ret = __aio_put_req(ctx, req);
538 spin_unlock_irq(&ctx->ctx_lock);
539 return ret;
542 /* Lookup an ioctx id. ioctx_list is lockless for reads.
543 * FIXME: this is O(n) and is only suitable for development.
545 struct kioctx *lookup_ioctx(unsigned long ctx_id)
547 struct kioctx *ioctx;
548 struct mm_struct *mm;
550 mm = current->mm;
551 read_lock(&mm->ioctx_list_lock);
552 for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
553 if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
554 get_ioctx(ioctx);
555 break;
557 read_unlock(&mm->ioctx_list_lock);
559 return ioctx;
563 * use_mm
564 * Makes the calling kernel thread take on the specified
565 * mm context.
566 * Called by the retry thread execute retries within the
567 * iocb issuer's mm context, so that copy_from/to_user
568 * operations work seamlessly for aio.
569 * (Note: this routine is intended to be called only
570 * from a kernel thread context)
572 static void use_mm(struct mm_struct *mm)
574 struct mm_struct *active_mm;
575 struct task_struct *tsk = current;
577 task_lock(tsk);
578 tsk->flags |= PF_BORROWED_MM;
579 active_mm = tsk->active_mm;
580 atomic_inc(&mm->mm_count);
581 tsk->mm = mm;
582 tsk->active_mm = mm;
584 * Note that on UML this *requires* PF_BORROWED_MM to be set, otherwise
585 * it won't work. Update it accordingly if you change it here
587 switch_mm(active_mm, mm, tsk);
588 task_unlock(tsk);
590 mmdrop(active_mm);
594 * unuse_mm
595 * Reverses the effect of use_mm, i.e. releases the
596 * specified mm context which was earlier taken on
597 * by the calling kernel thread
598 * (Note: this routine is intended to be called only
599 * from a kernel thread context)
601 static void unuse_mm(struct mm_struct *mm)
603 struct task_struct *tsk = current;
605 task_lock(tsk);
606 tsk->flags &= ~PF_BORROWED_MM;
607 tsk->mm = NULL;
608 /* active_mm is still 'mm' */
609 enter_lazy_tlb(mm, tsk);
610 task_unlock(tsk);
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
620 * already queued.
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,
630 &ctx->run_list);
631 return 1;
633 return 0;
636 /* aio_run_iocb
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 *);
662 ssize_t ret;
664 if (!(retry = iocb->ki_retry)) {
665 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
666 return 0;
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
677 * this one.
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
685 * it up afresh.
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)) {
701 ret = -EINTR;
702 aio_complete(iocb, ret, 0);
703 /* must not access the iocb after this */
704 goto out;
708 * Now we are all set to call the retry method in async
709 * context. By setting this thread's io_wait context
710 * to point to the wait queue entry inside the currently
711 * running iocb for the duration of the retry, we ensure
712 * that async notification wakeups are queued by the
713 * operation instead of blocking waits, and when notified,
714 * cause the iocb to be kicked for continuation (through
715 * the aio_wake_function callback).
717 BUG_ON(current->io_wait != NULL);
718 current->io_wait = &iocb->ki_wait;
719 ret = retry(iocb);
720 current->io_wait = NULL;
722 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
723 BUG_ON(!list_empty(&iocb->ki_wait.task_list));
724 aio_complete(iocb, ret, 0);
726 out:
727 spin_lock_irq(&ctx->ctx_lock);
729 if (-EIOCBRETRY == ret) {
731 * OK, now that we are done with this iteration
732 * and know that there is more left to go,
733 * this is where we let go so that a subsequent
734 * "kick" can start the next iteration
737 /* will make __queue_kicked_iocb succeed from here on */
738 INIT_LIST_HEAD(&iocb->ki_run_list);
739 /* we must queue the next iteration ourselves, if it
740 * has already been kicked */
741 if (kiocbIsKicked(iocb)) {
742 __queue_kicked_iocb(iocb);
745 * __queue_kicked_iocb will always return 1 here, because
746 * iocb->ki_run_list is empty at this point so it should
747 * be safe to unconditionally queue the context into the
748 * work queue.
750 aio_queue_work(ctx);
753 return ret;
757 * __aio_run_iocbs:
758 * Process all pending retries queued on the ioctx
759 * run list.
760 * Assumes it is operating within the aio issuer's mm
761 * context.
763 static int __aio_run_iocbs(struct kioctx *ctx)
765 struct kiocb *iocb;
766 struct list_head run_list;
768 assert_spin_locked(&ctx->ctx_lock);
770 list_replace_init(&ctx->run_list, &run_list);
771 while (!list_empty(&run_list)) {
772 iocb = list_entry(run_list.next, struct kiocb,
773 ki_run_list);
774 list_del(&iocb->ki_run_list);
776 * Hold an extra reference while retrying i/o.
778 iocb->ki_users++; /* grab extra reference */
779 aio_run_iocb(iocb);
780 __aio_put_req(ctx, iocb);
782 if (!list_empty(&ctx->run_list))
783 return 1;
784 return 0;
787 static void aio_queue_work(struct kioctx * ctx)
789 unsigned long timeout;
791 * if someone is waiting, get the work started right
792 * away, otherwise, use a longer delay
794 smp_mb();
795 if (waitqueue_active(&ctx->wait))
796 timeout = 1;
797 else
798 timeout = HZ/10;
799 queue_delayed_work(aio_wq, &ctx->wq, timeout);
804 * aio_run_iocbs:
805 * Process all pending retries queued on the ioctx
806 * run list.
807 * Assumes it is operating within the aio issuer's mm
808 * context.
810 static inline void aio_run_iocbs(struct kioctx *ctx)
812 int requeue;
814 spin_lock_irq(&ctx->ctx_lock);
816 requeue = __aio_run_iocbs(ctx);
817 spin_unlock_irq(&ctx->ctx_lock);
818 if (requeue)
819 aio_queue_work(ctx);
823 * just like aio_run_iocbs, but keeps running them until
824 * the list stays empty
826 static inline void aio_run_all_iocbs(struct kioctx *ctx)
828 spin_lock_irq(&ctx->ctx_lock);
829 while (__aio_run_iocbs(ctx))
831 spin_unlock_irq(&ctx->ctx_lock);
835 * aio_kick_handler:
836 * Work queue handler triggered to process pending
837 * retries on an ioctx. Takes on the aio issuer's
838 * mm context before running the iocbs, so that
839 * copy_xxx_user operates on the issuer's address
840 * space.
841 * Run on aiod's context.
843 static void aio_kick_handler(struct work_struct *work)
845 struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
846 mm_segment_t oldfs = get_fs();
847 struct mm_struct *mm;
848 int requeue;
850 set_fs(USER_DS);
851 use_mm(ctx->mm);
852 spin_lock_irq(&ctx->ctx_lock);
853 requeue =__aio_run_iocbs(ctx);
854 mm = ctx->mm;
855 spin_unlock_irq(&ctx->ctx_lock);
856 unuse_mm(mm);
857 set_fs(oldfs);
859 * we're in a worker thread already, don't use queue_delayed_work,
861 if (requeue)
862 queue_delayed_work(aio_wq, &ctx->wq, 0);
867 * Called by kick_iocb to queue the kiocb for retry
868 * and if required activate the aio work queue to process
869 * it
871 static void try_queue_kicked_iocb(struct kiocb *iocb)
873 struct kioctx *ctx = iocb->ki_ctx;
874 unsigned long flags;
875 int run = 0;
877 /* We're supposed to be the only path putting the iocb back on the run
878 * list. If we find that the iocb is *back* on a wait queue already
879 * than retry has happened before we could queue the iocb. This also
880 * means that the retry could have completed and freed our iocb, no
881 * good. */
882 BUG_ON((!list_empty(&iocb->ki_wait.task_list)));
884 spin_lock_irqsave(&ctx->ctx_lock, flags);
885 /* set this inside the lock so that we can't race with aio_run_iocb()
886 * testing it and putting the iocb on the run list under the lock */
887 if (!kiocbTryKick(iocb))
888 run = __queue_kicked_iocb(iocb);
889 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
890 if (run)
891 aio_queue_work(ctx);
895 * kick_iocb:
896 * Called typically from a wait queue callback context
897 * (aio_wake_function) to trigger a retry of the iocb.
898 * The retry is usually executed by aio workqueue
899 * threads (See aio_kick_handler).
901 void fastcall kick_iocb(struct kiocb *iocb)
903 /* sync iocbs are easy: they can only ever be executing from a
904 * single context. */
905 if (is_sync_kiocb(iocb)) {
906 kiocbSetKicked(iocb);
907 wake_up_process(iocb->ki_obj.tsk);
908 return;
911 try_queue_kicked_iocb(iocb);
913 EXPORT_SYMBOL(kick_iocb);
915 /* aio_complete
916 * Called when the io request on the given iocb is complete.
917 * Returns true if this is the last user of the request. The
918 * only other user of the request can be the cancellation code.
920 int fastcall aio_complete(struct kiocb *iocb, long res, long res2)
922 struct kioctx *ctx = iocb->ki_ctx;
923 struct aio_ring_info *info;
924 struct aio_ring *ring;
925 struct io_event *event;
926 unsigned long flags;
927 unsigned long tail;
928 int ret;
931 * Special case handling for sync iocbs:
932 * - events go directly into the iocb for fast handling
933 * - the sync task with the iocb in its stack holds the single iocb
934 * ref, no other paths have a way to get another ref
935 * - the sync task helpfully left a reference to itself in the iocb
937 if (is_sync_kiocb(iocb)) {
938 BUG_ON(iocb->ki_users != 1);
939 iocb->ki_user_data = res;
940 iocb->ki_users = 0;
941 wake_up_process(iocb->ki_obj.tsk);
942 return 1;
945 info = &ctx->ring_info;
947 /* add a completion event to the ring buffer.
948 * must be done holding ctx->ctx_lock to prevent
949 * other code from messing with the tail
950 * pointer since we might be called from irq
951 * context.
953 spin_lock_irqsave(&ctx->ctx_lock, flags);
955 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
956 list_del_init(&iocb->ki_run_list);
959 * cancelled requests don't get events, userland was given one
960 * when the event got cancelled.
962 if (kiocbIsCancelled(iocb))
963 goto put_rq;
965 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
967 tail = info->tail;
968 event = aio_ring_event(info, tail, KM_IRQ0);
969 if (++tail >= info->nr)
970 tail = 0;
972 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
973 event->data = iocb->ki_user_data;
974 event->res = res;
975 event->res2 = res2;
977 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
978 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
979 res, res2);
981 /* after flagging the request as done, we
982 * must never even look at it again
984 smp_wmb(); /* make event visible before updating tail */
986 info->tail = tail;
987 ring->tail = tail;
989 put_aio_ring_event(event, KM_IRQ0);
990 kunmap_atomic(ring, KM_IRQ1);
992 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
993 put_rq:
994 /* everything turned out well, dispose of the aiocb. */
995 ret = __aio_put_req(ctx, iocb);
997 if (waitqueue_active(&ctx->wait))
998 wake_up(&ctx->wait);
1000 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
1001 return ret;
1004 /* aio_read_evt
1005 * Pull an event off of the ioctx's event ring. Returns the number of
1006 * events fetched (0 or 1 ;-)
1007 * FIXME: make this use cmpxchg.
1008 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1010 static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1012 struct aio_ring_info *info = &ioctx->ring_info;
1013 struct aio_ring *ring;
1014 unsigned long head;
1015 int ret = 0;
1017 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
1018 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1019 (unsigned long)ring->head, (unsigned long)ring->tail,
1020 (unsigned long)ring->nr);
1022 if (ring->head == ring->tail)
1023 goto out;
1025 spin_lock(&info->ring_lock);
1027 head = ring->head % info->nr;
1028 if (head != ring->tail) {
1029 struct io_event *evp = aio_ring_event(info, head, KM_USER1);
1030 *ent = *evp;
1031 head = (head + 1) % info->nr;
1032 smp_mb(); /* finish reading the event before updatng the head */
1033 ring->head = head;
1034 ret = 1;
1035 put_aio_ring_event(evp, KM_USER1);
1037 spin_unlock(&info->ring_lock);
1039 out:
1040 kunmap_atomic(ring, KM_USER0);
1041 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
1042 (unsigned long)ring->head, (unsigned long)ring->tail);
1043 return ret;
1046 struct aio_timeout {
1047 struct timer_list timer;
1048 int timed_out;
1049 struct task_struct *p;
1052 static void timeout_func(unsigned long data)
1054 struct aio_timeout *to = (struct aio_timeout *)data;
1056 to->timed_out = 1;
1057 wake_up_process(to->p);
1060 static inline void init_timeout(struct aio_timeout *to)
1062 init_timer(&to->timer);
1063 to->timer.data = (unsigned long)to;
1064 to->timer.function = timeout_func;
1065 to->timed_out = 0;
1066 to->p = current;
1069 static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1070 const struct timespec *ts)
1072 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
1073 if (time_after(to->timer.expires, jiffies))
1074 add_timer(&to->timer);
1075 else
1076 to->timed_out = 1;
1079 static inline void clear_timeout(struct aio_timeout *to)
1081 del_singleshot_timer_sync(&to->timer);
1084 static int read_events(struct kioctx *ctx,
1085 long min_nr, long nr,
1086 struct io_event __user *event,
1087 struct timespec __user *timeout)
1089 long start_jiffies = jiffies;
1090 struct task_struct *tsk = current;
1091 DECLARE_WAITQUEUE(wait, tsk);
1092 int ret;
1093 int i = 0;
1094 struct io_event ent;
1095 struct aio_timeout to;
1096 int retry = 0;
1098 /* needed to zero any padding within an entry (there shouldn't be
1099 * any, but C is fun!
1101 memset(&ent, 0, sizeof(ent));
1102 retry:
1103 ret = 0;
1104 while (likely(i < nr)) {
1105 ret = aio_read_evt(ctx, &ent);
1106 if (unlikely(ret <= 0))
1107 break;
1109 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1110 ent.data, ent.obj, ent.res, ent.res2);
1112 /* Could we split the check in two? */
1113 ret = -EFAULT;
1114 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1115 dprintk("aio: lost an event due to EFAULT.\n");
1116 break;
1118 ret = 0;
1120 /* Good, event copied to userland, update counts. */
1121 event ++;
1122 i ++;
1125 if (min_nr <= i)
1126 return i;
1127 if (ret)
1128 return ret;
1130 /* End fast path */
1132 /* racey check, but it gets redone */
1133 if (!retry && unlikely(!list_empty(&ctx->run_list))) {
1134 retry = 1;
1135 aio_run_all_iocbs(ctx);
1136 goto retry;
1139 init_timeout(&to);
1140 if (timeout) {
1141 struct timespec ts;
1142 ret = -EFAULT;
1143 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
1144 goto out;
1146 set_timeout(start_jiffies, &to, &ts);
1149 while (likely(i < nr)) {
1150 add_wait_queue_exclusive(&ctx->wait, &wait);
1151 do {
1152 set_task_state(tsk, TASK_INTERRUPTIBLE);
1153 ret = aio_read_evt(ctx, &ent);
1154 if (ret)
1155 break;
1156 if (min_nr <= i)
1157 break;
1158 ret = 0;
1159 if (to.timed_out) /* Only check after read evt */
1160 break;
1161 schedule();
1162 if (signal_pending(tsk)) {
1163 ret = -EINTR;
1164 break;
1166 /*ret = aio_read_evt(ctx, &ent);*/
1167 } while (1) ;
1169 set_task_state(tsk, TASK_RUNNING);
1170 remove_wait_queue(&ctx->wait, &wait);
1172 if (unlikely(ret <= 0))
1173 break;
1175 ret = -EFAULT;
1176 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1177 dprintk("aio: lost an event due to EFAULT.\n");
1178 break;
1181 /* Good, event copied to userland, update counts. */
1182 event ++;
1183 i ++;
1186 if (timeout)
1187 clear_timeout(&to);
1188 out:
1189 return i ? i : ret;
1192 /* Take an ioctx and remove it from the list of ioctx's. Protects
1193 * against races with itself via ->dead.
1195 static void io_destroy(struct kioctx *ioctx)
1197 struct mm_struct *mm = current->mm;
1198 struct kioctx **tmp;
1199 int was_dead;
1201 /* delete the entry from the list is someone else hasn't already */
1202 write_lock(&mm->ioctx_list_lock);
1203 was_dead = ioctx->dead;
1204 ioctx->dead = 1;
1205 for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
1206 tmp = &(*tmp)->next)
1208 if (*tmp)
1209 *tmp = ioctx->next;
1210 write_unlock(&mm->ioctx_list_lock);
1212 dprintk("aio_release(%p)\n", ioctx);
1213 if (likely(!was_dead))
1214 put_ioctx(ioctx); /* twice for the list */
1216 aio_cancel_all(ioctx);
1217 wait_for_all_aios(ioctx);
1218 put_ioctx(ioctx); /* once for the lookup */
1221 /* sys_io_setup:
1222 * Create an aio_context capable of receiving at least nr_events.
1223 * ctxp must not point to an aio_context that already exists, and
1224 * must be initialized to 0 prior to the call. On successful
1225 * creation of the aio_context, *ctxp is filled in with the resulting
1226 * handle. May fail with -EINVAL if *ctxp is not initialized,
1227 * if the specified nr_events exceeds internal limits. May fail
1228 * with -EAGAIN if the specified nr_events exceeds the user's limit
1229 * of available events. May fail with -ENOMEM if insufficient kernel
1230 * resources are available. May fail with -EFAULT if an invalid
1231 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1232 * implemented.
1234 asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
1236 struct kioctx *ioctx = NULL;
1237 unsigned long ctx;
1238 long ret;
1240 ret = get_user(ctx, ctxp);
1241 if (unlikely(ret))
1242 goto out;
1244 ret = -EINVAL;
1245 if (unlikely(ctx || nr_events == 0)) {
1246 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1247 ctx, nr_events);
1248 goto out;
1251 ioctx = ioctx_alloc(nr_events);
1252 ret = PTR_ERR(ioctx);
1253 if (!IS_ERR(ioctx)) {
1254 ret = put_user(ioctx->user_id, ctxp);
1255 if (!ret)
1256 return 0;
1258 get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
1259 io_destroy(ioctx);
1262 out:
1263 return ret;
1266 /* sys_io_destroy:
1267 * Destroy the aio_context specified. May cancel any outstanding
1268 * AIOs and block on completion. Will fail with -ENOSYS if not
1269 * implemented. May fail with -EFAULT if the context pointed to
1270 * is invalid.
1272 asmlinkage long sys_io_destroy(aio_context_t ctx)
1274 struct kioctx *ioctx = lookup_ioctx(ctx);
1275 if (likely(NULL != ioctx)) {
1276 io_destroy(ioctx);
1277 return 0;
1279 pr_debug("EINVAL: io_destroy: invalid context id\n");
1280 return -EINVAL;
1283 static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
1285 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
1287 BUG_ON(ret <= 0);
1289 while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
1290 ssize_t this = min((ssize_t)iov->iov_len, ret);
1291 iov->iov_base += this;
1292 iov->iov_len -= this;
1293 iocb->ki_left -= this;
1294 ret -= this;
1295 if (iov->iov_len == 0) {
1296 iocb->ki_cur_seg++;
1297 iov++;
1301 /* the caller should not have done more io than what fit in
1302 * the remaining iovecs */
1303 BUG_ON(ret > 0 && iocb->ki_left == 0);
1306 static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
1308 struct file *file = iocb->ki_filp;
1309 struct address_space *mapping = file->f_mapping;
1310 struct inode *inode = mapping->host;
1311 ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
1312 unsigned long, loff_t);
1313 ssize_t ret = 0;
1314 unsigned short opcode;
1316 if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
1317 (iocb->ki_opcode == IOCB_CMD_PREAD)) {
1318 rw_op = file->f_op->aio_read;
1319 opcode = IOCB_CMD_PREADV;
1320 } else {
1321 rw_op = file->f_op->aio_write;
1322 opcode = IOCB_CMD_PWRITEV;
1325 do {
1326 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
1327 iocb->ki_nr_segs - iocb->ki_cur_seg,
1328 iocb->ki_pos);
1329 if (ret > 0)
1330 aio_advance_iovec(iocb, ret);
1332 /* retry all partial writes. retry partial reads as long as its a
1333 * regular file. */
1334 } while (ret > 0 && iocb->ki_left > 0 &&
1335 (opcode == IOCB_CMD_PWRITEV ||
1336 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
1338 /* This means we must have transferred all that we could */
1339 /* No need to retry anymore */
1340 if ((ret == 0) || (iocb->ki_left == 0))
1341 ret = iocb->ki_nbytes - iocb->ki_left;
1343 return ret;
1346 static ssize_t aio_fdsync(struct kiocb *iocb)
1348 struct file *file = iocb->ki_filp;
1349 ssize_t ret = -EINVAL;
1351 if (file->f_op->aio_fsync)
1352 ret = file->f_op->aio_fsync(iocb, 1);
1353 return ret;
1356 static ssize_t aio_fsync(struct kiocb *iocb)
1358 struct file *file = iocb->ki_filp;
1359 ssize_t ret = -EINVAL;
1361 if (file->f_op->aio_fsync)
1362 ret = file->f_op->aio_fsync(iocb, 0);
1363 return ret;
1366 static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
1368 ssize_t ret;
1370 ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
1371 kiocb->ki_nbytes, 1,
1372 &kiocb->ki_inline_vec, &kiocb->ki_iovec);
1373 if (ret < 0)
1374 goto out;
1376 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1377 kiocb->ki_cur_seg = 0;
1378 /* ki_nbytes/left now reflect bytes instead of segs */
1379 kiocb->ki_nbytes = ret;
1380 kiocb->ki_left = ret;
1382 ret = 0;
1383 out:
1384 return ret;
1387 static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
1389 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1390 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
1391 kiocb->ki_iovec->iov_len = kiocb->ki_left;
1392 kiocb->ki_nr_segs = 1;
1393 kiocb->ki_cur_seg = 0;
1394 return 0;
1398 * aio_setup_iocb:
1399 * Performs the initial checks and aio retry method
1400 * setup for the kiocb at the time of io submission.
1402 static ssize_t aio_setup_iocb(struct kiocb *kiocb)
1404 struct file *file = kiocb->ki_filp;
1405 ssize_t ret = 0;
1407 switch (kiocb->ki_opcode) {
1408 case IOCB_CMD_PREAD:
1409 ret = -EBADF;
1410 if (unlikely(!(file->f_mode & FMODE_READ)))
1411 break;
1412 ret = -EFAULT;
1413 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1414 kiocb->ki_left)))
1415 break;
1416 ret = security_file_permission(file, MAY_READ);
1417 if (unlikely(ret))
1418 break;
1419 ret = aio_setup_single_vector(kiocb);
1420 if (ret)
1421 break;
1422 ret = -EINVAL;
1423 if (file->f_op->aio_read)
1424 kiocb->ki_retry = aio_rw_vect_retry;
1425 break;
1426 case IOCB_CMD_PWRITE:
1427 ret = -EBADF;
1428 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1429 break;
1430 ret = -EFAULT;
1431 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1432 kiocb->ki_left)))
1433 break;
1434 ret = security_file_permission(file, MAY_WRITE);
1435 if (unlikely(ret))
1436 break;
1437 ret = aio_setup_single_vector(kiocb);
1438 if (ret)
1439 break;
1440 ret = -EINVAL;
1441 if (file->f_op->aio_write)
1442 kiocb->ki_retry = aio_rw_vect_retry;
1443 break;
1444 case IOCB_CMD_PREADV:
1445 ret = -EBADF;
1446 if (unlikely(!(file->f_mode & FMODE_READ)))
1447 break;
1448 ret = security_file_permission(file, MAY_READ);
1449 if (unlikely(ret))
1450 break;
1451 ret = aio_setup_vectored_rw(READ, kiocb);
1452 if (ret)
1453 break;
1454 ret = -EINVAL;
1455 if (file->f_op->aio_read)
1456 kiocb->ki_retry = aio_rw_vect_retry;
1457 break;
1458 case IOCB_CMD_PWRITEV:
1459 ret = -EBADF;
1460 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1461 break;
1462 ret = security_file_permission(file, MAY_WRITE);
1463 if (unlikely(ret))
1464 break;
1465 ret = aio_setup_vectored_rw(WRITE, kiocb);
1466 if (ret)
1467 break;
1468 ret = -EINVAL;
1469 if (file->f_op->aio_write)
1470 kiocb->ki_retry = aio_rw_vect_retry;
1471 break;
1472 case IOCB_CMD_FDSYNC:
1473 ret = -EINVAL;
1474 if (file->f_op->aio_fsync)
1475 kiocb->ki_retry = aio_fdsync;
1476 break;
1477 case IOCB_CMD_FSYNC:
1478 ret = -EINVAL;
1479 if (file->f_op->aio_fsync)
1480 kiocb->ki_retry = aio_fsync;
1481 break;
1482 default:
1483 dprintk("EINVAL: io_submit: no operation provided\n");
1484 ret = -EINVAL;
1487 if (!kiocb->ki_retry)
1488 return ret;
1490 return 0;
1494 * aio_wake_function:
1495 * wait queue callback function for aio notification,
1496 * Simply triggers a retry of the operation via kick_iocb.
1498 * This callback is specified in the wait queue entry in
1499 * a kiocb (current->io_wait points to this wait queue
1500 * entry when an aio operation executes; it is used
1501 * instead of a synchronous wait when an i/o blocking
1502 * condition is encountered during aio).
1504 * Note:
1505 * This routine is executed with the wait queue lock held.
1506 * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
1507 * the ioctx lock inside the wait queue lock. This is safe
1508 * because this callback isn't used for wait queues which
1509 * are nested inside ioctx lock (i.e. ctx->wait)
1511 static int aio_wake_function(wait_queue_t *wait, unsigned mode,
1512 int sync, void *key)
1514 struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
1516 list_del_init(&wait->task_list);
1517 kick_iocb(iocb);
1518 return 1;
1521 int fastcall io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1522 struct iocb *iocb)
1524 struct kiocb *req;
1525 struct file *file;
1526 ssize_t ret;
1528 /* enforce forwards compatibility on users */
1529 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2 ||
1530 iocb->aio_reserved3)) {
1531 pr_debug("EINVAL: io_submit: reserve field set\n");
1532 return -EINVAL;
1535 /* prevent overflows */
1536 if (unlikely(
1537 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1538 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1539 ((ssize_t)iocb->aio_nbytes < 0)
1540 )) {
1541 pr_debug("EINVAL: io_submit: overflow check\n");
1542 return -EINVAL;
1545 file = fget(iocb->aio_fildes);
1546 if (unlikely(!file))
1547 return -EBADF;
1549 req = aio_get_req(ctx); /* returns with 2 references to req */
1550 if (unlikely(!req)) {
1551 fput(file);
1552 return -EAGAIN;
1555 req->ki_filp = file;
1556 ret = put_user(req->ki_key, &user_iocb->aio_key);
1557 if (unlikely(ret)) {
1558 dprintk("EFAULT: aio_key\n");
1559 goto out_put_req;
1562 req->ki_obj.user = user_iocb;
1563 req->ki_user_data = iocb->aio_data;
1564 req->ki_pos = iocb->aio_offset;
1566 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1567 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1568 req->ki_opcode = iocb->aio_lio_opcode;
1569 init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
1570 INIT_LIST_HEAD(&req->ki_wait.task_list);
1572 ret = aio_setup_iocb(req);
1574 if (ret)
1575 goto out_put_req;
1577 spin_lock_irq(&ctx->ctx_lock);
1578 aio_run_iocb(req);
1579 if (!list_empty(&ctx->run_list)) {
1580 /* drain the run list */
1581 while (__aio_run_iocbs(ctx))
1584 spin_unlock_irq(&ctx->ctx_lock);
1585 aio_put_req(req); /* drop extra ref to req */
1586 return 0;
1588 out_put_req:
1589 aio_put_req(req); /* drop extra ref to req */
1590 aio_put_req(req); /* drop i/o ref to req */
1591 return ret;
1594 /* sys_io_submit:
1595 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1596 * the number of iocbs queued. May return -EINVAL if the aio_context
1597 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1598 * *iocbpp[0] is not properly initialized, if the operation specified
1599 * is invalid for the file descriptor in the iocb. May fail with
1600 * -EFAULT if any of the data structures point to invalid data. May
1601 * fail with -EBADF if the file descriptor specified in the first
1602 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1603 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1604 * fail with -ENOSYS if not implemented.
1606 asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1607 struct iocb __user * __user *iocbpp)
1609 struct kioctx *ctx;
1610 long ret = 0;
1611 int i;
1613 if (unlikely(nr < 0))
1614 return -EINVAL;
1616 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1617 return -EFAULT;
1619 ctx = lookup_ioctx(ctx_id);
1620 if (unlikely(!ctx)) {
1621 pr_debug("EINVAL: io_submit: invalid context id\n");
1622 return -EINVAL;
1626 * AKPM: should this return a partial result if some of the IOs were
1627 * successfully submitted?
1629 for (i=0; i<nr; i++) {
1630 struct iocb __user *user_iocb;
1631 struct iocb tmp;
1633 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1634 ret = -EFAULT;
1635 break;
1638 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1639 ret = -EFAULT;
1640 break;
1643 ret = io_submit_one(ctx, user_iocb, &tmp);
1644 if (ret)
1645 break;
1648 put_ioctx(ctx);
1649 return i ? i : ret;
1652 /* lookup_kiocb
1653 * Finds a given iocb for cancellation.
1655 static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1656 u32 key)
1658 struct list_head *pos;
1660 assert_spin_locked(&ctx->ctx_lock);
1662 /* TODO: use a hash or array, this sucks. */
1663 list_for_each(pos, &ctx->active_reqs) {
1664 struct kiocb *kiocb = list_kiocb(pos);
1665 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1666 return kiocb;
1668 return NULL;
1671 /* sys_io_cancel:
1672 * Attempts to cancel an iocb previously passed to io_submit. If
1673 * the operation is successfully cancelled, the resulting event is
1674 * copied into the memory pointed to by result without being placed
1675 * into the completion queue and 0 is returned. May fail with
1676 * -EFAULT if any of the data structures pointed to are invalid.
1677 * May fail with -EINVAL if aio_context specified by ctx_id is
1678 * invalid. May fail with -EAGAIN if the iocb specified was not
1679 * cancelled. Will fail with -ENOSYS if not implemented.
1681 asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
1682 struct io_event __user *result)
1684 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1685 struct kioctx *ctx;
1686 struct kiocb *kiocb;
1687 u32 key;
1688 int ret;
1690 ret = get_user(key, &iocb->aio_key);
1691 if (unlikely(ret))
1692 return -EFAULT;
1694 ctx = lookup_ioctx(ctx_id);
1695 if (unlikely(!ctx))
1696 return -EINVAL;
1698 spin_lock_irq(&ctx->ctx_lock);
1699 ret = -EAGAIN;
1700 kiocb = lookup_kiocb(ctx, iocb, key);
1701 if (kiocb && kiocb->ki_cancel) {
1702 cancel = kiocb->ki_cancel;
1703 kiocb->ki_users ++;
1704 kiocbSetCancelled(kiocb);
1705 } else
1706 cancel = NULL;
1707 spin_unlock_irq(&ctx->ctx_lock);
1709 if (NULL != cancel) {
1710 struct io_event tmp;
1711 pr_debug("calling cancel\n");
1712 memset(&tmp, 0, sizeof(tmp));
1713 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1714 tmp.data = kiocb->ki_user_data;
1715 ret = cancel(kiocb, &tmp);
1716 if (!ret) {
1717 /* Cancellation succeeded -- copy the result
1718 * into the user's buffer.
1720 if (copy_to_user(result, &tmp, sizeof(tmp)))
1721 ret = -EFAULT;
1723 } else
1724 ret = -EINVAL;
1726 put_ioctx(ctx);
1728 return ret;
1731 /* io_getevents:
1732 * Attempts to read at least min_nr events and up to nr events from
1733 * the completion queue for the aio_context specified by ctx_id. May
1734 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1735 * if nr is out of range, if when is out of range. May fail with
1736 * -EFAULT if any of the memory specified to is invalid. May return
1737 * 0 or < min_nr if no events are available and the timeout specified
1738 * by when has elapsed, where when == NULL specifies an infinite
1739 * timeout. Note that the timeout pointed to by when is relative and
1740 * will be updated if not NULL and the operation blocks. Will fail
1741 * with -ENOSYS if not implemented.
1743 asmlinkage long sys_io_getevents(aio_context_t ctx_id,
1744 long min_nr,
1745 long nr,
1746 struct io_event __user *events,
1747 struct timespec __user *timeout)
1749 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1750 long ret = -EINVAL;
1752 if (likely(ioctx)) {
1753 if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
1754 ret = read_events(ioctx, min_nr, nr, events, timeout);
1755 put_ioctx(ioctx);
1758 return ret;
1761 __initcall(aio_setup);
1763 EXPORT_SYMBOL(aio_complete);
1764 EXPORT_SYMBOL(aio_put_req);
1765 EXPORT_SYMBOL(wait_on_sync_kiocb);