aio.c 48 KB

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