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