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