aio.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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 inline struct kiocb *aio_get_req(struct kioctx *ctx)
  419. {
  420. struct kiocb *req;
  421. if (atomic_read(&ctx->reqs_active) >= ctx->ring_info.nr)
  422. return NULL;
  423. if (atomic_inc_return(&ctx->reqs_active) > ctx->ring_info.nr - 1)
  424. goto out_put;
  425. req = kmem_cache_alloc(kiocb_cachep, GFP_KERNEL|__GFP_ZERO);
  426. if (unlikely(!req))
  427. goto out_put;
  428. atomic_set(&req->ki_users, 2);
  429. req->ki_ctx = ctx;
  430. return req;
  431. out_put:
  432. atomic_dec(&ctx->reqs_active);
  433. return NULL;
  434. }
  435. static void kiocb_free(struct kiocb *req)
  436. {
  437. if (req->ki_filp)
  438. fput(req->ki_filp);
  439. if (req->ki_eventfd != NULL)
  440. eventfd_ctx_put(req->ki_eventfd);
  441. if (req->ki_dtor)
  442. req->ki_dtor(req);
  443. if (req->ki_iovec != &req->ki_inline_vec)
  444. kfree(req->ki_iovec);
  445. kmem_cache_free(kiocb_cachep, req);
  446. }
  447. void aio_put_req(struct kiocb *req)
  448. {
  449. if (atomic_dec_and_test(&req->ki_users))
  450. kiocb_free(req);
  451. }
  452. EXPORT_SYMBOL(aio_put_req);
  453. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  454. {
  455. struct mm_struct *mm = current->mm;
  456. struct kioctx *ctx, *ret = NULL;
  457. rcu_read_lock();
  458. hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
  459. if (ctx->user_id == ctx_id) {
  460. atomic_inc(&ctx->users);
  461. ret = ctx;
  462. break;
  463. }
  464. }
  465. rcu_read_unlock();
  466. return ret;
  467. }
  468. /* aio_complete
  469. * Called when the io request on the given iocb is complete.
  470. */
  471. void aio_complete(struct kiocb *iocb, long res, long res2)
  472. {
  473. struct kioctx *ctx = iocb->ki_ctx;
  474. struct aio_ring_info *info;
  475. struct aio_ring *ring;
  476. struct io_event *ev_page, *event;
  477. unsigned long flags;
  478. unsigned tail, pos;
  479. /*
  480. * Special case handling for sync iocbs:
  481. * - events go directly into the iocb for fast handling
  482. * - the sync task with the iocb in its stack holds the single iocb
  483. * ref, no other paths have a way to get another ref
  484. * - the sync task helpfully left a reference to itself in the iocb
  485. */
  486. if (is_sync_kiocb(iocb)) {
  487. BUG_ON(atomic_read(&iocb->ki_users) != 1);
  488. iocb->ki_user_data = res;
  489. atomic_set(&iocb->ki_users, 0);
  490. wake_up_process(iocb->ki_obj.tsk);
  491. return;
  492. }
  493. info = &ctx->ring_info;
  494. /*
  495. * Take rcu_read_lock() in case the kioctx is being destroyed, as we
  496. * need to issue a wakeup after decrementing reqs_active.
  497. */
  498. rcu_read_lock();
  499. if (iocb->ki_list.next) {
  500. unsigned long flags;
  501. spin_lock_irqsave(&ctx->ctx_lock, flags);
  502. list_del(&iocb->ki_list);
  503. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  504. }
  505. /*
  506. * cancelled requests don't get events, userland was given one
  507. * when the event got cancelled.
  508. */
  509. if (unlikely(xchg(&iocb->ki_cancel,
  510. KIOCB_CANCELLED) == KIOCB_CANCELLED)) {
  511. atomic_dec(&ctx->reqs_active);
  512. /* Still need the wake_up in case free_ioctx is waiting */
  513. goto put_rq;
  514. }
  515. /*
  516. * Add a completion event to the ring buffer. Must be done holding
  517. * ctx->ctx_lock to prevent other code from messing with the tail
  518. * pointer since we might be called from irq context.
  519. */
  520. spin_lock_irqsave(&ctx->completion_lock, flags);
  521. tail = info->tail;
  522. pos = tail + AIO_EVENTS_OFFSET;
  523. if (++tail >= info->nr)
  524. tail = 0;
  525. ev_page = kmap_atomic(info->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  526. event = ev_page + pos % AIO_EVENTS_PER_PAGE;
  527. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  528. event->data = iocb->ki_user_data;
  529. event->res = res;
  530. event->res2 = res2;
  531. kunmap_atomic(ev_page);
  532. flush_dcache_page(info->ring_pages[pos / AIO_EVENTS_PER_PAGE]);
  533. pr_debug("%p[%u]: %p: %p %Lx %lx %lx\n",
  534. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  535. res, res2);
  536. /* after flagging the request as done, we
  537. * must never even look at it again
  538. */
  539. smp_wmb(); /* make event visible before updating tail */
  540. info->tail = tail;
  541. ring = kmap_atomic(info->ring_pages[0]);
  542. ring->tail = tail;
  543. kunmap_atomic(ring);
  544. flush_dcache_page(info->ring_pages[0]);
  545. spin_unlock_irqrestore(&ctx->completion_lock, flags);
  546. pr_debug("added to ring %p at [%u]\n", iocb, tail);
  547. /*
  548. * Check if the user asked us to deliver the result through an
  549. * eventfd. The eventfd_signal() function is safe to be called
  550. * from IRQ context.
  551. */
  552. if (iocb->ki_eventfd != NULL)
  553. eventfd_signal(iocb->ki_eventfd, 1);
  554. put_rq:
  555. /* everything turned out well, dispose of the aiocb. */
  556. aio_put_req(iocb);
  557. /*
  558. * We have to order our ring_info tail store above and test
  559. * of the wait list below outside the wait lock. This is
  560. * like in wake_up_bit() where clearing a bit has to be
  561. * ordered with the unlocked test.
  562. */
  563. smp_mb();
  564. if (waitqueue_active(&ctx->wait))
  565. wake_up(&ctx->wait);
  566. rcu_read_unlock();
  567. }
  568. EXPORT_SYMBOL(aio_complete);
  569. /* aio_read_events
  570. * Pull an event off of the ioctx's event ring. Returns the number of
  571. * events fetched
  572. */
  573. static long aio_read_events_ring(struct kioctx *ctx,
  574. struct io_event __user *event, long nr)
  575. {
  576. struct aio_ring_info *info = &ctx->ring_info;
  577. struct aio_ring *ring;
  578. unsigned head, pos;
  579. long ret = 0;
  580. int copy_ret;
  581. mutex_lock(&info->ring_lock);
  582. ring = kmap_atomic(info->ring_pages[0]);
  583. head = ring->head;
  584. kunmap_atomic(ring);
  585. pr_debug("h%u t%u m%u\n", head, info->tail, info->nr);
  586. if (head == info->tail)
  587. goto out;
  588. while (ret < nr) {
  589. long avail;
  590. struct io_event *ev;
  591. struct page *page;
  592. avail = (head <= info->tail ? info->tail : info->nr) - head;
  593. if (head == info->tail)
  594. break;
  595. avail = min(avail, nr - ret);
  596. avail = min_t(long, avail, AIO_EVENTS_PER_PAGE -
  597. ((head + AIO_EVENTS_OFFSET) % AIO_EVENTS_PER_PAGE));
  598. pos = head + AIO_EVENTS_OFFSET;
  599. page = info->ring_pages[pos / AIO_EVENTS_PER_PAGE];
  600. pos %= AIO_EVENTS_PER_PAGE;
  601. ev = kmap(page);
  602. copy_ret = copy_to_user(event + ret, ev + pos,
  603. sizeof(*ev) * avail);
  604. kunmap(page);
  605. if (unlikely(copy_ret)) {
  606. ret = -EFAULT;
  607. goto out;
  608. }
  609. ret += avail;
  610. head += avail;
  611. head %= info->nr;
  612. }
  613. ring = kmap_atomic(info->ring_pages[0]);
  614. ring->head = head;
  615. kunmap_atomic(ring);
  616. flush_dcache_page(info->ring_pages[0]);
  617. pr_debug("%li h%u t%u\n", ret, head, info->tail);
  618. atomic_sub(ret, &ctx->reqs_active);
  619. out:
  620. mutex_unlock(&info->ring_lock);
  621. return ret;
  622. }
  623. static bool aio_read_events(struct kioctx *ctx, long min_nr, long nr,
  624. struct io_event __user *event, long *i)
  625. {
  626. long ret = aio_read_events_ring(ctx, event + *i, nr - *i);
  627. if (ret > 0)
  628. *i += ret;
  629. if (unlikely(atomic_read(&ctx->dead)))
  630. ret = -EINVAL;
  631. if (!*i)
  632. *i = ret;
  633. return ret < 0 || *i >= min_nr;
  634. }
  635. static long read_events(struct kioctx *ctx, long min_nr, long nr,
  636. struct io_event __user *event,
  637. struct timespec __user *timeout)
  638. {
  639. ktime_t until = { .tv64 = KTIME_MAX };
  640. long ret = 0;
  641. if (timeout) {
  642. struct timespec ts;
  643. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  644. return -EFAULT;
  645. until = timespec_to_ktime(ts);
  646. }
  647. /*
  648. * Note that aio_read_events() is being called as the conditional - i.e.
  649. * we're calling it after prepare_to_wait() has set task state to
  650. * TASK_INTERRUPTIBLE.
  651. *
  652. * But aio_read_events() can block, and if it blocks it's going to flip
  653. * the task state back to TASK_RUNNING.
  654. *
  655. * This should be ok, provided it doesn't flip the state back to
  656. * TASK_RUNNING and return 0 too much - that causes us to spin. That
  657. * will only happen if the mutex_lock() call blocks, and we then find
  658. * the ringbuffer empty. So in practice we should be ok, but it's
  659. * something to be aware of when touching this code.
  660. */
  661. wait_event_interruptible_hrtimeout(ctx->wait,
  662. aio_read_events(ctx, min_nr, nr, event, &ret), until);
  663. if (!ret && signal_pending(current))
  664. ret = -EINTR;
  665. return ret;
  666. }
  667. /* sys_io_setup:
  668. * Create an aio_context capable of receiving at least nr_events.
  669. * ctxp must not point to an aio_context that already exists, and
  670. * must be initialized to 0 prior to the call. On successful
  671. * creation of the aio_context, *ctxp is filled in with the resulting
  672. * handle. May fail with -EINVAL if *ctxp is not initialized,
  673. * if the specified nr_events exceeds internal limits. May fail
  674. * with -EAGAIN if the specified nr_events exceeds the user's limit
  675. * of available events. May fail with -ENOMEM if insufficient kernel
  676. * resources are available. May fail with -EFAULT if an invalid
  677. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  678. * implemented.
  679. */
  680. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  681. {
  682. struct kioctx *ioctx = NULL;
  683. unsigned long ctx;
  684. long ret;
  685. ret = get_user(ctx, ctxp);
  686. if (unlikely(ret))
  687. goto out;
  688. ret = -EINVAL;
  689. if (unlikely(ctx || nr_events == 0)) {
  690. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  691. ctx, nr_events);
  692. goto out;
  693. }
  694. ioctx = ioctx_alloc(nr_events);
  695. ret = PTR_ERR(ioctx);
  696. if (!IS_ERR(ioctx)) {
  697. ret = put_user(ioctx->user_id, ctxp);
  698. if (ret)
  699. kill_ioctx(ioctx);
  700. put_ioctx(ioctx);
  701. }
  702. out:
  703. return ret;
  704. }
  705. /* sys_io_destroy:
  706. * Destroy the aio_context specified. May cancel any outstanding
  707. * AIOs and block on completion. Will fail with -ENOSYS if not
  708. * implemented. May fail with -EINVAL if the context pointed to
  709. * is invalid.
  710. */
  711. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  712. {
  713. struct kioctx *ioctx = lookup_ioctx(ctx);
  714. if (likely(NULL != ioctx)) {
  715. kill_ioctx(ioctx);
  716. put_ioctx(ioctx);
  717. return 0;
  718. }
  719. pr_debug("EINVAL: io_destroy: invalid context id\n");
  720. return -EINVAL;
  721. }
  722. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  723. {
  724. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  725. BUG_ON(ret <= 0);
  726. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  727. ssize_t this = min((ssize_t)iov->iov_len, ret);
  728. iov->iov_base += this;
  729. iov->iov_len -= this;
  730. iocb->ki_left -= this;
  731. ret -= this;
  732. if (iov->iov_len == 0) {
  733. iocb->ki_cur_seg++;
  734. iov++;
  735. }
  736. }
  737. /* the caller should not have done more io than what fit in
  738. * the remaining iovecs */
  739. BUG_ON(ret > 0 && iocb->ki_left == 0);
  740. }
  741. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  742. {
  743. struct file *file = iocb->ki_filp;
  744. struct address_space *mapping = file->f_mapping;
  745. struct inode *inode = mapping->host;
  746. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  747. unsigned long, loff_t);
  748. ssize_t ret = 0;
  749. unsigned short opcode;
  750. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  751. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  752. rw_op = file->f_op->aio_read;
  753. opcode = IOCB_CMD_PREADV;
  754. } else {
  755. rw_op = file->f_op->aio_write;
  756. opcode = IOCB_CMD_PWRITEV;
  757. }
  758. /* This matches the pread()/pwrite() logic */
  759. if (iocb->ki_pos < 0)
  760. return -EINVAL;
  761. if (opcode == IOCB_CMD_PWRITEV)
  762. file_start_write(file);
  763. do {
  764. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  765. iocb->ki_nr_segs - iocb->ki_cur_seg,
  766. iocb->ki_pos);
  767. if (ret > 0)
  768. aio_advance_iovec(iocb, ret);
  769. /* retry all partial writes. retry partial reads as long as its a
  770. * regular file. */
  771. } while (ret > 0 && iocb->ki_left > 0 &&
  772. (opcode == IOCB_CMD_PWRITEV ||
  773. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  774. if (opcode == IOCB_CMD_PWRITEV)
  775. file_end_write(file);
  776. /* This means we must have transferred all that we could */
  777. /* No need to retry anymore */
  778. if ((ret == 0) || (iocb->ki_left == 0))
  779. ret = iocb->ki_nbytes - iocb->ki_left;
  780. /* If we managed to write some out we return that, rather than
  781. * the eventual error. */
  782. if (opcode == IOCB_CMD_PWRITEV
  783. && ret < 0 && ret != -EIOCBQUEUED
  784. && iocb->ki_nbytes - iocb->ki_left)
  785. ret = iocb->ki_nbytes - iocb->ki_left;
  786. return ret;
  787. }
  788. static ssize_t aio_fdsync(struct kiocb *iocb)
  789. {
  790. struct file *file = iocb->ki_filp;
  791. ssize_t ret = -EINVAL;
  792. if (file->f_op->aio_fsync)
  793. ret = file->f_op->aio_fsync(iocb, 1);
  794. return ret;
  795. }
  796. static ssize_t aio_fsync(struct kiocb *iocb)
  797. {
  798. struct file *file = iocb->ki_filp;
  799. ssize_t ret = -EINVAL;
  800. if (file->f_op->aio_fsync)
  801. ret = file->f_op->aio_fsync(iocb, 0);
  802. return ret;
  803. }
  804. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
  805. {
  806. ssize_t ret;
  807. #ifdef CONFIG_COMPAT
  808. if (compat)
  809. ret = compat_rw_copy_check_uvector(type,
  810. (struct compat_iovec __user *)kiocb->ki_buf,
  811. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  812. &kiocb->ki_iovec);
  813. else
  814. #endif
  815. ret = rw_copy_check_uvector(type,
  816. (struct iovec __user *)kiocb->ki_buf,
  817. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  818. &kiocb->ki_iovec);
  819. if (ret < 0)
  820. goto out;
  821. ret = rw_verify_area(type, kiocb->ki_filp, &kiocb->ki_pos, ret);
  822. if (ret < 0)
  823. goto out;
  824. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  825. kiocb->ki_cur_seg = 0;
  826. /* ki_nbytes/left now reflect bytes instead of segs */
  827. kiocb->ki_nbytes = ret;
  828. kiocb->ki_left = ret;
  829. ret = 0;
  830. out:
  831. return ret;
  832. }
  833. static ssize_t aio_setup_single_vector(int type, struct file * file, struct kiocb *kiocb)
  834. {
  835. int bytes;
  836. bytes = rw_verify_area(type, file, &kiocb->ki_pos, kiocb->ki_left);
  837. if (bytes < 0)
  838. return bytes;
  839. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  840. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  841. kiocb->ki_iovec->iov_len = bytes;
  842. kiocb->ki_nr_segs = 1;
  843. kiocb->ki_cur_seg = 0;
  844. return 0;
  845. }
  846. /*
  847. * aio_setup_iocb:
  848. * Performs the initial checks and aio retry method
  849. * setup for the kiocb at the time of io submission.
  850. */
  851. static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
  852. {
  853. struct file *file = kiocb->ki_filp;
  854. ssize_t ret = 0;
  855. switch (kiocb->ki_opcode) {
  856. case IOCB_CMD_PREAD:
  857. ret = -EBADF;
  858. if (unlikely(!(file->f_mode & FMODE_READ)))
  859. break;
  860. ret = -EFAULT;
  861. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  862. kiocb->ki_left)))
  863. break;
  864. ret = aio_setup_single_vector(READ, file, kiocb);
  865. if (ret)
  866. break;
  867. ret = -EINVAL;
  868. if (file->f_op->aio_read)
  869. kiocb->ki_retry = aio_rw_vect_retry;
  870. break;
  871. case IOCB_CMD_PWRITE:
  872. ret = -EBADF;
  873. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  874. break;
  875. ret = -EFAULT;
  876. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  877. kiocb->ki_left)))
  878. break;
  879. ret = aio_setup_single_vector(WRITE, file, kiocb);
  880. if (ret)
  881. break;
  882. ret = -EINVAL;
  883. if (file->f_op->aio_write)
  884. kiocb->ki_retry = aio_rw_vect_retry;
  885. break;
  886. case IOCB_CMD_PREADV:
  887. ret = -EBADF;
  888. if (unlikely(!(file->f_mode & FMODE_READ)))
  889. break;
  890. ret = aio_setup_vectored_rw(READ, kiocb, compat);
  891. if (ret)
  892. break;
  893. ret = -EINVAL;
  894. if (file->f_op->aio_read)
  895. kiocb->ki_retry = aio_rw_vect_retry;
  896. break;
  897. case IOCB_CMD_PWRITEV:
  898. ret = -EBADF;
  899. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  900. break;
  901. ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
  902. if (ret)
  903. break;
  904. ret = -EINVAL;
  905. if (file->f_op->aio_write)
  906. kiocb->ki_retry = aio_rw_vect_retry;
  907. break;
  908. case IOCB_CMD_FDSYNC:
  909. ret = -EINVAL;
  910. if (file->f_op->aio_fsync)
  911. kiocb->ki_retry = aio_fdsync;
  912. break;
  913. case IOCB_CMD_FSYNC:
  914. ret = -EINVAL;
  915. if (file->f_op->aio_fsync)
  916. kiocb->ki_retry = aio_fsync;
  917. break;
  918. default:
  919. pr_debug("EINVAL: no operation provided\n");
  920. ret = -EINVAL;
  921. }
  922. if (!kiocb->ki_retry)
  923. return ret;
  924. return 0;
  925. }
  926. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  927. struct iocb *iocb, bool compat)
  928. {
  929. struct kiocb *req;
  930. ssize_t ret;
  931. /* enforce forwards compatibility on users */
  932. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  933. pr_debug("EINVAL: reserve field set\n");
  934. return -EINVAL;
  935. }
  936. /* prevent overflows */
  937. if (unlikely(
  938. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  939. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  940. ((ssize_t)iocb->aio_nbytes < 0)
  941. )) {
  942. pr_debug("EINVAL: io_submit: overflow check\n");
  943. return -EINVAL;
  944. }
  945. req = aio_get_req(ctx); /* returns with 2 references to req */
  946. if (unlikely(!req))
  947. return -EAGAIN;
  948. req->ki_filp = fget(iocb->aio_fildes);
  949. if (unlikely(!req->ki_filp)) {
  950. ret = -EBADF;
  951. goto out_put_req;
  952. }
  953. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  954. /*
  955. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  956. * instance of the file* now. The file descriptor must be
  957. * an eventfd() fd, and will be signaled for each completed
  958. * event using the eventfd_signal() function.
  959. */
  960. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  961. if (IS_ERR(req->ki_eventfd)) {
  962. ret = PTR_ERR(req->ki_eventfd);
  963. req->ki_eventfd = NULL;
  964. goto out_put_req;
  965. }
  966. }
  967. ret = put_user(req->ki_key, &user_iocb->aio_key);
  968. if (unlikely(ret)) {
  969. pr_debug("EFAULT: aio_key\n");
  970. goto out_put_req;
  971. }
  972. req->ki_obj.user = user_iocb;
  973. req->ki_user_data = iocb->aio_data;
  974. req->ki_pos = iocb->aio_offset;
  975. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  976. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  977. req->ki_opcode = iocb->aio_lio_opcode;
  978. ret = aio_setup_iocb(req, compat);
  979. if (ret)
  980. goto out_put_req;
  981. ret = req->ki_retry(req);
  982. if (ret != -EIOCBQUEUED) {
  983. /*
  984. * There's no easy way to restart the syscall since other AIO's
  985. * may be already running. Just fail this IO with EINTR.
  986. */
  987. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  988. ret == -ERESTARTNOHAND ||
  989. ret == -ERESTART_RESTARTBLOCK))
  990. ret = -EINTR;
  991. aio_complete(req, ret, 0);
  992. }
  993. aio_put_req(req); /* drop extra ref to req */
  994. return 0;
  995. out_put_req:
  996. atomic_dec(&ctx->reqs_active);
  997. aio_put_req(req); /* drop extra ref to req */
  998. aio_put_req(req); /* drop i/o ref to req */
  999. return ret;
  1000. }
  1001. long do_io_submit(aio_context_t ctx_id, long nr,
  1002. struct iocb __user *__user *iocbpp, bool compat)
  1003. {
  1004. struct kioctx *ctx;
  1005. long ret = 0;
  1006. int i = 0;
  1007. struct blk_plug plug;
  1008. if (unlikely(nr < 0))
  1009. return -EINVAL;
  1010. if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
  1011. nr = LONG_MAX/sizeof(*iocbpp);
  1012. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1013. return -EFAULT;
  1014. ctx = lookup_ioctx(ctx_id);
  1015. if (unlikely(!ctx)) {
  1016. pr_debug("EINVAL: invalid context id\n");
  1017. return -EINVAL;
  1018. }
  1019. blk_start_plug(&plug);
  1020. /*
  1021. * AKPM: should this return a partial result if some of the IOs were
  1022. * successfully submitted?
  1023. */
  1024. for (i=0; i<nr; i++) {
  1025. struct iocb __user *user_iocb;
  1026. struct iocb tmp;
  1027. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1028. ret = -EFAULT;
  1029. break;
  1030. }
  1031. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1032. ret = -EFAULT;
  1033. break;
  1034. }
  1035. ret = io_submit_one(ctx, user_iocb, &tmp, compat);
  1036. if (ret)
  1037. break;
  1038. }
  1039. blk_finish_plug(&plug);
  1040. put_ioctx(ctx);
  1041. return i ? i : ret;
  1042. }
  1043. /* sys_io_submit:
  1044. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1045. * the number of iocbs queued. May return -EINVAL if the aio_context
  1046. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1047. * *iocbpp[0] is not properly initialized, if the operation specified
  1048. * is invalid for the file descriptor in the iocb. May fail with
  1049. * -EFAULT if any of the data structures point to invalid data. May
  1050. * fail with -EBADF if the file descriptor specified in the first
  1051. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1052. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1053. * fail with -ENOSYS if not implemented.
  1054. */
  1055. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1056. struct iocb __user * __user *, iocbpp)
  1057. {
  1058. return do_io_submit(ctx_id, nr, iocbpp, 0);
  1059. }
  1060. /* lookup_kiocb
  1061. * Finds a given iocb for cancellation.
  1062. */
  1063. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1064. u32 key)
  1065. {
  1066. struct list_head *pos;
  1067. assert_spin_locked(&ctx->ctx_lock);
  1068. /* TODO: use a hash or array, this sucks. */
  1069. list_for_each(pos, &ctx->active_reqs) {
  1070. struct kiocb *kiocb = list_kiocb(pos);
  1071. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1072. return kiocb;
  1073. }
  1074. return NULL;
  1075. }
  1076. /* sys_io_cancel:
  1077. * Attempts to cancel an iocb previously passed to io_submit. If
  1078. * the operation is successfully cancelled, the resulting event is
  1079. * copied into the memory pointed to by result without being placed
  1080. * into the completion queue and 0 is returned. May fail with
  1081. * -EFAULT if any of the data structures pointed to are invalid.
  1082. * May fail with -EINVAL if aio_context specified by ctx_id is
  1083. * invalid. May fail with -EAGAIN if the iocb specified was not
  1084. * cancelled. Will fail with -ENOSYS if not implemented.
  1085. */
  1086. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1087. struct io_event __user *, result)
  1088. {
  1089. struct io_event res;
  1090. struct kioctx *ctx;
  1091. struct kiocb *kiocb;
  1092. u32 key;
  1093. int ret;
  1094. ret = get_user(key, &iocb->aio_key);
  1095. if (unlikely(ret))
  1096. return -EFAULT;
  1097. ctx = lookup_ioctx(ctx_id);
  1098. if (unlikely(!ctx))
  1099. return -EINVAL;
  1100. spin_lock_irq(&ctx->ctx_lock);
  1101. kiocb = lookup_kiocb(ctx, iocb, key);
  1102. if (kiocb)
  1103. ret = kiocb_cancel(ctx, kiocb, &res);
  1104. else
  1105. ret = -EINVAL;
  1106. spin_unlock_irq(&ctx->ctx_lock);
  1107. if (!ret) {
  1108. /* Cancellation succeeded -- copy the result
  1109. * into the user's buffer.
  1110. */
  1111. if (copy_to_user(result, &res, sizeof(res)))
  1112. ret = -EFAULT;
  1113. }
  1114. put_ioctx(ctx);
  1115. return ret;
  1116. }
  1117. /* io_getevents:
  1118. * Attempts to read at least min_nr events and up to nr events from
  1119. * the completion queue for the aio_context specified by ctx_id. If
  1120. * it succeeds, the number of read events is returned. May fail with
  1121. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1122. * out of range, if timeout is out of range. May fail with -EFAULT
  1123. * if any of the memory specified is invalid. May return 0 or
  1124. * < min_nr if the timeout specified by timeout has elapsed
  1125. * before sufficient events are available, where timeout == NULL
  1126. * specifies an infinite timeout. Note that the timeout pointed to by
  1127. * timeout is relative and will be updated if not NULL and the
  1128. * operation blocks. Will fail with -ENOSYS if not implemented.
  1129. */
  1130. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1131. long, min_nr,
  1132. long, nr,
  1133. struct io_event __user *, events,
  1134. struct timespec __user *, timeout)
  1135. {
  1136. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1137. long ret = -EINVAL;
  1138. if (likely(ioctx)) {
  1139. if (likely(min_nr <= nr && min_nr >= 0))
  1140. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1141. put_ioctx(ioctx);
  1142. }
  1143. return ret;
  1144. }