aio.c 44 KB

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