aio.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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. #define pr_fmt(fmt) "%s: " fmt, __func__
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/errno.h>
  15. #include <linux/time.h>
  16. #include <linux/aio_abi.h>
  17. #include <linux/export.h>
  18. #include <linux/syscalls.h>
  19. #include <linux/backing-dev.h>
  20. #include <linux/uio.h>
  21. #include <linux/sched.h>
  22. #include <linux/fs.h>
  23. #include <linux/file.h>
  24. #include <linux/mm.h>
  25. #include <linux/mman.h>
  26. #include <linux/mmu_context.h>
  27. #include <linux/slab.h>
  28. #include <linux/timer.h>
  29. #include <linux/aio.h>
  30. #include <linux/highmem.h>
  31. #include <linux/workqueue.h>
  32. #include <linux/security.h>
  33. #include <linux/eventfd.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/compat.h>
  36. #include <asm/kmap_types.h>
  37. #include <asm/uaccess.h>
  38. #define AIO_RING_MAGIC 0xa10a10a1
  39. #define AIO_RING_COMPAT_FEATURES 1
  40. #define AIO_RING_INCOMPAT_FEATURES 0
  41. struct aio_ring {
  42. unsigned id; /* kernel internal index number */
  43. unsigned nr; /* number of io_events */
  44. unsigned head;
  45. unsigned tail;
  46. unsigned magic;
  47. unsigned compat_features;
  48. unsigned incompat_features;
  49. unsigned header_length; /* size of aio_ring */
  50. struct io_event io_events[0];
  51. }; /* 128 bytes + ring size */
  52. #define AIO_RING_PAGES 8
  53. struct aio_ring_info {
  54. unsigned long mmap_base;
  55. unsigned long mmap_size;
  56. struct page **ring_pages;
  57. struct mutex ring_lock;
  58. long nr_pages;
  59. unsigned nr, tail;
  60. struct page *internal_pages[AIO_RING_PAGES];
  61. };
  62. struct kioctx {
  63. atomic_t users;
  64. atomic_t dead;
  65. /* This needs improving */
  66. unsigned long user_id;
  67. struct hlist_node list;
  68. wait_queue_head_t wait;
  69. spinlock_t ctx_lock;
  70. atomic_t reqs_active;
  71. struct list_head active_reqs; /* used for cancellation */
  72. /*
  73. * This is what userspace passed to io_setup(), it's not used for
  74. * anything but counting against the global max_reqs quota.
  75. *
  76. * The real limit is ring->nr - 1, which will be larger (see
  77. * aio_setup_ring())
  78. */
  79. unsigned max_reqs;
  80. struct aio_ring_info ring_info;
  81. spinlock_t completion_lock;
  82. struct rcu_head rcu_head;
  83. struct work_struct rcu_work;
  84. };
  85. /*------ sysctl variables----*/
  86. static DEFINE_SPINLOCK(aio_nr_lock);
  87. unsigned long aio_nr; /* current system wide number of aio requests */
  88. unsigned long aio_max_nr = 0x10000; /* system wide maximum number of aio requests */
  89. /*----end sysctl variables---*/
  90. static struct kmem_cache *kiocb_cachep;
  91. static struct kmem_cache *kioctx_cachep;
  92. /* aio_setup
  93. * Creates the slab caches used by the aio routines, panic on
  94. * failure as this is done early during the boot sequence.
  95. */
  96. static int __init aio_setup(void)
  97. {
  98. kiocb_cachep = KMEM_CACHE(kiocb, SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  99. kioctx_cachep = KMEM_CACHE(kioctx,SLAB_HWCACHE_ALIGN|SLAB_PANIC);
  100. pr_debug("sizeof(struct page) = %zu\n", sizeof(struct page));
  101. return 0;
  102. }
  103. __initcall(aio_setup);
  104. static void aio_free_ring(struct kioctx *ctx)
  105. {
  106. struct aio_ring_info *info = &ctx->ring_info;
  107. long i;
  108. for (i=0; i<info->nr_pages; i++)
  109. put_page(info->ring_pages[i]);
  110. if (info->mmap_size) {
  111. vm_munmap(info->mmap_base, info->mmap_size);
  112. }
  113. if (info->ring_pages && info->ring_pages != info->internal_pages)
  114. kfree(info->ring_pages);
  115. info->ring_pages = NULL;
  116. info->nr = 0;
  117. }
  118. static int aio_setup_ring(struct kioctx *ctx)
  119. {
  120. struct aio_ring *ring;
  121. struct aio_ring_info *info = &ctx->ring_info;
  122. unsigned nr_events = ctx->max_reqs;
  123. struct mm_struct *mm = current->mm;
  124. unsigned long size, populate;
  125. int nr_pages;
  126. /* Compensate for the ring buffer's head/tail overlap entry */
  127. nr_events += 2; /* 1 is required, 2 for good luck */
  128. size = sizeof(struct aio_ring);
  129. size += sizeof(struct io_event) * nr_events;
  130. nr_pages = (size + PAGE_SIZE-1) >> PAGE_SHIFT;
  131. if (nr_pages < 0)
  132. return -EINVAL;
  133. nr_events = (PAGE_SIZE * nr_pages - sizeof(struct aio_ring)) / sizeof(struct io_event);
  134. info->nr = 0;
  135. info->ring_pages = info->internal_pages;
  136. if (nr_pages > AIO_RING_PAGES) {
  137. info->ring_pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL);
  138. if (!info->ring_pages)
  139. return -ENOMEM;
  140. }
  141. info->mmap_size = nr_pages * PAGE_SIZE;
  142. pr_debug("attempting mmap of %lu bytes\n", info->mmap_size);
  143. down_write(&mm->mmap_sem);
  144. info->mmap_base = do_mmap_pgoff(NULL, 0, info->mmap_size,
  145. PROT_READ|PROT_WRITE,
  146. MAP_ANONYMOUS|MAP_PRIVATE, 0,
  147. &populate);
  148. if (IS_ERR((void *)info->mmap_base)) {
  149. up_write(&mm->mmap_sem);
  150. info->mmap_size = 0;
  151. aio_free_ring(ctx);
  152. return -EAGAIN;
  153. }
  154. pr_debug("mmap address: 0x%08lx\n", info->mmap_base);
  155. info->nr_pages = get_user_pages(current, mm, info->mmap_base, nr_pages,
  156. 1, 0, info->ring_pages, NULL);
  157. up_write(&mm->mmap_sem);
  158. if (unlikely(info->nr_pages != nr_pages)) {
  159. aio_free_ring(ctx);
  160. return -EAGAIN;
  161. }
  162. if (populate)
  163. mm_populate(info->mmap_base, populate);
  164. ctx->user_id = info->mmap_base;
  165. info->nr = nr_events; /* trusted copy */
  166. ring = kmap_atomic(info->ring_pages[0]);
  167. ring->nr = nr_events; /* user copy */
  168. ring->id = ctx->user_id;
  169. ring->head = ring->tail = 0;
  170. ring->magic = AIO_RING_MAGIC;
  171. ring->compat_features = AIO_RING_COMPAT_FEATURES;
  172. ring->incompat_features = AIO_RING_INCOMPAT_FEATURES;
  173. ring->header_length = sizeof(struct aio_ring);
  174. kunmap_atomic(ring);
  175. flush_dcache_page(info->ring_pages[0]);
  176. return 0;
  177. }
  178. #define AIO_EVENTS_PER_PAGE (PAGE_SIZE / sizeof(struct io_event))
  179. #define AIO_EVENTS_FIRST_PAGE ((PAGE_SIZE - sizeof(struct aio_ring)) / sizeof(struct io_event))
  180. #define AIO_EVENTS_OFFSET (AIO_EVENTS_PER_PAGE - AIO_EVENTS_FIRST_PAGE)
  181. void kiocb_set_cancel_fn(struct kiocb *req, kiocb_cancel_fn *cancel)
  182. {
  183. struct kioctx *ctx = req->ki_ctx;
  184. unsigned long flags;
  185. spin_lock_irqsave(&ctx->ctx_lock, flags);
  186. if (!req->ki_list.next)
  187. list_add(&req->ki_list, &ctx->active_reqs);
  188. req->ki_cancel = cancel;
  189. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  190. }
  191. EXPORT_SYMBOL(kiocb_set_cancel_fn);
  192. static int kiocb_cancel(struct kioctx *ctx, struct kiocb *kiocb,
  193. struct io_event *res)
  194. {
  195. kiocb_cancel_fn *old, *cancel;
  196. int ret = -EINVAL;
  197. /*
  198. * Don't want to set kiocb->ki_cancel = KIOCB_CANCELLED unless it
  199. * actually has a cancel function, hence the cmpxchg()
  200. */
  201. cancel = ACCESS_ONCE(kiocb->ki_cancel);
  202. do {
  203. if (!cancel || cancel == KIOCB_CANCELLED)
  204. return ret;
  205. old = cancel;
  206. cancel = cmpxchg(&kiocb->ki_cancel, old, KIOCB_CANCELLED);
  207. } while (cancel != old);
  208. atomic_inc(&kiocb->ki_users);
  209. spin_unlock_irq(&ctx->ctx_lock);
  210. memset(res, 0, sizeof(*res));
  211. res->obj = (u64)(unsigned long)kiocb->ki_obj.user;
  212. res->data = kiocb->ki_user_data;
  213. ret = cancel(kiocb, res);
  214. spin_lock_irq(&ctx->ctx_lock);
  215. return ret;
  216. }
  217. static void free_ioctx_rcu(struct rcu_head *head)
  218. {
  219. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  220. kmem_cache_free(kioctx_cachep, ctx);
  221. }
  222. /*
  223. * When this function runs, the kioctx has been removed from the "hash table"
  224. * and ctx->users has dropped to 0, so we know no more kiocbs can be submitted -
  225. * now it's safe to cancel any that need to be.
  226. */
  227. static void free_ioctx(struct kioctx *ctx)
  228. {
  229. struct aio_ring_info *info = &ctx->ring_info;
  230. struct aio_ring *ring;
  231. struct io_event res;
  232. struct kiocb *req;
  233. unsigned head, avail;
  234. spin_lock_irq(&ctx->ctx_lock);
  235. while (!list_empty(&ctx->active_reqs)) {
  236. req = list_first_entry(&ctx->active_reqs,
  237. struct kiocb, ki_list);
  238. list_del_init(&req->ki_list);
  239. kiocb_cancel(ctx, req, &res);
  240. }
  241. spin_unlock_irq(&ctx->ctx_lock);
  242. ring = kmap_atomic(info->ring_pages[0]);
  243. head = ring->head;
  244. kunmap_atomic(ring);
  245. while (atomic_read(&ctx->reqs_active) > 0) {
  246. wait_event(ctx->wait, head != info->tail);
  247. avail = (head <= info->tail ? info->tail : info->nr) - head;
  248. atomic_sub(avail, &ctx->reqs_active);
  249. head += avail;
  250. head %= info->nr;
  251. }
  252. WARN_ON(atomic_read(&ctx->reqs_active) < 0);
  253. aio_free_ring(ctx);
  254. spin_lock(&aio_nr_lock);
  255. BUG_ON(aio_nr - ctx->max_reqs > aio_nr);
  256. aio_nr -= ctx->max_reqs;
  257. spin_unlock(&aio_nr_lock);
  258. pr_debug("freeing %p\n", ctx);
  259. /*
  260. * Here the call_rcu() is between the wait_event() for reqs_active to
  261. * hit 0, and freeing the ioctx.
  262. *
  263. * aio_complete() decrements reqs_active, but it has to touch the ioctx
  264. * after to issue a wakeup so we use rcu.
  265. */
  266. call_rcu(&ctx->rcu_head, free_ioctx_rcu);
  267. }
  268. static void put_ioctx(struct kioctx *ctx)
  269. {
  270. if (unlikely(atomic_dec_and_test(&ctx->users)))
  271. free_ioctx(ctx);
  272. }
  273. /* ioctx_alloc
  274. * Allocates and initializes an ioctx. Returns an ERR_PTR if it failed.
  275. */
  276. static struct kioctx *ioctx_alloc(unsigned nr_events)
  277. {
  278. struct mm_struct *mm = current->mm;
  279. struct kioctx *ctx;
  280. int err = -ENOMEM;
  281. /* Prevent overflows */
  282. if ((nr_events > (0x10000000U / sizeof(struct io_event))) ||
  283. (nr_events > (0x10000000U / sizeof(struct kiocb)))) {
  284. pr_debug("ENOMEM: nr_events too high\n");
  285. return ERR_PTR(-EINVAL);
  286. }
  287. if (!nr_events || (unsigned long)nr_events > aio_max_nr)
  288. return ERR_PTR(-EAGAIN);
  289. ctx = kmem_cache_zalloc(kioctx_cachep, GFP_KERNEL);
  290. if (!ctx)
  291. return ERR_PTR(-ENOMEM);
  292. ctx->max_reqs = nr_events;
  293. atomic_set(&ctx->users, 2);
  294. atomic_set(&ctx->dead, 0);
  295. spin_lock_init(&ctx->ctx_lock);
  296. spin_lock_init(&ctx->completion_lock);
  297. mutex_init(&ctx->ring_info.ring_lock);
  298. init_waitqueue_head(&ctx->wait);
  299. INIT_LIST_HEAD(&ctx->active_reqs);
  300. if (aio_setup_ring(ctx) < 0)
  301. goto out_freectx;
  302. /* limit the number of system wide aios */
  303. spin_lock(&aio_nr_lock);
  304. if (aio_nr + nr_events > aio_max_nr ||
  305. aio_nr + nr_events < aio_nr) {
  306. spin_unlock(&aio_nr_lock);
  307. goto out_cleanup;
  308. }
  309. aio_nr += ctx->max_reqs;
  310. spin_unlock(&aio_nr_lock);
  311. /* now link into global list. */
  312. spin_lock(&mm->ioctx_lock);
  313. hlist_add_head_rcu(&ctx->list, &mm->ioctx_list);
  314. spin_unlock(&mm->ioctx_lock);
  315. pr_debug("allocated ioctx %p[%ld]: mm=%p mask=0x%x\n",
  316. ctx, ctx->user_id, mm, ctx->ring_info.nr);
  317. return ctx;
  318. out_cleanup:
  319. err = -EAGAIN;
  320. aio_free_ring(ctx);
  321. out_freectx:
  322. kmem_cache_free(kioctx_cachep, ctx);
  323. pr_debug("error allocating ioctx %d\n", err);
  324. return ERR_PTR(err);
  325. }
  326. static void kill_ioctx_work(struct work_struct *work)
  327. {
  328. struct kioctx *ctx = container_of(work, struct kioctx, rcu_work);
  329. wake_up_all(&ctx->wait);
  330. put_ioctx(ctx);
  331. }
  332. static void kill_ioctx_rcu(struct rcu_head *head)
  333. {
  334. struct kioctx *ctx = container_of(head, struct kioctx, rcu_head);
  335. INIT_WORK(&ctx->rcu_work, kill_ioctx_work);
  336. schedule_work(&ctx->rcu_work);
  337. }
  338. /* kill_ioctx
  339. * Cancels all outstanding aio requests on an aio context. Used
  340. * when the processes owning a context have all exited to encourage
  341. * the rapid destruction of the kioctx.
  342. */
  343. static void kill_ioctx(struct kioctx *ctx)
  344. {
  345. if (!atomic_xchg(&ctx->dead, 1)) {
  346. hlist_del_rcu(&ctx->list);
  347. /* Between hlist_del_rcu() and dropping the initial ref */
  348. synchronize_rcu();
  349. /*
  350. * We can't punt to workqueue here because put_ioctx() ->
  351. * free_ioctx() will unmap the ringbuffer, and that has to be
  352. * done in the original process's context. kill_ioctx_rcu/work()
  353. * exist for exit_aio(), as in that path free_ioctx() won't do
  354. * the unmap.
  355. */
  356. kill_ioctx_work(&ctx->rcu_work);
  357. }
  358. }
  359. /* wait_on_sync_kiocb:
  360. * Waits on the given sync kiocb to complete.
  361. */
  362. ssize_t wait_on_sync_kiocb(struct kiocb *iocb)
  363. {
  364. while (atomic_read(&iocb->ki_users)) {
  365. set_current_state(TASK_UNINTERRUPTIBLE);
  366. if (!atomic_read(&iocb->ki_users))
  367. break;
  368. io_schedule();
  369. }
  370. __set_current_state(TASK_RUNNING);
  371. return iocb->ki_user_data;
  372. }
  373. EXPORT_SYMBOL(wait_on_sync_kiocb);
  374. /*
  375. * exit_aio: called when the last user of mm goes away. At this point, there is
  376. * no way for any new requests to be submited or any of the io_* syscalls to be
  377. * called on the context.
  378. *
  379. * There may be outstanding kiocbs, but free_ioctx() will explicitly wait on
  380. * them.
  381. */
  382. void exit_aio(struct mm_struct *mm)
  383. {
  384. struct kioctx *ctx;
  385. struct hlist_node *n;
  386. hlist_for_each_entry_safe(ctx, n, &mm->ioctx_list, list) {
  387. if (1 != atomic_read(&ctx->users))
  388. printk(KERN_DEBUG
  389. "exit_aio:ioctx still alive: %d %d %d\n",
  390. atomic_read(&ctx->users),
  391. atomic_read(&ctx->dead),
  392. atomic_read(&ctx->reqs_active));
  393. /*
  394. * We don't need to bother with munmap() here -
  395. * exit_mmap(mm) is coming and it'll unmap everything.
  396. * Since aio_free_ring() uses non-zero ->mmap_size
  397. * as indicator that it needs to unmap the area,
  398. * just set it to 0; aio_free_ring() is the only
  399. * place that uses ->mmap_size, so it's safe.
  400. */
  401. ctx->ring_info.mmap_size = 0;
  402. if (!atomic_xchg(&ctx->dead, 1)) {
  403. hlist_del_rcu(&ctx->list);
  404. call_rcu(&ctx->rcu_head, kill_ioctx_rcu);
  405. }
  406. }
  407. }
  408. /* aio_get_req
  409. * Allocate a slot for an aio request. Increments the ki_users count
  410. * of the kioctx so that the kioctx stays around until all requests are
  411. * complete. Returns NULL if no requests are free.
  412. *
  413. * Returns with kiocb->ki_users set to 2. The io submit code path holds
  414. * an extra reference while submitting the i/o.
  415. * This prevents races between the aio code path referencing the
  416. * req (after submitting it) and aio_complete() freeing the req.
  417. */
  418. static struct kiocb *__aio_get_req(struct kioctx *ctx)
  419. {
  420. struct kiocb *req = NULL;
  421. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
  422. if (unlikely(!req))
  423. return NULL;
  424. atomic_set(&req->ki_users, 2);
  425. req->ki_ctx = ctx;
  426. return req;
  427. }
  428. /*
  429. * struct kiocb's are allocated in batches to reduce the number of
  430. * times the ctx lock is acquired and released.
  431. */
  432. #define KIOCB_BATCH_SIZE 32L
  433. struct kiocb_batch {
  434. struct list_head head;
  435. long count; /* number of requests left to allocate */
  436. };
  437. static void kiocb_batch_init(struct kiocb_batch *batch, long total)
  438. {
  439. INIT_LIST_HEAD(&batch->head);
  440. batch->count = total;
  441. }
  442. static void kiocb_batch_free(struct kioctx *ctx, struct kiocb_batch *batch)
  443. {
  444. struct kiocb *req, *n;
  445. if (list_empty(&batch->head))
  446. return;
  447. spin_lock_irq(&ctx->ctx_lock);
  448. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  449. list_del(&req->ki_batch);
  450. kmem_cache_free(kiocb_cachep, req);
  451. atomic_dec(&ctx->reqs_active);
  452. }
  453. spin_unlock_irq(&ctx->ctx_lock);
  454. }
  455. /*
  456. * Allocate a batch of kiocbs. This avoids taking and dropping the
  457. * context lock a lot during setup.
  458. */
  459. static int kiocb_batch_refill(struct kioctx *ctx, struct kiocb_batch *batch)
  460. {
  461. unsigned short allocated, to_alloc;
  462. long avail;
  463. struct kiocb *req, *n;
  464. to_alloc = min(batch->count, KIOCB_BATCH_SIZE);
  465. for (allocated = 0; allocated < to_alloc; allocated++) {
  466. req = __aio_get_req(ctx);
  467. if (!req)
  468. /* allocation failed, go with what we've got */
  469. break;
  470. list_add(&req->ki_batch, &batch->head);
  471. }
  472. if (allocated == 0)
  473. goto out;
  474. spin_lock_irq(&ctx->ctx_lock);
  475. avail = ctx->ring_info.nr - atomic_read(&ctx->reqs_active) - 1;
  476. BUG_ON(avail < 0);
  477. if (avail < allocated) {
  478. /* Trim back the number of requests. */
  479. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  480. list_del(&req->ki_batch);
  481. kmem_cache_free(kiocb_cachep, req);
  482. if (--allocated <= avail)
  483. break;
  484. }
  485. }
  486. batch->count -= allocated;
  487. atomic_add(allocated, &ctx->reqs_active);
  488. spin_unlock_irq(&ctx->ctx_lock);
  489. out:
  490. return allocated;
  491. }
  492. static inline struct kiocb *aio_get_req(struct kioctx *ctx,
  493. struct kiocb_batch *batch)
  494. {
  495. struct kiocb *req;
  496. if (list_empty(&batch->head))
  497. if (kiocb_batch_refill(ctx, batch) == 0)
  498. return NULL;
  499. req = list_first_entry(&batch->head, struct kiocb, ki_batch);
  500. list_del(&req->ki_batch);
  501. return req;
  502. }
  503. static void kiocb_free(struct kiocb *req)
  504. {
  505. if (req->ki_filp)
  506. fput(req->ki_filp);
  507. if (req->ki_eventfd != NULL)
  508. eventfd_ctx_put(req->ki_eventfd);
  509. if (req->ki_dtor)
  510. req->ki_dtor(req);
  511. if (req->ki_iovec != &req->ki_inline_vec)
  512. kfree(req->ki_iovec);
  513. kmem_cache_free(kiocb_cachep, req);
  514. }
  515. void aio_put_req(struct kiocb *req)
  516. {
  517. if (atomic_dec_and_test(&req->ki_users))
  518. kiocb_free(req);
  519. }
  520. EXPORT_SYMBOL(aio_put_req);
  521. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  522. {
  523. struct mm_struct *mm = current->mm;
  524. struct kioctx *ctx, *ret = NULL;
  525. rcu_read_lock();
  526. hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
  527. if (ctx->user_id == ctx_id) {
  528. atomic_inc(&ctx->users);
  529. ret = ctx;
  530. break;
  531. }
  532. }
  533. rcu_read_unlock();
  534. return ret;
  535. }
  536. /* aio_complete
  537. * Called when the io request on the given iocb is complete.
  538. */
  539. void aio_complete(struct kiocb *iocb, long res, long res2)
  540. {
  541. struct kioctx *ctx = iocb->ki_ctx;
  542. struct aio_ring_info *info;
  543. struct aio_ring *ring;
  544. struct io_event *ev_page, *event;
  545. unsigned long flags;
  546. unsigned tail, pos;
  547. /*
  548. * Special case handling for sync iocbs:
  549. * - events go directly into the iocb for fast handling
  550. * - the sync task with the iocb in its stack holds the single iocb
  551. * ref, no other paths have a way to get another ref
  552. * - the sync task helpfully left a reference to itself in the iocb
  553. */
  554. if (is_sync_kiocb(iocb)) {
  555. BUG_ON(atomic_read(&iocb->ki_users) != 1);
  556. iocb->ki_user_data = res;
  557. atomic_set(&iocb->ki_users, 0);
  558. wake_up_process(iocb->ki_obj.tsk);
  559. return;
  560. }
  561. info = &ctx->ring_info;
  562. /*
  563. * Take rcu_read_lock() in case the kioctx is being destroyed, as we
  564. * need to issue a wakeup after decrementing reqs_active.
  565. */
  566. rcu_read_lock();
  567. if (iocb->ki_list.next) {
  568. unsigned long flags;
  569. spin_lock_irqsave(&ctx->ctx_lock, flags);
  570. list_del(&iocb->ki_list);
  571. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  572. }
  573. /*
  574. * cancelled requests don't get events, userland was given one
  575. * when the event got cancelled.
  576. */
  577. if (unlikely(xchg(&iocb->ki_cancel,
  578. KIOCB_CANCELLED) == KIOCB_CANCELLED)) {
  579. atomic_dec(&ctx->reqs_active);
  580. /* Still need the wake_up in case free_ioctx is waiting */
  581. goto put_rq;
  582. }
  583. /*
  584. * Add a completion event to the ring buffer. Must be done holding
  585. * ctx->ctx_lock to prevent other code from messing with the tail
  586. * pointer since we might be called from irq context.
  587. */
  588. spin_lock_irqsave(&ctx->completion_lock, flags);
  589. tail = info->tail;
  590. pos = tail + AIO_EVENTS_OFFSET;
  591. if (++tail >= info->nr)
  592. tail = 0;
  593. ev_page = kmap_atomic(info->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  594. event = ev_page + pos % AIO_EVENTS_PER_PAGE;
  595. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  596. event->data = iocb->ki_user_data;
  597. event->res = res;
  598. event->res2 = res2;
  599. kunmap_atomic(ev_page);
  600. flush_dcache_page(info->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  601. pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
  602. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  603. res, res2);
  604. /* after flagging the request as done, we
  605. * must never even look at it again
  606. */
  607. smp_wmb(); /* make event visible before updating tail */
  608. info->tail = tail;
  609. ring = kmap_atomic(info->ring_pages[0]);
  610. ring->tail = tail;
  611. kunmap_atomic(ring);
  612. flush_dcache_page(info->ring_pages[0]);
  613. spin_unlock_irqrestore(&ctx->completion_lock, flags);
  614. pr_debug("added to ring %p at [%u]\n", iocb, tail);
  615. /*
  616. * Check if the user asked us to deliver the result through an
  617. * eventfd. The eventfd_signal() function is safe to be called
  618. * from IRQ context.
  619. */
  620. if (iocb->ki_eventfd != NULL)
  621. eventfd_signal(iocb->ki_eventfd, 1);
  622. put_rq:
  623. /* everything turned out well, dispose of the aiocb. */
  624. aio_put_req(iocb);
  625. /*
  626. * We have to order our ring_info tail store above and test
  627. * of the wait list below outside the wait lock. This is
  628. * like in wake_up_bit() where clearing a bit has to be
  629. * ordered with the unlocked test.
  630. */
  631. smp_mb();
  632. if (waitqueue_active(&ctx->wait))
  633. wake_up(&ctx->wait);
  634. rcu_read_unlock();
  635. }
  636. EXPORT_SYMBOL(aio_complete);
  637. /* aio_read_events
  638. * Pull an event off of the ioctx's event ring. Returns the number of
  639. * events fetched
  640. */
  641. static long aio_read_events_ring(struct kioctx *ctx,
  642. struct io_event __user *event, long nr)
  643. {
  644. struct aio_ring_info *info = &ctx->ring_info;
  645. struct aio_ring *ring;
  646. unsigned head, pos;
  647. long ret = 0;
  648. int copy_ret;
  649. mutex_lock(&info->ring_lock);
  650. ring = kmap_atomic(info->ring_pages[0]);
  651. head = ring->head;
  652. kunmap_atomic(ring);
  653. pr_debug("h%u t%u m%u\n", head, info->tail, info->nr);
  654. if (head == info->tail)
  655. goto out;
  656. while (ret < nr) {
  657. long avail;
  658. struct io_event *ev;
  659. struct page *page;
  660. avail = (head <= info->tail ? info->tail : info->nr) - head;
  661. if (head == info->tail)
  662. break;
  663. avail = min(avail, nr - ret);
  664. avail = min_t(long, avail, AIO_EVENTS_PER_PAGE -
  665. ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
  666. pos = head + AIO_EVENTS_OFFSET;
  667. page = info->ring_pages[pos / AIO_EVENTS_PER_PAGE];
  668. pos %= AIO_EVENTS_PER_PAGE;
  669. ev = kmap(page);
  670. copy_ret = copy_to_user(event + ret, ev + pos,
  671. sizeof(*ev) * avail);
  672. kunmap(page);
  673. if (unlikely(copy_ret)) {
  674. ret = -EFAULT;
  675. goto out;
  676. }
  677. ret += avail;
  678. head += avail;
  679. head %= info->nr;
  680. }
  681. ring = kmap_atomic(info->ring_pages[0]);
  682. ring->head = head;
  683. kunmap_atomic(ring);
  684. flush_dcache_page(info->ring_pages[0]);
  685. pr_debug("%li h%u t%u\n", ret, head, info->tail);
  686. atomic_sub(ret, &ctx->reqs_active);
  687. out:
  688. mutex_unlock(&info->ring_lock);
  689. return ret;
  690. }
  691. static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
  692. struct io_event __user *event, long *i)
  693. {
  694. long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
  695. if (ret > 0)
  696. *i += ret;
  697. if (unlikely(atomic_read(&ctx->dead)))
  698. ret = -EINVAL;
  699. if (!*i)
  700. *i = ret;
  701. return ret < 0 || *i >= min_nr;
  702. }
  703. static long read_events(struct kioctx *ctx, long min_nr, long nr,
  704. struct io_event __user *event,
  705. struct timespec __user *timeout)
  706. {
  707. ktime_t until = { .tv64 = KTIME_MAX };
  708. long ret = 0;
  709. if (timeout) {
  710. struct timespec ts;
  711. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  712. return -EFAULT;
  713. until = timespec_to_ktime(ts);
  714. }
  715. /*
  716. * Note that aio_read_events() is being called as the conditional - i.e.
  717. * we're calling it after prepare_to_wait() has set task state to
  718. * TASK_INTERRUPTIBLE.
  719. *
  720. * But aio_read_events() can block, and if it blocks it's going to flip
  721. * the task state back to TASK_RUNNING.
  722. *
  723. * This should be ok, provided it doesn't flip the state back to
  724. * TASK_RUNNING and return 0 too much - that causes us to spin. That
  725. * will only happen if the mutex_lock() call blocks, and we then find
  726. * the ringbuffer empty. So in practice we should be ok, but it's
  727. * something to be aware of when touching this code.
  728. */
  729. wait_event_interruptible_hrtimeout(ctx->wait,
  730. aio_read_events(ctx, min_nr, nr, event, &ret), until);
  731. if (!ret && signal_pending(current))
  732. ret = -EINTR;
  733. return ret;
  734. }
  735. /* sys_io_setup:
  736. * Create an aio_context capable of receiving at least nr_events.
  737. * ctxp must not point to an aio_context that already exists, and
  738. * must be initialized to 0 prior to the call. On successful
  739. * creation of the aio_context, *ctxp is filled in with the resulting
  740. * handle. May fail with -EINVAL if *ctxp is not initialized,
  741. * if the specified nr_events exceeds internal limits. May fail
  742. * with -EAGAIN if the specified nr_events exceeds the user's limit
  743. * of available events. May fail with -ENOMEM if insufficient kernel
  744. * resources are available. May fail with -EFAULT if an invalid
  745. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  746. * implemented.
  747. */
  748. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  749. {
  750. struct kioctx *ioctx = NULL;
  751. unsigned long ctx;
  752. long ret;
  753. ret = get_user(ctx, ctxp);
  754. if (unlikely(ret))
  755. goto out;
  756. ret = -EINVAL;
  757. if (unlikely(ctx || nr_events == 0)) {
  758. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  759. ctx, nr_events);
  760. goto out;
  761. }
  762. ioctx = ioctx_alloc(nr_events);
  763. ret = PTR_ERR(ioctx);
  764. if (!IS_ERR(ioctx)) {
  765. ret = put_user(ioctx->user_id, ctxp);
  766. if (ret)
  767. kill_ioctx(ioctx);
  768. put_ioctx(ioctx);
  769. }
  770. out:
  771. return ret;
  772. }
  773. /* sys_io_destroy:
  774. * Destroy the aio_context specified. May cancel any outstanding
  775. * AIOs and block on completion. Will fail with -ENOSYS if not
  776. * implemented. May fail with -EINVAL if the context pointed to
  777. * is invalid.
  778. */
  779. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  780. {
  781. struct kioctx *ioctx = lookup_ioctx(ctx);
  782. if (likely(NULL != ioctx)) {
  783. kill_ioctx(ioctx);
  784. put_ioctx(ioctx);
  785. return 0;
  786. }
  787. pr_debug("EINVAL: io_destroy: invalid context id\n");
  788. return -EINVAL;
  789. }
  790. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  791. {
  792. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  793. BUG_ON(ret <= 0);
  794. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  795. ssize_t this = min((ssize_t)iov->iov_len, ret);
  796. iov->iov_base += this;
  797. iov->iov_len -= this;
  798. iocb->ki_left -= this;
  799. ret -= this;
  800. if (iov->iov_len == 0) {
  801. iocb->ki_cur_seg++;
  802. iov++;
  803. }
  804. }
  805. /* the caller should not have done more io than what fit in
  806. * the remaining iovecs */
  807. BUG_ON(ret > 0 && iocb->ki_left == 0);
  808. }
  809. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  810. {
  811. struct file *file = iocb->ki_filp;
  812. struct address_space *mapping = file->f_mapping;
  813. struct inode *inode = mapping->host;
  814. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  815. unsigned long, loff_t);
  816. ssize_t ret = 0;
  817. unsigned short opcode;
  818. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  819. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  820. rw_op = file->f_op->aio_read;
  821. opcode = IOCB_CMD_PREADV;
  822. } else {
  823. rw_op = file->f_op->aio_write;
  824. opcode = IOCB_CMD_PWRITEV;
  825. }
  826. /* This matches the pread()/pwrite() logic */
  827. if (iocb->ki_pos < 0)
  828. return -EINVAL;
  829. if (opcode == IOCB_CMD_PWRITEV)
  830. file_start_write(file);
  831. do {
  832. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  833. iocb->ki_nr_segs - iocb->ki_cur_seg,
  834. iocb->ki_pos);
  835. if (ret > 0)
  836. aio_advance_iovec(iocb, ret);
  837. /* retry all partial writes. retry partial reads as long as its a
  838. * regular file. */
  839. } while (ret > 0 && iocb->ki_left > 0 &&
  840. (opcode == IOCB_CMD_PWRITEV ||
  841. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  842. if (opcode == IOCB_CMD_PWRITEV)
  843. file_end_write(file);
  844. /* This means we must have transferred all that we could */
  845. /* No need to retry anymore */
  846. if ((ret == 0) || (iocb->ki_left == 0))
  847. ret = iocb->ki_nbytes - iocb->ki_left;
  848. /* If we managed to write some out we return that, rather than
  849. * the eventual error. */
  850. if (opcode == IOCB_CMD_PWRITEV
  851. && ret < 0 && ret != -EIOCBQUEUED
  852. && iocb->ki_nbytes - iocb->ki_left)
  853. ret = iocb->ki_nbytes - iocb->ki_left;
  854. return ret;
  855. }
  856. static ssize_t aio_fdsync(struct kiocb *iocb)
  857. {
  858. struct file *file = iocb->ki_filp;
  859. ssize_t ret = -EINVAL;
  860. if (file->f_op->aio_fsync)
  861. ret = file->f_op->aio_fsync(iocb, 1);
  862. return ret;
  863. }
  864. static ssize_t aio_fsync(struct kiocb *iocb)
  865. {
  866. struct file *file = iocb->ki_filp;
  867. ssize_t ret = -EINVAL;
  868. if (file->f_op->aio_fsync)
  869. ret = file->f_op->aio_fsync(iocb, 0);
  870. return ret;
  871. }
  872. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
  873. {
  874. ssize_t ret;
  875. #ifdef CONFIG_COMPAT
  876. if (compat)
  877. ret = compat_rw_copy_check_uvector(type,
  878. (struct compat_iovec __user *)kiocb->ki_buf,
  879. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  880. &kiocb->ki_iovec);
  881. else
  882. #endif
  883. ret = rw_copy_check_uvector(type,
  884. (struct iovec __user *)kiocb->ki_buf,
  885. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  886. &kiocb->ki_iovec);
  887. if (ret < 0)
  888. goto out;
  889. ret = rw_verify_area(type, kiocb->ki_filp, &kiocb->ki_pos, ret);
  890. if (ret < 0)
  891. goto out;
  892. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  893. kiocb->ki_cur_seg = 0;
  894. /* ki_nbytes/left now reflect bytes instead of segs */
  895. kiocb->ki_nbytes = ret;
  896. kiocb->ki_left = ret;
  897. ret = 0;
  898. out:
  899. return ret;
  900. }
  901. static ssize_t aio_setup_single_vector(int type, struct file * file, struct kiocb *kiocb)
  902. {
  903. int bytes;
  904. bytes = rw_verify_area(type, file, &kiocb->ki_pos, kiocb->ki_left);
  905. if (bytes < 0)
  906. return bytes;
  907. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  908. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  909. kiocb->ki_iovec->iov_len = bytes;
  910. kiocb->ki_nr_segs = 1;
  911. kiocb->ki_cur_seg = 0;
  912. return 0;
  913. }
  914. /*
  915. * aio_setup_iocb:
  916. * Performs the initial checks and aio retry method
  917. * setup for the kiocb at the time of io submission.
  918. */
  919. static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
  920. {
  921. struct file *file = kiocb->ki_filp;
  922. ssize_t ret = 0;
  923. switch (kiocb->ki_opcode) {
  924. case IOCB_CMD_PREAD:
  925. ret = -EBADF;
  926. if (unlikely(!(file->f_mode & FMODE_READ)))
  927. break;
  928. ret = -EFAULT;
  929. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  930. kiocb->ki_left)))
  931. break;
  932. ret = aio_setup_single_vector(READ, file, kiocb);
  933. if (ret)
  934. break;
  935. ret = -EINVAL;
  936. if (file->f_op->aio_read)
  937. kiocb->ki_retry = aio_rw_vect_retry;
  938. break;
  939. case IOCB_CMD_PWRITE:
  940. ret = -EBADF;
  941. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  942. break;
  943. ret = -EFAULT;
  944. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  945. kiocb->ki_left)))
  946. break;
  947. ret = aio_setup_single_vector(WRITE, file, kiocb);
  948. if (ret)
  949. break;
  950. ret = -EINVAL;
  951. if (file->f_op->aio_write)
  952. kiocb->ki_retry = aio_rw_vect_retry;
  953. break;
  954. case IOCB_CMD_PREADV:
  955. ret = -EBADF;
  956. if (unlikely(!(file->f_mode & FMODE_READ)))
  957. break;
  958. ret = aio_setup_vectored_rw(READ, kiocb, compat);
  959. if (ret)
  960. break;
  961. ret = -EINVAL;
  962. if (file->f_op->aio_read)
  963. kiocb->ki_retry = aio_rw_vect_retry;
  964. break;
  965. case IOCB_CMD_PWRITEV:
  966. ret = -EBADF;
  967. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  968. break;
  969. ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
  970. if (ret)
  971. break;
  972. ret = -EINVAL;
  973. if (file->f_op->aio_write)
  974. kiocb->ki_retry = aio_rw_vect_retry;
  975. break;
  976. case IOCB_CMD_FDSYNC:
  977. ret = -EINVAL;
  978. if (file->f_op->aio_fsync)
  979. kiocb->ki_retry = aio_fdsync;
  980. break;
  981. case IOCB_CMD_FSYNC:
  982. ret = -EINVAL;
  983. if (file->f_op->aio_fsync)
  984. kiocb->ki_retry = aio_fsync;
  985. break;
  986. default:
  987. pr_debug("EINVAL: no operation provided\n");
  988. ret = -EINVAL;
  989. }
  990. if (!kiocb->ki_retry)
  991. return ret;
  992. return 0;
  993. }
  994. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  995. struct iocb *iocb, struct kiocb_batch *batch,
  996. bool compat)
  997. {
  998. struct kiocb *req;
  999. ssize_t ret;
  1000. /* enforce forwards compatibility on users */
  1001. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  1002. pr_debug("EINVAL: reserve field set\n");
  1003. return -EINVAL;
  1004. }
  1005. /* prevent overflows */
  1006. if (unlikely(
  1007. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  1008. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  1009. ((ssize_t)iocb->aio_nbytes < 0)
  1010. )) {
  1011. pr_debug("EINVAL: io_submit: overflow check\n");
  1012. return -EINVAL;
  1013. }
  1014. req = aio_get_req(ctx, batch); /* returns with 2 references to req */
  1015. if (unlikely(!req))
  1016. return -EAGAIN;
  1017. req->ki_filp = fget(iocb->aio_fildes);
  1018. if (unlikely(!req->ki_filp)) {
  1019. ret = -EBADF;
  1020. goto out_put_req;
  1021. }
  1022. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  1023. /*
  1024. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1025. * instance of the file* now. The file descriptor must be
  1026. * an eventfd() fd, and will be signaled for each completed
  1027. * event using the eventfd_signal() function.
  1028. */
  1029. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  1030. if (IS_ERR(req->ki_eventfd)) {
  1031. ret = PTR_ERR(req->ki_eventfd);
  1032. req->ki_eventfd = NULL;
  1033. goto out_put_req;
  1034. }
  1035. }
  1036. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1037. if (unlikely(ret)) {
  1038. pr_debug("EFAULT: aio_key\n");
  1039. goto out_put_req;
  1040. }
  1041. req->ki_obj.user = user_iocb;
  1042. req->ki_user_data = iocb->aio_data;
  1043. req->ki_pos = iocb->aio_offset;
  1044. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1045. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1046. req->ki_opcode = iocb->aio_lio_opcode;
  1047. ret = aio_setup_iocb(req, compat);
  1048. if (ret)
  1049. goto out_put_req;
  1050. ret = req->ki_retry(req);
  1051. if (ret != -EIOCBQUEUED) {
  1052. /*
  1053. * There's no easy way to restart the syscall since other AIO's
  1054. * may be already running. Just fail this IO with EINTR.
  1055. */
  1056. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  1057. ret == -ERESTARTNOHAND ||
  1058. ret == -ERESTART_RESTARTBLOCK))
  1059. ret = -EINTR;
  1060. aio_complete(req, ret, 0);
  1061. }
  1062. aio_put_req(req); /* drop extra ref to req */
  1063. return 0;
  1064. out_put_req:
  1065. atomic_dec(&ctx->reqs_active);
  1066. aio_put_req(req); /* drop extra ref to req */
  1067. aio_put_req(req); /* drop i/o ref to req */
  1068. return ret;
  1069. }
  1070. long do_io_submit(aio_context_t ctx_id, long nr,
  1071. struct iocb __user *__user *iocbpp, bool compat)
  1072. {
  1073. struct kioctx *ctx;
  1074. long ret = 0;
  1075. int i = 0;
  1076. struct blk_plug plug;
  1077. struct kiocb_batch batch;
  1078. if (unlikely(nr < 0))
  1079. return -EINVAL;
  1080. if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
  1081. nr = LONG_MAX/sizeof(*iocbpp);
  1082. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1083. return -EFAULT;
  1084. ctx = lookup_ioctx(ctx_id);
  1085. if (unlikely(!ctx)) {
  1086. pr_debug("EINVAL: invalid context id\n");
  1087. return -EINVAL;
  1088. }
  1089. kiocb_batch_init(&batch, nr);
  1090. blk_start_plug(&plug);
  1091. /*
  1092. * AKPM: should this return a partial result if some of the IOs were
  1093. * successfully submitted?
  1094. */
  1095. for (i=0; i<nr; i++) {
  1096. struct iocb __user *user_iocb;
  1097. struct iocb tmp;
  1098. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1099. ret = -EFAULT;
  1100. break;
  1101. }
  1102. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1103. ret = -EFAULT;
  1104. break;
  1105. }
  1106. ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
  1107. if (ret)
  1108. break;
  1109. }
  1110. blk_finish_plug(&plug);
  1111. kiocb_batch_free(ctx, &batch);
  1112. put_ioctx(ctx);
  1113. return i ? i : ret;
  1114. }
  1115. /* sys_io_submit:
  1116. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1117. * the number of iocbs queued. May return -EINVAL if the aio_context
  1118. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1119. * *iocbpp[0] is not properly initialized, if the operation specified
  1120. * is invalid for the file descriptor in the iocb. May fail with
  1121. * -EFAULT if any of the data structures point to invalid data. May
  1122. * fail with -EBADF if the file descriptor specified in the first
  1123. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1124. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1125. * fail with -ENOSYS if not implemented.
  1126. */
  1127. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1128. struct iocb __user * __user *, iocbpp)
  1129. {
  1130. return do_io_submit(ctx_id, nr, iocbpp, 0);
  1131. }
  1132. /* lookup_kiocb
  1133. * Finds a given iocb for cancellation.
  1134. */
  1135. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1136. u32 key)
  1137. {
  1138. struct list_head *pos;
  1139. assert_spin_locked(&ctx->ctx_lock);
  1140. /* TODO: use a hash or array, this sucks. */
  1141. list_for_each(pos, &ctx->active_reqs) {
  1142. struct kiocb *kiocb = list_kiocb(pos);
  1143. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1144. return kiocb;
  1145. }
  1146. return NULL;
  1147. }
  1148. /* sys_io_cancel:
  1149. * Attempts to cancel an iocb previously passed to io_submit. If
  1150. * the operation is successfully cancelled, the resulting event is
  1151. * copied into the memory pointed to by result without being placed
  1152. * into the completion queue and 0 is returned. May fail with
  1153. * -EFAULT if any of the data structures pointed to are invalid.
  1154. * May fail with -EINVAL if aio_context specified by ctx_id is
  1155. * invalid. May fail with -EAGAIN if the iocb specified was not
  1156. * cancelled. Will fail with -ENOSYS if not implemented.
  1157. */
  1158. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1159. struct io_event __user *, result)
  1160. {
  1161. struct io_event res;
  1162. struct kioctx *ctx;
  1163. struct kiocb *kiocb;
  1164. u32 key;
  1165. int ret;
  1166. ret = get_user(key, &iocb->aio_key);
  1167. if (unlikely(ret))
  1168. return -EFAULT;
  1169. ctx = lookup_ioctx(ctx_id);
  1170. if (unlikely(!ctx))
  1171. return -EINVAL;
  1172. spin_lock_irq(&ctx->ctx_lock);
  1173. kiocb = lookup_kiocb(ctx, iocb, key);
  1174. if (kiocb)
  1175. ret = kiocb_cancel(ctx, kiocb, &res);
  1176. else
  1177. ret = -EINVAL;
  1178. spin_unlock_irq(&ctx->ctx_lock);
  1179. if (!ret) {
  1180. /* Cancellation succeeded -- copy the result
  1181. * into the user's buffer.
  1182. */
  1183. if (copy_to_user(result, &res, sizeof(res)))
  1184. ret = -EFAULT;
  1185. }
  1186. put_ioctx(ctx);
  1187. return ret;
  1188. }
  1189. /* io_getevents:
  1190. * Attempts to read at least min_nr events and up to nr events from
  1191. * the completion queue for the aio_context specified by ctx_id. If
  1192. * it succeeds, the number of read events is returned. May fail with
  1193. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1194. * out of range, if timeout is out of range. May fail with -EFAULT
  1195. * if any of the memory specified is invalid. May return 0 or
  1196. * < min_nr if the timeout specified by timeout has elapsed
  1197. * before sufficient events are available, where timeout == NULL
  1198. * specifies an infinite timeout. Note that the timeout pointed to by
  1199. * timeout is relative and will be updated if not NULL and the
  1200. * operation blocks. Will fail with -ENOSYS if not implemented.
  1201. */
  1202. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1203. long, min_nr,
  1204. long, nr,
  1205. struct io_event __user *, events,
  1206. struct timespec __user *, timeout)
  1207. {
  1208. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1209. long ret = -EINVAL;
  1210. if (likely(ioctx)) {
  1211. if (likely(min_nr <= nr && min_nr >= 0))
  1212. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1213. put_ioctx(ioctx);
  1214. }
  1215. return ret;
  1216. }