blob: 82eec7c7b4bb25ff55230f1392fdb4467ff27d90 [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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050016#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
Jens Axboeb9d128f2009-10-29 13:59:26 +010018#include <linux/backing-dev.h>
Badari Pulavarty027445c2006-09-30 23:28:46 -070019#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#define DEBUG 0
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>
Michael S. Tsirkin3d2d8272009-09-21 17:03:51 -070028#include <linux/mmu_context.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#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>
Davide Libenzi9c3060b2007-05-10 22:23:21 -070035#include <linux/eventfd.h>
Jeff Moyercfb1e332009-10-02 18:57:36 -040036#include <linux/blkdev.h>
Jeff Moyer9d85cba72010-05-26 14:44:26 -070037#include <linux/compat.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/kmap_types.h>
40#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#if DEBUG > 1
43#define dprintk printk
44#else
45#define dprintk(x...) do { ; } while (0)
46#endif
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -080049static DEFINE_SPINLOCK(aio_nr_lock);
50unsigned long aio_nr; /* current system wide number of aio requests */
51unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
Linus Torvalds1da177e2005-04-16 15:20:36 -070052/*----end sysctl variables---*/
53
Christoph Lametere18b8902006-12-06 20:33:20 -080054static struct kmem_cache *kiocb_cachep;
55static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57static struct workqueue_struct *aio_wq;
58
David Howells65f27f32006-11-22 14:55:48 +000059static void aio_kick_handler(struct work_struct *);
Sébastien Duguc016e222005-06-28 20:44:59 -070060static void aio_queue_work(struct kioctx *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* aio_setup
63 * Creates the slab caches used by the aio routines, panic on
64 * failure as this is done early during the boot sequence.
65 */
66static int __init aio_setup(void)
67{
Christoph Lameter0a31bd52007-05-06 14:49:57 -070068 kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
69 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Tejun Heod37adaa2011-01-26 17:42:27 +010071 aio_wq = alloc_workqueue("aio", 0, 1); /* used to limit concurrency */
Jens Axboecf159002011-03-02 20:12:18 -050072 BUG_ON(!aio_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
75
76 return 0;
77}
H Hartley Sweeten385773e2009-09-22 16:43:53 -070078__initcall(aio_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80static void aio_free_ring(struct kioctx *ctx)
81{
82 struct aio_ring_info *info = &ctx->ring_info;
83 long i;
84
85 for (i=0; i<info->nr_pages; i++)
86 put_page(info->ring_pages[i]);
87
Al Viro936af152012-04-20 21:49:41 -040088 if (info->mmap_size) {
89 BUG_ON(ctx->mm != current->mm);
Al Virobfce2812012-04-20 21:57:04 -040090 vm_munmap(info->mmap_base, info->mmap_size);
Al Viro936af152012-04-20 21:49:41 -040091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 if (info->ring_pages && info->ring_pages != info->internal_pages)
94 kfree(info->ring_pages);
95 info->ring_pages = NULL;
96 info->nr = 0;
97}
98
99static int aio_setup_ring(struct kioctx *ctx)
100{
101 struct aio_ring *ring;
102 struct aio_ring_info *info = &ctx->ring_info;
103 unsigned nr_events = ctx->max_reqs;
104 unsigned long size;
105 int nr_pages;
Michel Lespinassebebeb3d2013-02-22 16:32:37 -0800106 bool populate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 /* Compensate for the ring buffer's head/tail overlap entry */
109 nr_events += 2; /* 1 is required, 2 for good luck */
110
111 size = sizeof(struct aio_ring);
112 size += sizeof(struct io_event) * nr_events;
113 nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
114
115 if (nr_pages < 0)
116 return -EINVAL;
117
118 nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
119
120 info->nr = 0;
121 info->ring_pages = info->internal_pages;
122 if (nr_pages > AIO_RING_PAGES) {
Oliver Neukum11b0b5a2006-03-25 03:08:13 -0800123 info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 if (!info->ring_pages)
125 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 }
127
128 info->mmap_size = nr_pages * PAGE_SIZE;
129 dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
130 down_write(&ctx->mm->mmap_sem);
Al Viroe3fc6292012-05-30 20:08:42 -0400131 info->mmap_base = do_mmap_pgoff(NULL, 0, info->mmap_size,
132 PROT_READ|PROT_WRITE,
Michel Lespinassebebeb3d2013-02-22 16:32:37 -0800133 MAP_ANONYMOUS|MAP_PRIVATE, 0,
134 &populate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 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 }
Michel Lespinassebebeb3d2013-02-22 16:32:37 -0800152 if (populate)
153 mm_populate(info->mmap_base, info->mmap_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 ctx->user_id = info->mmap_base;
156
157 info->nr = nr_events; /* trusted copy */
158
Cong Wange8e3c3d2011-11-25 23:14:27 +0800159 ring = kmap_atomic(info->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 ring->nr = nr_events; /* user copy */
161 ring->id = ctx->user_id;
162 ring->head = ring->tail = 0;
163 ring->magic = AIO_RING_MAGIC;
164 ring->compat_features = AIO_RING_COMPAT_FEATURES;
165 ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
166 ring->header_length = sizeof(struct aio_ring);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800167 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return 0;
170}
171
172
173/* aio_ring_event: returns a pointer to the event at the given index from
Cong Wange8e3c3d2011-11-25 23:14:27 +0800174 * kmap_atomic(). Release the pointer with put_aio_ring_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 */
176#define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
177#define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
178#define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
179
Cong Wange8e3c3d2011-11-25 23:14:27 +0800180#define aio_ring_event(info, nr) ({ \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
182 struct io_event *__event; \
183 __event = kmap_atomic( \
Cong Wange8e3c3d2011-11-25 23:14:27 +0800184 (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE]); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 __event += pos % AIO_EVENTS_PER_PAGE; \
186 __event; \
187})
188
Cong Wange8e3c3d2011-11-25 23:14:27 +0800189#define put_aio_ring_event(event) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 struct io_event *__event = (event); \
191 (void)__event; \
Cong Wange8e3c3d2011-11-25 23:14:27 +0800192 kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193} while(0)
194
Jens Axboeabf137d2008-12-09 08:11:22 +0100195static void ctx_rcu_free(struct rcu_head *head)
Adrian Bunkd5470b52008-04-29 00:58:57 -0700196{
Jens Axboeabf137d2008-12-09 08:11:22 +0100197 struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
Adrian Bunkd5470b52008-04-29 00:58:57 -0700198 kmem_cache_free(kioctx_cachep, ctx);
Adrian Bunkd5470b52008-04-29 00:58:57 -0700199}
200
Jens Axboeabf137d2008-12-09 08:11:22 +0100201/* __put_ioctx
202 * Called when the last user of an aio context has gone away,
203 * and the struct needs to be freed.
204 */
205static void __put_ioctx(struct kioctx *ctx)
206{
Al Viro2dd542b2012-03-10 23:10:35 -0500207 unsigned nr_events = ctx->max_reqs;
Jens Axboeabf137d2008-12-09 08:11:22 +0100208 BUG_ON(ctx->reqs_active);
209
Al Virobf507222012-03-11 00:58:40 -0500210 cancel_delayed_work_sync(&ctx->wq);
Jens Axboeabf137d2008-12-09 08:11:22 +0100211 aio_free_ring(ctx);
212 mmdrop(ctx->mm);
213 ctx->mm = NULL;
Al Viro2dd542b2012-03-10 23:10:35 -0500214 if (nr_events) {
215 spin_lock(&aio_nr_lock);
216 BUG_ON(aio_nr - nr_events > aio_nr);
217 aio_nr -= nr_events;
218 spin_unlock(&aio_nr_lock);
219 }
Jens Axboeabf137d2008-12-09 08:11:22 +0100220 pr_debug("__put_ioctx: freeing %p\n", ctx);
221 call_rcu(&ctx->rcu_head, ctx_rcu_free);
222}
223
Nick Piggin3bd9a5d2011-02-25 14:44:26 -0800224static inline int try_get_ioctx(struct kioctx *kioctx)
225{
226 return atomic_inc_not_zero(&kioctx->users);
227}
228
229static inline void put_ioctx(struct kioctx *kioctx)
230{
231 BUG_ON(atomic_read(&kioctx->users) <= 0);
232 if (unlikely(atomic_dec_and_test(&kioctx->users)))
233 __put_ioctx(kioctx);
234}
Adrian Bunkd5470b52008-04-29 00:58:57 -0700235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/* ioctx_alloc
237 * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
238 */
239static struct kioctx *ioctx_alloc(unsigned nr_events)
240{
241 struct mm_struct *mm;
242 struct kioctx *ctx;
Al Viroe23754f2012-03-06 14:33:22 -0500243 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* Prevent overflows */
246 if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
247 (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
248 pr_debug("ENOMEM: nr_events too high\n");
249 return ERR_PTR(-EINVAL);
250 }
251
Al Viro2dd542b2012-03-10 23:10:35 -0500252 if (!nr_events || (unsigned long)nr_events > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return ERR_PTR(-EAGAIN);
254
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800255 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 if (!ctx)
257 return ERR_PTR(-ENOMEM);
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 ctx->max_reqs = nr_events;
260 mm = ctx->mm = current->mm;
261 atomic_inc(&mm->mm_count);
262
Al Viro86b62a22012-03-07 05:16:35 +0000263 atomic_set(&ctx->users, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 spin_lock_init(&ctx->ctx_lock);
265 spin_lock_init(&ctx->ring_info.ring_lock);
266 init_waitqueue_head(&ctx->wait);
267
268 INIT_LIST_HEAD(&ctx->active_reqs);
269 INIT_LIST_HEAD(&ctx->run_list);
David Howells65f27f32006-11-22 14:55:48 +0000270 INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 if (aio_setup_ring(ctx) < 0)
273 goto out_freectx;
274
275 /* limit the number of system wide aios */
Al Viro9fa1cb32012-03-10 23:14:05 -0500276 spin_lock(&aio_nr_lock);
Al Viro2dd542b2012-03-10 23:10:35 -0500277 if (aio_nr + nr_events > aio_max_nr ||
278 aio_nr + nr_events < aio_nr) {
Al Viro9fa1cb32012-03-10 23:14:05 -0500279 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 goto out_cleanup;
Al Viro2dd542b2012-03-10 23:10:35 -0500281 }
282 aio_nr += ctx->max_reqs;
Al Viro9fa1cb32012-03-10 23:14:05 -0500283 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Jeff Moyer39fa0032008-04-29 01:03:48 -0700285 /* now link into global list. */
Jens Axboeabf137d2008-12-09 08:11:22 +0100286 spin_lock(&mm->ioctx_lock);
287 hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
288 spin_unlock(&mm->ioctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
291 ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
292 return ctx;
293
294out_cleanup:
Al Viroe23754f2012-03-06 14:33:22 -0500295 err = -EAGAIN;
296 aio_free_ring(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297out_freectx:
298 mmdrop(mm);
299 kmem_cache_free(kioctx_cachep, ctx);
Al Viroe23754f2012-03-06 14:33:22 -0500300 dprintk("aio: error allocating ioctx %d\n", err);
301 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Al Viro06af1212012-03-20 16:26:24 -0400304/* kill_ctx
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * Cancels all outstanding aio requests on an aio context. Used
306 * when the processes owning a context have all exited to encourage
307 * the rapid destruction of the kioctx.
308 */
Al Viro06af1212012-03-20 16:26:24 -0400309static void kill_ctx(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 int (*cancel)(struct kiocb *, struct io_event *);
Al Viro06af1212012-03-20 16:26:24 -0400312 struct task_struct *tsk = current;
313 DECLARE_WAITQUEUE(wait, tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct io_event res;
Al Viro06af1212012-03-20 16:26:24 -0400315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 spin_lock_irq(&ctx->ctx_lock);
317 ctx->dead = 1;
318 while (!list_empty(&ctx->active_reqs)) {
319 struct list_head *pos = ctx->active_reqs.next;
320 struct kiocb *iocb = list_kiocb(pos);
321 list_del_init(&iocb->ki_list);
322 cancel = iocb->ki_cancel;
323 kiocbSetCancelled(iocb);
324 if (cancel) {
325 iocb->ki_users++;
326 spin_unlock_irq(&ctx->ctx_lock);
327 cancel(iocb, &res);
328 spin_lock_irq(&ctx->ctx_lock);
329 }
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!ctx->reqs_active)
Ken Chendee11c22007-02-03 01:13:45 -0800333 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 add_wait_queue(&ctx->wait, &wait);
336 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
337 while (ctx->reqs_active) {
Ken Chendee11c22007-02-03 01:13:45 -0800338 spin_unlock_irq(&ctx->ctx_lock);
Jeff Moyer41d10da2007-10-16 23:27:20 -0700339 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 set_task_state(tsk, TASK_UNINTERRUPTIBLE);
Ken Chendee11c22007-02-03 01:13:45 -0800341 spin_lock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343 __set_task_state(tsk, TASK_RUNNING);
344 remove_wait_queue(&ctx->wait, &wait);
Ken Chendee11c22007-02-03 01:13:45 -0800345
346out:
347 spin_unlock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
350/* wait_on_sync_kiocb:
351 * Waits on the given sync kiocb to complete.
352 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800353ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354{
355 while (iocb->ki_users) {
356 set_current_state(TASK_UNINTERRUPTIBLE);
357 if (!iocb->ki_users)
358 break;
Jeff Moyer41d10da2007-10-16 23:27:20 -0700359 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 __set_current_state(TASK_RUNNING);
362 return iocb->ki_user_data;
363}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700364EXPORT_SYMBOL(wait_on_sync_kiocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366/* exit_aio: called when the last user of mm goes away. At this point,
367 * there is no way for any new requests to be submited or any of the
368 * io_* syscalls to be called on the context. However, there may be
369 * outstanding requests which hold references to the context; as they
370 * go away, they will call put_ioctx and release any pinned memory
371 * associated with the request (held via struct page * references).
372 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800373void exit_aio(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Jens Axboeabf137d2008-12-09 08:11:22 +0100375 struct kioctx *ctx;
376
377 while (!hlist_empty(&mm->ioctx_list)) {
378 ctx = hlist_entry(mm->ioctx_list.first, struct kioctx, list);
379 hlist_del_rcu(&ctx->list);
380
Al Viro06af1212012-03-20 16:26:24 -0400381 kill_ctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (1 != atomic_read(&ctx->users))
384 printk(KERN_DEBUG
385 "exit_aio:ioctx still alive: %d %d %d\n",
386 atomic_read(&ctx->users), ctx->dead,
387 ctx->reqs_active);
Al Viro936af152012-04-20 21:49:41 -0400388 /*
389 * We don't need to bother with munmap() here -
390 * exit_mmap(mm) is coming and it'll unmap everything.
391 * Since aio_free_ring() uses non-zero ->mmap_size
392 * as indicator that it needs to unmap the area,
393 * just set it to 0; aio_free_ring() is the only
394 * place that uses ->mmap_size, so it's safe.
395 * That way we get all munmap done to current->mm -
396 * all other callers have ctx->mm == current->mm.
397 */
398 ctx->ring_info.mmap_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 put_ioctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401}
402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403/* aio_get_req
404 * Allocate a slot for an aio request. Increments the users count
405 * of the kioctx so that the kioctx stays around until all requests are
406 * complete. Returns NULL if no requests are free.
407 *
408 * Returns with kiocb->users set to 2. The io submit code path holds
409 * an extra reference while submitting the i/o.
410 * This prevents races between the aio code path referencing the
411 * req (after submitting it) and aio_complete() freeing the req.
412 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800413static struct kiocb *__aio_get_req(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 struct kiocb *req = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
418 if (unlikely(!req))
419 return NULL;
420
Zach Brown4faa5282005-10-17 16:43:33 -0700421 req->ki_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 req->ki_users = 2;
423 req->ki_key = 0;
424 req->ki_ctx = ctx;
425 req->ki_cancel = NULL;
426 req->ki_retry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 req->ki_dtor = NULL;
428 req->private = NULL;
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700429 req->ki_iovec = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 INIT_LIST_HEAD(&req->ki_run_list);
Davide Libenzi87c3a862009-03-18 17:04:19 -0700431 req->ki_eventfd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return req;
434}
435
Jeff Moyer080d6762011-11-02 13:40:10 -0700436/*
437 * struct kiocb's are allocated in batches to reduce the number of
438 * times the ctx lock is acquired and released.
439 */
440#define KIOCB_BATCH_SIZE 32L
441struct kiocb_batch {
442 struct list_head head;
443 long count; /* number of requests left to allocate */
444};
445
446static void kiocb_batch_init(struct kiocb_batch *batch, long total)
447{
448 INIT_LIST_HEAD(&batch->head);
449 batch->count = total;
450}
451
Gleb Natapov69e47472012-01-08 17:07:28 +0200452static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
Jeff Moyer080d6762011-11-02 13:40:10 -0700453{
454 struct kiocb *req, *n;
455
Gleb Natapov69e47472012-01-08 17:07:28 +0200456 if (list_empty(&batch->head))
457 return;
458
459 spin_lock_irq(&ctx->ctx_lock);
Jeff Moyer080d6762011-11-02 13:40:10 -0700460 list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
461 list_del(&req->ki_batch);
Gleb Natapov69e47472012-01-08 17:07:28 +0200462 list_del(&req->ki_list);
Jeff Moyer080d6762011-11-02 13:40:10 -0700463 kmem_cache_free(kiocb_cachep, req);
Gleb Natapov69e47472012-01-08 17:07:28 +0200464 ctx->reqs_active--;
Jeff Moyer080d6762011-11-02 13:40:10 -0700465 }
Jeff Moyer880641b2012-03-05 14:59:12 -0800466 if (unlikely(!ctx->reqs_active && ctx->dead))
467 wake_up_all(&ctx->wait);
Gleb Natapov69e47472012-01-08 17:07:28 +0200468 spin_unlock_irq(&ctx->ctx_lock);
Jeff Moyer080d6762011-11-02 13:40:10 -0700469}
470
471/*
472 * Allocate a batch of kiocbs. This avoids taking and dropping the
473 * context lock a lot during setup.
474 */
475static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
476{
477 unsigned short allocated, to_alloc;
478 long avail;
Jeff Moyer080d6762011-11-02 13:40:10 -0700479 struct kiocb *req, *n;
480 struct aio_ring *ring;
481
482 to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
483 for (allocated = 0; allocated < to_alloc; allocated++) {
484 req = __aio_get_req(ctx);
485 if (!req)
486 /* allocation failed, go with what we've got */
487 break;
488 list_add(&req->ki_batch, &batch->head);
489 }
490
491 if (allocated == 0)
492 goto out;
493
Jeff Moyer080d6762011-11-02 13:40:10 -0700494 spin_lock_irq(&ctx->ctx_lock);
495 ring = kmap_atomic(ctx->ring_info.ring_pages[0]);
496
497 avail = aio_ring_avail(&ctx->ring_info, ring) - ctx->reqs_active;
498 BUG_ON(avail < 0);
Jeff Moyer080d6762011-11-02 13:40:10 -0700499 if (avail < allocated) {
500 /* Trim back the number of requests. */
501 list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
502 list_del(&req->ki_batch);
503 kmem_cache_free(kiocb_cachep, req);
504 if (--allocated <= avail)
505 break;
506 }
507 }
508
509 batch->count -= allocated;
510 list_for_each_entry(req, &batch->head, ki_batch) {
511 list_add(&req->ki_list, &ctx->active_reqs);
512 ctx->reqs_active++;
513 }
514
515 kunmap_atomic(ring);
516 spin_unlock_irq(&ctx->ctx_lock);
517
518out:
519 return allocated;
520}
521
522static inline struct kiocb *aio_get_req(struct kioctx *ctx,
523 struct kiocb_batch *batch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
525 struct kiocb *req;
Jeff Moyer080d6762011-11-02 13:40:10 -0700526
527 if (list_empty(&batch->head))
528 if (kiocb_batch_refill(ctx, batch) == 0)
529 return NULL;
530 req = list_first_entry(&batch->head, struct kiocb, ki_batch);
531 list_del(&req->ki_batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 return req;
533}
534
535static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
536{
Zach Brownd00689a2005-11-13 16:07:34 -0800537 assert_spin_locked(&ctx->ctx_lock);
538
Davide Libenzi13389012009-06-30 11:41:11 -0700539 if (req->ki_eventfd != NULL)
540 eventfd_ctx_put(req->ki_eventfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (req->ki_dtor)
542 req->ki_dtor(req);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700543 if (req->ki_iovec != &req->ki_inline_vec)
544 kfree(req->ki_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 kmem_cache_free(kiocb_cachep, req);
546 ctx->reqs_active--;
547
548 if (unlikely(!ctx->reqs_active && ctx->dead))
Roland Dreiere91f90b2011-03-22 16:35:10 -0700549 wake_up_all(&ctx->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550}
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552/* __aio_put_req
553 * Returns true if this put was the last user of the request.
554 */
555static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
556{
Al Viro516e0cc2008-07-26 00:39:17 -0400557 dprintk(KERN_DEBUG "aio_put(%p): f_count=%ld\n",
558 req, atomic_long_read(&req->ki_filp->f_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Zach Brownd00689a2005-11-13 16:07:34 -0800560 assert_spin_locked(&ctx->ctx_lock);
561
Davide Libenzi87c3a862009-03-18 17:04:19 -0700562 req->ki_users--;
Eric Sesterhenn93e06b42006-11-30 05:29:23 +0100563 BUG_ON(req->ki_users < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 if (likely(req->ki_users))
565 return 0;
566 list_del(&req->ki_list); /* remove from active_reqs */
567 req->ki_cancel = NULL;
568 req->ki_retry = NULL;
569
Al Viro3ffa3c02012-06-24 10:00:10 +0400570 fput(req->ki_filp);
571 req->ki_filp = NULL;
572 really_put_req(ctx, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return 1;
574}
575
576/* aio_put_req
577 * Returns true if this put was the last user of the kiocb,
578 * false if the request is still in use.
579 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800580int aio_put_req(struct kiocb *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
582 struct kioctx *ctx = req->ki_ctx;
583 int ret;
584 spin_lock_irq(&ctx->ctx_lock);
585 ret = __aio_put_req(ctx, req);
586 spin_unlock_irq(&ctx->ctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return ret;
588}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700589EXPORT_SYMBOL(aio_put_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Adrian Bunkd5470b52008-04-29 00:58:57 -0700591static struct kioctx *lookup_ioctx(unsigned long ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Jens Axboeabf137d2008-12-09 08:11:22 +0100593 struct mm_struct *mm = current->mm;
Jeff Moyer65c24492009-03-18 17:04:21 -0700594 struct kioctx *ctx, *ret = NULL;
Jens Axboeabf137d2008-12-09 08:11:22 +0100595 struct hlist_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Jens Axboeabf137d2008-12-09 08:11:22 +0100597 rcu_read_lock();
598
599 hlist_for_each_entry_rcu(ctx, n, &mm->ioctx_list, list) {
Nick Piggin3bd9a5d2011-02-25 14:44:26 -0800600 /*
601 * RCU protects us against accessing freed memory but
602 * we have to be careful not to get a reference when the
603 * reference count already dropped to 0 (ctx->dead test
604 * is unreliable because of races).
605 */
606 if (ctx->user_id == ctx_id && !ctx->dead && try_get_ioctx(ctx)){
Jeff Moyer65c24492009-03-18 17:04:21 -0700607 ret = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
609 }
Jens Axboeabf137d2008-12-09 08:11:22 +0100610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Jens Axboeabf137d2008-12-09 08:11:22 +0100612 rcu_read_unlock();
Jeff Moyer65c24492009-03-18 17:04:21 -0700613 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * Queue up a kiocb to be retried. Assumes that the kiocb
618 * has already been marked as kicked, and places it on
619 * the retry run list for the corresponding ioctx, if it
620 * isn't already queued. Returns 1 if it actually queued
621 * the kiocb (to tell the caller to activate the work
622 * queue to process it), or 0, if it found that it was
623 * already queued.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
625static inline int __queue_kicked_iocb(struct kiocb *iocb)
626{
627 struct kioctx *ctx = iocb->ki_ctx;
628
Zach Brownd00689a2005-11-13 16:07:34 -0800629 assert_spin_locked(&ctx->ctx_lock);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (list_empty(&iocb->ki_run_list)) {
632 list_add_tail(&iocb->ki_run_list,
633 &ctx->run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 return 1;
635 }
636 return 0;
637}
638
639/* aio_run_iocb
640 * This is the core aio execution routine. It is
641 * invoked both for initial i/o submission and
642 * subsequent retries via the aio_kick_handler.
643 * Expects to be invoked with iocb->ki_ctx->lock
Andreas Mohrd6e05ed2006-06-26 18:35:02 +0200644 * already held. The lock is released and reacquired
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * as needed during processing.
646 *
647 * Calls the iocb retry method (already setup for the
648 * iocb on initial submission) for operation specific
649 * handling, but takes care of most of common retry
650 * execution details for a given iocb. The retry method
651 * needs to be non-blocking as far as possible, to avoid
652 * holding up other iocbs waiting to be serviced by the
653 * retry kernel thread.
654 *
655 * The trickier parts in this code have to do with
656 * ensuring that only one retry instance is in progress
657 * for a given iocb at any time. Providing that guarantee
658 * simplifies the coding of individual aio operations as
659 * it avoids various potential races.
660 */
661static ssize_t aio_run_iocb(struct kiocb *iocb)
662{
663 struct kioctx *ctx = iocb->ki_ctx;
664 ssize_t (*retry)(struct kiocb *);
665 ssize_t ret;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (!(retry = iocb->ki_retry)) {
668 printk("aio_run_iocb: iocb->ki_retry = NULL\n");
669 return 0;
670 }
671
672 /*
673 * We don't want the next retry iteration for this
674 * operation to start until this one has returned and
675 * updated the iocb state. However, wait_queue functions
676 * can trigger a kick_iocb from interrupt context in the
677 * meantime, indicating that data is available for the next
678 * iteration. We want to remember that and enable the
679 * next retry iteration _after_ we are through with
680 * this one.
681 *
682 * So, in order to be able to register a "kick", but
683 * prevent it from being queued now, we clear the kick
684 * flag, but make the kick code *think* that the iocb is
685 * still on the run list until we are actually done.
686 * When we are done with this iteration, we check if
687 * the iocb was kicked in the meantime and if so, queue
688 * it up afresh.
689 */
690
691 kiocbClearKicked(iocb);
692
693 /*
694 * This is so that aio_complete knows it doesn't need to
695 * pull the iocb off the run list (We can't just call
696 * INIT_LIST_HEAD because we don't want a kick_iocb to
697 * queue this on the run list yet)
698 */
699 iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
700 spin_unlock_irq(&ctx->ctx_lock);
701
702 /* Quit retrying if the i/o has been cancelled */
703 if (kiocbIsCancelled(iocb)) {
704 ret = -EINTR;
705 aio_complete(iocb, ret, 0);
706 /* must not access the iocb after this */
707 goto out;
708 }
709
710 /*
711 * Now we are all set to call the retry method in async
Alexey Dobriyan6212e3a2007-10-18 03:04:56 -0700712 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 ret = retry(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Jan Karaa0c42ba2010-09-22 13:05:03 -0700716 if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
717 /*
718 * There's no easy way to restart the syscall since other AIO's
719 * may be already running. Just fail this IO with EINTR.
720 */
721 if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
722 ret == -ERESTARTNOHAND || ret == -ERESTART_RESTARTBLOCK))
723 ret = -EINTR;
Zach Brown897f15f2005-09-30 11:58:55 -0700724 aio_complete(iocb, ret, 0);
Jan Karaa0c42ba2010-09-22 13:05:03 -0700725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726out:
727 spin_lock_irq(&ctx->ctx_lock);
728
729 if (-EIOCBRETRY == ret) {
730 /*
731 * OK, now that we are done with this iteration
732 * and know that there is more left to go,
733 * this is where we let go so that a subsequent
734 * "kick" can start the next iteration
735 */
736
737 /* will make __queue_kicked_iocb succeed from here on */
738 INIT_LIST_HEAD(&iocb->ki_run_list);
739 /* we must queue the next iteration ourselves, if it
740 * has already been kicked */
741 if (kiocbIsKicked(iocb)) {
742 __queue_kicked_iocb(iocb);
Sébastien Duguc016e222005-06-28 20:44:59 -0700743
744 /*
745 * __queue_kicked_iocb will always return 1 here, because
746 * iocb->ki_run_list is empty at this point so it should
747 * be safe to unconditionally queue the context into the
748 * work queue.
749 */
750 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
752 }
753 return ret;
754}
755
756/*
757 * __aio_run_iocbs:
758 * Process all pending retries queued on the ioctx
759 * run list.
760 * Assumes it is operating within the aio issuer's mm
Zach Brownd00689a2005-11-13 16:07:34 -0800761 * context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
763static int __aio_run_iocbs(struct kioctx *ctx)
764{
765 struct kiocb *iocb;
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700766 struct list_head run_list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Zach Brownd00689a2005-11-13 16:07:34 -0800768 assert_spin_locked(&ctx->ctx_lock);
769
Oleg Nesterov626ab0e2006-06-23 02:05:55 -0700770 list_replace_init(&ctx->run_list, &run_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 while (!list_empty(&run_list)) {
772 iocb = list_entry(run_list.next, struct kiocb,
773 ki_run_list);
774 list_del(&iocb->ki_run_list);
775 /*
776 * Hold an extra reference while retrying i/o.
777 */
778 iocb->ki_users++; /* grab extra reference */
779 aio_run_iocb(iocb);
Ken Chendee11c22007-02-03 01:13:45 -0800780 __aio_put_req(ctx, iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 if (!list_empty(&ctx->run_list))
783 return 1;
784 return 0;
785}
786
787static void aio_queue_work(struct kioctx * ctx)
788{
789 unsigned long timeout;
790 /*
791 * if someone is waiting, get the work started right
792 * away, otherwise, use a longer delay
793 */
794 smp_mb();
795 if (waitqueue_active(&ctx->wait))
796 timeout = 1;
797 else
798 timeout = HZ/10;
799 queue_delayed_work(aio_wq, &ctx->wq, timeout);
800}
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/*
Jeff Moyerd3486f82011-01-12 17:01:08 -0800803 * aio_run_all_iocbs:
804 * Process all pending retries queued on the ioctx
805 * run list, and keep running them until the list
806 * stays empty.
807 * Assumes it is operating within the aio issuer's mm context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 */
809static inline void aio_run_all_iocbs(struct kioctx *ctx)
810{
811 spin_lock_irq(&ctx->ctx_lock);
812 while (__aio_run_iocbs(ctx))
813 ;
814 spin_unlock_irq(&ctx->ctx_lock);
815}
816
817/*
818 * aio_kick_handler:
819 * Work queue handler triggered to process pending
820 * retries on an ioctx. Takes on the aio issuer's
821 * mm context before running the iocbs, so that
822 * copy_xxx_user operates on the issuer's address
823 * space.
824 * Run on aiod's context.
825 */
David Howells65f27f32006-11-22 14:55:48 +0000826static void aio_kick_handler(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
David Howells65f27f32006-11-22 14:55:48 +0000828 struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 mm_segment_t oldfs = get_fs();
Zach Brown1ebb1102006-12-29 16:47:02 -0800830 struct mm_struct *mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 int requeue;
832
833 set_fs(USER_DS);
834 use_mm(ctx->mm);
835 spin_lock_irq(&ctx->ctx_lock);
836 requeue =__aio_run_iocbs(ctx);
Zach Brown1ebb1102006-12-29 16:47:02 -0800837 mm = ctx->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 spin_unlock_irq(&ctx->ctx_lock);
Zach Brown1ebb1102006-12-29 16:47:02 -0800839 unuse_mm(mm);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 set_fs(oldfs);
841 /*
Al Viro9fcf03d2012-03-13 22:06:28 -0400842 * we're in a worker thread already; no point using non-zero delay
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 */
844 if (requeue)
David Howells52bad642006-11-22 14:54:01 +0000845 queue_delayed_work(aio_wq, &ctx->wq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848
849/*
850 * Called by kick_iocb to queue the kiocb for retry
851 * and if required activate the aio work queue to process
852 * it
853 */
Zach Brown998765e2005-09-30 11:58:54 -0700854static void try_queue_kicked_iocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct kioctx *ctx = iocb->ki_ctx;
857 unsigned long flags;
858 int run = 0;
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 spin_lock_irqsave(&ctx->ctx_lock, flags);
Zach Brown998765e2005-09-30 11:58:54 -0700861 /* set this inside the lock so that we can't race with aio_run_iocb()
862 * testing it and putting the iocb on the run list under the lock */
863 if (!kiocbTryKick(iocb))
864 run = __queue_kicked_iocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Ken Chen644d3a02005-05-01 08:59:15 -0700866 if (run)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 aio_queue_work(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
870/*
871 * kick_iocb:
872 * Called typically from a wait queue callback context
Shaohua Lifac046a2009-12-15 16:47:47 -0800873 * to trigger a retry of the iocb.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * The retry is usually executed by aio workqueue
875 * threads (See aio_kick_handler).
876 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800877void kick_iocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 /* sync iocbs are easy: they can only ever be executing from a
880 * single context. */
881 if (is_sync_kiocb(iocb)) {
882 kiocbSetKicked(iocb);
883 wake_up_process(iocb->ki_obj.tsk);
884 return;
885 }
886
Zach Brown998765e2005-09-30 11:58:54 -0700887 try_queue_kicked_iocb(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889EXPORT_SYMBOL(kick_iocb);
890
891/* aio_complete
892 * Called when the io request on the given iocb is complete.
893 * Returns true if this is the last user of the request. The
894 * only other user of the request can be the cancellation code.
895 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800896int aio_complete(struct kiocb *iocb, long res, long res2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
898 struct kioctx *ctx = iocb->ki_ctx;
899 struct aio_ring_info *info;
900 struct aio_ring *ring;
901 struct io_event *event;
902 unsigned long flags;
903 unsigned long tail;
904 int ret;
905
Zach Brown20dcae32005-11-13 16:07:33 -0800906 /*
907 * Special case handling for sync iocbs:
908 * - events go directly into the iocb for fast handling
909 * - the sync task with the iocb in its stack holds the single iocb
910 * ref, no other paths have a way to get another ref
911 * - the sync task helpfully left a reference to itself in the iocb
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 */
913 if (is_sync_kiocb(iocb)) {
Zach Brown20dcae32005-11-13 16:07:33 -0800914 BUG_ON(iocb->ki_users != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 iocb->ki_user_data = res;
Zach Brown20dcae32005-11-13 16:07:33 -0800916 iocb->ki_users = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 wake_up_process(iocb->ki_obj.tsk);
Zach Brown20dcae32005-11-13 16:07:33 -0800918 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921 info = &ctx->ring_info;
922
923 /* add a completion event to the ring buffer.
924 * must be done holding ctx->ctx_lock to prevent
925 * other code from messing with the tail
926 * pointer since we might be called from irq
927 * context.
928 */
929 spin_lock_irqsave(&ctx->ctx_lock, flags);
930
931 if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
932 list_del_init(&iocb->ki_run_list);
933
934 /*
935 * cancelled requests don't get events, userland was given one
936 * when the event got cancelled.
937 */
938 if (kiocbIsCancelled(iocb))
939 goto put_rq;
940
Cong Wange8e3c3d2011-11-25 23:14:27 +0800941 ring = kmap_atomic(info->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 tail = info->tail;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800944 event = aio_ring_event(info, tail);
Ken Chen4bf69b22005-05-01 08:59:15 -0700945 if (++tail >= info->nr)
946 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 event->obj = (u64)(unsigned long)iocb->ki_obj.user;
949 event->data = iocb->ki_user_data;
950 event->res = res;
951 event->res2 = res2;
952
953 dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
954 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
955 res, res2);
956
957 /* after flagging the request as done, we
958 * must never even look at it again
959 */
960 smp_wmb(); /* make event visible before updating tail */
961
962 info->tail = tail;
963 ring->tail = tail;
964
Cong Wange8e3c3d2011-11-25 23:14:27 +0800965 put_aio_ring_event(event);
966 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 pr_debug("added to ring %p at [%lu]\n", iocb, tail);
Davide Libenzi8d1c98b2008-04-10 21:29:19 -0700969
970 /*
971 * Check if the user asked us to deliver the result through an
972 * eventfd. The eventfd_signal() function is safe to be called
973 * from IRQ context.
974 */
Davide Libenzi87c3a862009-03-18 17:04:19 -0700975 if (iocb->ki_eventfd != NULL)
Davide Libenzi8d1c98b2008-04-10 21:29:19 -0700976 eventfd_signal(iocb->ki_eventfd, 1);
977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978put_rq:
979 /* everything turned out well, dispose of the aiocb. */
980 ret = __aio_put_req(ctx, iocb);
981
Quentin Barnes6cb2a212008-03-19 17:00:39 -0700982 /*
983 * We have to order our ring_info tail store above and test
984 * of the wait list below outside the wait lock. This is
985 * like in wake_up_bit() where clearing a bit has to be
986 * ordered with the unlocked test.
987 */
988 smp_mb();
989
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (waitqueue_active(&ctx->wait))
991 wake_up(&ctx->wait);
992
Ken Chendee11c22007-02-03 01:13:45 -0800993 spin_unlock_irqrestore(&ctx->ctx_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return ret;
995}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700996EXPORT_SYMBOL(aio_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998/* aio_read_evt
999 * Pull an event off of the ioctx's event ring. Returns the number of
1000 * events fetched (0 or 1 ;-)
1001 * FIXME: make this use cmpxchg.
1002 * TODO: make the ringbuffer user mmap()able (requires FIXME).
1003 */
1004static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
1005{
1006 struct aio_ring_info *info = &ioctx->ring_info;
1007 struct aio_ring *ring;
1008 unsigned long head;
1009 int ret = 0;
1010
Cong Wange8e3c3d2011-11-25 23:14:27 +08001011 ring = kmap_atomic(info->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 dprintk("in aio_read_evt h%lu t%lu m%lu\n",
1013 (unsigned long)ring->head, (unsigned long)ring->tail,
1014 (unsigned long)ring->nr);
1015
1016 if (ring->head == ring->tail)
1017 goto out;
1018
1019 spin_lock(&info->ring_lock);
1020
1021 head = ring->head % info->nr;
1022 if (head != ring->tail) {
Cong Wange8e3c3d2011-11-25 23:14:27 +08001023 struct io_event *evp = aio_ring_event(info, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 *ent = *evp;
1025 head = (head + 1) % info->nr;
1026 smp_mb(); /* finish reading the event before updatng the head */
1027 ring->head = head;
1028 ret = 1;
Cong Wange8e3c3d2011-11-25 23:14:27 +08001029 put_aio_ring_event(evp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031 spin_unlock(&info->ring_lock);
1032
1033out:
Cong Wange8e3c3d2011-11-25 23:14:27 +08001034 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
1036 (unsigned long)ring->head, (unsigned long)ring->tail);
1037 return ret;
1038}
1039
1040struct aio_timeout {
1041 struct timer_list timer;
1042 int timed_out;
1043 struct task_struct *p;
1044};
1045
1046static void timeout_func(unsigned long data)
1047{
1048 struct aio_timeout *to = (struct aio_timeout *)data;
1049
1050 to->timed_out = 1;
1051 wake_up_process(to->p);
1052}
1053
1054static inline void init_timeout(struct aio_timeout *to)
1055{
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001056 setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 to->timed_out = 0;
1058 to->p = current;
1059}
1060
1061static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
1062 const struct timespec *ts)
1063{
1064 to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
1065 if (time_after(to->timer.expires, jiffies))
1066 add_timer(&to->timer);
1067 else
1068 to->timed_out = 1;
1069}
1070
1071static inline void clear_timeout(struct aio_timeout *to)
1072{
1073 del_singleshot_timer_sync(&to->timer);
1074}
1075
1076static int read_events(struct kioctx *ctx,
1077 long min_nr, long nr,
1078 struct io_event __user *event,
1079 struct timespec __user *timeout)
1080{
1081 long start_jiffies = jiffies;
1082 struct task_struct *tsk = current;
1083 DECLARE_WAITQUEUE(wait, tsk);
1084 int ret;
1085 int i = 0;
1086 struct io_event ent;
1087 struct aio_timeout to;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 int retry = 0;
1089
1090 /* needed to zero any padding within an entry (there shouldn't be
1091 * any, but C is fun!
1092 */
1093 memset(&ent, 0, sizeof(ent));
1094retry:
1095 ret = 0;
1096 while (likely(i < nr)) {
1097 ret = aio_read_evt(ctx, &ent);
1098 if (unlikely(ret <= 0))
1099 break;
1100
1101 dprintk("read event: %Lx %Lx %Lx %Lx\n",
1102 ent.data, ent.obj, ent.res, ent.res2);
1103
1104 /* Could we split the check in two? */
1105 ret = -EFAULT;
1106 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1107 dprintk("aio: lost an event due to EFAULT.\n");
1108 break;
1109 }
1110 ret = 0;
1111
1112 /* Good, event copied to userland, update counts. */
1113 event ++;
1114 i ++;
1115 }
1116
1117 if (min_nr <= i)
1118 return i;
1119 if (ret)
1120 return ret;
1121
1122 /* End fast path */
1123
1124 /* racey check, but it gets redone */
1125 if (!retry && unlikely(!list_empty(&ctx->run_list))) {
1126 retry = 1;
1127 aio_run_all_iocbs(ctx);
1128 goto retry;
1129 }
1130
1131 init_timeout(&to);
1132 if (timeout) {
1133 struct timespec ts;
1134 ret = -EFAULT;
1135 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
1136 goto out;
1137
1138 set_timeout(start_jiffies, &to, &ts);
1139 }
1140
1141 while (likely(i < nr)) {
1142 add_wait_queue_exclusive(&ctx->wait, &wait);
1143 do {
1144 set_task_state(tsk, TASK_INTERRUPTIBLE);
1145 ret = aio_read_evt(ctx, &ent);
1146 if (ret)
1147 break;
1148 if (min_nr <= i)
1149 break;
Jeff Moyere92adcb2008-04-28 02:12:04 -07001150 if (unlikely(ctx->dead)) {
1151 ret = -EINVAL;
1152 break;
1153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (to.timed_out) /* Only check after read evt */
1155 break;
Jeff Moyere00ba3d2007-12-04 23:45:02 -08001156 /* Try to only show up in io wait if there are ops
1157 * in flight */
1158 if (ctx->reqs_active)
1159 io_schedule();
1160 else
1161 schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (signal_pending(tsk)) {
1163 ret = -EINTR;
1164 break;
1165 }
1166 /*ret = aio_read_evt(ctx, &ent);*/
1167 } while (1) ;
1168
1169 set_task_state(tsk, TASK_RUNNING);
1170 remove_wait_queue(&ctx->wait, &wait);
1171
1172 if (unlikely(ret <= 0))
1173 break;
1174
1175 ret = -EFAULT;
1176 if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
1177 dprintk("aio: lost an event due to EFAULT.\n");
1178 break;
1179 }
1180
1181 /* Good, event copied to userland, update counts. */
1182 event ++;
1183 i ++;
1184 }
1185
1186 if (timeout)
1187 clear_timeout(&to);
1188out:
Thomas Gleixnerc6f3a972008-04-30 00:55:03 -07001189 destroy_timer_on_stack(&to.timer);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int was_dead;
1200
1201 /* delete the entry from the list is someone else hasn't already */
Jens Axboeabf137d2008-12-09 08:11:22 +01001202 spin_lock(&mm->ioctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 was_dead = ioctx->dead;
1204 ioctx->dead = 1;
Jens Axboeabf137d2008-12-09 08:11:22 +01001205 hlist_del_rcu(&ioctx->list);
1206 spin_unlock(&mm->ioctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
1208 dprintk("aio_release(%p)\n", ioctx);
1209 if (likely(!was_dead))
1210 put_ioctx(ioctx); /* twice for the list */
1211
Al Viro06af1212012-03-20 16:26:24 -04001212 kill_ctx(ioctx);
Jeff Moyere92adcb2008-04-28 02:12:04 -07001213
1214 /*
1215 * Wake up any waiters. The setting of ctx->dead must be seen
1216 * by other CPUs at this point. Right now, we rely on the
1217 * locking done by the above calls to ensure this consistency.
1218 */
Roland Dreiere91f90b2011-03-22 16:35:10 -07001219 wake_up_all(&ioctx->wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220}
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 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001235SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
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);
Al Viroa2e18592012-03-20 16:27:57 -04001256 if (ret)
1257 io_destroy(ioctx);
1258 put_ioctx(ioctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
1261out:
1262 return ret;
1263}
1264
1265/* sys_io_destroy:
1266 * Destroy the aio_context specified. May cancel any outstanding
1267 * AIOs and block on completion. Will fail with -ENOSYS if not
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001268 * implemented. May fail with -EINVAL if the context pointed to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * is invalid.
1270 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001271SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
1273 struct kioctx *ioctx = lookup_ioctx(ctx);
1274 if (likely(NULL != ioctx)) {
1275 io_destroy(ioctx);
Al Viroa2e18592012-03-20 16:27:57 -04001276 put_ioctx(ioctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return 0;
1278 }
1279 pr_debug("EINVAL: io_destroy: invalid context id\n");
1280 return -EINVAL;
1281}
1282
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001283static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
1284{
1285 struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
1286
1287 BUG_ON(ret <= 0);
1288
1289 while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
1290 ssize_t this = min((ssize_t)iov->iov_len, ret);
1291 iov->iov_base += this;
1292 iov->iov_len -= this;
1293 iocb->ki_left -= this;
1294 ret -= this;
1295 if (iov->iov_len == 0) {
1296 iocb->ki_cur_seg++;
1297 iov++;
1298 }
1299 }
1300
1301 /* the caller should not have done more io than what fit in
1302 * the remaining iovecs */
1303 BUG_ON(ret > 0 && iocb->ki_left == 0);
1304}
1305
1306static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct file *file = iocb->ki_filp;
1309 struct address_space *mapping = file->f_mapping;
1310 struct inode *inode = mapping->host;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001311 ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
1312 unsigned long, loff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 ssize_t ret = 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001314 unsigned short opcode;
1315
1316 if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
1317 (iocb->ki_opcode == IOCB_CMD_PREAD)) {
1318 rw_op = file->f_op->aio_read;
1319 opcode = IOCB_CMD_PREADV;
1320 } else {
1321 rw_op = file->f_op->aio_write;
1322 opcode = IOCB_CMD_PWRITEV;
1323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Rusty Russellc2ec6682008-02-08 04:20:15 -08001325 /* This matches the pread()/pwrite() logic */
1326 if (iocb->ki_pos < 0)
1327 return -EINVAL;
1328
Zach Brown897f15f2005-09-30 11:58:55 -07001329 do {
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001330 ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
1331 iocb->ki_nr_segs - iocb->ki_cur_seg,
1332 iocb->ki_pos);
1333 if (ret > 0)
1334 aio_advance_iovec(iocb, ret);
Badari Pulavarty027445c2006-09-30 23:28:46 -07001335
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001336 /* retry all partial writes. retry partial reads as long as its a
1337 * regular file. */
Zach Brown353fb072005-09-30 11:58:56 -07001338 } while (ret > 0 && iocb->ki_left > 0 &&
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001339 (opcode == IOCB_CMD_PWRITEV ||
1340 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342 /* This means we must have transferred all that we could */
1343 /* No need to retry anymore */
1344 if ((ret == 0) || (iocb->ki_left == 0))
1345 ret = iocb->ki_nbytes - iocb->ki_left;
1346
Rusty Russell7adfa2f2008-02-08 04:20:14 -08001347 /* If we managed to write some out we return that, rather than
1348 * the eventual error. */
1349 if (opcode == IOCB_CMD_PWRITEV
1350 && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
1351 && iocb->ki_nbytes - iocb->ki_left)
1352 ret = iocb->ki_nbytes - iocb->ki_left;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 return ret;
1355}
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357static ssize_t aio_fdsync(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, 1);
1364 return ret;
1365}
1366
1367static ssize_t aio_fsync(struct kiocb *iocb)
1368{
1369 struct file *file = iocb->ki_filp;
1370 ssize_t ret = -EINVAL;
1371
1372 if (file->f_op->aio_fsync)
1373 ret = file->f_op->aio_fsync(iocb, 0);
1374 return ret;
1375}
1376
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001377static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001378{
1379 ssize_t ret;
1380
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001381#ifdef CONFIG_COMPAT
1382 if (compat)
1383 ret = compat_rw_copy_check_uvector(type,
1384 (struct compat_iovec __user *)kiocb->ki_buf,
1385 kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
Christopher Yeohac34ebb2012-05-31 16:26:42 -07001386 &kiocb->ki_iovec);
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001387 else
1388#endif
1389 ret = rw_copy_check_uvector(type,
1390 (struct iovec __user *)kiocb->ki_buf,
1391 kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
Christopher Yeohac34ebb2012-05-31 16:26:42 -07001392 &kiocb->ki_iovec);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001393 if (ret < 0)
1394 goto out;
1395
Linus Torvaldsa70b52e2012-05-21 16:06:20 -07001396 ret = rw_verify_area(type, kiocb->ki_filp, &kiocb->ki_pos, ret);
1397 if (ret < 0)
1398 goto out;
1399
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001400 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1401 kiocb->ki_cur_seg = 0;
1402 /* ki_nbytes/left now reflect bytes instead of segs */
1403 kiocb->ki_nbytes = ret;
1404 kiocb->ki_left = ret;
1405
1406 ret = 0;
1407out:
1408 return ret;
1409}
1410
Linus Torvaldsa70b52e2012-05-21 16:06:20 -07001411static ssize_t aio_setup_single_vector(int type, struct file * file, struct kiocb *kiocb)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001412{
Linus Torvaldsa70b52e2012-05-21 16:06:20 -07001413 int bytes;
1414
1415 bytes = rw_verify_area(type, file, &kiocb->ki_pos, kiocb->ki_left);
1416 if (bytes < 0)
1417 return bytes;
1418
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001419 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1420 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
Linus Torvaldsa70b52e2012-05-21 16:06:20 -07001421 kiocb->ki_iovec->iov_len = bytes;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001422 kiocb->ki_nr_segs = 1;
1423 kiocb->ki_cur_seg = 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001424 return 0;
1425}
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
1428 * aio_setup_iocb:
1429 * Performs the initial checks and aio retry method
1430 * setup for the kiocb at the time of io submission.
1431 */
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001432static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
1434 struct file *file = kiocb->ki_filp;
1435 ssize_t ret = 0;
1436
1437 switch (kiocb->ki_opcode) {
1438 case IOCB_CMD_PREAD:
1439 ret = -EBADF;
1440 if (unlikely(!(file->f_mode & FMODE_READ)))
1441 break;
1442 ret = -EFAULT;
1443 if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
1444 kiocb->ki_left)))
1445 break;
Linus Torvaldsa70b52e2012-05-21 16:06:20 -07001446 ret = aio_setup_single_vector(READ, file, kiocb);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001447 if (ret)
1448 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 ret = -EINVAL;
1450 if (file->f_op->aio_read)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001451 kiocb->ki_retry = aio_rw_vect_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
1453 case IOCB_CMD_PWRITE:
1454 ret = -EBADF;
1455 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1456 break;
1457 ret = -EFAULT;
1458 if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
1459 kiocb->ki_left)))
1460 break;
Linus Torvaldsa70b52e2012-05-21 16:06:20 -07001461 ret = aio_setup_single_vector(WRITE, file, kiocb);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001462 if (ret)
1463 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 ret = -EINVAL;
1465 if (file->f_op->aio_write)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001466 kiocb->ki_retry = aio_rw_vect_retry;
1467 break;
1468 case IOCB_CMD_PREADV:
1469 ret = -EBADF;
1470 if (unlikely(!(file->f_mode & FMODE_READ)))
1471 break;
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001472 ret = aio_setup_vectored_rw(READ, kiocb, compat);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001473 if (ret)
1474 break;
1475 ret = -EINVAL;
1476 if (file->f_op->aio_read)
1477 kiocb->ki_retry = aio_rw_vect_retry;
1478 break;
1479 case IOCB_CMD_PWRITEV:
1480 ret = -EBADF;
1481 if (unlikely(!(file->f_mode & FMODE_WRITE)))
1482 break;
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001483 ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001484 if (ret)
1485 break;
1486 ret = -EINVAL;
1487 if (file->f_op->aio_write)
1488 kiocb->ki_retry = aio_rw_vect_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 break;
1490 case IOCB_CMD_FDSYNC:
1491 ret = -EINVAL;
1492 if (file->f_op->aio_fsync)
1493 kiocb->ki_retry = aio_fdsync;
1494 break;
1495 case IOCB_CMD_FSYNC:
1496 ret = -EINVAL;
1497 if (file->f_op->aio_fsync)
1498 kiocb->ki_retry = aio_fsync;
1499 break;
1500 default:
1501 dprintk("EINVAL: io_submit: no operation provided\n");
1502 ret = -EINVAL;
1503 }
1504
1505 if (!kiocb->ki_retry)
1506 return ret;
1507
1508 return 0;
1509}
1510
Adrian Bunkd5470b52008-04-29 00:58:57 -07001511static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
Jeff Moyer080d6762011-11-02 13:40:10 -07001512 struct iocb *iocb, struct kiocb_batch *batch,
1513 bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514{
1515 struct kiocb *req;
1516 struct file *file;
1517 ssize_t ret;
1518
1519 /* enforce forwards compatibility on users */
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001520 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 pr_debug("EINVAL: io_submit: reserve field set\n");
1522 return -EINVAL;
1523 }
1524
1525 /* prevent overflows */
1526 if (unlikely(
1527 (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
1528 (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
1529 ((ssize_t)iocb->aio_nbytes < 0)
1530 )) {
1531 pr_debug("EINVAL: io_submit: overflow check\n");
1532 return -EINVAL;
1533 }
1534
1535 file = fget(iocb->aio_fildes);
1536 if (unlikely(!file))
1537 return -EBADF;
1538
Jeff Moyer080d6762011-11-02 13:40:10 -07001539 req = aio_get_req(ctx, batch); /* returns with 2 references to req */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (unlikely(!req)) {
1541 fput(file);
1542 return -EAGAIN;
1543 }
Yan Zheng87e28312007-10-08 12:16:20 -07001544 req->ki_filp = file;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001545 if (iocb->aio_flags & IOCB_FLAG_RESFD) {
1546 /*
1547 * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
1548 * instance of the file* now. The file descriptor must be
1549 * an eventfd() fd, and will be signaled for each completed
1550 * event using the eventfd_signal() function.
1551 */
Davide Libenzi13389012009-06-30 11:41:11 -07001552 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001553 if (IS_ERR(req->ki_eventfd)) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001554 ret = PTR_ERR(req->ki_eventfd);
Davide Libenzi87c3a862009-03-18 17:04:19 -07001555 req->ki_eventfd = NULL;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001556 goto out_put_req;
1557 }
1558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Ken Chen212079c2005-05-01 08:59:15 -07001560 ret = put_user(req->ki_key, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (unlikely(ret)) {
1562 dprintk("EFAULT: aio_key\n");
1563 goto out_put_req;
1564 }
1565
1566 req->ki_obj.user = user_iocb;
1567 req->ki_user_data = iocb->aio_data;
1568 req->ki_pos = iocb->aio_offset;
1569
1570 req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
1571 req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
1572 req->ki_opcode = iocb->aio_lio_opcode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001574 ret = aio_setup_iocb(req, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
1576 if (ret)
1577 goto out_put_req;
1578
1579 spin_lock_irq(&ctx->ctx_lock);
Jan Kara7137c6bd2011-02-25 14:44:27 -08001580 /*
1581 * We could have raced with io_destroy() and are currently holding a
1582 * reference to ctx which should be destroyed. We cannot submit IO
1583 * since ctx gets freed as soon as io_submit() puts its reference. The
1584 * check here is reliable: io_destroy() sets ctx->dead before waiting
1585 * for outstanding IO and the barrier between these two is realized by
1586 * unlock of mm->ioctx_lock and lock of ctx->ctx_lock. Analogously we
1587 * increment ctx->reqs_active before checking for ctx->dead and the
1588 * barrier is realized by unlock and lock of ctx->ctx_lock. Thus if we
1589 * don't see ctx->dead set here, io_destroy() waits for our IO to
1590 * finish.
1591 */
1592 if (ctx->dead) {
1593 spin_unlock_irq(&ctx->ctx_lock);
1594 ret = -EINVAL;
1595 goto out_put_req;
1596 }
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001597 aio_run_iocb(req);
Benjamin LaHaiseac0b1bc2005-09-09 13:02:09 -07001598 if (!list_empty(&ctx->run_list)) {
Ken Chen954d3e92005-05-01 08:59:16 -07001599 /* drain the run list */
1600 while (__aio_run_iocbs(ctx))
1601 ;
1602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 spin_unlock_irq(&ctx->ctx_lock);
Jeff Moyercfb1e332009-10-02 18:57:36 -04001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 aio_put_req(req); /* drop extra ref to req */
1606 return 0;
1607
1608out_put_req:
1609 aio_put_req(req); /* drop extra ref to req */
1610 aio_put_req(req); /* drop i/o ref to req */
1611 return ret;
1612}
1613
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001614long do_io_submit(aio_context_t ctx_id, long nr,
1615 struct iocb __user *__user *iocbpp, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
1617 struct kioctx *ctx;
1618 long ret = 0;
Jeff Moyer080d6762011-11-02 13:40:10 -07001619 int i = 0;
Shaohua Li9f5b9422010-07-01 07:55:01 +02001620 struct blk_plug plug;
Jeff Moyer080d6762011-11-02 13:40:10 -07001621 struct kiocb_batch batch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622
1623 if (unlikely(nr < 0))
1624 return -EINVAL;
1625
Jeff Moyer75e1c702010-09-10 14:16:00 -07001626 if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
1627 nr = LONG_MAX/sizeof(*iocbpp);
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1630 return -EFAULT;
1631
1632 ctx = lookup_ioctx(ctx_id);
1633 if (unlikely(!ctx)) {
1634 pr_debug("EINVAL: io_submit: invalid context id\n");
1635 return -EINVAL;
1636 }
1637
Jeff Moyer080d6762011-11-02 13:40:10 -07001638 kiocb_batch_init(&batch, nr);
1639
Shaohua Li9f5b9422010-07-01 07:55:01 +02001640 blk_start_plug(&plug);
1641
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 /*
1643 * AKPM: should this return a partial result if some of the IOs were
1644 * successfully submitted?
1645 */
1646 for (i=0; i<nr; i++) {
1647 struct iocb __user *user_iocb;
1648 struct iocb tmp;
1649
1650 if (unlikely(__get_user(user_iocb, iocbpp + i))) {
1651 ret = -EFAULT;
1652 break;
1653 }
1654
1655 if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
1656 ret = -EFAULT;
1657 break;
1658 }
1659
Jeff Moyer080d6762011-11-02 13:40:10 -07001660 ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 if (ret)
1662 break;
1663 }
Shaohua Li9f5b9422010-07-01 07:55:01 +02001664 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Gleb Natapov69e47472012-01-08 17:07:28 +02001666 kiocb_batch_free(ctx, &batch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 put_ioctx(ctx);
1668 return i ? i : ret;
1669}
1670
Jeff Moyer9d85cba72010-05-26 14:44:26 -07001671/* sys_io_submit:
1672 * Queue the nr iocbs pointed to by iocbpp for processing. Returns
1673 * the number of iocbs queued. May return -EINVAL if the aio_context
1674 * specified by ctx_id is invalid, if nr is < 0, if the iocb at
1675 * *iocbpp[0] is not properly initialized, if the operation specified
1676 * is invalid for the file descriptor in the iocb. May fail with
1677 * -EFAULT if any of the data structures point to invalid data. May
1678 * fail with -EBADF if the file descriptor specified in the first
1679 * iocb is invalid. May fail with -EAGAIN if insufficient resources
1680 * are available to queue any iocbs. Will return 0 if nr is 0. Will
1681 * fail with -ENOSYS if not implemented.
1682 */
1683SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
1684 struct iocb __user * __user *, iocbpp)
1685{
1686 return do_io_submit(ctx_id, nr, iocbpp, 0);
1687}
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689/* lookup_kiocb
1690 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001692static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1693 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct list_head *pos;
Zach Brownd00689a2005-11-13 16:07:34 -08001696
1697 assert_spin_locked(&ctx->ctx_lock);
1698
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 /* TODO: use a hash or array, this sucks. */
1700 list_for_each(pos, &ctx->active_reqs) {
1701 struct kiocb *kiocb = list_kiocb(pos);
1702 if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
1703 return kiocb;
1704 }
1705 return NULL;
1706}
1707
1708/* sys_io_cancel:
1709 * Attempts to cancel an iocb previously passed to io_submit. If
1710 * the operation is successfully cancelled, the resulting event is
1711 * copied into the memory pointed to by result without being placed
1712 * into the completion queue and 0 is returned. May fail with
1713 * -EFAULT if any of the data structures pointed to are invalid.
1714 * May fail with -EINVAL if aio_context specified by ctx_id is
1715 * invalid. May fail with -EAGAIN if the iocb specified was not
1716 * cancelled. Will fail with -ENOSYS if not implemented.
1717 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001718SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1719 struct io_event __user *, result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 int (*cancel)(struct kiocb *iocb, struct io_event *res);
1722 struct kioctx *ctx;
1723 struct kiocb *kiocb;
1724 u32 key;
1725 int ret;
1726
1727 ret = get_user(key, &iocb->aio_key);
1728 if (unlikely(ret))
1729 return -EFAULT;
1730
1731 ctx = lookup_ioctx(ctx_id);
1732 if (unlikely(!ctx))
1733 return -EINVAL;
1734
1735 spin_lock_irq(&ctx->ctx_lock);
1736 ret = -EAGAIN;
1737 kiocb = lookup_kiocb(ctx, iocb, key);
1738 if (kiocb && kiocb->ki_cancel) {
1739 cancel = kiocb->ki_cancel;
1740 kiocb->ki_users ++;
1741 kiocbSetCancelled(kiocb);
1742 } else
1743 cancel = NULL;
1744 spin_unlock_irq(&ctx->ctx_lock);
1745
1746 if (NULL != cancel) {
1747 struct io_event tmp;
1748 pr_debug("calling cancel\n");
1749 memset(&tmp, 0, sizeof(tmp));
1750 tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
1751 tmp.data = kiocb->ki_user_data;
1752 ret = cancel(kiocb, &tmp);
1753 if (!ret) {
1754 /* Cancellation succeeded -- copy the result
1755 * into the user's buffer.
1756 */
1757 if (copy_to_user(result, &tmp, sizeof(tmp)))
1758 ret = -EFAULT;
1759 }
1760 } else
Wendy Cheng8f582022005-09-09 13:02:08 -07001761 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
1763 put_ioctx(ctx);
1764
1765 return ret;
1766}
1767
1768/* io_getevents:
1769 * Attempts to read at least min_nr events and up to nr events from
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001770 * the completion queue for the aio_context specified by ctx_id. If
1771 * it succeeds, the number of read events is returned. May fail with
1772 * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
1773 * out of range, if timeout is out of range. May fail with -EFAULT
1774 * if any of the memory specified is invalid. May return 0 or
1775 * < min_nr if the timeout specified by timeout has elapsed
1776 * before sufficient events are available, where timeout == NULL
1777 * specifies an infinite timeout. Note that the timeout pointed to by
1778 * timeout is relative and will be updated if not NULL and the
1779 * operation blocks. Will fail with -ENOSYS if not implemented.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001781SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
1782 long, min_nr,
1783 long, nr,
1784 struct io_event __user *, events,
1785 struct timespec __user *, timeout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
1787 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1788 long ret = -EINVAL;
1789
1790 if (likely(ioctx)) {
Namhyung Kim2e410252011-01-12 17:01:08 -08001791 if (likely(min_nr <= nr && min_nr >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 ret = read_events(ioctx, min_nr, nr, events, timeout);
1793 put_ioctx(ioctx);
1794 }
1795
Roland McGrath598af052008-04-10 15:38:45 -07001796 asmlinkage_protect(5, ret, ctx_id, min_nr, nr, events, timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return ret;
1798}