direct-io.c 37 KB

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