aio.c 45 KB

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