blob: b97ab8028b6d2e9dde3ce85e5a2a91ad3140a7b0 [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 /*
349 * this is an overkill, but ensures we don't leave
350 * the ctx on the aio_wq
351 */
352 flush_workqueue(aio_wq);
353
354 if (1 != atomic_read(&ctx->users))
355 printk(KERN_DEBUG
356 "exit_aio:ioctx still alive: %d %d %d\n",
357 atomic_read(&ctx->users), ctx->dead,
358 ctx->reqs_active);
359 put_ioctx(ctx);
360 ctx = next;
361 }
362}
363
364/* __put_ioctx
365 * Called when the last user of an aio context has gone away,
366 * and the struct needs to be freed.
367 */
368void fastcall __put_ioctx(struct kioctx *ctx)
369{
370 unsigned nr_events = ctx->max_reqs;
371
Eric Sesterhenn93e06b42006-11-30 05:29:23 +0100372 BUG_ON(ctx->reqs_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 cancel_delayed_work(&ctx->wq);
375 flush_workqueue(aio_wq);
376 aio_free_ring(ctx);
377 mmdrop(ctx->mm);
378 ctx->mm = NULL;
379 pr_debug("__put_ioctx: freeing %p\n", ctx);
380 kmem_cache_free(kioctx_cachep, ctx);
381
Zach Brownd55b5fd2005-11-07 00:59:31 -0800382 if (nr_events) {
383 spin_lock(&aio_nr_lock);
384 BUG_ON(aio_nr - nr_events > aio_nr);
385 aio_nr -= nr_events;
386 spin_unlock(&aio_nr_lock);
387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390/* aio_get_req
391 * Allocate a slot for an aio request. Increments the users count
392 * of the kioctx so that the kioctx stays around until all requests are
393 * complete. Returns NULL if no requests are free.
394 *
395 * Returns with kiocb->users set to 2. The io submit code path holds
396 * an extra reference while submitting the i/o.
397 * This prevents races between the aio code path referencing the
398 * req (after submitting it) and aio_complete() freeing the req.
399 */
400static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
401static struct kiocb fastcall *__aio_get_req(struct kioctx *ctx)
402{
403 struct kiocb *req = NULL;
404 struct aio_ring *ring;
405 int okay = 0;
406
407 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
408 if (unlikely(!req))
409 return NULL;
410
Zach Brown4faa5282005-10-17 16:43:33 -0700411 req->ki_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 req->ki_users = 2;
413 req->ki_key = 0;
414 req->ki_ctx = ctx;
415 req->ki_cancel = NULL;
416 req->ki_retry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 req->ki_dtor = NULL;
418 req->private = NULL;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700419 req->ki_iovec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 INIT_LIST_HEAD(&req->ki_run_list);
421
422 /* Check if the completion queue has enough free space to
423 * accept an event from this io.
424 */
425 spin_lock_irq(&ctx->ctx_lock);
426 ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
427 if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
428 list_add(&req->ki_list, &ctx->active_reqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 ctx->reqs_active++;
430 okay = 1;
431 }
432 kunmap_atomic(ring, KM_USER0);
433 spin_unlock_irq(&ctx->ctx_lock);
434
435 if (!okay) {
436 kmem_cache_free(kiocb_cachep, req);
437 req = NULL;
438 }
439
440 return req;
441}
442
443static inline struct kiocb *aio_get_req(struct kioctx *ctx)
444{
445 struct kiocb *req;
446 /* Handle a potential starvation case -- should be exceedingly rare as
447 * requests will be stuck on fput_head only if the aio_fput_routine is
448 * delayed and the requests were the last user of the struct file.
449 */
450 req = __aio_get_req(ctx);
451 if (unlikely(NULL == req)) {
452 aio_fput_routine(NULL);
453 req = __aio_get_req(ctx);
454 }
455 return req;
456}
457
458static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
459{
Zach Brownd00689a2005-11-13 16:07:34 -0800460 assert_spin_locked(&ctx->ctx_lock);
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (req->ki_dtor)
463 req->ki_dtor(req);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700464 if (req->ki_iovec != &req->ki_inline_vec)
465 kfree(req->ki_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 kmem_cache_free(kiocb_cachep, req);
467 ctx->reqs_active--;
468
469 if (unlikely(!ctx->reqs_active && ctx->dead))
470 wake_up(&ctx->wait);
471}
472
David Howells65f27f32006-11-22 14:55:48 +0000473static void aio_fput_routine(struct work_struct *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 spin_lock_irq(&fput_lock);
476 while (likely(!list_empty(&fput_head))) {
477 struct kiocb *req = list_kiocb(fput_head.next);
478 struct kioctx *ctx = req->ki_ctx;
479
480 list_del(&req->ki_list);
481 spin_unlock_irq(&fput_lock);
482
483 /* Complete the fput */
484 __fput(req->ki_filp);
485
486 /* Link the iocb into the context's free list */
487 spin_lock_irq(&ctx->ctx_lock);
488 really_put_req(ctx, req);
489 spin_unlock_irq(&ctx->ctx_lock);
490
491 put_ioctx(ctx);
492 spin_lock_irq(&fput_lock);
493 }
494 spin_unlock_irq(&fput_lock);
495}
496
497/* __aio_put_req
498 * Returns true if this put was the last user of the request.
499 */
500static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
501{
502 dprintk(KERN_DEBUG "aio_put(%p): f_count=%d\n",
503 req, atomic_read(&req->ki_filp->f_count));
504
Zach Brownd00689a2005-11-13 16:07:34 -0800505 assert_spin_locked(&ctx->ctx_lock);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 req->ki_users --;
Eric Sesterhenn93e06b42006-11-30 05:29:23 +0100508 BUG_ON(req->ki_users < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (likely(req->ki_users))
510 return 0;
511 list_del(&req->ki_list); /* remove from active_reqs */
512 req->ki_cancel = NULL;
513 req->ki_retry = NULL;
514
515 /* Must be done under the lock to serialise against cancellation.
516 * Call this aio_fput as it duplicates fput via the fput_work.
517 */
Nick Piggin095975d2006-01-08 01:02:19 -0800518 if (unlikely(atomic_dec_and_test(&req->ki_filp->f_count))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 get_ioctx(ctx);
520 spin_lock(&fput_lock);
521 list_add(&req->ki_list, &fput_head);
522 spin_unlock(&fput_lock);
523 queue_work(aio_wq, &fput_work);
524 } else
525 really_put_req(ctx, req);
526 return 1;
527}
528
529/* aio_put_req
530 * Returns true if this put was the last user of the kiocb,
531 * false if the request is still in use.
532 */
533int fastcall aio_put_req(struct kiocb *req)
534{
535 struct kioctx *ctx = req->ki_ctx;
536 int ret;
537 spin_lock_irq(&ctx->ctx_lock);
538 ret = __aio_put_req(ctx, req);
539 spin_unlock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return ret;
541}
542
543/* Lookup an ioctx id. ioctx_list is lockless for reads.
544 * FIXME: this is O(n) and is only suitable for development.
545 */
546struct kioctx *lookup_ioctx(unsigned long ctx_id)
547{
548 struct kioctx *ioctx;
549 struct mm_struct *mm;
550
551 mm = current->mm;
552 read_lock(&mm->ioctx_list_lock);
553 for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
554 if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
555 get_ioctx(ioctx);
556 break;
557 }
558 read_unlock(&mm->ioctx_list_lock);
559
560 return ioctx;
561}
562
563/*
564 * use_mm
565 * Makes the calling kernel thread take on the specified
566 * mm context.
567 * Called by the retry thread execute retries within the
568 * iocb issuer's mm context, so that copy_from/to_user
569 * operations work seamlessly for aio.
570 * (Note: this routine is intended to be called only
571 * from a kernel thread context)
572 */
573static void use_mm(struct mm_struct *mm)
574{
575 struct mm_struct *active_mm;
576 struct task_struct *tsk = current;
577
578 task_lock(tsk);
579 tsk->flags |= PF_BORROWED_MM;
580 active_mm = tsk->active_mm;
581 atomic_inc(&mm->mm_count);
582 tsk->mm = mm;
583 tsk->active_mm = mm;
Paolo 'Blaisorblade' Giarrusso1e40cd32005-09-03 15:57:25 -0700584 /*
585 * Note that on UML this *requires* PF_BORROWED_MM to be set, otherwise
586 * it won't work. Update it accordingly if you change it here
587 */
Jeremy Fitzhardinge90aef122006-12-13 00:34:49 -0800588 switch_mm(active_mm, mm, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 task_unlock(tsk);
590
591 mmdrop(active_mm);
592}
593
594/*
595 * unuse_mm
596 * Reverses the effect of use_mm, i.e. releases the
597 * specified mm context which was earlier taken on
598 * by the calling kernel thread
599 * (Note: this routine is intended to be called only
600 * from a kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -0700602static void unuse_mm(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603{
604 struct task_struct *tsk = current;
605
606 task_lock(tsk);
607 tsk->flags &= ~PF_BORROWED_MM;
608 tsk->mm = NULL;
609 /* active_mm is still 'mm' */
610 enter_lazy_tlb(mm, tsk);
611 task_unlock(tsk);
612}
613
614/*
615 * Queue up a kiocb to be retried. Assumes that the kiocb
616 * has already been marked as kicked, and places it on
617 * the retry run list for the corresponding ioctx, if it
618 * isn't already queued. Returns 1 if it actually queued
619 * the kiocb (to tell the caller to activate the work
620 * queue to process it), or 0, if it found that it was
621 * already queued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 */
623static inline int __queue_kicked_iocb(struct kiocb *iocb)
624{
625 struct kioctx *ctx = iocb->ki_ctx;
626
Zach Brownd00689a2005-11-13 16:07:34 -0800627 assert_spin_locked(&ctx->ctx_lock);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (list_empty(&iocb->ki_run_list)) {
630 list_add_tail(&iocb->ki_run_list,
631 &ctx->run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 return 1;
633 }
634 return 0;
635}
636
637/* aio_run_iocb
638 * This is the core aio execution routine. It is
639 * invoked both for initial i/o submission and
640 * subsequent retries via the aio_kick_handler.
641 * Expects to be invoked with iocb->ki_ctx->lock
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200642 * already held. The lock is released and reacquired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * as needed during processing.
644 *
645 * Calls the iocb retry method (already setup for the
646 * iocb on initial submission) for operation specific
647 * handling, but takes care of most of common retry
648 * execution details for a given iocb. The retry method
649 * needs to be non-blocking as far as possible, to avoid
650 * holding up other iocbs waiting to be serviced by the
651 * retry kernel thread.
652 *
653 * The trickier parts in this code have to do with
654 * ensuring that only one retry instance is in progress
655 * for a given iocb at any time. Providing that guarantee
656 * simplifies the coding of individual aio operations as
657 * it avoids various potential races.
658 */
659static ssize_t aio_run_iocb(struct kiocb *iocb)
660{
661 struct kioctx *ctx = iocb->ki_ctx;
662 ssize_t (*retry)(struct kiocb *);
663 ssize_t ret;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 if (!(retry = iocb->ki_retry)) {
666 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
667 return 0;
668 }
669
670 /*
671 * We don't want the next retry iteration for this
672 * operation to start until this one has returned and
673 * updated the iocb state. However, wait_queue functions
674 * can trigger a kick_iocb from interrupt context in the
675 * meantime, indicating that data is available for the next
676 * iteration. We want to remember that and enable the
677 * next retry iteration _after_ we are through with
678 * this one.
679 *
680 * So, in order to be able to register a "kick", but
681 * prevent it from being queued now, we clear the kick
682 * flag, but make the kick code *think* that the iocb is
683 * still on the run list until we are actually done.
684 * When we are done with this iteration, we check if
685 * the iocb was kicked in the meantime and if so, queue
686 * it up afresh.
687 */
688
689 kiocbClearKicked(iocb);
690
691 /*
692 * This is so that aio_complete knows it doesn't need to
693 * pull the iocb off the run list (We can't just call
694 * INIT_LIST_HEAD because we don't want a kick_iocb to
695 * queue this on the run list yet)
696 */
697 iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
698 spin_unlock_irq(&ctx->ctx_lock);
699
700 /* Quit retrying if the i/o has been cancelled */
701 if (kiocbIsCancelled(iocb)) {
702 ret = -EINTR;
703 aio_complete(iocb, ret, 0);
704 /* must not access the iocb after this */
705 goto out;
706 }
707
708 /*
709 * Now we are all set to call the retry method in async
710 * context. By setting this thread's io_wait context
711 * to point to the wait queue entry inside the currently
712 * running iocb for the duration of the retry, we ensure
713 * that async notification wakeups are queued by the
714 * operation instead of blocking waits, and when notified,
715 * cause the iocb to be kicked for continuation (through
716 * the aio_wake_function callback).
717 */
718 BUG_ON(current->io_wait != NULL);
719 current->io_wait = &iocb->ki_wait;
720 ret = retry(iocb);
721 current->io_wait = NULL;
722
Zach Brown897f15f2005-09-30 11:58:55 -0700723 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
724 BUG_ON(!list_empty(&iocb->ki_wait.task_list));
725 aio_complete(iocb, ret, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727out:
728 spin_lock_irq(&ctx->ctx_lock);
729
730 if (-EIOCBRETRY == ret) {
731 /*
732 * OK, now that we are done with this iteration
733 * and know that there is more left to go,
734 * this is where we let go so that a subsequent
735 * "kick" can start the next iteration
736 */
737
738 /* will make __queue_kicked_iocb succeed from here on */
739 INIT_LIST_HEAD(&iocb->ki_run_list);
740 /* we must queue the next iteration ourselves, if it
741 * has already been kicked */
742 if (kiocbIsKicked(iocb)) {
743 __queue_kicked_iocb(iocb);
Sébastien Duguc016e222005-06-28 20:44:59 -0700744
745 /*
746 * __queue_kicked_iocb will always return 1 here, because
747 * iocb->ki_run_list is empty at this point so it should
748 * be safe to unconditionally queue the context into the
749 * work queue.
750 */
751 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753 }
754 return ret;
755}
756
757/*
758 * __aio_run_iocbs:
759 * Process all pending retries queued on the ioctx
760 * run list.
761 * Assumes it is operating within the aio issuer's mm
Zach Brownd00689a2005-11-13 16:07:34 -0800762 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 */
764static int __aio_run_iocbs(struct kioctx *ctx)
765{
766 struct kiocb *iocb;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700767 struct list_head run_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Zach Brownd00689a2005-11-13 16:07:34 -0800769 assert_spin_locked(&ctx->ctx_lock);
770
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700771 list_replace_init(&ctx->run_list, &run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 while (!list_empty(&run_list)) {
773 iocb = list_entry(run_list.next, struct kiocb,
774 ki_run_list);
775 list_del(&iocb->ki_run_list);
776 /*
777 * Hold an extra reference while retrying i/o.
778 */
779 iocb->ki_users++; /* grab extra reference */
780 aio_run_iocb(iocb);
Ken Chendee11c22007-02-03 01:13:45 -0800781 __aio_put_req(ctx, iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!list_empty(&ctx->run_list))
784 return 1;
785 return 0;
786}
787
788static void aio_queue_work(struct kioctx * ctx)
789{
790 unsigned long timeout;
791 /*
792 * if someone is waiting, get the work started right
793 * away, otherwise, use a longer delay
794 */
795 smp_mb();
796 if (waitqueue_active(&ctx->wait))
797 timeout = 1;
798 else
799 timeout = HZ/10;
800 queue_delayed_work(aio_wq, &ctx->wq, timeout);
801}
802
803
804/*
805 * aio_run_iocbs:
806 * Process all pending retries queued on the ioctx
807 * run list.
808 * Assumes it is operating within the aio issuer's mm
809 * context.
810 */
811static inline void aio_run_iocbs(struct kioctx *ctx)
812{
813 int requeue;
814
815 spin_lock_irq(&ctx->ctx_lock);
816
817 requeue = __aio_run_iocbs(ctx);
818 spin_unlock_irq(&ctx->ctx_lock);
819 if (requeue)
820 aio_queue_work(ctx);
821}
822
823/*
824 * just like aio_run_iocbs, but keeps running them until
825 * the list stays empty
826 */
827static inline void aio_run_all_iocbs(struct kioctx *ctx)
828{
829 spin_lock_irq(&ctx->ctx_lock);
830 while (__aio_run_iocbs(ctx))
831 ;
832 spin_unlock_irq(&ctx->ctx_lock);
833}
834
835/*
836 * aio_kick_handler:
837 * Work queue handler triggered to process pending
838 * retries on an ioctx. Takes on the aio issuer's
839 * mm context before running the iocbs, so that
840 * copy_xxx_user operates on the issuer's address
841 * space.
842 * Run on aiod's context.
843 */
David Howells65f27f32006-11-22 14:55:48 +0000844static void aio_kick_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
David Howells65f27f32006-11-22 14:55:48 +0000846 struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 mm_segment_t oldfs = get_fs();
Zach Brown1ebb1102006-12-29 16:47:02 -0800848 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 int requeue;
850
851 set_fs(USER_DS);
852 use_mm(ctx->mm);
853 spin_lock_irq(&ctx->ctx_lock);
854 requeue =__aio_run_iocbs(ctx);
Zach Brown1ebb1102006-12-29 16:47:02 -0800855 mm = ctx->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 spin_unlock_irq(&ctx->ctx_lock);
Zach Brown1ebb1102006-12-29 16:47:02 -0800857 unuse_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 set_fs(oldfs);
859 /*
860 * we're in a worker thread already, don't use queue_delayed_work,
861 */
862 if (requeue)
David Howells52bad642006-11-22 14:54:01 +0000863 queue_delayed_work(aio_wq, &ctx->wq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866
867/*
868 * Called by kick_iocb to queue the kiocb for retry
869 * and if required activate the aio work queue to process
870 * it
871 */
Zach Brown998765e2005-09-30 11:58:54 -0700872static void try_queue_kicked_iocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873{
874 struct kioctx *ctx = iocb->ki_ctx;
875 unsigned long flags;
876 int run = 0;
877
Zach Brown998765e2005-09-30 11:58:54 -0700878 /* We're supposed to be the only path putting the iocb back on the run
879 * list. If we find that the iocb is *back* on a wait queue already
880 * than retry has happened before we could queue the iocb. This also
881 * means that the retry could have completed and freed our iocb, no
882 * good. */
883 BUG_ON((!list_empty(&iocb->ki_wait.task_list)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 spin_lock_irqsave(&ctx->ctx_lock, flags);
Zach Brown998765e2005-09-30 11:58:54 -0700886 /* set this inside the lock so that we can't race with aio_run_iocb()
887 * testing it and putting the iocb on the run list under the lock */
888 if (!kiocbTryKick(iocb))
889 run = __queue_kicked_iocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Ken Chen644d3a02005-05-01 08:59:15 -0700891 if (run)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
896 * kick_iocb:
897 * Called typically from a wait queue callback context
898 * (aio_wake_function) to trigger a retry of the iocb.
899 * The retry is usually executed by aio workqueue
900 * threads (See aio_kick_handler).
901 */
902void fastcall kick_iocb(struct kiocb *iocb)
903{
904 /* sync iocbs are easy: they can only ever be executing from a
905 * single context. */
906 if (is_sync_kiocb(iocb)) {
907 kiocbSetKicked(iocb);
908 wake_up_process(iocb->ki_obj.tsk);
909 return;
910 }
911
Zach Brown998765e2005-09-30 11:58:54 -0700912 try_queue_kicked_iocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913}
914EXPORT_SYMBOL(kick_iocb);
915
916/* aio_complete
917 * Called when the io request on the given iocb is complete.
918 * Returns true if this is the last user of the request. The
919 * only other user of the request can be the cancellation code.
920 */
921int fastcall aio_complete(struct kiocb *iocb, long res, long res2)
922{
923 struct kioctx *ctx = iocb->ki_ctx;
924 struct aio_ring_info *info;
925 struct aio_ring *ring;
926 struct io_event *event;
927 unsigned long flags;
928 unsigned long tail;
929 int ret;
930
Zach Brown20dcae32005-11-13 16:07:33 -0800931 /*
932 * Special case handling for sync iocbs:
933 * - events go directly into the iocb for fast handling
934 * - the sync task with the iocb in its stack holds the single iocb
935 * ref, no other paths have a way to get another ref
936 * - the sync task helpfully left a reference to itself in the iocb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 */
938 if (is_sync_kiocb(iocb)) {
Zach Brown20dcae32005-11-13 16:07:33 -0800939 BUG_ON(iocb->ki_users != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 iocb->ki_user_data = res;
Zach Brown20dcae32005-11-13 16:07:33 -0800941 iocb->ki_users = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 wake_up_process(iocb->ki_obj.tsk);
Zach Brown20dcae32005-11-13 16:07:33 -0800943 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
946 info = &ctx->ring_info;
947
948 /* add a completion event to the ring buffer.
949 * must be done holding ctx->ctx_lock to prevent
950 * other code from messing with the tail
951 * pointer since we might be called from irq
952 * context.
953 */
954 spin_lock_irqsave(&ctx->ctx_lock, flags);
955
956 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
957 list_del_init(&iocb->ki_run_list);
958
959 /*
960 * cancelled requests don't get events, userland was given one
961 * when the event got cancelled.
962 */
963 if (kiocbIsCancelled(iocb))
964 goto put_rq;
965
966 ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
967
968 tail = info->tail;
969 event = aio_ring_event(info, tail, KM_IRQ0);
Ken Chen4bf69b22005-05-01 08:59:15 -0700970 if (++tail >= info->nr)
971 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
974 event->data = iocb->ki_user_data;
975 event->res = res;
976 event->res2 = res2;
977
978 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
979 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
980 res, res2);
981
982 /* after flagging the request as done, we
983 * must never even look at it again
984 */
985 smp_wmb(); /* make event visible before updating tail */
986
987 info->tail = tail;
988 ring->tail = tail;
989
990 put_aio_ring_event(event, KM_IRQ0);
991 kunmap_atomic(ring, KM_IRQ1);
992
993 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994put_rq:
995 /* everything turned out well, dispose of the aiocb. */
996 ret = __aio_put_req(ctx, iocb);
997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (waitqueue_active(&ctx->wait))
999 wake_up(&ctx->wait);
1000
Ken Chendee11c22007-02-03 01:13:45 -08001001 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 return ret;
1003}
1004
1005/* aio_read_evt
1006 * Pull an event off of the ioctx's event ring. Returns the number of
1007 * events fetched (0 or 1 ;-)
1008 * FIXME: make this use cmpxchg.
1009 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1010 */
1011static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1012{
1013 struct aio_ring_info *info = &ioctx->ring_info;
1014 struct aio_ring *ring;
1015 unsigned long head;
1016 int ret = 0;
1017
1018 ring = kmap_atomic(info->ring_pages[0], KM_USER0);
1019 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1020 (unsigned long)ring->head, (unsigned long)ring->tail,
1021 (unsigned long)ring->nr);
1022
1023 if (ring->head == ring->tail)
1024 goto out;
1025
1026 spin_lock(&info->ring_lock);
1027
1028 head = ring->head % info->nr;
1029 if (head != ring->tail) {
1030 struct io_event *evp = aio_ring_event(info, head, KM_USER1);
1031 *ent = *evp;
1032 head = (head + 1) % info->nr;
1033 smp_mb(); /* finish reading the event before updatng the head */
1034 ring->head = head;
1035 ret = 1;
1036 put_aio_ring_event(evp, KM_USER1);
1037 }
1038 spin_unlock(&info->ring_lock);
1039
1040out:
1041 kunmap_atomic(ring, KM_USER0);
1042 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
1043 (unsigned long)ring->head, (unsigned long)ring->tail);
1044 return ret;
1045}
1046
1047struct aio_timeout {
1048 struct timer_list timer;
1049 int timed_out;
1050 struct task_struct *p;
1051};
1052
1053static void timeout_func(unsigned long data)
1054{
1055 struct aio_timeout *to = (struct aio_timeout *)data;
1056
1057 to->timed_out = 1;
1058 wake_up_process(to->p);
1059}
1060
1061static inline void init_timeout(struct aio_timeout *to)
1062{
1063 init_timer(&to->timer);
1064 to->timer.data = (unsigned long)to;
1065 to->timer.function = timeout_func;
1066 to->timed_out = 0;
1067 to->p = current;
1068}
1069
1070static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1071 const struct timespec *ts)
1072{
1073 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
1074 if (time_after(to->timer.expires, jiffies))
1075 add_timer(&to->timer);
1076 else
1077 to->timed_out = 1;
1078}
1079
1080static inline void clear_timeout(struct aio_timeout *to)
1081{
1082 del_singleshot_timer_sync(&to->timer);
1083}
1084
1085static int read_events(struct kioctx *ctx,
1086 long min_nr, long nr,
1087 struct io_event __user *event,
1088 struct timespec __user *timeout)
1089{
1090 long start_jiffies = jiffies;
1091 struct task_struct *tsk = current;
1092 DECLARE_WAITQUEUE(wait, tsk);
1093 int ret;
1094 int i = 0;
1095 struct io_event ent;
1096 struct aio_timeout to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int retry = 0;
1098
1099 /* needed to zero any padding within an entry (there shouldn't be
1100 * any, but C is fun!
1101 */
1102 memset(&ent, 0, sizeof(ent));
1103retry:
1104 ret = 0;
1105 while (likely(i < nr)) {
1106 ret = aio_read_evt(ctx, &ent);
1107 if (unlikely(ret <= 0))
1108 break;
1109
1110 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1111 ent.data, ent.obj, ent.res, ent.res2);
1112
1113 /* Could we split the check in two? */
1114 ret = -EFAULT;
1115 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1116 dprintk("aio: lost an event due to EFAULT.\n");
1117 break;
1118 }
1119 ret = 0;
1120
1121 /* Good, event copied to userland, update counts. */
1122 event ++;
1123 i ++;
1124 }
1125
1126 if (min_nr <= i)
1127 return i;
1128 if (ret)
1129 return ret;
1130
1131 /* End fast path */
1132
1133 /* racey check, but it gets redone */
1134 if (!retry && unlikely(!list_empty(&ctx->run_list))) {
1135 retry = 1;
1136 aio_run_all_iocbs(ctx);
1137 goto retry;
1138 }
1139
1140 init_timeout(&to);
1141 if (timeout) {
1142 struct timespec ts;
1143 ret = -EFAULT;
1144 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
1145 goto out;
1146
1147 set_timeout(start_jiffies, &to, &ts);
1148 }
1149
1150 while (likely(i < nr)) {
1151 add_wait_queue_exclusive(&ctx->wait, &wait);
1152 do {
1153 set_task_state(tsk, TASK_INTERRUPTIBLE);
1154 ret = aio_read_evt(ctx, &ent);
1155 if (ret)
1156 break;
1157 if (min_nr <= i)
1158 break;
1159 ret = 0;
1160 if (to.timed_out) /* Only check after read evt */
1161 break;
1162 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 if (signal_pending(tsk)) {
1164 ret = -EINTR;
1165 break;
1166 }
1167 /*ret = aio_read_evt(ctx, &ent);*/
1168 } while (1) ;
1169
1170 set_task_state(tsk, TASK_RUNNING);
1171 remove_wait_queue(&ctx->wait, &wait);
1172
1173 if (unlikely(ret <= 0))
1174 break;
1175
1176 ret = -EFAULT;
1177 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1178 dprintk("aio: lost an event due to EFAULT.\n");
1179 break;
1180 }
1181
1182 /* Good, event copied to userland, update counts. */
1183 event ++;
1184 i ++;
1185 }
1186
1187 if (timeout)
1188 clear_timeout(&to);
1189out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 return i ? i : ret;
1191}
1192
1193/* Take an ioctx and remove it from the list of ioctx's. Protects
1194 * against races with itself via ->dead.
1195 */
1196static void io_destroy(struct kioctx *ioctx)
1197{
1198 struct mm_struct *mm = current->mm;
1199 struct kioctx **tmp;
1200 int was_dead;
1201
1202 /* delete the entry from the list is someone else hasn't already */
1203 write_lock(&mm->ioctx_list_lock);
1204 was_dead = ioctx->dead;
1205 ioctx->dead = 1;
1206 for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
1207 tmp = &(*tmp)->next)
1208 ;
1209 if (*tmp)
1210 *tmp = ioctx->next;
1211 write_unlock(&mm->ioctx_list_lock);
1212
1213 dprintk("aio_release(%p)\n", ioctx);
1214 if (likely(!was_dead))
1215 put_ioctx(ioctx); /* twice for the list */
1216
1217 aio_cancel_all(ioctx);
1218 wait_for_all_aios(ioctx);
1219 put_ioctx(ioctx); /* once for the lookup */
1220}
1221
1222/* sys_io_setup:
1223 * Create an aio_context capable of receiving at least nr_events.
1224 * ctxp must not point to an aio_context that already exists, and
1225 * must be initialized to 0 prior to the call. On successful
1226 * creation of the aio_context, *ctxp is filled in with the resulting
1227 * handle. May fail with -EINVAL if *ctxp is not initialized,
1228 * if the specified nr_events exceeds internal limits. May fail
1229 * with -EAGAIN if the specified nr_events exceeds the user's limit
1230 * of available events. May fail with -ENOMEM if insufficient kernel
1231 * resources are available. May fail with -EFAULT if an invalid
1232 * pointer is passed for ctxp. Will fail with -ENOSYS if not
1233 * implemented.
1234 */
1235asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
1236{
1237 struct kioctx *ioctx = NULL;
1238 unsigned long ctx;
1239 long ret;
1240
1241 ret = get_user(ctx, ctxp);
1242 if (unlikely(ret))
1243 goto out;
1244
1245 ret = -EINVAL;
Zach Brownd55b5fd2005-11-07 00:59:31 -08001246 if (unlikely(ctx || nr_events == 0)) {
1247 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
1248 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 goto out;
1250 }
1251
1252 ioctx = ioctx_alloc(nr_events);
1253 ret = PTR_ERR(ioctx);
1254 if (!IS_ERR(ioctx)) {
1255 ret = put_user(ioctx->user_id, ctxp);
1256 if (!ret)
1257 return 0;
1258
1259 get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
1260 io_destroy(ioctx);
1261 }
1262
1263out:
1264 return ret;
1265}
1266
1267/* sys_io_destroy:
1268 * Destroy the aio_context specified. May cancel any outstanding
1269 * AIOs and block on completion. Will fail with -ENOSYS if not
1270 * implemented. May fail with -EFAULT if the context pointed to
1271 * is invalid.
1272 */
1273asmlinkage long sys_io_destroy(aio_context_t ctx)
1274{
1275 struct kioctx *ioctx = lookup_ioctx(ctx);
1276 if (likely(NULL != ioctx)) {
1277 io_destroy(ioctx);
1278 return 0;
1279 }
1280 pr_debug("EINVAL: io_destroy: invalid context id\n");
1281 return -EINVAL;
1282}
1283
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001284static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
1285{
1286 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
1287
1288 BUG_ON(ret <= 0);
1289
1290 while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
1291 ssize_t this = min((ssize_t)iov->iov_len, ret);
1292 iov->iov_base += this;
1293 iov->iov_len -= this;
1294 iocb->ki_left -= this;
1295 ret -= this;
1296 if (iov->iov_len == 0) {
1297 iocb->ki_cur_seg++;
1298 iov++;
1299 }
1300 }
1301
1302 /* the caller should not have done more io than what fit in
1303 * the remaining iovecs */
1304 BUG_ON(ret > 0 && iocb->ki_left == 0);
1305}
1306
1307static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 struct file *file = iocb->ki_filp;
1310 struct address_space *mapping = file->f_mapping;
1311 struct inode *inode = mapping->host;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001312 ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
1313 unsigned long, loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ssize_t ret = 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001315 unsigned short opcode;
1316
1317 if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
1318 (iocb->ki_opcode == IOCB_CMD_PREAD)) {
1319 rw_op = file->f_op->aio_read;
1320 opcode = IOCB_CMD_PREADV;
1321 } else {
1322 rw_op = file->f_op->aio_write;
1323 opcode = IOCB_CMD_PWRITEV;
1324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
Zach Brown897f15f2005-09-30 11:58:55 -07001326 do {
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001327 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
1328 iocb->ki_nr_segs - iocb->ki_cur_seg,
1329 iocb->ki_pos);
1330 if (ret > 0)
1331 aio_advance_iovec(iocb, ret);
Badari Pulavarty027445c2006-09-30 23:28:46 -07001332
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001333 /* retry all partial writes. retry partial reads as long as its a
1334 * regular file. */
Zach Brown353fb072005-09-30 11:58:56 -07001335 } while (ret > 0 && iocb->ki_left > 0 &&
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001336 (opcode == IOCB_CMD_PWRITEV ||
1337 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 /* This means we must have transferred all that we could */
1340 /* No need to retry anymore */
1341 if ((ret == 0) || (iocb->ki_left == 0))
1342 ret = iocb->ki_nbytes - iocb->ki_left;
1343
1344 return ret;
1345}
1346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347static ssize_t aio_fdsync(struct kiocb *iocb)
1348{
1349 struct file *file = iocb->ki_filp;
1350 ssize_t ret = -EINVAL;
1351
1352 if (file->f_op->aio_fsync)
1353 ret = file->f_op->aio_fsync(iocb, 1);
1354 return ret;
1355}
1356
1357static ssize_t aio_fsync(struct kiocb *iocb)
1358{
1359 struct file *file = iocb->ki_filp;
1360 ssize_t ret = -EINVAL;
1361
1362 if (file->f_op->aio_fsync)
1363 ret = file->f_op->aio_fsync(iocb, 0);
1364 return ret;
1365}
1366
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001367static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
1368{
1369 ssize_t ret;
1370
1371 ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
1372 kiocb->ki_nbytes, 1,
1373 &kiocb->ki_inline_vec, &kiocb->ki_iovec);
1374 if (ret < 0)
1375 goto out;
1376
1377 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1378 kiocb->ki_cur_seg = 0;
1379 /* ki_nbytes/left now reflect bytes instead of segs */
1380 kiocb->ki_nbytes = ret;
1381 kiocb->ki_left = ret;
1382
1383 ret = 0;
1384out:
1385 return ret;
1386}
1387
1388static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
1389{
1390 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1391 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
1392 kiocb->ki_iovec->iov_len = kiocb->ki_left;
1393 kiocb->ki_nr_segs = 1;
1394 kiocb->ki_cur_seg = 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001395 return 0;
1396}
1397
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398/*
1399 * aio_setup_iocb:
1400 * Performs the initial checks and aio retry method
1401 * setup for the kiocb at the time of io submission.
1402 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001403static ssize_t aio_setup_iocb(struct kiocb *kiocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 struct file *file = kiocb->ki_filp;
1406 ssize_t ret = 0;
1407
1408 switch (kiocb->ki_opcode) {
1409 case IOCB_CMD_PREAD:
1410 ret = -EBADF;
1411 if (unlikely(!(file->f_mode & FMODE_READ)))
1412 break;
1413 ret = -EFAULT;
1414 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1415 kiocb->ki_left)))
1416 break;
Kostik Belousov8766ce42005-10-23 12:57:13 -07001417 ret = security_file_permission(file, MAY_READ);
1418 if (unlikely(ret))
1419 break;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001420 ret = aio_setup_single_vector(kiocb);
1421 if (ret)
1422 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 ret = -EINVAL;
1424 if (file->f_op->aio_read)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001425 kiocb->ki_retry = aio_rw_vect_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 case IOCB_CMD_PWRITE:
1428 ret = -EBADF;
1429 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1430 break;
1431 ret = -EFAULT;
1432 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1433 kiocb->ki_left)))
1434 break;
Kostik Belousov8766ce42005-10-23 12:57:13 -07001435 ret = security_file_permission(file, MAY_WRITE);
1436 if (unlikely(ret))
1437 break;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001438 ret = aio_setup_single_vector(kiocb);
1439 if (ret)
1440 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 ret = -EINVAL;
1442 if (file->f_op->aio_write)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001443 kiocb->ki_retry = aio_rw_vect_retry;
1444 break;
1445 case IOCB_CMD_PREADV:
1446 ret = -EBADF;
1447 if (unlikely(!(file->f_mode & FMODE_READ)))
1448 break;
1449 ret = security_file_permission(file, MAY_READ);
1450 if (unlikely(ret))
1451 break;
1452 ret = aio_setup_vectored_rw(READ, kiocb);
1453 if (ret)
1454 break;
1455 ret = -EINVAL;
1456 if (file->f_op->aio_read)
1457 kiocb->ki_retry = aio_rw_vect_retry;
1458 break;
1459 case IOCB_CMD_PWRITEV:
1460 ret = -EBADF;
1461 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1462 break;
1463 ret = security_file_permission(file, MAY_WRITE);
1464 if (unlikely(ret))
1465 break;
1466 ret = aio_setup_vectored_rw(WRITE, kiocb);
1467 if (ret)
1468 break;
1469 ret = -EINVAL;
1470 if (file->f_op->aio_write)
1471 kiocb->ki_retry = aio_rw_vect_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 break;
1473 case IOCB_CMD_FDSYNC:
1474 ret = -EINVAL;
1475 if (file->f_op->aio_fsync)
1476 kiocb->ki_retry = aio_fdsync;
1477 break;
1478 case IOCB_CMD_FSYNC:
1479 ret = -EINVAL;
1480 if (file->f_op->aio_fsync)
1481 kiocb->ki_retry = aio_fsync;
1482 break;
1483 default:
1484 dprintk("EINVAL: io_submit: no operation provided\n");
1485 ret = -EINVAL;
1486 }
1487
1488 if (!kiocb->ki_retry)
1489 return ret;
1490
1491 return 0;
1492}
1493
1494/*
1495 * aio_wake_function:
1496 * wait queue callback function for aio notification,
1497 * Simply triggers a retry of the operation via kick_iocb.
1498 *
1499 * This callback is specified in the wait queue entry in
1500 * a kiocb (current->io_wait points to this wait queue
1501 * entry when an aio operation executes; it is used
1502 * instead of a synchronous wait when an i/o blocking
1503 * condition is encountered during aio).
1504 *
1505 * Note:
1506 * This routine is executed with the wait queue lock held.
1507 * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
1508 * the ioctx lock inside the wait queue lock. This is safe
1509 * because this callback isn't used for wait queues which
1510 * are nested inside ioctx lock (i.e. ctx->wait)
1511 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001512static int aio_wake_function(wait_queue_t *wait, unsigned mode,
1513 int sync, void *key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
1516
1517 list_del_init(&wait->task_list);
1518 kick_iocb(iocb);
1519 return 1;
1520}
1521
1522int fastcall io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1523 struct iocb *iocb)
1524{
1525 struct kiocb *req;
1526 struct file *file;
1527 ssize_t ret;
1528
1529 /* enforce forwards compatibility on users */
1530 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2 ||
1531 iocb->aio_reserved3)) {
1532 pr_debug("EINVAL: io_submit: reserve field set\n");
1533 return -EINVAL;
1534 }
1535
1536 /* prevent overflows */
1537 if (unlikely(
1538 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1539 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1540 ((ssize_t)iocb->aio_nbytes < 0)
1541 )) {
1542 pr_debug("EINVAL: io_submit: overflow check\n");
1543 return -EINVAL;
1544 }
1545
1546 file = fget(iocb->aio_fildes);
1547 if (unlikely(!file))
1548 return -EBADF;
1549
1550 req = aio_get_req(ctx); /* returns with 2 references to req */
1551 if (unlikely(!req)) {
1552 fput(file);
1553 return -EAGAIN;
1554 }
1555
1556 req->ki_filp = file;
Ken Chen212079c2005-05-01 08:59:15 -07001557 ret = put_user(req->ki_key, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (unlikely(ret)) {
1559 dprintk("EFAULT: aio_key\n");
1560 goto out_put_req;
1561 }
1562
1563 req->ki_obj.user = user_iocb;
1564 req->ki_user_data = iocb->aio_data;
1565 req->ki_pos = iocb->aio_offset;
1566
1567 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1568 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1569 req->ki_opcode = iocb->aio_lio_opcode;
1570 init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
1571 INIT_LIST_HEAD(&req->ki_wait.task_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
1573 ret = aio_setup_iocb(req);
1574
1575 if (ret)
1576 goto out_put_req;
1577
1578 spin_lock_irq(&ctx->ctx_lock);
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001579 aio_run_iocb(req);
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001580 if (!list_empty(&ctx->run_list)) {
Ken Chen954d3e92005-05-01 08:59:16 -07001581 /* drain the run list */
1582 while (__aio_run_iocbs(ctx))
1583 ;
1584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 spin_unlock_irq(&ctx->ctx_lock);
1586 aio_put_req(req); /* drop extra ref to req */
1587 return 0;
1588
1589out_put_req:
1590 aio_put_req(req); /* drop extra ref to req */
1591 aio_put_req(req); /* drop i/o ref to req */
1592 return ret;
1593}
1594
1595/* sys_io_submit:
1596 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1597 * the number of iocbs queued. May return -EINVAL if the aio_context
1598 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1599 * *iocbpp[0] is not properly initialized, if the operation specified
1600 * is invalid for the file descriptor in the iocb. May fail with
1601 * -EFAULT if any of the data structures point to invalid data. May
1602 * fail with -EBADF if the file descriptor specified in the first
1603 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1604 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1605 * fail with -ENOSYS if not implemented.
1606 */
1607asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
1608 struct iocb __user * __user *iocbpp)
1609{
1610 struct kioctx *ctx;
1611 long ret = 0;
1612 int i;
1613
1614 if (unlikely(nr < 0))
1615 return -EINVAL;
1616
1617 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1618 return -EFAULT;
1619
1620 ctx = lookup_ioctx(ctx_id);
1621 if (unlikely(!ctx)) {
1622 pr_debug("EINVAL: io_submit: invalid context id\n");
1623 return -EINVAL;
1624 }
1625
1626 /*
1627 * AKPM: should this return a partial result if some of the IOs were
1628 * successfully submitted?
1629 */
1630 for (i=0; i<nr; i++) {
1631 struct iocb __user *user_iocb;
1632 struct iocb tmp;
1633
1634 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1635 ret = -EFAULT;
1636 break;
1637 }
1638
1639 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1640 ret = -EFAULT;
1641 break;
1642 }
1643
1644 ret = io_submit_one(ctx, user_iocb, &tmp);
1645 if (ret)
1646 break;
1647 }
1648
1649 put_ioctx(ctx);
1650 return i ? i : ret;
1651}
1652
1653/* lookup_kiocb
1654 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001656static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1657 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658{
1659 struct list_head *pos;
Zach Brownd00689a2005-11-13 16:07:34 -08001660
1661 assert_spin_locked(&ctx->ctx_lock);
1662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* TODO: use a hash or array, this sucks. */
1664 list_for_each(pos, &ctx->active_reqs) {
1665 struct kiocb *kiocb = list_kiocb(pos);
1666 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1667 return kiocb;
1668 }
1669 return NULL;
1670}
1671
1672/* sys_io_cancel:
1673 * Attempts to cancel an iocb previously passed to io_submit. If
1674 * the operation is successfully cancelled, the resulting event is
1675 * copied into the memory pointed to by result without being placed
1676 * into the completion queue and 0 is returned. May fail with
1677 * -EFAULT if any of the data structures pointed to are invalid.
1678 * May fail with -EINVAL if aio_context specified by ctx_id is
1679 * invalid. May fail with -EAGAIN if the iocb specified was not
1680 * cancelled. Will fail with -ENOSYS if not implemented.
1681 */
1682asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
1683 struct io_event __user *result)
1684{
1685 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1686 struct kioctx *ctx;
1687 struct kiocb *kiocb;
1688 u32 key;
1689 int ret;
1690
1691 ret = get_user(key, &iocb->aio_key);
1692 if (unlikely(ret))
1693 return -EFAULT;
1694
1695 ctx = lookup_ioctx(ctx_id);
1696 if (unlikely(!ctx))
1697 return -EINVAL;
1698
1699 spin_lock_irq(&ctx->ctx_lock);
1700 ret = -EAGAIN;
1701 kiocb = lookup_kiocb(ctx, iocb, key);
1702 if (kiocb && kiocb->ki_cancel) {
1703 cancel = kiocb->ki_cancel;
1704 kiocb->ki_users ++;
1705 kiocbSetCancelled(kiocb);
1706 } else
1707 cancel = NULL;
1708 spin_unlock_irq(&ctx->ctx_lock);
1709
1710 if (NULL != cancel) {
1711 struct io_event tmp;
1712 pr_debug("calling cancel\n");
1713 memset(&tmp, 0, sizeof(tmp));
1714 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1715 tmp.data = kiocb->ki_user_data;
1716 ret = cancel(kiocb, &tmp);
1717 if (!ret) {
1718 /* Cancellation succeeded -- copy the result
1719 * into the user's buffer.
1720 */
1721 if (copy_to_user(result, &tmp, sizeof(tmp)))
1722 ret = -EFAULT;
1723 }
1724 } else
Wendy Cheng8f582022005-09-09 13:02:08 -07001725 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 put_ioctx(ctx);
1728
1729 return ret;
1730}
1731
1732/* io_getevents:
1733 * Attempts to read at least min_nr events and up to nr events from
1734 * the completion queue for the aio_context specified by ctx_id. May
1735 * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
1736 * if nr is out of range, if when is out of range. May fail with
1737 * -EFAULT if any of the memory specified to is invalid. May return
1738 * 0 or < min_nr if no events are available and the timeout specified
1739 * by when has elapsed, where when == NULL specifies an infinite
1740 * timeout. Note that the timeout pointed to by when is relative and
1741 * will be updated if not NULL and the operation blocks. Will fail
1742 * with -ENOSYS if not implemented.
1743 */
1744asmlinkage long sys_io_getevents(aio_context_t ctx_id,
1745 long min_nr,
1746 long nr,
1747 struct io_event __user *events,
1748 struct timespec __user *timeout)
1749{
1750 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1751 long ret = -EINVAL;
1752
1753 if (likely(ioctx)) {
1754 if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
1755 ret = read_events(ioctx, min_nr, nr, events, timeout);
1756 put_ioctx(ioctx);
1757 }
1758
1759 return ret;
1760}
1761
1762__initcall(aio_setup);
1763
1764EXPORT_SYMBOL(aio_complete);
1765EXPORT_SYMBOL(aio_put_req);
1766EXPORT_SYMBOL(wait_on_sync_kiocb);