aio.c 44 KB

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