aio.c 44 KB

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