aio.c 44 KB

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