aio.c 44 KB

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