aio.c 45 KB

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