aio.c 44 KB

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