1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (c) 2016-2021 Christoph Hellwig.
6 #include <linux/module.h>
7 #include <linux/compiler.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>
17 #include "../internal.h"
20 * Private flags for iomap_dio, must not overlap with the public ones in
23 #define IOMAP_DIO_WRITE_FUA (1 << 28)
24 #define IOMAP_DIO_NEED_SYNC (1 << 29)
25 #define IOMAP_DIO_WRITE (1 << 30)
26 #define IOMAP_DIO_DIRTY (1 << 31)
30 const struct iomap_dio_ops *dops;
37 bool wait_for_completion;
40 /* used during submission and for synchronous completion: */
42 struct iov_iter *iter;
43 struct task_struct *waiter;
47 /* used for aio completion: */
49 struct work_struct work;
54 static struct bio *iomap_dio_alloc_bio(const struct iomap_iter *iter,
55 struct iomap_dio *dio, unsigned short nr_vecs, blk_opf_t opf)
57 if (dio->dops && dio->dops->bio_set)
58 return bio_alloc_bioset(iter->iomap.bdev, nr_vecs, opf,
59 GFP_KERNEL, dio->dops->bio_set);
60 return bio_alloc(iter->iomap.bdev, nr_vecs, opf, GFP_KERNEL);
63 static void iomap_dio_submit_bio(const struct iomap_iter *iter,
64 struct iomap_dio *dio, struct bio *bio, loff_t pos)
66 atomic_inc(&dio->ref);
68 /* Sync dio can't be polled reliably */
69 if ((dio->iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(dio->iocb)) {
70 bio_set_polled(bio, dio->iocb);
71 dio->submit.poll_bio = bio;
74 if (dio->dops && dio->dops->submit_io)
75 dio->dops->submit_io(iter, bio, pos);
80 ssize_t iomap_dio_complete(struct iomap_dio *dio)
82 const struct iomap_dio_ops *dops = dio->dops;
83 struct kiocb *iocb = dio->iocb;
84 struct inode *inode = file_inode(iocb->ki_filp);
85 loff_t offset = iocb->ki_pos;
86 ssize_t ret = dio->error;
88 if (dops && dops->end_io)
89 ret = dops->end_io(iocb, dio->size, ret, dio->flags);
93 /* check for short read */
94 if (offset + ret > dio->i_size &&
95 !(dio->flags & IOMAP_DIO_WRITE))
96 ret = dio->i_size - offset;
101 * Try again to invalidate clean pages which might have been cached by
102 * non-direct readahead, or faulted in by get_user_pages() if the source
103 * of the write was an mmap'ed region of the file we're writing. Either
104 * one is a pretty crazy thing to do, so we don't support it 100%. If
105 * this invalidation fails, tough, the write still worked...
107 * And this page cache invalidation has to be after ->end_io(), as some
108 * filesystems convert unwritten extents to real allocations in
109 * ->end_io() when necessary, otherwise a racing buffer read would cache
110 * zeros from unwritten extents.
112 if (!dio->error && dio->size &&
113 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
115 err = invalidate_inode_pages2_range(inode->i_mapping,
116 offset >> PAGE_SHIFT,
117 (offset + dio->size - 1) >> PAGE_SHIFT);
119 dio_warn_stale_pagecache(iocb->ki_filp);
122 inode_dio_end(file_inode(iocb->ki_filp));
124 * If this is a DSYNC write, make sure we push it to stable storage now
125 * that we've written data.
127 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
128 ret = generic_write_sync(iocb, ret);
131 ret += dio->done_before;
133 trace_iomap_dio_complete(iocb, dio->error, ret);
138 EXPORT_SYMBOL_GPL(iomap_dio_complete);
140 static void iomap_dio_complete_work(struct work_struct *work)
142 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
143 struct kiocb *iocb = dio->iocb;
145 iocb->ki_complete(iocb, iomap_dio_complete(dio));
149 * Set an error in the dio if none is set yet. We have to use cmpxchg
150 * as the submission context and the completion context(s) can race to
153 static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
155 cmpxchg(&dio->error, 0, ret);
158 void iomap_dio_bio_end_io(struct bio *bio)
160 struct iomap_dio *dio = bio->bi_private;
161 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
164 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
166 if (atomic_dec_and_test(&dio->ref)) {
167 if (dio->wait_for_completion) {
168 struct task_struct *waiter = dio->submit.waiter;
169 WRITE_ONCE(dio->submit.waiter, NULL);
170 blk_wake_io_task(waiter);
171 } else if (dio->flags & IOMAP_DIO_WRITE) {
172 struct inode *inode = file_inode(dio->iocb->ki_filp);
174 WRITE_ONCE(dio->iocb->private, NULL);
175 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
176 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
178 WRITE_ONCE(dio->iocb->private, NULL);
179 iomap_dio_complete_work(&dio->aio.work);
184 bio_check_pages_dirty(bio);
186 bio_release_pages(bio, false);
190 EXPORT_SYMBOL_GPL(iomap_dio_bio_end_io);
192 static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
193 loff_t pos, unsigned len)
195 struct inode *inode = file_inode(dio->iocb->ki_filp);
196 struct page *page = ZERO_PAGE(0);
199 bio = iomap_dio_alloc_bio(iter, dio, 1, REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
200 fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
202 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
203 bio->bi_private = dio;
204 bio->bi_end_io = iomap_dio_bio_end_io;
207 __bio_add_page(bio, page, len, 0);
208 iomap_dio_submit_bio(iter, dio, bio, pos);
212 * Figure out the bio's operation flags from the dio request, the
213 * mapping, and whether or not we want FUA. Note that we can end up
214 * clearing the WRITE_FUA flag in the dio request.
216 static inline blk_opf_t iomap_dio_bio_opflags(struct iomap_dio *dio,
217 const struct iomap *iomap, bool use_fua)
219 blk_opf_t opflags = REQ_SYNC | REQ_IDLE;
221 if (!(dio->flags & IOMAP_DIO_WRITE))
224 opflags |= REQ_OP_WRITE;
228 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
233 static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter,
234 struct iomap_dio *dio)
236 const struct iomap *iomap = &iter->iomap;
237 struct inode *inode = iter->inode;
238 unsigned int fs_block_size = i_blocksize(inode), pad;
239 loff_t length = iomap_length(iter);
240 loff_t pos = iter->pos;
243 bool need_zeroout = false;
244 bool use_fua = false;
245 int nr_pages, ret = 0;
249 if ((pos | length) & (bdev_logical_block_size(iomap->bdev) - 1) ||
250 !bdev_iter_is_aligned(iomap->bdev, dio->submit.iter))
253 if (iomap->type == IOMAP_UNWRITTEN) {
254 dio->flags |= IOMAP_DIO_UNWRITTEN;
258 if (iomap->flags & IOMAP_F_SHARED)
259 dio->flags |= IOMAP_DIO_COW;
261 if (iomap->flags & IOMAP_F_NEW) {
263 } else if (iomap->type == IOMAP_MAPPED) {
265 * Use a FUA write if we need datasync semantics, this is a pure
266 * data IO that doesn't require any metadata updates (including
267 * after IO completion such as unwritten extent conversion) and
268 * the underlying device supports FUA. This allows us to avoid
269 * cache flushes on IO completion.
271 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
272 (dio->flags & IOMAP_DIO_WRITE_FUA) && bdev_fua(iomap->bdev))
277 * Save the original count and trim the iter to just the extent we
278 * are operating on right now. The iter will be re-expanded once
281 orig_count = iov_iter_count(dio->submit.iter);
282 iov_iter_truncate(dio->submit.iter, length);
284 if (!iov_iter_count(dio->submit.iter))
288 * We can only poll for single bio I/Os.
291 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode)))
292 dio->iocb->ki_flags &= ~IOCB_HIPRI;
295 /* zero out from the start of the block to the write offset */
296 pad = pos & (fs_block_size - 1);
298 iomap_dio_zero(iter, dio, pos - pad, pad);
302 * Set the operation flags early so that bio_iov_iter_get_pages
303 * can set up the page vector appropriately for a ZONE_APPEND
306 bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua);
308 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
312 iov_iter_revert(dio->submit.iter, copied);
317 bio = iomap_dio_alloc_bio(iter, dio, nr_pages, bio_opf);
318 fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits,
320 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
321 bio->bi_ioprio = dio->iocb->ki_ioprio;
322 bio->bi_private = dio;
323 bio->bi_end_io = iomap_dio_bio_end_io;
325 ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
328 * We have to stop part way through an IO. We must fall
329 * through to the sub-block tail zeroing here, otherwise
330 * this short IO may expose stale data in the tail of
331 * the block we haven't written data to.
337 n = bio->bi_iter.bi_size;
338 if (dio->flags & IOMAP_DIO_WRITE) {
339 task_io_account_write(n);
341 if (dio->flags & IOMAP_DIO_DIRTY)
342 bio_set_pages_dirty(bio);
348 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter,
351 * We can only poll for single bio I/Os.
354 dio->iocb->ki_flags &= ~IOCB_HIPRI;
355 iomap_dio_submit_bio(iter, dio, bio, pos);
360 * We need to zeroout the tail of a sub-block write if the extent type
361 * requires zeroing or the write extends beyond EOF. If we don't zero
362 * the block tail in the latter case, we can expose stale data via mmap
363 * reads of the EOF block.
367 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
368 /* zero out from the end of the write to the end of the block */
369 pad = pos & (fs_block_size - 1);
371 iomap_dio_zero(iter, dio, pos, fs_block_size - pad);
374 /* Undo iter limitation to current extent */
375 iov_iter_reexpand(dio->submit.iter, orig_count - copied);
381 static loff_t iomap_dio_hole_iter(const struct iomap_iter *iter,
382 struct iomap_dio *dio)
384 loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter);
392 static loff_t iomap_dio_inline_iter(const struct iomap_iter *iomi,
393 struct iomap_dio *dio)
395 const struct iomap *iomap = &iomi->iomap;
396 struct iov_iter *iter = dio->submit.iter;
397 void *inline_data = iomap_inline_data(iomap, iomi->pos);
398 loff_t length = iomap_length(iomi);
399 loff_t pos = iomi->pos;
402 if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
405 if (dio->flags & IOMAP_DIO_WRITE) {
406 loff_t size = iomi->inode->i_size;
409 memset(iomap_inline_data(iomap, size), 0, pos - size);
410 copied = copy_from_iter(inline_data, length, iter);
412 if (pos + copied > size)
413 i_size_write(iomi->inode, pos + copied);
414 mark_inode_dirty(iomi->inode);
417 copied = copy_to_iter(inline_data, length, iter);
425 static loff_t iomap_dio_iter(const struct iomap_iter *iter,
426 struct iomap_dio *dio)
428 switch (iter->iomap.type) {
430 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
432 return iomap_dio_hole_iter(iter, dio);
433 case IOMAP_UNWRITTEN:
434 if (!(dio->flags & IOMAP_DIO_WRITE))
435 return iomap_dio_hole_iter(iter, dio);
436 return iomap_dio_bio_iter(iter, dio);
438 return iomap_dio_bio_iter(iter, dio);
440 return iomap_dio_inline_iter(iter, dio);
443 * DIO is not serialised against mmap() access at all, and so
444 * if the page_mkwrite occurs between the writeback and the
445 * iomap_iter() call in the DIO path, then it will see the
446 * DELALLOC block that the page-mkwrite allocated.
448 pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
449 dio->iocb->ki_filp, current->comm);
458 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
459 * is being issued as AIO or not. This allows us to optimise pure data writes
460 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
461 * REQ_FLUSH post write. This is slightly tricky because a single request here
462 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
463 * may be pure data writes. In that case, we still need to do a full data sync
466 * When page faults are disabled and @dio_flags includes IOMAP_DIO_PARTIAL,
467 * __iomap_dio_rw can return a partial result if it encounters a non-resident
468 * page in @iter after preparing a transfer. In that case, the non-resident
469 * pages can be faulted in and the request resumed with @done_before set to the
470 * number of bytes previously transferred. The request will then complete with
471 * the correct total number of bytes transferred; this is essential for
472 * completing partial requests asynchronously.
474 * Returns -ENOTBLK In case of a page invalidation invalidation failure for
475 * writes. The callers needs to fall back to buffered I/O in this case.
478 __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
479 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
480 unsigned int dio_flags, void *private, size_t done_before)
482 struct address_space *mapping = iocb->ki_filp->f_mapping;
483 struct inode *inode = file_inode(iocb->ki_filp);
484 struct iomap_iter iomi = {
487 .len = iov_iter_count(iter),
488 .flags = IOMAP_DIRECT,
491 loff_t end = iomi.pos + iomi.len - 1, ret = 0;
492 bool wait_for_completion =
493 is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
494 struct blk_plug plug;
495 struct iomap_dio *dio;
497 trace_iomap_dio_rw_begin(iocb, iter, dio_flags, done_before);
502 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
504 return ERR_PTR(-ENOMEM);
507 atomic_set(&dio->ref, 1);
509 dio->i_size = i_size_read(inode);
513 dio->done_before = done_before;
515 dio->submit.iter = iter;
516 dio->submit.waiter = current;
517 dio->submit.poll_bio = NULL;
519 if (iov_iter_rw(iter) == READ) {
520 if (iomi.pos >= dio->i_size)
523 if (iocb->ki_flags & IOCB_NOWAIT) {
524 if (filemap_range_needs_writeback(mapping, iomi.pos,
529 iomi.flags |= IOMAP_NOWAIT;
532 if (user_backed_iter(iter))
533 dio->flags |= IOMAP_DIO_DIRTY;
535 iomi.flags |= IOMAP_WRITE;
536 dio->flags |= IOMAP_DIO_WRITE;
538 if (iocb->ki_flags & IOCB_NOWAIT) {
539 if (filemap_range_has_page(mapping, iomi.pos, end)) {
543 iomi.flags |= IOMAP_NOWAIT;
546 /* for data sync or sync, we need sync completion processing */
547 if (iocb_is_dsync(iocb)) {
548 dio->flags |= IOMAP_DIO_NEED_SYNC;
551 * For datasync only writes, we optimistically try
552 * using FUA for this IO. Any non-FUA write that
553 * occurs will clear this flag, hence we know before
554 * completion whether a cache flush is necessary.
556 if (!(iocb->ki_flags & IOCB_SYNC))
557 dio->flags |= IOMAP_DIO_WRITE_FUA;
561 if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
563 if (iomi.pos >= dio->i_size ||
564 iomi.pos + iomi.len > dio->i_size)
566 iomi.flags |= IOMAP_OVERWRITE_ONLY;
569 ret = filemap_write_and_wait_range(mapping, iomi.pos, end);
573 if (iov_iter_rw(iter) == WRITE) {
575 * Try to invalidate cache pages for the range we are writing.
576 * If this invalidation fails, let the caller fall back to
579 if (invalidate_inode_pages2_range(mapping,
580 iomi.pos >> PAGE_SHIFT, end >> PAGE_SHIFT)) {
581 trace_iomap_dio_invalidate_fail(inode, iomi.pos,
587 if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
588 ret = sb_init_dio_done_wq(inode->i_sb);
594 inode_dio_begin(inode);
596 blk_start_plug(&plug);
597 while ((ret = iomap_iter(&iomi, ops)) > 0) {
598 iomi.processed = iomap_dio_iter(&iomi, dio);
601 * We can only poll for single bio I/Os.
603 iocb->ki_flags &= ~IOCB_HIPRI;
606 blk_finish_plug(&plug);
609 * We only report that we've read data up to i_size.
610 * Revert iter to a state corresponding to that as some callers (such
611 * as the splice code) rely on it.
613 if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size)
614 iov_iter_revert(iter, iomi.pos - dio->i_size);
616 if (ret == -EFAULT && dio->size && (dio_flags & IOMAP_DIO_PARTIAL)) {
617 if (!(iocb->ki_flags & IOCB_NOWAIT))
618 wait_for_completion = true;
622 /* magic error code to fall back to buffered I/O */
623 if (ret == -ENOTBLK) {
624 wait_for_completion = true;
628 iomap_dio_set_error(dio, ret);
631 * If all the writes we issued were FUA, we don't need to flush the
632 * cache on IO completion. Clear the sync flag for this case.
634 if (dio->flags & IOMAP_DIO_WRITE_FUA)
635 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
637 WRITE_ONCE(iocb->private, dio->submit.poll_bio);
640 * We are about to drop our additional submission reference, which
641 * might be the last reference to the dio. There are three different
642 * ways we can progress here:
644 * (a) If this is the last reference we will always complete and free
646 * (b) If this is not the last reference, and we serve an asynchronous
647 * iocb, we must never touch the dio after the decrement, the
648 * I/O completion handler will complete and free it.
649 * (c) If this is not the last reference, but we serve a synchronous
650 * iocb, the I/O completion handler will wake us up on the drop
651 * of the final reference, and we will complete and free it here
652 * after we got woken by the I/O completion handler.
654 dio->wait_for_completion = wait_for_completion;
655 if (!atomic_dec_and_test(&dio->ref)) {
656 if (!wait_for_completion) {
657 trace_iomap_dio_rw_queued(inode, iomi.pos, iomi.len);
658 return ERR_PTR(-EIOCBQUEUED);
662 set_current_state(TASK_UNINTERRUPTIBLE);
663 if (!READ_ONCE(dio->submit.waiter))
668 __set_current_state(TASK_RUNNING);
679 EXPORT_SYMBOL_GPL(__iomap_dio_rw);
682 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
683 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
684 unsigned int dio_flags, void *private, size_t done_before)
686 struct iomap_dio *dio;
688 dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags, private,
690 if (IS_ERR_OR_NULL(dio))
691 return PTR_ERR_OR_ZERO(dio);
692 return iomap_dio_complete(dio);
694 EXPORT_SYMBOL_GPL(iomap_dio_rw);