aio.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553
  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. int 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(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. 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 (!ctx->reqs_active)
  315. goto out;
  316. add_wait_queue(&ctx->wait, &wait);
  317. set_task_state(tsk, TASK_UNINTERRUPTIBLE);
  318. while (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 (iocb->ki_users) {
  335. set_current_state(TASK_UNINTERRUPTIBLE);
  336. if (!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. 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 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->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. 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. ctx->reqs_active--;
  428. }
  429. if (unlikely(!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) - ctx->reqs_active;
  456. BUG_ON(avail < 0);
  457. if (avail < allocated) {
  458. /* Trim back the number of requests. */
  459. list_for_each_entry_safe(req, n, &batch->head, ki_batch) {
  460. list_del(&req->ki_batch);
  461. kmem_cache_free(kiocb_cachep, req);
  462. if (--allocated <= avail)
  463. break;
  464. }
  465. }
  466. batch->count -= allocated;
  467. list_for_each_entry(req, &batch->head, ki_batch) {
  468. list_add(&req->ki_list, &ctx->active_reqs);
  469. ctx->reqs_active++;
  470. }
  471. kunmap_atomic(ring);
  472. spin_unlock_irq(&ctx->ctx_lock);
  473. out:
  474. return allocated;
  475. }
  476. static inline struct kiocb *aio_get_req(struct kioctx *ctx,
  477. struct kiocb_batch *batch)
  478. {
  479. struct kiocb *req;
  480. if (list_empty(&batch->head))
  481. if (kiocb_batch_refill(ctx, batch) == 0)
  482. return NULL;
  483. req = list_first_entry(&batch->head, struct kiocb, ki_batch);
  484. list_del(&req->ki_batch);
  485. return req;
  486. }
  487. static inline void really_put_req(struct kioctx *ctx, struct kiocb *req)
  488. {
  489. assert_spin_locked(&ctx->ctx_lock);
  490. if (req->ki_eventfd != NULL)
  491. eventfd_ctx_put(req->ki_eventfd);
  492. if (req->ki_dtor)
  493. req->ki_dtor(req);
  494. if (req->ki_iovec != &req->ki_inline_vec)
  495. kfree(req->ki_iovec);
  496. kmem_cache_free(kiocb_cachep, req);
  497. ctx->reqs_active--;
  498. if (unlikely(!ctx->reqs_active && ctx->dead))
  499. wake_up_all(&ctx->wait);
  500. }
  501. /* __aio_put_req
  502. * Returns true if this put was the last user of the request.
  503. */
  504. static void __aio_put_req(struct kioctx *ctx, struct kiocb *req)
  505. {
  506. pr_debug("(%p): f_count=%ld\n",
  507. req, atomic_long_read(&req->ki_filp->f_count));
  508. assert_spin_locked(&ctx->ctx_lock);
  509. req->ki_users--;
  510. BUG_ON(req->ki_users < 0);
  511. if (likely(req->ki_users))
  512. return;
  513. list_del(&req->ki_list); /* remove from active_reqs */
  514. req->ki_cancel = NULL;
  515. req->ki_retry = NULL;
  516. fput(req->ki_filp);
  517. req->ki_filp = NULL;
  518. really_put_req(ctx, req);
  519. }
  520. /* aio_put_req
  521. * Returns true if this put was the last user of the kiocb,
  522. * false if the request is still in use.
  523. */
  524. void aio_put_req(struct kiocb *req)
  525. {
  526. struct kioctx *ctx = req->ki_ctx;
  527. spin_lock_irq(&ctx->ctx_lock);
  528. __aio_put_req(ctx, req);
  529. spin_unlock_irq(&ctx->ctx_lock);
  530. }
  531. EXPORT_SYMBOL(aio_put_req);
  532. static struct kioctx *lookup_ioctx(unsigned long ctx_id)
  533. {
  534. struct mm_struct *mm = current->mm;
  535. struct kioctx *ctx, *ret = NULL;
  536. rcu_read_lock();
  537. hlist_for_each_entry_rcu(ctx, &mm->ioctx_list, list) {
  538. /*
  539. * RCU protects us against accessing freed memory but
  540. * we have to be careful not to get a reference when the
  541. * reference count already dropped to 0 (ctx->dead test
  542. * is unreliable because of races).
  543. */
  544. if (ctx->user_id == ctx_id && !ctx->dead && try_get_ioctx(ctx)){
  545. ret = ctx;
  546. break;
  547. }
  548. }
  549. rcu_read_unlock();
  550. return ret;
  551. }
  552. /* aio_complete
  553. * Called when the io request on the given iocb is complete.
  554. */
  555. void aio_complete(struct kiocb *iocb, long res, long res2)
  556. {
  557. struct kioctx *ctx = iocb->ki_ctx;
  558. struct aio_ring_info *info;
  559. struct aio_ring *ring;
  560. struct io_event *event;
  561. unsigned long flags;
  562. unsigned long tail;
  563. /*
  564. * Special case handling for sync iocbs:
  565. * - events go directly into the iocb for fast handling
  566. * - the sync task with the iocb in its stack holds the single iocb
  567. * ref, no other paths have a way to get another ref
  568. * - the sync task helpfully left a reference to itself in the iocb
  569. */
  570. if (is_sync_kiocb(iocb)) {
  571. BUG_ON(iocb->ki_users != 1);
  572. iocb->ki_user_data = res;
  573. iocb->ki_users = 0;
  574. wake_up_process(iocb->ki_obj.tsk);
  575. return;
  576. }
  577. info = &ctx->ring_info;
  578. /* add a completion event to the ring buffer.
  579. * must be done holding ctx->ctx_lock to prevent
  580. * other code from messing with the tail
  581. * pointer since we might be called from irq
  582. * context.
  583. */
  584. spin_lock_irqsave(&ctx->ctx_lock, flags);
  585. /*
  586. * cancelled requests don't get events, userland was given one
  587. * when the event got cancelled.
  588. */
  589. if (kiocbIsCancelled(iocb))
  590. goto put_rq;
  591. ring = kmap_atomic(info->ring_pages[0]);
  592. tail = info->tail;
  593. event = aio_ring_event(info, tail);
  594. if (++tail >= info->nr)
  595. tail = 0;
  596. event->obj = (u64)(unsigned long)iocb->ki_obj.user;
  597. event->data = iocb->ki_user_data;
  598. event->res = res;
  599. event->res2 = res2;
  600. pr_debug("%p[%lu]: %p: %p %Lx %lx %lx\n",
  601. ctx, tail, iocb, iocb->ki_obj.user, iocb->ki_user_data,
  602. res, res2);
  603. /* after flagging the request as done, we
  604. * must never even look at it again
  605. */
  606. smp_wmb(); /* make event visible before updating tail */
  607. info->tail = tail;
  608. ring->tail = tail;
  609. put_aio_ring_event(event);
  610. kunmap_atomic(ring);
  611. pr_debug("added to ring %p at [%lu]\n", iocb, tail);
  612. /*
  613. * Check if the user asked us to deliver the result through an
  614. * eventfd. The eventfd_signal() function is safe to be called
  615. * from IRQ context.
  616. */
  617. if (iocb->ki_eventfd != NULL)
  618. eventfd_signal(iocb->ki_eventfd, 1);
  619. put_rq:
  620. /* everything turned out well, dispose of the aiocb. */
  621. __aio_put_req(ctx, iocb);
  622. /*
  623. * We have to order our ring_info tail store above and test
  624. * of the wait list below outside the wait lock. This is
  625. * like in wake_up_bit() where clearing a bit has to be
  626. * ordered with the unlocked test.
  627. */
  628. smp_mb();
  629. if (waitqueue_active(&ctx->wait))
  630. wake_up(&ctx->wait);
  631. spin_unlock_irqrestore(&ctx->ctx_lock, flags);
  632. }
  633. EXPORT_SYMBOL(aio_complete);
  634. /* aio_read_evt
  635. * Pull an event off of the ioctx's event ring. Returns the number of
  636. * events fetched (0 or 1 ;-)
  637. * FIXME: make this use cmpxchg.
  638. * TODO: make the ringbuffer user mmap()able (requires FIXME).
  639. */
  640. static int aio_read_evt(struct kioctx *ioctx, struct io_event *ent)
  641. {
  642. struct aio_ring_info *info = &ioctx->ring_info;
  643. struct aio_ring *ring;
  644. unsigned long head;
  645. int ret = 0;
  646. ring = kmap_atomic(info->ring_pages[0]);
  647. pr_debug("h%u t%u m%u\n", ring->head, ring->tail, ring->nr);
  648. if (ring->head == ring->tail)
  649. goto out;
  650. spin_lock(&info->ring_lock);
  651. head = ring->head % info->nr;
  652. if (head != ring->tail) {
  653. struct io_event *evp = aio_ring_event(info, head);
  654. *ent = *evp;
  655. head = (head + 1) % info->nr;
  656. smp_mb(); /* finish reading the event before updatng the head */
  657. ring->head = head;
  658. ret = 1;
  659. put_aio_ring_event(evp);
  660. }
  661. spin_unlock(&info->ring_lock);
  662. out:
  663. kunmap_atomic(ring);
  664. pr_debug("%d h%u t%u\n", ret, ring->head, ring->tail);
  665. return ret;
  666. }
  667. struct aio_timeout {
  668. struct timer_list timer;
  669. int timed_out;
  670. struct task_struct *p;
  671. };
  672. static void timeout_func(unsigned long data)
  673. {
  674. struct aio_timeout *to = (struct aio_timeout *)data;
  675. to->timed_out = 1;
  676. wake_up_process(to->p);
  677. }
  678. static inline void init_timeout(struct aio_timeout *to)
  679. {
  680. setup_timer_on_stack(&to->timer, timeout_func, (unsigned long) to);
  681. to->timed_out = 0;
  682. to->p = current;
  683. }
  684. static inline void set_timeout(long start_jiffies, struct aio_timeout *to,
  685. const struct timespec *ts)
  686. {
  687. to->timer.expires = start_jiffies + timespec_to_jiffies(ts);
  688. if (time_after(to->timer.expires, jiffies))
  689. add_timer(&to->timer);
  690. else
  691. to->timed_out = 1;
  692. }
  693. static inline void clear_timeout(struct aio_timeout *to)
  694. {
  695. del_singleshot_timer_sync(&to->timer);
  696. }
  697. static int read_events(struct kioctx *ctx,
  698. long min_nr, long nr,
  699. struct io_event __user *event,
  700. struct timespec __user *timeout)
  701. {
  702. long start_jiffies = jiffies;
  703. struct task_struct *tsk = current;
  704. DECLARE_WAITQUEUE(wait, tsk);
  705. int ret;
  706. int i = 0;
  707. struct io_event ent;
  708. struct aio_timeout to;
  709. /* needed to zero any padding within an entry (there shouldn't be
  710. * any, but C is fun!
  711. */
  712. memset(&ent, 0, sizeof(ent));
  713. ret = 0;
  714. while (likely(i < nr)) {
  715. ret = aio_read_evt(ctx, &ent);
  716. if (unlikely(ret <= 0))
  717. break;
  718. pr_debug("%Lx %Lx %Lx %Lx\n",
  719. ent.data, ent.obj, ent.res, ent.res2);
  720. /* Could we split the check in two? */
  721. ret = -EFAULT;
  722. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  723. pr_debug("lost an event due to EFAULT.\n");
  724. break;
  725. }
  726. ret = 0;
  727. /* Good, event copied to userland, update counts. */
  728. event ++;
  729. i ++;
  730. }
  731. if (min_nr <= i)
  732. return i;
  733. if (ret)
  734. return ret;
  735. /* End fast path */
  736. init_timeout(&to);
  737. if (timeout) {
  738. struct timespec ts;
  739. ret = -EFAULT;
  740. if (unlikely(copy_from_user(&ts, timeout, sizeof(ts))))
  741. goto out;
  742. set_timeout(start_jiffies, &to, &ts);
  743. }
  744. while (likely(i < nr)) {
  745. add_wait_queue_exclusive(&ctx->wait, &wait);
  746. do {
  747. set_task_state(tsk, TASK_INTERRUPTIBLE);
  748. ret = aio_read_evt(ctx, &ent);
  749. if (ret)
  750. break;
  751. if (min_nr <= i)
  752. break;
  753. if (unlikely(ctx->dead)) {
  754. ret = -EINVAL;
  755. break;
  756. }
  757. if (to.timed_out) /* Only check after read evt */
  758. break;
  759. /* Try to only show up in io wait if there are ops
  760. * in flight */
  761. if (ctx->reqs_active)
  762. io_schedule();
  763. else
  764. schedule();
  765. if (signal_pending(tsk)) {
  766. ret = -EINTR;
  767. break;
  768. }
  769. /*ret = aio_read_evt(ctx, &ent);*/
  770. } while (1) ;
  771. set_task_state(tsk, TASK_RUNNING);
  772. remove_wait_queue(&ctx->wait, &wait);
  773. if (unlikely(ret <= 0))
  774. break;
  775. ret = -EFAULT;
  776. if (unlikely(copy_to_user(event, &ent, sizeof(ent)))) {
  777. pr_debug("lost an event due to EFAULT.\n");
  778. break;
  779. }
  780. /* Good, event copied to userland, update counts. */
  781. event ++;
  782. i ++;
  783. }
  784. if (timeout)
  785. clear_timeout(&to);
  786. out:
  787. destroy_timer_on_stack(&to.timer);
  788. return i ? i : ret;
  789. }
  790. /* Take an ioctx and remove it from the list of ioctx's. Protects
  791. * against races with itself via ->dead.
  792. */
  793. static void io_destroy(struct kioctx *ioctx)
  794. {
  795. struct mm_struct *mm = current->mm;
  796. int was_dead;
  797. /* delete the entry from the list is someone else hasn't already */
  798. spin_lock(&mm->ioctx_lock);
  799. was_dead = ioctx->dead;
  800. ioctx->dead = 1;
  801. hlist_del_rcu(&ioctx->list);
  802. spin_unlock(&mm->ioctx_lock);
  803. pr_debug("(%p)\n", ioctx);
  804. if (likely(!was_dead))
  805. put_ioctx(ioctx); /* twice for the list */
  806. kill_ctx(ioctx);
  807. /*
  808. * Wake up any waiters. The setting of ctx->dead must be seen
  809. * by other CPUs at this point. Right now, we rely on the
  810. * locking done by the above calls to ensure this consistency.
  811. */
  812. wake_up_all(&ioctx->wait);
  813. }
  814. /* sys_io_setup:
  815. * Create an aio_context capable of receiving at least nr_events.
  816. * ctxp must not point to an aio_context that already exists, and
  817. * must be initialized to 0 prior to the call. On successful
  818. * creation of the aio_context, *ctxp is filled in with the resulting
  819. * handle. May fail with -EINVAL if *ctxp is not initialized,
  820. * if the specified nr_events exceeds internal limits. May fail
  821. * with -EAGAIN if the specified nr_events exceeds the user's limit
  822. * of available events. May fail with -ENOMEM if insufficient kernel
  823. * resources are available. May fail with -EFAULT if an invalid
  824. * pointer is passed for ctxp. Will fail with -ENOSYS if not
  825. * implemented.
  826. */
  827. SYSCALL_DEFINE2(io_setup, unsigned, nr_events, aio_context_t __user *, ctxp)
  828. {
  829. struct kioctx *ioctx = NULL;
  830. unsigned long ctx;
  831. long ret;
  832. ret = get_user(ctx, ctxp);
  833. if (unlikely(ret))
  834. goto out;
  835. ret = -EINVAL;
  836. if (unlikely(ctx || nr_events == 0)) {
  837. pr_debug("EINVAL: io_setup: ctx %lu nr_events %u\n",
  838. ctx, nr_events);
  839. goto out;
  840. }
  841. ioctx = ioctx_alloc(nr_events);
  842. ret = PTR_ERR(ioctx);
  843. if (!IS_ERR(ioctx)) {
  844. ret = put_user(ioctx->user_id, ctxp);
  845. if (ret)
  846. io_destroy(ioctx);
  847. put_ioctx(ioctx);
  848. }
  849. out:
  850. return ret;
  851. }
  852. /* sys_io_destroy:
  853. * Destroy the aio_context specified. May cancel any outstanding
  854. * AIOs and block on completion. Will fail with -ENOSYS if not
  855. * implemented. May fail with -EINVAL if the context pointed to
  856. * is invalid.
  857. */
  858. SYSCALL_DEFINE1(io_destroy, aio_context_t, ctx)
  859. {
  860. struct kioctx *ioctx = lookup_ioctx(ctx);
  861. if (likely(NULL != ioctx)) {
  862. io_destroy(ioctx);
  863. put_ioctx(ioctx);
  864. return 0;
  865. }
  866. pr_debug("EINVAL: io_destroy: invalid context id\n");
  867. return -EINVAL;
  868. }
  869. static void aio_advance_iovec(struct kiocb *iocb, ssize_t ret)
  870. {
  871. struct iovec *iov = &iocb->ki_iovec[iocb->ki_cur_seg];
  872. BUG_ON(ret <= 0);
  873. while (iocb->ki_cur_seg < iocb->ki_nr_segs && ret > 0) {
  874. ssize_t this = min((ssize_t)iov->iov_len, ret);
  875. iov->iov_base += this;
  876. iov->iov_len -= this;
  877. iocb->ki_left -= this;
  878. ret -= this;
  879. if (iov->iov_len == 0) {
  880. iocb->ki_cur_seg++;
  881. iov++;
  882. }
  883. }
  884. /* the caller should not have done more io than what fit in
  885. * the remaining iovecs */
  886. BUG_ON(ret > 0 && iocb->ki_left == 0);
  887. }
  888. static ssize_t aio_rw_vect_retry(struct kiocb *iocb)
  889. {
  890. struct file *file = iocb->ki_filp;
  891. struct address_space *mapping = file->f_mapping;
  892. struct inode *inode = mapping->host;
  893. ssize_t (*rw_op)(struct kiocb *, const struct iovec *,
  894. unsigned long, loff_t);
  895. ssize_t ret = 0;
  896. unsigned short opcode;
  897. if ((iocb->ki_opcode == IOCB_CMD_PREADV) ||
  898. (iocb->ki_opcode == IOCB_CMD_PREAD)) {
  899. rw_op = file->f_op->aio_read;
  900. opcode = IOCB_CMD_PREADV;
  901. } else {
  902. rw_op = file->f_op->aio_write;
  903. opcode = IOCB_CMD_PWRITEV;
  904. }
  905. /* This matches the pread()/pwrite() logic */
  906. if (iocb->ki_pos < 0)
  907. return -EINVAL;
  908. if (opcode == IOCB_CMD_PWRITEV)
  909. file_start_write(file);
  910. do {
  911. ret = rw_op(iocb, &iocb->ki_iovec[iocb->ki_cur_seg],
  912. iocb->ki_nr_segs - iocb->ki_cur_seg,
  913. iocb->ki_pos);
  914. if (ret > 0)
  915. aio_advance_iovec(iocb, ret);
  916. /* retry all partial writes. retry partial reads as long as its a
  917. * regular file. */
  918. } while (ret > 0 && iocb->ki_left > 0 &&
  919. (opcode == IOCB_CMD_PWRITEV ||
  920. (!S_ISFIFO(inode->i_mode) && !S_ISSOCK(inode->i_mode))));
  921. if (opcode == IOCB_CMD_PWRITEV)
  922. file_end_write(file);
  923. /* This means we must have transferred all that we could */
  924. /* No need to retry anymore */
  925. if ((ret == 0) || (iocb->ki_left == 0))
  926. ret = iocb->ki_nbytes - iocb->ki_left;
  927. /* If we managed to write some out we return that, rather than
  928. * the eventual error. */
  929. if (opcode == IOCB_CMD_PWRITEV
  930. && ret < 0 && ret != -EIOCBQUEUED
  931. && iocb->ki_nbytes - iocb->ki_left)
  932. ret = iocb->ki_nbytes - iocb->ki_left;
  933. return ret;
  934. }
  935. static ssize_t aio_fdsync(struct kiocb *iocb)
  936. {
  937. struct file *file = iocb->ki_filp;
  938. ssize_t ret = -EINVAL;
  939. if (file->f_op->aio_fsync)
  940. ret = file->f_op->aio_fsync(iocb, 1);
  941. return ret;
  942. }
  943. static ssize_t aio_fsync(struct kiocb *iocb)
  944. {
  945. struct file *file = iocb->ki_filp;
  946. ssize_t ret = -EINVAL;
  947. if (file->f_op->aio_fsync)
  948. ret = file->f_op->aio_fsync(iocb, 0);
  949. return ret;
  950. }
  951. static ssize_t aio_setup_vectored_rw(int type, struct kiocb *kiocb, bool compat)
  952. {
  953. ssize_t ret;
  954. #ifdef CONFIG_COMPAT
  955. if (compat)
  956. ret = compat_rw_copy_check_uvector(type,
  957. (struct compat_iovec __user *)kiocb->ki_buf,
  958. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  959. &kiocb->ki_iovec);
  960. else
  961. #endif
  962. ret = rw_copy_check_uvector(type,
  963. (struct iovec __user *)kiocb->ki_buf,
  964. kiocb->ki_nbytes, 1, &kiocb->ki_inline_vec,
  965. &kiocb->ki_iovec);
  966. if (ret < 0)
  967. goto out;
  968. ret = rw_verify_area(type, kiocb->ki_filp, &kiocb->ki_pos, ret);
  969. if (ret < 0)
  970. goto out;
  971. kiocb->ki_nr_segs = kiocb->ki_nbytes;
  972. kiocb->ki_cur_seg = 0;
  973. /* ki_nbytes/left now reflect bytes instead of segs */
  974. kiocb->ki_nbytes = ret;
  975. kiocb->ki_left = ret;
  976. ret = 0;
  977. out:
  978. return ret;
  979. }
  980. static ssize_t aio_setup_single_vector(int type, struct file * file, struct kiocb *kiocb)
  981. {
  982. int bytes;
  983. bytes = rw_verify_area(type, file, &kiocb->ki_pos, kiocb->ki_left);
  984. if (bytes < 0)
  985. return bytes;
  986. kiocb->ki_iovec = &kiocb->ki_inline_vec;
  987. kiocb->ki_iovec->iov_base = kiocb->ki_buf;
  988. kiocb->ki_iovec->iov_len = bytes;
  989. kiocb->ki_nr_segs = 1;
  990. kiocb->ki_cur_seg = 0;
  991. return 0;
  992. }
  993. /*
  994. * aio_setup_iocb:
  995. * Performs the initial checks and aio retry method
  996. * setup for the kiocb at the time of io submission.
  997. */
  998. static ssize_t aio_setup_iocb(struct kiocb *kiocb, bool compat)
  999. {
  1000. struct file *file = kiocb->ki_filp;
  1001. ssize_t ret = 0;
  1002. switch (kiocb->ki_opcode) {
  1003. case IOCB_CMD_PREAD:
  1004. ret = -EBADF;
  1005. if (unlikely(!(file->f_mode & FMODE_READ)))
  1006. break;
  1007. ret = -EFAULT;
  1008. if (unlikely(!access_ok(VERIFY_WRITE, kiocb->ki_buf,
  1009. kiocb->ki_left)))
  1010. break;
  1011. ret = aio_setup_single_vector(READ, file, kiocb);
  1012. if (ret)
  1013. break;
  1014. ret = -EINVAL;
  1015. if (file->f_op->aio_read)
  1016. kiocb->ki_retry = aio_rw_vect_retry;
  1017. break;
  1018. case IOCB_CMD_PWRITE:
  1019. ret = -EBADF;
  1020. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1021. break;
  1022. ret = -EFAULT;
  1023. if (unlikely(!access_ok(VERIFY_READ, kiocb->ki_buf,
  1024. kiocb->ki_left)))
  1025. break;
  1026. ret = aio_setup_single_vector(WRITE, file, kiocb);
  1027. if (ret)
  1028. break;
  1029. ret = -EINVAL;
  1030. if (file->f_op->aio_write)
  1031. kiocb->ki_retry = aio_rw_vect_retry;
  1032. break;
  1033. case IOCB_CMD_PREADV:
  1034. ret = -EBADF;
  1035. if (unlikely(!(file->f_mode & FMODE_READ)))
  1036. break;
  1037. ret = aio_setup_vectored_rw(READ, kiocb, compat);
  1038. if (ret)
  1039. break;
  1040. ret = -EINVAL;
  1041. if (file->f_op->aio_read)
  1042. kiocb->ki_retry = aio_rw_vect_retry;
  1043. break;
  1044. case IOCB_CMD_PWRITEV:
  1045. ret = -EBADF;
  1046. if (unlikely(!(file->f_mode & FMODE_WRITE)))
  1047. break;
  1048. ret = aio_setup_vectored_rw(WRITE, kiocb, compat);
  1049. if (ret)
  1050. break;
  1051. ret = -EINVAL;
  1052. if (file->f_op->aio_write)
  1053. kiocb->ki_retry = aio_rw_vect_retry;
  1054. break;
  1055. case IOCB_CMD_FDSYNC:
  1056. ret = -EINVAL;
  1057. if (file->f_op->aio_fsync)
  1058. kiocb->ki_retry = aio_fdsync;
  1059. break;
  1060. case IOCB_CMD_FSYNC:
  1061. ret = -EINVAL;
  1062. if (file->f_op->aio_fsync)
  1063. kiocb->ki_retry = aio_fsync;
  1064. break;
  1065. default:
  1066. pr_debug("EINVAL: no operation provided\n");
  1067. ret = -EINVAL;
  1068. }
  1069. if (!kiocb->ki_retry)
  1070. return ret;
  1071. return 0;
  1072. }
  1073. static int io_submit_one(struct kioctx *ctx, struct iocb __user *user_iocb,
  1074. struct iocb *iocb, struct kiocb_batch *batch,
  1075. bool compat)
  1076. {
  1077. struct kiocb *req;
  1078. struct file *file;
  1079. ssize_t ret;
  1080. /* enforce forwards compatibility on users */
  1081. if (unlikely(iocb->aio_reserved1 || iocb->aio_reserved2)) {
  1082. pr_debug("EINVAL: reserve field set\n");
  1083. return -EINVAL;
  1084. }
  1085. /* prevent overflows */
  1086. if (unlikely(
  1087. (iocb->aio_buf != (unsigned long)iocb->aio_buf) ||
  1088. (iocb->aio_nbytes != (size_t)iocb->aio_nbytes) ||
  1089. ((ssize_t)iocb->aio_nbytes < 0)
  1090. )) {
  1091. pr_debug("EINVAL: io_submit: overflow check\n");
  1092. return -EINVAL;
  1093. }
  1094. file = fget(iocb->aio_fildes);
  1095. if (unlikely(!file))
  1096. return -EBADF;
  1097. req = aio_get_req(ctx, batch); /* returns with 2 references to req */
  1098. if (unlikely(!req)) {
  1099. fput(file);
  1100. return -EAGAIN;
  1101. }
  1102. req->ki_filp = file;
  1103. if (iocb->aio_flags & IOCB_FLAG_RESFD) {
  1104. /*
  1105. * If the IOCB_FLAG_RESFD flag of aio_flags is set, get an
  1106. * instance of the file* now. The file descriptor must be
  1107. * an eventfd() fd, and will be signaled for each completed
  1108. * event using the eventfd_signal() function.
  1109. */
  1110. req->ki_eventfd = eventfd_ctx_fdget((int) iocb->aio_resfd);
  1111. if (IS_ERR(req->ki_eventfd)) {
  1112. ret = PTR_ERR(req->ki_eventfd);
  1113. req->ki_eventfd = NULL;
  1114. goto out_put_req;
  1115. }
  1116. }
  1117. ret = put_user(req->ki_key, &user_iocb->aio_key);
  1118. if (unlikely(ret)) {
  1119. pr_debug("EFAULT: aio_key\n");
  1120. goto out_put_req;
  1121. }
  1122. req->ki_obj.user = user_iocb;
  1123. req->ki_user_data = iocb->aio_data;
  1124. req->ki_pos = iocb->aio_offset;
  1125. req->ki_buf = (char __user *)(unsigned long)iocb->aio_buf;
  1126. req->ki_left = req->ki_nbytes = iocb->aio_nbytes;
  1127. req->ki_opcode = iocb->aio_lio_opcode;
  1128. ret = aio_setup_iocb(req, compat);
  1129. if (ret)
  1130. goto out_put_req;
  1131. spin_lock_irq(&ctx->ctx_lock);
  1132. /*
  1133. * We could have raced with io_destroy() and are currently holding a
  1134. * reference to ctx which should be destroyed. We cannot submit IO
  1135. * since ctx gets freed as soon as io_submit() puts its reference. The
  1136. * check here is reliable: io_destroy() sets ctx->dead before waiting
  1137. * for outstanding IO and the barrier between these two is realized by
  1138. * unlock of mm->ioctx_lock and lock of ctx->ctx_lock. Analogously we
  1139. * increment ctx->reqs_active before checking for ctx->dead and the
  1140. * barrier is realized by unlock and lock of ctx->ctx_lock. Thus if we
  1141. * don't see ctx->dead set here, io_destroy() waits for our IO to
  1142. * finish.
  1143. */
  1144. if (ctx->dead)
  1145. ret = -EINVAL;
  1146. spin_unlock_irq(&ctx->ctx_lock);
  1147. if (ret)
  1148. goto out_put_req;
  1149. if (unlikely(kiocbIsCancelled(req)))
  1150. ret = -EINTR;
  1151. else
  1152. ret = req->ki_retry(req);
  1153. if (ret != -EIOCBQUEUED) {
  1154. /*
  1155. * There's no easy way to restart the syscall since other AIO's
  1156. * may be already running. Just fail this IO with EINTR.
  1157. */
  1158. if (unlikely(ret == -ERESTARTSYS || ret == -ERESTARTNOINTR ||
  1159. ret == -ERESTARTNOHAND ||
  1160. ret == -ERESTART_RESTARTBLOCK))
  1161. ret = -EINTR;
  1162. aio_complete(req, ret, 0);
  1163. }
  1164. aio_put_req(req); /* drop extra ref to req */
  1165. return 0;
  1166. out_put_req:
  1167. aio_put_req(req); /* drop extra ref to req */
  1168. aio_put_req(req); /* drop i/o ref to req */
  1169. return ret;
  1170. }
  1171. long do_io_submit(aio_context_t ctx_id, long nr,
  1172. struct iocb __user *__user *iocbpp, bool compat)
  1173. {
  1174. struct kioctx *ctx;
  1175. long ret = 0;
  1176. int i = 0;
  1177. struct blk_plug plug;
  1178. struct kiocb_batch batch;
  1179. if (unlikely(nr < 0))
  1180. return -EINVAL;
  1181. if (unlikely(nr > LONG_MAX/sizeof(*iocbpp)))
  1182. nr = LONG_MAX/sizeof(*iocbpp);
  1183. if (unlikely(!access_ok(VERIFY_READ, iocbpp, (nr*sizeof(*iocbpp)))))
  1184. return -EFAULT;
  1185. ctx = lookup_ioctx(ctx_id);
  1186. if (unlikely(!ctx)) {
  1187. pr_debug("EINVAL: invalid context id\n");
  1188. return -EINVAL;
  1189. }
  1190. kiocb_batch_init(&batch, nr);
  1191. blk_start_plug(&plug);
  1192. /*
  1193. * AKPM: should this return a partial result if some of the IOs were
  1194. * successfully submitted?
  1195. */
  1196. for (i=0; i<nr; i++) {
  1197. struct iocb __user *user_iocb;
  1198. struct iocb tmp;
  1199. if (unlikely(__get_user(user_iocb, iocbpp + i))) {
  1200. ret = -EFAULT;
  1201. break;
  1202. }
  1203. if (unlikely(copy_from_user(&tmp, user_iocb, sizeof(tmp)))) {
  1204. ret = -EFAULT;
  1205. break;
  1206. }
  1207. ret = io_submit_one(ctx, user_iocb, &tmp, &batch, compat);
  1208. if (ret)
  1209. break;
  1210. }
  1211. blk_finish_plug(&plug);
  1212. kiocb_batch_free(ctx, &batch);
  1213. put_ioctx(ctx);
  1214. return i ? i : ret;
  1215. }
  1216. /* sys_io_submit:
  1217. * Queue the nr iocbs pointed to by iocbpp for processing. Returns
  1218. * the number of iocbs queued. May return -EINVAL if the aio_context
  1219. * specified by ctx_id is invalid, if nr is < 0, if the iocb at
  1220. * *iocbpp[0] is not properly initialized, if the operation specified
  1221. * is invalid for the file descriptor in the iocb. May fail with
  1222. * -EFAULT if any of the data structures point to invalid data. May
  1223. * fail with -EBADF if the file descriptor specified in the first
  1224. * iocb is invalid. May fail with -EAGAIN if insufficient resources
  1225. * are available to queue any iocbs. Will return 0 if nr is 0. Will
  1226. * fail with -ENOSYS if not implemented.
  1227. */
  1228. SYSCALL_DEFINE3(io_submit, aio_context_t, ctx_id, long, nr,
  1229. struct iocb __user * __user *, iocbpp)
  1230. {
  1231. return do_io_submit(ctx_id, nr, iocbpp, 0);
  1232. }
  1233. /* lookup_kiocb
  1234. * Finds a given iocb for cancellation.
  1235. */
  1236. static struct kiocb *lookup_kiocb(struct kioctx *ctx, struct iocb __user *iocb,
  1237. u32 key)
  1238. {
  1239. struct list_head *pos;
  1240. assert_spin_locked(&ctx->ctx_lock);
  1241. /* TODO: use a hash or array, this sucks. */
  1242. list_for_each(pos, &ctx->active_reqs) {
  1243. struct kiocb *kiocb = list_kiocb(pos);
  1244. if (kiocb->ki_obj.user == iocb && kiocb->ki_key == key)
  1245. return kiocb;
  1246. }
  1247. return NULL;
  1248. }
  1249. /* sys_io_cancel:
  1250. * Attempts to cancel an iocb previously passed to io_submit. If
  1251. * the operation is successfully cancelled, the resulting event is
  1252. * copied into the memory pointed to by result without being placed
  1253. * into the completion queue and 0 is returned. May fail with
  1254. * -EFAULT if any of the data structures pointed to are invalid.
  1255. * May fail with -EINVAL if aio_context specified by ctx_id is
  1256. * invalid. May fail with -EAGAIN if the iocb specified was not
  1257. * cancelled. Will fail with -ENOSYS if not implemented.
  1258. */
  1259. SYSCALL_DEFINE3(io_cancel, aio_context_t, ctx_id, struct iocb __user *, iocb,
  1260. struct io_event __user *, result)
  1261. {
  1262. struct io_event res;
  1263. struct kioctx *ctx;
  1264. struct kiocb *kiocb;
  1265. u32 key;
  1266. int ret;
  1267. ret = get_user(key, &iocb->aio_key);
  1268. if (unlikely(ret))
  1269. return -EFAULT;
  1270. ctx = lookup_ioctx(ctx_id);
  1271. if (unlikely(!ctx))
  1272. return -EINVAL;
  1273. spin_lock_irq(&ctx->ctx_lock);
  1274. kiocb = lookup_kiocb(ctx, iocb, key);
  1275. if (kiocb)
  1276. ret = kiocb_cancel(ctx, kiocb, &res);
  1277. else
  1278. ret = -EINVAL;
  1279. spin_unlock_irq(&ctx->ctx_lock);
  1280. if (!ret) {
  1281. /* Cancellation succeeded -- copy the result
  1282. * into the user's buffer.
  1283. */
  1284. if (copy_to_user(result, &res, sizeof(res)))
  1285. ret = -EFAULT;
  1286. }
  1287. put_ioctx(ctx);
  1288. return ret;
  1289. }
  1290. /* io_getevents:
  1291. * Attempts to read at least min_nr events and up to nr events from
  1292. * the completion queue for the aio_context specified by ctx_id. If
  1293. * it succeeds, the number of read events is returned. May fail with
  1294. * -EINVAL if ctx_id is invalid, if min_nr is out of range, if nr is
  1295. * out of range, if timeout is out of range. May fail with -EFAULT
  1296. * if any of the memory specified is invalid. May return 0 or
  1297. * < min_nr if the timeout specified by timeout has elapsed
  1298. * before sufficient events are available, where timeout == NULL
  1299. * specifies an infinite timeout. Note that the timeout pointed to by
  1300. * timeout is relative and will be updated if not NULL and the
  1301. * operation blocks. Will fail with -ENOSYS if not implemented.
  1302. */
  1303. SYSCALL_DEFINE5(io_getevents, aio_context_t, ctx_id,
  1304. long, min_nr,
  1305. long, nr,
  1306. struct io_event __user *, events,
  1307. struct timespec __user *, timeout)
  1308. {
  1309. struct kioctx *ioctx = lookup_ioctx(ctx_id);
  1310. long ret = -EINVAL;
  1311. if (likely(ioctx)) {
  1312. if (likely(min_nr <= nr && min_nr >= 0))
  1313. ret = read_events(ioctx, min_nr, nr, events, timeout);
  1314. put_ioctx(ioctx);
  1315. }
  1316. return ret;
  1317. }