aio.c 44 KB

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