PageRenderTime 30ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/aio.c

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