aio.c 38 KB

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