]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/aio.c
aio: kill struct aio_ring_info
[karo-tx-linux.git] / fs / aio.c
1 /*
2  *      An async IO implementation for Linux
3  *      Written by Benjamin LaHaise <bcrl@kvack.org>
4  *
5  *      Implements an efficient asynchronous io interface.
6  *
7  *      Copyright 2000, 2001, 2002 Red Hat, Inc.  All Rights Reserved.
8  *
9  *      See ../COPYING for licensing terms.
10  */
11 #define pr_fmt(fmt) "%s: " fmt, __func__
12
13 #include <linux/kernel.h>
14 #include <linux/init.h>
15 #include <linux/errno.h>
16 #include <linux/time.h>
17 #include <linux/aio_abi.h>
18 #include <linux/export.h>
19 #include <linux/syscalls.h>
20 #include <linux/backing-dev.h>
21 #include <linux/uio.h>
22
23 #include <linux/sched.h>
24 #include <linux/fs.h>
25 #include <linux/file.h>
26 #include <linux/mm.h>
27 #include <linux/mman.h>
28 #include <linux/mmu_context.h>
29 #include <linux/slab.h>
30 #include <linux/timer.h>
31 #include <linux/aio.h>
32 #include <linux/highmem.h>
33 #include <linux/workqueue.h>
34 #include <linux/security.h>
35 #include <linux/eventfd.h>
36 #include <linux/blkdev.h>
37 #include <linux/compat.h>
38
39 #include <asm/kmap_types.h>
40 #include <asm/uaccess.h>
41
42 #define AIO_RING_MAGIC                  0xa10a10a1
43 #define AIO_RING_COMPAT_FEATURES        1
44 #define AIO_RING_INCOMPAT_FEATURES      0
45 struct aio_ring {
46         unsigned        id;     /* kernel internal index number */
47         unsigned        nr;     /* number of io_events */
48         unsigned        head;
49         unsigned        tail;
50
51         unsigned        magic;
52         unsigned        compat_features;
53         unsigned        incompat_features;
54         unsigned        header_length;  /* size of aio_ring */
55
56
57         struct io_event         io_events[0];
58 }; /* 128 bytes + ring size */
59
60 #define AIO_RING_PAGES  8
61
62 struct kioctx {
63         atomic_t                users;
64         atomic_t                dead;
65
66         /* This needs improving */
67         unsigned long           user_id;
68         struct hlist_node       list;
69
70         wait_queue_head_t       wait;
71
72         spinlock_t              ctx_lock;
73
74         atomic_t                reqs_active;
75         struct list_head        active_reqs;    /* used for cancellation */
76
77         unsigned                nr;
78
79         /* sys_io_setup currently limits this to an unsigned int */
80         unsigned                max_reqs;
81
82         unsigned long           mmap_base;
83         unsigned long           mmap_size;
84
85         struct page             **ring_pages;
86         long                    nr_pages;
87
88         struct {
89                 struct mutex    ring_lock;
90         } ____cacheline_aligned;
91
92         struct {
93                 unsigned        tail;
94                 spinlock_t      completion_lock;
95         } ____cacheline_aligned;
96
97         struct page             *internal_pages[AIO_RING_PAGES];
98
99         struct rcu_head         rcu_head;
100         struct work_struct      rcu_work;
101 };
102
103 /*------ sysctl variables----*/
104 static DEFINE_SPINLOCK(aio_nr_lock);
105 unsigned long aio_nr;           /* current system wide number of aio requests */
106 unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
107 /*----end sysctl variables---*/
108
109 static struct kmem_cache        *kiocb_cachep;
110 static struct kmem_cache        *kioctx_cachep;
111
112 /* aio_setup
113  *      Creates the slab caches used by the aio routines, panic on
114  *      failure as this is done early during the boot sequence.
115  */
116 static int __init aio_setup(void)
117 {
118         kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
119         kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
120
121         pr_debug("sizeof(struct page) = %zu\n", sizeof(struct page));
122
123         return 0;
124 }
125 __initcall(aio_setup);
126
127 static void aio_free_ring(struct kioctx *ctx)
128 {
129         long i;
130
131         for (i = 0; i < ctx->nr_pages; i++)
132                 put_page(ctx->ring_pages[i]);
133
134         if (ctx->mmap_size)
135                 vm_munmap(ctx->mmap_base, ctx->mmap_size);
136
137         if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages)
138                 kfree(ctx->ring_pages);
139 }
140
141 static int aio_setup_ring(struct kioctx *ctx)
142 {
143         struct aio_ring *ring;
144         unsigned nr_events = ctx->max_reqs;
145         struct mm_struct *mm = current->mm;
146         unsigned long size, populate;
147         int nr_pages;
148
149         /* Compensate for the ring buffer's head/tail overlap entry */
150         nr_events += 2; /* 1 is required, 2 for good luck */
151
152         size = sizeof(struct aio_ring);
153         size += sizeof(struct io_event) * nr_events;
154         nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
155
156         if (nr_pages < 0)
157                 return -EINVAL;
158
159         nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
160
161         ctx->nr = 0;
162         ctx->ring_pages = ctx->internal_pages;
163         if (nr_pages > AIO_RING_PAGES) {
164                 ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
165                                           GFP_KERNEL);
166                 if (!ctx->ring_pages)
167                         return -ENOMEM;
168         }
169
170         ctx->mmap_size = nr_pages * PAGE_SIZE;
171         pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
172         down_write(&mm->mmap_sem);
173         ctx->mmap_base = do_mmap_pgoff(NULL, 0, ctx->mmap_size,
174                                        PROT_READ|PROT_WRITE,
175                                        MAP_ANONYMOUS|MAP_PRIVATE, 0, &populate);
176         if (IS_ERR((void *)ctx->mmap_base)) {
177                 up_write(&mm->mmap_sem);
178                 ctx->mmap_size = 0;
179                 aio_free_ring(ctx);
180                 return -EAGAIN;
181         }
182
183         pr_debug("mmap address: 0x%08lx\n", ctx->mmap_base);
184         ctx->nr_pages = get_user_pages(current, mm, ctx->mmap_base, nr_pages,
185                                        1, 0, ctx->ring_pages, NULL);
186         up_write(&mm->mmap_sem);
187
188         if (unlikely(ctx->nr_pages != nr_pages)) {
189                 aio_free_ring(ctx);
190                 return -EAGAIN;
191         }
192         if (populate)
193                 mm_populate(ctx->mmap_base, populate);
194
195         ctx->user_id = ctx->mmap_base;
196         ctx->nr = nr_events;            /* trusted copy */
197
198         ring = kmap_atomic(ctx->ring_pages[0]);
199         ring->nr = nr_events;   /* user copy */
200         ring->id = ctx->user_id;
201         ring->head = ring->tail = 0;
202         ring->magic = AIO_RING_MAGIC;
203         ring->compat_features = AIO_RING_COMPAT_FEATURES;
204         ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
205         ring->header_length = sizeof(struct aio_ring);
206         kunmap_atomic(ring);
207         flush_dcache_page(ctx->ring_pages[0]);
208
209         return 0;
210 }
211
212 #define AIO_EVENTS_PER_PAGE     (PAGE_SIZE / sizeof(struct io_event))
213 #define AIO_EVENTS_FIRST_PAGE   ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
214 #define AIO_EVENTS_OFFSET       (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
215
216 void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel)
217 {
218         struct kioctx *ctx = req->ki_ctx;
219         unsigned long flags;
220
221         spin_lock_irqsave(&ctx->ctx_lock, flags);
222
223         if (!req->ki_list.next)
224                 list_add(&req->ki_list, &ctx->active_reqs);
225
226         req->ki_cancel = cancel;
227
228         spin_unlock_irqrestore(&ctx->ctx_lock, flags);
229 }
230 EXPORT_SYMBOL(kiocb_set_cancel_fn);
231
232 static int kiocb_cancel(struct kioctx *ctx, struct kiocb *kiocb,
233                         struct io_event *res)
234 {
235         kiocb_cancel_fn *old, *cancel;
236         int ret = -EINVAL;
237
238         /*
239          * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
240          * actually has a cancel function, hence the cmpxchg()
241          */
242
243         cancel = ACCESS_ONCE(kiocb->ki_cancel);
244         do {
245                 if (!cancel || cancel == KIOCB_CANCELLED)
246                         return ret;
247
248                 old = cancel;
249                 cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
250         } while (cancel != old);
251
252         atomic_inc(&kiocb->ki_users);
253         spin_unlock_irq(&ctx->ctx_lock);
254
255         memset(res, 0, sizeof(*res));
256         res->obj = (u64)(unsigned long)kiocb->ki_obj.user;
257         res->data = kiocb->ki_user_data;
258         ret = cancel(kiocb, res);
259
260         spin_lock_irq(&ctx->ctx_lock);
261
262         return ret;
263 }
264
265 static void free_ioctx_rcu(struct rcu_head *head)
266 {
267         struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
268         kmem_cache_free(kioctx_cachep, ctx);
269 }
270
271 /*
272  * When this function runs, the kioctx has been removed from the "hash table"
273  * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
274  * now it's safe to cancel any that need to be.
275  */
276 static void free_ioctx(struct kioctx *ctx)
277 {
278         struct aio_ring *ring;
279         struct io_event res;
280         struct kiocb *req;
281         unsigned head, avail;
282
283         spin_lock_irq(&ctx->ctx_lock);
284
285         while (!list_empty(&ctx->active_reqs)) {
286                 req = list_first_entry(&ctx->active_reqs,
287                                        struct kiocb, ki_list);
288
289                 list_del_init(&req->ki_list);
290                 kiocb_cancel(ctx, req, &res);
291         }
292
293         spin_unlock_irq(&ctx->ctx_lock);
294
295         ring = kmap_atomic(ctx->ring_pages[0]);
296         head = ring->head;
297         kunmap_atomic(ring);
298
299         while (atomic_read(&ctx->reqs_active) > 0) {
300                 wait_event(ctx->wait, head != ctx->tail);
301
302                 avail = (head < ctx->tail ? ctx->tail : ctx->nr) - head;
303
304                 atomic_sub(avail, &ctx->reqs_active);
305                 head += avail;
306                 head %= ctx->nr;
307         }
308
309         WARN_ON(atomic_read(&ctx->reqs_active) < 0);
310
311         aio_free_ring(ctx);
312
313         spin_lock(&aio_nr_lock);
314         BUG_ON(aio_nr - ctx->max_reqs > aio_nr);
315         aio_nr -= ctx->max_reqs;
316         spin_unlock(&aio_nr_lock);
317
318         pr_debug("freeing %p\n", ctx);
319
320         /*
321          * Here the call_rcu() is between the wait_event() for reqs_active to
322          * hit 0, and freeing the ioctx.
323          *
324          * aio_complete() decrements reqs_active, but it has to touch the ioctx
325          * after to issue a wakeup so we use rcu.
326          */
327         call_rcu(&ctx->rcu_head, free_ioctx_rcu);
328 }
329
330 static void put_ioctx(struct kioctx *ctx)
331 {
332         if (unlikely(atomic_dec_and_test(&ctx->users)))
333                 free_ioctx(ctx);
334 }
335
336 /* ioctx_alloc
337  *      Allocates and initializes an ioctx.  Returns an ERR_PTR if it failed.
338  */
339 static struct kioctx *ioctx_alloc(unsigned nr_events)
340 {
341         struct mm_struct *mm = current->mm;
342         struct kioctx *ctx;
343         int err = -ENOMEM;
344
345         /* Prevent overflows */
346         if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
347             (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
348                 pr_debug("ENOMEM: nr_events too high\n");
349                 return ERR_PTR(-EINVAL);
350         }
351
352         if (!nr_events || (unsigned long)nr_events > aio_max_nr)
353                 return ERR_PTR(-EAGAIN);
354
355         ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
356         if (!ctx)
357                 return ERR_PTR(-ENOMEM);
358
359         ctx->max_reqs = nr_events;
360
361         atomic_set(&ctx->users, 2);
362         atomic_set(&ctx->dead, 0);
363         spin_lock_init(&ctx->ctx_lock);
364         spin_lock_init(&ctx->completion_lock);
365         mutex_init(&ctx->ring_lock);
366         init_waitqueue_head(&ctx->wait);
367
368         INIT_LIST_HEAD(&ctx->active_reqs);
369
370         if (aio_setup_ring(ctx) < 0)
371                 goto out_freectx;
372
373         /* limit the number of system wide aios */
374         spin_lock(&aio_nr_lock);
375         if (aio_nr + nr_events > aio_max_nr ||
376             aio_nr + nr_events < aio_nr) {
377                 spin_unlock(&aio_nr_lock);
378                 goto out_cleanup;
379         }
380         aio_nr += ctx->max_reqs;
381         spin_unlock(&aio_nr_lock);
382
383         /* now link into global list. */
384         spin_lock(&mm->ioctx_lock);
385         hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
386         spin_unlock(&mm->ioctx_lock);
387
388         pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
389                  ctx, ctx->user_id, mm, ctx->nr);
390         return ctx;
391
392 out_cleanup:
393         err = -EAGAIN;
394         aio_free_ring(ctx);
395 out_freectx:
396         kmem_cache_free(kioctx_cachep, ctx);
397         pr_debug("error allocating ioctx %d\n", err);
398         return ERR_PTR(err);
399 }
400
401 static void kill_ioctx_work(struct work_struct *work)
402 {
403         struct kioctx *ctx = container_of(work, struct kioctx, rcu_work);
404
405         wake_up_all(&ctx->wait);
406         put_ioctx(ctx);
407 }
408
409 static void kill_ioctx_rcu(struct rcu_head *head)
410 {
411         struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
412
413         INIT_WORK(&ctx->rcu_work, kill_ioctx_work);
414         schedule_work(&ctx->rcu_work);
415 }
416
417 /* kill_ioctx
418  *      Cancels all outstanding aio requests on an aio context.  Used
419  *      when the processes owning a context have all exited to encourage
420  *      the rapid destruction of the kioctx.
421  */
422 static void kill_ioctx(struct kioctx *ctx)
423 {
424         if (!atomic_xchg(&ctx->dead, 1)) {
425                 hlist_del_rcu(&ctx->list);
426                 /* Between hlist_del_rcu() and dropping the initial ref */
427                 synchronize_rcu();
428
429                 /*
430                  * We can't punt to workqueue here because put_ioctx() ->
431                  * free_ioctx() will unmap the ringbuffer, and that has to be
432                  * done in the original process's context. kill_ioctx_rcu/work()
433                  * exist for exit_aio(), as in that path free_ioctx() won't do
434                  * the unmap.
435                  */
436                 kill_ioctx_work(&ctx->rcu_work);
437         }
438 }
439
440 /* wait_on_sync_kiocb:
441  *      Waits on the given sync kiocb to complete.
442  */
443 ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
444 {
445         while (atomic_read(&iocb->ki_users)) {
446                 set_current_state(TASK_UNINTERRUPTIBLE);
447                 if (!atomic_read(&iocb->ki_users))
448                         break;
449                 io_schedule();
450         }
451         __set_current_state(TASK_RUNNING);
452         return iocb->ki_user_data;
453 }
454 EXPORT_SYMBOL(wait_on_sync_kiocb);
455
456 /*
457  * exit_aio: called when the last user of mm goes away.  At this point, there is
458  * no way for any new requests to be submited or any of the io_* syscalls to be
459  * called on the context.
460  *
461  * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
462  * them.
463  */
464 void exit_aio(struct mm_struct *mm)
465 {
466         struct kioctx *ctx;
467         struct hlist_node *n;
468
469         hlist_for_each_entry_safe(ctx, n, &mm->ioctx_list, list) {
470                 if (1 != atomic_read(&ctx->users))
471                         printk(KERN_DEBUG
472                                 "exit_aio:ioctx still alive: %d %d %d\n",
473                                 atomic_read(&ctx->users),
474                                 atomic_read(&ctx->dead),
475                                 atomic_read(&ctx->reqs_active));
476                 /*
477                  * We don't need to bother with munmap() here -
478                  * exit_mmap(mm) is coming and it'll unmap everything.
479                  * Since aio_free_ring() uses non-zero ->mmap_size
480                  * as indicator that it needs to unmap the area,
481                  * just set it to 0; aio_free_ring() is the only
482                  * place that uses ->mmap_size, so it's safe.
483                  */
484                 ctx->mmap_size = 0;
485
486                 if (!atomic_xchg(&ctx->dead, 1)) {
487                         hlist_del_rcu(&ctx->list);
488                         call_rcu(&ctx->rcu_head, kill_ioctx_rcu);
489                 }
490         }
491 }
492
493 /* aio_get_req
494  *      Allocate a slot for an aio request.  Increments the ki_users count
495  * of the kioctx so that the kioctx stays around until all requests are
496  * complete.  Returns NULL if no requests are free.
497  *
498  * Returns with kiocb->ki_users set to 2.  The io submit code path holds
499  * an extra reference while submitting the i/o.
500  * This prevents races between the aio code path referencing the
501  * req (after submitting it) and aio_complete() freeing the req.
502  */
503 static inline struct kiocb *aio_get_req(struct kioctx *ctx)
504 {
505         struct kiocb *req;
506
507         if (atomic_read(&ctx->reqs_active) >= ctx->nr)
508                 return NULL;
509
510         if (atomic_inc_return(&ctx->reqs_active) > ctx->nr)
511                 goto out_put;
512
513         req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
514         if (unlikely(!req))
515                 goto out_put;
516
517         atomic_set(&req->ki_users, 2);
518         req->ki_ctx = ctx;
519
520         return req;
521 out_put:
522         atomic_dec(&ctx->reqs_active);
523         return NULL;
524 }
525
526 static void kiocb_free(struct kiocb *req)
527 {
528         if (req->ki_filp)
529                 fput(req->ki_filp);
530         if (req->ki_eventfd != NULL)
531                 eventfd_ctx_put(req->ki_eventfd);
532         if (req->ki_dtor)
533                 req->ki_dtor(req);
534         if (req->ki_iovec != &req->ki_inline_vec)
535                 kfree(req->ki_iovec);
536         kmem_cache_free(kiocb_cachep, req);
537 }
538
539 void aio_put_req(struct kiocb *req)
540 {
541         if (atomic_dec_and_test(&req->ki_users))
542                 kiocb_free(req);
543 }
544 EXPORT_SYMBOL(aio_put_req);
545
546 static struct kioctx *lookup_ioctx(unsigned long ctx_id)
547 {
548         struct mm_struct *mm = current->mm;
549         struct kioctx *ctx, *ret = NULL;
550
551         rcu_read_lock();
552
553         hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
554                 if (ctx->user_id == ctx_id){
555                         atomic_inc(&ctx->users);
556                         ret = ctx;
557                         break;
558                 }
559         }
560
561         rcu_read_unlock();
562         return ret;
563 }
564
565 /* aio_complete
566  *      Called when the io request on the given iocb is complete.
567  */
568 void aio_complete(struct kiocb *iocb, long res, long res2)
569 {
570         struct kioctx   *ctx = iocb->ki_ctx;
571         struct aio_ring *ring;
572         struct io_event *ev_page, *event;
573         unsigned long   flags;
574         unsigned tail, pos;
575
576         /*
577          * Special case handling for sync iocbs:
578          *  - events go directly into the iocb for fast handling
579          *  - the sync task with the iocb in its stack holds the single iocb
580          *    ref, no other paths have a way to get another ref
581          *  - the sync task helpfully left a reference to itself in the iocb
582          */
583         if (is_sync_kiocb(iocb)) {
584                 BUG_ON(atomic_read(&iocb->ki_users) != 1);
585                 iocb->ki_user_data = res;
586                 atomic_set(&iocb->ki_users, 0);
587                 wake_up_process(iocb->ki_obj.tsk);
588                 return;
589         }
590
591         /*
592          * Take rcu_read_lock() in case the kioctx is being destroyed, as we
593          * need to issue a wakeup after decrementing reqs_active.
594          */
595         rcu_read_lock();
596
597         if (iocb->ki_list.next) {
598                 unsigned long flags;
599
600                 spin_lock_irqsave(&ctx->ctx_lock, flags);
601                 list_del(&iocb->ki_list);
602                 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
603         }
604
605         /*
606          * cancelled requests don't get events, userland was given one
607          * when the event got cancelled.
608          */
609         if (unlikely(xchg(&iocb->ki_cancel,
610                           KIOCB_CANCELLED) == KIOCB_CANCELLED)) {
611                 atomic_dec(&ctx->reqs_active);
612                 /* Still need the wake_up in case free_ioctx is waiting */
613                 goto put_rq;
614         }
615
616         /*
617          * Add a completion event to the ring buffer. Must be done holding
618          * ctx->ctx_lock to prevent other code from messing with the tail
619          * pointer since we might be called from irq context.
620          */
621         spin_lock_irqsave(&ctx->completion_lock, flags);
622
623         tail = ctx->tail;
624         pos = tail + AIO_EVENTS_OFFSET;
625
626         if (++tail >= ctx->nr)
627                 tail = 0;
628
629         ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
630         event = ev_page + pos % AIO_EVENTS_PER_PAGE;
631
632         event->obj = (u64)(unsigned long)iocb->ki_obj.user;
633         event->data = iocb->ki_user_data;
634         event->res = res;
635         event->res2 = res2;
636
637         kunmap_atomic(ev_page);
638         flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
639
640         pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
641                  ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
642                  res, res2);
643
644         /* after flagging the request as done, we
645          * must never even look at it again
646          */
647         smp_wmb();      /* make event visible before updating tail */
648
649         ctx->tail = tail;
650
651         ring = kmap_atomic(ctx->ring_pages[0]);
652         ring->tail = tail;
653         kunmap_atomic(ring);
654         flush_dcache_page(ctx->ring_pages[0]);
655
656         spin_unlock_irqrestore(&ctx->completion_lock, flags);
657
658         pr_debug("added to ring %p at [%u]\n", iocb, tail);
659
660         /*
661          * Check if the user asked us to deliver the result through an
662          * eventfd. The eventfd_signal() function is safe to be called
663          * from IRQ context.
664          */
665         if (iocb->ki_eventfd != NULL)
666                 eventfd_signal(iocb->ki_eventfd, 1);
667
668 put_rq:
669         /* everything turned out well, dispose of the aiocb. */
670         aio_put_req(iocb);
671
672         /*
673          * We have to order our ring_info tail store above and test
674          * of the wait list below outside the wait lock.  This is
675          * like in wake_up_bit() where clearing a bit has to be
676          * ordered with the unlocked test.
677          */
678         smp_mb();
679
680         if (waitqueue_active(&ctx->wait))
681                 wake_up(&ctx->wait);
682
683         rcu_read_unlock();
684 }
685 EXPORT_SYMBOL(aio_complete);
686
687 /* aio_read_events
688  *      Pull an event off of the ioctx's event ring.  Returns the number of
689  *      events fetched
690  */
691 static int aio_read_events_ring(struct kioctx *ctx,
692                                 struct io_event __user *event, long nr)
693 {
694         struct aio_ring *ring;
695         unsigned head, pos;
696         int ret = 0, copy_ret;
697
698         if (!mutex_trylock(&ctx->ring_lock)) {
699                 __set_current_state(TASK_RUNNING);
700                 mutex_lock(&ctx->ring_lock);
701         }
702
703         ring = kmap_atomic(ctx->ring_pages[0]);
704         head = ring->head;
705         kunmap_atomic(ring);
706
707         pr_debug("h%u t%u m%u\n", head, ctx->tail, ctx->nr);
708
709         if (head == ctx->tail)
710                 goto out;
711
712         __set_current_state(TASK_RUNNING);
713
714         while (ret < nr) {
715                 unsigned i = (head < ctx->tail ? ctx->tail : ctx->nr) - head;
716                 struct io_event *ev;
717                 struct page *page;
718
719                 if (head == ctx->tail)
720                         break;
721
722                 i = min_t(int, i, nr - ret);
723                 i = min_t(int, i, AIO_EVENTS_PER_PAGE -
724                           ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
725
726                 pos = head + AIO_EVENTS_OFFSET;
727                 page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
728                 pos %= AIO_EVENTS_PER_PAGE;
729
730                 ev = kmap(page);
731                 copy_ret = copy_to_user(event + ret, ev + pos, sizeof(*ev) * i);
732                 kunmap(page);
733
734                 if (unlikely(copy_ret)) {
735                         ret = -EFAULT;
736                         goto out;
737                 }
738
739                 ret += i;
740                 head += i;
741                 head %= ctx->nr;
742         }
743
744         ring = kmap_atomic(ctx->ring_pages[0]);
745         ring->head = head;
746         kunmap_atomic(ring);
747         flush_dcache_page(ctx->ring_pages[0]);
748
749         pr_debug("%d  h%u t%u\n", ret, head, ctx->tail);
750
751         atomic_sub(ret, &ctx->reqs_active);
752 out:
753         mutex_unlock(&ctx->ring_lock);
754
755         return ret;
756 }
757
758 static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
759                             struct io_event __user *event, long *i)
760 {
761         long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
762
763         if (ret > 0)
764                 *i += ret;
765
766         if (unlikely(atomic_read(&ctx->dead)))
767                 ret = -EINVAL;
768
769         if (!*i)
770                 *i = ret;
771
772         return ret < 0 || *i >= min_nr;
773 }
774
775 static long read_events(struct kioctx *ctx, long min_nr, long nr,
776                         struct io_event __user *event,
777                         struct timespec __user *timeout)
778 {
779         ktime_t until = { .tv64 = KTIME_MAX };
780         long ret = 0;
781
782         if (timeout) {
783                 struct timespec ts;
784
785                 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
786                         return -EFAULT;
787
788                 until = timespec_to_ktime(ts);
789         }
790
791         wait_event_interruptible_hrtimeout(ctx->wait,
792                         aio_read_events(ctx, min_nr, nr, event, &ret), until);
793
794         if (!ret && signal_pending(current))
795                 ret = -EINTR;
796
797         return ret;
798 }
799
800 /* sys_io_setup:
801  *      Create an aio_context capable of receiving at least nr_events.
802  *      ctxp must not point to an aio_context that already exists, and
803  *      must be initialized to 0 prior to the call.  On successful
804  *      creation of the aio_context, *ctxp is filled in with the resulting 
805  *      handle.  May fail with -EINVAL if *ctxp is not initialized,
806  *      if the specified nr_events exceeds internal limits.  May fail 
807  *      with -EAGAIN if the specified nr_events exceeds the user's limit 
808  *      of available events.  May fail with -ENOMEM if insufficient kernel
809  *      resources are available.  May fail with -EFAULT if an invalid
810  *      pointer is passed for ctxp.  Will fail with -ENOSYS if not
811  *      implemented.
812  */
813 SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
814 {
815         struct kioctx *ioctx = NULL;
816         unsigned long ctx;
817         long ret;
818
819         ret = get_user(ctx, ctxp);
820         if (unlikely(ret))
821                 goto out;
822
823         ret = -EINVAL;
824         if (unlikely(ctx || nr_events == 0)) {
825                 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
826                          ctx, nr_events);
827                 goto out;
828         }
829
830         ioctx = ioctx_alloc(nr_events);
831         ret = PTR_ERR(ioctx);
832         if (!IS_ERR(ioctx)) {
833                 ret = put_user(ioctx->user_id, ctxp);
834                 if (ret)
835                         kill_ioctx(ioctx);
836                 put_ioctx(ioctx);
837         }
838
839 out:
840         return ret;
841 }
842
843 /* sys_io_destroy:
844  *      Destroy the aio_context specified.  May cancel any outstanding 
845  *      AIOs and block on completion.  Will fail with -ENOSYS if not
846  *      implemented.  May fail with -EINVAL if the context pointed to
847  *      is invalid.
848  */
849 SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
850 {
851         struct kioctx *ioctx = lookup_ioctx(ctx);
852         if (likely(NULL != ioctx)) {
853                 kill_ioctx(ioctx);
854                 put_ioctx(ioctx);
855                 return 0;
856         }
857         pr_debug("EINVAL: io_destroy: invalid context id\n");
858         return -EINVAL;
859 }
860
861 static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
862 {
863         struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
864
865         BUG_ON(ret <= 0);
866
867         while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
868                 ssize_t this = min((ssize_t)iov->iov_len, ret);
869                 iov->iov_base += this;
870                 iov->iov_len -= this;
871                 iocb->ki_left -= this;
872                 ret -= this;
873                 if (iov->iov_len == 0) {
874                         iocb->ki_cur_seg++;
875                         iov++;
876                 }
877         }
878
879         /* the caller should not have done more io than what fit in
880          * the remaining iovecs */
881         BUG_ON(ret > 0 && iocb->ki_left == 0);
882 }
883
884 static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
885 {
886         struct file *file = iocb->ki_filp;
887         struct address_space *mapping = file->f_mapping;
888         struct inode *inode = mapping->host;
889         ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
890                          unsigned long, loff_t);
891         ssize_t ret = 0;
892         unsigned short opcode;
893
894         if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
895                 (iocb->ki_opcode == IOCB_CMD_PREAD)) {
896                 rw_op = file->f_op->aio_read;
897                 opcode = IOCB_CMD_PREADV;
898         } else {
899                 rw_op = file->f_op->aio_write;
900                 opcode = IOCB_CMD_PWRITEV;
901         }
902
903         /* This matches the pread()/pwrite() logic */
904         if (iocb->ki_pos < 0)
905                 return -EINVAL;
906
907         do {
908                 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
909                             iocb->ki_nr_segs - iocb->ki_cur_seg,
910                             iocb->ki_pos);
911                 if (ret > 0)
912                         aio_advance_iovec(iocb, ret);
913
914         /* retry all partial writes.  retry partial reads as long as its a
915          * regular file. */
916         } while (ret > 0 && iocb->ki_left > 0 &&
917                  (opcode == IOCB_CMD_PWRITEV ||
918                   (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
919
920         /* This means we must have transferred all that we could */
921         /* No need to retry anymore */
922         if ((ret == 0) || (iocb->ki_left == 0))
923                 ret = iocb->ki_nbytes - iocb->ki_left;
924
925         /* If we managed to write some out we return that, rather than
926          * the eventual error. */
927         if (opcode == IOCB_CMD_PWRITEV
928             && ret < 0 && ret != -EIOCBQUEUED
929             && iocb->ki_nbytes - iocb->ki_left)
930                 ret = iocb->ki_nbytes - iocb->ki_left;
931
932         return ret;
933 }
934
935 static ssize_t aio_fdsync(struct kiocb *iocb)
936 {
937         struct file *file = iocb->ki_filp;
938         ssize_t ret = -EINVAL;
939
940         if (file->f_op->aio_fsync)
941                 ret = file->f_op->aio_fsync(iocb, 1);
942         return ret;
943 }
944
945 static ssize_t aio_fsync(struct kiocb *iocb)
946 {
947         struct file *file = iocb->ki_filp;
948         ssize_t ret = -EINVAL;
949
950         if (file->f_op->aio_fsync)
951                 ret = file->f_op->aio_fsync(iocb, 0);
952         return ret;
953 }
954
955 static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
956 {
957         ssize_t ret;
958
959 #ifdef CONFIG_COMPAT
960         if (compat)
961                 ret = compat_rw_copy_check_uvector(type,
962                                 (struct compat_iovec __user *)kiocb->ki_buf,
963                                 kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
964                                 &kiocb->ki_iovec);
965         else
966 #endif
967                 ret = rw_copy_check_uvector(type,
968                                 (struct iovec __user *)kiocb->ki_buf,
969                                 kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
970                                 &kiocb->ki_iovec);
971         if (ret < 0)
972                 goto out;
973
974         ret = rw_verify_area(type, kiocb->ki_filp, &kiocb->ki_pos, ret);
975         if (ret < 0)
976                 goto out;
977
978         kiocb->ki_nr_segs = kiocb->ki_nbytes;
979         kiocb->ki_cur_seg = 0;
980         /* ki_nbytes/left now reflect bytes instead of segs */
981         kiocb->ki_nbytes = ret;
982         kiocb->ki_left = ret;
983
984         ret = 0;
985 out:
986         return ret;
987 }
988
989 static ssize_t aio_setup_single_vector(int type, struct file * file, struct kiocb *kiocb)
990 {
991         int bytes;
992
993         bytes = rw_verify_area(type, file, &kiocb->ki_pos, kiocb->ki_left);
994         if (bytes < 0)
995                 return bytes;
996
997         kiocb->ki_iovec = &kiocb->ki_inline_vec;
998         kiocb->ki_iovec->iov_base = kiocb->ki_buf;
999         kiocb->ki_iovec->iov_len = bytes;
1000         kiocb->ki_nr_segs = 1;
1001         kiocb->ki_cur_seg = 0;
1002         return 0;
1003 }
1004
1005 /*
1006  * aio_setup_iocb:
1007  *      Performs the initial checks and aio retry method
1008  *      setup for the kiocb at the time of io submission.
1009  */
1010 static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
1011 {
1012         struct file *file = kiocb->ki_filp;
1013         ssize_t ret = 0;
1014
1015         switch (kiocb->ki_opcode) {
1016         case IOCB_CMD_PREAD:
1017                 ret = -EBADF;
1018                 if (unlikely(!(file->f_mode & FMODE_READ)))
1019                         break;
1020                 ret = -EFAULT;
1021                 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1022                         kiocb->ki_left)))
1023                         break;
1024                 ret = aio_setup_single_vector(READ, file, kiocb);
1025                 if (ret)
1026                         break;
1027                 ret = -EINVAL;
1028                 if (file->f_op->aio_read)
1029                         kiocb->ki_retry = aio_rw_vect_retry;
1030                 break;
1031         case IOCB_CMD_PWRITE:
1032                 ret = -EBADF;
1033                 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1034                         break;
1035                 ret = -EFAULT;
1036                 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1037                         kiocb->ki_left)))
1038                         break;
1039                 ret = aio_setup_single_vector(WRITE, file, kiocb);
1040                 if (ret)
1041                         break;
1042                 ret = -EINVAL;
1043                 if (file->f_op->aio_write)
1044                         kiocb->ki_retry = aio_rw_vect_retry;
1045                 break;
1046         case IOCB_CMD_PREADV:
1047                 ret = -EBADF;
1048                 if (unlikely(!(file->f_mode & FMODE_READ)))
1049                         break;
1050                 ret = aio_setup_vectored_rw(READ, kiocb, compat);
1051                 if (ret)
1052                         break;
1053                 ret = -EINVAL;
1054                 if (file->f_op->aio_read)
1055                         kiocb->ki_retry = aio_rw_vect_retry;
1056                 break;
1057         case IOCB_CMD_PWRITEV:
1058                 ret = -EBADF;
1059                 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1060                         break;
1061                 ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
1062                 if (ret)
1063                         break;
1064                 ret = -EINVAL;
1065                 if (file->f_op->aio_write)
1066                         kiocb->ki_retry = aio_rw_vect_retry;
1067                 break;
1068         case IOCB_CMD_FDSYNC:
1069                 ret = -EINVAL;
1070                 if (file->f_op->aio_fsync)
1071                         kiocb->ki_retry = aio_fdsync;
1072                 break;
1073         case IOCB_CMD_FSYNC:
1074                 ret = -EINVAL;
1075                 if (file->f_op->aio_fsync)
1076                         kiocb->ki_retry = aio_fsync;
1077                 break;
1078         default:
1079                 pr_debug("EINVAL: no operation provided\n");
1080                 ret = -EINVAL;
1081         }
1082
1083         if (!kiocb->ki_retry)
1084                 return ret;
1085
1086         return 0;
1087 }
1088
1089 static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
1090                          struct iocb *iocb, bool compat)
1091 {
1092         struct kiocb *req;
1093         ssize_t ret;
1094
1095         /* enforce forwards compatibility on users */
1096         if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
1097                 pr_debug("EINVAL: reserve field set\n");
1098                 return -EINVAL;
1099         }
1100
1101         /* prevent overflows */
1102         if (unlikely(
1103             (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1104             (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1105             ((ssize_t)iocb->aio_nbytes < 0)
1106            )) {
1107                 pr_debug("EINVAL: io_submit: overflow check\n");
1108                 return -EINVAL;
1109         }
1110
1111         req = aio_get_req(ctx);  /* returns with 2 references to req */
1112         if (unlikely(!req))
1113                 return -EAGAIN;
1114
1115         req->ki_filp = fget(iocb->aio_fildes);
1116         if (unlikely(!req->ki_filp)) {
1117                 ret = -EBADF;
1118                 goto out_put_req;
1119         }
1120
1121         if (iocb->aio_flags & IOCB_FLAG_RESFD) {
1122                 /*
1123                  * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1124                  * instance of the file* now. The file descriptor must be
1125                  * an eventfd() fd, and will be signaled for each completed
1126                  * event using the eventfd_signal() function.
1127                  */
1128                 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
1129                 if (IS_ERR(req->ki_eventfd)) {
1130                         ret = PTR_ERR(req->ki_eventfd);
1131                         req->ki_eventfd = NULL;
1132                         goto out_put_req;
1133                 }
1134         }
1135
1136         ret = put_user(req->ki_key, &user_iocb->aio_key);
1137         if (unlikely(ret)) {
1138                 pr_debug("EFAULT: aio_key\n");
1139                 goto out_put_req;
1140         }
1141
1142         req->ki_obj.user = user_iocb;
1143         req->ki_user_data = iocb->aio_data;
1144         req->ki_pos = iocb->aio_offset;
1145
1146         req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1147         req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1148         req->ki_opcode = iocb->aio_lio_opcode;
1149
1150         ret = aio_setup_iocb(req, compat);
1151         if (ret)
1152                 goto out_put_req;
1153
1154         ret = req->ki_retry(req);
1155         if (ret != -EIOCBQUEUED) {
1156                 /*
1157                  * There's no easy way to restart the syscall since other AIO's
1158                  * may be already running. Just fail this IO with EINTR.
1159                  */
1160                 if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
1161                              ret == -ERESTARTNOHAND || ret == -ERESTART_RESTARTBLOCK))
1162                         ret = -EINTR;
1163                 aio_complete(req, ret, 0);
1164         }
1165
1166         aio_put_req(req);       /* drop extra ref to req */
1167         return 0;
1168
1169 out_put_req:
1170         atomic_dec(&ctx->reqs_active);
1171         aio_put_req(req);       /* drop extra ref to req */
1172         aio_put_req(req);       /* drop i/o ref to req */
1173         return ret;
1174 }
1175
1176 long do_io_submit(aio_context_t ctx_id, long nr,
1177                   struct iocb __user *__user *iocbpp, bool compat)
1178 {
1179         struct kioctx *ctx;
1180         long ret = 0;
1181         int i = 0;
1182         struct blk_plug plug;
1183
1184         if (unlikely(nr < 0))
1185                 return -EINVAL;
1186
1187         if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
1188                 nr = LONG_MAX/sizeof(*iocbpp);
1189
1190         if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1191                 return -EFAULT;
1192
1193         ctx = lookup_ioctx(ctx_id);
1194         if (unlikely(!ctx)) {
1195                 pr_debug("EINVAL: invalid context id\n");
1196                 return -EINVAL;
1197         }
1198
1199         blk_start_plug(&plug);
1200
1201         /*
1202          * AKPM: should this return a partial result if some of the IOs were
1203          * successfully submitted?
1204          */
1205         for (i=0; i<nr; i++) {
1206                 struct iocb __user *user_iocb;
1207                 struct iocb tmp;
1208
1209                 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1210                         ret = -EFAULT;
1211                         break;
1212                 }
1213
1214                 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1215                         ret = -EFAULT;
1216                         break;
1217                 }
1218
1219                 ret = io_submit_one(ctx, user_iocb, &tmp, compat);
1220                 if (ret)
1221                         break;
1222         }
1223         blk_finish_plug(&plug);
1224
1225         put_ioctx(ctx);
1226         return i ? i : ret;
1227 }
1228
1229 /* sys_io_submit:
1230  *      Queue the nr iocbs pointed to by iocbpp for processing.  Returns
1231  *      the number of iocbs queued.  May return -EINVAL if the aio_context
1232  *      specified by ctx_id is invalid, if nr is < 0, if the iocb at
1233  *      *iocbpp[0] is not properly initialized, if the operation specified
1234  *      is invalid for the file descriptor in the iocb.  May fail with
1235  *      -EFAULT if any of the data structures point to invalid data.  May
1236  *      fail with -EBADF if the file descriptor specified in the first
1237  *      iocb is invalid.  May fail with -EAGAIN if insufficient resources
1238  *      are available to queue any iocbs.  Will return 0 if nr is 0.  Will
1239  *      fail with -ENOSYS if not implemented.
1240  */
1241 SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1242                 struct iocb __user * __user *, iocbpp)
1243 {
1244         return do_io_submit(ctx_id, nr, iocbpp, 0);
1245 }
1246
1247 /* lookup_kiocb
1248  *      Finds a given iocb for cancellation.
1249  */
1250 static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1251                                   u32 key)
1252 {
1253         struct list_head *pos;
1254
1255         assert_spin_locked(&ctx->ctx_lock);
1256
1257         /* TODO: use a hash or array, this sucks. */
1258         list_for_each(pos, &ctx->active_reqs) {
1259                 struct kiocb *kiocb = list_kiocb(pos);
1260                 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1261                         return kiocb;
1262         }
1263         return NULL;
1264 }
1265
1266 /* sys_io_cancel:
1267  *      Attempts to cancel an iocb previously passed to io_submit.  If
1268  *      the operation is successfully cancelled, the resulting event is
1269  *      copied into the memory pointed to by result without being placed
1270  *      into the completion queue and 0 is returned.  May fail with
1271  *      -EFAULT if any of the data structures pointed to are invalid.
1272  *      May fail with -EINVAL if aio_context specified by ctx_id is
1273  *      invalid.  May fail with -EAGAIN if the iocb specified was not
1274  *      cancelled.  Will fail with -ENOSYS if not implemented.
1275  */
1276 SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1277                 struct io_event __user *, result)
1278 {
1279         struct io_event res;
1280         struct kioctx *ctx;
1281         struct kiocb *kiocb;
1282         u32 key;
1283         int ret;
1284
1285         ret = get_user(key, &iocb->aio_key);
1286         if (unlikely(ret))
1287                 return -EFAULT;
1288
1289         ctx = lookup_ioctx(ctx_id);
1290         if (unlikely(!ctx))
1291                 return -EINVAL;
1292
1293         spin_lock_irq(&ctx->ctx_lock);
1294
1295         kiocb = lookup_kiocb(ctx, iocb, key);
1296         if (kiocb)
1297                 ret = kiocb_cancel(ctx, kiocb, &res);
1298         else
1299                 ret = -EINVAL;
1300
1301         spin_unlock_irq(&ctx->ctx_lock);
1302
1303         if (!ret) {
1304                 /* Cancellation succeeded -- copy the result
1305                  * into the user's buffer.
1306                  */
1307                 if (copy_to_user(result, &res, sizeof(res)))
1308                         ret = -EFAULT;
1309         }
1310
1311         put_ioctx(ctx);
1312
1313         return ret;
1314 }
1315
1316 /* io_getevents:
1317  *      Attempts to read at least min_nr events and up to nr events from
1318  *      the completion queue for the aio_context specified by ctx_id. If
1319  *      it succeeds, the number of read events is returned. May fail with
1320  *      -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
1321  *      out of range, if timeout is out of range.  May fail with -EFAULT
1322  *      if any of the memory specified is invalid.  May return 0 or
1323  *      < min_nr if the timeout specified by timeout has elapsed
1324  *      before sufficient events are available, where timeout == NULL
1325  *      specifies an infinite timeout. Note that the timeout pointed to by
1326  *      timeout is relative and will be updated if not NULL and the
1327  *      operation blocks. Will fail with -ENOSYS if not implemented.
1328  */
1329 SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
1330                 long, min_nr,
1331                 long, nr,
1332                 struct io_event __user *, events,
1333                 struct timespec __user *, timeout)
1334 {
1335         struct kioctx *ioctx = lookup_ioctx(ctx_id);
1336         long ret = -EINVAL;
1337
1338         if (likely(ioctx)) {
1339                 if (likely(min_nr <= nr && min_nr >= 0))
1340                         ret = read_events(ioctx, min_nr, nr, events, timeout);
1341                 put_ioctx(ioctx);
1342         }
1343         return ret;
1344 }