aio.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772
  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_ANON|MAP_PRIVATE,
  111. 0);
  112. if (IS_ERR((void *)info->mmap_base)) {
  113. up_write(&ctx->mm->mmap_sem);
  114. printk("mmap err: %ld\n", -info->mmap_base);
  115. info->mmap_size = 0;
  116. aio_free_ring(ctx);
  117. return -EAGAIN;
  118. }
  119. dprintk("mmap address: 0x%08lx\n", info->mmap_base);
  120. info->nr_pages = get_user_pages(current, ctx->mm,
  121. info->mmap_base, nr_pages,
  122. 1, 0, info->ring_pages, NULL);
  123. up_write(&ctx->mm->mmap_sem);
  124. if (unlikely(info->nr_pages != nr_pages)) {
  125. aio_free_ring(ctx);
  126. return -EAGAIN;
  127. }
  128. ctx->user_id = info->mmap_base;
  129. info->nr = nr_events; /* trusted copy */
  130. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  131. ring->nr = nr_events; /* user copy */
  132. ring->id = ctx->user_id;
  133. ring->head = ring->tail = 0;
  134. ring->magic = AIO_RING_MAGIC;
  135. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  136. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  137. ring->header_length = sizeof(struct aio_ring);
  138. kunmap_atomic(ring, KM_USER0);
  139. return 0;
  140. }
  141. /* aio_ring_event: returns a pointer to the event at the given index from
  142. * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
  143. */
  144. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  145. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  146. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  147. #define aio_ring_event(info, nr, km) ({ \
  148. unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
  149. struct io_event *__event; \
  150. __event = kmap_atomic( \
  151. (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
  152. __event += pos % AIO_EVENTS_PER_PAGE; \
  153. __event; \
  154. })
  155. #define put_aio_ring_event(event, km) do { \
  156. struct io_event *__event = (event); \
  157. (void)__event; \
  158. kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
  159. } while(0)
  160. /* ioctx_alloc
  161. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  162. */
  163. static struct kioctx *ioctx_alloc(unsigned nr_events)
  164. {
  165. struct mm_struct *mm;
  166. struct kioctx *ctx;
  167. /* Prevent overflows */
  168. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  169. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  170. pr_debug("ENOMEM: nr_events too high\n");
  171. return ERR_PTR(-EINVAL);
  172. }
  173. if ((unsigned long)nr_events > aio_max_nr)
  174. return ERR_PTR(-EAGAIN);
  175. ctx = kmem_cache_alloc(kioctx_cachep, GFP_KERNEL);
  176. if (!ctx)
  177. return ERR_PTR(-ENOMEM);
  178. memset(ctx, 0, sizeof(*ctx));
  179. ctx->max_reqs = nr_events;
  180. mm = ctx->mm = current->mm;
  181. atomic_inc(&mm->mm_count);
  182. atomic_set(&ctx->users, 1);
  183. spin_lock_init(&ctx->ctx_lock);
  184. spin_lock_init(&ctx->ring_info.ring_lock);
  185. init_waitqueue_head(&ctx->wait);
  186. INIT_LIST_HEAD(&ctx->active_reqs);
  187. INIT_LIST_HEAD(&ctx->run_list);
  188. INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
  189. if (aio_setup_ring(ctx) < 0)
  190. goto out_freectx;
  191. /* limit the number of system wide aios */
  192. spin_lock(&aio_nr_lock);
  193. if (aio_nr + ctx->max_reqs > aio_max_nr ||
  194. aio_nr + ctx->max_reqs < aio_nr)
  195. ctx->max_reqs = 0;
  196. else
  197. aio_nr += ctx->max_reqs;
  198. spin_unlock(&aio_nr_lock);
  199. if (ctx->max_reqs == 0)
  200. goto out_cleanup;
  201. /* now link into global list. kludge. FIXME */
  202. write_lock(&mm->ioctx_list_lock);
  203. ctx->next = mm->ioctx_list;
  204. mm->ioctx_list = ctx;
  205. write_unlock(&mm->ioctx_list_lock);
  206. dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  207. ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
  208. return ctx;
  209. out_cleanup:
  210. __put_ioctx(ctx);
  211. return ERR_PTR(-EAGAIN);
  212. out_freectx:
  213. mmdrop(mm);
  214. kmem_cache_free(kioctx_cachep, ctx);
  215. ctx = ERR_PTR(-ENOMEM);
  216. dprintk("aio: error allocating ioctx %p\n", ctx);
  217. return ctx;
  218. }
  219. /* aio_cancel_all
  220. * Cancels all outstanding aio requests on an aio context. Used
  221. * when the processes owning a context have all exited to encourage
  222. * the rapid destruction of the kioctx.
  223. */
  224. static void aio_cancel_all(struct kioctx *ctx)
  225. {
  226. int (*cancel)(struct kiocb *, struct io_event *);
  227. struct io_event res;
  228. spin_lock_irq(&ctx->ctx_lock);
  229. ctx->dead = 1;
  230. while (!list_empty(&ctx->active_reqs)) {
  231. struct list_head *pos = ctx->active_reqs.next;
  232. struct kiocb *iocb = list_kiocb(pos);
  233. list_del_init(&iocb->ki_list);
  234. cancel = iocb->ki_cancel;
  235. kiocbSetCancelled(iocb);
  236. if (cancel) {
  237. iocb->ki_users++;
  238. spin_unlock_irq(&ctx->ctx_lock);
  239. cancel(iocb, &res);
  240. spin_lock_irq(&ctx->ctx_lock);
  241. }
  242. }
  243. spin_unlock_irq(&ctx->ctx_lock);
  244. }
  245. static void wait_for_all_aios(struct kioctx *ctx)
  246. {
  247. struct task_struct *tsk = current;
  248. DECLARE_WAITQUEUE(wait, tsk);
  249. if (!ctx->reqs_active)
  250. return;
  251. add_wait_queue(&ctx->wait, &wait);
  252. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  253. while (ctx->reqs_active) {
  254. schedule();
  255. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  256. }
  257. __set_task_state(tsk, TASK_RUNNING);
  258. remove_wait_queue(&ctx->wait, &wait);
  259. }
  260. /* wait_on_sync_kiocb:
  261. * Waits on the given sync kiocb to complete.
  262. */
  263. ssize_t fastcall wait_on_sync_kiocb(struct kiocb *iocb)
  264. {
  265. while (iocb->ki_users) {
  266. set_current_state(TASK_UNINTERRUPTIBLE);
  267. if (!iocb->ki_users)
  268. break;
  269. schedule();
  270. }
  271. __set_current_state(TASK_RUNNING);
  272. return iocb->ki_user_data;
  273. }
  274. /* exit_aio: called when the last user of mm goes away. At this point,
  275. * there is no way for any new requests to be submited or any of the
  276. * io_* syscalls to be called on the context. However, there may be
  277. * outstanding requests which hold references to the context; as they
  278. * go away, they will call put_ioctx and release any pinned memory
  279. * associated with the request (held via struct page * references).
  280. */
  281. void fastcall exit_aio(struct mm_struct *mm)
  282. {
  283. struct kioctx *ctx = mm->ioctx_list;
  284. mm->ioctx_list = NULL;
  285. while (ctx) {
  286. struct kioctx *next = ctx->next;
  287. ctx->next = NULL;
  288. aio_cancel_all(ctx);
  289. wait_for_all_aios(ctx);
  290. /*
  291. * this is an overkill, but ensures we don't leave
  292. * the ctx on the aio_wq
  293. */
  294. flush_workqueue(aio_wq);
  295. if (1 != atomic_read(&ctx->users))
  296. printk(KERN_DEBUG
  297. "exit_aio:ioctx still alive: %d %d %d\n",
  298. atomic_read(&ctx->users), ctx->dead,
  299. ctx->reqs_active);
  300. put_ioctx(ctx);
  301. ctx = next;
  302. }
  303. }
  304. /* __put_ioctx
  305. * Called when the last user of an aio context has gone away,
  306. * and the struct needs to be freed.
  307. */
  308. void fastcall __put_ioctx(struct kioctx *ctx)
  309. {
  310. unsigned nr_events = ctx->max_reqs;
  311. BUG_ON(ctx->reqs_active);
  312. cancel_delayed_work(&ctx->wq);
  313. flush_workqueue(aio_wq);
  314. aio_free_ring(ctx);
  315. mmdrop(ctx->mm);
  316. ctx->mm = NULL;
  317. pr_debug("__put_ioctx: freeing %p\n", ctx);
  318. kmem_cache_free(kioctx_cachep, ctx);
  319. if (nr_events) {
  320. spin_lock(&aio_nr_lock);
  321. BUG_ON(aio_nr - nr_events > aio_nr);
  322. aio_nr -= nr_events;
  323. spin_unlock(&aio_nr_lock);
  324. }
  325. }
  326. /* aio_get_req
  327. * Allocate a slot for an aio request. Increments the users count
  328. * of the kioctx so that the kioctx stays around until all requests are
  329. * complete. Returns NULL if no requests are free.
  330. *
  331. * Returns with kiocb->users set to 2. The io submit code path holds
  332. * an extra reference while submitting the i/o.
  333. * This prevents races between the aio code path referencing the
  334. * req (after submitting it) and aio_complete() freeing the req.
  335. */
  336. static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
  337. static struct kiocb fastcall *__aio_get_req(struct kioctx *ctx)
  338. {
  339. struct kiocb *req = NULL;
  340. struct aio_ring *ring;
  341. int okay = 0;
  342. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
  343. if (unlikely(!req))
  344. return NULL;
  345. req->ki_flags = 0;
  346. req->ki_users = 2;
  347. req->ki_key = 0;
  348. req->ki_ctx = ctx;
  349. req->ki_cancel = NULL;
  350. req->ki_retry = NULL;
  351. req->ki_dtor = NULL;
  352. req->private = NULL;
  353. req->ki_iovec = NULL;
  354. INIT_LIST_HEAD(&req->ki_run_list);
  355. /* Check if the completion queue has enough free space to
  356. * accept an event from this io.
  357. */
  358. spin_lock_irq(&ctx->ctx_lock);
  359. ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
  360. if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
  361. list_add(&req->ki_list, &ctx->active_reqs);
  362. get_ioctx(ctx);
  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. if (ret)
  459. put_ioctx(ctx);
  460. return ret;
  461. }
  462. /* Lookup an ioctx id. ioctx_list is lockless for reads.
  463. * FIXME: this is O(n) and is only suitable for development.
  464. */
  465. struct kioctx *lookup_ioctx(unsigned long ctx_id)
  466. {
  467. struct kioctx *ioctx;
  468. struct mm_struct *mm;
  469. mm = current->mm;
  470. read_lock(&mm->ioctx_list_lock);
  471. for (ioctx = mm->ioctx_list; ioctx; ioctx = ioctx->next)
  472. if (likely(ioctx->user_id == ctx_id && !ioctx->dead)) {
  473. get_ioctx(ioctx);
  474. break;
  475. }
  476. read_unlock(&mm->ioctx_list_lock);
  477. return ioctx;
  478. }
  479. /*
  480. * use_mm
  481. * Makes the calling kernel thread take on the specified
  482. * mm context.
  483. * Called by the retry thread execute retries within the
  484. * iocb issuer's mm context, so that copy_from/to_user
  485. * operations work seamlessly for aio.
  486. * (Note: this routine is intended to be called only
  487. * from a kernel thread context)
  488. */
  489. static void use_mm(struct mm_struct *mm)
  490. {
  491. struct mm_struct *active_mm;
  492. struct task_struct *tsk = current;
  493. task_lock(tsk);
  494. tsk->flags |= PF_BORROWED_MM;
  495. active_mm = tsk->active_mm;
  496. atomic_inc(&mm->mm_count);
  497. tsk->mm = mm;
  498. tsk->active_mm = mm;
  499. /*
  500. * Note that on UML this *requires* PF_BORROWED_MM to be set, otherwise
  501. * it won't work. Update it accordingly if you change it here
  502. */
  503. switch_mm(active_mm, mm, tsk);
  504. task_unlock(tsk);
  505. mmdrop(active_mm);
  506. }
  507. /*
  508. * unuse_mm
  509. * Reverses the effect of use_mm, i.e. releases the
  510. * specified mm context which was earlier taken on
  511. * by the calling kernel thread
  512. * (Note: this routine is intended to be called only
  513. * from a kernel thread context)
  514. */
  515. static void unuse_mm(struct mm_struct *mm)
  516. {
  517. struct task_struct *tsk = current;
  518. task_lock(tsk);
  519. tsk->flags &= ~PF_BORROWED_MM;
  520. tsk->mm = NULL;
  521. /* active_mm is still 'mm' */
  522. enter_lazy_tlb(mm, tsk);
  523. task_unlock(tsk);
  524. }
  525. /*
  526. * Queue up a kiocb to be retried. Assumes that the kiocb
  527. * has already been marked as kicked, and places it on
  528. * the retry run list for the corresponding ioctx, if it
  529. * isn't already queued. Returns 1 if it actually queued
  530. * the kiocb (to tell the caller to activate the work
  531. * queue to process it), or 0, if it found that it was
  532. * already queued.
  533. */
  534. static inline int __queue_kicked_iocb(struct kiocb *iocb)
  535. {
  536. struct kioctx *ctx = iocb->ki_ctx;
  537. assert_spin_locked(&ctx->ctx_lock);
  538. if (list_empty(&iocb->ki_run_list)) {
  539. list_add_tail(&iocb->ki_run_list,
  540. &ctx->run_list);
  541. return 1;
  542. }
  543. return 0;
  544. }
  545. /* aio_run_iocb
  546. * This is the core aio execution routine. It is
  547. * invoked both for initial i/o submission and
  548. * subsequent retries via the aio_kick_handler.
  549. * Expects to be invoked with iocb->ki_ctx->lock
  550. * already held. The lock is released and reacquired
  551. * as needed during processing.
  552. *
  553. * Calls the iocb retry method (already setup for the
  554. * iocb on initial submission) for operation specific
  555. * handling, but takes care of most of common retry
  556. * execution details for a given iocb. The retry method
  557. * needs to be non-blocking as far as possible, to avoid
  558. * holding up other iocbs waiting to be serviced by the
  559. * retry kernel thread.
  560. *
  561. * The trickier parts in this code have to do with
  562. * ensuring that only one retry instance is in progress
  563. * for a given iocb at any time. Providing that guarantee
  564. * simplifies the coding of individual aio operations as
  565. * it avoids various potential races.
  566. */
  567. static ssize_t aio_run_iocb(struct kiocb *iocb)
  568. {
  569. struct kioctx *ctx = iocb->ki_ctx;
  570. ssize_t (*retry)(struct kiocb *);
  571. ssize_t ret;
  572. if (!(retry = iocb->ki_retry)) {
  573. printk("aio_run_iocb: iocb->ki_retry = NULL\n");
  574. return 0;
  575. }
  576. /*
  577. * We don't want the next retry iteration for this
  578. * operation to start until this one has returned and
  579. * updated the iocb state. However, wait_queue functions
  580. * can trigger a kick_iocb from interrupt context in the
  581. * meantime, indicating that data is available for the next
  582. * iteration. We want to remember that and enable the
  583. * next retry iteration _after_ we are through with
  584. * this one.
  585. *
  586. * So, in order to be able to register a "kick", but
  587. * prevent it from being queued now, we clear the kick
  588. * flag, but make the kick code *think* that the iocb is
  589. * still on the run list until we are actually done.
  590. * When we are done with this iteration, we check if
  591. * the iocb was kicked in the meantime and if so, queue
  592. * it up afresh.
  593. */
  594. kiocbClearKicked(iocb);
  595. /*
  596. * This is so that aio_complete knows it doesn't need to
  597. * pull the iocb off the run list (We can't just call
  598. * INIT_LIST_HEAD because we don't want a kick_iocb to
  599. * queue this on the run list yet)
  600. */
  601. iocb->ki_run_list.next = iocb->ki_run_list.prev = NULL;
  602. spin_unlock_irq(&ctx->ctx_lock);
  603. /* Quit retrying if the i/o has been cancelled */
  604. if (kiocbIsCancelled(iocb)) {
  605. ret = -EINTR;
  606. aio_complete(iocb, ret, 0);
  607. /* must not access the iocb after this */
  608. goto out;
  609. }
  610. /*
  611. * Now we are all set to call the retry method in async
  612. * context. By setting this thread's io_wait context
  613. * to point to the wait queue entry inside the currently
  614. * running iocb for the duration of the retry, we ensure
  615. * that async notification wakeups are queued by the
  616. * operation instead of blocking waits, and when notified,
  617. * cause the iocb to be kicked for continuation (through
  618. * the aio_wake_function callback).
  619. */
  620. BUG_ON(current->io_wait != NULL);
  621. current->io_wait = &iocb->ki_wait;
  622. ret = retry(iocb);
  623. current->io_wait = NULL;
  624. if (ret != -EIOCBRETRY && ret != -EIOCBQUEUED) {
  625. BUG_ON(!list_empty(&iocb->ki_wait.task_list));
  626. aio_complete(iocb, ret, 0);
  627. }
  628. out:
  629. spin_lock_irq(&ctx->ctx_lock);
  630. if (-EIOCBRETRY == ret) {
  631. /*
  632. * OK, now that we are done with this iteration
  633. * and know that there is more left to go,
  634. * this is where we let go so that a subsequent
  635. * "kick" can start the next iteration
  636. */
  637. /* will make __queue_kicked_iocb succeed from here on */
  638. INIT_LIST_HEAD(&iocb->ki_run_list);
  639. /* we must queue the next iteration ourselves, if it
  640. * has already been kicked */
  641. if (kiocbIsKicked(iocb)) {
  642. __queue_kicked_iocb(iocb);
  643. /*
  644. * __queue_kicked_iocb will always return 1 here, because
  645. * iocb->ki_run_list is empty at this point so it should
  646. * be safe to unconditionally queue the context into the
  647. * work queue.
  648. */
  649. aio_queue_work(ctx);
  650. }
  651. }
  652. return ret;
  653. }
  654. /*
  655. * __aio_run_iocbs:
  656. * Process all pending retries queued on the ioctx
  657. * run list.
  658. * Assumes it is operating within the aio issuer's mm
  659. * context.
  660. */
  661. static int __aio_run_iocbs(struct kioctx *ctx)
  662. {
  663. struct kiocb *iocb;
  664. struct list_head run_list;
  665. assert_spin_locked(&ctx->ctx_lock);
  666. list_replace_init(&ctx->run_list, &run_list);
  667. while (!list_empty(&run_list)) {
  668. iocb = list_entry(run_list.next, struct kiocb,
  669. ki_run_list);
  670. list_del(&iocb->ki_run_list);
  671. /*
  672. * Hold an extra reference while retrying i/o.
  673. */
  674. iocb->ki_users++; /* grab extra reference */
  675. aio_run_iocb(iocb);
  676. if (__aio_put_req(ctx, iocb)) /* drop extra ref */
  677. put_ioctx(ctx);
  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. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  866. if (waitqueue_active(&ctx->wait))
  867. wake_up(&ctx->wait);
  868. if (ret)
  869. put_ioctx(ctx);
  870. return ret;
  871. }
  872. /* aio_read_evt
  873. * Pull an event off of the ioctx's event ring. Returns the number of
  874. * events fetched (0 or 1 ;-)
  875. * FIXME: make this use cmpxchg.
  876. * TODO: make the ringbuffer user mmap()able (requires FIXME).
  877. */
  878. static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
  879. {
  880. struct aio_ring_info *info = &ioctx->ring_info;
  881. struct aio_ring *ring;
  882. unsigned long head;
  883. int ret = 0;
  884. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  885. dprintk("in aio_read_evt h%lu t%lu m%lu\n",
  886. (unsigned long)ring->head, (unsigned long)ring->tail,
  887. (unsigned long)ring->nr);
  888. if (ring->head == ring->tail)
  889. goto out;
  890. spin_lock(&info->ring_lock);
  891. head = ring->head % info->nr;
  892. if (head != ring->tail) {
  893. struct io_event *evp = aio_ring_event(info, head, KM_USER1);
  894. *ent = *evp;
  895. head = (head + 1) % info->nr;
  896. smp_mb(); /* finish reading the event before updatng the head */
  897. ring->head = head;
  898. ret = 1;
  899. put_aio_ring_event(evp, KM_USER1);
  900. }
  901. spin_unlock(&info->ring_lock);
  902. out:
  903. kunmap_atomic(ring, KM_USER0);
  904. dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
  905. (unsigned long)ring->head, (unsigned long)ring->tail);
  906. return ret;
  907. }
  908. struct aio_timeout {
  909. struct timer_list timer;
  910. int timed_out;
  911. struct task_struct *p;
  912. };
  913. static void timeout_func(unsigned long data)
  914. {
  915. struct aio_timeout *to = (struct aio_timeout *)data;
  916. to->timed_out = 1;
  917. wake_up_process(to->p);
  918. }
  919. static inline void init_timeout(struct aio_timeout *to)
  920. {
  921. init_timer(&to->timer);
  922. to->timer.data = (unsigned long)to;
  923. to->timer.function = timeout_func;
  924. to->timed_out = 0;
  925. to->p = current;
  926. }
  927. static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
  928. const struct timespec *ts)
  929. {
  930. to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
  931. if (time_after(to->timer.expires, jiffies))
  932. add_timer(&to->timer);
  933. else
  934. to->timed_out = 1;
  935. }
  936. static inline void clear_timeout(struct aio_timeout *to)
  937. {
  938. del_singleshot_timer_sync(&to->timer);
  939. }
  940. static int read_events(struct kioctx *ctx,
  941. long min_nr, long nr,
  942. struct io_event __user *event,
  943. struct timespec __user *timeout)
  944. {
  945. long start_jiffies = jiffies;
  946. struct task_struct *tsk = current;
  947. DECLARE_WAITQUEUE(wait, tsk);
  948. int ret;
  949. int i = 0;
  950. struct io_event ent;
  951. struct aio_timeout to;
  952. int retry = 0;
  953. /* needed to zero any padding within an entry (there shouldn't be
  954. * any, but C is fun!
  955. */
  956. memset(&ent, 0, sizeof(ent));
  957. retry:
  958. ret = 0;
  959. while (likely(i < nr)) {
  960. ret = aio_read_evt(ctx, &ent);
  961. if (unlikely(ret <= 0))
  962. break;
  963. dprintk("read event: %Lx %Lx %Lx %Lx\n",
  964. ent.data, ent.obj, ent.res, ent.res2);
  965. /* Could we split the check in two? */
  966. ret = -EFAULT;
  967. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  968. dprintk("aio: lost an event due to EFAULT.\n");
  969. break;
  970. }
  971. ret = 0;
  972. /* Good, event copied to userland, update counts. */
  973. event ++;
  974. i ++;
  975. }
  976. if (min_nr <= i)
  977. return i;
  978. if (ret)
  979. return ret;
  980. /* End fast path */
  981. /* racey check, but it gets redone */
  982. if (!retry && unlikely(!list_empty(&ctx->run_list))) {
  983. retry = 1;
  984. aio_run_all_iocbs(ctx);
  985. goto retry;
  986. }
  987. init_timeout(&to);
  988. if (timeout) {
  989. struct timespec ts;
  990. ret = -EFAULT;
  991. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  992. goto out;
  993. set_timeout(start_jiffies, &to, &ts);
  994. }
  995. while (likely(i < nr)) {
  996. add_wait_queue_exclusive(&ctx->wait, &wait);
  997. do {
  998. set_task_state(tsk, TASK_INTERRUPTIBLE);
  999. ret = aio_read_evt(ctx, &ent);
  1000. if (ret)
  1001. break;
  1002. if (min_nr <= i)
  1003. break;
  1004. ret = 0;
  1005. if (to.timed_out) /* Only check after read evt */
  1006. break;
  1007. schedule();
  1008. if (signal_pending(tsk)) {
  1009. ret = -EINTR;
  1010. break;
  1011. }
  1012. /*ret = aio_read_evt(ctx, &ent);*/
  1013. } while (1) ;
  1014. set_task_state(tsk, TASK_RUNNING);
  1015. remove_wait_queue(&ctx->wait, &wait);
  1016. if (unlikely(ret <= 0))
  1017. break;
  1018. ret = -EFAULT;
  1019. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1020. dprintk("aio: lost an event due to EFAULT.\n");
  1021. break;
  1022. }
  1023. /* Good, event copied to userland, update counts. */
  1024. event ++;
  1025. i ++;
  1026. }
  1027. if (timeout)
  1028. clear_timeout(&to);
  1029. out:
  1030. return i ? i : ret;
  1031. }
  1032. /* Take an ioctx and remove it from the list of ioctx's. Protects
  1033. * against races with itself via ->dead.
  1034. */
  1035. static void io_destroy(struct kioctx *ioctx)
  1036. {
  1037. struct mm_struct *mm = current->mm;
  1038. struct kioctx **tmp;
  1039. int was_dead;
  1040. /* delete the entry from the list is someone else hasn't already */
  1041. write_lock(&mm->ioctx_list_lock);
  1042. was_dead = ioctx->dead;
  1043. ioctx->dead = 1;
  1044. for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
  1045. tmp = &(*tmp)->next)
  1046. ;
  1047. if (*tmp)
  1048. *tmp = ioctx->next;
  1049. write_unlock(&mm->ioctx_list_lock);
  1050. dprintk("aio_release(%p)\n", ioctx);
  1051. if (likely(!was_dead))
  1052. put_ioctx(ioctx); /* twice for the list */
  1053. aio_cancel_all(ioctx);
  1054. wait_for_all_aios(ioctx);
  1055. put_ioctx(ioctx); /* once for the lookup */
  1056. }
  1057. /* sys_io_setup:
  1058. * Create an aio_context capable of receiving at least nr_events.
  1059. * ctxp must not point to an aio_context that already exists, and
  1060. * must be initialized to 0 prior to the call. On successful
  1061. * creation of the aio_context, *ctxp is filled in with the resulting
  1062. * handle. May fail with -EINVAL if *ctxp is not initialized,
  1063. * if the specified nr_events exceeds internal limits. May fail
  1064. * with -EAGAIN if the specified nr_events exceeds the user's limit
  1065. * of available events. May fail with -ENOMEM if insufficient kernel
  1066. * resources are available. May fail with -EFAULT if an invalid
  1067. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  1068. * implemented.
  1069. */
  1070. asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
  1071. {
  1072. struct kioctx *ioctx = NULL;
  1073. unsigned long ctx;
  1074. long ret;
  1075. ret = get_user(ctx, ctxp);
  1076. if (unlikely(ret))
  1077. goto out;
  1078. ret = -EINVAL;
  1079. if (unlikely(ctx || nr_events == 0)) {
  1080. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  1081. ctx, nr_events);
  1082. goto out;
  1083. }
  1084. ioctx = ioctx_alloc(nr_events);
  1085. ret = PTR_ERR(ioctx);
  1086. if (!IS_ERR(ioctx)) {
  1087. ret = put_user(ioctx->user_id, ctxp);
  1088. if (!ret)
  1089. return 0;
  1090. get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
  1091. io_destroy(ioctx);
  1092. }
  1093. out:
  1094. return ret;
  1095. }
  1096. /* sys_io_destroy:
  1097. * Destroy the aio_context specified. May cancel any outstanding
  1098. * AIOs and block on completion. Will fail with -ENOSYS if not
  1099. * implemented. May fail with -EFAULT if the context pointed to
  1100. * is invalid.
  1101. */
  1102. asmlinkage long sys_io_destroy(aio_context_t ctx)
  1103. {
  1104. struct kioctx *ioctx = lookup_ioctx(ctx);
  1105. if (likely(NULL != ioctx)) {
  1106. io_destroy(ioctx);
  1107. return 0;
  1108. }
  1109. pr_debug("EINVAL: io_destroy: invalid context id\n");
  1110. return -EINVAL;
  1111. }
  1112. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  1113. {
  1114. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  1115. BUG_ON(ret <= 0);
  1116. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  1117. ssize_t this = min((ssize_t)iov->iov_len, ret);
  1118. iov->iov_base += this;
  1119. iov->iov_len -= this;
  1120. iocb->ki_left -= this;
  1121. ret -= this;
  1122. if (iov->iov_len == 0) {
  1123. iocb->ki_cur_seg++;
  1124. iov++;
  1125. }
  1126. }
  1127. /* the caller should not have done more io than what fit in
  1128. * the remaining iovecs */
  1129. BUG_ON(ret > 0 && iocb->ki_left == 0);
  1130. }
  1131. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  1132. {
  1133. struct file *file = iocb->ki_filp;
  1134. struct address_space *mapping = file->f_mapping;
  1135. struct inode *inode = mapping->host;
  1136. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  1137. unsigned long, loff_t);
  1138. ssize_t ret = 0;
  1139. unsigned short opcode;
  1140. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  1141. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  1142. rw_op = file->f_op->aio_read;
  1143. opcode = IOCB_CMD_PREADV;
  1144. } else {
  1145. rw_op = file->f_op->aio_write;
  1146. opcode = IOCB_CMD_PWRITEV;
  1147. }
  1148. do {
  1149. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  1150. iocb->ki_nr_segs - iocb->ki_cur_seg,
  1151. iocb->ki_pos);
  1152. if (ret > 0)
  1153. aio_advance_iovec(iocb, ret);
  1154. /* retry all partial writes. retry partial reads as long as its a
  1155. * regular file. */
  1156. } while (ret > 0 && iocb->ki_left > 0 &&
  1157. (opcode == IOCB_CMD_PWRITEV ||
  1158. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  1159. /* This means we must have transferred all that we could */
  1160. /* No need to retry anymore */
  1161. if ((ret == 0) || (iocb->ki_left == 0))
  1162. ret = iocb->ki_nbytes - iocb->ki_left;
  1163. return ret;
  1164. }
  1165. static ssize_t aio_fdsync(struct kiocb *iocb)
  1166. {
  1167. struct file *file = iocb->ki_filp;
  1168. ssize_t ret = -EINVAL;
  1169. if (file->f_op->aio_fsync)
  1170. ret = file->f_op->aio_fsync(iocb, 1);
  1171. return ret;
  1172. }
  1173. static ssize_t aio_fsync(struct kiocb *iocb)
  1174. {
  1175. struct file *file = iocb->ki_filp;
  1176. ssize_t ret = -EINVAL;
  1177. if (file->f_op->aio_fsync)
  1178. ret = file->f_op->aio_fsync(iocb, 0);
  1179. return ret;
  1180. }
  1181. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
  1182. {
  1183. ssize_t ret;
  1184. ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
  1185. kiocb->ki_nbytes, 1,
  1186. &kiocb->ki_inline_vec, &kiocb->ki_iovec);
  1187. if (ret < 0)
  1188. goto out;
  1189. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  1190. kiocb->ki_cur_seg = 0;
  1191. /* ki_nbytes/left now reflect bytes instead of segs */
  1192. kiocb->ki_nbytes = ret;
  1193. kiocb->ki_left = ret;
  1194. ret = 0;
  1195. out:
  1196. return ret;
  1197. }
  1198. static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
  1199. {
  1200. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  1201. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  1202. kiocb->ki_iovec->iov_len = kiocb->ki_left;
  1203. kiocb->ki_nr_segs = 1;
  1204. kiocb->ki_cur_seg = 0;
  1205. return 0;
  1206. }
  1207. /*
  1208. * aio_setup_iocb:
  1209. * Performs the initial checks and aio retry method
  1210. * setup for the kiocb at the time of io submission.
  1211. */
  1212. static ssize_t aio_setup_iocb(struct kiocb *kiocb)
  1213. {
  1214. struct file *file = kiocb->ki_filp;
  1215. ssize_t ret = 0;
  1216. switch (kiocb->ki_opcode) {
  1217. case IOCB_CMD_PREAD:
  1218. ret = -EBADF;
  1219. if (unlikely(!(file->f_mode & FMODE_READ)))
  1220. break;
  1221. ret = -EFAULT;
  1222. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  1223. kiocb->ki_left)))
  1224. break;
  1225. ret = security_file_permission(file, MAY_READ);
  1226. if (unlikely(ret))
  1227. break;
  1228. ret = aio_setup_single_vector(kiocb);
  1229. if (ret)
  1230. break;
  1231. ret = -EINVAL;
  1232. if (file->f_op->aio_read)
  1233. kiocb->ki_retry = aio_rw_vect_retry;
  1234. break;
  1235. case IOCB_CMD_PWRITE:
  1236. ret = -EBADF;
  1237. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1238. break;
  1239. ret = -EFAULT;
  1240. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  1241. kiocb->ki_left)))
  1242. break;
  1243. ret = security_file_permission(file, MAY_WRITE);
  1244. if (unlikely(ret))
  1245. break;
  1246. ret = aio_setup_single_vector(kiocb);
  1247. if (ret)
  1248. break;
  1249. ret = -EINVAL;
  1250. if (file->f_op->aio_write)
  1251. kiocb->ki_retry = aio_rw_vect_retry;
  1252. break;
  1253. case IOCB_CMD_PREADV:
  1254. ret = -EBADF;
  1255. if (unlikely(!(file->f_mode & FMODE_READ)))
  1256. break;
  1257. ret = security_file_permission(file, MAY_READ);
  1258. if (unlikely(ret))
  1259. break;
  1260. ret = aio_setup_vectored_rw(READ, kiocb);
  1261. if (ret)
  1262. break;
  1263. ret = -EINVAL;
  1264. if (file->f_op->aio_read)
  1265. kiocb->ki_retry = aio_rw_vect_retry;
  1266. break;
  1267. case IOCB_CMD_PWRITEV:
  1268. ret = -EBADF;
  1269. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1270. break;
  1271. ret = security_file_permission(file, MAY_WRITE);
  1272. if (unlikely(ret))
  1273. break;
  1274. ret = aio_setup_vectored_rw(WRITE, kiocb);
  1275. if (ret)
  1276. break;
  1277. ret = -EINVAL;
  1278. if (file->f_op->aio_write)
  1279. kiocb->ki_retry = aio_rw_vect_retry;
  1280. break;
  1281. case IOCB_CMD_FDSYNC:
  1282. ret = -EINVAL;
  1283. if (file->f_op->aio_fsync)
  1284. kiocb->ki_retry = aio_fdsync;
  1285. break;
  1286. case IOCB_CMD_FSYNC:
  1287. ret = -EINVAL;
  1288. if (file->f_op->aio_fsync)
  1289. kiocb->ki_retry = aio_fsync;
  1290. break;
  1291. default:
  1292. dprintk("EINVAL: io_submit: no operation provided\n");
  1293. ret = -EINVAL;
  1294. }
  1295. if (!kiocb->ki_retry)
  1296. return ret;
  1297. return 0;
  1298. }
  1299. /*
  1300. * aio_wake_function:
  1301. * wait queue callback function for aio notification,
  1302. * Simply triggers a retry of the operation via kick_iocb.
  1303. *
  1304. * This callback is specified in the wait queue entry in
  1305. * a kiocb (current->io_wait points to this wait queue
  1306. * entry when an aio operation executes; it is used
  1307. * instead of a synchronous wait when an i/o blocking
  1308. * condition is encountered during aio).
  1309. *
  1310. * Note:
  1311. * This routine is executed with the wait queue lock held.
  1312. * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
  1313. * the ioctx lock inside the wait queue lock. This is safe
  1314. * because this callback isn't used for wait queues which
  1315. * are nested inside ioctx lock (i.e. ctx->wait)
  1316. */
  1317. static int aio_wake_function(wait_queue_t *wait, unsigned mode,
  1318. int sync, void *key)
  1319. {
  1320. struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
  1321. list_del_init(&wait->task_list);
  1322. kick_iocb(iocb);
  1323. return 1;
  1324. }
  1325. int fastcall io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  1326. struct iocb *iocb)
  1327. {
  1328. struct kiocb *req;
  1329. struct file *file;
  1330. ssize_t ret;
  1331. /* enforce forwards compatibility on users */
  1332. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2 ||
  1333. iocb->aio_reserved3)) {
  1334. pr_debug("EINVAL: io_submit: reserve field set\n");
  1335. return -EINVAL;
  1336. }
  1337. /* prevent overflows */
  1338. if (unlikely(
  1339. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  1340. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  1341. ((ssize_t)iocb->aio_nbytes < 0)
  1342. )) {
  1343. pr_debug("EINVAL: io_submit: overflow check\n");
  1344. return -EINVAL;
  1345. }
  1346. file = fget(iocb->aio_fildes);
  1347. if (unlikely(!file))
  1348. return -EBADF;
  1349. req = aio_get_req(ctx); /* returns with 2 references to req */
  1350. if (unlikely(!req)) {
  1351. fput(file);
  1352. return -EAGAIN;
  1353. }
  1354. req->ki_filp = file;
  1355. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1356. if (unlikely(ret)) {
  1357. dprintk("EFAULT: aio_key\n");
  1358. goto out_put_req;
  1359. }
  1360. req->ki_obj.user = user_iocb;
  1361. req->ki_user_data = iocb->aio_data;
  1362. req->ki_pos = iocb->aio_offset;
  1363. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1364. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1365. req->ki_opcode = iocb->aio_lio_opcode;
  1366. init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
  1367. INIT_LIST_HEAD(&req->ki_wait.task_list);
  1368. ret = aio_setup_iocb(req);
  1369. if (ret)
  1370. goto out_put_req;
  1371. spin_lock_irq(&ctx->ctx_lock);
  1372. aio_run_iocb(req);
  1373. if (!list_empty(&ctx->run_list)) {
  1374. /* drain the run list */
  1375. while (__aio_run_iocbs(ctx))
  1376. ;
  1377. }
  1378. spin_unlock_irq(&ctx->ctx_lock);
  1379. aio_put_req(req); /* drop extra ref to req */
  1380. return 0;
  1381. out_put_req:
  1382. aio_put_req(req); /* drop extra ref to req */
  1383. aio_put_req(req); /* drop i/o ref to req */
  1384. return ret;
  1385. }
  1386. /* sys_io_submit:
  1387. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1388. * the number of iocbs queued. May return -EINVAL if the aio_context
  1389. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1390. * *iocbpp[0] is not properly initialized, if the operation specified
  1391. * is invalid for the file descriptor in the iocb. May fail with
  1392. * -EFAULT if any of the data structures point to invalid data. May
  1393. * fail with -EBADF if the file descriptor specified in the first
  1394. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1395. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1396. * fail with -ENOSYS if not implemented.
  1397. */
  1398. asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
  1399. struct iocb __user * __user *iocbpp)
  1400. {
  1401. struct kioctx *ctx;
  1402. long ret = 0;
  1403. int i;
  1404. if (unlikely(nr < 0))
  1405. return -EINVAL;
  1406. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1407. return -EFAULT;
  1408. ctx = lookup_ioctx(ctx_id);
  1409. if (unlikely(!ctx)) {
  1410. pr_debug("EINVAL: io_submit: invalid context id\n");
  1411. return -EINVAL;
  1412. }
  1413. /*
  1414. * AKPM: should this return a partial result if some of the IOs were
  1415. * successfully submitted?
  1416. */
  1417. for (i=0; i<nr; i++) {
  1418. struct iocb __user *user_iocb;
  1419. struct iocb tmp;
  1420. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1421. ret = -EFAULT;
  1422. break;
  1423. }
  1424. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1425. ret = -EFAULT;
  1426. break;
  1427. }
  1428. ret = io_submit_one(ctx, user_iocb, &tmp);
  1429. if (ret)
  1430. break;
  1431. }
  1432. put_ioctx(ctx);
  1433. return i ? i : ret;
  1434. }
  1435. /* lookup_kiocb
  1436. * Finds a given iocb for cancellation.
  1437. */
  1438. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1439. u32 key)
  1440. {
  1441. struct list_head *pos;
  1442. assert_spin_locked(&ctx->ctx_lock);
  1443. /* TODO: use a hash or array, this sucks. */
  1444. list_for_each(pos, &ctx->active_reqs) {
  1445. struct kiocb *kiocb = list_kiocb(pos);
  1446. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1447. return kiocb;
  1448. }
  1449. return NULL;
  1450. }
  1451. /* sys_io_cancel:
  1452. * Attempts to cancel an iocb previously passed to io_submit. If
  1453. * the operation is successfully cancelled, the resulting event is
  1454. * copied into the memory pointed to by result without being placed
  1455. * into the completion queue and 0 is returned. May fail with
  1456. * -EFAULT if any of the data structures pointed to are invalid.
  1457. * May fail with -EINVAL if aio_context specified by ctx_id is
  1458. * invalid. May fail with -EAGAIN if the iocb specified was not
  1459. * cancelled. Will fail with -ENOSYS if not implemented.
  1460. */
  1461. asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
  1462. struct io_event __user *result)
  1463. {
  1464. int (*cancel)(struct kiocb *iocb, struct io_event *res);
  1465. struct kioctx *ctx;
  1466. struct kiocb *kiocb;
  1467. u32 key;
  1468. int ret;
  1469. ret = get_user(key, &iocb->aio_key);
  1470. if (unlikely(ret))
  1471. return -EFAULT;
  1472. ctx = lookup_ioctx(ctx_id);
  1473. if (unlikely(!ctx))
  1474. return -EINVAL;
  1475. spin_lock_irq(&ctx->ctx_lock);
  1476. ret = -EAGAIN;
  1477. kiocb = lookup_kiocb(ctx, iocb, key);
  1478. if (kiocb && kiocb->ki_cancel) {
  1479. cancel = kiocb->ki_cancel;
  1480. kiocb->ki_users ++;
  1481. kiocbSetCancelled(kiocb);
  1482. } else
  1483. cancel = NULL;
  1484. spin_unlock_irq(&ctx->ctx_lock);
  1485. if (NULL != cancel) {
  1486. struct io_event tmp;
  1487. pr_debug("calling cancel\n");
  1488. memset(&tmp, 0, sizeof(tmp));
  1489. tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
  1490. tmp.data = kiocb->ki_user_data;
  1491. ret = cancel(kiocb, &tmp);
  1492. if (!ret) {
  1493. /* Cancellation succeeded -- copy the result
  1494. * into the user's buffer.
  1495. */
  1496. if (copy_to_user(result, &tmp, sizeof(tmp)))
  1497. ret = -EFAULT;
  1498. }
  1499. } else
  1500. ret = -EINVAL;
  1501. put_ioctx(ctx);
  1502. return ret;
  1503. }
  1504. /* io_getevents:
  1505. * Attempts to read at least min_nr events and up to nr events from
  1506. * the completion queue for the aio_context specified by ctx_id. May
  1507. * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
  1508. * if nr is out of range, if when is out of range. May fail with
  1509. * -EFAULT if any of the memory specified to is invalid. May return
  1510. * 0 or < min_nr if no events are available and the timeout specified
  1511. * by when has elapsed, where when == NULL specifies an infinite
  1512. * timeout. Note that the timeout pointed to by when is relative and
  1513. * will be updated if not NULL and the operation blocks. Will fail
  1514. * with -ENOSYS if not implemented.
  1515. */
  1516. asmlinkage long sys_io_getevents(aio_context_t ctx_id,
  1517. long min_nr,
  1518. long nr,
  1519. struct io_event __user *events,
  1520. struct timespec __user *timeout)
  1521. {
  1522. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1523. long ret = -EINVAL;
  1524. if (likely(ioctx)) {
  1525. if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
  1526. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1527. put_ioctx(ioctx);
  1528. }
  1529. return ret;
  1530. }
  1531. __initcall(aio_setup);
  1532. EXPORT_SYMBOL(aio_complete);
  1533. EXPORT_SYMBOL(aio_put_req);
  1534. EXPORT_SYMBOL(wait_on_sync_kiocb);