aio.c 45 KB

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