direct-io.c 37 KB

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