ia64/linux-2.6.18-xen.hg

view fs/aio.c @ 452:c7ed6fe5dca0

kexec: dont initialise regions in reserve_memory()

There is no need to initialise efi_memmap_res and boot_param_res in
reserve_memory() for the initial xen domain as it is done in
machine_kexec_setup_resources() using values from the kexec hypercall.

Signed-off-by: Simon Horman <horms@verge.net.au>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Feb 28 10:55:18 2008 +0000 (2008-02-28)
parents 4763065c587c
children
line source
1 /*
2 * An async IO implementation for Linux
3 * Written by Benjamin LaHaise <bcrl@kvack.org>
4 *
5 * Implements an efficient asynchronous io interface.
6 *
7 * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
8 *
9 * See ../COPYING for licensing terms.
10 */
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>
19 #define DEBUG 0
21 #include <linux/sched.h>
22 #include <linux/fs.h>
23 #include <linux/file.h>
24 #include <linux/mm.h>
25 #include <linux/mman.h>
26 #include <linux/slab.h>
27 #include <linux/timer.h>
28 #include <linux/aio.h>
29 #include <linux/highmem.h>
30 #include <linux/workqueue.h>
31 #include <linux/security.h>
33 #include <asm/kmap_types.h>
34 #include <asm/uaccess.h>
35 #include <asm/mmu_context.h>
37 #ifdef CONFIG_EPOLL
38 #include <linux/poll.h>
39 #include <linux/eventpoll.h>
40 #endif
42 #if DEBUG > 1
43 #define dprintk printk
44 #else
45 #define dprintk(x...) do { ; } while (0)
46 #endif
48 /*------ sysctl variables----*/
49 static DEFINE_SPINLOCK(aio_nr_lock);
50 unsigned long aio_nr; /* current system wide number of aio requests */
51 unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
52 /*----end sysctl variables---*/
54 static kmem_cache_t *kiocb_cachep;
55 static kmem_cache_t *kioctx_cachep;
57 static struct workqueue_struct *aio_wq;
59 /* Used for rare fput completion. */
60 static void aio_fput_routine(void *);
61 static DECLARE_WORK(fput_work, aio_fput_routine, NULL);
63 static DEFINE_SPINLOCK(fput_lock);
64 static LIST_HEAD(fput_head);
66 static void aio_kick_handler(void *);
67 static void aio_queue_work(struct kioctx *);
69 /* aio_setup
70 * Creates the slab caches used by the aio routines, panic on
71 * failure as this is done early during the boot sequence.
72 */
73 static int __init aio_setup(void)
74 {
75 kiocb_cachep = kmem_cache_create("kiocb", sizeof(struct kiocb),
76 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
77 kioctx_cachep = kmem_cache_create("kioctx", sizeof(struct kioctx),
78 0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
80 aio_wq = create_workqueue("aio");
82 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
84 return 0;
85 }
87 static void aio_free_ring(struct kioctx *ctx)
88 {
89 struct aio_ring_info *info = &ctx->ring_info;
90 long i;
92 for (i=0; i<info->nr_pages; i++)
93 put_page(info->ring_pages[i]);
95 if (info->mmap_size) {
96 down_write(&ctx->mm->mmap_sem);
97 do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
98 up_write(&ctx->mm->mmap_sem);
99 }
101 if (info->ring_pages && info->ring_pages != info->internal_pages)
102 kfree(info->ring_pages);
103 info->ring_pages = NULL;
104 info->nr = 0;
105 }
107 static int aio_setup_ring(struct kioctx *ctx)
108 {
109 struct aio_ring *ring;
110 struct aio_ring_info *info = &ctx->ring_info;
111 unsigned nr_events = ctx->max_reqs;
112 unsigned long size;
113 int nr_pages;
115 /* Compensate for the ring buffer's head/tail overlap entry */
116 nr_events += 2; /* 1 is required, 2 for good luck */
118 size = sizeof(struct aio_ring);
119 size += sizeof(struct io_event) * nr_events;
120 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
122 if (nr_pages < 0)
123 return -EINVAL;
125 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
127 info->nr = 0;
128 info->ring_pages = info->internal_pages;
129 if (nr_pages > AIO_RING_PAGES) {
130 info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
131 if (!info->ring_pages)
132 return -ENOMEM;
133 }
135 info->mmap_size = nr_pages * PAGE_SIZE;
136 dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
137 down_write(&ctx->mm->mmap_sem);
138 info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
139 PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE,
140 0);
141 if (IS_ERR((void *)info->mmap_base)) {
142 up_write(&ctx->mm->mmap_sem);
143 printk("mmap err: %ld\n", -info->mmap_base);
144 info->mmap_size = 0;
145 aio_free_ring(ctx);
146 return -EAGAIN;
147 }
149 dprintk("mmap address: 0x%08lx\n", info->mmap_base);
150 info->nr_pages = get_user_pages(current, ctx->mm,
151 info->mmap_base, nr_pages,
152 1, 0, info->ring_pages, NULL);
153 up_write(&ctx->mm->mmap_sem);
155 if (unlikely(info->nr_pages != nr_pages)) {
156 aio_free_ring(ctx);
157 return -EAGAIN;
158 }
160 ctx->user_id = info->mmap_base;
162 info->nr = nr_events; /* trusted copy */
164 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
165 ring->nr = nr_events; /* user copy */
166 ring->id = ctx->user_id;
167 ring->head = ring->tail = 0;
168 ring->magic = AIO_RING_MAGIC;
169 ring->compat_features = AIO_RING_COMPAT_FEATURES;
170 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
171 ring->header_length = sizeof(struct aio_ring);
172 kunmap_atomic(ring, KM_USER0);
174 return 0;
175 }
178 /* aio_ring_event: returns a pointer to the event at the given index from
179 * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
180 */
181 #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
182 #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
183 #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
185 #define aio_ring_event(info, nr, km) ({ \
186 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
187 struct io_event *__event; \
188 __event = kmap_atomic( \
189 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
190 __event += pos % AIO_EVENTS_PER_PAGE; \
191 __event; \
192 })
194 #define put_aio_ring_event(event, km) do { \
195 struct io_event *__event = (event); \
196 (void)__event; \
197 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
198 } while(0)
200 /* ioctx_alloc
201 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
202 */
203 static struct kioctx *ioctx_alloc(unsigned nr_events)
204 {
205 struct mm_struct *mm;
206 struct kioctx *ctx;
208 /* Prevent overflows */
209 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
210 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
211 pr_debug("ENOMEM: nr_events too high\n");
212 return ERR_PTR(-EINVAL);
213 }
215 if ((unsigned long)nr_events > aio_max_nr)
216 return ERR_PTR(-EAGAIN);
218 ctx = kmem_cache_alloc(kioctx_cachep, GFP_KERNEL);
219 if (!ctx)
220 return ERR_PTR(-ENOMEM);
222 memset(ctx, 0, sizeof(*ctx));
223 ctx->max_reqs = nr_events;
224 mm = ctx->mm = current->mm;
225 atomic_inc(&mm->mm_count);
227 atomic_set(&ctx->users, 1);
228 spin_lock_init(&ctx->ctx_lock);
229 spin_lock_init(&ctx->ring_info.ring_lock);
230 init_waitqueue_head(&ctx->wait);
232 INIT_LIST_HEAD(&ctx->active_reqs);
233 INIT_LIST_HEAD(&ctx->run_list);
234 INIT_WORK(&ctx->wq, aio_kick_handler, ctx);
236 if (aio_setup_ring(ctx) < 0)
237 goto out_freectx;
239 /* limit the number of system wide aios */
240 spin_lock(&aio_nr_lock);
241 if (aio_nr + ctx->max_reqs > aio_max_nr ||
242 aio_nr + ctx->max_reqs < aio_nr)
243 ctx->max_reqs = 0;
244 else
245 aio_nr += ctx->max_reqs;
246 spin_unlock(&aio_nr_lock);
247 if (ctx->max_reqs == 0)
248 goto out_cleanup;
250 /* now link into global list. kludge. FIXME */
251 write_lock(&mm->ioctx_list_lock);
252 ctx->next = mm->ioctx_list;
253 mm->ioctx_list = ctx;
254 write_unlock(&mm->ioctx_list_lock);
256 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
257 ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
258 return ctx;
260 out_cleanup:
261 __put_ioctx(ctx);
262 return ERR_PTR(-EAGAIN);
264 out_freectx:
265 mmdrop(mm);
266 kmem_cache_free(kioctx_cachep, ctx);
267 ctx = ERR_PTR(-ENOMEM);
269 dprintk("aio: error allocating ioctx %p\n", ctx);
270 return ctx;
271 }
273 /* aio_cancel_all
274 * Cancels all outstanding aio requests on an aio context. Used
275 * when the processes owning a context have all exited to encourage
276 * the rapid destruction of the kioctx.
277 */
278 static void aio_cancel_all(struct kioctx *ctx)
279 {
280 int (*cancel)(struct kiocb *, struct io_event *);
281 struct io_event res;
282 spin_lock_irq(&ctx->ctx_lock);
283 ctx->dead = 1;
284 while (!list_empty(&ctx->active_reqs)) {
285 struct list_head *pos = ctx->active_reqs.next;
286 struct kiocb *iocb = list_kiocb(pos);
287 list_del_init(&iocb->ki_list);
288 cancel = iocb->ki_cancel;
289 kiocbSetCancelled(iocb);
290 if (cancel) {
291 iocb->ki_users++;
292 spin_unlock_irq(&ctx->ctx_lock);
293 cancel(iocb, &res);
294 spin_lock_irq(&ctx->ctx_lock);
295 }
296 }
297 spin_unlock_irq(&ctx->ctx_lock);
298 }
300 static void wait_for_all_aios(struct kioctx *ctx)
301 {
302 struct task_struct *tsk = current;
303 DECLARE_WAITQUEUE(wait, tsk);
305 if (!ctx->reqs_active)
306 return;
308 add_wait_queue(&ctx->wait, &wait);
309 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
310 while (ctx->reqs_active) {
311 schedule();
312 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
313 }
314 __set_task_state(tsk, TASK_RUNNING);
315 remove_wait_queue(&ctx->wait, &wait);
316 }
318 /* wait_on_sync_kiocb:
319 * Waits on the given sync kiocb to complete.
320 */
321 ssize_t fastcall wait_on_sync_kiocb(struct kiocb *iocb)
322 {
323 while (iocb->ki_users) {
324 set_current_state(TASK_UNINTERRUPTIBLE);
325 if (!iocb->ki_users)
326 break;
327 schedule();
328 }
329 __set_current_state(TASK_RUNNING);
330 return iocb->ki_user_data;
331 }
333 /* exit_aio: called when the last user of mm goes away. At this point,
334 * there is no way for any new requests to be submited or any of the
335 * io_* syscalls to be called on the context. However, there may be
336 * outstanding requests which hold references to the context; as they
337 * go away, they will call put_ioctx and release any pinned memory
338 * associated with the request (held via struct page * references).
339 */
340 void fastcall exit_aio(struct mm_struct *mm)
341 {
342 struct kioctx *ctx = mm->ioctx_list;
343 mm->ioctx_list = NULL;
344 while (ctx) {
345 struct kioctx *next = ctx->next;
346 ctx->next = NULL;
347 aio_cancel_all(ctx);
349 wait_for_all_aios(ctx);
350 /*
351 * this is an overkill, but ensures we don't leave
352 * the ctx on the aio_wq
353 */
354 flush_workqueue(aio_wq);
356 if (1 != atomic_read(&ctx->users))
357 printk(KERN_DEBUG
358 "exit_aio:ioctx still alive: %d %d %d\n",
359 atomic_read(&ctx->users), ctx->dead,
360 ctx->reqs_active);
361 put_ioctx(ctx);
362 ctx = next;
363 }
364 }
366 /* __put_ioctx
367 * Called when the last user of an aio context has gone away,
368 * and the struct needs to be freed.
369 */
370 void fastcall __put_ioctx(struct kioctx *ctx)
371 {
372 unsigned nr_events = ctx->max_reqs;
374 if (unlikely(ctx->reqs_active))
375 BUG();
377 cancel_delayed_work(&ctx->wq);
378 flush_workqueue(aio_wq);
379 aio_free_ring(ctx);
380 mmdrop(ctx->mm);
381 ctx->mm = NULL;
382 pr_debug("__put_ioctx: freeing %p\n", ctx);
383 kmem_cache_free(kioctx_cachep, ctx);
385 if (nr_events) {
386 spin_lock(&aio_nr_lock);
387 BUG_ON(aio_nr - nr_events > aio_nr);
388 aio_nr -= nr_events;
389 spin_unlock(&aio_nr_lock);
390 }
391 }
393 /* aio_get_req
394 * Allocate a slot for an aio request. Increments the users count
395 * of the kioctx so that the kioctx stays around until all requests are
396 * complete. Returns NULL if no requests are free.
397 *
398 * Returns with kiocb->users set to 2. The io submit code path holds
399 * an extra reference while submitting the i/o.
400 * This prevents races between the aio code path referencing the
401 * req (after submitting it) and aio_complete() freeing the req.
402 */
403 static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
404 static struct kiocb fastcall *__aio_get_req(struct kioctx *ctx)
405 {
406 struct kiocb *req = NULL;
407 struct aio_ring *ring;
408 int okay = 0;
410 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
411 if (unlikely(!req))
412 return NULL;
414 req->ki_flags = 0;
415 req->ki_users = 2;
416 req->ki_key = 0;
417 req->ki_ctx = ctx;
418 req->ki_cancel = NULL;
419 req->ki_retry = NULL;
420 req->ki_dtor = NULL;
421 req->private = NULL;
422 INIT_LIST_HEAD(&req->ki_run_list);
424 /* Check if the completion queue has enough free space to
425 * accept an event from this io.
426 */
427 spin_lock_irq(&ctx->ctx_lock);
428 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
429 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
430 list_add(&req->ki_list, &ctx->active_reqs);
431 get_ioctx(ctx);
432 ctx->reqs_active++;
433 okay = 1;
434 }
435 kunmap_atomic(ring, KM_USER0);
436 spin_unlock_irq(&ctx->ctx_lock);
438 if (!okay) {
439 kmem_cache_free(kiocb_cachep, req);
440 req = NULL;
441 }
443 return req;
444 }
446 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
447 {
448 struct kiocb *req;
449 /* Handle a potential starvation case -- should be exceedingly rare as
450 * requests will be stuck on fput_head only if the aio_fput_routine is
451 * delayed and the requests were the last user of the struct file.
452 */
453 req = __aio_get_req(ctx);
454 if (unlikely(NULL == req)) {
455 aio_fput_routine(NULL);
456 req = __aio_get_req(ctx);
457 }
458 return req;
459 }
461 static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
462 {
463 assert_spin_locked(&ctx->ctx_lock);
465 if (req->ki_dtor)
466 req->ki_dtor(req);
467 kmem_cache_free(kiocb_cachep, req);
468 ctx->reqs_active--;
470 if (unlikely(!ctx->reqs_active && ctx->dead))
471 wake_up(&ctx->wait);
472 }
474 static void aio_fput_routine(void *data)
475 {
476 spin_lock_irq(&fput_lock);
477 while (likely(!list_empty(&fput_head))) {
478 struct kiocb *req = list_kiocb(fput_head.next);
479 struct kioctx *ctx = req->ki_ctx;
481 list_del(&req->ki_list);
482 spin_unlock_irq(&fput_lock);
484 /* Complete the fput */
485 __fput(req->ki_filp);
487 /* Link the iocb into the context's free list */
488 spin_lock_irq(&ctx->ctx_lock);
489 really_put_req(ctx, req);
490 spin_unlock_irq(&ctx->ctx_lock);
492 put_ioctx(ctx);
493 spin_lock_irq(&fput_lock);
494 }
495 spin_unlock_irq(&fput_lock);
496 }
498 /* __aio_put_req
499 * Returns true if this put was the last user of the request.
500 */
501 static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
502 {
503 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
504 req, atomic_read(&req->ki_filp->f_count));
506 assert_spin_locked(&ctx->ctx_lock);
508 req->ki_users --;
509 if (unlikely(req->ki_users < 0))
510 BUG();
511 if (likely(req->ki_users))
512 return 0;
513 list_del(&req->ki_list); /* remove from active_reqs */
514 req->ki_cancel = NULL;
515 req->ki_retry = NULL;
517 /* Must be done under the lock to serialise against cancellation.
518 * Call this aio_fput as it duplicates fput via the fput_work.
519 */
520 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
521 get_ioctx(ctx);
522 spin_lock(&fput_lock);
523 list_add(&req->ki_list, &fput_head);
524 spin_unlock(&fput_lock);
525 queue_work(aio_wq, &fput_work);
526 } else
527 really_put_req(ctx, req);
528 return 1;
529 }
531 /* aio_put_req
532 * Returns true if this put was the last user of the kiocb,
533 * false if the request is still in use.
534 */
535 int fastcall aio_put_req(struct kiocb *req)
536 {
537 struct kioctx *ctx = req->ki_ctx;
538 int ret;
539 spin_lock_irq(&ctx->ctx_lock);
540 ret = __aio_put_req(ctx, req);
541 spin_unlock_irq(&ctx->ctx_lock);
542 if (ret)
543 put_ioctx(ctx);
544 return ret;
545 }
547 /* Lookup an ioctx id. ioctx_list is lockless for reads.
548 * FIXME: this is O(n) and is only suitable for development.
549 */
550 struct kioctx *lookup_ioctx(unsigned long ctx_id)
551 {
552 struct kioctx *ioctx;
553 struct mm_struct *mm;
555 mm = current->mm;
556 read_lock(&mm->ioctx_list_lock);
557 for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
558 if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
559 get_ioctx(ioctx);
560 break;
561 }
562 read_unlock(&mm->ioctx_list_lock);
564 return ioctx;
565 }
567 /*
568 * use_mm
569 * Makes the calling kernel thread take on the specified
570 * mm context.
571 * Called by the retry thread execute retries within the
572 * iocb issuer's mm context, so that copy_from/to_user
573 * operations work seamlessly for aio.
574 * (Note: this routine is intended to be called only
575 * from a kernel thread context)
576 */
577 static void use_mm(struct mm_struct *mm)
578 {
579 struct mm_struct *active_mm;
580 struct task_struct *tsk = current;
582 task_lock(tsk);
583 tsk->flags |= PF_BORROWED_MM;
584 active_mm = tsk->active_mm;
585 atomic_inc(&mm->mm_count);
586 tsk->mm = mm;
587 tsk->active_mm = mm;
588 /*
589 * Note that on UML this *requires* PF_BORROWED_MM to be set, otherwise
590 * it won't work. Update it accordingly if you change it here
591 */
592 activate_mm(active_mm, mm);
593 task_unlock(tsk);
595 mmdrop(active_mm);
596 }
598 /*
599 * unuse_mm
600 * Reverses the effect of use_mm, i.e. releases the
601 * specified mm context which was earlier taken on
602 * by the calling kernel thread
603 * (Note: this routine is intended to be called only
604 * from a kernel thread context)
605 *
606 * Comments: Called with ctx->ctx_lock held. This nests
607 * task_lock instead ctx_lock.
608 */
609 static void unuse_mm(struct mm_struct *mm)
610 {
611 struct task_struct *tsk = current;
613 task_lock(tsk);
614 tsk->flags &= ~PF_BORROWED_MM;
615 tsk->mm = NULL;
616 /* active_mm is still 'mm' */
617 enter_lazy_tlb(mm, tsk);
618 task_unlock(tsk);
619 }
621 /*
622 * Queue up a kiocb to be retried. Assumes that the kiocb
623 * has already been marked as kicked, and places it on
624 * the retry run list for the corresponding ioctx, if it
625 * isn't already queued. Returns 1 if it actually queued
626 * the kiocb (to tell the caller to activate the work
627 * queue to process it), or 0, if it found that it was
628 * already queued.
629 */
630 static inline int __queue_kicked_iocb(struct kiocb *iocb)
631 {
632 struct kioctx *ctx = iocb->ki_ctx;
634 assert_spin_locked(&ctx->ctx_lock);
636 if (list_empty(&iocb->ki_run_list)) {
637 list_add_tail(&iocb->ki_run_list,
638 &ctx->run_list);
639 return 1;
640 }
641 return 0;
642 }
644 /* aio_run_iocb
645 * This is the core aio execution routine. It is
646 * invoked both for initial i/o submission and
647 * subsequent retries via the aio_kick_handler.
648 * Expects to be invoked with iocb->ki_ctx->lock
649 * already held. The lock is released and reacquired
650 * as needed during processing.
651 *
652 * Calls the iocb retry method (already setup for the
653 * iocb on initial submission) for operation specific
654 * handling, but takes care of most of common retry
655 * execution details for a given iocb. The retry method
656 * needs to be non-blocking as far as possible, to avoid
657 * holding up other iocbs waiting to be serviced by the
658 * retry kernel thread.
659 *
660 * The trickier parts in this code have to do with
661 * ensuring that only one retry instance is in progress
662 * for a given iocb at any time. Providing that guarantee
663 * simplifies the coding of individual aio operations as
664 * it avoids various potential races.
665 */
666 static ssize_t aio_run_iocb(struct kiocb *iocb)
667 {
668 struct kioctx *ctx = iocb->ki_ctx;
669 ssize_t (*retry)(struct kiocb *);
670 ssize_t ret;
672 if (iocb->ki_retried++ > 1024*1024) {
673 printk("Maximal retry count. Bytes done %Zd\n",
674 iocb->ki_nbytes - iocb->ki_left);
675 return -EAGAIN;
676 }
678 if (!(iocb->ki_retried & 0xff)) {
679 pr_debug("%ld retry: %d of %d\n", iocb->ki_retried,
680 iocb->ki_nbytes - iocb->ki_left, iocb->ki_nbytes);
681 }
683 if (!(retry = iocb->ki_retry)) {
684 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
685 return 0;
686 }
688 /*
689 * We don't want the next retry iteration for this
690 * operation to start until this one has returned and
691 * updated the iocb state. However, wait_queue functions
692 * can trigger a kick_iocb from interrupt context in the
693 * meantime, indicating that data is available for the next
694 * iteration. We want to remember that and enable the
695 * next retry iteration _after_ we are through with
696 * this one.
697 *
698 * So, in order to be able to register a "kick", but
699 * prevent it from being queued now, we clear the kick
700 * flag, but make the kick code *think* that the iocb is
701 * still on the run list until we are actually done.
702 * When we are done with this iteration, we check if
703 * the iocb was kicked in the meantime and if so, queue
704 * it up afresh.
705 */
707 kiocbClearKicked(iocb);
709 /*
710 * This is so that aio_complete knows it doesn't need to
711 * pull the iocb off the run list (We can't just call
712 * INIT_LIST_HEAD because we don't want a kick_iocb to
713 * queue this on the run list yet)
714 */
715 iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
716 spin_unlock_irq(&ctx->ctx_lock);
718 /* Quit retrying if the i/o has been cancelled */
719 if (kiocbIsCancelled(iocb)) {
720 ret = -EINTR;
721 aio_complete(iocb, ret, 0);
722 /* must not access the iocb after this */
723 goto out;
724 }
726 /*
727 * Now we are all set to call the retry method in async
728 * context. By setting this thread's io_wait context
729 * to point to the wait queue entry inside the currently
730 * running iocb for the duration of the retry, we ensure
731 * that async notification wakeups are queued by the
732 * operation instead of blocking waits, and when notified,
733 * cause the iocb to be kicked for continuation (through
734 * the aio_wake_function callback).
735 */
736 BUG_ON(current->io_wait != NULL);
737 current->io_wait = &iocb->ki_wait;
738 ret = retry(iocb);
739 current->io_wait = NULL;
741 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
742 BUG_ON(!list_empty(&iocb->ki_wait.task_list));
743 aio_complete(iocb, ret, 0);
744 }
745 out:
746 spin_lock_irq(&ctx->ctx_lock);
748 if (-EIOCBRETRY == ret) {
749 /*
750 * OK, now that we are done with this iteration
751 * and know that there is more left to go,
752 * this is where we let go so that a subsequent
753 * "kick" can start the next iteration
754 */
756 /* will make __queue_kicked_iocb succeed from here on */
757 INIT_LIST_HEAD(&iocb->ki_run_list);
758 /* we must queue the next iteration ourselves, if it
759 * has already been kicked */
760 if (kiocbIsKicked(iocb)) {
761 __queue_kicked_iocb(iocb);
763 /*
764 * __queue_kicked_iocb will always return 1 here, because
765 * iocb->ki_run_list is empty at this point so it should
766 * be safe to unconditionally queue the context into the
767 * work queue.
768 */
769 aio_queue_work(ctx);
770 }
771 }
772 return ret;
773 }
775 /*
776 * __aio_run_iocbs:
777 * Process all pending retries queued on the ioctx
778 * run list.
779 * Assumes it is operating within the aio issuer's mm
780 * context.
781 */
782 static int __aio_run_iocbs(struct kioctx *ctx)
783 {
784 struct kiocb *iocb;
785 struct list_head run_list;
787 assert_spin_locked(&ctx->ctx_lock);
789 list_replace_init(&ctx->run_list, &run_list);
790 while (!list_empty(&run_list)) {
791 iocb = list_entry(run_list.next, struct kiocb,
792 ki_run_list);
793 list_del(&iocb->ki_run_list);
794 /*
795 * Hold an extra reference while retrying i/o.
796 */
797 iocb->ki_users++; /* grab extra reference */
798 aio_run_iocb(iocb);
799 if (__aio_put_req(ctx, iocb)) /* drop extra ref */
800 put_ioctx(ctx);
801 }
802 if (!list_empty(&ctx->run_list))
803 return 1;
804 return 0;
805 }
807 static void aio_queue_work(struct kioctx * ctx)
808 {
809 unsigned long timeout;
810 /*
811 * if someone is waiting, get the work started right
812 * away, otherwise, use a longer delay
813 */
814 smp_mb();
815 if (waitqueue_active(&ctx->wait))
816 timeout = 1;
817 else
818 timeout = HZ/10;
819 queue_delayed_work(aio_wq, &ctx->wq, timeout);
820 }
823 /*
824 * aio_run_iocbs:
825 * Process all pending retries queued on the ioctx
826 * run list.
827 * Assumes it is operating within the aio issuer's mm
828 * context.
829 */
830 static inline void aio_run_iocbs(struct kioctx *ctx)
831 {
832 int requeue;
834 spin_lock_irq(&ctx->ctx_lock);
836 requeue = __aio_run_iocbs(ctx);
837 spin_unlock_irq(&ctx->ctx_lock);
838 if (requeue)
839 aio_queue_work(ctx);
840 }
842 /*
843 * just like aio_run_iocbs, but keeps running them until
844 * the list stays empty
845 */
846 static inline void aio_run_all_iocbs(struct kioctx *ctx)
847 {
848 spin_lock_irq(&ctx->ctx_lock);
849 while (__aio_run_iocbs(ctx))
850 ;
851 spin_unlock_irq(&ctx->ctx_lock);
852 }
854 /*
855 * aio_kick_handler:
856 * Work queue handler triggered to process pending
857 * retries on an ioctx. Takes on the aio issuer's
858 * mm context before running the iocbs, so that
859 * copy_xxx_user operates on the issuer's address
860 * space.
861 * Run on aiod's context.
862 */
863 static void aio_kick_handler(void *data)
864 {
865 struct kioctx *ctx = data;
866 mm_segment_t oldfs = get_fs();
867 int requeue;
869 set_fs(USER_DS);
870 use_mm(ctx->mm);
871 spin_lock_irq(&ctx->ctx_lock);
872 requeue =__aio_run_iocbs(ctx);
873 unuse_mm(ctx->mm);
874 spin_unlock_irq(&ctx->ctx_lock);
875 set_fs(oldfs);
876 /*
877 * we're in a worker thread already, don't use queue_delayed_work,
878 */
879 if (requeue)
880 queue_work(aio_wq, &ctx->wq);
881 }
884 /*
885 * Called by kick_iocb to queue the kiocb for retry
886 * and if required activate the aio work queue to process
887 * it
888 */
889 static void try_queue_kicked_iocb(struct kiocb *iocb)
890 {
891 struct kioctx *ctx = iocb->ki_ctx;
892 unsigned long flags;
893 int run = 0;
895 /* We're supposed to be the only path putting the iocb back on the run
896 * list. If we find that the iocb is *back* on a wait queue already
897 * than retry has happened before we could queue the iocb. This also
898 * means that the retry could have completed and freed our iocb, no
899 * good. */
900 BUG_ON((!list_empty(&iocb->ki_wait.task_list)));
902 spin_lock_irqsave(&ctx->ctx_lock, flags);
903 /* set this inside the lock so that we can't race with aio_run_iocb()
904 * testing it and putting the iocb on the run list under the lock */
905 if (!kiocbTryKick(iocb))
906 run = __queue_kicked_iocb(iocb);
907 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
908 if (run)
909 aio_queue_work(ctx);
910 }
912 /*
913 * kick_iocb:
914 * Called typically from a wait queue callback context
915 * (aio_wake_function) to trigger a retry of the iocb.
916 * The retry is usually executed by aio workqueue
917 * threads (See aio_kick_handler).
918 */
919 void fastcall kick_iocb(struct kiocb *iocb)
920 {
921 /* sync iocbs are easy: they can only ever be executing from a
922 * single context. */
923 if (is_sync_kiocb(iocb)) {
924 kiocbSetKicked(iocb);
925 wake_up_process(iocb->ki_obj.tsk);
926 return;
927 }
929 try_queue_kicked_iocb(iocb);
930 }
931 EXPORT_SYMBOL(kick_iocb);
933 /* aio_complete
934 * Called when the io request on the given iocb is complete.
935 * Returns true if this is the last user of the request. The
936 * only other user of the request can be the cancellation code.
937 */
938 int fastcall aio_complete(struct kiocb *iocb, long res, long res2)
939 {
940 struct kioctx *ctx = iocb->ki_ctx;
941 struct aio_ring_info *info;
942 struct aio_ring *ring;
943 struct io_event *event;
944 unsigned long flags;
945 unsigned long tail;
946 int ret;
948 /*
949 * Special case handling for sync iocbs:
950 * - events go directly into the iocb for fast handling
951 * - the sync task with the iocb in its stack holds the single iocb
952 * ref, no other paths have a way to get another ref
953 * - the sync task helpfully left a reference to itself in the iocb
954 */
955 if (is_sync_kiocb(iocb)) {
956 BUG_ON(iocb->ki_users != 1);
957 iocb->ki_user_data = res;
958 iocb->ki_users = 0;
959 wake_up_process(iocb->ki_obj.tsk);
960 return 1;
961 }
963 info = &ctx->ring_info;
965 /* add a completion event to the ring buffer.
966 * must be done holding ctx->ctx_lock to prevent
967 * other code from messing with the tail
968 * pointer since we might be called from irq
969 * context.
970 */
971 spin_lock_irqsave(&ctx->ctx_lock, flags);
973 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
974 list_del_init(&iocb->ki_run_list);
976 /*
977 * cancelled requests don't get events, userland was given one
978 * when the event got cancelled.
979 */
980 if (kiocbIsCancelled(iocb))
981 goto put_rq;
983 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
985 tail = info->tail;
986 event = aio_ring_event(info, tail, KM_IRQ0);
987 if (++tail >= info->nr)
988 tail = 0;
990 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
991 event->data = iocb->ki_user_data;
992 event->res = res;
993 event->res2 = res2;
995 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
996 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
997 res, res2);
999 /* after flagging the request as done, we
1000 * must never even look at it again
1001 */
1002 smp_wmb(); /* make event visible before updating tail */
1004 info->tail = tail;
1005 ring->tail = tail;
1007 put_aio_ring_event(event, KM_IRQ0);
1008 kunmap_atomic(ring, KM_IRQ1);
1010 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
1012 pr_debug("%ld retries: %d of %d\n", iocb->ki_retried,
1013 iocb->ki_nbytes - iocb->ki_left, iocb->ki_nbytes);
1014 put_rq:
1015 /* everything turned out well, dispose of the aiocb. */
1016 ret = __aio_put_req(ctx, iocb);
1018 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
1020 if (waitqueue_active(&ctx->wait))
1021 wake_up(&ctx->wait);
1023 #ifdef CONFIG_EPOLL
1024 if (ctx->file && waitqueue_active(&ctx->poll_wait))
1025 wake_up(&ctx->poll_wait);
1026 #endif
1027 if (ret)
1028 put_ioctx(ctx);
1030 return ret;
1033 /* aio_read_evt
1034 * Pull an event off of the ioctx's event ring. Returns the number of
1035 * events fetched (0 or 1 ;-)
1036 * If ent parameter is 0, just returns the number of events that would
1037 * be fetched.
1038 * FIXME: make this use cmpxchg.
1039 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1040 */
1041 static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1043 struct aio_ring_info *info = &ioctx->ring_info;
1044 struct aio_ring *ring;
1045 unsigned long head;
1046 int ret = 0;
1048 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
1049 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1050 (unsigned long)ring->head, (unsigned long)ring->tail,
1051 (unsigned long)ring->nr);
1053 if (ring->head == ring->tail)
1054 goto out;
1056 spin_lock(&info->ring_lock);
1058 head = ring->head % info->nr;
1059 if (head != ring->tail) {
1060 if (ent) { /* event requested */
1061 struct io_event *evp =
1062 aio_ring_event(info, head, KM_USER1);
1063 *ent = *evp;
1064 head = (head + 1) % info->nr;
1065 /* finish reading the event before updatng the head */
1066 smp_mb();
1067 ring->head = head;
1068 ret = 1;
1069 put_aio_ring_event(evp, KM_USER1);
1070 } else /* only need to know availability */
1071 ret = 1;
1073 spin_unlock(&info->ring_lock);
1075 out:
1076 kunmap_atomic(ring, KM_USER0);
1077 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
1078 (unsigned long)ring->head, (unsigned long)ring->tail);
1079 return ret;
1082 struct aio_timeout {
1083 struct timer_list timer;
1084 int timed_out;
1085 struct task_struct *p;
1086 };
1088 static void timeout_func(unsigned long data)
1090 struct aio_timeout *to = (struct aio_timeout *)data;
1092 to->timed_out = 1;
1093 wake_up_process(to->p);
1096 static inline void init_timeout(struct aio_timeout *to)
1098 init_timer(&to->timer);
1099 to->timer.data = (unsigned long)to;
1100 to->timer.function = timeout_func;
1101 to->timed_out = 0;
1102 to->p = current;
1105 static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1106 const struct timespec *ts)
1108 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
1109 if (time_after(to->timer.expires, jiffies))
1110 add_timer(&to->timer);
1111 else
1112 to->timed_out = 1;
1115 static inline void clear_timeout(struct aio_timeout *to)
1117 del_singleshot_timer_sync(&to->timer);
1120 static int read_events(struct kioctx *ctx,
1121 long min_nr, long nr,
1122 struct io_event __user *event,
1123 struct timespec __user *timeout)
1125 long start_jiffies = jiffies;
1126 struct task_struct *tsk = current;
1127 DECLARE_WAITQUEUE(wait, tsk);
1128 int ret;
1129 int i = 0;
1130 struct io_event ent;
1131 struct aio_timeout to;
1132 int retry = 0;
1134 /* needed to zero any padding within an entry (there shouldn't be
1135 * any, but C is fun!
1136 */
1137 memset(&ent, 0, sizeof(ent));
1138 retry:
1139 ret = 0;
1140 while (likely(i < nr)) {
1141 ret = aio_read_evt(ctx, &ent);
1142 if (unlikely(ret <= 0))
1143 break;
1145 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1146 ent.data, ent.obj, ent.res, ent.res2);
1148 /* Could we split the check in two? */
1149 ret = -EFAULT;
1150 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1151 dprintk("aio: lost an event due to EFAULT.\n");
1152 break;
1154 ret = 0;
1156 /* Good, event copied to userland, update counts. */
1157 event ++;
1158 i ++;
1161 if (min_nr <= i)
1162 return i;
1163 if (ret)
1164 return ret;
1166 /* End fast path */
1168 /* racey check, but it gets redone */
1169 if (!retry && unlikely(!list_empty(&ctx->run_list))) {
1170 retry = 1;
1171 aio_run_all_iocbs(ctx);
1172 goto retry;
1175 init_timeout(&to);
1176 if (timeout) {
1177 struct timespec ts;
1178 ret = -EFAULT;
1179 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
1180 goto out;
1182 set_timeout(start_jiffies, &to, &ts);
1185 while (likely(i < nr)) {
1186 add_wait_queue_exclusive(&ctx->wait, &wait);
1187 do {
1188 set_task_state(tsk, TASK_INTERRUPTIBLE);
1189 ret = aio_read_evt(ctx, &ent);
1190 if (ret)
1191 break;
1192 if (min_nr <= i)
1193 break;
1194 ret = 0;
1195 if (to.timed_out) /* Only check after read evt */
1196 break;
1197 schedule();
1198 if (signal_pending(tsk)) {
1199 ret = -EINTR;
1200 break;
1202 /*ret = aio_read_evt(ctx, &ent);*/
1203 } while (1) ;
1205 set_task_state(tsk, TASK_RUNNING);
1206 remove_wait_queue(&ctx->wait, &wait);
1208 if (unlikely(ret <= 0))
1209 break;
1211 ret = -EFAULT;
1212 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1213 dprintk("aio: lost an event due to EFAULT.\n");
1214 break;
1217 /* Good, event copied to userland, update counts. */
1218 event ++;
1219 i ++;
1222 if (timeout)
1223 clear_timeout(&to);
1224 out:
1225 return i ? i : ret;
1228 /* Take an ioctx and remove it from the list of ioctx's. Protects
1229 * against races with itself via ->dead.
1230 */
1231 static void io_destroy(struct kioctx *ioctx)
1233 struct mm_struct *mm = current->mm;
1234 struct kioctx **tmp;
1235 int was_dead;
1237 /* delete the entry from the list is someone else hasn't already */
1238 write_lock(&mm->ioctx_list_lock);
1239 was_dead = ioctx->dead;
1240 ioctx->dead = 1;
1241 for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
1242 tmp = &(*tmp)->next)
1244 if (*tmp)
1245 *tmp = ioctx->next;
1246 write_unlock(&mm->ioctx_list_lock);
1248 dprintk("aio_release(%p)\n", ioctx);
1249 if (likely(!was_dead))
1250 put_ioctx(ioctx); /* twice for the list */
1252 aio_cancel_all(ioctx);
1253 wait_for_all_aios(ioctx);
1254 #ifdef CONFIG_EPOLL
1255 /* forget the poll file, but it's up to the user to close it */
1256 if (ioctx->file) {
1257 ioctx->file->private_data = 0;
1258 ioctx->file = 0;
1260 #endif
1261 put_ioctx(ioctx); /* once for the lookup */
1264 #ifdef CONFIG_EPOLL
1266 static int aio_queue_fd_close(struct inode *inode, struct file *file)
1268 struct kioctx *ioctx = file->private_data;
1269 if (ioctx) {
1270 file->private_data = 0;
1271 spin_lock_irq(&ioctx->ctx_lock);
1272 ioctx->file = 0;
1273 spin_unlock_irq(&ioctx->ctx_lock);
1275 return 0;
1278 static unsigned int aio_queue_fd_poll(struct file *file, poll_table *wait)
1279 { unsigned int pollflags = 0;
1280 struct kioctx *ioctx = file->private_data;
1282 if (ioctx) {
1284 spin_lock_irq(&ioctx->ctx_lock);
1285 /* Insert inside our poll wait queue */
1286 poll_wait(file, &ioctx->poll_wait, wait);
1288 /* Check our condition */
1289 if (aio_read_evt(ioctx, 0))
1290 pollflags = POLLIN | POLLRDNORM;
1291 spin_unlock_irq(&ioctx->ctx_lock);
1294 return pollflags;
1297 static const struct file_operations aioq_fops = {
1298 .release = aio_queue_fd_close,
1299 .poll = aio_queue_fd_poll
1300 };
1302 /* make_aio_fd:
1303 * Create a file descriptor that can be used to poll the event queue.
1304 * Based and piggybacked on the excellent epoll code.
1305 */
1307 static int make_aio_fd(struct kioctx *ioctx)
1309 int error, fd;
1310 struct inode *inode;
1311 struct file *file;
1313 error = ep_getfd(&fd, &inode, &file, NULL, &aioq_fops);
1314 if (error)
1315 return error;
1317 /* associate the file with the IO context */
1318 file->private_data = ioctx;
1319 ioctx->file = file;
1320 init_waitqueue_head(&ioctx->poll_wait);
1321 return fd;
1323 #endif
1326 /* sys_io_setup:
1327 * Create an aio_context capable of receiving at least nr_events.
1328 * ctxp must not point to an aio_context that already exists, and
1329 * must be initialized to 0 prior to the call. On successful
1330 * creation of the aio_context, *ctxp is filled in with the resulting
1331 * handle. May fail with -EINVAL if *ctxp is not initialized,
1332 * if the specified nr_events exceeds internal limits. May fail
1333 * with -EAGAIN if the specified nr_events exceeds the user's limit
1334 * of available events. May fail with -ENOMEM if insufficient kernel
1335 * resources are available. May fail with -EFAULT if an invalid
1336 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1337 * implemented.
1339 * To request a selectable fd, the user context has to be initialized
1340 * to 1, instead of 0, and the return value is the fd.
1341 * This keeps the system call compatible, since a non-zero value
1342 * was not allowed so far.
1343 */
1344 asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
1346 struct kioctx *ioctx = NULL;
1347 unsigned long ctx;
1348 long ret;
1349 int make_fd = 0;
1351 ret = get_user(ctx, ctxp);
1352 if (unlikely(ret))
1353 goto out;
1355 ret = -EINVAL;
1356 #ifdef CONFIG_EPOLL
1357 if (ctx == 1) {
1358 make_fd = 1;
1359 ctx = 0;
1361 #endif
1362 if (unlikely(ctx || nr_events == 0)) {
1363 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1364 ctx, nr_events);
1365 goto out;
1368 ioctx = ioctx_alloc(nr_events);
1369 ret = PTR_ERR(ioctx);
1370 if (!IS_ERR(ioctx)) {
1371 ret = put_user(ioctx->user_id, ctxp);
1372 #ifdef CONFIG_EPOLL
1373 if (make_fd && ret >= 0)
1374 ret = make_aio_fd(ioctx);
1375 #endif
1376 if (ret >= 0)
1377 return ret;
1379 get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
1380 io_destroy(ioctx);
1383 out:
1384 return ret;
1387 /* sys_io_destroy:
1388 * Destroy the aio_context specified. May cancel any outstanding
1389 * AIOs and block on completion. Will fail with -ENOSYS if not
1390 * implemented. May fail with -EFAULT if the context pointed to
1391 * is invalid.
1392 */
1393 asmlinkage long sys_io_destroy(aio_context_t ctx)
1395 struct kioctx *ioctx = lookup_ioctx(ctx);
1396 if (likely(NULL != ioctx)) {
1397 io_destroy(ioctx);
1398 return 0;
1400 pr_debug("EINVAL: io_destroy: invalid context id\n");
1401 return -EINVAL;
1404 /*
1405 * aio_p{read,write} are the default ki_retry methods for
1406 * IO_CMD_P{READ,WRITE}. They maintains kiocb retry state around potentially
1407 * multiple calls to f_op->aio_read(). They loop around partial progress
1408 * instead of returning -EIOCBRETRY because they don't have the means to call
1409 * kick_iocb().
1410 */
1411 static ssize_t aio_pread(struct kiocb *iocb)
1413 struct file *file = iocb->ki_filp;
1414 struct address_space *mapping = file->f_mapping;
1415 struct inode *inode = mapping->host;
1416 ssize_t ret = 0;
1418 do {
1419 ret = file->f_op->aio_read(iocb, iocb->ki_buf,
1420 iocb->ki_left, iocb->ki_pos);
1421 /*
1422 * Can't just depend on iocb->ki_left to determine
1423 * whether we are done. This may have been a short read.
1424 */
1425 if (ret > 0) {
1426 iocb->ki_buf += ret;
1427 iocb->ki_left -= ret;
1430 /*
1431 * For pipes and sockets we return once we have some data; for
1432 * regular files we retry till we complete the entire read or
1433 * find that we can't read any more data (e.g short reads).
1434 */
1435 } while (ret > 0 && iocb->ki_left > 0 &&
1436 !S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode));
1438 /* This means we must have transferred all that we could */
1439 /* No need to retry anymore */
1440 if ((ret == 0) || (iocb->ki_left == 0))
1441 ret = iocb->ki_nbytes - iocb->ki_left;
1443 return ret;
1446 /* see aio_pread() */
1447 static ssize_t aio_pwrite(struct kiocb *iocb)
1449 struct file *file = iocb->ki_filp;
1450 ssize_t ret = 0;
1452 do {
1453 ret = file->f_op->aio_write(iocb, iocb->ki_buf,
1454 iocb->ki_left, iocb->ki_pos);
1455 if (ret > 0) {
1456 iocb->ki_buf += ret;
1457 iocb->ki_left -= ret;
1459 } while (ret > 0 && iocb->ki_left > 0);
1461 if ((ret == 0) || (iocb->ki_left == 0))
1462 ret = iocb->ki_nbytes - iocb->ki_left;
1464 return ret;
1467 static ssize_t aio_fdsync(struct kiocb *iocb)
1469 struct file *file = iocb->ki_filp;
1470 ssize_t ret = -EINVAL;
1472 if (file->f_op->aio_fsync)
1473 ret = file->f_op->aio_fsync(iocb, 1);
1474 return ret;
1477 static ssize_t aio_fsync(struct kiocb *iocb)
1479 struct file *file = iocb->ki_filp;
1480 ssize_t ret = -EINVAL;
1482 if (file->f_op->aio_fsync)
1483 ret = file->f_op->aio_fsync(iocb, 0);
1484 return ret;
1487 /*
1488 * aio_setup_iocb:
1489 * Performs the initial checks and aio retry method
1490 * setup for the kiocb at the time of io submission.
1491 */
1492 static ssize_t aio_setup_iocb(struct kiocb *kiocb)
1494 struct file *file = kiocb->ki_filp;
1495 ssize_t ret = 0;
1497 switch (kiocb->ki_opcode) {
1498 case IOCB_CMD_PREAD:
1499 ret = -EBADF;
1500 if (unlikely(!(file->f_mode & FMODE_READ)))
1501 break;
1502 ret = -EFAULT;
1503 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1504 kiocb->ki_left)))
1505 break;
1506 ret = security_file_permission(file, MAY_READ);
1507 if (unlikely(ret))
1508 break;
1509 ret = -EINVAL;
1510 if (file->f_op->aio_read)
1511 kiocb->ki_retry = aio_pread;
1512 break;
1513 case IOCB_CMD_PWRITE:
1514 ret = -EBADF;
1515 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1516 break;
1517 ret = -EFAULT;
1518 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1519 kiocb->ki_left)))
1520 break;
1521 ret = security_file_permission(file, MAY_WRITE);
1522 if (unlikely(ret))
1523 break;
1524 ret = -EINVAL;
1525 if (file->f_op->aio_write)
1526 kiocb->ki_retry = aio_pwrite;
1527 break;
1528 case IOCB_CMD_FDSYNC:
1529 ret = -EINVAL;
1530 if (file->f_op->aio_fsync)
1531 kiocb->ki_retry = aio_fdsync;
1532 break;
1533 case IOCB_CMD_FSYNC:
1534 ret = -EINVAL;
1535 if (file->f_op->aio_fsync)
1536 kiocb->ki_retry = aio_fsync;
1537 break;
1538 default:
1539 dprintk("EINVAL: io_submit: no operation provided\n");
1540 ret = -EINVAL;
1543 if (!kiocb->ki_retry)
1544 return ret;
1546 return 0;
1549 /*
1550 * aio_wake_function:
1551 * wait queue callback function for aio notification,
1552 * Simply triggers a retry of the operation via kick_iocb.
1554 * This callback is specified in the wait queue entry in
1555 * a kiocb (current->io_wait points to this wait queue
1556 * entry when an aio operation executes; it is used
1557 * instead of a synchronous wait when an i/o blocking
1558 * condition is encountered during aio).
1560 * Note:
1561 * This routine is executed with the wait queue lock held.
1562 * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
1563 * the ioctx lock inside the wait queue lock. This is safe
1564 * because this callback isn't used for wait queues which
1565 * are nested inside ioctx lock (i.e. ctx->wait)
1566 */
1567 static int aio_wake_function(wait_queue_t *wait, unsigned mode,
1568 int sync, void *key)
1570 struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
1572 list_del_init(&wait->task_list);
1573 kick_iocb(iocb);
1574 return 1;
1577 int fastcall io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1578 struct iocb *iocb)
1580 struct kiocb *req;
1581 struct file *file;
1582 ssize_t ret;
1584 /* enforce forwards compatibility on users */
1585 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2 ||
1586 iocb->aio_reserved3)) {
1587 pr_debug("EINVAL: io_submit: reserve field set\n");
1588 return -EINVAL;
1591 /* prevent overflows */
1592 if (unlikely(
1593 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1594 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1595 ((ssize_t)iocb->aio_nbytes < 0)
1596 )) {
1597 pr_debug("EINVAL: io_submit: overflow check\n");
1598 return -EINVAL;
1601 file = fget(iocb->aio_fildes);
1602 if (unlikely(!file))
1603 return -EBADF;
1605 req = aio_get_req(ctx); /* returns with 2 references to req */
1606 if (unlikely(!req)) {
1607 fput(file);
1608 return -EAGAIN;
1611 req->ki_filp = file;
1612 ret = put_user(req->ki_key, &user_iocb->aio_key);
1613 if (unlikely(ret)) {
1614 dprintk("EFAULT: aio_key\n");
1615 goto out_put_req;
1618 req->ki_obj.user = user_iocb;
1619 req->ki_user_data = iocb->aio_data;
1620 req->ki_pos = iocb->aio_offset;
1622 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1623 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1624 req->ki_opcode = iocb->aio_lio_opcode;
1625 init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
1626 INIT_LIST_HEAD(&req->ki_wait.task_list);
1627 req->ki_retried = 0;
1629 ret = aio_setup_iocb(req);
1631 if (ret)
1632 goto out_put_req;
1634 spin_lock_irq(&ctx->ctx_lock);
1635 aio_run_iocb(req);
1636 if (!list_empty(&ctx->run_list)) {
1637 /* drain the run list */
1638 while (__aio_run_iocbs(ctx))
1641 spin_unlock_irq(&ctx->ctx_lock);
1642 aio_put_req(req); /* drop extra ref to req */
1643 return 0;
1645 out_put_req:
1646 aio_put_req(req); /* drop extra ref to req */
1647 aio_put_req(req); /* drop i/o ref to req */
1648 return ret;
1651 /* sys_io_submit:
1652 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1653 * the number of iocbs queued. May return -EINVAL if the aio_context
1654 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1655 * *iocbpp[0] is not properly initialized, if the operation specified
1656 * is invalid for the file descriptor in the iocb. May fail with
1657 * -EFAULT if any of the data structures point to invalid data. May
1658 * fail with -EBADF if the file descriptor specified in the first
1659 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1660 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1661 * fail with -ENOSYS if not implemented.
1662 */
1663 asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1664 struct iocb __user * __user *iocbpp)
1666 struct kioctx *ctx;
1667 long ret = 0;
1668 int i;
1670 if (unlikely(nr < 0))
1671 return -EINVAL;
1673 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1674 return -EFAULT;
1676 ctx = lookup_ioctx(ctx_id);
1677 if (unlikely(!ctx)) {
1678 pr_debug("EINVAL: io_submit: invalid context id\n");
1679 return -EINVAL;
1682 /*
1683 * AKPM: should this return a partial result if some of the IOs were
1684 * successfully submitted?
1685 */
1686 for (i=0; i<nr; i++) {
1687 struct iocb __user *user_iocb;
1688 struct iocb tmp;
1690 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1691 ret = -EFAULT;
1692 break;
1695 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1696 ret = -EFAULT;
1697 break;
1700 ret = io_submit_one(ctx, user_iocb, &tmp);
1701 if (ret)
1702 break;
1705 put_ioctx(ctx);
1706 return i ? i : ret;
1709 /* lookup_kiocb
1710 * Finds a given iocb for cancellation.
1711 */
1712 static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1713 u32 key)
1715 struct list_head *pos;
1717 assert_spin_locked(&ctx->ctx_lock);
1719 /* TODO: use a hash or array, this sucks. */
1720 list_for_each(pos, &ctx->active_reqs) {
1721 struct kiocb *kiocb = list_kiocb(pos);
1722 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1723 return kiocb;
1725 return NULL;
1728 /* sys_io_cancel:
1729 * Attempts to cancel an iocb previously passed to io_submit. If
1730 * the operation is successfully cancelled, the resulting event is
1731 * copied into the memory pointed to by result without being placed
1732 * into the completion queue and 0 is returned. May fail with
1733 * -EFAULT if any of the data structures pointed to are invalid.
1734 * May fail with -EINVAL if aio_context specified by ctx_id is
1735 * invalid. May fail with -EAGAIN if the iocb specified was not
1736 * cancelled. Will fail with -ENOSYS if not implemented.
1737 */
1738 asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
1739 struct io_event __user *result)
1741 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1742 struct kioctx *ctx;
1743 struct kiocb *kiocb;
1744 u32 key;
1745 int ret;
1747 ret = get_user(key, &iocb->aio_key);
1748 if (unlikely(ret))
1749 return -EFAULT;
1751 ctx = lookup_ioctx(ctx_id);
1752 if (unlikely(!ctx))
1753 return -EINVAL;
1755 spin_lock_irq(&ctx->ctx_lock);
1756 ret = -EAGAIN;
1757 kiocb = lookup_kiocb(ctx, iocb, key);
1758 if (kiocb && kiocb->ki_cancel) {
1759 cancel = kiocb->ki_cancel;
1760 kiocb->ki_users ++;
1761 kiocbSetCancelled(kiocb);
1762 } else
1763 cancel = NULL;
1764 spin_unlock_irq(&ctx->ctx_lock);
1766 if (NULL != cancel) {
1767 struct io_event tmp;
1768 pr_debug("calling cancel\n");
1769 memset(&tmp, 0, sizeof(tmp));
1770 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1771 tmp.data = kiocb->ki_user_data;
1772 ret = cancel(kiocb, &tmp);
1773 if (!ret) {
1774 /* Cancellation succeeded -- copy the result
1775 * into the user's buffer.
1776 */
1777 if (copy_to_user(result, &tmp, sizeof(tmp)))
1778 ret = -EFAULT;
1780 } else
1781 ret = -EINVAL;
1783 put_ioctx(ctx);
1785 return ret;
1788 /* io_getevents:
1789 * Attempts to read at least min_nr events and up to nr events from
1790 * the completion queue for the aio_context specified by ctx_id. May
1791 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1792 * if nr is out of range, if when is out of range. May fail with
1793 * -EFAULT if any of the memory specified to is invalid. May return
1794 * 0 or < min_nr if no events are available and the timeout specified
1795 * by when has elapsed, where when == NULL specifies an infinite
1796 * timeout. Note that the timeout pointed to by when is relative and
1797 * will be updated if not NULL and the operation blocks. Will fail
1798 * with -ENOSYS if not implemented.
1799 */
1800 asmlinkage long sys_io_getevents(aio_context_t ctx_id,
1801 long min_nr,
1802 long nr,
1803 struct io_event __user *events,
1804 struct timespec __user *timeout)
1806 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1807 long ret = -EINVAL;
1809 if (likely(ioctx)) {
1810 if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
1811 ret = read_events(ioctx, min_nr, nr, events, timeout);
1812 put_ioctx(ioctx);
1815 return ret;
1818 __initcall(aio_setup);
1820 EXPORT_SYMBOL(aio_complete);
1821 EXPORT_SYMBOL(aio_put_req);
1822 EXPORT_SYMBOL(wait_on_sync_kiocb);