direct-io.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * Copyright (c) 2016-2021 Christoph Hellwig.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/compiler.h>
  8. #include <linux/fs.h>
  9. #include <linux/fscrypt.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/iomap.h>
  12. #include <linux/backing-dev.h>
  13. #include <linux/uio.h>
  14. #include <linux/task_io_accounting_ops.h>
  15. #include "trace.h"
  16. #include "../internal.h"
  17. /*
  18. * Private flags for iomap_dio, must not overlap with the public ones in
  19. * iomap.h:
  20. */
  21. #define IOMAP_DIO_CALLER_COMP (1U << 26)
  22. #define IOMAP_DIO_INLINE_COMP (1U << 27)
  23. #define IOMAP_DIO_WRITE_THROUGH (1U << 28)
  24. #define IOMAP_DIO_NEED_SYNC (1U << 29)
  25. #define IOMAP_DIO_WRITE (1U << 30)
  26. #define IOMAP_DIO_DIRTY (1U << 31)
  27. /*
  28. * Used for sub block zeroing in iomap_dio_zero()
  29. */
  30. #define IOMAP_ZERO_PAGE_SIZE (SZ_64K)
  31. #define IOMAP_ZERO_PAGE_ORDER (get_order(IOMAP_ZERO_PAGE_SIZE))
  32. static struct page *zero_page;
  33. struct iomap_dio {
  34. struct kiocb *iocb;
  35. const struct iomap_dio_ops *dops;
  36. loff_t i_size;
  37. loff_t size;
  38. atomic_t ref;
  39. unsigned flags;
  40. int error;
  41. size_t done_before;
  42. bool wait_for_completion;
  43. union {
  44. /* used during submission and for synchronous completion: */
  45. struct {
  46. struct iov_iter *iter;
  47. struct task_struct *waiter;
  48. } submit;
  49. /* used for aio completion: */
  50. struct {
  51. struct work_struct work;
  52. } aio;
  53. };
  54. };
  55. static struct bio *iomap_dio_alloc_bio(const struct iomap_iter *iter,
  56. struct iomap_dio *dio, unsigned short nr_vecs, blk_opf_t opf)
  57. {
  58. if (dio->dops && dio->dops->bio_set)
  59. return bio_alloc_bioset(iter->iomap.bdev, nr_vecs, opf,
  60. GFP_KERNEL, dio->dops->bio_set);
  61. return bio_alloc(iter->iomap.bdev, nr_vecs, opf, GFP_KERNEL);
  62. }
  63. static void iomap_dio_submit_bio(const struct iomap_iter *iter,
  64. struct iomap_dio *dio, struct bio *bio, loff_t pos)
  65. {
  66. struct kiocb *iocb = dio->iocb;
  67. atomic_inc(&dio->ref);
  68. /* Sync dio can't be polled reliably */
  69. if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) {
  70. bio_set_polled(bio, iocb);
  71. WRITE_ONCE(iocb->private, bio);
  72. }
  73. if (dio->dops && dio->dops->submit_io)
  74. dio->dops->submit_io(iter, bio, pos);
  75. else
  76. submit_bio(bio);
  77. }
  78. ssize_t iomap_dio_complete(struct iomap_dio *dio)
  79. {
  80. const struct iomap_dio_ops *dops = dio->dops;
  81. struct kiocb *iocb = dio->iocb;
  82. loff_t offset = iocb->ki_pos;
  83. ssize_t ret = dio->error;
  84. if (dops && dops->end_io)
  85. ret = dops->end_io(iocb, dio->size, ret, dio->flags);
  86. if (likely(!ret)) {
  87. ret = dio->size;
  88. /* check for short read */
  89. if (offset + ret > dio->i_size &&
  90. !(dio->flags & IOMAP_DIO_WRITE))
  91. ret = dio->i_size - offset;
  92. }
  93. /*
  94. * Try again to invalidate clean pages which might have been cached by
  95. * non-direct readahead, or faulted in by get_user_pages() if the source
  96. * of the write was an mmap'ed region of the file we're writing. Either
  97. * one is a pretty crazy thing to do, so we don't support it 100%. If
  98. * this invalidation fails, tough, the write still worked...
  99. *
  100. * And this page cache invalidation has to be after ->end_io(), as some
  101. * filesystems convert unwritten extents to real allocations in
  102. * ->end_io() when necessary, otherwise a racing buffer read would cache
  103. * zeros from unwritten extents.
  104. */
  105. if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE))
  106. kiocb_invalidate_post_direct_write(iocb, dio->size);
  107. inode_dio_end(file_inode(iocb->ki_filp));
  108. if (ret > 0) {
  109. iocb->ki_pos += ret;
  110. /*
  111. * If this is a DSYNC write, make sure we push it to stable
  112. * storage now that we've written data.
  113. */
  114. if (dio->flags & IOMAP_DIO_NEED_SYNC)
  115. ret = generic_write_sync(iocb, ret);
  116. if (ret > 0)
  117. ret += dio->done_before;
  118. }
  119. trace_iomap_dio_complete(iocb, dio->error, ret);
  120. kfree(dio);
  121. return ret;
  122. }
  123. EXPORT_SYMBOL_GPL(iomap_dio_complete);
  124. static ssize_t iomap_dio_deferred_complete(void *data)
  125. {
  126. return iomap_dio_complete(data);
  127. }
  128. static void iomap_dio_complete_work(struct work_struct *work)
  129. {
  130. struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
  131. struct kiocb *iocb = dio->iocb;
  132. iocb->ki_complete(iocb, iomap_dio_complete(dio));
  133. }
  134. /*
  135. * Set an error in the dio if none is set yet. We have to use cmpxchg
  136. * as the submission context and the completion context(s) can race to
  137. * update the error.
  138. */
  139. static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
  140. {
  141. cmpxchg(&dio->error, 0, ret);
  142. }
  143. void iomap_dio_bio_end_io(struct bio *bio)
  144. {
  145. struct iomap_dio *dio = bio->bi_private;
  146. bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
  147. struct kiocb *iocb = dio->iocb;
  148. if (bio->bi_status)
  149. iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
  150. if (!atomic_dec_and_test(&dio->ref))
  151. goto release_bio;
  152. /*
  153. * Synchronous dio, task itself will handle any completion work
  154. * that needs after IO. All we need to do is wake the task.
  155. */
  156. if (dio->wait_for_completion) {
  157. struct task_struct *waiter = dio->submit.waiter;
  158. WRITE_ONCE(dio->submit.waiter, NULL);
  159. blk_wake_io_task(waiter);
  160. goto release_bio;
  161. }
  162. /*
  163. * Flagged with IOMAP_DIO_INLINE_COMP, we can complete it inline
  164. */
  165. if (dio->flags & IOMAP_DIO_INLINE_COMP) {
  166. WRITE_ONCE(iocb->private, NULL);
  167. iomap_dio_complete_work(&dio->aio.work);
  168. goto release_bio;
  169. }
  170. /*
  171. * If this dio is flagged with IOMAP_DIO_CALLER_COMP, then schedule
  172. * our completion that way to avoid an async punt to a workqueue.
  173. */
  174. if (dio->flags & IOMAP_DIO_CALLER_COMP) {
  175. /* only polled IO cares about private cleared */
  176. iocb->private = dio;
  177. iocb->dio_complete = iomap_dio_deferred_complete;
  178. /*
  179. * Invoke ->ki_complete() directly. We've assigned our
  180. * dio_complete callback handler, and since the issuer set
  181. * IOCB_DIO_CALLER_COMP, we know their ki_complete handler will
  182. * notice ->dio_complete being set and will defer calling that
  183. * handler until it can be done from a safe task context.
  184. *
  185. * Note that the 'res' being passed in here is not important
  186. * for this case. The actual completion value of the request
  187. * will be gotten from dio_complete when that is run by the
  188. * issuer.
  189. */
  190. iocb->ki_complete(iocb, 0);
  191. goto release_bio;
  192. }
  193. /*
  194. * Async DIO completion that requires filesystem level completion work
  195. * gets punted to a work queue to complete as the operation may require
  196. * more IO to be issued to finalise filesystem metadata changes or
  197. * guarantee data integrity.
  198. */
  199. INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
  200. queue_work(file_inode(iocb->ki_filp)->i_sb->s_dio_done_wq,
  201. &dio->aio.work);
  202. release_bio:
  203. if (should_dirty) {
  204. bio_check_pages_dirty(bio);
  205. } else {
  206. bio_release_pages(bio, false);
  207. bio_put(bio);
  208. }
  209. }
  210. EXPORT_SYMBOL_GPL(iomap_dio_bio_end_io);
  211. static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
  212. loff_t pos, unsigned len)
  213. {
  214. struct inode *inode = file_inode(dio->iocb->ki_filp);
  215. struct bio *bio;
  216. if (!len)
  217. return 0;
  218. /*
  219. * Max block size supported is 64k
  220. */
  221. if (WARN_ON_ONCE(len > IOMAP_ZERO_PAGE_SIZE))
  222. return -EINVAL;
  223. bio = iomap_dio_alloc_bio(iter, dio, 1, REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
  224. fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
  225. GFP_KERNEL);
  226. bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
  227. bio->bi_private = dio;
  228. bio->bi_end_io = iomap_dio_bio_end_io;
  229. __bio_add_page(bio, zero_page, len, 0);
  230. iomap_dio_submit_bio(iter, dio, bio, pos);
  231. return 0;
  232. }
  233. /*
  234. * Figure out the bio's operation flags from the dio request, the
  235. * mapping, and whether or not we want FUA. Note that we can end up
  236. * clearing the WRITE_THROUGH flag in the dio request.
  237. */
  238. static inline blk_opf_t iomap_dio_bio_opflags(struct iomap_dio *dio,
  239. const struct iomap *iomap, bool use_fua)
  240. {
  241. blk_opf_t opflags = REQ_SYNC | REQ_IDLE;
  242. if (!(dio->flags & IOMAP_DIO_WRITE))
  243. return REQ_OP_READ;
  244. opflags |= REQ_OP_WRITE;
  245. if (use_fua)
  246. opflags |= REQ_FUA;
  247. else
  248. dio->flags &= ~IOMAP_DIO_WRITE_THROUGH;
  249. return opflags;
  250. }
  251. static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter,
  252. struct iomap_dio *dio)
  253. {
  254. const struct iomap *iomap = &iter->iomap;
  255. struct inode *inode = iter->inode;
  256. unsigned int fs_block_size = i_blocksize(inode), pad;
  257. loff_t length = iomap_length(iter);
  258. loff_t pos = iter->pos;
  259. blk_opf_t bio_opf;
  260. struct bio *bio;
  261. bool need_zeroout = false;
  262. bool use_fua = false;
  263. int nr_pages, ret = 0;
  264. size_t copied = 0;
  265. size_t orig_count;
  266. if ((pos | length) & (bdev_logical_block_size(iomap->bdev) - 1) ||
  267. !bdev_iter_is_aligned(iomap->bdev, dio->submit.iter))
  268. return -EINVAL;
  269. if (iomap->type == IOMAP_UNWRITTEN) {
  270. dio->flags |= IOMAP_DIO_UNWRITTEN;
  271. need_zeroout = true;
  272. }
  273. if (iomap->flags & IOMAP_F_SHARED)
  274. dio->flags |= IOMAP_DIO_COW;
  275. if (iomap->flags & IOMAP_F_NEW) {
  276. need_zeroout = true;
  277. } else if (iomap->type == IOMAP_MAPPED) {
  278. /*
  279. * Use a FUA write if we need datasync semantics, this is a pure
  280. * data IO that doesn't require any metadata updates (including
  281. * after IO completion such as unwritten extent conversion) and
  282. * the underlying device either supports FUA or doesn't have
  283. * a volatile write cache. This allows us to avoid cache flushes
  284. * on IO completion. If we can't use writethrough and need to
  285. * sync, disable in-task completions as dio completion will
  286. * need to call generic_write_sync() which will do a blocking
  287. * fsync / cache flush call.
  288. */
  289. if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
  290. (dio->flags & IOMAP_DIO_WRITE_THROUGH) &&
  291. (bdev_fua(iomap->bdev) || !bdev_write_cache(iomap->bdev)))
  292. use_fua = true;
  293. else if (dio->flags & IOMAP_DIO_NEED_SYNC)
  294. dio->flags &= ~IOMAP_DIO_CALLER_COMP;
  295. }
  296. /*
  297. * Save the original count and trim the iter to just the extent we
  298. * are operating on right now. The iter will be re-expanded once
  299. * we are done.
  300. */
  301. orig_count = iov_iter_count(dio->submit.iter);
  302. iov_iter_truncate(dio->submit.iter, length);
  303. if (!iov_iter_count(dio->submit.iter))
  304. goto out;
  305. /*
  306. * We can only do deferred completion for pure overwrites that
  307. * don't require additional IO at completion. This rules out
  308. * writes that need zeroing or extent conversion, extend
  309. * the file size, or issue journal IO or cache flushes
  310. * during completion processing.
  311. */
  312. if (need_zeroout ||
  313. ((dio->flags & IOMAP_DIO_NEED_SYNC) && !use_fua) ||
  314. ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode)))
  315. dio->flags &= ~IOMAP_DIO_CALLER_COMP;
  316. /*
  317. * The rules for polled IO completions follow the guidelines as the
  318. * ones we set for inline and deferred completions. If none of those
  319. * are available for this IO, clear the polled flag.
  320. */
  321. if (!(dio->flags & (IOMAP_DIO_INLINE_COMP|IOMAP_DIO_CALLER_COMP)))
  322. dio->iocb->ki_flags &= ~IOCB_HIPRI;
  323. if (need_zeroout) {
  324. /* zero out from the start of the block to the write offset */
  325. pad = pos & (fs_block_size - 1);
  326. ret = iomap_dio_zero(iter, dio, pos - pad, pad);
  327. if (ret)
  328. goto out;
  329. }
  330. /*
  331. * Set the operation flags early so that bio_iov_iter_get_pages
  332. * can set up the page vector appropriately for a ZONE_APPEND
  333. * operation.
  334. */
  335. bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua);
  336. nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
  337. do {
  338. size_t n;
  339. if (dio->error) {
  340. iov_iter_revert(dio->submit.iter, copied);
  341. copied = ret = 0;
  342. goto out;
  343. }
  344. bio = iomap_dio_alloc_bio(iter, dio, nr_pages, bio_opf);
  345. fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
  346. GFP_KERNEL);
  347. bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
  348. bio->bi_write_hint = inode->i_write_hint;
  349. bio->bi_ioprio = dio->iocb->ki_ioprio;
  350. bio->bi_private = dio;
  351. bio->bi_end_io = iomap_dio_bio_end_io;
  352. ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
  353. if (unlikely(ret)) {
  354. /*
  355. * We have to stop part way through an IO. We must fall
  356. * through to the sub-block tail zeroing here, otherwise
  357. * this short IO may expose stale data in the tail of
  358. * the block we haven't written data to.
  359. */
  360. bio_put(bio);
  361. goto zero_tail;
  362. }
  363. n = bio->bi_iter.bi_size;
  364. if (dio->flags & IOMAP_DIO_WRITE) {
  365. task_io_account_write(n);
  366. } else {
  367. if (dio->flags & IOMAP_DIO_DIRTY)
  368. bio_set_pages_dirty(bio);
  369. }
  370. dio->size += n;
  371. copied += n;
  372. nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter,
  373. BIO_MAX_VECS);
  374. /*
  375. * We can only poll for single bio I/Os.
  376. */
  377. if (nr_pages)
  378. dio->iocb->ki_flags &= ~IOCB_HIPRI;
  379. iomap_dio_submit_bio(iter, dio, bio, pos);
  380. pos += n;
  381. } while (nr_pages);
  382. /*
  383. * We need to zeroout the tail of a sub-block write if the extent type
  384. * requires zeroing or the write extends beyond EOF. If we don't zero
  385. * the block tail in the latter case, we can expose stale data via mmap
  386. * reads of the EOF block.
  387. */
  388. zero_tail:
  389. if (need_zeroout ||
  390. ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
  391. /* zero out from the end of the write to the end of the block */
  392. pad = pos & (fs_block_size - 1);
  393. if (pad)
  394. ret = iomap_dio_zero(iter, dio, pos,
  395. fs_block_size - pad);
  396. }
  397. out:
  398. /* Undo iter limitation to current extent */
  399. iov_iter_reexpand(dio->submit.iter, orig_count - copied);
  400. if (copied)
  401. return copied;
  402. return ret;
  403. }
  404. static loff_t iomap_dio_hole_iter(const struct iomap_iter *iter,
  405. struct iomap_dio *dio)
  406. {
  407. loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter);
  408. dio->size += length;
  409. if (!length)
  410. return -EFAULT;
  411. return length;
  412. }
  413. static loff_t iomap_dio_inline_iter(const struct iomap_iter *iomi,
  414. struct iomap_dio *dio)
  415. {
  416. const struct iomap *iomap = &iomi->iomap;
  417. struct iov_iter *iter = dio->submit.iter;
  418. void *inline_data = iomap_inline_data(iomap, iomi->pos);
  419. loff_t length = iomap_length(iomi);
  420. loff_t pos = iomi->pos;
  421. size_t copied;
  422. if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
  423. return -EIO;
  424. if (dio->flags & IOMAP_DIO_WRITE) {
  425. loff_t size = iomi->inode->i_size;
  426. if (pos > size)
  427. memset(iomap_inline_data(iomap, size), 0, pos - size);
  428. copied = copy_from_iter(inline_data, length, iter);
  429. if (copied) {
  430. if (pos + copied > size)
  431. i_size_write(iomi->inode, pos + copied);
  432. mark_inode_dirty(iomi->inode);
  433. }
  434. } else {
  435. copied = copy_to_iter(inline_data, length, iter);
  436. }
  437. dio->size += copied;
  438. if (!copied)
  439. return -EFAULT;
  440. return copied;
  441. }
  442. static loff_t iomap_dio_iter(const struct iomap_iter *iter,
  443. struct iomap_dio *dio)
  444. {
  445. switch (iter->iomap.type) {
  446. case IOMAP_HOLE:
  447. if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
  448. return -EIO;
  449. return iomap_dio_hole_iter(iter, dio);
  450. case IOMAP_UNWRITTEN:
  451. if (!(dio->flags & IOMAP_DIO_WRITE))
  452. return iomap_dio_hole_iter(iter, dio);
  453. return iomap_dio_bio_iter(iter, dio);
  454. case IOMAP_MAPPED:
  455. return iomap_dio_bio_iter(iter, dio);
  456. case IOMAP_INLINE:
  457. return iomap_dio_inline_iter(iter, dio);
  458. case IOMAP_DELALLOC:
  459. /*
  460. * DIO is not serialised against mmap() access at all, and so
  461. * if the page_mkwrite occurs between the writeback and the
  462. * iomap_iter() call in the DIO path, then it will see the
  463. * DELALLOC block that the page-mkwrite allocated.
  464. */
  465. pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
  466. dio->iocb->ki_filp, current->comm);
  467. return -EIO;
  468. default:
  469. WARN_ON_ONCE(1);
  470. return -EIO;
  471. }
  472. }
  473. /*
  474. * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
  475. * is being issued as AIO or not. This allows us to optimise pure data writes
  476. * to use REQ_FUA rather than requiring generic_write_sync() to issue a
  477. * REQ_FLUSH post write. This is slightly tricky because a single request here
  478. * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
  479. * may be pure data writes. In that case, we still need to do a full data sync
  480. * completion.
  481. *
  482. * When page faults are disabled and @dio_flags includes IOMAP_DIO_PARTIAL,
  483. * __iomap_dio_rw can return a partial result if it encounters a non-resident
  484. * page in @iter after preparing a transfer. In that case, the non-resident
  485. * pages can be faulted in and the request resumed with @done_before set to the
  486. * number of bytes previously transferred. The request will then complete with
  487. * the correct total number of bytes transferred; this is essential for
  488. * completing partial requests asynchronously.
  489. *
  490. * Returns -ENOTBLK In case of a page invalidation invalidation failure for
  491. * writes. The callers needs to fall back to buffered I/O in this case.
  492. */
  493. struct iomap_dio *
  494. __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
  495. const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
  496. unsigned int dio_flags, void *private, size_t done_before)
  497. {
  498. struct inode *inode = file_inode(iocb->ki_filp);
  499. struct iomap_iter iomi = {
  500. .inode = inode,
  501. .pos = iocb->ki_pos,
  502. .len = iov_iter_count(iter),
  503. .flags = IOMAP_DIRECT,
  504. .private = private,
  505. };
  506. bool wait_for_completion =
  507. is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
  508. struct blk_plug plug;
  509. struct iomap_dio *dio;
  510. loff_t ret = 0;
  511. trace_iomap_dio_rw_begin(iocb, iter, dio_flags, done_before);
  512. if (!iomi.len)
  513. return NULL;
  514. dio = kmalloc(sizeof(*dio), GFP_KERNEL);
  515. if (!dio)
  516. return ERR_PTR(-ENOMEM);
  517. dio->iocb = iocb;
  518. atomic_set(&dio->ref, 1);
  519. dio->size = 0;
  520. dio->i_size = i_size_read(inode);
  521. dio->dops = dops;
  522. dio->error = 0;
  523. dio->flags = 0;
  524. dio->done_before = done_before;
  525. dio->submit.iter = iter;
  526. dio->submit.waiter = current;
  527. if (iocb->ki_flags & IOCB_NOWAIT)
  528. iomi.flags |= IOMAP_NOWAIT;
  529. if (iov_iter_rw(iter) == READ) {
  530. /* reads can always complete inline */
  531. dio->flags |= IOMAP_DIO_INLINE_COMP;
  532. if (iomi.pos >= dio->i_size)
  533. goto out_free_dio;
  534. if (user_backed_iter(iter))
  535. dio->flags |= IOMAP_DIO_DIRTY;
  536. ret = kiocb_write_and_wait(iocb, iomi.len);
  537. if (ret)
  538. goto out_free_dio;
  539. } else {
  540. iomi.flags |= IOMAP_WRITE;
  541. dio->flags |= IOMAP_DIO_WRITE;
  542. /*
  543. * Flag as supporting deferred completions, if the issuer
  544. * groks it. This can avoid a workqueue punt for writes.
  545. * We may later clear this flag if we need to do other IO
  546. * as part of this IO completion.
  547. */
  548. if (iocb->ki_flags & IOCB_DIO_CALLER_COMP)
  549. dio->flags |= IOMAP_DIO_CALLER_COMP;
  550. if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
  551. ret = -EAGAIN;
  552. if (iomi.pos >= dio->i_size ||
  553. iomi.pos + iomi.len > dio->i_size)
  554. goto out_free_dio;
  555. iomi.flags |= IOMAP_OVERWRITE_ONLY;
  556. }
  557. /* for data sync or sync, we need sync completion processing */
  558. if (iocb_is_dsync(iocb)) {
  559. dio->flags |= IOMAP_DIO_NEED_SYNC;
  560. /*
  561. * For datasync only writes, we optimistically try using
  562. * WRITE_THROUGH for this IO. This flag requires either
  563. * FUA writes through the device's write cache, or a
  564. * normal write to a device without a volatile write
  565. * cache. For the former, Any non-FUA write that occurs
  566. * will clear this flag, hence we know before completion
  567. * whether a cache flush is necessary.
  568. */
  569. if (!(iocb->ki_flags & IOCB_SYNC))
  570. dio->flags |= IOMAP_DIO_WRITE_THROUGH;
  571. }
  572. /*
  573. * Try to invalidate cache pages for the range we are writing.
  574. * If this invalidation fails, let the caller fall back to
  575. * buffered I/O.
  576. */
  577. ret = kiocb_invalidate_pages(iocb, iomi.len);
  578. if (ret) {
  579. if (ret != -EAGAIN) {
  580. trace_iomap_dio_invalidate_fail(inode, iomi.pos,
  581. iomi.len);
  582. ret = -ENOTBLK;
  583. }
  584. goto out_free_dio;
  585. }
  586. if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
  587. ret = sb_init_dio_done_wq(inode->i_sb);
  588. if (ret < 0)
  589. goto out_free_dio;
  590. }
  591. }
  592. inode_dio_begin(inode);
  593. blk_start_plug(&plug);
  594. while ((ret = iomap_iter(&iomi, ops)) > 0) {
  595. iomi.processed = iomap_dio_iter(&iomi, dio);
  596. /*
  597. * We can only poll for single bio I/Os.
  598. */
  599. iocb->ki_flags &= ~IOCB_HIPRI;
  600. }
  601. blk_finish_plug(&plug);
  602. /*
  603. * We only report that we've read data up to i_size.
  604. * Revert iter to a state corresponding to that as some callers (such
  605. * as the splice code) rely on it.
  606. */
  607. if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size)
  608. iov_iter_revert(iter, iomi.pos - dio->i_size);
  609. if (ret == -EFAULT && dio->size && (dio_flags & IOMAP_DIO_PARTIAL)) {
  610. if (!(iocb->ki_flags & IOCB_NOWAIT))
  611. wait_for_completion = true;
  612. ret = 0;
  613. }
  614. /* magic error code to fall back to buffered I/O */
  615. if (ret == -ENOTBLK) {
  616. wait_for_completion = true;
  617. ret = 0;
  618. }
  619. if (ret < 0)
  620. iomap_dio_set_error(dio, ret);
  621. /*
  622. * If all the writes we issued were already written through to the
  623. * media, we don't need to flush the cache on IO completion. Clear the
  624. * sync flag for this case.
  625. */
  626. if (dio->flags & IOMAP_DIO_WRITE_THROUGH)
  627. dio->flags &= ~IOMAP_DIO_NEED_SYNC;
  628. /*
  629. * We are about to drop our additional submission reference, which
  630. * might be the last reference to the dio. There are three different
  631. * ways we can progress here:
  632. *
  633. * (a) If this is the last reference we will always complete and free
  634. * the dio ourselves.
  635. * (b) If this is not the last reference, and we serve an asynchronous
  636. * iocb, we must never touch the dio after the decrement, the
  637. * I/O completion handler will complete and free it.
  638. * (c) If this is not the last reference, but we serve a synchronous
  639. * iocb, the I/O completion handler will wake us up on the drop
  640. * of the final reference, and we will complete and free it here
  641. * after we got woken by the I/O completion handler.
  642. */
  643. dio->wait_for_completion = wait_for_completion;
  644. if (!atomic_dec_and_test(&dio->ref)) {
  645. if (!wait_for_completion) {
  646. trace_iomap_dio_rw_queued(inode, iomi.pos, iomi.len);
  647. return ERR_PTR(-EIOCBQUEUED);
  648. }
  649. for (;;) {
  650. set_current_state(TASK_UNINTERRUPTIBLE);
  651. if (!READ_ONCE(dio->submit.waiter))
  652. break;
  653. blk_io_schedule();
  654. }
  655. __set_current_state(TASK_RUNNING);
  656. }
  657. return dio;
  658. out_free_dio:
  659. kfree(dio);
  660. if (ret)
  661. return ERR_PTR(ret);
  662. return NULL;
  663. }
  664. EXPORT_SYMBOL_GPL(__iomap_dio_rw);
  665. ssize_t
  666. iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
  667. const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
  668. unsigned int dio_flags, void *private, size_t done_before)
  669. {
  670. struct iomap_dio *dio;
  671. dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags, private,
  672. done_before);
  673. if (IS_ERR_OR_NULL(dio))
  674. return PTR_ERR_OR_ZERO(dio);
  675. return iomap_dio_complete(dio);
  676. }
  677. EXPORT_SYMBOL_GPL(iomap_dio_rw);
  678. static int __init iomap_dio_init(void)
  679. {
  680. zero_page = alloc_pages(GFP_KERNEL | __GFP_ZERO,
  681. IOMAP_ZERO_PAGE_ORDER);
  682. if (!zero_page)
  683. return -ENOMEM;
  684. return 0;
  685. }
  686. fs_initcall(iomap_dio_init);