direct-io.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * Copyright (c) 2016-2025 Christoph Hellwig.
  5. */
  6. #include <linux/blk-crypto.h>
  7. #include <linux/fscrypt.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/iomap.h>
  10. #include <linux/task_io_accounting_ops.h>
  11. #include <linux/fserror.h>
  12. #include "internal.h"
  13. #include "trace.h"
  14. #include "../internal.h"
  15. /*
  16. * Private flags for iomap_dio, must not overlap with the public ones in
  17. * iomap.h:
  18. */
  19. #define IOMAP_DIO_NO_INVALIDATE (1U << 26)
  20. #define IOMAP_DIO_COMP_WORK (1U << 27)
  21. #define IOMAP_DIO_WRITE_THROUGH (1U << 28)
  22. #define IOMAP_DIO_NEED_SYNC (1U << 29)
  23. #define IOMAP_DIO_WRITE (1U << 30)
  24. #define IOMAP_DIO_USER_BACKED (1U << 31)
  25. struct iomap_dio {
  26. struct kiocb *iocb;
  27. const struct iomap_dio_ops *dops;
  28. loff_t i_size;
  29. loff_t size;
  30. atomic_t ref;
  31. unsigned flags;
  32. int error;
  33. size_t done_before;
  34. bool wait_for_completion;
  35. union {
  36. /* used during submission and for synchronous completion: */
  37. struct {
  38. struct iov_iter *iter;
  39. struct task_struct *waiter;
  40. } submit;
  41. /* used for aio completion: */
  42. struct {
  43. struct work_struct work;
  44. } aio;
  45. };
  46. };
  47. static struct bio *iomap_dio_alloc_bio(const struct iomap_iter *iter,
  48. struct iomap_dio *dio, unsigned short nr_vecs, blk_opf_t opf)
  49. {
  50. if (dio->dops && dio->dops->bio_set)
  51. return bio_alloc_bioset(iter->iomap.bdev, nr_vecs, opf,
  52. GFP_KERNEL, dio->dops->bio_set);
  53. return bio_alloc(iter->iomap.bdev, nr_vecs, opf, GFP_KERNEL);
  54. }
  55. static void iomap_dio_submit_bio(const struct iomap_iter *iter,
  56. struct iomap_dio *dio, struct bio *bio, loff_t pos)
  57. {
  58. struct kiocb *iocb = dio->iocb;
  59. atomic_inc(&dio->ref);
  60. /* Sync dio can't be polled reliably */
  61. if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) {
  62. bio_set_polled(bio, iocb);
  63. WRITE_ONCE(iocb->private, bio);
  64. }
  65. if (dio->dops && dio->dops->submit_io) {
  66. dio->dops->submit_io(iter, bio, pos);
  67. } else {
  68. WARN_ON_ONCE(iter->iomap.flags & IOMAP_F_ANON_WRITE);
  69. blk_crypto_submit_bio(bio);
  70. }
  71. }
  72. static inline enum fserror_type iomap_dio_err_type(const struct iomap_dio *dio)
  73. {
  74. if (dio->flags & IOMAP_DIO_WRITE)
  75. return FSERR_DIRECTIO_WRITE;
  76. return FSERR_DIRECTIO_READ;
  77. }
  78. static inline bool should_report_dio_fserror(const struct iomap_dio *dio)
  79. {
  80. switch (dio->error) {
  81. case 0:
  82. case -EAGAIN:
  83. case -ENOTBLK:
  84. /* don't send fsnotify for success or magic retry codes */
  85. return false;
  86. default:
  87. return true;
  88. }
  89. }
  90. ssize_t iomap_dio_complete(struct iomap_dio *dio)
  91. {
  92. const struct iomap_dio_ops *dops = dio->dops;
  93. struct kiocb *iocb = dio->iocb;
  94. loff_t offset = iocb->ki_pos;
  95. ssize_t ret = dio->error;
  96. if (dops && dops->end_io)
  97. ret = dops->end_io(iocb, dio->size, ret, dio->flags);
  98. if (should_report_dio_fserror(dio))
  99. fserror_report_io(file_inode(iocb->ki_filp),
  100. iomap_dio_err_type(dio), offset, dio->size,
  101. dio->error, GFP_NOFS);
  102. if (likely(!ret)) {
  103. ret = dio->size;
  104. /* check for short read */
  105. if (offset + ret > dio->i_size &&
  106. !(dio->flags & IOMAP_DIO_WRITE))
  107. ret = dio->i_size - offset;
  108. }
  109. /*
  110. * Try again to invalidate clean pages which might have been cached by
  111. * non-direct readahead, or faulted in by get_user_pages() if the source
  112. * of the write was an mmap'ed region of the file we're writing. Either
  113. * one is a pretty crazy thing to do, so we don't support it 100%. If
  114. * this invalidation fails, tough, the write still worked...
  115. *
  116. * And this page cache invalidation has to be after ->end_io(), as some
  117. * filesystems convert unwritten extents to real allocations in
  118. * ->end_io() when necessary, otherwise a racing buffer read would cache
  119. * zeros from unwritten extents.
  120. */
  121. if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE) &&
  122. !(dio->flags & IOMAP_DIO_NO_INVALIDATE))
  123. kiocb_invalidate_post_direct_write(iocb, dio->size);
  124. inode_dio_end(file_inode(iocb->ki_filp));
  125. if (ret > 0) {
  126. iocb->ki_pos += ret;
  127. /*
  128. * If this is a DSYNC write, make sure we push it to stable
  129. * storage now that we've written data.
  130. */
  131. if (dio->flags & IOMAP_DIO_NEED_SYNC)
  132. ret = generic_write_sync(iocb, ret);
  133. if (ret > 0)
  134. ret += dio->done_before;
  135. }
  136. trace_iomap_dio_complete(iocb, dio->error, ret);
  137. kfree(dio);
  138. return ret;
  139. }
  140. EXPORT_SYMBOL_GPL(iomap_dio_complete);
  141. static void iomap_dio_complete_work(struct work_struct *work)
  142. {
  143. struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
  144. struct kiocb *iocb = dio->iocb;
  145. iocb->ki_complete(iocb, iomap_dio_complete(dio));
  146. }
  147. /*
  148. * Set an error in the dio if none is set yet. We have to use cmpxchg
  149. * as the submission context and the completion context(s) can race to
  150. * update the error.
  151. */
  152. static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
  153. {
  154. cmpxchg(&dio->error, 0, ret);
  155. }
  156. /*
  157. * Called when dio->ref reaches zero from an I/O completion.
  158. */
  159. static void iomap_dio_done(struct iomap_dio *dio)
  160. {
  161. struct kiocb *iocb = dio->iocb;
  162. if (dio->wait_for_completion) {
  163. /*
  164. * Synchronous I/O, task itself will handle any completion work
  165. * that needs after IO. All we need to do is wake the task.
  166. */
  167. struct task_struct *waiter = dio->submit.waiter;
  168. WRITE_ONCE(dio->submit.waiter, NULL);
  169. blk_wake_io_task(waiter);
  170. return;
  171. }
  172. /*
  173. * Always run error completions in user context. These are not
  174. * performance critical and some code relies on taking sleeping locks
  175. * for error handling.
  176. */
  177. if (dio->error)
  178. dio->flags |= IOMAP_DIO_COMP_WORK;
  179. /*
  180. * Never invalidate pages from this context to avoid deadlocks with
  181. * buffered I/O completions when called from the ioend workqueue,
  182. * or avoid sleeping when called directly from ->bi_end_io.
  183. * Tough luck if you hit the tiny race with someone dirtying the range
  184. * right between this check and the actual completion.
  185. */
  186. if ((dio->flags & IOMAP_DIO_WRITE) &&
  187. !(dio->flags & IOMAP_DIO_COMP_WORK)) {
  188. if (dio->iocb->ki_filp->f_mapping->nrpages)
  189. dio->flags |= IOMAP_DIO_COMP_WORK;
  190. else
  191. dio->flags |= IOMAP_DIO_NO_INVALIDATE;
  192. }
  193. if (dio->flags & IOMAP_DIO_COMP_WORK) {
  194. struct inode *inode = file_inode(iocb->ki_filp);
  195. /*
  196. * Async DIO completion that requires filesystem level
  197. * completion work gets punted to a work queue to complete as
  198. * the operation may require more IO to be issued to finalise
  199. * filesystem metadata changes or guarantee data integrity.
  200. */
  201. INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
  202. queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
  203. return;
  204. }
  205. WRITE_ONCE(iocb->private, NULL);
  206. iomap_dio_complete_work(&dio->aio.work);
  207. }
  208. static void __iomap_dio_bio_end_io(struct bio *bio, bool inline_completion)
  209. {
  210. struct iomap_dio *dio = bio->bi_private;
  211. if (dio->flags & IOMAP_DIO_BOUNCE) {
  212. bio_iov_iter_unbounce(bio, !!dio->error,
  213. dio->flags & IOMAP_DIO_USER_BACKED);
  214. bio_put(bio);
  215. } else if (dio->flags & IOMAP_DIO_USER_BACKED) {
  216. bio_check_pages_dirty(bio);
  217. } else {
  218. bio_release_pages(bio, false);
  219. bio_put(bio);
  220. }
  221. /* Do not touch bio below, we just gave up our reference. */
  222. if (atomic_dec_and_test(&dio->ref)) {
  223. /*
  224. * Avoid another context switch for the completion when already
  225. * called from the ioend completion workqueue.
  226. */
  227. if (inline_completion)
  228. dio->flags &= ~IOMAP_DIO_COMP_WORK;
  229. iomap_dio_done(dio);
  230. }
  231. }
  232. void iomap_dio_bio_end_io(struct bio *bio)
  233. {
  234. struct iomap_dio *dio = bio->bi_private;
  235. if (bio->bi_status)
  236. iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
  237. __iomap_dio_bio_end_io(bio, false);
  238. }
  239. EXPORT_SYMBOL_GPL(iomap_dio_bio_end_io);
  240. u32 iomap_finish_ioend_direct(struct iomap_ioend *ioend)
  241. {
  242. struct iomap_dio *dio = ioend->io_bio.bi_private;
  243. u32 vec_count = ioend->io_bio.bi_vcnt;
  244. if (ioend->io_error)
  245. iomap_dio_set_error(dio, ioend->io_error);
  246. __iomap_dio_bio_end_io(&ioend->io_bio, true);
  247. /*
  248. * Return the number of bvecs completed as even direct I/O completions
  249. * do significant per-folio work and we'll still want to give up the
  250. * CPU after a lot of completions.
  251. */
  252. return vec_count;
  253. }
  254. static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
  255. loff_t pos, unsigned len)
  256. {
  257. struct inode *inode = file_inode(dio->iocb->ki_filp);
  258. struct bio *bio;
  259. struct folio *zero_folio = largest_zero_folio();
  260. int nr_vecs = max(1, i_blocksize(inode) / folio_size(zero_folio));
  261. if (!len)
  262. return 0;
  263. /*
  264. * This limit shall never be reached as most filesystems have a
  265. * maximum blocksize of 64k.
  266. */
  267. if (WARN_ON_ONCE(nr_vecs > BIO_MAX_VECS))
  268. return -EINVAL;
  269. bio = iomap_dio_alloc_bio(iter, dio, nr_vecs,
  270. REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
  271. fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
  272. GFP_KERNEL);
  273. bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
  274. bio->bi_private = dio;
  275. bio->bi_end_io = iomap_dio_bio_end_io;
  276. while (len > 0) {
  277. unsigned int io_len = min(len, folio_size(zero_folio));
  278. bio_add_folio_nofail(bio, zero_folio, io_len, 0);
  279. len -= io_len;
  280. }
  281. iomap_dio_submit_bio(iter, dio, bio, pos);
  282. return 0;
  283. }
  284. static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter,
  285. struct iomap_dio *dio, loff_t pos, unsigned int alignment,
  286. blk_opf_t op)
  287. {
  288. unsigned int nr_vecs;
  289. struct bio *bio;
  290. ssize_t ret;
  291. if (dio->flags & IOMAP_DIO_BOUNCE)
  292. nr_vecs = bio_iov_bounce_nr_vecs(dio->submit.iter, op);
  293. else
  294. nr_vecs = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
  295. bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, op);
  296. fscrypt_set_bio_crypt_ctx(bio, iter->inode,
  297. pos >> iter->inode->i_blkbits, GFP_KERNEL);
  298. bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
  299. bio->bi_write_hint = iter->inode->i_write_hint;
  300. bio->bi_ioprio = dio->iocb->ki_ioprio;
  301. bio->bi_private = dio;
  302. bio->bi_end_io = iomap_dio_bio_end_io;
  303. if (dio->flags & IOMAP_DIO_BOUNCE)
  304. ret = bio_iov_iter_bounce(bio, dio->submit.iter);
  305. else
  306. ret = bio_iov_iter_get_pages(bio, dio->submit.iter,
  307. alignment - 1);
  308. if (unlikely(ret))
  309. goto out_put_bio;
  310. ret = bio->bi_iter.bi_size;
  311. /*
  312. * An atomic write bio must cover the complete length. If it doesn't,
  313. * error out.
  314. */
  315. if ((op & REQ_ATOMIC) && WARN_ON_ONCE(ret != iomap_length(iter))) {
  316. ret = -EINVAL;
  317. goto out_put_bio;
  318. }
  319. if (dio->flags & IOMAP_DIO_WRITE)
  320. task_io_account_write(ret);
  321. else if ((dio->flags & IOMAP_DIO_USER_BACKED) &&
  322. !(dio->flags & IOMAP_DIO_BOUNCE))
  323. bio_set_pages_dirty(bio);
  324. /*
  325. * We can only poll for single bio I/Os.
  326. */
  327. if (iov_iter_count(dio->submit.iter))
  328. dio->iocb->ki_flags &= ~IOCB_HIPRI;
  329. iomap_dio_submit_bio(iter, dio, bio, pos);
  330. return ret;
  331. out_put_bio:
  332. bio_put(bio);
  333. return ret;
  334. }
  335. static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
  336. {
  337. const struct iomap *iomap = &iter->iomap;
  338. struct inode *inode = iter->inode;
  339. unsigned int fs_block_size = i_blocksize(inode), pad;
  340. const loff_t length = iomap_length(iter);
  341. loff_t pos = iter->pos;
  342. blk_opf_t bio_opf = REQ_SYNC | REQ_IDLE;
  343. bool need_zeroout = false;
  344. u64 copied = 0;
  345. size_t orig_count;
  346. unsigned int alignment;
  347. ssize_t ret = 0;
  348. /*
  349. * File systems that write out of place and always allocate new blocks
  350. * need each bio to be block aligned as that's the unit of allocation.
  351. */
  352. if (dio->flags & IOMAP_DIO_FSBLOCK_ALIGNED)
  353. alignment = fs_block_size;
  354. else
  355. alignment = bdev_logical_block_size(iomap->bdev);
  356. if ((pos | length) & (alignment - 1))
  357. return -EINVAL;
  358. if (dio->flags & IOMAP_DIO_WRITE) {
  359. bool need_completion_work = true;
  360. switch (iomap->type) {
  361. case IOMAP_MAPPED:
  362. /*
  363. * Directly mapped I/O does not inherently need to do
  364. * work at I/O completion time. But there are various
  365. * cases below where this will get set again.
  366. */
  367. need_completion_work = false;
  368. break;
  369. case IOMAP_UNWRITTEN:
  370. dio->flags |= IOMAP_DIO_UNWRITTEN;
  371. need_zeroout = true;
  372. break;
  373. default:
  374. break;
  375. }
  376. if (iomap->flags & IOMAP_F_ATOMIC_BIO) {
  377. /*
  378. * Ensure that the mapping covers the full write
  379. * length, otherwise it won't be submitted as a single
  380. * bio, which is required to use hardware atomics.
  381. */
  382. if (length != iter->len)
  383. return -EINVAL;
  384. bio_opf |= REQ_ATOMIC;
  385. }
  386. if (iomap->flags & IOMAP_F_SHARED) {
  387. /*
  388. * Unsharing of needs to update metadata at I/O
  389. * completion time.
  390. */
  391. need_completion_work = true;
  392. dio->flags |= IOMAP_DIO_COW;
  393. }
  394. if (iomap->flags & IOMAP_F_NEW) {
  395. /*
  396. * Newly allocated blocks might need recording in
  397. * metadata at I/O completion time.
  398. */
  399. need_completion_work = true;
  400. need_zeroout = true;
  401. }
  402. /*
  403. * Use a FUA write if we need datasync semantics and this is a
  404. * pure overwrite that doesn't require any metadata updates.
  405. *
  406. * This allows us to avoid cache flushes on I/O completion.
  407. */
  408. if (dio->flags & IOMAP_DIO_WRITE_THROUGH) {
  409. if (!need_completion_work &&
  410. !(iomap->flags & IOMAP_F_DIRTY) &&
  411. (!bdev_write_cache(iomap->bdev) ||
  412. bdev_fua(iomap->bdev)))
  413. bio_opf |= REQ_FUA;
  414. else
  415. dio->flags &= ~IOMAP_DIO_WRITE_THROUGH;
  416. }
  417. /*
  418. * We can only do inline completion for pure overwrites that
  419. * don't require additional I/O at completion time.
  420. *
  421. * This rules out writes that need zeroing or metdata updates to
  422. * convert unwritten or shared extents.
  423. *
  424. * Writes that extend i_size are also not supported, but this is
  425. * handled in __iomap_dio_rw().
  426. */
  427. if (need_completion_work)
  428. dio->flags |= IOMAP_DIO_COMP_WORK;
  429. bio_opf |= REQ_OP_WRITE;
  430. } else {
  431. bio_opf |= REQ_OP_READ;
  432. }
  433. /*
  434. * Save the original count and trim the iter to just the extent we
  435. * are operating on right now. The iter will be re-expanded once
  436. * we are done.
  437. */
  438. orig_count = iov_iter_count(dio->submit.iter);
  439. iov_iter_truncate(dio->submit.iter, length);
  440. if (!iov_iter_count(dio->submit.iter))
  441. goto out;
  442. /*
  443. * The rules for polled IO completions follow the guidelines as the
  444. * ones we set for inline and deferred completions. If none of those
  445. * are available for this IO, clear the polled flag.
  446. */
  447. if (dio->flags & IOMAP_DIO_COMP_WORK)
  448. dio->iocb->ki_flags &= ~IOCB_HIPRI;
  449. if (need_zeroout) {
  450. /* zero out from the start of the block to the write offset */
  451. pad = pos & (fs_block_size - 1);
  452. ret = iomap_dio_zero(iter, dio, pos - pad, pad);
  453. if (ret)
  454. goto out;
  455. }
  456. do {
  457. /*
  458. * If completions already occurred and reported errors, give up now and
  459. * don't bother submitting more bios.
  460. */
  461. if (unlikely(data_race(dio->error)))
  462. goto out;
  463. ret = iomap_dio_bio_iter_one(iter, dio, pos, alignment, bio_opf);
  464. if (unlikely(ret < 0)) {
  465. /*
  466. * We have to stop part way through an IO. We must fall
  467. * through to the sub-block tail zeroing here, otherwise
  468. * this short IO may expose stale data in the tail of
  469. * the block we haven't written data to.
  470. */
  471. break;
  472. }
  473. dio->size += ret;
  474. copied += ret;
  475. pos += ret;
  476. ret = 0;
  477. } while (iov_iter_count(dio->submit.iter));
  478. /*
  479. * We need to zeroout the tail of a sub-block write if the extent type
  480. * requires zeroing or the write extends beyond EOF. If we don't zero
  481. * the block tail in the latter case, we can expose stale data via mmap
  482. * reads of the EOF block.
  483. */
  484. if (need_zeroout ||
  485. ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
  486. /* zero out from the end of the write to the end of the block */
  487. pad = pos & (fs_block_size - 1);
  488. if (pad)
  489. ret = iomap_dio_zero(iter, dio, pos,
  490. fs_block_size - pad);
  491. }
  492. out:
  493. /* Undo iter limitation to current extent */
  494. iov_iter_reexpand(dio->submit.iter, orig_count - copied);
  495. if (copied)
  496. return iomap_iter_advance(iter, copied);
  497. return ret;
  498. }
  499. static int iomap_dio_hole_iter(struct iomap_iter *iter, struct iomap_dio *dio)
  500. {
  501. loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter);
  502. dio->size += length;
  503. if (!length)
  504. return -EFAULT;
  505. return iomap_iter_advance(iter, length);
  506. }
  507. static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio)
  508. {
  509. const struct iomap *iomap = &iomi->iomap;
  510. struct iov_iter *iter = dio->submit.iter;
  511. void *inline_data = iomap_inline_data(iomap, iomi->pos);
  512. loff_t length = iomap_length(iomi);
  513. loff_t pos = iomi->pos;
  514. u64 copied;
  515. if (WARN_ON_ONCE(!inline_data))
  516. return -EIO;
  517. if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
  518. return -EIO;
  519. if (dio->flags & IOMAP_DIO_WRITE) {
  520. loff_t size = iomi->inode->i_size;
  521. if (pos > size)
  522. memset(iomap_inline_data(iomap, size), 0, pos - size);
  523. copied = copy_from_iter(inline_data, length, iter);
  524. if (copied) {
  525. if (pos + copied > size)
  526. i_size_write(iomi->inode, pos + copied);
  527. mark_inode_dirty(iomi->inode);
  528. }
  529. } else {
  530. copied = copy_to_iter(inline_data, length, iter);
  531. }
  532. dio->size += copied;
  533. if (!copied)
  534. return -EFAULT;
  535. return iomap_iter_advance(iomi, copied);
  536. }
  537. static int iomap_dio_iter(struct iomap_iter *iter, struct iomap_dio *dio)
  538. {
  539. switch (iter->iomap.type) {
  540. case IOMAP_HOLE:
  541. if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
  542. return -EIO;
  543. return iomap_dio_hole_iter(iter, dio);
  544. case IOMAP_UNWRITTEN:
  545. if (!(dio->flags & IOMAP_DIO_WRITE))
  546. return iomap_dio_hole_iter(iter, dio);
  547. return iomap_dio_bio_iter(iter, dio);
  548. case IOMAP_MAPPED:
  549. return iomap_dio_bio_iter(iter, dio);
  550. case IOMAP_INLINE:
  551. return iomap_dio_inline_iter(iter, dio);
  552. case IOMAP_DELALLOC:
  553. /*
  554. * DIO is not serialised against mmap() access at all, and so
  555. * if the page_mkwrite occurs between the writeback and the
  556. * iomap_iter() call in the DIO path, then it will see the
  557. * DELALLOC block that the page-mkwrite allocated.
  558. */
  559. pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
  560. dio->iocb->ki_filp, current->comm);
  561. return -EIO;
  562. default:
  563. WARN_ON_ONCE(1);
  564. return -EIO;
  565. }
  566. }
  567. /*
  568. * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
  569. * is being issued as AIO or not. This allows us to optimise pure data writes
  570. * to use REQ_FUA rather than requiring generic_write_sync() to issue a
  571. * REQ_FLUSH post write. This is slightly tricky because a single request here
  572. * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
  573. * may be pure data writes. In that case, we still need to do a full data sync
  574. * completion.
  575. *
  576. * When page faults are disabled and @dio_flags includes IOMAP_DIO_PARTIAL,
  577. * __iomap_dio_rw can return a partial result if it encounters a non-resident
  578. * page in @iter after preparing a transfer. In that case, the non-resident
  579. * pages can be faulted in and the request resumed with @done_before set to the
  580. * number of bytes previously transferred. The request will then complete with
  581. * the correct total number of bytes transferred; this is essential for
  582. * completing partial requests asynchronously.
  583. *
  584. * Returns -ENOTBLK In case of a page invalidation invalidation failure for
  585. * writes. The callers needs to fall back to buffered I/O in this case.
  586. */
  587. struct iomap_dio *
  588. __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
  589. const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
  590. unsigned int dio_flags, void *private, size_t done_before)
  591. {
  592. struct inode *inode = file_inode(iocb->ki_filp);
  593. struct iomap_iter iomi = {
  594. .inode = inode,
  595. .pos = iocb->ki_pos,
  596. .len = iov_iter_count(iter),
  597. .flags = IOMAP_DIRECT,
  598. .private = private,
  599. };
  600. bool wait_for_completion =
  601. is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
  602. struct blk_plug plug;
  603. struct iomap_dio *dio;
  604. loff_t ret = 0;
  605. trace_iomap_dio_rw_begin(iocb, iter, dio_flags, done_before);
  606. if (!iomi.len)
  607. return NULL;
  608. dio = kmalloc_obj(*dio);
  609. if (!dio)
  610. return ERR_PTR(-ENOMEM);
  611. dio->iocb = iocb;
  612. atomic_set(&dio->ref, 1);
  613. dio->size = 0;
  614. dio->i_size = i_size_read(inode);
  615. dio->dops = dops;
  616. dio->error = 0;
  617. dio->flags = dio_flags & (IOMAP_DIO_FSBLOCK_ALIGNED | IOMAP_DIO_BOUNCE);
  618. dio->done_before = done_before;
  619. dio->submit.iter = iter;
  620. dio->submit.waiter = current;
  621. if (iocb->ki_flags & IOCB_NOWAIT)
  622. iomi.flags |= IOMAP_NOWAIT;
  623. if (iov_iter_rw(iter) == READ) {
  624. if (iomi.pos >= dio->i_size)
  625. goto out_free_dio;
  626. if (user_backed_iter(iter))
  627. dio->flags |= IOMAP_DIO_USER_BACKED;
  628. ret = kiocb_write_and_wait(iocb, iomi.len);
  629. if (ret)
  630. goto out_free_dio;
  631. } else {
  632. iomi.flags |= IOMAP_WRITE;
  633. dio->flags |= IOMAP_DIO_WRITE;
  634. if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
  635. ret = -EAGAIN;
  636. if (iomi.pos >= dio->i_size ||
  637. iomi.pos + iomi.len > dio->i_size)
  638. goto out_free_dio;
  639. iomi.flags |= IOMAP_OVERWRITE_ONLY;
  640. }
  641. if (iocb->ki_flags & IOCB_ATOMIC)
  642. iomi.flags |= IOMAP_ATOMIC;
  643. /* for data sync or sync, we need sync completion processing */
  644. if (iocb_is_dsync(iocb)) {
  645. dio->flags |= IOMAP_DIO_NEED_SYNC;
  646. /*
  647. * For datasync only writes, we optimistically try using
  648. * WRITE_THROUGH for this IO. This flag requires either
  649. * FUA writes through the device's write cache, or a
  650. * normal write to a device without a volatile write
  651. * cache. For the former, Any non-FUA write that occurs
  652. * will clear this flag, hence we know before completion
  653. * whether a cache flush is necessary.
  654. */
  655. if (!(iocb->ki_flags & IOCB_SYNC))
  656. dio->flags |= IOMAP_DIO_WRITE_THROUGH;
  657. }
  658. /*
  659. * i_size updates must to happen from process context.
  660. */
  661. if (iomi.pos + iomi.len > dio->i_size)
  662. dio->flags |= IOMAP_DIO_COMP_WORK;
  663. /*
  664. * Try to invalidate cache pages for the range we are writing.
  665. * If this invalidation fails, let the caller fall back to
  666. * buffered I/O.
  667. */
  668. ret = kiocb_invalidate_pages(iocb, iomi.len);
  669. if (ret) {
  670. if (ret != -EAGAIN) {
  671. trace_iomap_dio_invalidate_fail(inode, iomi.pos,
  672. iomi.len);
  673. if (iocb->ki_flags & IOCB_ATOMIC) {
  674. /*
  675. * folio invalidation failed, maybe
  676. * this is transient, unlock and see if
  677. * the caller tries again.
  678. */
  679. ret = -EAGAIN;
  680. } else {
  681. /* fall back to buffered write */
  682. ret = -ENOTBLK;
  683. }
  684. }
  685. goto out_free_dio;
  686. }
  687. }
  688. if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
  689. ret = sb_init_dio_done_wq(inode->i_sb);
  690. if (ret < 0)
  691. goto out_free_dio;
  692. }
  693. inode_dio_begin(inode);
  694. blk_start_plug(&plug);
  695. while ((ret = iomap_iter(&iomi, ops)) > 0) {
  696. iomi.status = iomap_dio_iter(&iomi, dio);
  697. /*
  698. * We can only poll for single bio I/Os.
  699. */
  700. iocb->ki_flags &= ~IOCB_HIPRI;
  701. }
  702. blk_finish_plug(&plug);
  703. /*
  704. * We only report that we've read data up to i_size.
  705. * Revert iter to a state corresponding to that as some callers (such
  706. * as the splice code) rely on it.
  707. */
  708. if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size)
  709. iov_iter_revert(iter, iomi.pos - dio->i_size);
  710. if (ret == -EFAULT && dio->size && (dio_flags & IOMAP_DIO_PARTIAL)) {
  711. if (!(iocb->ki_flags & IOCB_NOWAIT))
  712. wait_for_completion = true;
  713. ret = 0;
  714. }
  715. /* magic error code to fall back to buffered I/O */
  716. if (ret == -ENOTBLK) {
  717. wait_for_completion = true;
  718. ret = 0;
  719. }
  720. if (ret < 0)
  721. iomap_dio_set_error(dio, ret);
  722. /*
  723. * If all the writes we issued were already written through to the
  724. * media, we don't need to flush the cache on IO completion. Clear the
  725. * sync flag for this case.
  726. *
  727. * Otherwise clear the inline completion flag if any sync work is
  728. * needed, as that needs to be performed from process context.
  729. */
  730. if (dio->flags & IOMAP_DIO_WRITE_THROUGH)
  731. dio->flags &= ~IOMAP_DIO_NEED_SYNC;
  732. else if (dio->flags & IOMAP_DIO_NEED_SYNC)
  733. dio->flags |= IOMAP_DIO_COMP_WORK;
  734. /*
  735. * We are about to drop our additional submission reference, which
  736. * might be the last reference to the dio. There are three different
  737. * ways we can progress here:
  738. *
  739. * (a) If this is the last reference we will always complete and free
  740. * the dio ourselves.
  741. * (b) If this is not the last reference, and we serve an asynchronous
  742. * iocb, we must never touch the dio after the decrement, the
  743. * I/O completion handler will complete and free it.
  744. * (c) If this is not the last reference, but we serve a synchronous
  745. * iocb, the I/O completion handler will wake us up on the drop
  746. * of the final reference, and we will complete and free it here
  747. * after we got woken by the I/O completion handler.
  748. */
  749. dio->wait_for_completion = wait_for_completion;
  750. if (!atomic_dec_and_test(&dio->ref)) {
  751. if (!wait_for_completion) {
  752. trace_iomap_dio_rw_queued(inode, iomi.pos, iomi.len);
  753. return ERR_PTR(-EIOCBQUEUED);
  754. }
  755. for (;;) {
  756. set_current_state(TASK_UNINTERRUPTIBLE);
  757. if (!READ_ONCE(dio->submit.waiter))
  758. break;
  759. blk_io_schedule();
  760. }
  761. __set_current_state(TASK_RUNNING);
  762. }
  763. return dio;
  764. out_free_dio:
  765. kfree(dio);
  766. if (ret)
  767. return ERR_PTR(ret);
  768. return NULL;
  769. }
  770. EXPORT_SYMBOL_GPL(__iomap_dio_rw);
  771. ssize_t
  772. iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
  773. const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
  774. unsigned int dio_flags, void *private, size_t done_before)
  775. {
  776. struct iomap_dio *dio;
  777. dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags, private,
  778. done_before);
  779. if (IS_ERR_OR_NULL(dio))
  780. return PTR_ERR_OR_ZERO(dio);
  781. return iomap_dio_complete(dio);
  782. }
  783. EXPORT_SYMBOL_GPL(iomap_dio_rw);