aio.c 33 KB

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