direct-io.c 38 KB

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