aio.c 46 KB

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