aio.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789
  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 <linux/eventfd.h>
  32. #include <asm/kmap_types.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/mmu_context.h>
  35. #if DEBUG > 1
  36. #define dprintk printk
  37. #else
  38. #define dprintk(x...) do { ; } while (0)
  39. #endif
  40. /*------ sysctl variables----*/
  41. static DEFINE_SPINLOCK(aio_nr_lock);
  42. unsigned long aio_nr; /* current system wide number of aio requests */
  43. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  44. /*----end sysctl variables---*/
  45. static struct kmem_cache *kiocb_cachep;
  46. static struct kmem_cache *kioctx_cachep;
  47. static struct workqueue_struct *aio_wq;
  48. /* Used for rare fput completion. */
  49. static void aio_fput_routine(struct work_struct *);
  50. static DECLARE_WORK(fput_work, aio_fput_routine);
  51. static DEFINE_SPINLOCK(fput_lock);
  52. static LIST_HEAD(fput_head);
  53. static void aio_kick_handler(struct work_struct *);
  54. static void aio_queue_work(struct kioctx *);
  55. /* aio_setup
  56. * Creates the slab caches used by the aio routines, panic on
  57. * failure as this is done early during the boot sequence.
  58. */
  59. static int __init aio_setup(void)
  60. {
  61. kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  62. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  63. aio_wq = create_workqueue("aio");
  64. pr_debug("aio_setup: sizeof(struct page) = %d\n", (int)sizeof(struct page));
  65. return 0;
  66. }
  67. static void aio_free_ring(struct kioctx *ctx)
  68. {
  69. struct aio_ring_info *info = &ctx->ring_info;
  70. long i;
  71. for (i=0; i<info->nr_pages; i++)
  72. put_page(info->ring_pages[i]);
  73. if (info->mmap_size) {
  74. down_write(&ctx->mm->mmap_sem);
  75. do_munmap(ctx->mm, info->mmap_base, info->mmap_size);
  76. up_write(&ctx->mm->mmap_sem);
  77. }
  78. if (info->ring_pages && info->ring_pages != info->internal_pages)
  79. kfree(info->ring_pages);
  80. info->ring_pages = NULL;
  81. info->nr = 0;
  82. }
  83. static int aio_setup_ring(struct kioctx *ctx)
  84. {
  85. struct aio_ring *ring;
  86. struct aio_ring_info *info = &ctx->ring_info;
  87. unsigned nr_events = ctx->max_reqs;
  88. unsigned long size;
  89. int nr_pages;
  90. /* Compensate for the ring buffer's head/tail overlap entry */
  91. nr_events += 2; /* 1 is required, 2 for good luck */
  92. size = sizeof(struct aio_ring);
  93. size += sizeof(struct io_event) * nr_events;
  94. nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
  95. if (nr_pages < 0)
  96. return -EINVAL;
  97. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
  98. info->nr = 0;
  99. info->ring_pages = info->internal_pages;
  100. if (nr_pages > AIO_RING_PAGES) {
  101. info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  102. if (!info->ring_pages)
  103. return -ENOMEM;
  104. }
  105. info->mmap_size = nr_pages * PAGE_SIZE;
  106. dprintk("attempting mmap of %lu bytes\n", info->mmap_size);
  107. down_write(&ctx->mm->mmap_sem);
  108. info->mmap_base = do_mmap(NULL, 0, info->mmap_size,
  109. PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
  110. 0);
  111. if (IS_ERR((void *)info->mmap_base)) {
  112. up_write(&ctx->mm->mmap_sem);
  113. info->mmap_size = 0;
  114. aio_free_ring(ctx);
  115. return -EAGAIN;
  116. }
  117. dprintk("mmap address: 0x%08lx\n", info->mmap_base);
  118. info->nr_pages = get_user_pages(current, ctx->mm,
  119. info->mmap_base, nr_pages,
  120. 1, 0, info->ring_pages, NULL);
  121. up_write(&ctx->mm->mmap_sem);
  122. if (unlikely(info->nr_pages != nr_pages)) {
  123. aio_free_ring(ctx);
  124. return -EAGAIN;
  125. }
  126. ctx->user_id = info->mmap_base;
  127. info->nr = nr_events; /* trusted copy */
  128. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  129. ring->nr = nr_events; /* user copy */
  130. ring->id = ctx->user_id;
  131. ring->head = ring->tail = 0;
  132. ring->magic = AIO_RING_MAGIC;
  133. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  134. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  135. ring->header_length = sizeof(struct aio_ring);
  136. kunmap_atomic(ring, KM_USER0);
  137. return 0;
  138. }
  139. /* aio_ring_event: returns a pointer to the event at the given index from
  140. * kmap_atomic(, km). Release the pointer with put_aio_ring_event();
  141. */
  142. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  143. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  144. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  145. #define aio_ring_event(info, nr, km) ({ \
  146. unsigned pos = (nr) + AIO_EVENTS_OFFSET; \
  147. struct io_event *__event; \
  148. __event = kmap_atomic( \
  149. (info)->ring_pages[pos / AIO_EVENTS_PER_PAGE], km); \
  150. __event += pos % AIO_EVENTS_PER_PAGE; \
  151. __event; \
  152. })
  153. #define put_aio_ring_event(event, km) do { \
  154. struct io_event *__event = (event); \
  155. (void)__event; \
  156. kunmap_atomic((void *)((unsigned long)__event & PAGE_MASK), km); \
  157. } while(0)
  158. /* ioctx_alloc
  159. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  160. */
  161. static struct kioctx *ioctx_alloc(unsigned nr_events)
  162. {
  163. struct mm_struct *mm;
  164. struct kioctx *ctx;
  165. /* Prevent overflows */
  166. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  167. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  168. pr_debug("ENOMEM: nr_events too high\n");
  169. return ERR_PTR(-EINVAL);
  170. }
  171. if ((unsigned long)nr_events > aio_max_nr)
  172. return ERR_PTR(-EAGAIN);
  173. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  174. if (!ctx)
  175. return ERR_PTR(-ENOMEM);
  176. ctx->max_reqs = nr_events;
  177. mm = ctx->mm = current->mm;
  178. atomic_inc(&mm->mm_count);
  179. atomic_set(&ctx->users, 1);
  180. spin_lock_init(&ctx->ctx_lock);
  181. spin_lock_init(&ctx->ring_info.ring_lock);
  182. init_waitqueue_head(&ctx->wait);
  183. INIT_LIST_HEAD(&ctx->active_reqs);
  184. INIT_LIST_HEAD(&ctx->run_list);
  185. INIT_DELAYED_WORK(&ctx->wq, aio_kick_handler);
  186. if (aio_setup_ring(ctx) < 0)
  187. goto out_freectx;
  188. /* limit the number of system wide aios */
  189. spin_lock(&aio_nr_lock);
  190. if (aio_nr + ctx->max_reqs > aio_max_nr ||
  191. aio_nr + ctx->max_reqs < aio_nr)
  192. ctx->max_reqs = 0;
  193. else
  194. aio_nr += ctx->max_reqs;
  195. spin_unlock(&aio_nr_lock);
  196. if (ctx->max_reqs == 0)
  197. goto out_cleanup;
  198. /* now link into global list. kludge. FIXME */
  199. write_lock(&mm->ioctx_list_lock);
  200. ctx->next = mm->ioctx_list;
  201. mm->ioctx_list = ctx;
  202. write_unlock(&mm->ioctx_list_lock);
  203. dprintk("aio: allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  204. ctx, ctx->user_id, current->mm, ctx->ring_info.nr);
  205. return ctx;
  206. out_cleanup:
  207. __put_ioctx(ctx);
  208. return ERR_PTR(-EAGAIN);
  209. out_freectx:
  210. mmdrop(mm);
  211. kmem_cache_free(kioctx_cachep, ctx);
  212. ctx = ERR_PTR(-ENOMEM);
  213. dprintk("aio: error allocating ioctx %p\n", ctx);
  214. return ctx;
  215. }
  216. /* aio_cancel_all
  217. * Cancels all outstanding aio requests on an aio context. Used
  218. * when the processes owning a context have all exited to encourage
  219. * the rapid destruction of the kioctx.
  220. */
  221. static void aio_cancel_all(struct kioctx *ctx)
  222. {
  223. int (*cancel)(struct kiocb *, struct io_event *);
  224. struct io_event res;
  225. spin_lock_irq(&ctx->ctx_lock);
  226. ctx->dead = 1;
  227. while (!list_empty(&ctx->active_reqs)) {
  228. struct list_head *pos = ctx->active_reqs.next;
  229. struct kiocb *iocb = list_kiocb(pos);
  230. list_del_init(&iocb->ki_list);
  231. cancel = iocb->ki_cancel;
  232. kiocbSetCancelled(iocb);
  233. if (cancel) {
  234. iocb->ki_users++;
  235. spin_unlock_irq(&ctx->ctx_lock);
  236. cancel(iocb, &res);
  237. spin_lock_irq(&ctx->ctx_lock);
  238. }
  239. }
  240. spin_unlock_irq(&ctx->ctx_lock);
  241. }
  242. static void wait_for_all_aios(struct kioctx *ctx)
  243. {
  244. struct task_struct *tsk = current;
  245. DECLARE_WAITQUEUE(wait, tsk);
  246. spin_lock_irq(&ctx->ctx_lock);
  247. if (!ctx->reqs_active)
  248. goto out;
  249. add_wait_queue(&ctx->wait, &wait);
  250. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  251. while (ctx->reqs_active) {
  252. spin_unlock_irq(&ctx->ctx_lock);
  253. schedule();
  254. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  255. spin_lock_irq(&ctx->ctx_lock);
  256. }
  257. __set_task_state(tsk, TASK_RUNNING);
  258. remove_wait_queue(&ctx->wait, &wait);
  259. out:
  260. spin_unlock_irq(&ctx->ctx_lock);
  261. }
  262. /* wait_on_sync_kiocb:
  263. * Waits on the given sync kiocb to complete.
  264. */
  265. ssize_t fastcall wait_on_sync_kiocb(struct kiocb *iocb)
  266. {
  267. while (iocb->ki_users) {
  268. set_current_state(TASK_UNINTERRUPTIBLE);
  269. if (!iocb->ki_users)
  270. break;
  271. schedule();
  272. }
  273. __set_current_state(TASK_RUNNING);
  274. return iocb->ki_user_data;
  275. }
  276. /* exit_aio: called when the last user of mm goes away. At this point,
  277. * there is no way for any new requests to be submited or any of the
  278. * io_* syscalls to be called on the context. However, there may be
  279. * outstanding requests which hold references to the context; as they
  280. * go away, they will call put_ioctx and release any pinned memory
  281. * associated with the request (held via struct page * references).
  282. */
  283. void fastcall exit_aio(struct mm_struct *mm)
  284. {
  285. struct kioctx *ctx = mm->ioctx_list;
  286. mm->ioctx_list = NULL;
  287. while (ctx) {
  288. struct kioctx *next = ctx->next;
  289. ctx->next = NULL;
  290. aio_cancel_all(ctx);
  291. wait_for_all_aios(ctx);
  292. /*
  293. * Ensure we don't leave the ctx on the aio_wq
  294. */
  295. cancel_work_sync(&ctx->wq.work);
  296. if (1 != atomic_read(&ctx->users))
  297. printk(KERN_DEBUG
  298. "exit_aio:ioctx still alive: %d %d %d\n",
  299. atomic_read(&ctx->users), ctx->dead,
  300. ctx->reqs_active);
  301. put_ioctx(ctx);
  302. ctx = next;
  303. }
  304. }
  305. /* __put_ioctx
  306. * Called when the last user of an aio context has gone away,
  307. * and the struct needs to be freed.
  308. */
  309. void fastcall __put_ioctx(struct kioctx *ctx)
  310. {
  311. unsigned nr_events = ctx->max_reqs;
  312. BUG_ON(ctx->reqs_active);
  313. cancel_delayed_work(&ctx->wq);
  314. cancel_work_sync(&ctx->wq.work);
  315. aio_free_ring(ctx);
  316. mmdrop(ctx->mm);
  317. ctx->mm = NULL;
  318. pr_debug("__put_ioctx: freeing %p\n", ctx);
  319. kmem_cache_free(kioctx_cachep, ctx);
  320. if (nr_events) {
  321. spin_lock(&aio_nr_lock);
  322. BUG_ON(aio_nr - nr_events > aio_nr);
  323. aio_nr -= nr_events;
  324. spin_unlock(&aio_nr_lock);
  325. }
  326. }
  327. /* aio_get_req
  328. * Allocate a slot for an aio request. Increments the users count
  329. * of the kioctx so that the kioctx stays around until all requests are
  330. * complete. Returns NULL if no requests are free.
  331. *
  332. * Returns with kiocb->users set to 2. The io submit code path holds
  333. * an extra reference while submitting the i/o.
  334. * This prevents races between the aio code path referencing the
  335. * req (after submitting it) and aio_complete() freeing the req.
  336. */
  337. static struct kiocb *FASTCALL(__aio_get_req(struct kioctx *ctx));
  338. static struct kiocb fastcall *__aio_get_req(struct kioctx *ctx)
  339. {
  340. struct kiocb *req = NULL;
  341. struct aio_ring *ring;
  342. int okay = 0;
  343. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL);
  344. if (unlikely(!req))
  345. return NULL;
  346. req->ki_flags = 0;
  347. req->ki_users = 2;
  348. req->ki_key = 0;
  349. req->ki_ctx = ctx;
  350. req->ki_cancel = NULL;
  351. req->ki_retry = NULL;
  352. req->ki_dtor = NULL;
  353. req->private = NULL;
  354. req->ki_iovec = NULL;
  355. INIT_LIST_HEAD(&req->ki_run_list);
  356. req->ki_eventfd = ERR_PTR(-EINVAL);
  357. /* Check if the completion queue has enough free space to
  358. * accept an event from this io.
  359. */
  360. spin_lock_irq(&ctx->ctx_lock);
  361. ring = kmap_atomic(ctx->ring_info.ring_pages[0], KM_USER0);
  362. if (ctx->reqs_active < aio_ring_avail(&ctx->ring_info, ring)) {
  363. list_add(&req->ki_list, &ctx->active_reqs);
  364. ctx->reqs_active++;
  365. okay = 1;
  366. }
  367. kunmap_atomic(ring, KM_USER0);
  368. spin_unlock_irq(&ctx->ctx_lock);
  369. if (!okay) {
  370. kmem_cache_free(kiocb_cachep, req);
  371. req = NULL;
  372. }
  373. return req;
  374. }
  375. static inline struct kiocb *aio_get_req(struct kioctx *ctx)
  376. {
  377. struct kiocb *req;
  378. /* Handle a potential starvation case -- should be exceedingly rare as
  379. * requests will be stuck on fput_head only if the aio_fput_routine is
  380. * delayed and the requests were the last user of the struct file.
  381. */
  382. req = __aio_get_req(ctx);
  383. if (unlikely(NULL == req)) {
  384. aio_fput_routine(NULL);
  385. req = __aio_get_req(ctx);
  386. }
  387. return req;
  388. }
  389. static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
  390. {
  391. assert_spin_locked(&ctx->ctx_lock);
  392. if (!IS_ERR(req->ki_eventfd))
  393. fput(req->ki_eventfd);
  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. /*
  826. * Check if the user asked us to deliver the result through an
  827. * eventfd. The eventfd_signal() function is safe to be called
  828. * from IRQ context.
  829. */
  830. if (!IS_ERR(iocb->ki_eventfd))
  831. eventfd_signal(iocb->ki_eventfd, 1);
  832. info = &ctx->ring_info;
  833. /* add a completion event to the ring buffer.
  834. * must be done holding ctx->ctx_lock to prevent
  835. * other code from messing with the tail
  836. * pointer since we might be called from irq
  837. * context.
  838. */
  839. spin_lock_irqsave(&ctx->ctx_lock, flags);
  840. if (iocb->ki_run_list.prev && !list_empty(&iocb->ki_run_list))
  841. list_del_init(&iocb->ki_run_list);
  842. /*
  843. * cancelled requests don't get events, userland was given one
  844. * when the event got cancelled.
  845. */
  846. if (kiocbIsCancelled(iocb))
  847. goto put_rq;
  848. ring = kmap_atomic(info->ring_pages[0], KM_IRQ1);
  849. tail = info->tail;
  850. event = aio_ring_event(info, tail, KM_IRQ0);
  851. if (++tail >= info->nr)
  852. tail = 0;
  853. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  854. event->data = iocb->ki_user_data;
  855. event->res = res;
  856. event->res2 = res2;
  857. dprintk("aio_complete: %p[%lu]: %p: %p %Lx %lx %lx\n",
  858. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  859. res, res2);
  860. /* after flagging the request as done, we
  861. * must never even look at it again
  862. */
  863. smp_wmb(); /* make event visible before updating tail */
  864. info->tail = tail;
  865. ring->tail = tail;
  866. put_aio_ring_event(event, KM_IRQ0);
  867. kunmap_atomic(ring, KM_IRQ1);
  868. pr_debug("added to ring %p at [%lu]\n", iocb, tail);
  869. put_rq:
  870. /* everything turned out well, dispose of the aiocb. */
  871. ret = __aio_put_req(ctx, iocb);
  872. if (waitqueue_active(&ctx->wait))
  873. wake_up(&ctx->wait);
  874. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  875. return ret;
  876. }
  877. /* aio_read_evt
  878. * Pull an event off of the ioctx's event ring. Returns the number of
  879. * events fetched (0 or 1 ;-)
  880. * FIXME: make this use cmpxchg.
  881. * TODO: make the ringbuffer user mmap()able (requires FIXME).
  882. */
  883. static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
  884. {
  885. struct aio_ring_info *info = &ioctx->ring_info;
  886. struct aio_ring *ring;
  887. unsigned long head;
  888. int ret = 0;
  889. ring = kmap_atomic(info->ring_pages[0], KM_USER0);
  890. dprintk("in aio_read_evt h%lu t%lu m%lu\n",
  891. (unsigned long)ring->head, (unsigned long)ring->tail,
  892. (unsigned long)ring->nr);
  893. if (ring->head == ring->tail)
  894. goto out;
  895. spin_lock(&info->ring_lock);
  896. head = ring->head % info->nr;
  897. if (head != ring->tail) {
  898. struct io_event *evp = aio_ring_event(info, head, KM_USER1);
  899. *ent = *evp;
  900. head = (head + 1) % info->nr;
  901. smp_mb(); /* finish reading the event before updatng the head */
  902. ring->head = head;
  903. ret = 1;
  904. put_aio_ring_event(evp, KM_USER1);
  905. }
  906. spin_unlock(&info->ring_lock);
  907. out:
  908. kunmap_atomic(ring, KM_USER0);
  909. dprintk("leaving aio_read_evt: %d h%lu t%lu\n", ret,
  910. (unsigned long)ring->head, (unsigned long)ring->tail);
  911. return ret;
  912. }
  913. struct aio_timeout {
  914. struct timer_list timer;
  915. int timed_out;
  916. struct task_struct *p;
  917. };
  918. static void timeout_func(unsigned long data)
  919. {
  920. struct aio_timeout *to = (struct aio_timeout *)data;
  921. to->timed_out = 1;
  922. wake_up_process(to->p);
  923. }
  924. static inline void init_timeout(struct aio_timeout *to)
  925. {
  926. init_timer(&to->timer);
  927. to->timer.data = (unsigned long)to;
  928. to->timer.function = timeout_func;
  929. to->timed_out = 0;
  930. to->p = current;
  931. }
  932. static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
  933. const struct timespec *ts)
  934. {
  935. to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
  936. if (time_after(to->timer.expires, jiffies))
  937. add_timer(&to->timer);
  938. else
  939. to->timed_out = 1;
  940. }
  941. static inline void clear_timeout(struct aio_timeout *to)
  942. {
  943. del_singleshot_timer_sync(&to->timer);
  944. }
  945. static int read_events(struct kioctx *ctx,
  946. long min_nr, long nr,
  947. struct io_event __user *event,
  948. struct timespec __user *timeout)
  949. {
  950. long start_jiffies = jiffies;
  951. struct task_struct *tsk = current;
  952. DECLARE_WAITQUEUE(wait, tsk);
  953. int ret;
  954. int i = 0;
  955. struct io_event ent;
  956. struct aio_timeout to;
  957. int retry = 0;
  958. /* needed to zero any padding within an entry (there shouldn't be
  959. * any, but C is fun!
  960. */
  961. memset(&ent, 0, sizeof(ent));
  962. retry:
  963. ret = 0;
  964. while (likely(i < nr)) {
  965. ret = aio_read_evt(ctx, &ent);
  966. if (unlikely(ret <= 0))
  967. break;
  968. dprintk("read event: %Lx %Lx %Lx %Lx\n",
  969. ent.data, ent.obj, ent.res, ent.res2);
  970. /* Could we split the check in two? */
  971. ret = -EFAULT;
  972. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  973. dprintk("aio: lost an event due to EFAULT.\n");
  974. break;
  975. }
  976. ret = 0;
  977. /* Good, event copied to userland, update counts. */
  978. event ++;
  979. i ++;
  980. }
  981. if (min_nr <= i)
  982. return i;
  983. if (ret)
  984. return ret;
  985. /* End fast path */
  986. /* racey check, but it gets redone */
  987. if (!retry && unlikely(!list_empty(&ctx->run_list))) {
  988. retry = 1;
  989. aio_run_all_iocbs(ctx);
  990. goto retry;
  991. }
  992. init_timeout(&to);
  993. if (timeout) {
  994. struct timespec ts;
  995. ret = -EFAULT;
  996. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  997. goto out;
  998. set_timeout(start_jiffies, &to, &ts);
  999. }
  1000. while (likely(i < nr)) {
  1001. add_wait_queue_exclusive(&ctx->wait, &wait);
  1002. do {
  1003. set_task_state(tsk, TASK_INTERRUPTIBLE);
  1004. ret = aio_read_evt(ctx, &ent);
  1005. if (ret)
  1006. break;
  1007. if (min_nr <= i)
  1008. break;
  1009. ret = 0;
  1010. if (to.timed_out) /* Only check after read evt */
  1011. break;
  1012. schedule();
  1013. if (signal_pending(tsk)) {
  1014. ret = -EINTR;
  1015. break;
  1016. }
  1017. /*ret = aio_read_evt(ctx, &ent);*/
  1018. } while (1) ;
  1019. set_task_state(tsk, TASK_RUNNING);
  1020. remove_wait_queue(&ctx->wait, &wait);
  1021. if (unlikely(ret <= 0))
  1022. break;
  1023. ret = -EFAULT;
  1024. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  1025. dprintk("aio: lost an event due to EFAULT.\n");
  1026. break;
  1027. }
  1028. /* Good, event copied to userland, update counts. */
  1029. event ++;
  1030. i ++;
  1031. }
  1032. if (timeout)
  1033. clear_timeout(&to);
  1034. out:
  1035. return i ? i : ret;
  1036. }
  1037. /* Take an ioctx and remove it from the list of ioctx's. Protects
  1038. * against races with itself via ->dead.
  1039. */
  1040. static void io_destroy(struct kioctx *ioctx)
  1041. {
  1042. struct mm_struct *mm = current->mm;
  1043. struct kioctx **tmp;
  1044. int was_dead;
  1045. /* delete the entry from the list is someone else hasn't already */
  1046. write_lock(&mm->ioctx_list_lock);
  1047. was_dead = ioctx->dead;
  1048. ioctx->dead = 1;
  1049. for (tmp = &mm->ioctx_list; *tmp && *tmp != ioctx;
  1050. tmp = &(*tmp)->next)
  1051. ;
  1052. if (*tmp)
  1053. *tmp = ioctx->next;
  1054. write_unlock(&mm->ioctx_list_lock);
  1055. dprintk("aio_release(%p)\n", ioctx);
  1056. if (likely(!was_dead))
  1057. put_ioctx(ioctx); /* twice for the list */
  1058. aio_cancel_all(ioctx);
  1059. wait_for_all_aios(ioctx);
  1060. put_ioctx(ioctx); /* once for the lookup */
  1061. }
  1062. /* sys_io_setup:
  1063. * Create an aio_context capable of receiving at least nr_events.
  1064. * ctxp must not point to an aio_context that already exists, and
  1065. * must be initialized to 0 prior to the call. On successful
  1066. * creation of the aio_context, *ctxp is filled in with the resulting
  1067. * handle. May fail with -EINVAL if *ctxp is not initialized,
  1068. * if the specified nr_events exceeds internal limits. May fail
  1069. * with -EAGAIN if the specified nr_events exceeds the user's limit
  1070. * of available events. May fail with -ENOMEM if insufficient kernel
  1071. * resources are available. May fail with -EFAULT if an invalid
  1072. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  1073. * implemented.
  1074. */
  1075. asmlinkage long sys_io_setup(unsigned nr_events, aio_context_t __user *ctxp)
  1076. {
  1077. struct kioctx *ioctx = NULL;
  1078. unsigned long ctx;
  1079. long ret;
  1080. ret = get_user(ctx, ctxp);
  1081. if (unlikely(ret))
  1082. goto out;
  1083. ret = -EINVAL;
  1084. if (unlikely(ctx || nr_events == 0)) {
  1085. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  1086. ctx, nr_events);
  1087. goto out;
  1088. }
  1089. ioctx = ioctx_alloc(nr_events);
  1090. ret = PTR_ERR(ioctx);
  1091. if (!IS_ERR(ioctx)) {
  1092. ret = put_user(ioctx->user_id, ctxp);
  1093. if (!ret)
  1094. return 0;
  1095. get_ioctx(ioctx); /* io_destroy() expects us to hold a ref */
  1096. io_destroy(ioctx);
  1097. }
  1098. out:
  1099. return ret;
  1100. }
  1101. /* sys_io_destroy:
  1102. * Destroy the aio_context specified. May cancel any outstanding
  1103. * AIOs and block on completion. Will fail with -ENOSYS if not
  1104. * implemented. May fail with -EFAULT if the context pointed to
  1105. * is invalid.
  1106. */
  1107. asmlinkage long sys_io_destroy(aio_context_t ctx)
  1108. {
  1109. struct kioctx *ioctx = lookup_ioctx(ctx);
  1110. if (likely(NULL != ioctx)) {
  1111. io_destroy(ioctx);
  1112. return 0;
  1113. }
  1114. pr_debug("EINVAL: io_destroy: invalid context id\n");
  1115. return -EINVAL;
  1116. }
  1117. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  1118. {
  1119. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  1120. BUG_ON(ret <= 0);
  1121. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  1122. ssize_t this = min((ssize_t)iov->iov_len, ret);
  1123. iov->iov_base += this;
  1124. iov->iov_len -= this;
  1125. iocb->ki_left -= this;
  1126. ret -= this;
  1127. if (iov->iov_len == 0) {
  1128. iocb->ki_cur_seg++;
  1129. iov++;
  1130. }
  1131. }
  1132. /* the caller should not have done more io than what fit in
  1133. * the remaining iovecs */
  1134. BUG_ON(ret > 0 && iocb->ki_left == 0);
  1135. }
  1136. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  1137. {
  1138. struct file *file = iocb->ki_filp;
  1139. struct address_space *mapping = file->f_mapping;
  1140. struct inode *inode = mapping->host;
  1141. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  1142. unsigned long, loff_t);
  1143. ssize_t ret = 0;
  1144. unsigned short opcode;
  1145. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  1146. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  1147. rw_op = file->f_op->aio_read;
  1148. opcode = IOCB_CMD_PREADV;
  1149. } else {
  1150. rw_op = file->f_op->aio_write;
  1151. opcode = IOCB_CMD_PWRITEV;
  1152. }
  1153. do {
  1154. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  1155. iocb->ki_nr_segs - iocb->ki_cur_seg,
  1156. iocb->ki_pos);
  1157. if (ret > 0)
  1158. aio_advance_iovec(iocb, ret);
  1159. /* retry all partial writes. retry partial reads as long as its a
  1160. * regular file. */
  1161. } while (ret > 0 && iocb->ki_left > 0 &&
  1162. (opcode == IOCB_CMD_PWRITEV ||
  1163. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  1164. /* This means we must have transferred all that we could */
  1165. /* No need to retry anymore */
  1166. if ((ret == 0) || (iocb->ki_left == 0))
  1167. ret = iocb->ki_nbytes - iocb->ki_left;
  1168. return ret;
  1169. }
  1170. static ssize_t aio_fdsync(struct kiocb *iocb)
  1171. {
  1172. struct file *file = iocb->ki_filp;
  1173. ssize_t ret = -EINVAL;
  1174. if (file->f_op->aio_fsync)
  1175. ret = file->f_op->aio_fsync(iocb, 1);
  1176. return ret;
  1177. }
  1178. static ssize_t aio_fsync(struct kiocb *iocb)
  1179. {
  1180. struct file *file = iocb->ki_filp;
  1181. ssize_t ret = -EINVAL;
  1182. if (file->f_op->aio_fsync)
  1183. ret = file->f_op->aio_fsync(iocb, 0);
  1184. return ret;
  1185. }
  1186. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb)
  1187. {
  1188. ssize_t ret;
  1189. ret = rw_copy_check_uvector(type, (struct iovec __user *)kiocb->ki_buf,
  1190. kiocb->ki_nbytes, 1,
  1191. &kiocb->ki_inline_vec, &kiocb->ki_iovec);
  1192. if (ret < 0)
  1193. goto out;
  1194. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  1195. kiocb->ki_cur_seg = 0;
  1196. /* ki_nbytes/left now reflect bytes instead of segs */
  1197. kiocb->ki_nbytes = ret;
  1198. kiocb->ki_left = ret;
  1199. ret = 0;
  1200. out:
  1201. return ret;
  1202. }
  1203. static ssize_t aio_setup_single_vector(struct kiocb *kiocb)
  1204. {
  1205. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  1206. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  1207. kiocb->ki_iovec->iov_len = kiocb->ki_left;
  1208. kiocb->ki_nr_segs = 1;
  1209. kiocb->ki_cur_seg = 0;
  1210. return 0;
  1211. }
  1212. /*
  1213. * aio_setup_iocb:
  1214. * Performs the initial checks and aio retry method
  1215. * setup for the kiocb at the time of io submission.
  1216. */
  1217. static ssize_t aio_setup_iocb(struct kiocb *kiocb)
  1218. {
  1219. struct file *file = kiocb->ki_filp;
  1220. ssize_t ret = 0;
  1221. switch (kiocb->ki_opcode) {
  1222. case IOCB_CMD_PREAD:
  1223. ret = -EBADF;
  1224. if (unlikely(!(file->f_mode & FMODE_READ)))
  1225. break;
  1226. ret = -EFAULT;
  1227. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  1228. kiocb->ki_left)))
  1229. break;
  1230. ret = security_file_permission(file, MAY_READ);
  1231. if (unlikely(ret))
  1232. break;
  1233. ret = aio_setup_single_vector(kiocb);
  1234. if (ret)
  1235. break;
  1236. ret = -EINVAL;
  1237. if (file->f_op->aio_read)
  1238. kiocb->ki_retry = aio_rw_vect_retry;
  1239. break;
  1240. case IOCB_CMD_PWRITE:
  1241. ret = -EBADF;
  1242. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1243. break;
  1244. ret = -EFAULT;
  1245. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  1246. kiocb->ki_left)))
  1247. break;
  1248. ret = security_file_permission(file, MAY_WRITE);
  1249. if (unlikely(ret))
  1250. break;
  1251. ret = aio_setup_single_vector(kiocb);
  1252. if (ret)
  1253. break;
  1254. ret = -EINVAL;
  1255. if (file->f_op->aio_write)
  1256. kiocb->ki_retry = aio_rw_vect_retry;
  1257. break;
  1258. case IOCB_CMD_PREADV:
  1259. ret = -EBADF;
  1260. if (unlikely(!(file->f_mode & FMODE_READ)))
  1261. break;
  1262. ret = security_file_permission(file, MAY_READ);
  1263. if (unlikely(ret))
  1264. break;
  1265. ret = aio_setup_vectored_rw(READ, kiocb);
  1266. if (ret)
  1267. break;
  1268. ret = -EINVAL;
  1269. if (file->f_op->aio_read)
  1270. kiocb->ki_retry = aio_rw_vect_retry;
  1271. break;
  1272. case IOCB_CMD_PWRITEV:
  1273. ret = -EBADF;
  1274. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1275. break;
  1276. ret = security_file_permission(file, MAY_WRITE);
  1277. if (unlikely(ret))
  1278. break;
  1279. ret = aio_setup_vectored_rw(WRITE, kiocb);
  1280. if (ret)
  1281. break;
  1282. ret = -EINVAL;
  1283. if (file->f_op->aio_write)
  1284. kiocb->ki_retry = aio_rw_vect_retry;
  1285. break;
  1286. case IOCB_CMD_FDSYNC:
  1287. ret = -EINVAL;
  1288. if (file->f_op->aio_fsync)
  1289. kiocb->ki_retry = aio_fdsync;
  1290. break;
  1291. case IOCB_CMD_FSYNC:
  1292. ret = -EINVAL;
  1293. if (file->f_op->aio_fsync)
  1294. kiocb->ki_retry = aio_fsync;
  1295. break;
  1296. default:
  1297. dprintk("EINVAL: io_submit: no operation provided\n");
  1298. ret = -EINVAL;
  1299. }
  1300. if (!kiocb->ki_retry)
  1301. return ret;
  1302. return 0;
  1303. }
  1304. /*
  1305. * aio_wake_function:
  1306. * wait queue callback function for aio notification,
  1307. * Simply triggers a retry of the operation via kick_iocb.
  1308. *
  1309. * This callback is specified in the wait queue entry in
  1310. * a kiocb (current->io_wait points to this wait queue
  1311. * entry when an aio operation executes; it is used
  1312. * instead of a synchronous wait when an i/o blocking
  1313. * condition is encountered during aio).
  1314. *
  1315. * Note:
  1316. * This routine is executed with the wait queue lock held.
  1317. * Since kick_iocb acquires iocb->ctx->ctx_lock, it nests
  1318. * the ioctx lock inside the wait queue lock. This is safe
  1319. * because this callback isn't used for wait queues which
  1320. * are nested inside ioctx lock (i.e. ctx->wait)
  1321. */
  1322. static int aio_wake_function(wait_queue_t *wait, unsigned mode,
  1323. int sync, void *key)
  1324. {
  1325. struct kiocb *iocb = container_of(wait, struct kiocb, ki_wait);
  1326. list_del_init(&wait->task_list);
  1327. kick_iocb(iocb);
  1328. return 1;
  1329. }
  1330. int fastcall io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  1331. struct iocb *iocb)
  1332. {
  1333. struct kiocb *req;
  1334. struct file *file;
  1335. ssize_t ret;
  1336. /* enforce forwards compatibility on users */
  1337. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  1338. pr_debug("EINVAL: io_submit: reserve field set\n");
  1339. return -EINVAL;
  1340. }
  1341. /* prevent overflows */
  1342. if (unlikely(
  1343. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  1344. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  1345. ((ssize_t)iocb->aio_nbytes < 0)
  1346. )) {
  1347. pr_debug("EINVAL: io_submit: overflow check\n");
  1348. return -EINVAL;
  1349. }
  1350. file = fget(iocb->aio_fildes);
  1351. if (unlikely(!file))
  1352. return -EBADF;
  1353. req = aio_get_req(ctx); /* returns with 2 references to req */
  1354. if (unlikely(!req)) {
  1355. fput(file);
  1356. return -EAGAIN;
  1357. }
  1358. req->ki_filp = file;
  1359. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  1360. /*
  1361. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1362. * instance of the file* now. The file descriptor must be
  1363. * an eventfd() fd, and will be signaled for each completed
  1364. * event using the eventfd_signal() function.
  1365. */
  1366. req->ki_eventfd = eventfd_fget((int) iocb->aio_resfd);
  1367. if (unlikely(IS_ERR(req->ki_eventfd))) {
  1368. ret = PTR_ERR(req->ki_eventfd);
  1369. goto out_put_req;
  1370. }
  1371. }
  1372. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1373. if (unlikely(ret)) {
  1374. dprintk("EFAULT: aio_key\n");
  1375. goto out_put_req;
  1376. }
  1377. req->ki_obj.user = user_iocb;
  1378. req->ki_user_data = iocb->aio_data;
  1379. req->ki_pos = iocb->aio_offset;
  1380. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1381. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1382. req->ki_opcode = iocb->aio_lio_opcode;
  1383. init_waitqueue_func_entry(&req->ki_wait, aio_wake_function);
  1384. INIT_LIST_HEAD(&req->ki_wait.task_list);
  1385. ret = aio_setup_iocb(req);
  1386. if (ret)
  1387. goto out_put_req;
  1388. spin_lock_irq(&ctx->ctx_lock);
  1389. aio_run_iocb(req);
  1390. if (!list_empty(&ctx->run_list)) {
  1391. /* drain the run list */
  1392. while (__aio_run_iocbs(ctx))
  1393. ;
  1394. }
  1395. spin_unlock_irq(&ctx->ctx_lock);
  1396. aio_put_req(req); /* drop extra ref to req */
  1397. return 0;
  1398. out_put_req:
  1399. aio_put_req(req); /* drop extra ref to req */
  1400. aio_put_req(req); /* drop i/o ref to req */
  1401. return ret;
  1402. }
  1403. /* sys_io_submit:
  1404. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1405. * the number of iocbs queued. May return -EINVAL if the aio_context
  1406. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1407. * *iocbpp[0] is not properly initialized, if the operation specified
  1408. * is invalid for the file descriptor in the iocb. May fail with
  1409. * -EFAULT if any of the data structures point to invalid data. May
  1410. * fail with -EBADF if the file descriptor specified in the first
  1411. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1412. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1413. * fail with -ENOSYS if not implemented.
  1414. */
  1415. asmlinkage long sys_io_submit(aio_context_t ctx_id, long nr,
  1416. struct iocb __user * __user *iocbpp)
  1417. {
  1418. struct kioctx *ctx;
  1419. long ret = 0;
  1420. int i;
  1421. if (unlikely(nr < 0))
  1422. return -EINVAL;
  1423. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1424. return -EFAULT;
  1425. ctx = lookup_ioctx(ctx_id);
  1426. if (unlikely(!ctx)) {
  1427. pr_debug("EINVAL: io_submit: invalid context id\n");
  1428. return -EINVAL;
  1429. }
  1430. /*
  1431. * AKPM: should this return a partial result if some of the IOs were
  1432. * successfully submitted?
  1433. */
  1434. for (i=0; i<nr; i++) {
  1435. struct iocb __user *user_iocb;
  1436. struct iocb tmp;
  1437. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1438. ret = -EFAULT;
  1439. break;
  1440. }
  1441. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1442. ret = -EFAULT;
  1443. break;
  1444. }
  1445. ret = io_submit_one(ctx, user_iocb, &tmp);
  1446. if (ret)
  1447. break;
  1448. }
  1449. put_ioctx(ctx);
  1450. return i ? i : ret;
  1451. }
  1452. /* lookup_kiocb
  1453. * Finds a given iocb for cancellation.
  1454. */
  1455. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1456. u32 key)
  1457. {
  1458. struct list_head *pos;
  1459. assert_spin_locked(&ctx->ctx_lock);
  1460. /* TODO: use a hash or array, this sucks. */
  1461. list_for_each(pos, &ctx->active_reqs) {
  1462. struct kiocb *kiocb = list_kiocb(pos);
  1463. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1464. return kiocb;
  1465. }
  1466. return NULL;
  1467. }
  1468. /* sys_io_cancel:
  1469. * Attempts to cancel an iocb previously passed to io_submit. If
  1470. * the operation is successfully cancelled, the resulting event is
  1471. * copied into the memory pointed to by result without being placed
  1472. * into the completion queue and 0 is returned. May fail with
  1473. * -EFAULT if any of the data structures pointed to are invalid.
  1474. * May fail with -EINVAL if aio_context specified by ctx_id is
  1475. * invalid. May fail with -EAGAIN if the iocb specified was not
  1476. * cancelled. Will fail with -ENOSYS if not implemented.
  1477. */
  1478. asmlinkage long sys_io_cancel(aio_context_t ctx_id, struct iocb __user *iocb,
  1479. struct io_event __user *result)
  1480. {
  1481. int (*cancel)(struct kiocb *iocb, struct io_event *res);
  1482. struct kioctx *ctx;
  1483. struct kiocb *kiocb;
  1484. u32 key;
  1485. int ret;
  1486. ret = get_user(key, &iocb->aio_key);
  1487. if (unlikely(ret))
  1488. return -EFAULT;
  1489. ctx = lookup_ioctx(ctx_id);
  1490. if (unlikely(!ctx))
  1491. return -EINVAL;
  1492. spin_lock_irq(&ctx->ctx_lock);
  1493. ret = -EAGAIN;
  1494. kiocb = lookup_kiocb(ctx, iocb, key);
  1495. if (kiocb && kiocb->ki_cancel) {
  1496. cancel = kiocb->ki_cancel;
  1497. kiocb->ki_users ++;
  1498. kiocbSetCancelled(kiocb);
  1499. } else
  1500. cancel = NULL;
  1501. spin_unlock_irq(&ctx->ctx_lock);
  1502. if (NULL != cancel) {
  1503. struct io_event tmp;
  1504. pr_debug("calling cancel\n");
  1505. memset(&tmp, 0, sizeof(tmp));
  1506. tmp.obj = (u64)(unsigned long)kiocb->ki_obj.user;
  1507. tmp.data = kiocb->ki_user_data;
  1508. ret = cancel(kiocb, &tmp);
  1509. if (!ret) {
  1510. /* Cancellation succeeded -- copy the result
  1511. * into the user's buffer.
  1512. */
  1513. if (copy_to_user(result, &tmp, sizeof(tmp)))
  1514. ret = -EFAULT;
  1515. }
  1516. } else
  1517. ret = -EINVAL;
  1518. put_ioctx(ctx);
  1519. return ret;
  1520. }
  1521. /* io_getevents:
  1522. * Attempts to read at least min_nr events and up to nr events from
  1523. * the completion queue for the aio_context specified by ctx_id. May
  1524. * fail with -EINVAL if ctx_id is invalid, if min_nr is out of range,
  1525. * if nr is out of range, if when is out of range. May fail with
  1526. * -EFAULT if any of the memory specified to is invalid. May return
  1527. * 0 or < min_nr if no events are available and the timeout specified
  1528. * by when has elapsed, where when == NULL specifies an infinite
  1529. * timeout. Note that the timeout pointed to by when is relative and
  1530. * will be updated if not NULL and the operation blocks. Will fail
  1531. * with -ENOSYS if not implemented.
  1532. */
  1533. asmlinkage long sys_io_getevents(aio_context_t ctx_id,
  1534. long min_nr,
  1535. long nr,
  1536. struct io_event __user *events,
  1537. struct timespec __user *timeout)
  1538. {
  1539. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1540. long ret = -EINVAL;
  1541. if (likely(ioctx)) {
  1542. if (likely(min_nr <= nr && min_nr >= 0 && nr >= 0))
  1543. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1544. put_ioctx(ioctx);
  1545. }
  1546. return ret;
  1547. }
  1548. __initcall(aio_setup);
  1549. EXPORT_SYMBOL(aio_complete);
  1550. EXPORT_SYMBOL(aio_put_req);
  1551. EXPORT_SYMBOL(wait_on_sync_kiocb);