direct-io.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  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. inode_dio_done(dio->inode);
  237. if (is_async)
  238. aio_complete(dio->iocb, ret, 0);
  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. unsigned int i_blkbits = sdio->blkbits + sdio->blkfactor;
  484. /*
  485. * If there was a memory error and we've overwritten all the
  486. * mapped blocks then we can now return that memory error
  487. */
  488. ret = dio->page_errors;
  489. if (ret == 0) {
  490. BUG_ON(sdio->block_in_file >= sdio->final_block_in_request);
  491. fs_startblk = sdio->block_in_file >> sdio->blkfactor;
  492. fs_endblk = (sdio->final_block_in_request - 1) >>
  493. sdio->blkfactor;
  494. fs_count = fs_endblk - fs_startblk + 1;
  495. map_bh->b_state = 0;
  496. map_bh->b_size = fs_count << i_blkbits;
  497. /*
  498. * For writes inside i_size on a DIO_SKIP_HOLES filesystem we
  499. * forbid block creations: only overwrites are permitted.
  500. * We will return early to the caller once we see an
  501. * unmapped buffer head returned, and the caller will fall
  502. * back to buffered I/O.
  503. *
  504. * Otherwise the decision is left to the get_blocks method,
  505. * which may decide to handle it or also return an unmapped
  506. * buffer head.
  507. */
  508. create = dio->rw & WRITE;
  509. if (dio->flags & DIO_SKIP_HOLES) {
  510. if (sdio->block_in_file < (i_size_read(dio->inode) >>
  511. sdio->blkbits))
  512. create = 0;
  513. }
  514. ret = (*sdio->get_block)(dio->inode, fs_startblk,
  515. map_bh, create);
  516. /* Store for completion */
  517. dio->private = map_bh->b_private;
  518. }
  519. return ret;
  520. }
  521. /*
  522. * There is no bio. Make one now.
  523. */
  524. static inline int dio_new_bio(struct dio *dio, struct dio_submit *sdio,
  525. sector_t start_sector, struct buffer_head *map_bh)
  526. {
  527. sector_t sector;
  528. int ret, nr_pages;
  529. ret = dio_bio_reap(dio, sdio);
  530. if (ret)
  531. goto out;
  532. sector = start_sector << (sdio->blkbits - 9);
  533. nr_pages = min(sdio->pages_in_io, bio_get_nr_vecs(map_bh->b_bdev));
  534. nr_pages = min(nr_pages, BIO_MAX_PAGES);
  535. BUG_ON(nr_pages <= 0);
  536. dio_bio_alloc(dio, sdio, map_bh->b_bdev, sector, nr_pages);
  537. sdio->boundary = 0;
  538. out:
  539. return ret;
  540. }
  541. /*
  542. * Attempt to put the current chunk of 'cur_page' into the current BIO. If
  543. * that was successful then update final_block_in_bio and take a ref against
  544. * the just-added page.
  545. *
  546. * Return zero on success. Non-zero means the caller needs to start a new BIO.
  547. */
  548. static inline int dio_bio_add_page(struct dio_submit *sdio)
  549. {
  550. int ret;
  551. ret = bio_add_page(sdio->bio, sdio->cur_page,
  552. sdio->cur_page_len, sdio->cur_page_offset);
  553. if (ret == sdio->cur_page_len) {
  554. /*
  555. * Decrement count only, if we are done with this page
  556. */
  557. if ((sdio->cur_page_len + sdio->cur_page_offset) == PAGE_SIZE)
  558. sdio->pages_in_io--;
  559. page_cache_get(sdio->cur_page);
  560. sdio->final_block_in_bio = sdio->cur_page_block +
  561. (sdio->cur_page_len >> sdio->blkbits);
  562. ret = 0;
  563. } else {
  564. ret = 1;
  565. }
  566. return ret;
  567. }
  568. /*
  569. * Put cur_page under IO. The section of cur_page which is described by
  570. * cur_page_offset,cur_page_len is put into a BIO. The section of cur_page
  571. * starts on-disk at cur_page_block.
  572. *
  573. * We take a ref against the page here (on behalf of its presence in the bio).
  574. *
  575. * The caller of this function is responsible for removing cur_page from the
  576. * dio, and for dropping the refcount which came from that presence.
  577. */
  578. static inline int dio_send_cur_page(struct dio *dio, struct dio_submit *sdio,
  579. struct buffer_head *map_bh)
  580. {
  581. int ret = 0;
  582. if (sdio->bio) {
  583. loff_t cur_offset = sdio->cur_page_fs_offset;
  584. loff_t bio_next_offset = sdio->logical_offset_in_bio +
  585. sdio->bio->bi_size;
  586. /*
  587. * See whether this new request is contiguous with the old.
  588. *
  589. * Btrfs cannot handle having logically non-contiguous requests
  590. * submitted. For example if you have
  591. *
  592. * Logical: [0-4095][HOLE][8192-12287]
  593. * Physical: [0-4095] [4096-8191]
  594. *
  595. * We cannot submit those pages together as one BIO. So if our
  596. * current logical offset in the file does not equal what would
  597. * be the next logical offset in the bio, submit the bio we
  598. * have.
  599. */
  600. if (sdio->final_block_in_bio != sdio->cur_page_block ||
  601. cur_offset != bio_next_offset)
  602. dio_bio_submit(dio, sdio);
  603. }
  604. if (sdio->bio == NULL) {
  605. ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
  606. if (ret)
  607. goto out;
  608. }
  609. if (dio_bio_add_page(sdio) != 0) {
  610. dio_bio_submit(dio, sdio);
  611. ret = dio_new_bio(dio, sdio, sdio->cur_page_block, map_bh);
  612. if (ret == 0) {
  613. ret = dio_bio_add_page(sdio);
  614. BUG_ON(ret != 0);
  615. }
  616. }
  617. out:
  618. return ret;
  619. }
  620. /*
  621. * An autonomous function to put a chunk of a page under deferred IO.
  622. *
  623. * The caller doesn't actually know (or care) whether this piece of page is in
  624. * a BIO, or is under IO or whatever. We just take care of all possible
  625. * situations here. The separation between the logic of do_direct_IO() and
  626. * that of submit_page_section() is important for clarity. Please don't break.
  627. *
  628. * The chunk of page starts on-disk at blocknr.
  629. *
  630. * We perform deferred IO, by recording the last-submitted page inside our
  631. * private part of the dio structure. If possible, we just expand the IO
  632. * across that page here.
  633. *
  634. * If that doesn't work out then we put the old page into the bio and add this
  635. * page to the dio instead.
  636. */
  637. static inline int
  638. submit_page_section(struct dio *dio, struct dio_submit *sdio, struct page *page,
  639. unsigned offset, unsigned len, sector_t blocknr,
  640. struct buffer_head *map_bh)
  641. {
  642. int ret = 0;
  643. if (dio->rw & WRITE) {
  644. /*
  645. * Read accounting is performed in submit_bio()
  646. */
  647. task_io_account_write(len);
  648. }
  649. /*
  650. * Can we just grow the current page's presence in the dio?
  651. */
  652. if (sdio->cur_page == page &&
  653. sdio->cur_page_offset + sdio->cur_page_len == offset &&
  654. sdio->cur_page_block +
  655. (sdio->cur_page_len >> sdio->blkbits) == blocknr) {
  656. sdio->cur_page_len += len;
  657. goto out;
  658. }
  659. /*
  660. * If there's a deferred page already there then send it.
  661. */
  662. if (sdio->cur_page) {
  663. ret = dio_send_cur_page(dio, sdio, map_bh);
  664. page_cache_release(sdio->cur_page);
  665. sdio->cur_page = NULL;
  666. if (ret)
  667. return ret;
  668. }
  669. page_cache_get(page); /* It is in dio */
  670. sdio->cur_page = page;
  671. sdio->cur_page_offset = offset;
  672. sdio->cur_page_len = len;
  673. sdio->cur_page_block = blocknr;
  674. sdio->cur_page_fs_offset = sdio->block_in_file << sdio->blkbits;
  675. out:
  676. /*
  677. * If sdio->boundary then we want to schedule the IO now to
  678. * avoid metadata seeks.
  679. */
  680. if (sdio->boundary) {
  681. ret = dio_send_cur_page(dio, sdio, map_bh);
  682. dio_bio_submit(dio, sdio);
  683. page_cache_release(sdio->cur_page);
  684. sdio->cur_page = NULL;
  685. }
  686. return ret;
  687. }
  688. /*
  689. * Clean any dirty buffers in the blockdev mapping which alias newly-created
  690. * file blocks. Only called for S_ISREG files - blockdevs do not set
  691. * buffer_new
  692. */
  693. static void clean_blockdev_aliases(struct dio *dio, struct buffer_head *map_bh)
  694. {
  695. unsigned i;
  696. unsigned nblocks;
  697. nblocks = map_bh->b_size >> dio->inode->i_blkbits;
  698. for (i = 0; i < nblocks; i++) {
  699. unmap_underlying_metadata(map_bh->b_bdev,
  700. map_bh->b_blocknr + i);
  701. }
  702. }
  703. /*
  704. * If we are not writing the entire block and get_block() allocated
  705. * the block for us, we need to fill-in the unused portion of the
  706. * block with zeros. This happens only if user-buffer, fileoffset or
  707. * io length is not filesystem block-size multiple.
  708. *
  709. * `end' is zero if we're doing the start of the IO, 1 at the end of the
  710. * IO.
  711. */
  712. static inline void dio_zero_block(struct dio *dio, struct dio_submit *sdio,
  713. int end, struct buffer_head *map_bh)
  714. {
  715. unsigned dio_blocks_per_fs_block;
  716. unsigned this_chunk_blocks; /* In dio_blocks */
  717. unsigned this_chunk_bytes;
  718. struct page *page;
  719. sdio->start_zero_done = 1;
  720. if (!sdio->blkfactor || !buffer_new(map_bh))
  721. return;
  722. dio_blocks_per_fs_block = 1 << sdio->blkfactor;
  723. this_chunk_blocks = sdio->block_in_file & (dio_blocks_per_fs_block - 1);
  724. if (!this_chunk_blocks)
  725. return;
  726. /*
  727. * We need to zero out part of an fs block. It is either at the
  728. * beginning or the end of the fs block.
  729. */
  730. if (end)
  731. this_chunk_blocks = dio_blocks_per_fs_block - this_chunk_blocks;
  732. this_chunk_bytes = this_chunk_blocks << sdio->blkbits;
  733. page = ZERO_PAGE(0);
  734. if (submit_page_section(dio, sdio, page, 0, this_chunk_bytes,
  735. sdio->next_block_for_io, map_bh))
  736. return;
  737. sdio->next_block_for_io += this_chunk_blocks;
  738. }
  739. /*
  740. * Walk the user pages, and the file, mapping blocks to disk and generating
  741. * a sequence of (page,offset,len,block) mappings. These mappings are injected
  742. * into submit_page_section(), which takes care of the next stage of submission
  743. *
  744. * Direct IO against a blockdev is different from a file. Because we can
  745. * happily perform page-sized but 512-byte aligned IOs. It is important that
  746. * blockdev IO be able to have fine alignment and large sizes.
  747. *
  748. * So what we do is to permit the ->get_block function to populate bh.b_size
  749. * with the size of IO which is permitted at this offset and this i_blkbits.
  750. *
  751. * For best results, the blockdev should be set up with 512-byte i_blkbits and
  752. * it should set b_size to PAGE_SIZE or more inside get_block(). This gives
  753. * fine alignment but still allows this function to work in PAGE_SIZE units.
  754. */
  755. static int do_direct_IO(struct dio *dio, struct dio_submit *sdio,
  756. struct buffer_head *map_bh)
  757. {
  758. const unsigned blkbits = sdio->blkbits;
  759. const unsigned blocks_per_page = PAGE_SIZE >> blkbits;
  760. struct page *page;
  761. unsigned block_in_page;
  762. int ret = 0;
  763. /* The I/O can start at any block offset within the first page */
  764. block_in_page = sdio->first_block_in_page;
  765. while (sdio->block_in_file < sdio->final_block_in_request) {
  766. page = dio_get_page(dio, sdio);
  767. if (IS_ERR(page)) {
  768. ret = PTR_ERR(page);
  769. goto out;
  770. }
  771. while (block_in_page < blocks_per_page) {
  772. unsigned offset_in_page = block_in_page << blkbits;
  773. unsigned this_chunk_bytes; /* # of bytes mapped */
  774. unsigned this_chunk_blocks; /* # of blocks */
  775. unsigned u;
  776. if (sdio->blocks_available == 0) {
  777. /*
  778. * Need to go and map some more disk
  779. */
  780. unsigned long blkmask;
  781. unsigned long dio_remainder;
  782. ret = get_more_blocks(dio, sdio, map_bh);
  783. if (ret) {
  784. page_cache_release(page);
  785. goto out;
  786. }
  787. if (!buffer_mapped(map_bh))
  788. goto do_holes;
  789. sdio->blocks_available =
  790. map_bh->b_size >> sdio->blkbits;
  791. sdio->next_block_for_io =
  792. map_bh->b_blocknr << sdio->blkfactor;
  793. if (buffer_new(map_bh))
  794. clean_blockdev_aliases(dio, map_bh);
  795. if (!sdio->blkfactor)
  796. goto do_holes;
  797. blkmask = (1 << sdio->blkfactor) - 1;
  798. dio_remainder = (sdio->block_in_file & blkmask);
  799. /*
  800. * If we are at the start of IO and that IO
  801. * starts partway into a fs-block,
  802. * dio_remainder will be non-zero. If the IO
  803. * is a read then we can simply advance the IO
  804. * cursor to the first block which is to be
  805. * read. But if the IO is a write and the
  806. * block was newly allocated we cannot do that;
  807. * the start of the fs block must be zeroed out
  808. * on-disk
  809. */
  810. if (!buffer_new(map_bh))
  811. sdio->next_block_for_io += dio_remainder;
  812. sdio->blocks_available -= dio_remainder;
  813. }
  814. do_holes:
  815. /* Handle holes */
  816. if (!buffer_mapped(map_bh)) {
  817. loff_t i_size_aligned;
  818. /* AKPM: eargh, -ENOTBLK is a hack */
  819. if (dio->rw & WRITE) {
  820. page_cache_release(page);
  821. return -ENOTBLK;
  822. }
  823. /*
  824. * Be sure to account for a partial block as the
  825. * last block in the file
  826. */
  827. i_size_aligned = ALIGN(i_size_read(dio->inode),
  828. 1 << blkbits);
  829. if (sdio->block_in_file >=
  830. i_size_aligned >> blkbits) {
  831. /* We hit eof */
  832. page_cache_release(page);
  833. goto out;
  834. }
  835. zero_user(page, block_in_page << blkbits,
  836. 1 << blkbits);
  837. sdio->block_in_file++;
  838. block_in_page++;
  839. goto next_block;
  840. }
  841. /*
  842. * If we're performing IO which has an alignment which
  843. * is finer than the underlying fs, go check to see if
  844. * we must zero out the start of this block.
  845. */
  846. if (unlikely(sdio->blkfactor && !sdio->start_zero_done))
  847. dio_zero_block(dio, sdio, 0, map_bh);
  848. /*
  849. * Work out, in this_chunk_blocks, how much disk we
  850. * can add to this page
  851. */
  852. this_chunk_blocks = sdio->blocks_available;
  853. u = (PAGE_SIZE - offset_in_page) >> blkbits;
  854. if (this_chunk_blocks > u)
  855. this_chunk_blocks = u;
  856. u = sdio->final_block_in_request - sdio->block_in_file;
  857. if (this_chunk_blocks > u)
  858. this_chunk_blocks = u;
  859. this_chunk_bytes = this_chunk_blocks << blkbits;
  860. BUG_ON(this_chunk_bytes == 0);
  861. if (this_chunk_blocks == sdio->blocks_available)
  862. sdio->boundary = buffer_boundary(map_bh);
  863. ret = submit_page_section(dio, sdio, page,
  864. offset_in_page,
  865. this_chunk_bytes,
  866. sdio->next_block_for_io,
  867. map_bh);
  868. if (ret) {
  869. page_cache_release(page);
  870. goto out;
  871. }
  872. sdio->next_block_for_io += this_chunk_blocks;
  873. sdio->block_in_file += this_chunk_blocks;
  874. block_in_page += this_chunk_blocks;
  875. sdio->blocks_available -= this_chunk_blocks;
  876. next_block:
  877. BUG_ON(sdio->block_in_file > sdio->final_block_in_request);
  878. if (sdio->block_in_file == sdio->final_block_in_request)
  879. break;
  880. }
  881. /* Drop the ref which was taken in get_user_pages() */
  882. page_cache_release(page);
  883. block_in_page = 0;
  884. }
  885. out:
  886. return ret;
  887. }
  888. static inline int drop_refcount(struct dio *dio)
  889. {
  890. int ret2;
  891. unsigned long flags;
  892. /*
  893. * Sync will always be dropping the final ref and completing the
  894. * operation. AIO can if it was a broken operation described above or
  895. * in fact if all the bios race to complete before we get here. In
  896. * that case dio_complete() translates the EIOCBQUEUED into the proper
  897. * return code that the caller will hand to aio_complete().
  898. *
  899. * This is managed by the bio_lock instead of being an atomic_t so that
  900. * completion paths can drop their ref and use the remaining count to
  901. * decide to wake the submission path atomically.
  902. */
  903. spin_lock_irqsave(&dio->bio_lock, flags);
  904. ret2 = --dio->refcount;
  905. spin_unlock_irqrestore(&dio->bio_lock, flags);
  906. return ret2;
  907. }
  908. /*
  909. * This is a library function for use by filesystem drivers.
  910. *
  911. * The locking rules are governed by the flags parameter:
  912. * - if the flags value contains DIO_LOCKING we use a fancy locking
  913. * scheme for dumb filesystems.
  914. * For writes this function is called under i_mutex and returns with
  915. * i_mutex held, for reads, i_mutex is not held on entry, but it is
  916. * taken and dropped again before returning.
  917. * - if the flags value does NOT contain DIO_LOCKING we don't use any
  918. * internal locking but rather rely on the filesystem to synchronize
  919. * direct I/O reads/writes versus each other and truncate.
  920. *
  921. * To help with locking against truncate we incremented the i_dio_count
  922. * counter before starting direct I/O, and decrement it once we are done.
  923. * Truncate can wait for it to reach zero to provide exclusion. It is
  924. * expected that filesystem provide exclusion between new direct I/O
  925. * and truncates. For DIO_LOCKING filesystems this is done by i_mutex,
  926. * but other filesystems need to take care of this on their own.
  927. *
  928. * NOTE: if you pass "sdio" to anything by pointer make sure that function
  929. * is always inlined. Otherwise gcc is unable to split the structure into
  930. * individual fields and will generate much worse code. This is important
  931. * for the whole file.
  932. */
  933. static inline ssize_t
  934. do_blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
  935. struct block_device *bdev, const struct iovec *iov, loff_t offset,
  936. unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
  937. dio_submit_t submit_io, int flags)
  938. {
  939. int seg;
  940. size_t size;
  941. unsigned long addr;
  942. unsigned i_blkbits = ACCESS_ONCE(inode->i_blkbits);
  943. unsigned blkbits = i_blkbits;
  944. unsigned blocksize_mask = (1 << blkbits) - 1;
  945. ssize_t retval = -EINVAL;
  946. loff_t end = offset;
  947. struct dio *dio;
  948. struct dio_submit sdio = { 0, };
  949. unsigned long user_addr;
  950. size_t bytes;
  951. struct buffer_head map_bh = { 0, };
  952. struct blk_plug plug;
  953. if (rw & WRITE)
  954. rw = WRITE_ODIRECT;
  955. /*
  956. * Avoid references to bdev if not absolutely needed to give
  957. * the early prefetch in the caller enough time.
  958. */
  959. if (offset & blocksize_mask) {
  960. if (bdev)
  961. blkbits = blksize_bits(bdev_logical_block_size(bdev));
  962. blocksize_mask = (1 << blkbits) - 1;
  963. if (offset & blocksize_mask)
  964. goto out;
  965. }
  966. /* Check the memory alignment. Blocks cannot straddle pages */
  967. for (seg = 0; seg < nr_segs; seg++) {
  968. addr = (unsigned long)iov[seg].iov_base;
  969. size = iov[seg].iov_len;
  970. end += size;
  971. if (unlikely((addr & blocksize_mask) ||
  972. (size & blocksize_mask))) {
  973. if (bdev)
  974. blkbits = blksize_bits(
  975. bdev_logical_block_size(bdev));
  976. blocksize_mask = (1 << blkbits) - 1;
  977. if ((addr & blocksize_mask) || (size & blocksize_mask))
  978. goto out;
  979. }
  980. }
  981. /* watch out for a 0 len io from a tricksy fs */
  982. if (rw == READ && end == offset)
  983. return 0;
  984. dio = kmem_cache_alloc(dio_cache, GFP_KERNEL);
  985. retval = -ENOMEM;
  986. if (!dio)
  987. goto out;
  988. /*
  989. * Believe it or not, zeroing out the page array caused a .5%
  990. * performance regression in a database benchmark. So, we take
  991. * care to only zero out what's needed.
  992. */
  993. memset(dio, 0, offsetof(struct dio, pages));
  994. dio->flags = flags;
  995. if (dio->flags & DIO_LOCKING) {
  996. if (rw == READ) {
  997. struct address_space *mapping =
  998. iocb->ki_filp->f_mapping;
  999. /* will be released by direct_io_worker */
  1000. mutex_lock(&inode->i_mutex);
  1001. retval = filemap_write_and_wait_range(mapping, offset,
  1002. end - 1);
  1003. if (retval) {
  1004. mutex_unlock(&inode->i_mutex);
  1005. kmem_cache_free(dio_cache, dio);
  1006. goto out;
  1007. }
  1008. }
  1009. }
  1010. /*
  1011. * Will be decremented at I/O completion time.
  1012. */
  1013. atomic_inc(&inode->i_dio_count);
  1014. /*
  1015. * For file extending writes updating i_size before data
  1016. * writeouts complete can expose uninitialized blocks. So
  1017. * even for AIO, we need to wait for i/o to complete before
  1018. * returning in this case.
  1019. */
  1020. dio->is_async = !is_sync_kiocb(iocb) && !((rw & WRITE) &&
  1021. (end > i_size_read(inode)));
  1022. retval = 0;
  1023. dio->inode = inode;
  1024. dio->rw = rw;
  1025. sdio.blkbits = blkbits;
  1026. sdio.blkfactor = i_blkbits - blkbits;
  1027. sdio.block_in_file = offset >> blkbits;
  1028. sdio.get_block = get_block;
  1029. dio->end_io = end_io;
  1030. sdio.submit_io = submit_io;
  1031. sdio.final_block_in_bio = -1;
  1032. sdio.next_block_for_io = -1;
  1033. dio->iocb = iocb;
  1034. dio->i_size = i_size_read(inode);
  1035. spin_lock_init(&dio->bio_lock);
  1036. dio->refcount = 1;
  1037. /*
  1038. * In case of non-aligned buffers, we may need 2 more
  1039. * pages since we need to zero out first and last block.
  1040. */
  1041. if (unlikely(sdio.blkfactor))
  1042. sdio.pages_in_io = 2;
  1043. for (seg = 0; seg < nr_segs; seg++) {
  1044. user_addr = (unsigned long)iov[seg].iov_base;
  1045. sdio.pages_in_io +=
  1046. ((user_addr + iov[seg].iov_len + PAGE_SIZE-1) /
  1047. PAGE_SIZE - user_addr / PAGE_SIZE);
  1048. }
  1049. blk_start_plug(&plug);
  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. blk_finish_plug(&plug);
  1100. /*
  1101. * It is possible that, we return short IO due to end of file.
  1102. * In that case, we need to release all the pages we got hold on.
  1103. */
  1104. dio_cleanup(dio, &sdio);
  1105. /*
  1106. * All block lookups have been performed. For READ requests
  1107. * we can let i_mutex go now that its achieved its purpose
  1108. * of protecting us from looking up uninitialized blocks.
  1109. */
  1110. if (rw == READ && (dio->flags & DIO_LOCKING))
  1111. mutex_unlock(&dio->inode->i_mutex);
  1112. /*
  1113. * The only time we want to leave bios in flight is when a successful
  1114. * partial aio read or full aio write have been setup. In that case
  1115. * bio completion will call aio_complete. The only time it's safe to
  1116. * call aio_complete is when we return -EIOCBQUEUED, so we key on that.
  1117. * This had *better* be the only place that raises -EIOCBQUEUED.
  1118. */
  1119. BUG_ON(retval == -EIOCBQUEUED);
  1120. if (dio->is_async && retval == 0 && dio->result &&
  1121. ((rw == READ) || (dio->result == sdio.size)))
  1122. retval = -EIOCBQUEUED;
  1123. if (retval != -EIOCBQUEUED)
  1124. dio_await_completion(dio);
  1125. if (drop_refcount(dio) == 0) {
  1126. retval = dio_complete(dio, offset, retval, false);
  1127. kmem_cache_free(dio_cache, dio);
  1128. } else
  1129. BUG_ON(retval != -EIOCBQUEUED);
  1130. out:
  1131. return retval;
  1132. }
  1133. ssize_t
  1134. __blockdev_direct_IO(int rw, struct kiocb *iocb, struct inode *inode,
  1135. struct block_device *bdev, const struct iovec *iov, loff_t offset,
  1136. unsigned long nr_segs, get_block_t get_block, dio_iodone_t end_io,
  1137. dio_submit_t submit_io, int flags)
  1138. {
  1139. /*
  1140. * The block device state is needed in the end to finally
  1141. * submit everything. Since it's likely to be cache cold
  1142. * prefetch it here as first thing to hide some of the
  1143. * latency.
  1144. *
  1145. * Attempt to prefetch the pieces we likely need later.
  1146. */
  1147. prefetch(&bdev->bd_disk->part_tbl);
  1148. prefetch(bdev->bd_queue);
  1149. prefetch((char *)bdev->bd_queue + SMP_CACHE_BYTES);
  1150. return do_blockdev_direct_IO(rw, iocb, inode, bdev, iov, offset,
  1151. nr_segs, get_block, end_io,
  1152. submit_io, flags);
  1153. }
  1154. EXPORT_SYMBOL(__blockdev_direct_IO);
  1155. static __init int dio_init(void)
  1156. {
  1157. dio_cache = KMEM_CACHE(dio, SLAB_PANIC);
  1158. return 0;
  1159. }
  1160. module_init(dio_init)