aio.c 43 KB

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