aio.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.h>
  14. #include <linux/time.h>
  15. #include <linux/aio_abi.h>
  16. #include <linux/module.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/backing-dev.h>
  19. #include <linux/uio.h>
  20. #define DEBUG 0
  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. #if DEBUG > 1
  39. #define dprintk printk
  40. #else
  41. #define dprintk(x...) do { ; } while (0)
  42. #endif
  43. /*------ sysctl variables----*/
  44. static DEFINE_SPINLOCK(aio_nr_lock);
  45. unsigned long aio_nr; /* current system wide number of aio requests */
  46. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  47. /*----end sysctl variables---*/
  48. static struct kmem_cache *kiocb_cachep;
  49. static struct kmem_cache *kioctx_cachep;
  50. static struct workqueue_struct *aio_wq;
  51. /* Used for rare fput completion. */
  52. static void aio_fput_routine(struct work_struct *);
  53. static DECLARE_WORK(fput_work, aio_fput_routine);
  54. static DEFINE_SPINLOCK(fput_lock);
  55. static LIST_HEAD(fput_head);
  56. static void aio_kick_handler(struct work_struct *);
  57. static void aio_queue_work(struct kioctx *);
  58. /* aio_setup
  59. * Creates the slab caches used by the aio routines, panic on
  60. * failure as this is done early during the boot sequence.
  61. */
  62. static int __init aio_setup(void)
  63. {
  64. kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  65. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  66. aio_wq = alloc_workqueue("aio", 0, 1); /* used to limit concurrency */
  67. BUG_ON(!aio_wq);
  68. pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
  69. return 0;
  70. }
  71. __initcall(aio_setup);
  72. static void aio_free_ring(struct kioctx *ctx)
  73. {
  74. struct aio_ring_info *info = &ctx->ring_info;
  75. long i;
  76. for (i=0; i<info->nr_pages; i++)
  77. put_page(info->ring_pages[i]);
  78. if (info->mmap_size) {
  79. down_write(&ctx->mm->mmap_sem);
  80. do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
  81. up_write(&ctx->mm->mmap_sem);
  82. }
  83. if (info->ring_pages && info->ring_pages != info->internal_pages)
  84. kfree(info->ring_pages);
  85. info->ring_pages = NULL;
  86. info->nr = 0;
  87. }
  88. static int aio_setup_ring(struct kioctx *ctx)
  89. {
  90. struct aio_ring *ring;
  91. struct aio_ring_info *info = &ctx->ring_info;
  92. unsigned nr_events = ctx->max_reqs;
  93. unsigned long size;
  94. int nr_pages;
  95. /* Compensate for the ring buffer's head/tail overlap entry */
  96. nr_events += 2; /* 1 is required, 2 for good luck */
  97. size = sizeof(struct aio_ring);
  98. size += sizeof(struct io_event) * nr_events;
  99. nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
  100. if (nr_pages < 0)
  101. return -EINVAL;
  102. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
  103. info->nr = 0;
  104. info->ring_pages = info->internal_pages;
  105. if (nr_pages > AIO_RING_PAGES) {
  106. info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  107. if (!info->ring_pages)
  108. return -ENOMEM;
  109. }
  110. info->mmap_size = nr_pages * PAGE_SIZE;
  111. dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
  112. down_write(&ctx->mm->mmap_sem);
  113. info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
  114. PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
  115. 0);
  116. if (IS_ERR((void *)info->mmap_base)) {
  117. up_write(&ctx->mm->mmap_sem);
  118. info->mmap_size = 0;
  119. aio_free_ring(ctx);
  120. return -EAGAIN;
  121. }
  122. dprintk("mmap address: 0x%08lx\n", info->mmap_base);
  123. info->nr_pages = get_user_pages(current, ctx->mm,
  124. info->mmap_base, nr_pages,
  125. 1, 0, info->ring_pages, NULL);
  126. up_write(&ctx->mm->mmap_sem);
  127. if (unlikely(info->nr_pages != nr_pages)) {
  128. aio_free_ring(ctx);
  129. return -EAGAIN;
  130. }
  131. ctx->user_id = info->mmap_base;
  132. info->nr = nr_events; /* trusted copy */
  133. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  134. ring->nr = nr_events; /* user copy */
  135. ring->id = ctx->user_id;
  136. ring->head = ring->tail = 0;
  137. ring->magic = AIO_RING_MAGIC;
  138. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  139. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  140. ring->header_length = sizeof(struct aio_ring);
  141. kunmap_atomic(ring, KM_USER0);
  142. return 0;
  143. }
  144. /* aio_ring_event: returns a pointer to the event at the given index from
  145. * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
  146. */
  147. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  148. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  149. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  150. #define aio_ring_event(info, nr, km) ({ \
  151. unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
  152. struct io_event *__event; \
  153. __event = kmap_atomic( \
  154. (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
  155. __event += pos % AIO_EVENTS_PER_PAGE; \
  156. __event; \
  157. })
  158. #define put_aio_ring_event(event, km) do { \
  159. struct io_event *__event = (event); \
  160. (void)__event; \
  161. kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
  162. } while(0)
  163. static void ctx_rcu_free(struct rcu_head *head)
  164. {
  165. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  166. unsigned nr_events = ctx->max_reqs;
  167. kmem_cache_free(kioctx_cachep, ctx);
  168. if (nr_events) {
  169. spin_lock(&aio_nr_lock);
  170. BUG_ON(aio_nr - nr_events > aio_nr);
  171. aio_nr -= nr_events;
  172. spin_unlock(&aio_nr_lock);
  173. }
  174. }
  175. /* __put_ioctx
  176. * Called when the last user of an aio context has gone away,
  177. * and the struct needs to be freed.
  178. */
  179. static void __put_ioctx(struct kioctx *ctx)
  180. {
  181. BUG_ON(ctx->reqs_active);
  182. cancel_delayed_work(&ctx->wq);
  183. cancel_work_sync(&ctx->wq.work);
  184. aio_free_ring(ctx);
  185. mmdrop(ctx->mm);
  186. ctx->mm = NULL;
  187. pr_debug("__put_ioctx: freeing %p\n", ctx);
  188. call_rcu(&ctx->rcu_head, ctx_rcu_free);
  189. }
  190. static inline void get_ioctx(struct kioctx *kioctx)
  191. {
  192. BUG_ON(atomic_read(&kioctx->users) <= 0);
  193. atomic_inc(&kioctx->users);
  194. }
  195. static inline int try_get_ioctx(struct kioctx *kioctx)
  196. {
  197. return atomic_inc_not_zero(&kioctx->users);
  198. }
  199. static inline void put_ioctx(struct kioctx *kioctx)
  200. {
  201. BUG_ON(atomic_read(&kioctx->users) <= 0);
  202. if (unlikely(atomic_dec_and_test(&kioctx->users)))
  203. __put_ioctx(kioctx);
  204. }
  205. /* ioctx_alloc
  206. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  207. */
  208. static struct kioctx *ioctx_alloc(unsigned nr_events)
  209. {
  210. struct mm_struct *mm;
  211. struct kioctx *ctx;
  212. int did_sync = 0;
  213. /* Prevent overflows */
  214. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  215. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  216. pr_debug("ENOMEM: nr_events too high\n");
  217. return ERR_PTR(-EINVAL);
  218. }
  219. if ((unsigned long)nr_events > aio_max_nr)
  220. return ERR_PTR(-EAGAIN);
  221. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  222. if (!ctx)
  223. return ERR_PTR(-ENOMEM);
  224. ctx->max_reqs = nr_events;
  225. mm = ctx->mm = current->mm;
  226. atomic_inc(&mm->mm_count);
  227. atomic_set(&ctx->users, 1);
  228. spin_lock_init(&ctx->ctx_lock);
  229. spin_lock_init(&ctx->ring_info.ring_lock);
  230. init_waitqueue_head(&ctx->wait);
  231. INIT_LIST_HEAD(&ctx->active_reqs);
  232. INIT_LIST_HEAD(&ctx->run_list);
  233. INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
  234. if (aio_setup_ring(ctx) < 0)
  235. goto out_freectx;
  236. /* limit the number of system wide aios */
  237. do {
  238. spin_lock_bh(&aio_nr_lock);
  239. if (aio_nr + nr_events > aio_max_nr ||
  240. aio_nr + nr_events < aio_nr)
  241. ctx->max_reqs = 0;
  242. else
  243. aio_nr += ctx->max_reqs;
  244. spin_unlock_bh(&aio_nr_lock);
  245. if (ctx->max_reqs || did_sync)
  246. break;
  247. /* wait for rcu callbacks to have completed before giving up */
  248. synchronize_rcu();
  249. did_sync = 1;
  250. ctx->max_reqs = nr_events;
  251. } while (1);
  252. if (ctx->max_reqs == 0)
  253. goto out_cleanup;
  254. /* now link into global list. */
  255. spin_lock(&mm->ioctx_lock);
  256. hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
  257. spin_unlock(&mm->ioctx_lock);
  258. dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  259. ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
  260. return ctx;
  261. out_cleanup:
  262. __put_ioctx(ctx);
  263. return ERR_PTR(-EAGAIN);
  264. out_freectx:
  265. mmdrop(mm);
  266. kmem_cache_free(kioctx_cachep, ctx);
  267. ctx = ERR_PTR(-ENOMEM);
  268. dprintk("aio: error allocating ioctx %p\n", ctx);
  269. return ctx;
  270. }
  271. /* aio_cancel_all
  272. * Cancels all outstanding aio requests on an aio context. Used
  273. * when the processes owning a context have all exited to encourage
  274. * the rapid destruction of the kioctx.
  275. */
  276. static void aio_cancel_all(struct kioctx *ctx)
  277. {
  278. int (*cancel)(struct kiocb *, struct io_event *);
  279. struct io_event res;
  280. spin_lock_irq(&ctx->ctx_lock);
  281. ctx->dead = 1;
  282. while (!list_empty(&ctx->active_reqs)) {
  283. struct list_head *pos = ctx->active_reqs.next;
  284. struct kiocb *iocb = list_kiocb(pos);
  285. list_del_init(&iocb->ki_list);
  286. cancel = iocb->ki_cancel;
  287. kiocbSetCancelled(iocb);
  288. if (cancel) {
  289. iocb->ki_users++;
  290. spin_unlock_irq(&ctx->ctx_lock);
  291. cancel(iocb, &res);
  292. spin_lock_irq(&ctx->ctx_lock);
  293. }
  294. }
  295. spin_unlock_irq(&ctx->ctx_lock);
  296. }
  297. static void wait_for_all_aios(struct kioctx *ctx)
  298. {
  299. struct task_struct *tsk = current;
  300. DECLARE_WAITQUEUE(wait, tsk);
  301. spin_lock_irq(&ctx->ctx_lock);
  302. if (!ctx->reqs_active)
  303. goto out;
  304. add_wait_queue(&ctx->wait, &wait);
  305. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  306. while (ctx->reqs_active) {
  307. spin_unlock_irq(&ctx->ctx_lock);
  308. io_schedule();
  309. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  310. spin_lock_irq(&ctx->ctx_lock);
  311. }
  312. __set_task_state(tsk, TASK_RUNNING);
  313. remove_wait_queue(&ctx->wait, &wait);
  314. out:
  315. spin_unlock_irq(&ctx->ctx_lock);
  316. }
  317. /* wait_on_sync_kiocb:
  318. * Waits on the given sync kiocb to complete.
  319. */
  320. ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
  321. {
  322. while (iocb->ki_users) {
  323. set_current_state(TASK_UNINTERRUPTIBLE);
  324. if (!iocb->ki_users)
  325. break;
  326. io_schedule();
  327. }
  328. __set_current_state(TASK_RUNNING);
  329. return iocb->ki_user_data;
  330. }
  331. EXPORT_SYMBOL(wait_on_sync_kiocb);
  332. /* exit_aio: called when the last user of mm goes away. At this point,
  333. * there is no way for any new requests to be submited or any of the
  334. * io_* syscalls to be called on the context. However, there may be
  335. * outstanding requests which hold references to the context; as they
  336. * go away, they will call put_ioctx and release any pinned memory
  337. * associated with the request (held via struct page * references).
  338. */
  339. void exit_aio(struct mm_struct *mm)
  340. {
  341. struct kioctx *ctx;
  342. while (!hlist_empty(&mm->ioctx_list)) {
  343. ctx = hlist_entry(mm->ioctx_list.first, struct kioctx, list);
  344. hlist_del_rcu(&ctx->list);
  345. aio_cancel_all(ctx);
  346. wait_for_all_aios(ctx);
  347. /*
  348. * Ensure we don't leave the ctx on the aio_wq
  349. */
  350. cancel_work_sync(&ctx->wq.work);
  351. if (1 != atomic_read(&ctx->users))
  352. printk(KERN_DEBUG
  353. "exit_aio:ioctx still alive: %d %d %d\n",
  354. atomic_read(&ctx->users), ctx->dead,
  355. ctx->reqs_active);
  356. put_ioctx(ctx);
  357. }
  358. }
  359. /* aio_get_req
  360. * Allocate a slot for an aio request. Increments the users count
  361. * of the kioctx so that the kioctx stays around until all requests are
  362. * complete. Returns NULL if no requests are free.
  363. *
  364. * Returns with kiocb->users set to 2. The io submit code path holds
  365. * an extra reference while submitting the i/o.
  366. * This prevents races between the aio code path referencing the
  367. * req (after submitting it) and aio_complete() freeing the req.
  368. */
  369. static struct kiocb *__aio_get_req(struct kioctx *ctx)
  370. {
  371. struct kiocb *req = NULL;
  372. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
  373. if (unlikely(!req))
  374. return NULL;
  375. req->ki_flags = 0;
  376. req->ki_users = 2;
  377. req->ki_key = 0;
  378. req->ki_ctx = ctx;
  379. req->ki_cancel = NULL;
  380. req->ki_retry = NULL;
  381. req->ki_dtor = NULL;
  382. req->private = NULL;
  383. req->ki_iovec = NULL;
  384. INIT_LIST_HEAD(&req->ki_run_list);
  385. req->ki_eventfd = NULL;
  386. return req;
  387. }
  388. /*
  389. * struct kiocb's are allocated in batches to reduce the number of
  390. * times the ctx lock is acquired and released.
  391. */
  392. #define KIOCB_BATCH_SIZE 32L
  393. struct kiocb_batch {
  394. struct list_head head;
  395. long count; /* number of requests left to allocate */
  396. };
  397. static void kiocb_batch_init(struct kiocb_batch *batch, long total)
  398. {
  399. INIT_LIST_HEAD(&batch->head);
  400. batch->count = total;
  401. }
  402. static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
  403. {
  404. struct kiocb *req, *n;
  405. if (list_empty(&batch->head))
  406. return;
  407. spin_lock_irq(&ctx->ctx_lock);
  408. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  409. list_del(&req->ki_batch);
  410. list_del(&req->ki_list);
  411. kmem_cache_free(kiocb_cachep, req);
  412. ctx->reqs_active--;
  413. }
  414. spin_unlock_irq(&ctx->ctx_lock);
  415. }
  416. /*
  417. * Allocate a batch of kiocbs. This avoids taking and dropping the
  418. * context lock a lot during setup.
  419. */
  420. static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
  421. {
  422. unsigned short allocated, to_alloc;
  423. long avail;
  424. bool called_fput = false;
  425. struct kiocb *req, *n;
  426. struct aio_ring *ring;
  427. to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
  428. for (allocated = 0; allocated < to_alloc; allocated++) {
  429. req = __aio_get_req(ctx);
  430. if (!req)
  431. /* allocation failed, go with what we've got */
  432. break;
  433. list_add(&req->ki_batch, &batch->head);
  434. }
  435. if (allocated == 0)
  436. goto out;
  437. retry:
  438. spin_lock_irq(&ctx->ctx_lock);
  439. ring = kmap_atomic(ctx->ring_info.ring_pages[0]);
  440. avail = aio_ring_avail(&ctx->ring_info, ring) - ctx->reqs_active;
  441. BUG_ON(avail < 0);
  442. if (avail == 0 && !called_fput) {
  443. /*
  444. * Handle a potential starvation case. It is possible that
  445. * we hold the last reference on a struct file, causing us
  446. * to delay the final fput to non-irq context. In this case,
  447. * ctx->reqs_active is artificially high. Calling the fput
  448. * routine here may free up a slot in the event completion
  449. * ring, allowing this allocation to succeed.
  450. */
  451. kunmap_atomic(ring);
  452. spin_unlock_irq(&ctx->ctx_lock);
  453. aio_fput_routine(NULL);
  454. called_fput = true;
  455. goto retry;
  456. }
  457. if (avail < allocated) {
  458. /* Trim back the number of requests. */
  459. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  460. list_del(&req->ki_batch);
  461. kmem_cache_free(kiocb_cachep, req);
  462. if (--allocated <= avail)
  463. break;
  464. }
  465. }
  466. batch->count -= allocated;
  467. list_for_each_entry(req, &batch->head, ki_batch) {
  468. list_add(&req->ki_list, &ctx->active_reqs);
  469. ctx->reqs_active++;
  470. }
  471. kunmap_atomic(ring);
  472. spin_unlock_irq(&ctx->ctx_lock);
  473. out:
  474. return allocated;
  475. }
  476. static inline struct kiocb *aio_get_req(struct kioctx *ctx,
  477. struct kiocb_batch *batch)
  478. {
  479. struct kiocb *req;
  480. if (list_empty(&batch->head))
  481. if (kiocb_batch_refill(ctx, batch) == 0)
  482. return NULL;
  483. req = list_first_entry(&batch->head, struct kiocb, ki_batch);
  484. list_del(&req->ki_batch);
  485. return req;
  486. }
  487. static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
  488. {
  489. assert_spin_locked(&ctx->ctx_lock);
  490. if (req->ki_eventfd != NULL)
  491. eventfd_ctx_put(req->ki_eventfd);
  492. if (req->ki_dtor)
  493. req->ki_dtor(req);
  494. if (req->ki_iovec != &req->ki_inline_vec)
  495. kfree(req->ki_iovec);
  496. kmem_cache_free(kiocb_cachep, req);
  497. ctx->reqs_active--;
  498. if (unlikely(!ctx->reqs_active && ctx->dead))
  499. wake_up_all(&ctx->wait);
  500. }
  501. static void aio_fput_routine(struct work_struct *data)
  502. {
  503. spin_lock_irq(&fput_lock);
  504. while (likely(!list_empty(&fput_head))) {
  505. struct kiocb *req = list_kiocb(fput_head.next);
  506. struct kioctx *ctx = req->ki_ctx;
  507. list_del(&req->ki_list);
  508. spin_unlock_irq(&fput_lock);
  509. /* Complete the fput(s) */
  510. if (req->ki_filp != NULL)
  511. fput(req->ki_filp);
  512. /* Link the iocb into the context's free list */
  513. spin_lock_irq(&ctx->ctx_lock);
  514. really_put_req(ctx, req);
  515. spin_unlock_irq(&ctx->ctx_lock);
  516. put_ioctx(ctx);
  517. spin_lock_irq(&fput_lock);
  518. }
  519. spin_unlock_irq(&fput_lock);
  520. }
  521. /* __aio_put_req
  522. * Returns true if this put was the last user of the request.
  523. */
  524. static int __aio_put_req(struct kioctx *ctx, struct kiocb *req)
  525. {
  526. dprintk(KERN_DEBUG "aio_put(%p): f_count=%ld\n",
  527. req, atomic_long_read(&req->ki_filp->f_count));
  528. assert_spin_locked(&ctx->ctx_lock);
  529. req->ki_users--;
  530. BUG_ON(req->ki_users < 0);
  531. if (likely(req->ki_users))
  532. return 0;
  533. list_del(&req->ki_list); /* remove from active_reqs */
  534. req->ki_cancel = NULL;
  535. req->ki_retry = NULL;
  536. /*
  537. * Try to optimize the aio and eventfd file* puts, by avoiding to
  538. * schedule work in case it is not final fput() time. In normal cases,
  539. * we would not be holding the last reference to the file*, so
  540. * this function will be executed w/out any aio kthread wakeup.
  541. */
  542. if (unlikely(!fput_atomic(req->ki_filp))) {
  543. get_ioctx(ctx);
  544. spin_lock(&fput_lock);
  545. list_add(&req->ki_list, &fput_head);
  546. spin_unlock(&fput_lock);
  547. schedule_work(&fput_work);
  548. } else {
  549. req->ki_filp = NULL;
  550. really_put_req(ctx, req);
  551. }
  552. return 1;
  553. }
  554. /* aio_put_req
  555. * Returns true if this put was the last user of the kiocb,
  556. * false if the request is still in use.
  557. */
  558. int aio_put_req(struct kiocb *req)
  559. {
  560. struct kioctx *ctx = req->ki_ctx;
  561. int ret;
  562. spin_lock_irq(&ctx->ctx_lock);
  563. ret = __aio_put_req(ctx, req);
  564. spin_unlock_irq(&ctx->ctx_lock);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL(aio_put_req);
  568. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  569. {
  570. struct mm_struct *mm = current->mm;
  571. struct kioctx *ctx, *ret = NULL;
  572. struct hlist_node *n;
  573. rcu_read_lock();
  574. hlist_for_each_entry_rcu(ctx, n, &mm->ioctx_list, list) {
  575. /*
  576. * RCU protects us against accessing freed memory but
  577. * we have to be careful not to get a reference when the
  578. * reference count already dropped to 0 (ctx->dead test
  579. * is unreliable because of races).
  580. */
  581. if (ctx->user_id == ctx_id && !ctx->dead && try_get_ioctx(ctx)){
  582. ret = ctx;
  583. break;
  584. }
  585. }
  586. rcu_read_unlock();
  587. return ret;
  588. }
  589. /*
  590. * Queue up a kiocb to be retried. Assumes that the kiocb
  591. * has already been marked as kicked, and places it on
  592. * the retry run list for the corresponding ioctx, if it
  593. * isn't already queued. Returns 1 if it actually queued
  594. * the kiocb (to tell the caller to activate the work
  595. * queue to process it), or 0, if it found that it was
  596. * already queued.
  597. */
  598. static inline int __queue_kicked_iocb(struct kiocb *iocb)
  599. {
  600. struct kioctx *ctx = iocb->ki_ctx;
  601. assert_spin_locked(&ctx->ctx_lock);
  602. if (list_empty(&iocb->ki_run_list)) {
  603. list_add_tail(&iocb->ki_run_list,
  604. &ctx->run_list);
  605. return 1;
  606. }
  607. return 0;
  608. }
  609. /* aio_run_iocb
  610. * This is the core aio execution routine. It is
  611. * invoked both for initial i/o submission and
  612. * subsequent retries via the aio_kick_handler.
  613. * Expects to be invoked with iocb->ki_ctx->lock
  614. * already held. The lock is released and reacquired
  615. * as needed during processing.
  616. *
  617. * Calls the iocb retry method (already setup for the
  618. * iocb on initial submission) for operation specific
  619. * handling, but takes care of most of common retry
  620. * execution details for a given iocb. The retry method
  621. * needs to be non-blocking as far as possible, to avoid
  622. * holding up other iocbs waiting to be serviced by the
  623. * retry kernel thread.
  624. *
  625. * The trickier parts in this code have to do with
  626. * ensuring that only one retry instance is in progress
  627. * for a given iocb at any time. Providing that guarantee
  628. * simplifies the coding of individual aio operations as
  629. * it avoids various potential races.
  630. */
  631. static ssize_t aio_run_iocb(struct kiocb *iocb)
  632. {
  633. struct kioctx *ctx = iocb->ki_ctx;
  634. ssize_t (*retry)(struct kiocb *);
  635. ssize_t ret;
  636. if (!(retry = iocb->ki_retry)) {
  637. printk("aio_run_iocb: iocb->ki_retry = NULL\n");
  638. return 0;
  639. }
  640. /*
  641. * We don't want the next retry iteration for this
  642. * operation to start until this one has returned and
  643. * updated the iocb state. However, wait_queue functions
  644. * can trigger a kick_iocb from interrupt context in the
  645. * meantime, indicating that data is available for the next
  646. * iteration. We want to remember that and enable the
  647. * next retry iteration _after_ we are through with
  648. * this one.
  649. *
  650. * So, in order to be able to register a "kick", but
  651. * prevent it from being queued now, we clear the kick
  652. * flag, but make the kick code *think* that the iocb is
  653. * still on the run list until we are actually done.
  654. * When we are done with this iteration, we check if
  655. * the iocb was kicked in the meantime and if so, queue
  656. * it up afresh.
  657. */
  658. kiocbClearKicked(iocb);
  659. /*
  660. * This is so that aio_complete knows it doesn't need to
  661. * pull the iocb off the run list (We can't just call
  662. * INIT_LIST_HEAD because we don't want a kick_iocb to
  663. * queue this on the run list yet)
  664. */
  665. iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
  666. spin_unlock_irq(&ctx->ctx_lock);
  667. /* Quit retrying if the i/o has been cancelled */
  668. if (kiocbIsCancelled(iocb)) {
  669. ret = -EINTR;
  670. aio_complete(iocb, ret, 0);
  671. /* must not access the iocb after this */
  672. goto out;
  673. }
  674. /*
  675. * Now we are all set to call the retry method in async
  676. * context.
  677. */
  678. ret = retry(iocb);
  679. if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
  680. /*
  681. * There's no easy way to restart the syscall since other AIO's
  682. * may be already running. Just fail this IO with EINTR.
  683. */
  684. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  685. ret == -ERESTARTNOHAND || ret == -ERESTART_RESTARTBLOCK))
  686. ret = -EINTR;
  687. aio_complete(iocb, ret, 0);
  688. }
  689. out:
  690. spin_lock_irq(&ctx->ctx_lock);
  691. if (-EIOCBRETRY == ret) {
  692. /*
  693. * OK, now that we are done with this iteration
  694. * and know that there is more left to go,
  695. * this is where we let go so that a subsequent
  696. * "kick" can start the next iteration
  697. */
  698. /* will make __queue_kicked_iocb succeed from here on */
  699. INIT_LIST_HEAD(&iocb->ki_run_list);
  700. /* we must queue the next iteration ourselves, if it
  701. * has already been kicked */
  702. if (kiocbIsKicked(iocb)) {
  703. __queue_kicked_iocb(iocb);
  704. /*
  705. * __queue_kicked_iocb will always return 1 here, because
  706. * iocb->ki_run_list is empty at this point so it should
  707. * be safe to unconditionally queue the context into the
  708. * work queue.
  709. */
  710. aio_queue_work(ctx);
  711. }
  712. }
  713. return ret;
  714. }
  715. /*
  716. * __aio_run_iocbs:
  717. * Process all pending retries queued on the ioctx
  718. * run list.
  719. * Assumes it is operating within the aio issuer's mm
  720. * context.
  721. */
  722. static int __aio_run_iocbs(struct kioctx *ctx)
  723. {
  724. struct kiocb *iocb;
  725. struct list_head run_list;
  726. assert_spin_locked(&ctx->ctx_lock);
  727. list_replace_init(&ctx->run_list, &run_list);
  728. while (!list_empty(&run_list)) {
  729. iocb = list_entry(run_list.next, struct kiocb,
  730. ki_run_list);
  731. list_del(&iocb->ki_run_list);
  732. /*
  733. * Hold an extra reference while retrying i/o.
  734. */
  735. iocb->ki_users++; /* grab extra reference */
  736. aio_run_iocb(iocb);
  737. __aio_put_req(ctx, iocb);
  738. }
  739. if (!list_empty(&ctx->run_list))
  740. return 1;
  741. return 0;
  742. }
  743. static void aio_queue_work(struct kioctx * ctx)
  744. {
  745. unsigned long timeout;
  746. /*
  747. * if someone is waiting, get the work started right
  748. * away, otherwise, use a longer delay
  749. */
  750. smp_mb();
  751. if (waitqueue_active(&ctx->wait))
  752. timeout = 1;
  753. else
  754. timeout = HZ/10;
  755. queue_delayed_work(aio_wq, &ctx->wq, timeout);
  756. }
  757. /*
  758. * aio_run_all_iocbs:
  759. * Process all pending retries queued on the ioctx
  760. * run list, and keep running them until the list
  761. * stays empty.
  762. * Assumes it is operating within the aio issuer's mm context.
  763. */
  764. static inline void aio_run_all_iocbs(struct kioctx *ctx)
  765. {
  766. spin_lock_irq(&ctx->ctx_lock);
  767. while (__aio_run_iocbs(ctx))
  768. ;
  769. spin_unlock_irq(&ctx->ctx_lock);
  770. }
  771. /*
  772. * aio_kick_handler:
  773. * Work queue handler triggered to process pending
  774. * retries on an ioctx. Takes on the aio issuer's
  775. * mm context before running the iocbs, so that
  776. * copy_xxx_user operates on the issuer's address
  777. * space.
  778. * Run on aiod's context.
  779. */
  780. static void aio_kick_handler(struct work_struct *work)
  781. {
  782. struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
  783. mm_segment_t oldfs = get_fs();
  784. struct mm_struct *mm;
  785. int requeue;
  786. set_fs(USER_DS);
  787. use_mm(ctx->mm);
  788. spin_lock_irq(&ctx->ctx_lock);
  789. requeue =__aio_run_iocbs(ctx);
  790. mm = ctx->mm;
  791. spin_unlock_irq(&ctx->ctx_lock);
  792. unuse_mm(mm);
  793. set_fs(oldfs);
  794. /*
  795. * we're in a worker thread already, don't use queue_delayed_work,
  796. */
  797. if (requeue)
  798. queue_delayed_work(aio_wq, &ctx->wq, 0);
  799. }
  800. /*
  801. * Called by kick_iocb to queue the kiocb for retry
  802. * and if required activate the aio work queue to process
  803. * it
  804. */
  805. static void try_queue_kicked_iocb(struct kiocb *iocb)
  806. {
  807. struct kioctx *ctx = iocb->ki_ctx;
  808. unsigned long flags;
  809. int run = 0;
  810. spin_lock_irqsave(&ctx->ctx_lock, flags);
  811. /* set this inside the lock so that we can't race with aio_run_iocb()
  812. * testing it and putting the iocb on the run list under the lock */
  813. if (!kiocbTryKick(iocb))
  814. run = __queue_kicked_iocb(iocb);
  815. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  816. if (run)
  817. aio_queue_work(ctx);
  818. }
  819. /*
  820. * kick_iocb:
  821. * Called typically from a wait queue callback context
  822. * to trigger a retry of the iocb.
  823. * The retry is usually executed by aio workqueue
  824. * threads (See aio_kick_handler).
  825. */
  826. void kick_iocb(struct kiocb *iocb)
  827. {
  828. /* sync iocbs are easy: they can only ever be executing from a
  829. * single context. */
  830. if (is_sync_kiocb(iocb)) {
  831. kiocbSetKicked(iocb);
  832. wake_up_process(iocb->ki_obj.tsk);
  833. return;
  834. }
  835. try_queue_kicked_iocb(iocb);
  836. }
  837. EXPORT_SYMBOL(kick_iocb);
  838. /* aio_complete
  839. * Called when the io request on the given iocb is complete.
  840. * Returns true if this is the last user of the request. The
  841. * only other user of the request can be the cancellation code.
  842. */
  843. int aio_complete(struct kiocb *iocb, long res, long res2)
  844. {
  845. struct kioctx *ctx = iocb->ki_ctx;
  846. struct aio_ring_info *info;
  847. struct aio_ring *ring;
  848. struct io_event *event;
  849. unsigned long flags;
  850. unsigned long tail;
  851. int ret;
  852. /*
  853. * Special case handling for sync iocbs:
  854. * - events go directly into the iocb for fast handling
  855. * - the sync task with the iocb in its stack holds the single iocb
  856. * ref, no other paths have a way to get another ref
  857. * - the sync task helpfully left a reference to itself in the iocb
  858. */
  859. if (is_sync_kiocb(iocb)) {
  860. BUG_ON(iocb->ki_users != 1);
  861. iocb->ki_user_data = res;
  862. iocb->ki_users = 0;
  863. wake_up_process(iocb->ki_obj.tsk);
  864. return 1;
  865. }
  866. info = &ctx->ring_info;
  867. /* add a completion event to the ring buffer.
  868. * must be done holding ctx->ctx_lock to prevent
  869. * other code from messing with the tail
  870. * pointer since we might be called from irq
  871. * context.
  872. */
  873. spin_lock_irqsave(&ctx->ctx_lock, flags);
  874. if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
  875. list_del_init(&iocb->ki_run_list);
  876. /*
  877. * cancelled requests don't get events, userland was given one
  878. * when the event got cancelled.
  879. */
  880. if (kiocbIsCancelled(iocb))
  881. goto put_rq;
  882. ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
  883. tail = info->tail;
  884. event = aio_ring_event(info, tail, KM_IRQ0);
  885. if (++tail >= info->nr)
  886. tail = 0;
  887. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  888. event->data = iocb->ki_user_data;
  889. event->res = res;
  890. event->res2 = res2;
  891. dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
  892. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  893. res, res2);
  894. /* after flagging the request as done, we
  895. * must never even look at it again
  896. */
  897. smp_wmb(); /* make event visible before updating tail */
  898. info->tail = tail;
  899. ring->tail = tail;
  900. put_aio_ring_event(event, KM_IRQ0);
  901. kunmap_atomic(ring, KM_IRQ1);
  902. pr_debug("added to ring %p at [%lu]\n", iocb, tail);
  903. /*
  904. * Check if the user asked us to deliver the result through an
  905. * eventfd. The eventfd_signal() function is safe to be called
  906. * from IRQ context.
  907. */
  908. if (iocb->ki_eventfd != NULL)
  909. eventfd_signal(iocb->ki_eventfd, 1);
  910. put_rq:
  911. /* everything turned out well, dispose of the aiocb. */
  912. ret = __aio_put_req(ctx, iocb);
  913. /*
  914. * We have to order our ring_info tail store above and test
  915. * of the wait list below outside the wait lock. This is
  916. * like in wake_up_bit() where clearing a bit has to be
  917. * ordered with the unlocked test.
  918. */
  919. smp_mb();
  920. if (waitqueue_active(&ctx->wait))
  921. wake_up(&ctx->wait);
  922. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  923. return ret;
  924. }
  925. EXPORT_SYMBOL(aio_complete);
  926. /* aio_read_evt
  927. * Pull an event off of the ioctx's event ring. Returns the number of
  928. * events fetched (0 or 1 ;-)
  929. * FIXME: make this use cmpxchg.
  930. * TODO: make the ringbuffer user mmap()able (requires FIXME).
  931. */
  932. static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
  933. {
  934. struct aio_ring_info *info = &ioctx->ring_info;
  935. struct aio_ring *ring;
  936. unsigned long head;
  937. int ret = 0;
  938. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  939. dprintk("in aio_read_evt h%lu t%lu m%lu\n",
  940. (unsigned long)ring->head, (unsigned long)ring->tail,
  941. (unsigned long)ring->nr);
  942. if (ring->head == ring->tail)
  943. goto out;
  944. spin_lock(&info->ring_lock);
  945. head = ring->head % info->nr;
  946. if (head != ring->tail) {
  947. struct io_event *evp = aio_ring_event(info, head, KM_USER1);
  948. *ent = *evp;
  949. head = (head + 1) % info->nr;
  950. smp_mb(); /* finish reading the event before updatng the head */
  951. ring->head = head;
  952. ret = 1;
  953. put_aio_ring_event(evp, KM_USER1);
  954. }
  955. spin_unlock(&info->ring_lock);
  956. out:
  957. kunmap_atomic(ring, KM_USER0);
  958. dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
  959. (unsigned long)ring->head, (unsigned long)ring->tail);
  960. return ret;
  961. }
  962. struct aio_timeout {
  963. struct timer_list timer;
  964. int timed_out;
  965. struct task_struct *p;
  966. };
  967. static void timeout_func(unsigned long data)
  968. {
  969. struct aio_timeout *to = (struct aio_timeout *)data;
  970. to->timed_out = 1;
  971. wake_up_process(to->p);
  972. }
  973. static inline void init_timeout(struct aio_timeout *to)
  974. {
  975. setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
  976. to->timed_out = 0;
  977. to->p = current;
  978. }
  979. static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
  980. const struct timespec *ts)
  981. {
  982. to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
  983. if (time_after(to->timer.expires, jiffies))
  984. add_timer(&to->timer);
  985. else
  986. to->timed_out = 1;
  987. }
  988. static inline void clear_timeout(struct aio_timeout *to)
  989. {
  990. del_singleshot_timer_sync(&to->timer);
  991. }
  992. static int read_events(struct kioctx *ctx,
  993. long min_nr, long nr,
  994. struct io_event __user *event,
  995. struct timespec __user *timeout)
  996. {
  997. long start_jiffies = jiffies;
  998. struct task_struct *tsk = current;
  999. DECLARE_WAITQUEUE(wait, tsk);
  1000. int ret;
  1001. int i = 0;
  1002. struct io_event ent;
  1003. struct aio_timeout to;
  1004. int retry = 0;
  1005. /* needed to zero any padding within an entry (there shouldn't be
  1006. * any, but C is fun!
  1007. */
  1008. memset(&ent, 0, sizeof(ent));
  1009. retry:
  1010. ret = 0;
  1011. while (likely(i < nr)) {
  1012. ret = aio_read_evt(ctx, &ent);
  1013. if (unlikely(ret <= 0))
  1014. break;
  1015. dprintk("read event: %Lx %Lx %Lx %Lx\n",
  1016. ent.data, ent.obj, ent.res, ent.res2);
  1017. /* Could we split the check in two? */
  1018. ret = -EFAULT;
  1019. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1020. dprintk("aio: lost an event due to EFAULT.\n");
  1021. break;
  1022. }
  1023. ret = 0;
  1024. /* Good, event copied to userland, update counts. */
  1025. event ++;
  1026. i ++;
  1027. }
  1028. if (min_nr <= i)
  1029. return i;
  1030. if (ret)
  1031. return ret;
  1032. /* End fast path */
  1033. /* racey check, but it gets redone */
  1034. if (!retry && unlikely(!list_empty(&ctx->run_list))) {
  1035. retry = 1;
  1036. aio_run_all_iocbs(ctx);
  1037. goto retry;
  1038. }
  1039. init_timeout(&to);
  1040. if (timeout) {
  1041. struct timespec ts;
  1042. ret = -EFAULT;
  1043. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  1044. goto out;
  1045. set_timeout(start_jiffies, &to, &ts);
  1046. }
  1047. while (likely(i < nr)) {
  1048. add_wait_queue_exclusive(&ctx->wait, &wait);
  1049. do {
  1050. set_task_state(tsk, TASK_INTERRUPTIBLE);
  1051. ret = aio_read_evt(ctx, &ent);
  1052. if (ret)
  1053. break;
  1054. if (min_nr <= i)
  1055. break;
  1056. if (unlikely(ctx->dead)) {
  1057. ret = -EINVAL;
  1058. break;
  1059. }
  1060. if (to.timed_out) /* Only check after read evt */
  1061. break;
  1062. /* Try to only show up in io wait if there are ops
  1063. * in flight */
  1064. if (ctx->reqs_active)
  1065. io_schedule();
  1066. else
  1067. schedule();
  1068. if (signal_pending(tsk)) {
  1069. ret = -EINTR;
  1070. break;
  1071. }
  1072. /*ret = aio_read_evt(ctx, &ent);*/
  1073. } while (1) ;
  1074. set_task_state(tsk, TASK_RUNNING);
  1075. remove_wait_queue(&ctx->wait, &wait);
  1076. if (unlikely(ret <= 0))
  1077. break;
  1078. ret = -EFAULT;
  1079. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1080. dprintk("aio: lost an event due to EFAULT.\n");
  1081. break;
  1082. }
  1083. /* Good, event copied to userland, update counts. */
  1084. event ++;
  1085. i ++;
  1086. }
  1087. if (timeout)
  1088. clear_timeout(&to);
  1089. out:
  1090. destroy_timer_on_stack(&to.timer);
  1091. return i ? i : ret;
  1092. }
  1093. /* Take an ioctx and remove it from the list of ioctx's. Protects
  1094. * against races with itself via ->dead.
  1095. */
  1096. static void io_destroy(struct kioctx *ioctx)
  1097. {
  1098. struct mm_struct *mm = current->mm;
  1099. int was_dead;
  1100. /* delete the entry from the list is someone else hasn't already */
  1101. spin_lock(&mm->ioctx_lock);
  1102. was_dead = ioctx->dead;
  1103. ioctx->dead = 1;
  1104. hlist_del_rcu(&ioctx->list);
  1105. spin_unlock(&mm->ioctx_lock);
  1106. dprintk("aio_release(%p)\n", ioctx);
  1107. if (likely(!was_dead))
  1108. put_ioctx(ioctx); /* twice for the list */
  1109. aio_cancel_all(ioctx);
  1110. wait_for_all_aios(ioctx);
  1111. /*
  1112. * Wake up any waiters. The setting of ctx->dead must be seen
  1113. * by other CPUs at this point. Right now, we rely on the
  1114. * locking done by the above calls to ensure this consistency.
  1115. */
  1116. wake_up_all(&ioctx->wait);
  1117. put_ioctx(ioctx); /* once for the lookup */
  1118. }
  1119. /* sys_io_setup:
  1120. * Create an aio_context capable of receiving at least nr_events.
  1121. * ctxp must not point to an aio_context that already exists, and
  1122. * must be initialized to 0 prior to the call. On successful
  1123. * creation of the aio_context, *ctxp is filled in with the resulting
  1124. * handle. May fail with -EINVAL if *ctxp is not initialized,
  1125. * if the specified nr_events exceeds internal limits. May fail
  1126. * with -EAGAIN if the specified nr_events exceeds the user's limit
  1127. * of available events. May fail with -ENOMEM if insufficient kernel
  1128. * resources are available. May fail with -EFAULT if an invalid
  1129. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  1130. * implemented.
  1131. */
  1132. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  1133. {
  1134. struct kioctx *ioctx = NULL;
  1135. unsigned long ctx;
  1136. long ret;
  1137. ret = get_user(ctx, ctxp);
  1138. if (unlikely(ret))
  1139. goto out;
  1140. ret = -EINVAL;
  1141. if (unlikely(ctx || nr_events == 0)) {
  1142. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  1143. ctx, nr_events);
  1144. goto out;
  1145. }
  1146. ioctx = ioctx_alloc(nr_events);
  1147. ret = PTR_ERR(ioctx);
  1148. if (!IS_ERR(ioctx)) {
  1149. ret = put_user(ioctx->user_id, ctxp);
  1150. if (!ret)
  1151. return 0;
  1152. get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
  1153. io_destroy(ioctx);
  1154. }
  1155. out:
  1156. return ret;
  1157. }
  1158. /* sys_io_destroy:
  1159. * Destroy the aio_context specified. May cancel any outstanding
  1160. * AIOs and block on completion. Will fail with -ENOSYS if not
  1161. * implemented. May fail with -EINVAL if the context pointed to
  1162. * is invalid.
  1163. */
  1164. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  1165. {
  1166. struct kioctx *ioctx = lookup_ioctx(ctx);
  1167. if (likely(NULL != ioctx)) {
  1168. io_destroy(ioctx);
  1169. return 0;
  1170. }
  1171. pr_debug("EINVAL: io_destroy: invalid context id\n");
  1172. return -EINVAL;
  1173. }
  1174. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  1175. {
  1176. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  1177. BUG_ON(ret <= 0);
  1178. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  1179. ssize_t this = min((ssize_t)iov->iov_len, ret);
  1180. iov->iov_base += this;
  1181. iov->iov_len -= this;
  1182. iocb->ki_left -= this;
  1183. ret -= this;
  1184. if (iov->iov_len == 0) {
  1185. iocb->ki_cur_seg++;
  1186. iov++;
  1187. }
  1188. }
  1189. /* the caller should not have done more io than what fit in
  1190. * the remaining iovecs */
  1191. BUG_ON(ret > 0 && iocb->ki_left == 0);
  1192. }
  1193. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  1194. {
  1195. struct file *file = iocb->ki_filp;
  1196. struct address_space *mapping = file->f_mapping;
  1197. struct inode *inode = mapping->host;
  1198. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  1199. unsigned long, loff_t);
  1200. ssize_t ret = 0;
  1201. unsigned short opcode;
  1202. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  1203. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  1204. rw_op = file->f_op->aio_read;
  1205. opcode = IOCB_CMD_PREADV;
  1206. } else {
  1207. rw_op = file->f_op->aio_write;
  1208. opcode = IOCB_CMD_PWRITEV;
  1209. }
  1210. /* This matches the pread()/pwrite() logic */
  1211. if (iocb->ki_pos < 0)
  1212. return -EINVAL;
  1213. do {
  1214. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  1215. iocb->ki_nr_segs - iocb->ki_cur_seg,
  1216. iocb->ki_pos);
  1217. if (ret > 0)
  1218. aio_advance_iovec(iocb, ret);
  1219. /* retry all partial writes. retry partial reads as long as its a
  1220. * regular file. */
  1221. } while (ret > 0 && iocb->ki_left > 0 &&
  1222. (opcode == IOCB_CMD_PWRITEV ||
  1223. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  1224. /* This means we must have transferred all that we could */
  1225. /* No need to retry anymore */
  1226. if ((ret == 0) || (iocb->ki_left == 0))
  1227. ret = iocb->ki_nbytes - iocb->ki_left;
  1228. /* If we managed to write some out we return that, rather than
  1229. * the eventual error. */
  1230. if (opcode == IOCB_CMD_PWRITEV
  1231. && ret < 0 && ret != -EIOCBQUEUED && ret != -EIOCBRETRY
  1232. && iocb->ki_nbytes - iocb->ki_left)
  1233. ret = iocb->ki_nbytes - iocb->ki_left;
  1234. return ret;
  1235. }
  1236. static ssize_t aio_fdsync(struct kiocb *iocb)
  1237. {
  1238. struct file *file = iocb->ki_filp;
  1239. ssize_t ret = -EINVAL;
  1240. if (file->f_op->aio_fsync)
  1241. ret = file->f_op->aio_fsync(iocb, 1);
  1242. return ret;
  1243. }
  1244. static ssize_t aio_fsync(struct kiocb *iocb)
  1245. {
  1246. struct file *file = iocb->ki_filp;
  1247. ssize_t ret = -EINVAL;
  1248. if (file->f_op->aio_fsync)
  1249. ret = file->f_op->aio_fsync(iocb, 0);
  1250. return ret;
  1251. }
  1252. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
  1253. {
  1254. ssize_t ret;
  1255. #ifdef CONFIG_COMPAT
  1256. if (compat)
  1257. ret = compat_rw_copy_check_uvector(type,
  1258. (struct compat_iovec __user *)kiocb->ki_buf,
  1259. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  1260. &kiocb->ki_iovec, 1);
  1261. else
  1262. #endif
  1263. ret = rw_copy_check_uvector(type,
  1264. (struct iovec __user *)kiocb->ki_buf,
  1265. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  1266. &kiocb->ki_iovec, 1);
  1267. if (ret < 0)
  1268. goto out;
  1269. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  1270. kiocb->ki_cur_seg = 0;
  1271. /* ki_nbytes/left now reflect bytes instead of segs */
  1272. kiocb->ki_nbytes = ret;
  1273. kiocb->ki_left = ret;
  1274. ret = 0;
  1275. out:
  1276. return ret;
  1277. }
  1278. static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
  1279. {
  1280. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  1281. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  1282. kiocb->ki_iovec->iov_len = kiocb->ki_left;
  1283. kiocb->ki_nr_segs = 1;
  1284. kiocb->ki_cur_seg = 0;
  1285. return 0;
  1286. }
  1287. /*
  1288. * aio_setup_iocb:
  1289. * Performs the initial checks and aio retry method
  1290. * setup for the kiocb at the time of io submission.
  1291. */
  1292. static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
  1293. {
  1294. struct file *file = kiocb->ki_filp;
  1295. ssize_t ret = 0;
  1296. switch (kiocb->ki_opcode) {
  1297. case IOCB_CMD_PREAD:
  1298. ret = -EBADF;
  1299. if (unlikely(!(file->f_mode & FMODE_READ)))
  1300. break;
  1301. ret = -EFAULT;
  1302. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  1303. kiocb->ki_left)))
  1304. break;
  1305. ret = security_file_permission(file, MAY_READ);
  1306. if (unlikely(ret))
  1307. break;
  1308. ret = aio_setup_single_vector(kiocb);
  1309. if (ret)
  1310. break;
  1311. ret = -EINVAL;
  1312. if (file->f_op->aio_read)
  1313. kiocb->ki_retry = aio_rw_vect_retry;
  1314. break;
  1315. case IOCB_CMD_PWRITE:
  1316. ret = -EBADF;
  1317. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1318. break;
  1319. ret = -EFAULT;
  1320. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  1321. kiocb->ki_left)))
  1322. break;
  1323. ret = security_file_permission(file, MAY_WRITE);
  1324. if (unlikely(ret))
  1325. break;
  1326. ret = aio_setup_single_vector(kiocb);
  1327. if (ret)
  1328. break;
  1329. ret = -EINVAL;
  1330. if (file->f_op->aio_write)
  1331. kiocb->ki_retry = aio_rw_vect_retry;
  1332. break;
  1333. case IOCB_CMD_PREADV:
  1334. ret = -EBADF;
  1335. if (unlikely(!(file->f_mode & FMODE_READ)))
  1336. break;
  1337. ret = security_file_permission(file, MAY_READ);
  1338. if (unlikely(ret))
  1339. break;
  1340. ret = aio_setup_vectored_rw(READ, kiocb, compat);
  1341. if (ret)
  1342. break;
  1343. ret = -EINVAL;
  1344. if (file->f_op->aio_read)
  1345. kiocb->ki_retry = aio_rw_vect_retry;
  1346. break;
  1347. case IOCB_CMD_PWRITEV:
  1348. ret = -EBADF;
  1349. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1350. break;
  1351. ret = security_file_permission(file, MAY_WRITE);
  1352. if (unlikely(ret))
  1353. break;
  1354. ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
  1355. if (ret)
  1356. break;
  1357. ret = -EINVAL;
  1358. if (file->f_op->aio_write)
  1359. kiocb->ki_retry = aio_rw_vect_retry;
  1360. break;
  1361. case IOCB_CMD_FDSYNC:
  1362. ret = -EINVAL;
  1363. if (file->f_op->aio_fsync)
  1364. kiocb->ki_retry = aio_fdsync;
  1365. break;
  1366. case IOCB_CMD_FSYNC:
  1367. ret = -EINVAL;
  1368. if (file->f_op->aio_fsync)
  1369. kiocb->ki_retry = aio_fsync;
  1370. break;
  1371. default:
  1372. dprintk("EINVAL: io_submit: no operation provided\n");
  1373. ret = -EINVAL;
  1374. }
  1375. if (!kiocb->ki_retry)
  1376. return ret;
  1377. return 0;
  1378. }
  1379. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  1380. struct iocb *iocb, struct kiocb_batch *batch,
  1381. bool compat)
  1382. {
  1383. struct kiocb *req;
  1384. struct file *file;
  1385. ssize_t ret;
  1386. /* enforce forwards compatibility on users */
  1387. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  1388. pr_debug("EINVAL: io_submit: reserve field set\n");
  1389. return -EINVAL;
  1390. }
  1391. /* prevent overflows */
  1392. if (unlikely(
  1393. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  1394. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  1395. ((ssize_t)iocb->aio_nbytes < 0)
  1396. )) {
  1397. pr_debug("EINVAL: io_submit: overflow check\n");
  1398. return -EINVAL;
  1399. }
  1400. file = fget(iocb->aio_fildes);
  1401. if (unlikely(!file))
  1402. return -EBADF;
  1403. req = aio_get_req(ctx, batch); /* returns with 2 references to req */
  1404. if (unlikely(!req)) {
  1405. fput(file);
  1406. return -EAGAIN;
  1407. }
  1408. req->ki_filp = file;
  1409. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  1410. /*
  1411. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1412. * instance of the file* now. The file descriptor must be
  1413. * an eventfd() fd, and will be signaled for each completed
  1414. * event using the eventfd_signal() function.
  1415. */
  1416. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  1417. if (IS_ERR(req->ki_eventfd)) {
  1418. ret = PTR_ERR(req->ki_eventfd);
  1419. req->ki_eventfd = NULL;
  1420. goto out_put_req;
  1421. }
  1422. }
  1423. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1424. if (unlikely(ret)) {
  1425. dprintk("EFAULT: aio_key\n");
  1426. goto out_put_req;
  1427. }
  1428. req->ki_obj.user = user_iocb;
  1429. req->ki_user_data = iocb->aio_data;
  1430. req->ki_pos = iocb->aio_offset;
  1431. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1432. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1433. req->ki_opcode = iocb->aio_lio_opcode;
  1434. ret = aio_setup_iocb(req, compat);
  1435. if (ret)
  1436. goto out_put_req;
  1437. spin_lock_irq(&ctx->ctx_lock);
  1438. /*
  1439. * We could have raced with io_destroy() and are currently holding a
  1440. * reference to ctx which should be destroyed. We cannot submit IO
  1441. * since ctx gets freed as soon as io_submit() puts its reference. The
  1442. * check here is reliable: io_destroy() sets ctx->dead before waiting
  1443. * for outstanding IO and the barrier between these two is realized by
  1444. * unlock of mm->ioctx_lock and lock of ctx->ctx_lock. Analogously we
  1445. * increment ctx->reqs_active before checking for ctx->dead and the
  1446. * barrier is realized by unlock and lock of ctx->ctx_lock. Thus if we
  1447. * don't see ctx->dead set here, io_destroy() waits for our IO to
  1448. * finish.
  1449. */
  1450. if (ctx->dead) {
  1451. spin_unlock_irq(&ctx->ctx_lock);
  1452. ret = -EINVAL;
  1453. goto out_put_req;
  1454. }
  1455. aio_run_iocb(req);
  1456. if (!list_empty(&ctx->run_list)) {
  1457. /* drain the run list */
  1458. while (__aio_run_iocbs(ctx))
  1459. ;
  1460. }
  1461. spin_unlock_irq(&ctx->ctx_lock);
  1462. aio_put_req(req); /* drop extra ref to req */
  1463. return 0;
  1464. out_put_req:
  1465. aio_put_req(req); /* drop extra ref to req */
  1466. aio_put_req(req); /* drop i/o ref to req */
  1467. return ret;
  1468. }
  1469. long do_io_submit(aio_context_t ctx_id, long nr,
  1470. struct iocb __user *__user *iocbpp, bool compat)
  1471. {
  1472. struct kioctx *ctx;
  1473. long ret = 0;
  1474. int i = 0;
  1475. struct blk_plug plug;
  1476. struct kiocb_batch batch;
  1477. if (unlikely(nr < 0))
  1478. return -EINVAL;
  1479. if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
  1480. nr = LONG_MAX/sizeof(*iocbpp);
  1481. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1482. return -EFAULT;
  1483. ctx = lookup_ioctx(ctx_id);
  1484. if (unlikely(!ctx)) {
  1485. pr_debug("EINVAL: io_submit: invalid context id\n");
  1486. return -EINVAL;
  1487. }
  1488. kiocb_batch_init(&batch, nr);
  1489. blk_start_plug(&plug);
  1490. /*
  1491. * AKPM: should this return a partial result if some of the IOs were
  1492. * successfully submitted?
  1493. */
  1494. for (i=0; i<nr; i++) {
  1495. struct iocb __user *user_iocb;
  1496. struct iocb tmp;
  1497. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1498. ret = -EFAULT;
  1499. break;
  1500. }
  1501. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1502. ret = -EFAULT;
  1503. break;
  1504. }
  1505. ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
  1506. if (ret)
  1507. break;
  1508. }
  1509. blk_finish_plug(&plug);
  1510. kiocb_batch_free(ctx, &batch);
  1511. put_ioctx(ctx);
  1512. return i ? i : ret;
  1513. }
  1514. /* sys_io_submit:
  1515. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1516. * the number of iocbs queued. May return -EINVAL if the aio_context
  1517. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1518. * *iocbpp[0] is not properly initialized, if the operation specified
  1519. * is invalid for the file descriptor in the iocb. May fail with
  1520. * -EFAULT if any of the data structures point to invalid data. May
  1521. * fail with -EBADF if the file descriptor specified in the first
  1522. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1523. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1524. * fail with -ENOSYS if not implemented.
  1525. */
  1526. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1527. struct iocb __user * __user *, iocbpp)
  1528. {
  1529. return do_io_submit(ctx_id, nr, iocbpp, 0);
  1530. }
  1531. /* lookup_kiocb
  1532. * Finds a given iocb for cancellation.
  1533. */
  1534. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1535. u32 key)
  1536. {
  1537. struct list_head *pos;
  1538. assert_spin_locked(&ctx->ctx_lock);
  1539. /* TODO: use a hash or array, this sucks. */
  1540. list_for_each(pos, &ctx->active_reqs) {
  1541. struct kiocb *kiocb = list_kiocb(pos);
  1542. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1543. return kiocb;
  1544. }
  1545. return NULL;
  1546. }
  1547. /* sys_io_cancel:
  1548. * Attempts to cancel an iocb previously passed to io_submit. If
  1549. * the operation is successfully cancelled, the resulting event is
  1550. * copied into the memory pointed to by result without being placed
  1551. * into the completion queue and 0 is returned. May fail with
  1552. * -EFAULT if any of the data structures pointed to are invalid.
  1553. * May fail with -EINVAL if aio_context specified by ctx_id is
  1554. * invalid. May fail with -EAGAIN if the iocb specified was not
  1555. * cancelled. Will fail with -ENOSYS if not implemented.
  1556. */
  1557. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1558. struct io_event __user *, result)
  1559. {
  1560. int (*cancel)(struct kiocb *iocb, struct io_event *res);
  1561. struct kioctx *ctx;
  1562. struct kiocb *kiocb;
  1563. u32 key;
  1564. int ret;
  1565. ret = get_user(key, &iocb->aio_key);
  1566. if (unlikely(ret))
  1567. return -EFAULT;
  1568. ctx = lookup_ioctx(ctx_id);
  1569. if (unlikely(!ctx))
  1570. return -EINVAL;
  1571. spin_lock_irq(&ctx->ctx_lock);
  1572. ret = -EAGAIN;
  1573. kiocb = lookup_kiocb(ctx, iocb, key);
  1574. if (kiocb && kiocb->ki_cancel) {
  1575. cancel = kiocb->ki_cancel;
  1576. kiocb->ki_users ++;
  1577. kiocbSetCancelled(kiocb);
  1578. } else
  1579. cancel = NULL;
  1580. spin_unlock_irq(&ctx->ctx_lock);
  1581. if (NULL != cancel) {
  1582. struct io_event tmp;
  1583. pr_debug("calling cancel\n");
  1584. memset(&tmp, 0, sizeof(tmp));
  1585. tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
  1586. tmp.data = kiocb->ki_user_data;
  1587. ret = cancel(kiocb, &tmp);
  1588. if (!ret) {
  1589. /* Cancellation succeeded -- copy the result
  1590. * into the user's buffer.
  1591. */
  1592. if (copy_to_user(result, &tmp, sizeof(tmp)))
  1593. ret = -EFAULT;
  1594. }
  1595. } else
  1596. ret = -EINVAL;
  1597. put_ioctx(ctx);
  1598. return ret;
  1599. }
  1600. /* io_getevents:
  1601. * Attempts to read at least min_nr events and up to nr events from
  1602. * the completion queue for the aio_context specified by ctx_id. If
  1603. * it succeeds, the number of read events is returned. May fail with
  1604. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1605. * out of range, if timeout is out of range. May fail with -EFAULT
  1606. * if any of the memory specified is invalid. May return 0 or
  1607. * < min_nr if the timeout specified by timeout has elapsed
  1608. * before sufficient events are available, where timeout == NULL
  1609. * specifies an infinite timeout. Note that the timeout pointed to by
  1610. * timeout is relative and will be updated if not NULL and the
  1611. * operation blocks. Will fail with -ENOSYS if not implemented.
  1612. */
  1613. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1614. long, min_nr,
  1615. long, nr,
  1616. struct io_event __user *, events,
  1617. struct timespec __user *, timeout)
  1618. {
  1619. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1620. long ret = -EINVAL;
  1621. if (likely(ioctx)) {
  1622. if (likely(min_nr <= nr && min_nr >= 0))
  1623. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1624. put_ioctx(ioctx);
  1625. }
  1626. asmlinkage_protect(5, ret, ctx_id, min_nr, nr, events, timeout);
  1627. return ret;
  1628. }