aio.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886
  1. /*
  2. * An async IO implementation for Linux
  3. * Written by Benjamin LaHaise <bcrl@kvack.org>
  4. *
  5. * Implements an efficient asynchronous io interface.
  6. *
  7. * Copyright 2000, 2001, 2002 Red Hat, Inc. All Rights Reserved.
  8. *
  9. * See ../COPYING for licensing terms.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/init.h>
  13. #include <linux/errno.h>
  14. #include <linux/time.h>
  15. #include <linux/aio_abi.h>
  16. #include <linux/module.h>
  17. #include <linux/syscalls.h>
  18. #include <linux/backing-dev.h>
  19. #include <linux/uio.h>
  20. #define DEBUG 0
  21. #include <linux/sched.h>
  22. #include <linux/fs.h>
  23. #include <linux/file.h>
  24. #include <linux/mm.h>
  25. #include <linux/mman.h>
  26. #include <linux/mmu_context.h>
  27. #include <linux/slab.h>
  28. #include <linux/timer.h>
  29. #include <linux/aio.h>
  30. #include <linux/highmem.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/security.h>
  33. #include <linux/eventfd.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/compat.h>
  36. #include <asm/kmap_types.h>
  37. #include <asm/uaccess.h>
  38. #if DEBUG > 1
  39. #define dprintk printk
  40. #else
  41. #define dprintk(x...) do { ; } while (0)
  42. #endif
  43. /*------ sysctl variables----*/
  44. static DEFINE_SPINLOCK(aio_nr_lock);
  45. unsigned long aio_nr; /* current system wide number of aio requests */
  46. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  47. /*----end sysctl variables---*/
  48. static struct kmem_cache *kiocb_cachep;
  49. static struct kmem_cache *kioctx_cachep;
  50. static struct workqueue_struct *aio_wq;
  51. /* Used for rare fput completion. */
  52. static void aio_fput_routine(struct work_struct *);
  53. static DECLARE_WORK(fput_work, aio_fput_routine);
  54. static DEFINE_SPINLOCK(fput_lock);
  55. static LIST_HEAD(fput_head);
  56. static void aio_kick_handler(struct work_struct *);
  57. static void aio_queue_work(struct kioctx *);
  58. /* aio_setup
  59. * Creates the slab caches used by the aio routines, panic on
  60. * failure as this is done early during the boot sequence.
  61. */
  62. static int __init aio_setup(void)
  63. {
  64. kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  65. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  66. aio_wq = alloc_workqueue("aio", 0, 1); /* used to limit concurrency */
  67. BUG_ON(!aio_wq);
  68. pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
  69. return 0;
  70. }
  71. __initcall(aio_setup);
  72. static void aio_free_ring(struct kioctx *ctx)
  73. {
  74. struct aio_ring_info *info = &ctx->ring_info;
  75. long i;
  76. for (i=0; i<info->nr_pages; i++)
  77. put_page(info->ring_pages[i]);
  78. if (info->mmap_size) {
  79. down_write(&ctx->mm->mmap_sem);
  80. do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
  81. up_write(&ctx->mm->mmap_sem);
  82. }
  83. if (info->ring_pages && info->ring_pages != info->internal_pages)
  84. kfree(info->ring_pages);
  85. info->ring_pages = NULL;
  86. info->nr = 0;
  87. }
  88. static int aio_setup_ring(struct kioctx *ctx)
  89. {
  90. struct aio_ring *ring;
  91. struct aio_ring_info *info = &ctx->ring_info;
  92. unsigned nr_events = ctx->max_reqs;
  93. unsigned long size;
  94. int nr_pages;
  95. /* Compensate for the ring buffer's head/tail overlap entry */
  96. nr_events += 2; /* 1 is required, 2 for good luck */
  97. size = sizeof(struct aio_ring);
  98. size += sizeof(struct io_event) * nr_events;
  99. nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
  100. if (nr_pages < 0)
  101. return -EINVAL;
  102. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
  103. info->nr = 0;
  104. info->ring_pages = info->internal_pages;
  105. if (nr_pages > AIO_RING_PAGES) {
  106. info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  107. if (!info->ring_pages)
  108. return -ENOMEM;
  109. }
  110. info->mmap_size = nr_pages * PAGE_SIZE;
  111. dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
  112. down_write(&ctx->mm->mmap_sem);
  113. info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
  114. PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
  115. 0);
  116. if (IS_ERR((void *)info->mmap_base)) {
  117. up_write(&ctx->mm->mmap_sem);
  118. info->mmap_size = 0;
  119. aio_free_ring(ctx);
  120. return -EAGAIN;
  121. }
  122. dprintk("mmap address: 0x%08lx\n", info->mmap_base);
  123. info->nr_pages = get_user_pages(current, ctx->mm,
  124. info->mmap_base, nr_pages,
  125. 1, 0, info->ring_pages, NULL);
  126. up_write(&ctx->mm->mmap_sem);
  127. if (unlikely(info->nr_pages != nr_pages)) {
  128. aio_free_ring(ctx);
  129. return -EAGAIN;
  130. }
  131. ctx->user_id = info->mmap_base;
  132. info->nr = nr_events; /* trusted copy */
  133. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  134. ring->nr = nr_events; /* user copy */
  135. ring->id = ctx->user_id;
  136. ring->head = ring->tail = 0;
  137. ring->magic = AIO_RING_MAGIC;
  138. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  139. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  140. ring->header_length = sizeof(struct aio_ring);
  141. kunmap_atomic(ring, KM_USER0);
  142. return 0;
  143. }
  144. /* aio_ring_event: returns a pointer to the event at the given index from
  145. * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
  146. */
  147. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  148. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  149. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  150. #define aio_ring_event(info, nr, km) ({ \
  151. unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
  152. struct io_event *__event; \
  153. __event = kmap_atomic( \
  154. (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
  155. __event += pos % AIO_EVENTS_PER_PAGE; \
  156. __event; \
  157. })
  158. #define put_aio_ring_event(event, km) do { \
  159. struct io_event *__event = (event); \
  160. (void)__event; \
  161. kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
  162. } while(0)
  163. static void ctx_rcu_free(struct rcu_head *head)
  164. {
  165. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  166. unsigned nr_events = ctx->max_reqs;
  167. kmem_cache_free(kioctx_cachep, ctx);
  168. if (nr_events) {
  169. spin_lock(&aio_nr_lock);
  170. BUG_ON(aio_nr - nr_events > aio_nr);
  171. aio_nr -= nr_events;
  172. spin_unlock(&aio_nr_lock);
  173. }
  174. }
  175. /* __put_ioctx
  176. * Called when the last user of an aio context has gone away,
  177. * and the struct needs to be freed.
  178. */
  179. static void __put_ioctx(struct kioctx *ctx)
  180. {
  181. BUG_ON(ctx->reqs_active);
  182. cancel_delayed_work(&ctx->wq);
  183. cancel_work_sync(&ctx->wq.work);
  184. aio_free_ring(ctx);
  185. mmdrop(ctx->mm);
  186. ctx->mm = NULL;
  187. pr_debug("__put_ioctx: freeing %p\n", ctx);
  188. call_rcu(&ctx->rcu_head, ctx_rcu_free);
  189. }
  190. static inline void get_ioctx(struct kioctx *kioctx)
  191. {
  192. BUG_ON(atomic_read(&kioctx->users) <= 0);
  193. atomic_inc(&kioctx->users);
  194. }
  195. static inline int try_get_ioctx(struct kioctx *kioctx)
  196. {
  197. return atomic_inc_not_zero(&kioctx->users);
  198. }
  199. static inline void put_ioctx(struct kioctx *kioctx)
  200. {
  201. BUG_ON(atomic_read(&kioctx->users) <= 0);
  202. if (unlikely(atomic_dec_and_test(&kioctx->users)))
  203. __put_ioctx(kioctx);
  204. }
  205. /* ioctx_alloc
  206. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  207. */
  208. static struct kioctx *ioctx_alloc(unsigned nr_events)
  209. {
  210. struct mm_struct *mm;
  211. struct kioctx *ctx;
  212. int did_sync = 0;
  213. /* Prevent overflows */
  214. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  215. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  216. pr_debug("ENOMEM: nr_events too high\n");
  217. return ERR_PTR(-EINVAL);
  218. }
  219. if ((unsigned long)nr_events > aio_max_nr)
  220. return ERR_PTR(-EAGAIN);
  221. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  222. if (!ctx)
  223. return ERR_PTR(-ENOMEM);
  224. ctx->max_reqs = nr_events;
  225. mm = ctx->mm = current->mm;
  226. atomic_inc(&mm->mm_count);
  227. atomic_set(&ctx->users, 1);
  228. spin_lock_init(&ctx->ctx_lock);
  229. spin_lock_init(&ctx->ring_info.ring_lock);
  230. init_waitqueue_head(&ctx->wait);
  231. INIT_LIST_HEAD(&ctx->active_reqs);
  232. INIT_LIST_HEAD(&ctx->run_list);
  233. INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
  234. if (aio_setup_ring(ctx) < 0)
  235. goto out_freectx;
  236. /* limit the number of system wide aios */
  237. do {
  238. spin_lock_bh(&aio_nr_lock);
  239. if (aio_nr + nr_events > aio_max_nr ||
  240. aio_nr + nr_events < aio_nr)
  241. ctx->max_reqs = 0;
  242. else
  243. aio_nr += ctx->max_reqs;
  244. spin_unlock_bh(&aio_nr_lock);
  245. if (ctx->max_reqs || did_sync)
  246. break;
  247. /* wait for rcu callbacks to have completed before giving up */
  248. synchronize_rcu();
  249. did_sync = 1;
  250. ctx->max_reqs = nr_events;
  251. } while (1);
  252. if (ctx->max_reqs == 0)
  253. goto out_cleanup;
  254. /* now link into global list. */
  255. spin_lock(&mm->ioctx_lock);
  256. hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
  257. spin_unlock(&mm->ioctx_lock);
  258. dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  259. ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
  260. return ctx;
  261. out_cleanup:
  262. __put_ioctx(ctx);
  263. return ERR_PTR(-EAGAIN);
  264. out_freectx:
  265. mmdrop(mm);
  266. kmem_cache_free(kioctx_cachep, ctx);
  267. ctx = ERR_PTR(-ENOMEM);
  268. dprintk("aio: error allocating ioctx %p\n", ctx);
  269. return ctx;
  270. }
  271. /* aio_cancel_all
  272. * Cancels all outstanding aio requests on an aio context. Used
  273. * when the processes owning a context have all exited to encourage
  274. * the rapid destruction of the kioctx.
  275. */
  276. static void aio_cancel_all(struct kioctx *ctx)
  277. {
  278. int (*cancel)(struct kiocb *, struct io_event *);
  279. struct io_event res;
  280. spin_lock_irq(&ctx->ctx_lock);
  281. ctx->dead = 1;
  282. while (!list_empty(&ctx->active_reqs)) {
  283. struct list_head *pos = ctx->active_reqs.next;
  284. struct kiocb *iocb = list_kiocb(pos);
  285. list_del_init(&iocb->ki_list);
  286. cancel = iocb->ki_cancel;
  287. kiocbSetCancelled(iocb);
  288. if (cancel) {
  289. iocb->ki_users++;
  290. spin_unlock_irq(&ctx->ctx_lock);
  291. cancel(iocb, &res);
  292. spin_lock_irq(&ctx->ctx_lock);
  293. }
  294. }
  295. spin_unlock_irq(&ctx->ctx_lock);
  296. }
  297. static void wait_for_all_aios(struct kioctx *ctx)
  298. {
  299. struct task_struct *tsk = current;
  300. DECLARE_WAITQUEUE(wait, tsk);
  301. spin_lock_irq(&ctx->ctx_lock);
  302. if (!ctx->reqs_active)
  303. goto out;
  304. add_wait_queue(&ctx->wait, &wait);
  305. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  306. while (ctx->reqs_active) {
  307. spin_unlock_irq(&ctx->ctx_lock);
  308. io_schedule();
  309. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  310. spin_lock_irq(&ctx->ctx_lock);
  311. }
  312. __set_task_state(tsk, TASK_RUNNING);
  313. remove_wait_queue(&ctx->wait, &wait);
  314. out:
  315. spin_unlock_irq(&ctx->ctx_lock);
  316. }
  317. /* wait_on_sync_kiocb:
  318. * Waits on the given sync kiocb to complete.
  319. */
  320. ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
  321. {
  322. while (iocb->ki_users) {
  323. set_current_state(TASK_UNINTERRUPTIBLE);
  324. if (!iocb->ki_users)
  325. break;
  326. io_schedule();
  327. }
  328. __set_current_state(TASK_RUNNING);
  329. return iocb->ki_user_data;
  330. }
  331. EXPORT_SYMBOL(wait_on_sync_kiocb);
  332. /* exit_aio: called when the last user of mm goes away. At this point,
  333. * there is no way for any new requests to be submited or any of the
  334. * io_* syscalls to be called on the context. However, there may be
  335. * outstanding requests which hold references to the context; as they
  336. * go away, they will call put_ioctx and release any pinned memory
  337. * associated with the request (held via struct page * references).
  338. */
  339. void exit_aio(struct mm_struct *mm)
  340. {
  341. struct kioctx *ctx;
  342. while (!hlist_empty(&mm->ioctx_list)) {
  343. ctx = hlist_entry(mm->ioctx_list.first, struct kioctx, list);
  344. hlist_del_rcu(&ctx->list);
  345. aio_cancel_all(ctx);
  346. wait_for_all_aios(ctx);
  347. /*
  348. * Ensure we don't leave the ctx on the aio_wq
  349. */
  350. cancel_work_sync(&ctx->wq.work);
  351. if (1 != atomic_read(&ctx->users))
  352. printk(KERN_DEBUG
  353. "exit_aio:ioctx still alive: %d %d %d\n",
  354. atomic_read(&ctx->users), ctx->dead,
  355. ctx->reqs_active);
  356. put_ioctx(ctx);
  357. }
  358. }
  359. /* aio_get_req
  360. * Allocate a slot for an aio request. Increments the users count
  361. * of the kioctx so that the kioctx stays around until all requests are
  362. * complete. Returns NULL if no requests are free.
  363. *
  364. * Returns with kiocb->users set to 2. The io submit code path holds
  365. * an extra reference while submitting the i/o.
  366. * This prevents races between the aio code path referencing the
  367. * req (after submitting it) and aio_complete() freeing the req.
  368. */
  369. static struct kiocb *__aio_get_req(struct kioctx *ctx)
  370. {
  371. struct kiocb *req = NULL;
  372. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
  373. if (unlikely(!req))
  374. return NULL;
  375. req->ki_flags = 0;
  376. req->ki_users = 2;
  377. req->ki_key = 0;
  378. req->ki_ctx = ctx;
  379. req->ki_cancel = NULL;
  380. req->ki_retry = NULL;
  381. req->ki_dtor = NULL;
  382. req->private = NULL;
  383. req->ki_iovec = NULL;
  384. INIT_LIST_HEAD(&req->ki_run_list);
  385. req->ki_eventfd = NULL;
  386. return req;
  387. }
  388. /*
  389. * struct kiocb's are allocated in batches to reduce the number of
  390. * times the ctx lock is acquired and released.
  391. */
  392. #define KIOCB_BATCH_SIZE 32L
  393. struct kiocb_batch {
  394. struct list_head head;
  395. long count; /* number of requests left to allocate */
  396. };
  397. static void kiocb_batch_init(struct kiocb_batch *batch, long total)
  398. {
  399. INIT_LIST_HEAD(&batch->head);
  400. batch->count = total;
  401. }
  402. static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
  403. {
  404. struct kiocb *req, *n;
  405. if (list_empty(&batch->head))
  406. return;
  407. spin_lock_irq(&ctx->ctx_lock);
  408. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  409. list_del(&req->ki_batch);
  410. list_del(&req->ki_list);
  411. kmem_cache_free(kiocb_cachep, req);
  412. ctx->reqs_active--;
  413. }
  414. if (unlikely(!ctx->reqs_active && ctx->dead))
  415. wake_up_all(&ctx->wait);
  416. spin_unlock_irq(&ctx->ctx_lock);
  417. }
  418. /*
  419. * Allocate a batch of kiocbs. This avoids taking and dropping the
  420. * context lock a lot during setup.
  421. */
  422. static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
  423. {
  424. unsigned short allocated, to_alloc;
  425. long avail;
  426. bool called_fput = false;
  427. struct kiocb *req, *n;
  428. struct aio_ring *ring;
  429. to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
  430. for (allocated = 0; allocated < to_alloc; allocated++) {
  431. req = __aio_get_req(ctx);
  432. if (!req)
  433. /* allocation failed, go with what we've got */
  434. break;
  435. list_add(&req->ki_batch, &batch->head);
  436. }
  437. if (allocated == 0)
  438. goto out;
  439. retry:
  440. spin_lock_irq(&ctx->ctx_lock);
  441. ring = kmap_atomic(ctx->ring_info.ring_pages[0]);
  442. avail = aio_ring_avail(&ctx->ring_info, ring) - ctx->reqs_active;
  443. BUG_ON(avail < 0);
  444. if (avail == 0 && !called_fput) {
  445. /*
  446. * Handle a potential starvation case. It is possible that
  447. * we hold the last reference on a struct file, causing us
  448. * to delay the final fput to non-irq context. In this case,
  449. * ctx->reqs_active is artificially high. Calling the fput
  450. * routine here may free up a slot in the event completion
  451. * ring, allowing this allocation to succeed.
  452. */
  453. kunmap_atomic(ring);
  454. spin_unlock_irq(&ctx->ctx_lock);
  455. aio_fput_routine(NULL);
  456. called_fput = true;
  457. goto retry;
  458. }
  459. if (avail < allocated) {
  460. /* Trim back the number of requests. */
  461. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  462. list_del(&req->ki_batch);
  463. kmem_cache_free(kiocb_cachep, req);
  464. if (--allocated <= avail)
  465. break;
  466. }
  467. }
  468. batch->count -= allocated;
  469. list_for_each_entry(req, &batch->head, ki_batch) {
  470. list_add(&req->ki_list, &ctx->active_reqs);
  471. ctx->reqs_active++;
  472. }
  473. kunmap_atomic(ring);
  474. spin_unlock_irq(&ctx->ctx_lock);
  475. out:
  476. return allocated;
  477. }
  478. static inline struct kiocb *aio_get_req(struct kioctx *ctx,
  479. struct kiocb_batch *batch)
  480. {
  481. struct kiocb *req;
  482. if (list_empty(&batch->head))
  483. if (kiocb_batch_refill(ctx, batch) == 0)
  484. return NULL;
  485. req = list_first_entry(&batch->head, struct kiocb, ki_batch);
  486. list_del(&req->ki_batch);
  487. return req;
  488. }
  489. static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
  490. {
  491. assert_spin_locked(&ctx->ctx_lock);
  492. if (req->ki_eventfd != NULL)
  493. eventfd_ctx_put(req->ki_eventfd);
  494. if (req->ki_dtor)
  495. req->ki_dtor(req);
  496. if (req->ki_iovec != &req->ki_inline_vec)
  497. kfree(req->ki_iovec);
  498. kmem_cache_free(kiocb_cachep, req);
  499. ctx->reqs_active--;
  500. if (unlikely(!ctx->reqs_active && ctx->dead))
  501. wake_up_all(&ctx->wait);
  502. }
  503. static void aio_fput_routine(struct work_struct *data)
  504. {
  505. spin_lock_irq(&fput_lock);
  506. while (likely(!list_empty(&fput_head))) {
  507. struct kiocb *req = list_kiocb(fput_head.next);
  508. struct kioctx *ctx = req->ki_ctx;
  509. list_del(&req->ki_list);
  510. spin_unlock_irq(&fput_lock);
  511. /* Complete the fput(s) */
  512. if (req->ki_filp != NULL)
  513. fput(req->ki_filp);
  514. /* Link the iocb into the context's free list */
  515. spin_lock_irq(&ctx->ctx_lock);
  516. really_put_req(ctx, req);
  517. spin_unlock_irq(&ctx->ctx_lock);
  518. put_ioctx(ctx);
  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. get_ioctx(ctx);
  546. spin_lock(&fput_lock);
  547. list_add(&req->ki_list, &fput_head);
  548. spin_unlock(&fput_lock);
  549. schedule_work(&fput_work);
  550. } else {
  551. req->ki_filp = NULL;
  552. really_put_req(ctx, req);
  553. }
  554. return 1;
  555. }
  556. /* aio_put_req
  557. * Returns true if this put was the last user of the kiocb,
  558. * false if the request is still in use.
  559. */
  560. int aio_put_req(struct kiocb *req)
  561. {
  562. struct kioctx *ctx = req->ki_ctx;
  563. int ret;
  564. spin_lock_irq(&ctx->ctx_lock);
  565. ret = __aio_put_req(ctx, req);
  566. spin_unlock_irq(&ctx->ctx_lock);
  567. return ret;
  568. }
  569. EXPORT_SYMBOL(aio_put_req);
  570. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  571. {
  572. struct mm_struct *mm = current->mm;
  573. struct kioctx *ctx, *ret = NULL;
  574. struct hlist_node *n;
  575. rcu_read_lock();
  576. hlist_for_each_entry_rcu(ctx, n, &mm->ioctx_list, list) {
  577. /*
  578. * RCU protects us against accessing freed memory but
  579. * we have to be careful not to get a reference when the
  580. * reference count already dropped to 0 (ctx->dead test
  581. * is unreliable because of races).
  582. */
  583. if (ctx->user_id == ctx_id && !ctx->dead && try_get_ioctx(ctx)){
  584. ret = ctx;
  585. break;
  586. }
  587. }
  588. rcu_read_unlock();
  589. return ret;
  590. }
  591. /*
  592. * Queue up a kiocb to be retried. Assumes that the kiocb
  593. * has already been marked as kicked, and places it on
  594. * the retry run list for the corresponding ioctx, if it
  595. * isn't already queued. Returns 1 if it actually queued
  596. * the kiocb (to tell the caller to activate the work
  597. * queue to process it), or 0, if it found that it was
  598. * already queued.
  599. */
  600. static inline int __queue_kicked_iocb(struct kiocb *iocb)
  601. {
  602. struct kioctx *ctx = iocb->ki_ctx;
  603. assert_spin_locked(&ctx->ctx_lock);
  604. if (list_empty(&iocb->ki_run_list)) {
  605. list_add_tail(&iocb->ki_run_list,
  606. &ctx->run_list);
  607. return 1;
  608. }
  609. return 0;
  610. }
  611. /* aio_run_iocb
  612. * This is the core aio execution routine. It is
  613. * invoked both for initial i/o submission and
  614. * subsequent retries via the aio_kick_handler.
  615. * Expects to be invoked with iocb->ki_ctx->lock
  616. * already held. The lock is released and reacquired
  617. * as needed during processing.
  618. *
  619. * Calls the iocb retry method (already setup for the
  620. * iocb on initial submission) for operation specific
  621. * handling, but takes care of most of common retry
  622. * execution details for a given iocb. The retry method
  623. * needs to be non-blocking as far as possible, to avoid
  624. * holding up other iocbs waiting to be serviced by the
  625. * retry kernel thread.
  626. *
  627. * The trickier parts in this code have to do with
  628. * ensuring that only one retry instance is in progress
  629. * for a given iocb at any time. Providing that guarantee
  630. * simplifies the coding of individual aio operations as
  631. * it avoids various potential races.
  632. */
  633. static ssize_t aio_run_iocb(struct kiocb *iocb)
  634. {
  635. struct kioctx *ctx = iocb->ki_ctx;
  636. ssize_t (*retry)(struct kiocb *);
  637. ssize_t ret;
  638. if (!(retry = iocb->ki_retry)) {
  639. printk("aio_run_iocb: iocb->ki_retry = NULL\n");
  640. return 0;
  641. }
  642. /*
  643. * We don't want the next retry iteration for this
  644. * operation to start until this one has returned and
  645. * updated the iocb state. However, wait_queue functions
  646. * can trigger a kick_iocb from interrupt context in the
  647. * meantime, indicating that data is available for the next
  648. * iteration. We want to remember that and enable the
  649. * next retry iteration _after_ we are through with
  650. * this one.
  651. *
  652. * So, in order to be able to register a "kick", but
  653. * prevent it from being queued now, we clear the kick
  654. * flag, but make the kick code *think* that the iocb is
  655. * still on the run list until we are actually done.
  656. * When we are done with this iteration, we check if
  657. * the iocb was kicked in the meantime and if so, queue
  658. * it up afresh.
  659. */
  660. kiocbClearKicked(iocb);
  661. /*
  662. * This is so that aio_complete knows it doesn't need to
  663. * pull the iocb off the run list (We can't just call
  664. * INIT_LIST_HEAD because we don't want a kick_iocb to
  665. * queue this on the run list yet)
  666. */
  667. iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
  668. spin_unlock_irq(&ctx->ctx_lock);
  669. /* Quit retrying if the i/o has been cancelled */
  670. if (kiocbIsCancelled(iocb)) {
  671. ret = -EINTR;
  672. aio_complete(iocb, ret, 0);
  673. /* must not access the iocb after this */
  674. goto out;
  675. }
  676. /*
  677. * Now we are all set to call the retry method in async
  678. * context.
  679. */
  680. ret = retry(iocb);
  681. if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
  682. /*
  683. * There's no easy way to restart the syscall since other AIO's
  684. * may be already running. Just fail this IO with EINTR.
  685. */
  686. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  687. ret == -ERESTARTNOHAND || ret == -ERESTART_RESTARTBLOCK))
  688. ret = -EINTR;
  689. aio_complete(iocb, ret, 0);
  690. }
  691. out:
  692. spin_lock_irq(&ctx->ctx_lock);
  693. if (-EIOCBRETRY == ret) {
  694. /*
  695. * OK, now that we are done with this iteration
  696. * and know that there is more left to go,
  697. * this is where we let go so that a subsequent
  698. * "kick" can start the next iteration
  699. */
  700. /* will make __queue_kicked_iocb succeed from here on */
  701. INIT_LIST_HEAD(&iocb->ki_run_list);
  702. /* we must queue the next iteration ourselves, if it
  703. * has already been kicked */
  704. if (kiocbIsKicked(iocb)) {
  705. __queue_kicked_iocb(iocb);
  706. /*
  707. * __queue_kicked_iocb will always return 1 here, because
  708. * iocb->ki_run_list is empty at this point so it should
  709. * be safe to unconditionally queue the context into the
  710. * work queue.
  711. */
  712. aio_queue_work(ctx);
  713. }
  714. }
  715. return ret;
  716. }
  717. /*
  718. * __aio_run_iocbs:
  719. * Process all pending retries queued on the ioctx
  720. * run list.
  721. * Assumes it is operating within the aio issuer's mm
  722. * context.
  723. */
  724. static int __aio_run_iocbs(struct kioctx *ctx)
  725. {
  726. struct kiocb *iocb;
  727. struct list_head run_list;
  728. assert_spin_locked(&ctx->ctx_lock);
  729. list_replace_init(&ctx->run_list, &run_list);
  730. while (!list_empty(&run_list)) {
  731. iocb = list_entry(run_list.next, struct kiocb,
  732. ki_run_list);
  733. list_del(&iocb->ki_run_list);
  734. /*
  735. * Hold an extra reference while retrying i/o.
  736. */
  737. iocb->ki_users++; /* grab extra reference */
  738. aio_run_iocb(iocb);
  739. __aio_put_req(ctx, iocb);
  740. }
  741. if (!list_empty(&ctx->run_list))
  742. return 1;
  743. return 0;
  744. }
  745. static void aio_queue_work(struct kioctx * ctx)
  746. {
  747. unsigned long timeout;
  748. /*
  749. * if someone is waiting, get the work started right
  750. * away, otherwise, use a longer delay
  751. */
  752. smp_mb();
  753. if (waitqueue_active(&ctx->wait))
  754. timeout = 1;
  755. else
  756. timeout = HZ/10;
  757. queue_delayed_work(aio_wq, &ctx->wq, timeout);
  758. }
  759. /*
  760. * aio_run_all_iocbs:
  761. * Process all pending retries queued on the ioctx
  762. * run list, and keep running them until the list
  763. * stays empty.
  764. * Assumes it is operating within the aio issuer's mm context.
  765. */
  766. static inline void aio_run_all_iocbs(struct kioctx *ctx)
  767. {
  768. spin_lock_irq(&ctx->ctx_lock);
  769. while (__aio_run_iocbs(ctx))
  770. ;
  771. spin_unlock_irq(&ctx->ctx_lock);
  772. }
  773. /*
  774. * aio_kick_handler:
  775. * Work queue handler triggered to process pending
  776. * retries on an ioctx. Takes on the aio issuer's
  777. * mm context before running the iocbs, so that
  778. * copy_xxx_user operates on the issuer's address
  779. * space.
  780. * Run on aiod's context.
  781. */
  782. static void aio_kick_handler(struct work_struct *work)
  783. {
  784. struct kioctx *ctx = container_of(work, struct kioctx, wq.work);
  785. mm_segment_t oldfs = get_fs();
  786. struct mm_struct *mm;
  787. int requeue;
  788. set_fs(USER_DS);
  789. use_mm(ctx->mm);
  790. spin_lock_irq(&ctx->ctx_lock);
  791. requeue =__aio_run_iocbs(ctx);
  792. mm = ctx->mm;
  793. spin_unlock_irq(&ctx->ctx_lock);
  794. unuse_mm(mm);
  795. set_fs(oldfs);
  796. /*
  797. * we're in a worker thread already, don't use queue_delayed_work,
  798. */
  799. if (requeue)
  800. queue_delayed_work(aio_wq, &ctx->wq, 0);
  801. }
  802. /*
  803. * Called by kick_iocb to queue the kiocb for retry
  804. * and if required activate the aio work queue to process
  805. * it
  806. */
  807. static void try_queue_kicked_iocb(struct kiocb *iocb)
  808. {
  809. struct kioctx *ctx = iocb->ki_ctx;
  810. unsigned long flags;
  811. int run = 0;
  812. spin_lock_irqsave(&ctx->ctx_lock, flags);
  813. /* set this inside the lock so that we can't race with aio_run_iocb()
  814. * testing it and putting the iocb on the run list under the lock */
  815. if (!kiocbTryKick(iocb))
  816. run = __queue_kicked_iocb(iocb);
  817. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  818. if (run)
  819. aio_queue_work(ctx);
  820. }
  821. /*
  822. * kick_iocb:
  823. * Called typically from a wait queue callback context
  824. * to trigger a retry of the iocb.
  825. * The retry is usually executed by aio workqueue
  826. * threads (See aio_kick_handler).
  827. */
  828. void kick_iocb(struct kiocb *iocb)
  829. {
  830. /* sync iocbs are easy: they can only ever be executing from a
  831. * single context. */
  832. if (is_sync_kiocb(iocb)) {
  833. kiocbSetKicked(iocb);
  834. wake_up_process(iocb->ki_obj.tsk);
  835. return;
  836. }
  837. try_queue_kicked_iocb(iocb);
  838. }
  839. EXPORT_SYMBOL(kick_iocb);
  840. /* aio_complete
  841. * Called when the io request on the given iocb is complete.
  842. * Returns true if this is the last user of the request. The
  843. * only other user of the request can be the cancellation code.
  844. */
  845. int aio_complete(struct kiocb *iocb, long res, long res2)
  846. {
  847. struct kioctx *ctx = iocb->ki_ctx;
  848. struct aio_ring_info *info;
  849. struct aio_ring *ring;
  850. struct io_event *event;
  851. unsigned long flags;
  852. unsigned long tail;
  853. int ret;
  854. /*
  855. * Special case handling for sync iocbs:
  856. * - events go directly into the iocb for fast handling
  857. * - the sync task with the iocb in its stack holds the single iocb
  858. * ref, no other paths have a way to get another ref
  859. * - the sync task helpfully left a reference to itself in the iocb
  860. */
  861. if (is_sync_kiocb(iocb)) {
  862. BUG_ON(iocb->ki_users != 1);
  863. iocb->ki_user_data = res;
  864. iocb->ki_users = 0;
  865. wake_up_process(iocb->ki_obj.tsk);
  866. return 1;
  867. }
  868. info = &ctx->ring_info;
  869. /* add a completion event to the ring buffer.
  870. * must be done holding ctx->ctx_lock to prevent
  871. * other code from messing with the tail
  872. * pointer since we might be called from irq
  873. * context.
  874. */
  875. spin_lock_irqsave(&ctx->ctx_lock, flags);
  876. if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
  877. list_del_init(&iocb->ki_run_list);
  878. /*
  879. * cancelled requests don't get events, userland was given one
  880. * when the event got cancelled.
  881. */
  882. if (kiocbIsCancelled(iocb))
  883. goto put_rq;
  884. ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
  885. tail = info->tail;
  886. event = aio_ring_event(info, tail, KM_IRQ0);
  887. if (++tail >= info->nr)
  888. tail = 0;
  889. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  890. event->data = iocb->ki_user_data;
  891. event->res = res;
  892. event->res2 = res2;
  893. dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
  894. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  895. res, res2);
  896. /* after flagging the request as done, we
  897. * must never even look at it again
  898. */
  899. smp_wmb(); /* make event visible before updating tail */
  900. info->tail = tail;
  901. ring->tail = tail;
  902. put_aio_ring_event(event, KM_IRQ0);
  903. kunmap_atomic(ring, KM_IRQ1);
  904. pr_debug("added to ring %p at [%lu]\n", iocb, tail);
  905. /*
  906. * Check if the user asked us to deliver the result through an
  907. * eventfd. The eventfd_signal() function is safe to be called
  908. * from IRQ context.
  909. */
  910. if (iocb->ki_eventfd != NULL)
  911. eventfd_signal(iocb->ki_eventfd, 1);
  912. put_rq:
  913. /* everything turned out well, dispose of the aiocb. */
  914. ret = __aio_put_req(ctx, iocb);
  915. /*
  916. * We have to order our ring_info tail store above and test
  917. * of the wait list below outside the wait lock. This is
  918. * like in wake_up_bit() where clearing a bit has to be
  919. * ordered with the unlocked test.
  920. */
  921. smp_mb();
  922. if (waitqueue_active(&ctx->wait))
  923. wake_up(&ctx->wait);
  924. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  925. return ret;
  926. }
  927. EXPORT_SYMBOL(aio_complete);
  928. /* aio_read_evt
  929. * Pull an event off of the ioctx's event ring. Returns the number of
  930. * events fetched (0 or 1 ;-)
  931. * FIXME: make this use cmpxchg.
  932. * TODO: make the ringbuffer user mmap()able (requires FIXME).
  933. */
  934. static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
  935. {
  936. struct aio_ring_info *info = &ioctx->ring_info;
  937. struct aio_ring *ring;
  938. unsigned long head;
  939. int ret = 0;
  940. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  941. dprintk("in aio_read_evt h%lu t%lu m%lu\n",
  942. (unsigned long)ring->head, (unsigned long)ring->tail,
  943. (unsigned long)ring->nr);
  944. if (ring->head == ring->tail)
  945. goto out;
  946. spin_lock(&info->ring_lock);
  947. head = ring->head % info->nr;
  948. if (head != ring->tail) {
  949. struct io_event *evp = aio_ring_event(info, head, KM_USER1);
  950. *ent = *evp;
  951. head = (head + 1) % info->nr;
  952. smp_mb(); /* finish reading the event before updatng the head */
  953. ring->head = head;
  954. ret = 1;
  955. put_aio_ring_event(evp, KM_USER1);
  956. }
  957. spin_unlock(&info->ring_lock);
  958. out:
  959. kunmap_atomic(ring, KM_USER0);
  960. dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
  961. (unsigned long)ring->head, (unsigned long)ring->tail);
  962. return ret;
  963. }
  964. struct aio_timeout {
  965. struct timer_list timer;
  966. int timed_out;
  967. struct task_struct *p;
  968. };
  969. static void timeout_func(unsigned long data)
  970. {
  971. struct aio_timeout *to = (struct aio_timeout *)data;
  972. to->timed_out = 1;
  973. wake_up_process(to->p);
  974. }
  975. static inline void init_timeout(struct aio_timeout *to)
  976. {
  977. setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
  978. to->timed_out = 0;
  979. to->p = current;
  980. }
  981. static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
  982. const struct timespec *ts)
  983. {
  984. to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
  985. if (time_after(to->timer.expires, jiffies))
  986. add_timer(&to->timer);
  987. else
  988. to->timed_out = 1;
  989. }
  990. static inline void clear_timeout(struct aio_timeout *to)
  991. {
  992. del_singleshot_timer_sync(&to->timer);
  993. }
  994. static int read_events(struct kioctx *ctx,
  995. long min_nr, long nr,
  996. struct io_event __user *event,
  997. struct timespec __user *timeout)
  998. {
  999. long start_jiffies = jiffies;
  1000. struct task_struct *tsk = current;
  1001. DECLARE_WAITQUEUE(wait, tsk);
  1002. int ret;
  1003. int i = 0;
  1004. struct io_event ent;
  1005. struct aio_timeout to;
  1006. int retry = 0;
  1007. /* needed to zero any padding within an entry (there shouldn't be
  1008. * any, but C is fun!
  1009. */
  1010. memset(&ent, 0, sizeof(ent));
  1011. retry:
  1012. ret = 0;
  1013. while (likely(i < nr)) {
  1014. ret = aio_read_evt(ctx, &ent);
  1015. if (unlikely(ret <= 0))
  1016. break;
  1017. dprintk("read event: %Lx %Lx %Lx %Lx\n",
  1018. ent.data, ent.obj, ent.res, ent.res2);
  1019. /* Could we split the check in two? */
  1020. ret = -EFAULT;
  1021. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1022. dprintk("aio: lost an event due to EFAULT.\n");
  1023. break;
  1024. }
  1025. ret = 0;
  1026. /* Good, event copied to userland, update counts. */
  1027. event ++;
  1028. i ++;
  1029. }
  1030. if (min_nr <= i)
  1031. return i;
  1032. if (ret)
  1033. return ret;
  1034. /* End fast path */
  1035. /* racey check, but it gets redone */
  1036. if (!retry && unlikely(!list_empty(&ctx->run_list))) {
  1037. retry = 1;
  1038. aio_run_all_iocbs(ctx);
  1039. goto retry;
  1040. }
  1041. init_timeout(&to);
  1042. if (timeout) {
  1043. struct timespec ts;
  1044. ret = -EFAULT;
  1045. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  1046. goto out;
  1047. set_timeout(start_jiffies, &to, &ts);
  1048. }
  1049. while (likely(i < nr)) {
  1050. add_wait_queue_exclusive(&ctx->wait, &wait);
  1051. do {
  1052. set_task_state(tsk, TASK_INTERRUPTIBLE);
  1053. ret = aio_read_evt(ctx, &ent);
  1054. if (ret)
  1055. break;
  1056. if (min_nr <= i)
  1057. break;
  1058. if (unlikely(ctx->dead)) {
  1059. ret = -EINVAL;
  1060. break;
  1061. }
  1062. if (to.timed_out) /* Only check after read evt */
  1063. break;
  1064. /* Try to only show up in io wait if there are ops
  1065. * in flight */
  1066. if (ctx->reqs_active)
  1067. io_schedule();
  1068. else
  1069. schedule();
  1070. if (signal_pending(tsk)) {
  1071. ret = -EINTR;
  1072. break;
  1073. }
  1074. /*ret = aio_read_evt(ctx, &ent);*/
  1075. } while (1) ;
  1076. set_task_state(tsk, TASK_RUNNING);
  1077. remove_wait_queue(&ctx->wait, &wait);
  1078. if (unlikely(ret <= 0))
  1079. break;
  1080. ret = -EFAULT;
  1081. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1082. dprintk("aio: lost an event due to EFAULT.\n");
  1083. break;
  1084. }
  1085. /* Good, event copied to userland, update counts. */
  1086. event ++;
  1087. i ++;
  1088. }
  1089. if (timeout)
  1090. clear_timeout(&to);
  1091. out:
  1092. destroy_timer_on_stack(&to.timer);
  1093. return i ? i : ret;
  1094. }
  1095. /* Take an ioctx and remove it from the list of ioctx's. Protects
  1096. * against races with itself via ->dead.
  1097. */
  1098. static void io_destroy(struct kioctx *ioctx)
  1099. {
  1100. struct mm_struct *mm = current->mm;
  1101. int was_dead;
  1102. /* delete the entry from the list is someone else hasn't already */
  1103. spin_lock(&mm->ioctx_lock);
  1104. was_dead = ioctx->dead;
  1105. ioctx->dead = 1;
  1106. hlist_del_rcu(&ioctx->list);
  1107. spin_unlock(&mm->ioctx_lock);
  1108. dprintk("aio_release(%p)\n", ioctx);
  1109. if (likely(!was_dead))
  1110. put_ioctx(ioctx); /* twice for the list */
  1111. aio_cancel_all(ioctx);
  1112. wait_for_all_aios(ioctx);
  1113. /*
  1114. * Wake up any waiters. The setting of ctx->dead must be seen
  1115. * by other CPUs at this point. Right now, we rely on the
  1116. * locking done by the above calls to ensure this consistency.
  1117. */
  1118. wake_up_all(&ioctx->wait);
  1119. put_ioctx(ioctx); /* once for the lookup */
  1120. }
  1121. /* sys_io_setup:
  1122. * Create an aio_context capable of receiving at least nr_events.
  1123. * ctxp must not point to an aio_context that already exists, and
  1124. * must be initialized to 0 prior to the call. On successful
  1125. * creation of the aio_context, *ctxp is filled in with the resulting
  1126. * handle. May fail with -EINVAL if *ctxp is not initialized,
  1127. * if the specified nr_events exceeds internal limits. May fail
  1128. * with -EAGAIN if the specified nr_events exceeds the user's limit
  1129. * of available events. May fail with -ENOMEM if insufficient kernel
  1130. * resources are available. May fail with -EFAULT if an invalid
  1131. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  1132. * implemented.
  1133. */
  1134. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  1135. {
  1136. struct kioctx *ioctx = NULL;
  1137. unsigned long ctx;
  1138. long ret;
  1139. ret = get_user(ctx, ctxp);
  1140. if (unlikely(ret))
  1141. goto out;
  1142. ret = -EINVAL;
  1143. if (unlikely(ctx || nr_events == 0)) {
  1144. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  1145. ctx, nr_events);
  1146. goto out;
  1147. }
  1148. ioctx = ioctx_alloc(nr_events);
  1149. ret = PTR_ERR(ioctx);
  1150. if (!IS_ERR(ioctx)) {
  1151. ret = put_user(ioctx->user_id, ctxp);
  1152. if (!ret)
  1153. return 0;
  1154. get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
  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. }