direct-io.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /*
  2. * fs/direct-io.c
  3. *
  4. * Copyright (C) 2002, Linus Torvalds.
  5. *
  6. * O_DIRECT
  7. *
  8. * 04Jul2002 Andrew Morton
  9. * Initial version
  10. * 11Sep2002 janetinc@us.ibm.com
  11. * added readv/writev support.
  12. * 29Oct2002 Andrew Morton
  13. * rewrote bio_add_page() support.
  14. * 30Oct2002 pbadari@us.ibm.com
  15. * added support for non-aligned IO.
  16. * 06Nov2002 pbadari@us.ibm.com
  17. * added asynchronous IO support.
  18. * 21Jul2003 nathans@sgi.com
  19. * added IO completion notifier.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/types.h>
  24. #include <linux/fs.h>
  25. #include <linux/mm.h>
  26. #include <linux/slab.h>
  27. #include <linux/highmem.h>
  28. #include <linux/pagemap.h>
  29. #include <linux/task_io_accounting_ops.h>
  30. #include <linux/bio.h>
  31. #include <linux/wait.h>
  32. #include <linux/err.h>
  33. #include <linux/blkdev.h>
  34. #include <linux/buffer_head.h>
  35. #include <linux/rwsem.h>
  36. #include <linux/uio.h>
  37. #include <linux/atomic.h>
  38. #include <linux/prefetch.h>
  39. #include <linux/aio.h>
  40. /*
  41. * How many user pages to map in one call to get_user_pages(). This determines
  42. * the size of a structure in the slab cache
  43. */
  44. #define DIO_PAGES 64
  45. /*
  46. * This code generally works in units of "dio_blocks". A dio_block is
  47. * somewhere between the hard sector size and the filesystem block size. it
  48. * is determined on a per-invocation basis. When talking to the filesystem
  49. * we need to convert dio_blocks to fs_blocks by scaling the dio_block quantity
  50. * down by dio->blkfactor. Similarly, fs-blocksize quantities are converted
  51. * to bio_block quantities by shifting left by blkfactor.
  52. *
  53. * If blkfactor is zero then the user's request was aligned to the filesystem's
  54. * blocksize.
  55. */
  56. /* dio_state only used in the submission path */
  57. struct dio_submit {
  58. struct bio *bio; /* bio under assembly */
  59. unsigned blkbits; /* doesn't change */
  60. unsigned blkfactor; /* When we're using an alignment which
  61. is finer than the filesystem's soft
  62. blocksize, this specifies how much
  63. finer. blkfactor=2 means 1/4-block
  64. alignment. Does not change */
  65. unsigned start_zero_done; /* flag: sub-blocksize zeroing has
  66. been performed at the start of a
  67. write */
  68. int pages_in_io; /* approximate total IO pages */
  69. size_t size; /* total request size (doesn't change)*/
  70. sector_t block_in_file; /* Current offset into the underlying
  71. file in dio_block units. */
  72. unsigned blocks_available; /* At block_in_file. changes */
  73. int reap_counter; /* rate limit reaping */
  74. sector_t final_block_in_request;/* doesn't change */
  75. unsigned first_block_in_page; /* doesn't change, Used only once */
  76. int boundary; /* prev block is at a boundary */
  77. get_block_t *get_block; /* block mapping function */
  78. dio_submit_t *submit_io; /* IO submition function */
  79. loff_t logical_offset_in_bio; /* current first logical block in bio */
  80. sector_t final_block_in_bio; /* current final block in bio + 1 */
  81. sector_t next_block_for_io; /* next block to be put under IO,
  82. in dio_blocks units */
  83. /*
  84. * Deferred addition of a page to the dio. These variables are
  85. * private to dio_send_cur_page(), submit_page_section() and
  86. * dio_bio_add_page().
  87. */
  88. struct page *cur_page; /* The page */
  89. unsigned cur_page_offset; /* Offset into it, in bytes */
  90. unsigned cur_page_len; /* Nr of bytes at cur_page_offset */
  91. sector_t cur_page_block; /* Where it starts */
  92. loff_t cur_page_fs_offset; /* Offset in file */
  93. /*
  94. * Page fetching state. These variables belong to dio_refill_pages().
  95. */
  96. int curr_page; /* changes */
  97. int total_pages; /* doesn't change */
  98. unsigned long curr_user_address;/* changes */
  99. /*
  100. * Page queue. These variables belong to dio_refill_pages() and
  101. * dio_get_page().
  102. */
  103. unsigned head; /* next page to process */
  104. unsigned tail; /* last valid page + 1 */
  105. };
  106. /* dio_state communicated between submission path and end_io */
  107. struct dio {
  108. int flags; /* doesn't change */
  109. int rw;
  110. struct inode *inode;
  111. loff_t i_size; /* i_size when submitted */
  112. dio_iodone_t *end_io; /* IO completion function */
  113. void *private; /* copy from map_bh.b_private */
  114. /* BIO completion state */
  115. spinlock_t bio_lock; /* protects BIO fields below */
  116. int page_errors; /* errno from get_user_pages() */
  117. int is_async; /* is IO async ? */
  118. int io_error; /* IO error in completion path */
  119. unsigned long refcount; /* direct_io_worker() and bios */
  120. struct bio *bio_list; /* singly linked via bi_private */
  121. struct task_struct *waiter; /* waiting task (NULL if none) */
  122. /* AIO related stuff */
  123. struct kiocb *iocb; /* kiocb */
  124. ssize_t result; /* IO result */
  125. /*
  126. * pages[] (and any fields placed after it) are not zeroed out at
  127. * allocation time. Don't add new fields after pages[] unless you
  128. * wish that they not be zeroed.
  129. */
  130. struct page *pages[DIO_PAGES]; /* page buffer */
  131. } ____cacheline_aligned_in_smp;
  132. static struct kmem_cache *dio_cache __read_mostly;
  133. /*
  134. * How many pages are in the queue?
  135. */
  136. static inline unsigned dio_pages_present(struct dio_submit *sdio)
  137. {
  138. return sdio->tail - sdio->head;
  139. }
  140. /*
  141. * Go grab and pin some userspace pages. Typically we'll get 64 at a time.
  142. */
  143. static inline int dio_refill_pages(struct dio *dio, struct dio_submit *sdio)
  144. {
  145. int ret;
  146. int nr_pages;
  147. nr_pages = min(sdio->total_pages - sdio->curr_page, DIO_PAGES);
  148. ret = get_user_pages_fast(
  149. sdio->curr_user_address, /* Where from? */
  150. nr_pages, /* How many pages? */
  151. dio->rw == READ, /* Write to memory? */
  152. &dio->pages[0]); /* Put results here */
  153. if (ret < 0 && sdio->blocks_available && (dio->rw & WRITE)) {
  154. struct page *page = ZERO_PAGE(0);
  155. /*
  156. * A memory fault, but the filesystem has some outstanding
  157. * mapped blocks. We need to use those blocks up to avoid
  158. * leaking stale data in the file.
  159. */
  160. if (dio->page_errors == 0)
  161. dio->page_errors = ret;
  162. page_cache_get(page);
  163. dio->pages[0] = page;
  164. sdio->head = 0;
  165. sdio->tail = 1;
  166. ret = 0;
  167. goto out;
  168. }
  169. if (ret >= 0) {
  170. sdio->curr_user_address += ret * PAGE_SIZE;
  171. sdio->curr_page += ret;
  172. sdio->head = 0;
  173. sdio->tail = ret;
  174. ret = 0;
  175. }
  176. out:
  177. return ret;
  178. }
  179. /*
  180. * Get another userspace page. Returns an ERR_PTR on error. Pages are
  181. * buffered inside the dio so that we can call get_user_pages() against a
  182. * decent number of pages, less frequently. To provide nicer use of the
  183. * L1 cache.
  184. */
  185. static inline struct page *dio_get_page(struct dio *dio,
  186. struct dio_submit *sdio)
  187. {
  188. if (dio_pages_present(sdio) == 0) {
  189. int ret;
  190. ret = dio_refill_pages(dio, sdio);
  191. if (ret)
  192. return ERR_PTR(ret);
  193. BUG_ON(dio_pages_present(sdio) == 0);
  194. }
  195. return dio->pages[sdio->head++];
  196. }
  197. /**
  198. * dio_complete() - called when all DIO BIO I/O has been completed
  199. * @offset: the byte offset in the file of the completed operation
  200. *
  201. * This releases locks as dictated by the locking type, lets interested parties
  202. * know that a DIO operation has completed, and calculates the resulting return
  203. * code for the operation.
  204. *
  205. * It lets the filesystem know if it registered an interest earlier via
  206. * get_block. Pass the private field of the map buffer_head so that
  207. * filesystems can use it to hold additional state between get_block calls and
  208. * dio_complete.
  209. */
  210. static ssize_t dio_complete(struct dio *dio, loff_t offset, ssize_t ret, bool is_async)
  211. {
  212. ssize_t transferred = 0;
  213. /*
  214. * AIO submission can race with bio completion to get here while
  215. * expecting to have the last io completed by bio completion.
  216. * In that case -EIOCBQUEUED is in fact not an error we want
  217. * to preserve through this call.
  218. */
  219. if (ret == -EIOCBQUEUED)
  220. ret = 0;
  221. if (dio->result) {
  222. transferred = dio->result;
  223. /* Check for short read case */
  224. if ((dio->rw == READ) && ((offset + transferred) > dio->i_size))
  225. transferred = dio->i_size - offset;
  226. }
  227. if (ret == 0)
  228. ret = dio->page_errors;
  229. if (ret == 0)
  230. ret = dio->io_error;
  231. if (ret == 0)
  232. ret = transferred;
  233. if (dio->end_io && dio->result) {
  234. dio->end_io(dio->iocb, offset, transferred,
  235. dio->private, ret, is_async);
  236. } else {
  237. inode_dio_done(dio->inode);
  238. if (is_async)
  239. aio_complete(dio->iocb, ret, 0);
  240. }
  241. return ret;
  242. }
  243. static int dio_bio_complete(struct dio *dio, struct bio *bio);
  244. /*
  245. * Asynchronous IO callback.
  246. */
  247. static void dio_bio_end_aio(struct bio *bio, int error)
  248. {
  249. struct dio *dio = bio->bi_private;
  250. unsigned long remaining;
  251. unsigned long flags;
  252. /* cleanup the bio */
  253. dio_bio_complete(dio, bio);
  254. spin_lock_irqsave(&dio->bio_lock, flags);
  255. remaining = --dio->refcount;
  256. if (remaining == 1 && dio->waiter)
  257. wake_up_process(dio->waiter);
  258. spin_unlock_irqrestore(&dio->bio_lock, flags);
  259. if (remaining == 0) {
  260. dio_complete(dio, dio->iocb->ki_pos, 0, true);
  261. kmem_cache_free(dio_cache, dio);
  262. }
  263. }
  264. /*
  265. * The BIO completion handler simply queues the BIO up for the process-context
  266. * handler.
  267. *
  268. * During I/O bi_private points at the dio. After I/O, bi_private is used to
  269. * implement a singly-linked list of completed BIOs, at dio->bio_list.
  270. */
  271. static void dio_bio_end_io(struct bio *bio, int error)
  272. {
  273. struct dio *dio = bio->bi_private;
  274. unsigned long flags;
  275. spin_lock_irqsave(&dio->bio_lock, flags);
  276. bio->bi_private = dio->bio_list;
  277. dio->bio_list = bio;
  278. if (--dio->refcount == 1 && dio->waiter)
  279. wake_up_process(dio->waiter);
  280. spin_unlock_irqrestore(&dio->bio_lock, flags);
  281. }
  282. /**
  283. * dio_end_io - handle the end io action for the given bio
  284. * @bio: The direct io bio thats being completed
  285. * @error: Error if there was one
  286. *
  287. * This is meant to be called by any filesystem that uses their own dio_submit_t
  288. * so that the DIO specific endio actions are dealt with after the filesystem
  289. * has done it's completion work.
  290. */
  291. void dio_end_io(struct bio *bio, int error)
  292. {
  293. struct dio *dio = bio->bi_private;
  294. if (dio->is_async)
  295. dio_bio_end_aio(bio, error);
  296. else
  297. dio_bio_end_io(bio, error);
  298. }
  299. EXPORT_SYMBOL_GPL(dio_end_io);
  300. static inline void
  301. dio_bio_alloc(struct dio *dio, struct dio_submit *sdio,
  302. struct block_device *bdev,
  303. sector_t first_sector, int nr_vecs)
  304. {
  305. struct bio *bio;
  306. /*
  307. * bio_alloc() is guaranteed to return a bio when called with
  308. * __GFP_WAIT and we request a valid number of vectors.
  309. */
  310. bio = bio_alloc(GFP_KERNEL, nr_vecs);
  311. bio->bi_bdev = bdev;
  312. bio->bi_sector = first_sector;
  313. if (dio->is_async)
  314. bio->bi_end_io = dio_bio_end_aio;
  315. else
  316. bio->bi_end_io = dio_bio_end_io;
  317. sdio->bio = bio;
  318. sdio->logical_offset_in_bio = sdio->cur_page_fs_offset;
  319. }
  320. /*
  321. * In the AIO read case we speculatively dirty the pages before starting IO.
  322. * During IO completion, any of these pages which happen to have been written
  323. * back will be redirtied by bio_check_pages_dirty().
  324. *
  325. * bios hold a dio reference between submit_bio and ->end_io.
  326. */
  327. static inline void dio_bio_submit(struct dio *dio, struct dio_submit *sdio)
  328. {
  329. struct bio *bio = sdio->bio;
  330. unsigned long flags;
  331. bio->bi_private = dio;
  332. spin_lock_irqsave(&dio->bio_lock, flags);
  333. dio->refcount++;
  334. spin_unlock_irqrestore(&dio->bio_lock, flags);
  335. if (dio->is_async && dio->rw == READ)
  336. bio_set_pages_dirty(bio);
  337. if (sdio->submit_io)
  338. sdio->submit_io(dio->rw, bio, dio->inode,
  339. sdio->logical_offset_in_bio);
  340. else
  341. submit_bio(dio->rw, bio);
  342. sdio->bio = NULL;
  343. sdio->boundary = 0;
  344. sdio->logical_offset_in_bio = 0;
  345. }
  346. /*
  347. * Release any resources in case of a failure
  348. */
  349. static inline void dio_cleanup(struct dio *dio, struct dio_submit *sdio)
  350. {
  351. while (dio_pages_present(sdio))
  352. page_cache_release(dio_get_page(dio, sdio));
  353. }
  354. /*
  355. * Wait for the next BIO to complete. Remove it and return it. NULL is
  356. * returned once all BIOs have been completed. This must only be called once
  357. * all bios have been issued so that dio->refcount can only decrease. This
  358. * requires that that the caller hold a reference on the dio.
  359. */
  360. static struct bio *dio_await_one(struct dio *dio)
  361. {
  362. unsigned long flags;
  363. struct bio *bio = NULL;
  364. spin_lock_irqsave(&dio->bio_lock, flags);
  365. /*
  366. * Wait as long as the list is empty and there are bios in flight. bio
  367. * completion drops the count, maybe adds to the list, and wakes while
  368. * holding the bio_lock so we don't need set_current_state()'s barrier
  369. * and can call it after testing our condition.
  370. */
  371. while (dio->refcount > 1 && dio->bio_list == NULL) {
  372. __set_current_state(TASK_UNINTERRUPTIBLE);
  373. dio->waiter = current;
  374. spin_unlock_irqrestore(&dio->bio_lock, flags);
  375. io_schedule();
  376. /* wake up sets us TASK_RUNNING */
  377. spin_lock_irqsave(&dio->bio_lock, flags);
  378. dio->waiter = NULL;
  379. }
  380. if (dio->bio_list) {
  381. bio = dio->bio_list;
  382. dio->bio_list = bio->bi_private;
  383. }
  384. spin_unlock_irqrestore(&dio->bio_lock, flags);
  385. return bio;
  386. }
  387. /*
  388. * Process one completed BIO. No locks are held.
  389. */
  390. static int dio_bio_complete(struct dio *dio, struct bio *bio)
  391. {
  392. const int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
  393. struct bio_vec *bvec;
  394. unsigned i;
  395. if (!uptodate)
  396. dio->io_error = -EIO;
  397. if (dio->is_async && dio->rw == READ) {
  398. bio_check_pages_dirty(bio); /* transfers ownership */
  399. } else {
  400. bio_for_each_segment_all(bvec, bio, i) {
  401. struct page *page = bvec->bv_page;
  402. if (dio->rw == READ && !PageCompound(page))
  403. set_page_dirty_lock(page);
  404. page_cache_release(page);
  405. }
  406. bio_put(bio);
  407. }
  408. return uptodate ? 0 : -EIO;
  409. }
  410. /*
  411. * Wait on and process all in-flight BIOs. This must only be called once
  412. * all bios have been issued so that the refcount can only decrease.
  413. * This just waits for all bios to make it through dio_bio_complete. IO
  414. * errors are propagated through dio->io_error and should be propagated via
  415. * dio_complete().
  416. */
  417. static void dio_await_completion(struct dio *dio)
  418. {
  419. struct bio *bio;
  420. do {
  421. bio = dio_await_one(dio);
  422. if (bio)
  423. dio_bio_complete(dio, bio);
  424. } while (bio);
  425. }
  426. /*
  427. * A really large O_DIRECT read or write can generate a lot of BIOs. So
  428. * to keep the memory consumption sane we periodically reap any completed BIOs
  429. * during the BIO generation phase.
  430. *
  431. * This also helps to limit the peak amount of pinned userspace memory.
  432. */
  433. static inline int dio_bio_reap(struct dio *dio, struct dio_submit *sdio)
  434. {
  435. int ret = 0;
  436. if (sdio->reap_counter++ >= 64) {
  437. while (dio->bio_list) {
  438. unsigned long flags;
  439. struct bio *bio;
  440. int ret2;
  441. spin_lock_irqsave(&dio->bio_lock, flags);
  442. bio = dio->bio_list;
  443. dio->bio_list = bio->bi_private;
  444. spin_unlock_irqrestore(&dio->bio_lock, flags);
  445. ret2 = dio_bio_complete(dio, bio);
  446. if (ret == 0)
  447. ret = ret2;
  448. }
  449. sdio->reap_counter = 0;
  450. }
  451. return ret;
  452. }
  453. /*
  454. * Call into the fs to map some more disk blocks. We record the current number
  455. * of available blocks at sdio->blocks_available. These are in units of the
  456. * fs blocksize, (1 << inode->i_blkbits).
  457. *
  458. * The fs is allowed to map lots of blocks at once. If it wants to do that,
  459. * it uses the passed inode-relative block number as the file offset, as usual.
  460. *
  461. * get_block() is passed the number of i_blkbits-sized blocks which direct_io
  462. * has remaining to do. The fs should not map more than this number of blocks.
  463. *
  464. * If the fs has mapped a lot of blocks, it should populate bh->b_size to
  465. * indicate how much contiguous disk space has been made available at
  466. * bh->b_blocknr.
  467. *
  468. * If *any* of the mapped blocks are new, then the fs must set buffer_new().
  469. * This isn't very efficient...
  470. *
  471. * In the case of filesystem holes: the fs may return an arbitrarily-large
  472. * hole by returning an appropriate value in b_size and by clearing
  473. * buffer_mapped(). However the direct-io code will only process holes one
  474. * block at a time - it will repeatedly call get_block() as it walks the hole.
  475. */
  476. static int get_more_blocks(struct dio *dio, struct dio_submit *sdio,
  477. struct buffer_head *map_bh)
  478. {
  479. int ret;
  480. sector_t fs_startblk; /* Into file, in filesystem-sized blocks */
  481. sector_t fs_endblk; /* Into file, in filesystem-sized blocks */
  482. unsigned long fs_count; /* Number of filesystem-sized blocks */
  483. int create;
  484. unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor;
  485. /*
  486. * If there was a memory error and we've overwritten all the
  487. * mapped blocks then we can now return that memory error
  488. */
  489. ret = dio->page_errors;
  490. if (ret == 0) {
  491. BUG_ON(sdio->block_in_file >= sdio->final_block_in_request);
  492. fs_startblk = sdio->block_in_file >> sdio->blkfactor;
  493. fs_endblk = (sdio->final_block_in_request - 1) >>
  494. sdio->blkfactor;
  495. fs_count = fs_endblk - fs_startblk + 1;
  496. map_bh->b_state = 0;
  497. map_bh->b_size = fs_count << i_blkbits;
  498. /*
  499. * For writes inside i_size on a DIO_SKIP_HOLES filesystem we
  500. * forbid block creations: only overwrites are permitted.
  501. * We will return early to the caller once we see an
  502. * unmapped buffer head returned, and the caller will fall
  503. * back to buffered I/O.
  504. *
  505. * Otherwise the decision is left to the get_blocks method,
  506. * which may decide to handle it or also return an unmapped
  507. * buffer head.
  508. */
  509. create = dio->rw & WRITE;
  510. if (dio->flags & DIO_SKIP_HOLES) {
  511. if (sdio->block_in_file < (i_size_read(dio->inode) >>
  512. sdio->blkbits))
  513. create = 0;
  514. }
  515. ret = (*sdio->get_block)(dio->inode, fs_startblk,
  516. map_bh, create);
  517. /* Store for completion */
  518. dio->private = map_bh->b_private;
  519. }
  520. return ret;
  521. }
  522. /*
  523. * There is no bio. Make one now.
  524. */
  525. static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
  526. sector_t start_sector, struct buffer_head *map_bh)
  527. {
  528. sector_t sector;
  529. int ret, nr_pages;
  530. ret = dio_bio_reap(dio, sdio);
  531. if (ret)
  532. goto out;
  533. sector = start_sector << (sdio->blkbits - 9);
  534. nr_pages = min(sdio->pages_in_io, bio_get_nr_vecs(map_bh->b_bdev));
  535. nr_pages = min(nr_pages, BIO_MAX_PAGES);
  536. BUG_ON(nr_pages <= 0);
  537. dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
  538. sdio->boundary = 0;
  539. out:
  540. return ret;
  541. }
  542. /*
  543. * Attempt to put the current chunk of 'cur_page' into the current BIO. If
  544. * that was successful then update final_block_in_bio and take a ref against
  545. * the just-added page.
  546. *
  547. * Return zero on success. Non-zero means the caller needs to start a new BIO.
  548. */
  549. static inline int dio_bio_add_page(struct dio_submit *sdio)
  550. {
  551. int ret;
  552. ret = bio_add_page(sdio->bio, sdio->cur_page,
  553. sdio->cur_page_len, sdio->cur_page_offset);
  554. if (ret == sdio->cur_page_len) {
  555. /*
  556. * Decrement count only, if we are done with this page
  557. */
  558. if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE)
  559. sdio->pages_in_io--;
  560. page_cache_get(sdio->cur_page);
  561. sdio->final_block_in_bio = sdio->cur_page_block +
  562. (sdio->cur_page_len >> sdio->blkbits);
  563. ret = 0;
  564. } else {
  565. ret = 1;
  566. }
  567. return ret;
  568. }
  569. /*
  570. * Put cur_page under IO. The section of cur_page which is described by
  571. * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
  572. * starts on-disk at cur_page_block.
  573. *
  574. * We take a ref against the page here (on behalf of its presence in the bio).
  575. *
  576. * The caller of this function is responsible for removing cur_page from the
  577. * dio, and for dropping the refcount which came from that presence.
  578. */
  579. static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio,
  580. struct buffer_head *map_bh)
  581. {
  582. int ret = 0;
  583. if (sdio->bio) {
  584. loff_t cur_offset = sdio->cur_page_fs_offset;
  585. loff_t bio_next_offset = sdio->logical_offset_in_bio +
  586. sdio->bio->bi_size;
  587. /*
  588. * See whether this new request is contiguous with the old.
  589. *
  590. * Btrfs cannot handle having logically non-contiguous requests
  591. * submitted. For example if you have
  592. *
  593. * Logical: [0-4095][HOLE][8192-12287]
  594. * Physical: [0-4095] [4096-8191]
  595. *
  596. * We cannot submit those pages together as one BIO. So if our
  597. * current logical offset in the file does not equal what would
  598. * be the next logical offset in the bio, submit the bio we
  599. * have.
  600. */
  601. if (sdio->final_block_in_bio != sdio->cur_page_block ||
  602. cur_offset != bio_next_offset)
  603. dio_bio_submit(dio, sdio);
  604. }
  605. if (sdio->bio == NULL) {
  606. ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
  607. if (ret)
  608. goto out;
  609. }
  610. if (dio_bio_add_page(sdio) != 0) {
  611. dio_bio_submit(dio, sdio);
  612. ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
  613. if (ret == 0) {
  614. ret = dio_bio_add_page(sdio);
  615. BUG_ON(ret != 0);
  616. }
  617. }
  618. out:
  619. return ret;
  620. }
  621. /*
  622. * An autonomous function to put a chunk of a page under deferred IO.
  623. *
  624. * The caller doesn't actually know (or care) whether this piece of page is in
  625. * a BIO, or is under IO or whatever. We just take care of all possible
  626. * situations here. The separation between the logic of do_direct_IO() and
  627. * that of submit_page_section() is important for clarity. Please don't break.
  628. *
  629. * The chunk of page starts on-disk at blocknr.
  630. *
  631. * We perform deferred IO, by recording the last-submitted page inside our
  632. * private part of the dio structure. If possible, we just expand the IO
  633. * across that page here.
  634. *
  635. * If that doesn't work out then we put the old page into the bio and add this
  636. * page to the dio instead.
  637. */
  638. static inline int
  639. submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page,
  640. unsigned offset, unsigned len, sector_t blocknr,
  641. struct buffer_head *map_bh)
  642. {
  643. int ret = 0;
  644. if (dio->rw & WRITE) {
  645. /*
  646. * Read accounting is performed in submit_bio()
  647. */
  648. task_io_account_write(len);
  649. }
  650. /*
  651. * Can we just grow the current page's presence in the dio?
  652. */
  653. if (sdio->cur_page == page &&
  654. sdio->cur_page_offset + sdio->cur_page_len == offset &&
  655. sdio->cur_page_block +
  656. (sdio->cur_page_len >> sdio->blkbits) == blocknr) {
  657. sdio->cur_page_len += len;
  658. goto out;
  659. }
  660. /*
  661. * If there's a deferred page already there then send it.
  662. */
  663. if (sdio->cur_page) {
  664. ret = dio_send_cur_page(dio, sdio, map_bh);
  665. page_cache_release(sdio->cur_page);
  666. sdio->cur_page = NULL;
  667. if (ret)
  668. return ret;
  669. }
  670. page_cache_get(page); /* It is in dio */
  671. sdio->cur_page = page;
  672. sdio->cur_page_offset = offset;
  673. sdio->cur_page_len = len;
  674. sdio->cur_page_block = blocknr;
  675. sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits;
  676. out:
  677. /*
  678. * If sdio->boundary then we want to schedule the IO now to
  679. * avoid metadata seeks.
  680. */
  681. if (sdio->boundary) {
  682. ret = dio_send_cur_page(dio, sdio, map_bh);
  683. dio_bio_submit(dio, sdio);
  684. page_cache_release(sdio->cur_page);
  685. sdio->cur_page = NULL;
  686. }
  687. return ret;
  688. }
  689. /*
  690. * Clean any dirty buffers in the blockdev mapping which alias newly-created
  691. * file blocks. Only called for S_ISREG files - blockdevs do not set
  692. * buffer_new
  693. */
  694. static void clean_blockdev_aliases(struct dio *dio, struct buffer_head *map_bh)
  695. {
  696. unsigned i;
  697. unsigned nblocks;
  698. nblocks = map_bh->b_size >> dio->inode->i_blkbits;
  699. for (i = 0; i < nblocks; i++) {
  700. unmap_underlying_metadata(map_bh->b_bdev,
  701. map_bh->b_blocknr + i);
  702. }
  703. }
  704. /*
  705. * If we are not writing the entire block and get_block() allocated
  706. * the block for us, we need to fill-in the unused portion of the
  707. * block with zeros. This happens only if user-buffer, fileoffset or
  708. * io length is not filesystem block-size multiple.
  709. *
  710. * `end' is zero if we're doing the start of the IO, 1 at the end of the
  711. * IO.
  712. */
  713. static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio,
  714. int end, struct buffer_head *map_bh)
  715. {
  716. unsigned dio_blocks_per_fs_block;
  717. unsigned this_chunk_blocks; /* In dio_blocks */
  718. unsigned this_chunk_bytes;
  719. struct page *page;
  720. sdio->start_zero_done = 1;
  721. if (!sdio->blkfactor || !buffer_new(map_bh))
  722. return;
  723. dio_blocks_per_fs_block = 1 << sdio->blkfactor;
  724. this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1);
  725. if (!this_chunk_blocks)
  726. return;
  727. /*
  728. * We need to zero out part of an fs block. It is either at the
  729. * beginning or the end of the fs block.
  730. */
  731. if (end)
  732. this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
  733. this_chunk_bytes = this_chunk_blocks << sdio->blkbits;
  734. page = ZERO_PAGE(0);
  735. if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes,
  736. sdio->next_block_for_io, map_bh))
  737. return;
  738. sdio->next_block_for_io += this_chunk_blocks;
  739. }
  740. /*
  741. * Walk the user pages, and the file, mapping blocks to disk and generating
  742. * a sequence of (page,offset,len,block) mappings. These mappings are injected
  743. * into submit_page_section(), which takes care of the next stage of submission
  744. *
  745. * Direct IO against a blockdev is different from a file. Because we can
  746. * happily perform page-sized but 512-byte aligned IOs. It is important that
  747. * blockdev IO be able to have fine alignment and large sizes.
  748. *
  749. * So what we do is to permit the ->get_block function to populate bh.b_size
  750. * with the size of IO which is permitted at this offset and this i_blkbits.
  751. *
  752. * For best results, the blockdev should be set up with 512-byte i_blkbits and
  753. * it should set b_size to PAGE_SIZE or more inside get_block(). This gives
  754. * fine alignment but still allows this function to work in PAGE_SIZE units.
  755. */
  756. static int do_direct_IO(struct dio *dio, struct dio_submit *sdio,
  757. struct buffer_head *map_bh)
  758. {
  759. const unsigned blkbits = sdio->blkbits;
  760. const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
  761. struct page *page;
  762. unsigned block_in_page;
  763. int ret = 0;
  764. /* The I/O can start at any block offset within the first page */
  765. block_in_page = sdio->first_block_in_page;
  766. while (sdio->block_in_file < sdio->final_block_in_request) {
  767. page = dio_get_page(dio, sdio);
  768. if (IS_ERR(page)) {
  769. ret = PTR_ERR(page);
  770. goto out;
  771. }
  772. while (block_in_page < blocks_per_page) {
  773. unsigned offset_in_page = block_in_page << blkbits;
  774. unsigned this_chunk_bytes; /* # of bytes mapped */
  775. unsigned this_chunk_blocks; /* # of blocks */
  776. unsigned u;
  777. if (sdio->blocks_available == 0) {
  778. /*
  779. * Need to go and map some more disk
  780. */
  781. unsigned long blkmask;
  782. unsigned long dio_remainder;
  783. ret = get_more_blocks(dio, sdio, map_bh);
  784. if (ret) {
  785. page_cache_release(page);
  786. goto out;
  787. }
  788. if (!buffer_mapped(map_bh))
  789. goto do_holes;
  790. sdio->blocks_available =
  791. map_bh->b_size >> sdio->blkbits;
  792. sdio->next_block_for_io =
  793. map_bh->b_blocknr << sdio->blkfactor;
  794. if (buffer_new(map_bh))
  795. clean_blockdev_aliases(dio, map_bh);
  796. if (!sdio->blkfactor)
  797. goto do_holes;
  798. blkmask = (1 << sdio->blkfactor) - 1;
  799. dio_remainder = (sdio->block_in_file & blkmask);
  800. /*
  801. * If we are at the start of IO and that IO
  802. * starts partway into a fs-block,
  803. * dio_remainder will be non-zero. If the IO
  804. * is a read then we can simply advance the IO
  805. * cursor to the first block which is to be
  806. * read. But if the IO is a write and the
  807. * block was newly allocated we cannot do that;
  808. * the start of the fs block must be zeroed out
  809. * on-disk
  810. */
  811. if (!buffer_new(map_bh))
  812. sdio->next_block_for_io += dio_remainder;
  813. sdio->blocks_available -= dio_remainder;
  814. }
  815. do_holes:
  816. /* Handle holes */
  817. if (!buffer_mapped(map_bh)) {
  818. loff_t i_size_aligned;
  819. /* AKPM: eargh, -ENOTBLK is a hack */
  820. if (dio->rw & WRITE) {
  821. page_cache_release(page);
  822. return -ENOTBLK;
  823. }
  824. /*
  825. * Be sure to account for a partial block as the
  826. * last block in the file
  827. */
  828. i_size_aligned = ALIGN(i_size_read(dio->inode),
  829. 1 << blkbits);
  830. if (sdio->block_in_file >=
  831. i_size_aligned >> blkbits) {
  832. /* We hit eof */
  833. page_cache_release(page);
  834. goto out;
  835. }
  836. zero_user(page, block_in_page << blkbits,
  837. 1 << blkbits);
  838. sdio->block_in_file++;
  839. block_in_page++;
  840. goto next_block;
  841. }
  842. /*
  843. * If we're performing IO which has an alignment which
  844. * is finer than the underlying fs, go check to see if
  845. * we must zero out the start of this block.
  846. */
  847. if (unlikely(sdio->blkfactor && !sdio->start_zero_done))
  848. dio_zero_block(dio, sdio, 0, map_bh);
  849. /*
  850. * Work out, in this_chunk_blocks, how much disk we
  851. * can add to this page
  852. */
  853. this_chunk_blocks = sdio->blocks_available;
  854. u = (PAGE_SIZE - offset_in_page) >> blkbits;
  855. if (this_chunk_blocks > u)
  856. this_chunk_blocks = u;
  857. u = sdio->final_block_in_request - sdio->block_in_file;
  858. if (this_chunk_blocks > u)
  859. this_chunk_blocks = u;
  860. this_chunk_bytes = this_chunk_blocks << blkbits;
  861. BUG_ON(this_chunk_bytes == 0);
  862. if (this_chunk_blocks == sdio->blocks_available)
  863. sdio->boundary = buffer_boundary(map_bh);
  864. ret = submit_page_section(dio, sdio, page,
  865. offset_in_page,
  866. this_chunk_bytes,
  867. sdio->next_block_for_io,
  868. map_bh);
  869. if (ret) {
  870. page_cache_release(page);
  871. goto out;
  872. }
  873. sdio->next_block_for_io += this_chunk_blocks;
  874. sdio->block_in_file += this_chunk_blocks;
  875. block_in_page += this_chunk_blocks;
  876. sdio->blocks_available -= this_chunk_blocks;
  877. next_block:
  878. BUG_ON(sdio->block_in_file > sdio->final_block_in_request);
  879. if (sdio->block_in_file == sdio->final_block_in_request)
  880. break;
  881. }
  882. /* Drop the ref which was taken in get_user_pages() */
  883. page_cache_release(page);
  884. block_in_page = 0;
  885. }
  886. out:
  887. return ret;
  888. }
  889. static inline int drop_refcount(struct dio *dio)
  890. {
  891. int ret2;
  892. unsigned long flags;
  893. /*
  894. * Sync will always be dropping the final ref and completing the
  895. * operation. AIO can if it was a broken operation described above or
  896. * in fact if all the bios race to complete before we get here. In
  897. * that case dio_complete() translates the EIOCBQUEUED into the proper
  898. * return code that the caller will hand to aio_complete().
  899. *
  900. * This is managed by the bio_lock instead of being an atomic_t so that
  901. * completion paths can drop their ref and use the remaining count to
  902. * decide to wake the submission path atomically.
  903. */
  904. spin_lock_irqsave(&dio->bio_lock, flags);
  905. ret2 = --dio->refcount;
  906. spin_unlock_irqrestore(&dio->bio_lock, flags);
  907. return ret2;
  908. }
  909. /*
  910. * This is a library function for use by filesystem drivers.
  911. *
  912. * The locking rules are governed by the flags parameter:
  913. * - if the flags value contains DIO_LOCKING we use a fancy locking
  914. * scheme for dumb filesystems.
  915. * For writes this function is called under i_mutex and returns with
  916. * i_mutex held, for reads, i_mutex is not held on entry, but it is
  917. * taken and dropped again before returning.
  918. * - if the flags value does NOT contain DIO_LOCKING we don't use any
  919. * internal locking but rather rely on the filesystem to synchronize
  920. * direct I/O reads/writes versus each other and truncate.
  921. *
  922. * To help with locking against truncate we incremented the i_dio_count
  923. * counter before starting direct I/O, and decrement it once we are done.
  924. * Truncate can wait for it to reach zero to provide exclusion. It is
  925. * expected that filesystem provide exclusion between new direct I/O
  926. * and truncates. For DIO_LOCKING filesystems this is done by i_mutex,
  927. * but other filesystems need to take care of this on their own.
  928. *
  929. * NOTE: if you pass "sdio" to anything by pointer make sure that function
  930. * is always inlined. Otherwise gcc is unable to split the structure into
  931. * individual fields and will generate much worse code. This is important
  932. * for the whole file.
  933. */
  934. static inline ssize_t
  935. do_blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
  936. struct block_device *bdev, const struct iovec *iov, loff_t offset,
  937. unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
  938. dio_submit_t submit_io, int flags)
  939. {
  940. int seg;
  941. size_t size;
  942. unsigned long addr;
  943. unsigned i_blkbits = ACCESS_ONCE(inode->i_blkbits);
  944. unsigned blkbits = i_blkbits;
  945. unsigned blocksize_mask = (1 << blkbits) - 1;
  946. ssize_t retval = -EINVAL;
  947. loff_t end = offset;
  948. struct dio *dio;
  949. struct dio_submit sdio = { 0, };
  950. unsigned long user_addr;
  951. size_t bytes;
  952. struct buffer_head map_bh = { 0, };
  953. struct blk_plug plug;
  954. if (rw & WRITE)
  955. rw = WRITE_ODIRECT;
  956. /*
  957. * Avoid references to bdev if not absolutely needed to give
  958. * the early prefetch in the caller enough time.
  959. */
  960. if (offset & blocksize_mask) {
  961. if (bdev)
  962. blkbits = blksize_bits(bdev_logical_block_size(bdev));
  963. blocksize_mask = (1 << blkbits) - 1;
  964. if (offset & blocksize_mask)
  965. goto out;
  966. }
  967. /* Check the memory alignment. Blocks cannot straddle pages */
  968. for (seg = 0; seg < nr_segs; seg++) {
  969. addr = (unsigned long)iov[seg].iov_base;
  970. size = iov[seg].iov_len;
  971. end += size;
  972. if (unlikely((addr & blocksize_mask) ||
  973. (size & blocksize_mask))) {
  974. if (bdev)
  975. blkbits = blksize_bits(
  976. bdev_logical_block_size(bdev));
  977. blocksize_mask = (1 << blkbits) - 1;
  978. if ((addr & blocksize_mask) || (size & blocksize_mask))
  979. goto out;
  980. }
  981. }
  982. /* watch out for a 0 len io from a tricksy fs */
  983. if (rw == READ && end == offset)
  984. return 0;
  985. dio = kmem_cache_alloc(dio_cache, GFP_KERNEL);
  986. retval = -ENOMEM;
  987. if (!dio)
  988. goto out;
  989. /*
  990. * Believe it or not, zeroing out the page array caused a .5%
  991. * performance regression in a database benchmark. So, we take
  992. * care to only zero out what's needed.
  993. */
  994. memset(dio, 0, offsetof(struct dio, pages));
  995. dio->flags = flags;
  996. if (dio->flags & DIO_LOCKING) {
  997. if (rw == READ) {
  998. struct address_space *mapping =
  999. iocb->ki_filp->f_mapping;
  1000. /* will be released by direct_io_worker */
  1001. mutex_lock(&inode->i_mutex);
  1002. retval = filemap_write_and_wait_range(mapping, offset,
  1003. end - 1);
  1004. if (retval) {
  1005. mutex_unlock(&inode->i_mutex);
  1006. kmem_cache_free(dio_cache, dio);
  1007. goto out;
  1008. }
  1009. }
  1010. }
  1011. /*
  1012. * Will be decremented at I/O completion time.
  1013. */
  1014. atomic_inc(&inode->i_dio_count);
  1015. /*
  1016. * For file extending writes updating i_size before data
  1017. * writeouts complete can expose uninitialized blocks. So
  1018. * even for AIO, we need to wait for i/o to complete before
  1019. * returning in this case.
  1020. */
  1021. dio->is_async = !is_sync_kiocb(iocb) && !((rw & WRITE) &&
  1022. (end > i_size_read(inode)));
  1023. retval = 0;
  1024. dio->inode = inode;
  1025. dio->rw = rw;
  1026. sdio.blkbits = blkbits;
  1027. sdio.blkfactor = i_blkbits - blkbits;
  1028. sdio.block_in_file = offset >> blkbits;
  1029. sdio.get_block = get_block;
  1030. dio->end_io = end_io;
  1031. sdio.submit_io = submit_io;
  1032. sdio.final_block_in_bio = -1;
  1033. sdio.next_block_for_io = -1;
  1034. dio->iocb = iocb;
  1035. dio->i_size = i_size_read(inode);
  1036. spin_lock_init(&dio->bio_lock);
  1037. dio->refcount = 1;
  1038. /*
  1039. * In case of non-aligned buffers, we may need 2 more
  1040. * pages since we need to zero out first and last block.
  1041. */
  1042. if (unlikely(sdio.blkfactor))
  1043. sdio.pages_in_io = 2;
  1044. for (seg = 0; seg < nr_segs; seg++) {
  1045. user_addr = (unsigned long)iov[seg].iov_base;
  1046. sdio.pages_in_io +=
  1047. ((user_addr + iov[seg].iov_len + PAGE_SIZE-1) /
  1048. PAGE_SIZE - user_addr / PAGE_SIZE);
  1049. }
  1050. blk_start_plug(&plug);
  1051. for (seg = 0; seg < nr_segs; seg++) {
  1052. user_addr = (unsigned long)iov[seg].iov_base;
  1053. sdio.size += bytes = iov[seg].iov_len;
  1054. /* Index into the first page of the first block */
  1055. sdio.first_block_in_page = (user_addr & ~PAGE_MASK) >> blkbits;
  1056. sdio.final_block_in_request = sdio.block_in_file +
  1057. (bytes >> blkbits);
  1058. /* Page fetching state */
  1059. sdio.head = 0;
  1060. sdio.tail = 0;
  1061. sdio.curr_page = 0;
  1062. sdio.total_pages = 0;
  1063. if (user_addr & (PAGE_SIZE-1)) {
  1064. sdio.total_pages++;
  1065. bytes -= PAGE_SIZE - (user_addr & (PAGE_SIZE - 1));
  1066. }
  1067. sdio.total_pages += (bytes + PAGE_SIZE - 1) / PAGE_SIZE;
  1068. sdio.curr_user_address = user_addr;
  1069. retval = do_direct_IO(dio, &sdio, &map_bh);
  1070. dio->result += iov[seg].iov_len -
  1071. ((sdio.final_block_in_request - sdio.block_in_file) <<
  1072. blkbits);
  1073. if (retval) {
  1074. dio_cleanup(dio, &sdio);
  1075. break;
  1076. }
  1077. } /* end iovec loop */
  1078. if (retval == -ENOTBLK) {
  1079. /*
  1080. * The remaining part of the request will be
  1081. * be handled by buffered I/O when we return
  1082. */
  1083. retval = 0;
  1084. }
  1085. /*
  1086. * There may be some unwritten disk at the end of a part-written
  1087. * fs-block-sized block. Go zero that now.
  1088. */
  1089. dio_zero_block(dio, &sdio, 1, &map_bh);
  1090. if (sdio.cur_page) {
  1091. ssize_t ret2;
  1092. ret2 = dio_send_cur_page(dio, &sdio, &map_bh);
  1093. if (retval == 0)
  1094. retval = ret2;
  1095. page_cache_release(sdio.cur_page);
  1096. sdio.cur_page = NULL;
  1097. }
  1098. if (sdio.bio)
  1099. dio_bio_submit(dio, &sdio);
  1100. blk_finish_plug(&plug);
  1101. /*
  1102. * It is possible that, we return short IO due to end of file.
  1103. * In that case, we need to release all the pages we got hold on.
  1104. */
  1105. dio_cleanup(dio, &sdio);
  1106. /*
  1107. * All block lookups have been performed. For READ requests
  1108. * we can let i_mutex go now that its achieved its purpose
  1109. * of protecting us from looking up uninitialized blocks.
  1110. */
  1111. if (rw == READ && (dio->flags & DIO_LOCKING))
  1112. mutex_unlock(&dio->inode->i_mutex);
  1113. /*
  1114. * The only time we want to leave bios in flight is when a successful
  1115. * partial aio read or full aio write have been setup. In that case
  1116. * bio completion will call aio_complete. The only time it's safe to
  1117. * call aio_complete is when we return -EIOCBQUEUED, so we key on that.
  1118. * This had *better* be the only place that raises -EIOCBQUEUED.
  1119. */
  1120. BUG_ON(retval == -EIOCBQUEUED);
  1121. if (dio->is_async && retval == 0 && dio->result &&
  1122. ((rw == READ) || (dio->result == sdio.size)))
  1123. retval = -EIOCBQUEUED;
  1124. if (retval != -EIOCBQUEUED)
  1125. dio_await_completion(dio);
  1126. if (drop_refcount(dio) == 0) {
  1127. retval = dio_complete(dio, offset, retval, false);
  1128. kmem_cache_free(dio_cache, dio);
  1129. } else
  1130. BUG_ON(retval != -EIOCBQUEUED);
  1131. out:
  1132. return retval;
  1133. }
  1134. ssize_t
  1135. __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
  1136. struct block_device *bdev, const struct iovec *iov, loff_t offset,
  1137. unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
  1138. dio_submit_t submit_io, int flags)
  1139. {
  1140. /*
  1141. * The block device state is needed in the end to finally
  1142. * submit everything. Since it's likely to be cache cold
  1143. * prefetch it here as first thing to hide some of the
  1144. * latency.
  1145. *
  1146. * Attempt to prefetch the pieces we likely need later.
  1147. */
  1148. prefetch(&bdev->bd_disk->part_tbl);
  1149. prefetch(bdev->bd_queue);
  1150. prefetch((char *)bdev->bd_queue + SMP_CACHE_BYTES);
  1151. return do_blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset,
  1152. nr_segs, get_block, end_io,
  1153. submit_io, flags);
  1154. }
  1155. EXPORT_SYMBOL(__blockdev_direct_IO);
  1156. static __init int dio_init(void)
  1157. {
  1158. dio_cache = KMEM_CACHE(dio, SLAB_PANIC);
  1159. return 0;
  1160. }
  1161. module_init(dio_init)