aio.c 44 KB

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