blob: d18690bb03e9becaff3d098f3484fddb456a23d3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Badari Pulavarty027445c2006-09-30 23:28:46 -070018#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20#define DEBUG 0
21
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>
33
34#include <asm/kmap_types.h>
35#include <asm/uaccess.h>
36#include <asm/mmu_context.h>
37
38#if DEBUG > 1
39#define dprintk printk
40#else
41#define dprintk(x...) do { ; } while (0)
42#endif
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -080045static DEFINE_SPINLOCK(aio_nr_lock);
46unsigned long aio_nr; /* current system wide number of aio requests */
47unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*----end sysctl variables---*/
49
Christoph Lametere18b8902006-12-06 20:33:20 -080050static struct kmem_cache *kiocb_cachep;
51static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53static struct workqueue_struct *aio_wq;
54
55/* Used for rare fput completion. */
David Howells65f27f32006-11-22 14:55:48 +000056static void aio_fput_routine(struct work_struct *);
57static DECLARE_WORK(fput_work, aio_fput_routine);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59static DEFINE_SPINLOCK(fput_lock);
Adrian Bunk25ee7e32005-04-25 08:18:14 -070060static LIST_HEAD(fput_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
David Howells65f27f32006-11-22 14:55:48 +000062static void aio_kick_handler(struct work_struct *);
Sébastien Duguc016e222005-06-28 20:44:59 -070063static void aio_queue_work(struct kioctx *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
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.
68 */
69static int __init aio_setup(void)
70{
Christoph Lameter0a31bd52007-05-06 14:49:57 -070071 kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
72 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 aio_wq = create_workqueue("aio");
75
76 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
77
78 return 0;
79}
80
81static void aio_free_ring(struct kioctx *ctx)
82{
83 struct aio_ring_info *info = &ctx->ring_info;
84 long i;
85
86 for (i=0; i<info->nr_pages; i++)
87 put_page(info->ring_pages[i]);
88
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);
93 }
94
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;
99}
100
101static int aio_setup_ring(struct kioctx *ctx)
102{
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;
108
109 /* Compensate for the ring buffer's head/tail overlap entry */
110 nr_events += 2; /* 1 is required, 2 for good luck */
111
112 size = sizeof(struct aio_ring);
113 size += sizeof(struct io_event) * nr_events;
114 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
115
116 if (nr_pages < 0)
117 return -EINVAL;
118
119 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
120
121 info->nr = 0;
122 info->ring_pages = info->internal_pages;
123 if (nr_pages > AIO_RING_PAGES) {
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800124 info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (!info->ring_pages)
126 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
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,
Robert P. J. Daye10a4432007-02-10 01:42:56 -0800133 PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 0);
135 if (IS_ERR((void *)info->mmap_base)) {
136 up_write(&ctx->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 info->mmap_size = 0;
138 aio_free_ring(ctx);
139 return -EAGAIN;
140 }
141
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);
147
148 if (unlikely(info->nr_pages != nr_pages)) {
149 aio_free_ring(ctx);
150 return -EAGAIN;
151 }
152
153 ctx->user_id = info->mmap_base;
154
155 info->nr = nr_events; /* trusted copy */
156
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);
166
167 return 0;
168}
169
170
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();
173 */
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)
177
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; \
185})
186
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)
192
193/* ioctx_alloc
194 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
195 */
196static struct kioctx *ioctx_alloc(unsigned nr_events)
197{
198 struct mm_struct *mm;
199 struct kioctx *ctx;
200
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);
206 }
207
Zach Brownd55b5fd2005-11-07 00:59:31 -0800208 if ((unsigned long)nr_events > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return ERR_PTR(-EAGAIN);
210
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800211 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!ctx)
213 return ERR_PTR(-ENOMEM);
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 ctx->max_reqs = nr_events;
216 mm = ctx->mm = current->mm;
217 atomic_inc(&mm->mm_count);
218
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);
223
224 INIT_LIST_HEAD(&ctx->active_reqs);
225 INIT_LIST_HEAD(&ctx->run_list);
David Howells65f27f32006-11-22 14:55:48 +0000226 INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if (aio_setup_ring(ctx) < 0)
229 goto out_freectx;
230
231 /* limit the number of system wide aios */
Zach Brownd55b5fd2005-11-07 00:59:31 -0800232 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 goto out_cleanup;
241
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);
247
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;
251
252out_cleanup:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 __put_ioctx(ctx);
254 return ERR_PTR(-EAGAIN);
255
256out_freectx:
257 mmdrop(mm);
258 kmem_cache_free(kioctx_cachep, ctx);
259 ctx = ERR_PTR(-ENOMEM);
260
261 dprintk("aio: error allocating ioctx %p\n", ctx);
262 return ctx;
263}
264
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.
269 */
270static void aio_cancel_all(struct kioctx *ctx)
271{
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);
287 }
288 }
289 spin_unlock_irq(&ctx->ctx_lock);
290}
291
Adrian Bunk25ee7e32005-04-25 08:18:14 -0700292static void wait_for_all_aios(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 struct task_struct *tsk = current;
295 DECLARE_WAITQUEUE(wait, tsk);
296
Ken Chendee11c22007-02-03 01:13:45 -0800297 spin_lock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (!ctx->reqs_active)
Ken Chendee11c22007-02-03 01:13:45 -0800299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 add_wait_queue(&ctx->wait, &wait);
302 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
303 while (ctx->reqs_active) {
Ken Chendee11c22007-02-03 01:13:45 -0800304 spin_unlock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 schedule();
306 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Ken Chendee11c22007-02-03 01:13:45 -0800307 spin_lock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 }
309 __set_task_state(tsk, TASK_RUNNING);
310 remove_wait_queue(&ctx->wait, &wait);
Ken Chendee11c22007-02-03 01:13:45 -0800311
312out:
313 spin_unlock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314}
315
316/* wait_on_sync_kiocb:
317 * Waits on the given sync kiocb to complete.
318 */
319ssize_t fastcall wait_on_sync_kiocb(struct kiocb *iocb)
320{
321 while (iocb->ki_users) {
322 set_current_state(TASK_UNINTERRUPTIBLE);
323 if (!iocb->ki_users)
324 break;
325 schedule();
326 }
327 __set_current_state(TASK_RUNNING);
328 return iocb->ki_user_data;
329}
330
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).
337 */
338void fastcall exit_aio(struct mm_struct *mm)
339{
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);
346
347 wait_for_all_aios(ctx);
348 /*
Andrew Mortona9df62c2007-05-09 02:33:54 -0700349 * Ensure we don't leave the ctx on the aio_wq
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
Andrew Mortona9df62c2007-05-09 02:33:54 -0700351 flush_work(aio_wq, &ctx->wq.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
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;
360 }
361}
362
363/* __put_ioctx
364 * Called when the last user of an aio context has gone away,
365 * and the struct needs to be freed.
366 */
367void fastcall __put_ioctx(struct kioctx *ctx)
368{
369 unsigned nr_events = ctx->max_reqs;
370
Eric Sesterhenn93e06b42006-11-30 05:29:23 +0100371 BUG_ON(ctx->reqs_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 cancel_delayed_work(&ctx->wq);
Andrew Mortona9df62c2007-05-09 02:33:54 -0700374 flush_work(aio_wq, &ctx->wq.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 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);
380
Zach Brownd55b5fd2005-11-07 00:59:31 -0800381 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);
386 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
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.
393 *
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.
398 */
399static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
400static struct kiocb fastcall *__aio_get_req(struct kioctx *ctx)
401{
402 struct kiocb *req = NULL;
403 struct aio_ring *ring;
404 int okay = 0;
405
406 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
407 if (unlikely(!req))
408 return NULL;
409
Zach Brown4faa5282005-10-17 16:43:33 -0700410 req->ki_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 req->ki_users = 2;
412 req->ki_key = 0;
413 req->ki_ctx = ctx;
414 req->ki_cancel = NULL;
415 req->ki_retry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 req->ki_dtor = NULL;
417 req->private = NULL;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700418 req->ki_iovec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 INIT_LIST_HEAD(&req->ki_run_list);
420
421 /* Check if the completion queue has enough free space to
422 * accept an event from this io.
423 */
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ctx->reqs_active++;
429 okay = 1;
430 }
431 kunmap_atomic(ring, KM_USER0);
432 spin_unlock_irq(&ctx->ctx_lock);
433
434 if (!okay) {
435 kmem_cache_free(kiocb_cachep, req);
436 req = NULL;
437 }
438
439 return req;
440}
441
442static inline struct kiocb *aio_get_req(struct kioctx *ctx)
443{
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.
448 */
449 req = __aio_get_req(ctx);
450 if (unlikely(NULL == req)) {
451 aio_fput_routine(NULL);
452 req = __aio_get_req(ctx);
453 }
454 return req;
455}
456
457static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
458{
Zach Brownd00689a2005-11-13 16:07:34 -0800459 assert_spin_locked(&ctx->ctx_lock);
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 if (req->ki_dtor)
462 req->ki_dtor(req);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700463 if (req->ki_iovec != &req->ki_inline_vec)
464 kfree(req->ki_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 kmem_cache_free(kiocb_cachep, req);
466 ctx->reqs_active--;
467
468 if (unlikely(!ctx->reqs_active && ctx->dead))
469 wake_up(&ctx->wait);
470}
471
David Howells65f27f32006-11-22 14:55:48 +0000472static void aio_fput_routine(struct work_struct *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
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;
478
479 list_del(&req->ki_list);
480 spin_unlock_irq(&fput_lock);
481
482 /* Complete the fput */
483 __fput(req->ki_filp);
484
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);
489
490 put_ioctx(ctx);
491 spin_lock_irq(&fput_lock);
492 }
493 spin_unlock_irq(&fput_lock);
494}
495
496/* __aio_put_req
497 * Returns true if this put was the last user of the request.
498 */
499static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
500{
501 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
502 req, atomic_read(&req->ki_filp->f_count));
503
Zach Brownd00689a2005-11-13 16:07:34 -0800504 assert_spin_locked(&ctx->ctx_lock);
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 req->ki_users --;
Eric Sesterhenn93e06b42006-11-30 05:29:23 +0100507 BUG_ON(req->ki_users < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 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;
513
514 /* Must be done under the lock to serialise against cancellation.
515 * Call this aio_fput as it duplicates fput via the fput_work.
516 */
Nick Piggin095975d2006-01-08 01:02:19 -0800517 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 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;
526}
527
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.
531 */
532int fastcall aio_put_req(struct kiocb *req)
533{
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return ret;
540}
541
542/* Lookup an ioctx id. ioctx_list is lockless for reads.
543 * FIXME: this is O(n) and is only suitable for development.
544 */
545struct kioctx *lookup_ioctx(unsigned long ctx_id)
546{
547 struct kioctx *ioctx;
548 struct mm_struct *mm;
549
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;
556 }
557 read_unlock(&mm->ioctx_list_lock);
558
559 return ioctx;
560}
561
562/*
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)
571 */
572static void use_mm(struct mm_struct *mm)
573{
574 struct mm_struct *active_mm;
575 struct task_struct *tsk = current;
576
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;
Paolo 'Blaisorblade' Giarrusso1e40cd32005-09-03 15:57:25 -0700583 /*
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
586 */
Jeremy Fitzhardinge90aef122006-12-13 00:34:49 -0800587 switch_mm(active_mm, mm, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 task_unlock(tsk);
589
590 mmdrop(active_mm);
591}
592
593/*
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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -0700601static void unuse_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 struct task_struct *tsk = current;
604
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);
611}
612
613/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622static inline int __queue_kicked_iocb(struct kiocb *iocb)
623{
624 struct kioctx *ctx = iocb->ki_ctx;
625
Zach Brownd00689a2005-11-13 16:07:34 -0800626 assert_spin_locked(&ctx->ctx_lock);
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (list_empty(&iocb->ki_run_list)) {
629 list_add_tail(&iocb->ki_run_list,
630 &ctx->run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 return 1;
632 }
633 return 0;
634}
635
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
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200641 * already held. The lock is released and reacquired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 * as needed during processing.
643 *
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.
651 *
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.
657 */
658static ssize_t aio_run_iocb(struct kiocb *iocb)
659{
660 struct kioctx *ctx = iocb->ki_ctx;
661 ssize_t (*retry)(struct kiocb *);
662 ssize_t ret;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!(retry = iocb->ki_retry)) {
665 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
666 return 0;
667 }
668
669 /*
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.
678 *
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.
686 */
687
688 kiocbClearKicked(iocb);
689
690 /*
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)
695 */
696 iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
697 spin_unlock_irq(&ctx->ctx_lock);
698
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;
705 }
706
707 /*
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).
716 */
717 BUG_ON(current->io_wait != NULL);
718 current->io_wait = &iocb->ki_wait;
719 ret = retry(iocb);
720 current->io_wait = NULL;
721
Zach Brown897f15f2005-09-30 11:58:55 -0700722 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
723 BUG_ON(!list_empty(&iocb->ki_wait.task_list));
724 aio_complete(iocb, ret, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726out:
727 spin_lock_irq(&ctx->ctx_lock);
728
729 if (-EIOCBRETRY == ret) {
730 /*
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
735 */
736
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);
Sébastien Duguc016e222005-06-28 20:44:59 -0700743
744 /*
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.
749 */
750 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 }
753 return ret;
754}
755
756/*
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
Zach Brownd00689a2005-11-13 16:07:34 -0800761 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
763static int __aio_run_iocbs(struct kioctx *ctx)
764{
765 struct kiocb *iocb;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700766 struct list_head run_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Zach Brownd00689a2005-11-13 16:07:34 -0800768 assert_spin_locked(&ctx->ctx_lock);
769
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700770 list_replace_init(&ctx->run_list, &run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 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);
775 /*
776 * Hold an extra reference while retrying i/o.
777 */
778 iocb->ki_users++; /* grab extra reference */
779 aio_run_iocb(iocb);
Ken Chendee11c22007-02-03 01:13:45 -0800780 __aio_put_req(ctx, iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!list_empty(&ctx->run_list))
783 return 1;
784 return 0;
785}
786
787static void aio_queue_work(struct kioctx * ctx)
788{
789 unsigned long timeout;
790 /*
791 * if someone is waiting, get the work started right
792 * away, otherwise, use a longer delay
793 */
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);
800}
801
802
803/*
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.
809 */
810static inline void aio_run_iocbs(struct kioctx *ctx)
811{
812 int requeue;
813
814 spin_lock_irq(&ctx->ctx_lock);
815
816 requeue = __aio_run_iocbs(ctx);
817 spin_unlock_irq(&ctx->ctx_lock);
818 if (requeue)
819 aio_queue_work(ctx);
820}
821
822/*
823 * just like aio_run_iocbs, but keeps running them until
824 * the list stays empty
825 */
826static inline void aio_run_all_iocbs(struct kioctx *ctx)
827{
828 spin_lock_irq(&ctx->ctx_lock);
829 while (__aio_run_iocbs(ctx))
830 ;
831 spin_unlock_irq(&ctx->ctx_lock);
832}
833
834/*
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.
842 */
David Howells65f27f32006-11-22 14:55:48 +0000843static void aio_kick_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
David Howells65f27f32006-11-22 14:55:48 +0000845 struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 mm_segment_t oldfs = get_fs();
Zach Brown1ebb1102006-12-29 16:47:02 -0800847 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 int requeue;
849
850 set_fs(USER_DS);
851 use_mm(ctx->mm);
852 spin_lock_irq(&ctx->ctx_lock);
853 requeue =__aio_run_iocbs(ctx);
Zach Brown1ebb1102006-12-29 16:47:02 -0800854 mm = ctx->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 spin_unlock_irq(&ctx->ctx_lock);
Zach Brown1ebb1102006-12-29 16:47:02 -0800856 unuse_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 set_fs(oldfs);
858 /*
859 * we're in a worker thread already, don't use queue_delayed_work,
860 */
861 if (requeue)
David Howells52bad642006-11-22 14:54:01 +0000862 queue_delayed_work(aio_wq, &ctx->wq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
865
866/*
867 * Called by kick_iocb to queue the kiocb for retry
868 * and if required activate the aio work queue to process
869 * it
870 */
Zach Brown998765e2005-09-30 11:58:54 -0700871static void try_queue_kicked_iocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct kioctx *ctx = iocb->ki_ctx;
874 unsigned long flags;
875 int run = 0;
876
Zach Brown998765e2005-09-30 11:58:54 -0700877 /* 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)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 spin_lock_irqsave(&ctx->ctx_lock, flags);
Zach Brown998765e2005-09-30 11:58:54 -0700885 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Ken Chen644d3a02005-05-01 08:59:15 -0700890 if (run)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892}
893
894/*
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).
900 */
901void fastcall kick_iocb(struct kiocb *iocb)
902{
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;
909 }
910
Zach Brown998765e2005-09-30 11:58:54 -0700911 try_queue_kicked_iocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913EXPORT_SYMBOL(kick_iocb);
914
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.
919 */
920int fastcall aio_complete(struct kiocb *iocb, long res, long res2)
921{
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;
929
Zach Brown20dcae32005-11-13 16:07:33 -0800930 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 */
937 if (is_sync_kiocb(iocb)) {
Zach Brown20dcae32005-11-13 16:07:33 -0800938 BUG_ON(iocb->ki_users != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 iocb->ki_user_data = res;
Zach Brown20dcae32005-11-13 16:07:33 -0800940 iocb->ki_users = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 wake_up_process(iocb->ki_obj.tsk);
Zach Brown20dcae32005-11-13 16:07:33 -0800942 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
945 info = &ctx->ring_info;
946
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.
952 */
953 spin_lock_irqsave(&ctx->ctx_lock, flags);
954
955 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
956 list_del_init(&iocb->ki_run_list);
957
958 /*
959 * cancelled requests don't get events, userland was given one
960 * when the event got cancelled.
961 */
962 if (kiocbIsCancelled(iocb))
963 goto put_rq;
964
965 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
966
967 tail = info->tail;
968 event = aio_ring_event(info, tail, KM_IRQ0);
Ken Chen4bf69b22005-05-01 08:59:15 -0700969 if (++tail >= info->nr)
970 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
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;
976
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);
980
981 /* after flagging the request as done, we
982 * must never even look at it again
983 */
984 smp_wmb(); /* make event visible before updating tail */
985
986 info->tail = tail;
987 ring->tail = tail;
988
989 put_aio_ring_event(event, KM_IRQ0);
990 kunmap_atomic(ring, KM_IRQ1);
991
992 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993put_rq:
994 /* everything turned out well, dispose of the aiocb. */
995 ret = __aio_put_req(ctx, iocb);
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (waitqueue_active(&ctx->wait))
998 wake_up(&ctx->wait);
999
Ken Chendee11c22007-02-03 01:13:45 -08001000 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 return ret;
1002}
1003
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).
1009 */
1010static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1011{
1012 struct aio_ring_info *info = &ioctx->ring_info;
1013 struct aio_ring *ring;
1014 unsigned long head;
1015 int ret = 0;
1016
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);
1021
1022 if (ring->head == ring->tail)
1023 goto out;
1024
1025 spin_lock(&info->ring_lock);
1026
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);
1036 }
1037 spin_unlock(&info->ring_lock);
1038
1039out:
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;
1044}
1045
1046struct aio_timeout {
1047 struct timer_list timer;
1048 int timed_out;
1049 struct task_struct *p;
1050};
1051
1052static void timeout_func(unsigned long data)
1053{
1054 struct aio_timeout *to = (struct aio_timeout *)data;
1055
1056 to->timed_out = 1;
1057 wake_up_process(to->p);
1058}
1059
1060static inline void init_timeout(struct aio_timeout *to)
1061{
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;
1067}
1068
1069static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1070 const struct timespec *ts)
1071{
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;
1077}
1078
1079static inline void clear_timeout(struct aio_timeout *to)
1080{
1081 del_singleshot_timer_sync(&to->timer);
1082}
1083
1084static int read_events(struct kioctx *ctx,
1085 long min_nr, long nr,
1086 struct io_event __user *event,
1087 struct timespec __user *timeout)
1088{
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 int retry = 0;
1097
1098 /* needed to zero any padding within an entry (there shouldn't be
1099 * any, but C is fun!
1100 */
1101 memset(&ent, 0, sizeof(ent));
1102retry:
1103 ret = 0;
1104 while (likely(i < nr)) {
1105 ret = aio_read_evt(ctx, &ent);
1106 if (unlikely(ret <= 0))
1107 break;
1108
1109 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1110 ent.data, ent.obj, ent.res, ent.res2);
1111
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;
1117 }
1118 ret = 0;
1119
1120 /* Good, event copied to userland, update counts. */
1121 event ++;
1122 i ++;
1123 }
1124
1125 if (min_nr <= i)
1126 return i;
1127 if (ret)
1128 return ret;
1129
1130 /* End fast path */
1131
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;
1137 }
1138
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;
1145
1146 set_timeout(start_jiffies, &to, &ts);
1147 }
1148
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();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (signal_pending(tsk)) {
1163 ret = -EINTR;
1164 break;
1165 }
1166 /*ret = aio_read_evt(ctx, &ent);*/
1167 } while (1) ;
1168
1169 set_task_state(tsk, TASK_RUNNING);
1170 remove_wait_queue(&ctx->wait, &wait);
1171
1172 if (unlikely(ret <= 0))
1173 break;
1174
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;
1179 }
1180
1181 /* Good, event copied to userland, update counts. */
1182 event ++;
1183 i ++;
1184 }
1185
1186 if (timeout)
1187 clear_timeout(&to);
1188out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return i ? i : ret;
1190}
1191
1192/* Take an ioctx and remove it from the list of ioctx's. Protects
1193 * against races with itself via ->dead.
1194 */
1195static void io_destroy(struct kioctx *ioctx)
1196{
1197 struct mm_struct *mm = current->mm;
1198 struct kioctx **tmp;
1199 int was_dead;
1200
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)
1207 ;
1208 if (*tmp)
1209 *tmp = ioctx->next;
1210 write_unlock(&mm->ioctx_list_lock);
1211
1212 dprintk("aio_release(%p)\n", ioctx);
1213 if (likely(!was_dead))
1214 put_ioctx(ioctx); /* twice for the list */
1215
1216 aio_cancel_all(ioctx);
1217 wait_for_all_aios(ioctx);
1218 put_ioctx(ioctx); /* once for the lookup */
1219}
1220
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.
1233 */
1234asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
1235{
1236 struct kioctx *ioctx = NULL;
1237 unsigned long ctx;
1238 long ret;
1239
1240 ret = get_user(ctx, ctxp);
1241 if (unlikely(ret))
1242 goto out;
1243
1244 ret = -EINVAL;
Zach Brownd55b5fd2005-11-07 00:59:31 -08001245 if (unlikely(ctx || nr_events == 0)) {
1246 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1247 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 goto out;
1249 }
1250
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;
1257
1258 get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
1259 io_destroy(ioctx);
1260 }
1261
1262out:
1263 return ret;
1264}
1265
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.
1271 */
1272asmlinkage long sys_io_destroy(aio_context_t ctx)
1273{
1274 struct kioctx *ioctx = lookup_ioctx(ctx);
1275 if (likely(NULL != ioctx)) {
1276 io_destroy(ioctx);
1277 return 0;
1278 }
1279 pr_debug("EINVAL: io_destroy: invalid context id\n");
1280 return -EINVAL;
1281}
1282
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001283static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
1284{
1285 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
1286
1287 BUG_ON(ret <= 0);
1288
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++;
1298 }
1299 }
1300
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);
1304}
1305
1306static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct file *file = iocb->ki_filp;
1309 struct address_space *mapping = file->f_mapping;
1310 struct inode *inode = mapping->host;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001311 ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
1312 unsigned long, loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 ssize_t ret = 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001314 unsigned short opcode;
1315
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;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Zach Brown897f15f2005-09-30 11:58:55 -07001325 do {
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001326 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);
Badari Pulavarty027445c2006-09-30 23:28:46 -07001331
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001332 /* retry all partial writes. retry partial reads as long as its a
1333 * regular file. */
Zach Brown353fb072005-09-30 11:58:56 -07001334 } while (ret > 0 && iocb->ki_left > 0 &&
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001335 (opcode == IOCB_CMD_PWRITEV ||
1336 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
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;
1342
1343 return ret;
1344}
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346static ssize_t aio_fdsync(struct kiocb *iocb)
1347{
1348 struct file *file = iocb->ki_filp;
1349 ssize_t ret = -EINVAL;
1350
1351 if (file->f_op->aio_fsync)
1352 ret = file->f_op->aio_fsync(iocb, 1);
1353 return ret;
1354}
1355
1356static ssize_t aio_fsync(struct kiocb *iocb)
1357{
1358 struct file *file = iocb->ki_filp;
1359 ssize_t ret = -EINVAL;
1360
1361 if (file->f_op->aio_fsync)
1362 ret = file->f_op->aio_fsync(iocb, 0);
1363 return ret;
1364}
1365
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001366static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
1367{
1368 ssize_t ret;
1369
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;
1375
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;
1381
1382 ret = 0;
1383out:
1384 return ret;
1385}
1386
1387static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
1388{
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;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001394 return 0;
1395}
1396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397/*
1398 * aio_setup_iocb:
1399 * Performs the initial checks and aio retry method
1400 * setup for the kiocb at the time of io submission.
1401 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001402static ssize_t aio_setup_iocb(struct kiocb *kiocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403{
1404 struct file *file = kiocb->ki_filp;
1405 ssize_t ret = 0;
1406
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;
Kostik Belousov8766ce42005-10-23 12:57:13 -07001416 ret = security_file_permission(file, MAY_READ);
1417 if (unlikely(ret))
1418 break;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001419 ret = aio_setup_single_vector(kiocb);
1420 if (ret)
1421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 ret = -EINVAL;
1423 if (file->f_op->aio_read)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001424 kiocb->ki_retry = aio_rw_vect_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 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;
Kostik Belousov8766ce42005-10-23 12:57:13 -07001434 ret = security_file_permission(file, MAY_WRITE);
1435 if (unlikely(ret))
1436 break;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001437 ret = aio_setup_single_vector(kiocb);
1438 if (ret)
1439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 ret = -EINVAL;
1441 if (file->f_op->aio_write)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001442 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 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;
1485 }
1486
1487 if (!kiocb->ki_retry)
1488 return ret;
1489
1490 return 0;
1491}
1492
1493/*
1494 * aio_wake_function:
1495 * wait queue callback function for aio notification,
1496 * Simply triggers a retry of the operation via kick_iocb.
1497 *
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).
1503 *
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)
1510 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001511static int aio_wake_function(wait_queue_t *wait, unsigned mode,
1512 int sync, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
1514 struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
1515
1516 list_del_init(&wait->task_list);
1517 kick_iocb(iocb);
1518 return 1;
1519}
1520
1521int fastcall io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1522 struct iocb *iocb)
1523{
1524 struct kiocb *req;
1525 struct file *file;
1526 ssize_t ret;
1527
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;
1533 }
1534
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;
1543 }
1544
1545 file = fget(iocb->aio_fildes);
1546 if (unlikely(!file))
1547 return -EBADF;
1548
1549 req = aio_get_req(ctx); /* returns with 2 references to req */
1550 if (unlikely(!req)) {
1551 fput(file);
1552 return -EAGAIN;
1553 }
1554
1555 req->ki_filp = file;
Ken Chen212079c2005-05-01 08:59:15 -07001556 ret = put_user(req->ki_key, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (unlikely(ret)) {
1558 dprintk("EFAULT: aio_key\n");
1559 goto out_put_req;
1560 }
1561
1562 req->ki_obj.user = user_iocb;
1563 req->ki_user_data = iocb->aio_data;
1564 req->ki_pos = iocb->aio_offset;
1565
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
1572 ret = aio_setup_iocb(req);
1573
1574 if (ret)
1575 goto out_put_req;
1576
1577 spin_lock_irq(&ctx->ctx_lock);
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001578 aio_run_iocb(req);
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001579 if (!list_empty(&ctx->run_list)) {
Ken Chen954d3e92005-05-01 08:59:16 -07001580 /* drain the run list */
1581 while (__aio_run_iocbs(ctx))
1582 ;
1583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 spin_unlock_irq(&ctx->ctx_lock);
1585 aio_put_req(req); /* drop extra ref to req */
1586 return 0;
1587
1588out_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;
1592}
1593
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.
1605 */
1606asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1607 struct iocb __user * __user *iocbpp)
1608{
1609 struct kioctx *ctx;
1610 long ret = 0;
1611 int i;
1612
1613 if (unlikely(nr < 0))
1614 return -EINVAL;
1615
1616 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1617 return -EFAULT;
1618
1619 ctx = lookup_ioctx(ctx_id);
1620 if (unlikely(!ctx)) {
1621 pr_debug("EINVAL: io_submit: invalid context id\n");
1622 return -EINVAL;
1623 }
1624
1625 /*
1626 * AKPM: should this return a partial result if some of the IOs were
1627 * successfully submitted?
1628 */
1629 for (i=0; i<nr; i++) {
1630 struct iocb __user *user_iocb;
1631 struct iocb tmp;
1632
1633 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1634 ret = -EFAULT;
1635 break;
1636 }
1637
1638 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1639 ret = -EFAULT;
1640 break;
1641 }
1642
1643 ret = io_submit_one(ctx, user_iocb, &tmp);
1644 if (ret)
1645 break;
1646 }
1647
1648 put_ioctx(ctx);
1649 return i ? i : ret;
1650}
1651
1652/* lookup_kiocb
1653 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001655static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1656 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
1658 struct list_head *pos;
Zach Brownd00689a2005-11-13 16:07:34 -08001659
1660 assert_spin_locked(&ctx->ctx_lock);
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* 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;
1667 }
1668 return NULL;
1669}
1670
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.
1680 */
1681asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
1682 struct io_event __user *result)
1683{
1684 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1685 struct kioctx *ctx;
1686 struct kiocb *kiocb;
1687 u32 key;
1688 int ret;
1689
1690 ret = get_user(key, &iocb->aio_key);
1691 if (unlikely(ret))
1692 return -EFAULT;
1693
1694 ctx = lookup_ioctx(ctx_id);
1695 if (unlikely(!ctx))
1696 return -EINVAL;
1697
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);
1708
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.
1719 */
1720 if (copy_to_user(result, &tmp, sizeof(tmp)))
1721 ret = -EFAULT;
1722 }
1723 } else
Wendy Cheng8f582022005-09-09 13:02:08 -07001724 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 put_ioctx(ctx);
1727
1728 return ret;
1729}
1730
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.
1742 */
1743asmlinkage 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)
1748{
1749 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1750 long ret = -EINVAL;
1751
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);
1756 }
1757
1758 return ret;
1759}
1760
1761__initcall(aio_setup);
1762
1763EXPORT_SYMBOL(aio_complete);
1764EXPORT_SYMBOL(aio_put_req);
1765EXPORT_SYMBOL(wait_on_sync_kiocb);