blob: cbd0afe77273a9130114be13e9ab813b2a00b85a [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 */
Kent Overstreetcaf41672013-05-07 16:18:35 -070011#define pr_fmt(fmt) "%s: " fmt, __func__
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Paul Gortmaker630d9c42011-11-16 23:57:37 -050018#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/syscalls.h>
Jens Axboeb9d128f2009-10-29 13:59:26 +010020#include <linux/backing-dev.h>
Badari Pulavarty027445c2006-09-30 23:28:46 -070021#include <linux/uio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#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 Moyer9d85cba2010-05-26 14:44:26 -070037#include <linux/compat.h>
Gu Zheng36bc08c2013-07-16 17:56:16 +080038#include <linux/anon_inodes.h>
39#include <linux/migrate.h>
40#include <linux/ramfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42#include <asm/kmap_types.h>
43#include <asm/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Al Viro68d70d02013-06-19 15:26:04 +040045#include "internal.h"
46
Kent Overstreet4e179bc2013-05-07 16:18:33 -070047#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
Kent Overstreet4e179bc2013-05-07 16:18:33 -070066
Kent Overstreet4e179bc2013-05-07 16:18:33 -070067struct kioctx {
68 atomic_t users;
Kent Overstreet36f55882013-05-07 16:18:41 -070069 atomic_t dead;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070070
71 /* This needs improving */
72 unsigned long user_id;
73 struct hlist_node list;
74
Kent Overstreet3e845ce2013-05-07 16:18:51 -070075 /*
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 *
Kent Overstreet58c85dc2013-05-07 16:18:55 -070079 * The real limit is nr_events - 1, which will be larger (see
Kent Overstreet3e845ce2013-05-07 16:18:51 -070080 * aio_setup_ring())
81 */
Kent Overstreet4e179bc2013-05-07 16:18:33 -070082 unsigned max_reqs;
83
Kent Overstreet58c85dc2013-05-07 16:18:55 -070084 /* Size of ringbuffer, in units of struct io_event */
85 unsigned nr_events;
Kent Overstreet4e179bc2013-05-07 16:18:33 -070086
Kent Overstreet58c85dc2013-05-07 16:18:55 -070087 unsigned long mmap_base;
88 unsigned long mmap_size;
89
90 struct page **ring_pages;
91 long nr_pages;
92
Kent Overstreet4e23bca2013-05-07 16:18:56 -070093 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
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700105 struct {
106 struct mutex ring_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700107 wait_queue_head_t wait;
108 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700109
110 struct {
111 unsigned tail;
112 spinlock_t completion_lock;
Kent Overstreet4e23bca2013-05-07 16:18:56 -0700113 } ____cacheline_aligned_in_smp;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700114
115 struct page *internal_pages[AIO_RING_PAGES];
Gu Zheng36bc08c2013-07-16 17:56:16 +0800116 struct file *aio_ring_file;
Kent Overstreet4e179bc2013-05-07 16:18:33 -0700117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119/*------ sysctl variables----*/
Zach Brownd55b5fd2005-11-07 00:59:31 -0800120static 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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*----end sysctl variables---*/
124
Christoph Lametere18b8902006-12-06 20:33:20 -0800125static struct kmem_cache *kiocb_cachep;
126static struct kmem_cache *kioctx_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128/* 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{
Christoph Lameter0a31bd52007-05-06 14:49:57 -0700134 kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
135 kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Kent Overstreetcaf41672013-05-07 16:18:35 -0700137 pr_debug("sizeof(struct page) = %zu\n", sizeof(struct page));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 return 0;
140}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700141__initcall(aio_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143static void aio_free_ring(struct kioctx *ctx)
144{
Gu Zheng36bc08c2013-07-16 17:56:16 +0800145 int i;
146 struct file *aio_ring_file = ctx->aio_ring_file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Gu Zheng36bc08c2013-07-16 17:56:16 +0800148 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]));
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700151 put_page(ctx->ring_pages[i]);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700154 if (ctx->ring_pages && ctx->ring_pages != ctx->internal_pages)
155 kfree(ctx->ring_pages);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800156
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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Gu Zheng36bc08c2013-07-16 17:56:16 +0800169static 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;
210}
211
212static const struct address_space_operations aio_ctx_aops = {
213 .set_page_dirty = aio_set_page_dirty,
214 .migratepage = aio_migratepage,
215};
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217static int aio_setup_ring(struct kioctx *ctx)
218{
219 struct aio_ring *ring;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 unsigned nr_events = ctx->max_reqs;
Zach Brown41003a72013-05-07 16:18:25 -0700221 struct mm_struct *mm = current->mm;
Michel Lespinasse41badc12013-02-22 16:32:47 -0800222 unsigned long size, populate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 int nr_pages;
Gu Zheng36bc08c2013-07-16 17:56:16 +0800224 int i;
225 struct file *file;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Gu Zheng36bc08c2013-07-16 17:56:16 +0800233 nr_pages = PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (nr_pages < 0)
235 return -EINVAL;
236
Gu Zheng36bc08c2013-07-16 17:56:16 +0800237 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Gu Zheng36bc08c2013-07-16 17:56:16 +0800243 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);
262
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700263 ctx->ring_pages = ctx->internal_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 if (nr_pages > AIO_RING_PAGES) {
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700265 ctx->ring_pages = kcalloc(nr_pages, sizeof(struct page *),
266 GFP_KERNEL);
267 if (!ctx->ring_pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 }
270
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700271 ctx->mmap_size = nr_pages * PAGE_SIZE;
272 pr_debug("attempting mmap of %lu bytes\n", ctx->mmap_size);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800273
Zach Brown41003a72013-05-07 16:18:25 -0700274 down_write(&mm->mmap_sem);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800275 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);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700278 if (IS_ERR((void *)ctx->mmap_base)) {
Zach Brown41003a72013-05-07 16:18:25 -0700279 up_write(&mm->mmap_sem);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700280 ctx->mmap_size = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 aio_free_ring(ctx);
282 return -EAGAIN;
283 }
Gu Zheng36bc08c2013-07-16 17:56:16 +0800284 up_write(&mm->mmap_sem);
285
286 mm_populate(ctx->mmap_base, populate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700288 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);
Gu Zheng36bc08c2013-07-16 17:56:16 +0800291 for (i = 0; i < ctx->nr_pages; i++)
292 put_page(ctx->ring_pages[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700294 if (unlikely(ctx->nr_pages != nr_pages)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 aio_free_ring(ctx);
296 return -EAGAIN;
297 }
298
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700299 ctx->user_id = ctx->mmap_base;
300 ctx->nr_events = nr_events; /* trusted copy */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700302 ring = kmap_atomic(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 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);
Cong Wange8e3c3d2011-11-25 23:14:27 +0800310 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700311 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 return 0;
314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316#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
Kent Overstreet0460fef2013-05-07 16:18:49 -0700320void 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
Kent Overstreet906b9732013-05-07 16:18:31 -0700336static int kiocb_cancel(struct kioctx *ctx, struct kiocb *kiocb,
337 struct io_event *res)
338{
Kent Overstreet0460fef2013-05-07 16:18:49 -0700339 kiocb_cancel_fn *old, *cancel;
Kent Overstreet906b9732013-05-07 16:18:31 -0700340 int ret = -EINVAL;
341
Kent Overstreet0460fef2013-05-07 16:18:49 -0700342 /*
343 * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
344 * actually has a cancel function, hence the cmpxchg()
345 */
Kent Overstreet906b9732013-05-07 16:18:31 -0700346
Kent Overstreet0460fef2013-05-07 16:18:49 -0700347 cancel = ACCESS_ONCE(kiocb->ki_cancel);
348 do {
349 if (!cancel || cancel == KIOCB_CANCELLED)
350 return ret;
Kent Overstreet906b9732013-05-07 16:18:31 -0700351
Kent Overstreet0460fef2013-05-07 16:18:49 -0700352 old = cancel;
353 cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
354 } while (cancel != old);
355
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);
Kent Overstreet906b9732013-05-07 16:18:31 -0700365
366 return ret;
367}
368
Kent Overstreet36f55882013-05-07 16:18:41 -0700369static 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{
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700382 struct aio_ring *ring;
Kent Overstreet36f55882013-05-07 16:18:41 -0700383 struct io_event res;
384 struct kiocb *req;
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700385 unsigned head, avail;
Kent Overstreet36f55882013-05-07 16:18:41 -0700386
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
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700399 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700400 head = ring->head;
401 kunmap_atomic(ring);
402
403 while (atomic_read(&ctx->reqs_active) > 0) {
Benjamin LaHaise03e04f02013-05-24 15:55:38 -0700404 wait_event(ctx->wait,
405 head != ctx->tail ||
406 atomic_read(&ctx->reqs_active) <= 0);
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700407
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700408 avail = (head <= ctx->tail ? ctx->tail : ctx->nr_events) - head;
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700409
410 atomic_sub(avail, &ctx->reqs_active);
411 head += avail;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700412 head %= ctx->nr_events;
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700413 }
414
415 WARN_ON(atomic_read(&ctx->reqs_active) < 0);
Kent Overstreet36f55882013-05-07 16:18:41 -0700416
417 aio_free_ring(ctx);
418
Kent Overstreet36f55882013-05-07 16:18:41 -0700419 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437/* 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{
Zach Brown41003a72013-05-07 16:18:25 -0700442 struct mm_struct *mm = current->mm;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct kioctx *ctx;
Al Viroe23754f2012-03-06 14:33:22 -0500444 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
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
Al Viro2dd542b2012-03-10 23:10:35 -0500453 if (!nr_events || (unsigned long)nr_events > aio_max_nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return ERR_PTR(-EAGAIN);
455
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800456 ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!ctx)
458 return ERR_PTR(-ENOMEM);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 ctx->max_reqs = nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Al Viro86b62a22012-03-07 05:16:35 +0000462 atomic_set(&ctx->users, 2);
Kent Overstreet36f55882013-05-07 16:18:41 -0700463 atomic_set(&ctx->dead, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 spin_lock_init(&ctx->ctx_lock);
Kent Overstreet0460fef2013-05-07 16:18:49 -0700465 spin_lock_init(&ctx->completion_lock);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700466 mutex_init(&ctx->ring_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 init_waitqueue_head(&ctx->wait);
468
469 INIT_LIST_HEAD(&ctx->active_reqs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 if (aio_setup_ring(ctx) < 0)
472 goto out_freectx;
473
474 /* limit the number of system wide aios */
Al Viro9fa1cb32012-03-10 23:14:05 -0500475 spin_lock(&aio_nr_lock);
Al Viro2dd542b2012-03-10 23:10:35 -0500476 if (aio_nr + nr_events > aio_max_nr ||
477 aio_nr + nr_events < aio_nr) {
Al Viro9fa1cb32012-03-10 23:14:05 -0500478 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto out_cleanup;
Al Viro2dd542b2012-03-10 23:10:35 -0500480 }
481 aio_nr += ctx->max_reqs;
Al Viro9fa1cb32012-03-10 23:14:05 -0500482 spin_unlock(&aio_nr_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Jeff Moyer39fa0032008-04-29 01:03:48 -0700484 /* now link into global list. */
Jens Axboeabf137d2008-12-09 08:11:22 +0100485 spin_lock(&mm->ioctx_lock);
486 hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
487 spin_unlock(&mm->ioctx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Kent Overstreetcaf41672013-05-07 16:18:35 -0700489 pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700490 ctx, ctx->user_id, mm, ctx->nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return ctx;
492
493out_cleanup:
Al Viroe23754f2012-03-06 14:33:22 -0500494 err = -EAGAIN;
495 aio_free_ring(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496out_freectx:
Gu Zheng36bc08c2013-07-16 17:56:16 +0800497 if (ctx->aio_ring_file)
498 fput(ctx->aio_ring_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 kmem_cache_free(kioctx_cachep, ctx);
Kent Overstreetcaf41672013-05-07 16:18:35 -0700500 pr_debug("error allocating ioctx %d\n", err);
Al Viroe23754f2012-03-06 14:33:22 -0500501 return ERR_PTR(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Kent Overstreet36f55882013-05-07 16:18:41 -0700504static void kill_ioctx_work(struct work_struct *work)
505{
506 struct kioctx *ctx = container_of(work, struct kioctx, rcu_work);
507
508 wake_up_all(&ctx->wait);
509 put_ioctx(ctx);
510}
511
512static void kill_ioctx_rcu(struct rcu_head *head)
513{
514 struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
515
516 INIT_WORK(&ctx->rcu_work, kill_ioctx_work);
517 schedule_work(&ctx->rcu_work);
518}
519
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * the rapid destruction of the kioctx.
524 */
Kent Overstreet36f55882013-05-07 16:18:41 -0700525static void kill_ioctx(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Kent Overstreet36f55882013-05-07 16:18:41 -0700527 if (!atomic_xchg(&ctx->dead, 1)) {
528 hlist_del_rcu(&ctx->list);
Al Viro06af1212012-03-20 16:26:24 -0400529
Kent Overstreet36f55882013-05-07 16:18:41 -0700530 /*
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700531 * 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).
Kent Overstreet36f55882013-05-07 16:18:41 -0700536 */
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700537 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550/* wait_on_sync_kiocb:
551 * Waits on the given sync kiocb to complete.
552 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800553ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554{
Kent Overstreet11599eb2013-05-07 16:18:39 -0700555 while (atomic_read(&iocb->ki_users)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 set_current_state(TASK_UNINTERRUPTIBLE);
Kent Overstreet11599eb2013-05-07 16:18:39 -0700557 if (!atomic_read(&iocb->ki_users))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
Jeff Moyer41d10da2007-10-16 23:27:20 -0700559 io_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561 __set_current_state(TASK_RUNNING);
562 return iocb->ki_user_data;
563}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700564EXPORT_SYMBOL(wait_on_sync_kiocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Kent Overstreet36f55882013-05-07 16:18:41 -0700566/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800574void exit_aio(struct mm_struct *mm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Jens Axboeabf137d2008-12-09 08:11:22 +0100576 struct kioctx *ctx;
Kent Overstreet36f55882013-05-07 16:18:41 -0700577 struct hlist_node *n;
Jens Axboeabf137d2008-12-09 08:11:22 +0100578
Kent Overstreet36f55882013-05-07 16:18:41 -0700579 hlist_for_each_entry_safe(ctx, n, &mm->ioctx_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (1 != atomic_read(&ctx->users))
581 printk(KERN_DEBUG
582 "exit_aio:ioctx still alive: %d %d %d\n",
Kent Overstreet36f55882013-05-07 16:18:41 -0700583 atomic_read(&ctx->users),
584 atomic_read(&ctx->dead),
Kent Overstreet11599eb2013-05-07 16:18:39 -0700585 atomic_read(&ctx->reqs_active));
Al Viro936af152012-04-20 21:49:41 -0400586 /*
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.
Al Viro936af152012-04-20 21:49:41 -0400593 */
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700594 ctx->mmap_size = 0;
Kent Overstreet36f55882013-05-07 16:18:41 -0700595
Kent Overstreet4fcc7122013-06-12 14:04:59 -0700596 kill_ioctx(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/* aio_get_req
Kent Overstreet11599eb2013-05-07 16:18:39 -0700601 * Allocate a slot for an aio request. Increments the ki_users count
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 * of the kioctx so that the kioctx stays around until all requests are
603 * complete. Returns NULL if no requests are free.
604 *
Kent Overstreet11599eb2013-05-07 16:18:39 -0700605 * Returns with kiocb->ki_users set to 2. The io submit code path holds
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * 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 */
Kent Overstreeta1c8eae2013-05-07 16:18:53 -0700610static inline struct kiocb *aio_get_req(struct kioctx *ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Kent Overstreeta1c8eae2013-05-07 16:18:53 -0700612 struct kiocb *req;
613
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700614 if (atomic_read(&ctx->reqs_active) >= ctx->nr_events)
Kent Overstreeta1c8eae2013-05-07 16:18:53 -0700615 return NULL;
616
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700617 if (atomic_inc_return(&ctx->reqs_active) > ctx->nr_events - 1)
Kent Overstreeta1c8eae2013-05-07 16:18:53 -0700618 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Kent Overstreet0460fef2013-05-07 16:18:49 -0700620 req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if (unlikely(!req))
Kent Overstreeta1c8eae2013-05-07 16:18:53 -0700622 goto out_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Kent Overstreet11599eb2013-05-07 16:18:39 -0700624 atomic_set(&req->ki_users, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 req->ki_ctx = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 return req;
Kent Overstreeta1c8eae2013-05-07 16:18:53 -0700628out_put:
629 atomic_dec(&ctx->reqs_active);
630 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Kent Overstreet11599eb2013-05-07 16:18:39 -0700633static void kiocb_free(struct kiocb *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Kent Overstreet1d98ebf2013-05-07 16:18:37 -0700635 if (req->ki_filp)
636 fput(req->ki_filp);
Davide Libenzi13389012009-06-30 11:41:11 -0700637 if (req->ki_eventfd != NULL)
638 eventfd_ctx_put(req->ki_eventfd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 if (req->ki_dtor)
640 req->ki_dtor(req);
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700641 if (req->ki_iovec != &req->ki_inline_vec)
642 kfree(req->ki_iovec);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 kmem_cache_free(kiocb_cachep, req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Kent Overstreet2d684492013-05-07 16:18:29 -0700646void aio_put_req(struct kiocb *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Kent Overstreet11599eb2013-05-07 16:18:39 -0700648 if (atomic_dec_and_test(&req->ki_users))
649 kiocb_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700651EXPORT_SYMBOL(aio_put_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Adrian Bunkd5470b52008-04-29 00:58:57 -0700653static struct kioctx *lookup_ioctx(unsigned long ctx_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Jens Axboeabf137d2008-12-09 08:11:22 +0100655 struct mm_struct *mm = current->mm;
Jeff Moyer65c24492009-03-18 17:04:21 -0700656 struct kioctx *ctx, *ret = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Jens Axboeabf137d2008-12-09 08:11:22 +0100658 rcu_read_lock();
659
Sasha Levinb67bfe02013-02-27 17:06:00 -0800660 hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
Kent Overstreet36f55882013-05-07 16:18:41 -0700661 if (ctx->user_id == ctx_id) {
662 atomic_inc(&ctx->users);
Jeff Moyer65c24492009-03-18 17:04:21 -0700663 ret = ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 break;
665 }
Jens Axboeabf137d2008-12-09 08:11:22 +0100666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Jens Axboeabf137d2008-12-09 08:11:22 +0100668 rcu_read_unlock();
Jeff Moyer65c24492009-03-18 17:04:21 -0700669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672/* aio_complete
673 * Called when the io request on the given iocb is complete.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
Kent Overstreet2d684492013-05-07 16:18:29 -0700675void aio_complete(struct kiocb *iocb, long res, long res2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 struct kioctx *ctx = iocb->ki_ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 struct aio_ring *ring;
Kent Overstreet21b40202013-05-07 16:18:47 -0700679 struct io_event *ev_page, *event;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned long flags;
Kent Overstreet21b40202013-05-07 16:18:47 -0700681 unsigned tail, pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Zach Brown20dcae32005-11-13 16:07:33 -0800683 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
690 if (is_sync_kiocb(iocb)) {
Kent Overstreet11599eb2013-05-07 16:18:39 -0700691 BUG_ON(atomic_read(&iocb->ki_users) != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 iocb->ki_user_data = res;
Kent Overstreet11599eb2013-05-07 16:18:39 -0700693 atomic_set(&iocb->ki_users, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 wake_up_process(iocb->ki_obj.tsk);
Kent Overstreet2d684492013-05-07 16:18:29 -0700695 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
697
Kent Overstreet36f55882013-05-07 16:18:41 -0700698 /*
Kent Overstreet36f55882013-05-07 16:18:41 -0700699 * Take rcu_read_lock() in case the kioctx is being destroyed, as we
700 * need to issue a wakeup after decrementing reqs_active.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Kent Overstreet36f55882013-05-07 16:18:41 -0700702 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Kent Overstreet0460fef2013-05-07 16:18:49 -0700704 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 }
Kent Overstreet11599eb2013-05-07 16:18:39 -0700711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 /*
713 * cancelled requests don't get events, userland was given one
714 * when the event got cancelled.
715 */
Kent Overstreet0460fef2013-05-07 16:18:49 -0700716 if (unlikely(xchg(&iocb->ki_cancel,
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700717 KIOCB_CANCELLED) == KIOCB_CANCELLED)) {
718 atomic_dec(&ctx->reqs_active);
719 /* Still need the wake_up in case free_ioctx is waiting */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 goto put_rq;
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Kent Overstreet0460fef2013-05-07 16:18:49 -0700723 /*
724 * Add a completion event to the ring buffer. Must be done holding
Tang Chen4b30f072013-07-03 15:09:16 -0700725 * ctx->completion_lock to prevent other code from messing with the tail
Kent Overstreet0460fef2013-05-07 16:18:49 -0700726 * pointer since we might be called from irq context.
727 */
728 spin_lock_irqsave(&ctx->completion_lock, flags);
729
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700730 tail = ctx->tail;
Kent Overstreet21b40202013-05-07 16:18:47 -0700731 pos = tail + AIO_EVENTS_OFFSET;
732
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700733 if (++tail >= ctx->nr_events)
Ken Chen4bf69b22005-05-01 08:59:15 -0700734 tail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700736 ev_page = kmap_atomic(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -0700737 event = ev_page + pos % AIO_EVENTS_PER_PAGE;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 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
Kent Overstreet21b40202013-05-07 16:18:47 -0700744 kunmap_atomic(ev_page);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700745 flush_dcache_page(ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
Kent Overstreet21b40202013-05-07 16:18:47 -0700746
747 pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
Kent Overstreetcaf41672013-05-07 16:18:35 -0700748 ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
749 res, res2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
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
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700756 ctx->tail = tail;
Kent Overstreet21b40202013-05-07 16:18:47 -0700757
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700758 ring = kmap_atomic(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 ring->tail = tail;
Cong Wange8e3c3d2011-11-25 23:14:27 +0800760 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700761 flush_dcache_page(ctx->ring_pages[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Kent Overstreet0460fef2013-05-07 16:18:49 -0700763 spin_unlock_irqrestore(&ctx->completion_lock, flags);
764
Kent Overstreet21b40202013-05-07 16:18:47 -0700765 pr_debug("added to ring %p at [%u]\n", iocb, tail);
Davide Libenzi8d1c98b2008-04-10 21:29:19 -0700766
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 */
Davide Libenzi87c3a862009-03-18 17:04:19 -0700772 if (iocb->ki_eventfd != NULL)
Davide Libenzi8d1c98b2008-04-10 21:29:19 -0700773 eventfd_signal(iocb->ki_eventfd, 1);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775put_rq:
776 /* everything turned out well, dispose of the aiocb. */
Kent Overstreet11599eb2013-05-07 16:18:39 -0700777 aio_put_req(iocb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Quentin Barnes6cb2a212008-03-19 17:00:39 -0700779 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (waitqueue_active(&ctx->wait))
788 wake_up(&ctx->wait);
789
Kent Overstreet36f55882013-05-07 16:18:41 -0700790 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
H Hartley Sweeten385773e2009-09-22 16:43:53 -0700792EXPORT_SYMBOL(aio_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Kent Overstreeta31ad382013-05-07 16:18:45 -0700794/* aio_read_events
795 * Pull an event off of the ioctx's event ring. Returns the number of
796 * events fetched
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 */
Kent Overstreeta31ad382013-05-07 16:18:45 -0700798static long aio_read_events_ring(struct kioctx *ctx,
799 struct io_event __user *event, long nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 struct aio_ring *ring;
Kent Overstreeta31ad382013-05-07 16:18:45 -0700802 unsigned head, pos;
803 long ret = 0;
804 int copy_ret;
805
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700806 mutex_lock(&ctx->ring_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700808 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -0700809 head = ring->head;
810 kunmap_atomic(ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700812 pr_debug("h%u t%u m%u\n", head, ctx->tail, ctx->nr_events);
Kent Overstreeta31ad382013-05-07 16:18:45 -0700813
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700814 if (head == ctx->tail)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 goto out;
816
Kent Overstreeta31ad382013-05-07 16:18:45 -0700817 while (ret < nr) {
818 long avail;
819 struct io_event *ev;
820 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700822 avail = (head <= ctx->tail ? ctx->tail : ctx->nr_events) - head;
823 if (head == ctx->tail)
Kent Overstreeta31ad382013-05-07 16:18:45 -0700824 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;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700831 page = ctx->ring_pages[pos / AIO_EVENTS_PER_PAGE];
Kent Overstreeta31ad382013-05-07 16:18:45 -0700832 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;
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700846 head %= ctx->nr_events;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700849 ring = kmap_atomic(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -0700850 ring->head = head;
Zhao Hongjiang91d80a82013-04-26 11:03:53 +0800851 kunmap_atomic(ring);
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700852 flush_dcache_page(ctx->ring_pages[0]);
Kent Overstreeta31ad382013-05-07 16:18:45 -0700853
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700854 pr_debug("%li h%u t%u\n", ret, head, ctx->tail);
Kent Overstreet3e845ce2013-05-07 16:18:51 -0700855
856 atomic_sub(ret, &ctx->reqs_active);
Kent Overstreeta31ad382013-05-07 16:18:45 -0700857out:
Kent Overstreet58c85dc2013-05-07 16:18:55 -0700858 mutex_unlock(&ctx->ring_lock);
Kent Overstreeta31ad382013-05-07 16:18:45 -0700859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return ret;
861}
862
Kent Overstreeta31ad382013-05-07 16:18:45 -0700863static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
864 struct io_event __user *event, long *i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Kent Overstreeta31ad382013-05-07 16:18:45 -0700866 long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Kent Overstreeta31ad382013-05-07 16:18:45 -0700868 if (ret > 0)
869 *i += ret;
870
871 if (unlikely(atomic_read(&ctx->dead)))
872 ret = -EINVAL;
873
874 if (!*i)
875 *i = ret;
876
877 return ret < 0 || *i >= min_nr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878}
879
Kent Overstreeta31ad382013-05-07 16:18:45 -0700880static long read_events(struct kioctx *ctx, long min_nr, long nr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 struct io_event __user *event,
882 struct timespec __user *timeout)
883{
Kent Overstreeta31ad382013-05-07 16:18:45 -0700884 ktime_t until = { .tv64 = KTIME_MAX };
885 long ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 if (timeout) {
888 struct timespec ts;
Kent Overstreeta31ad382013-05-07 16:18:45 -0700889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
Kent Overstreeta31ad382013-05-07 16:18:45 -0700891 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Kent Overstreeta31ad382013-05-07 16:18:45 -0700893 until = timespec_to_ktime(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
895
Kent Overstreeta31ad382013-05-07 16:18:45 -0700896 /*
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Kent Overstreeta31ad382013-05-07 16:18:45 -0700913 if (!ret && signal_pending(current))
914 ret = -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Kent Overstreeta31ad382013-05-07 16:18:45 -0700916 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917}
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919/* 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 */
Heiko Carstens002c8972009-01-14 14:14:18 +0100932SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933{
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;
Zach Brownd55b5fd2005-11-07 00:59:31 -0800943 if (unlikely(ctx || nr_events == 0)) {
944 pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
945 ctx, nr_events);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 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);
Al Viroa2e18592012-03-20 16:27:57 -0400953 if (ret)
Kent Overstreet36f55882013-05-07 16:18:41 -0700954 kill_ioctx(ioctx);
Al Viroa2e18592012-03-20 16:27:57 -0400955 put_ioctx(ioctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
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
Satoru Takeuchi642b5122010-08-05 11:23:11 -0700965 * implemented. May fail with -EINVAL if the context pointed to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * is invalid.
967 */
Heiko Carstens002c8972009-01-14 14:14:18 +0100968SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
970 struct kioctx *ioctx = lookup_ioctx(ctx);
971 if (likely(NULL != ioctx)) {
Kent Overstreet36f55882013-05-07 16:18:41 -0700972 kill_ioctx(ioctx);
Al Viroa2e18592012-03-20 16:27:57 -0400973 put_ioctx(ioctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975 }
976 pr_debug("EINVAL: io_destroy: invalid context id\n");
977 return -EINVAL;
978}
979
Badari Pulavartyeed4e512006-09-30 23:28:49 -0700980static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
981{
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++;
995 }
996 }
997
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);
1001}
1002
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001003typedef 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 struct file *file = iocb->ki_filp;
1009 struct address_space *mapping = file->f_mapping;
1010 struct inode *inode = mapping->host;
1011 ssize_t ret = 0;
1012
Rusty Russellc2ec6682008-02-08 04:20:15 -08001013 /* This matches the pread()/pwrite() logic */
1014 if (iocb->ki_pos < 0)
1015 return -EINVAL;
1016
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001017 if (rw == WRITE)
Al Viro8d71db42013-03-19 21:01:03 -04001018 file_start_write(file);
Zach Brown897f15f2005-09-30 11:58:55 -07001019 do {
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001020 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);
Badari Pulavarty027445c2006-09-30 23:28:46 -07001025
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001026 /* retry all partial writes. retry partial reads as long as its a
1027 * regular file. */
Zach Brown353fb072005-09-30 11:58:56 -07001028 } while (ret > 0 && iocb->ki_left > 0 &&
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001029 (rw == WRITE ||
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001030 (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001031 if (rw == WRITE)
Al Viro8d71db42013-03-19 21:01:03 -04001032 file_end_write(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 /* This means we must have transferred all that we could */
1035 /* No need to retry anymore */
1036 if ((ret == 0) || (iocb->ki_left == 0))
1037 ret = iocb->ki_nbytes - iocb->ki_left;
1038
Rusty Russell7adfa2f2008-02-08 04:20:14 -08001039 /* If we managed to write some out we return that, rather than
1040 * the eventual error. */
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001041 if (rw == WRITE
Zach Brown41003a72013-05-07 16:18:25 -07001042 && ret < 0 && ret != -EIOCBQUEUED
Rusty Russell7adfa2f2008-02-08 04:20:14 -08001043 && iocb->ki_nbytes - iocb->ki_left)
1044 ret = iocb->ki_nbytes - iocb->ki_left;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 return ret;
1047}
1048
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001049static ssize_t aio_setup_vectored_rw(int rw, struct kiocb *kiocb, bool compat)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001050{
1051 ssize_t ret;
1052
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001053 kiocb->ki_nr_segs = kiocb->ki_nbytes;
1054
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001055#ifdef CONFIG_COMPAT
1056 if (compat)
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001057 ret = compat_rw_copy_check_uvector(rw,
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001058 (struct compat_iovec __user *)kiocb->ki_buf,
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001059 kiocb->ki_nr_segs, 1, &kiocb->ki_inline_vec,
Christopher Yeohac34ebb2012-05-31 16:26:42 -07001060 &kiocb->ki_iovec);
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001061 else
1062#endif
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001063 ret = rw_copy_check_uvector(rw,
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001064 (struct iovec __user *)kiocb->ki_buf,
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001065 kiocb->ki_nr_segs, 1, &kiocb->ki_inline_vec,
Christopher Yeohac34ebb2012-05-31 16:26:42 -07001066 &kiocb->ki_iovec);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001067 if (ret < 0)
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001068 return ret;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001069
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001070 /* ki_nbytes now reflect bytes instead of segs */
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001071 kiocb->ki_nbytes = ret;
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001072 return 0;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001073}
1074
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001075static ssize_t aio_setup_single_vector(int rw, struct kiocb *kiocb)
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001076{
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001077 if (unlikely(!access_ok(!rw, kiocb->ki_buf, kiocb->ki_nbytes)))
1078 return -EFAULT;
Linus Torvaldsa70b52e2012-05-21 16:06:20 -07001079
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001080 kiocb->ki_iovec = &kiocb->ki_inline_vec;
1081 kiocb->ki_iovec->iov_base = kiocb->ki_buf;
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001082 kiocb->ki_iovec->iov_len = kiocb->ki_nbytes;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001083 kiocb->ki_nr_segs = 1;
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001084 return 0;
1085}
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087/*
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 */
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001092static ssize_t aio_run_iocb(struct kiocb *req, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001094 struct file *file = req->ki_filp;
1095 ssize_t ret;
1096 int rw;
1097 fmode_t mode;
1098 aio_rw_op *rw_op;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001100 switch (req->ki_opcode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 case IOCB_CMD_PREAD:
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001102 case IOCB_CMD_PREADV:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001103 mode = FMODE_READ;
1104 rw = READ;
1105 rw_op = file->f_op->aio_read;
1106 goto rw_common;
1107
1108 case IOCB_CMD_PWRITE:
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001109 case IOCB_CMD_PWRITEV:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001110 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);
Badari Pulavartyeed4e512006-09-30 23:28:49 -07001125 if (ret)
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001126 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 break;
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 case IOCB_CMD_FDSYNC:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001139 if (!file->f_op->aio_fsync)
1140 return -EINVAL;
1141
1142 ret = file->f_op->aio_fsync(req, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 break;
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 case IOCB_CMD_FSYNC:
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001146 if (!file->f_op->aio_fsync)
1147 return -EINVAL;
1148
1149 ret = file->f_op->aio_fsync(req, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 break;
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 default:
Kent Overstreetcaf41672013-05-07 16:18:35 -07001153 pr_debug("EINVAL: no operation provided\n");
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001154 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001157 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 return 0;
1170}
1171
Adrian Bunkd5470b52008-04-29 00:58:57 -07001172static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001173 struct iocb *iocb, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174{
1175 struct kiocb *req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 ssize_t ret;
1177
1178 /* enforce forwards compatibility on users */
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001179 if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001180 pr_debug("EINVAL: reserve field set\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 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
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001194 req = aio_get_req(ctx);
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001195 if (unlikely(!req))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return -EAGAIN;
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001197
1198 req->ki_filp = fget(iocb->aio_fildes);
1199 if (unlikely(!req->ki_filp)) {
1200 ret = -EBADF;
1201 goto out_put_req;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
Kent Overstreet1d98ebf2013-05-07 16:18:37 -07001203
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001204 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 */
Davide Libenzi13389012009-06-30 11:41:11 -07001211 req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001212 if (IS_ERR(req->ki_eventfd)) {
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001213 ret = PTR_ERR(req->ki_eventfd);
Davide Libenzi87c3a862009-03-18 17:04:19 -07001214 req->ki_eventfd = NULL;
Davide Libenzi9c3060b2007-05-10 22:23:21 -07001215 goto out_put_req;
1216 }
1217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Kent Overstreet8a660892013-05-07 16:19:10 -07001219 ret = put_user(KIOCB_KEY, &user_iocb->aio_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (unlikely(ret)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001221 pr_debug("EFAULT: aio_key\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Kent Overstreet41ef4eb2013-05-07 16:19:11 -07001233 ret = aio_run_iocb(req, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (ret)
1235 goto out_put_req;
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 aio_put_req(req); /* drop extra ref to req */
1238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239out_put_req:
Kent Overstreet11599eb2013-05-07 16:18:39 -07001240 atomic_dec(&ctx->reqs_active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 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
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001246long do_io_submit(aio_context_t ctx_id, long nr,
1247 struct iocb __user *__user *iocbpp, bool compat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct kioctx *ctx;
1250 long ret = 0;
Jeff Moyer080d6762011-11-02 13:40:10 -07001251 int i = 0;
Shaohua Li9f5b9422010-07-01 07:55:01 +02001252 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (unlikely(nr < 0))
1255 return -EINVAL;
1256
Jeff Moyer75e1c702010-09-10 14:16:00 -07001257 if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
1258 nr = LONG_MAX/sizeof(*iocbpp);
1259
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
1261 return -EFAULT;
1262
1263 ctx = lookup_ioctx(ctx_id);
1264 if (unlikely(!ctx)) {
Kent Overstreetcaf41672013-05-07 16:18:35 -07001265 pr_debug("EINVAL: invalid context id\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return -EINVAL;
1267 }
1268
Shaohua Li9f5b9422010-07-01 07:55:01 +02001269 blk_start_plug(&plug);
1270
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 /*
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
Kent Overstreeta1c8eae2013-05-07 16:18:53 -07001289 ret = io_submit_one(ctx, user_iocb, &tmp, compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (ret)
1291 break;
1292 }
Shaohua Li9f5b9422010-07-01 07:55:01 +02001293 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 put_ioctx(ctx);
1296 return i ? i : ret;
1297}
1298
Jeff Moyer9d85cba2010-05-26 14:44:26 -07001299/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317/* lookup_kiocb
1318 * Finds a given iocb for cancellation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Adrian Bunk25ee7e32005-04-25 08:18:14 -07001320static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
1321 u32 key)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322{
1323 struct list_head *pos;
Zach Brownd00689a2005-11-13 16:07:34 -08001324
1325 assert_spin_locked(&ctx->ctx_lock);
1326
Kent Overstreet8a660892013-05-07 16:19:10 -07001327 if (key != KIOCB_KEY)
1328 return NULL;
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 /* TODO: use a hash or array, this sucks. */
1331 list_for_each(pos, &ctx->active_reqs) {
1332 struct kiocb *kiocb = list_kiocb(pos);
Kent Overstreet8a660892013-05-07 16:19:10 -07001333 if (kiocb->ki_obj.user == iocb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 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 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001349SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
1350 struct io_event __user *, result)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Kent Overstreet906b9732013-05-07 16:18:31 -07001352 struct io_event res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 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);
Kent Overstreet906b9732013-05-07 16:18:31 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 kiocb = lookup_kiocb(ctx, iocb, key);
Kent Overstreet906b9732013-05-07 16:18:31 -07001369 if (kiocb)
1370 ret = kiocb_cancel(ctx, kiocb, &res);
1371 else
1372 ret = -EINVAL;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 spin_unlock_irq(&ctx->ctx_lock);
1375
Kent Overstreet906b9732013-05-07 16:18:31 -07001376 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
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
Satoru Takeuchi642b5122010-08-05 11:23:11 -07001391 * 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
Jeff Moyer69008072013-05-24 15:55:24 -07001399 * timeout is relative. Will fail with -ENOSYS if not implemented.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 */
Heiko Carstens002c8972009-01-14 14:14:18 +01001401SYSCALL_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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406{
1407 struct kioctx *ioctx = lookup_ioctx(ctx_id);
1408 long ret = -EINVAL;
1409
1410 if (likely(ioctx)) {
Namhyung Kim2e410252011-01-12 17:01:08 -08001411 if (likely(min_nr <= nr && min_nr >= 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 ret = read_events(ioctx, min_nr, nr, events, timeout);
1413 put_ioctx(ioctx);
1414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 return ret;
1416}