aio.c 45 KB

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