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/iomap.h>
10 #include <linux/backing-dev.h>
11 #include <linux/uio.h>
12 #include <linux/task_io_accounting_ops.h>
15 #include "../internal.h"
18 * Private flags for iomap_dio, must not overlap with the public ones in
21 #define IOMAP_DIO_WRITE_FUA (1 << 28)
22 #define IOMAP_DIO_NEED_SYNC (1 << 29)
23 #define IOMAP_DIO_WRITE (1 << 30)
24 #define IOMAP_DIO_DIRTY (1 << 31)
28 const struct iomap_dio_ops *dops;
34 bool wait_for_completion;
37 /* used during submission and for synchronous completion: */
39 struct iov_iter *iter;
40 struct task_struct *waiter;
41 struct request_queue *last_queue;
45 /* used for aio completion: */
47 struct work_struct work;
52 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin)
54 struct request_queue *q = READ_ONCE(kiocb->private);
58 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin);
60 EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
62 static void iomap_dio_submit_bio(const struct iomap_iter *iter,
63 struct iomap_dio *dio, struct bio *bio, loff_t pos)
65 atomic_inc(&dio->ref);
67 if (dio->iocb->ki_flags & IOCB_HIPRI)
68 bio_set_polled(bio, dio->iocb);
70 dio->submit.last_queue = bdev_get_queue(iter->iomap.bdev);
71 if (dio->dops && dio->dops->submit_io)
72 dio->submit.cookie = dio->dops->submit_io(iter, bio, pos);
74 dio->submit.cookie = submit_bio(bio);
77 ssize_t iomap_dio_complete(struct iomap_dio *dio)
79 const struct iomap_dio_ops *dops = dio->dops;
80 struct kiocb *iocb = dio->iocb;
81 struct inode *inode = file_inode(iocb->ki_filp);
82 loff_t offset = iocb->ki_pos;
83 ssize_t ret = dio->error;
85 if (dops && dops->end_io)
86 ret = dops->end_io(iocb, dio->size, ret, dio->flags);
90 /* check for short read */
91 if (offset + ret > dio->i_size &&
92 !(dio->flags & IOMAP_DIO_WRITE))
93 ret = dio->i_size - offset;
98 * Try again to invalidate clean pages which might have been cached by
99 * non-direct readahead, or faulted in by get_user_pages() if the source
100 * of the write was an mmap'ed region of the file we're writing. Either
101 * one is a pretty crazy thing to do, so we don't support it 100%. If
102 * this invalidation fails, tough, the write still worked...
104 * And this page cache invalidation has to be after ->end_io(), as some
105 * filesystems convert unwritten extents to real allocations in
106 * ->end_io() when necessary, otherwise a racing buffer read would cache
107 * zeros from unwritten extents.
109 if (!dio->error && dio->size &&
110 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
112 err = invalidate_inode_pages2_range(inode->i_mapping,
113 offset >> PAGE_SHIFT,
114 (offset + dio->size - 1) >> PAGE_SHIFT);
116 dio_warn_stale_pagecache(iocb->ki_filp);
119 inode_dio_end(file_inode(iocb->ki_filp));
121 * If this is a DSYNC write, make sure we push it to stable storage now
122 * that we've written data.
124 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
125 ret = generic_write_sync(iocb, ret);
131 EXPORT_SYMBOL_GPL(iomap_dio_complete);
133 static void iomap_dio_complete_work(struct work_struct *work)
135 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
136 struct kiocb *iocb = dio->iocb;
138 iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
142 * Set an error in the dio if none is set yet. We have to use cmpxchg
143 * as the submission context and the completion context(s) can race to
146 static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
148 cmpxchg(&dio->error, 0, ret);
151 static void iomap_dio_bio_end_io(struct bio *bio)
153 struct iomap_dio *dio = bio->bi_private;
154 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
157 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
159 if (atomic_dec_and_test(&dio->ref)) {
160 if (dio->wait_for_completion) {
161 struct task_struct *waiter = dio->submit.waiter;
162 WRITE_ONCE(dio->submit.waiter, NULL);
163 blk_wake_io_task(waiter);
164 } else if (dio->flags & IOMAP_DIO_WRITE) {
165 struct inode *inode = file_inode(dio->iocb->ki_filp);
167 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
168 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
170 iomap_dio_complete_work(&dio->aio.work);
175 bio_check_pages_dirty(bio);
177 bio_release_pages(bio, false);
182 static void iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio,
183 loff_t pos, unsigned len)
185 struct page *page = ZERO_PAGE(0);
186 int flags = REQ_SYNC | REQ_IDLE;
189 bio = bio_alloc(GFP_KERNEL, 1);
190 bio_set_dev(bio, iter->iomap.bdev);
191 bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos);
192 bio->bi_private = dio;
193 bio->bi_end_io = iomap_dio_bio_end_io;
196 __bio_add_page(bio, page, len, 0);
197 bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
198 iomap_dio_submit_bio(iter, dio, bio, pos);
202 * Figure out the bio's operation flags from the dio request, the
203 * mapping, and whether or not we want FUA. Note that we can end up
204 * clearing the WRITE_FUA flag in the dio request.
206 static inline unsigned int iomap_dio_bio_opflags(struct iomap_dio *dio,
207 const struct iomap *iomap, bool use_fua)
209 unsigned int opflags = REQ_SYNC | REQ_IDLE;
211 if (!(dio->flags & IOMAP_DIO_WRITE)) {
212 WARN_ON_ONCE(iomap->flags & IOMAP_F_ZONE_APPEND);
216 if (iomap->flags & IOMAP_F_ZONE_APPEND)
217 opflags |= REQ_OP_ZONE_APPEND;
219 opflags |= REQ_OP_WRITE;
224 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
229 static loff_t iomap_dio_bio_iter(const struct iomap_iter *iter,
230 struct iomap_dio *dio)
232 const struct iomap *iomap = &iter->iomap;
233 struct inode *inode = iter->inode;
234 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
235 unsigned int fs_block_size = i_blocksize(inode), pad;
236 unsigned int align = iov_iter_alignment(dio->submit.iter);
237 loff_t length = iomap_length(iter);
238 loff_t pos = iter->pos;
239 unsigned int bio_opf;
241 bool need_zeroout = false;
242 bool use_fua = false;
243 int nr_pages, ret = 0;
247 if ((pos | length | align) & ((1 << blkbits) - 1))
250 if (iomap->type == IOMAP_UNWRITTEN) {
251 dio->flags |= IOMAP_DIO_UNWRITTEN;
255 if (iomap->flags & IOMAP_F_SHARED)
256 dio->flags |= IOMAP_DIO_COW;
258 if (iomap->flags & IOMAP_F_NEW) {
260 } else if (iomap->type == IOMAP_MAPPED) {
262 * Use a FUA write if we need datasync semantics, this is a pure
263 * data IO that doesn't require any metadata updates (including
264 * after IO completion such as unwritten extent conversion) and
265 * the underlying device supports FUA. This allows us to avoid
266 * cache flushes on IO completion.
268 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
269 (dio->flags & IOMAP_DIO_WRITE_FUA) &&
270 blk_queue_fua(bdev_get_queue(iomap->bdev)))
275 * Save the original count and trim the iter to just the extent we
276 * are operating on right now. The iter will be re-expanded once
279 orig_count = iov_iter_count(dio->submit.iter);
280 iov_iter_truncate(dio->submit.iter, length);
282 if (!iov_iter_count(dio->submit.iter))
286 /* zero out from the start of the block to the write offset */
287 pad = pos & (fs_block_size - 1);
289 iomap_dio_zero(iter, dio, pos - pad, pad);
293 * Set the operation flags early so that bio_iov_iter_get_pages
294 * can set up the page vector appropriately for a ZONE_APPEND
297 bio_opf = iomap_dio_bio_opflags(dio, iomap, use_fua);
299 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS);
303 iov_iter_revert(dio->submit.iter, copied);
308 bio = bio_alloc(GFP_KERNEL, nr_pages);
309 bio_set_dev(bio, iomap->bdev);
310 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
311 bio->bi_write_hint = dio->iocb->ki_hint;
312 bio->bi_ioprio = dio->iocb->ki_ioprio;
313 bio->bi_private = dio;
314 bio->bi_end_io = iomap_dio_bio_end_io;
315 bio->bi_opf = bio_opf;
317 ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
320 * We have to stop part way through an IO. We must fall
321 * through to the sub-block tail zeroing here, otherwise
322 * this short IO may expose stale data in the tail of
323 * the block we haven't written data to.
329 n = bio->bi_iter.bi_size;
330 if (dio->flags & IOMAP_DIO_WRITE) {
331 task_io_account_write(n);
333 if (dio->flags & IOMAP_DIO_DIRTY)
334 bio_set_pages_dirty(bio);
340 nr_pages = bio_iov_vecs_to_alloc(dio->submit.iter,
342 iomap_dio_submit_bio(iter, dio, bio, pos);
347 * We need to zeroout the tail of a sub-block write if the extent type
348 * requires zeroing or the write extends beyond EOF. If we don't zero
349 * the block tail in the latter case, we can expose stale data via mmap
350 * reads of the EOF block.
354 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
355 /* zero out from the end of the write to the end of the block */
356 pad = pos & (fs_block_size - 1);
358 iomap_dio_zero(iter, dio, pos, fs_block_size - pad);
361 /* Undo iter limitation to current extent */
362 iov_iter_reexpand(dio->submit.iter, orig_count - copied);
368 static loff_t iomap_dio_hole_iter(const struct iomap_iter *iter,
369 struct iomap_dio *dio)
371 loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter);
377 static loff_t iomap_dio_inline_iter(const struct iomap_iter *iomi,
378 struct iomap_dio *dio)
380 const struct iomap *iomap = &iomi->iomap;
381 struct iov_iter *iter = dio->submit.iter;
382 void *inline_data = iomap_inline_data(iomap, iomi->pos);
383 loff_t length = iomap_length(iomi);
384 loff_t pos = iomi->pos;
387 if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap)))
390 if (dio->flags & IOMAP_DIO_WRITE) {
391 loff_t size = iomi->inode->i_size;
394 memset(iomap_inline_data(iomap, size), 0, pos - size);
395 copied = copy_from_iter(inline_data, length, iter);
397 if (pos + copied > size)
398 i_size_write(iomi->inode, pos + copied);
399 mark_inode_dirty(iomi->inode);
402 copied = copy_to_iter(inline_data, length, iter);
408 static loff_t iomap_dio_iter(const struct iomap_iter *iter,
409 struct iomap_dio *dio)
411 switch (iter->iomap.type) {
413 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
415 return iomap_dio_hole_iter(iter, dio);
416 case IOMAP_UNWRITTEN:
417 if (!(dio->flags & IOMAP_DIO_WRITE))
418 return iomap_dio_hole_iter(iter, dio);
419 return iomap_dio_bio_iter(iter, dio);
421 return iomap_dio_bio_iter(iter, dio);
423 return iomap_dio_inline_iter(iter, dio);
426 * DIO is not serialised against mmap() access at all, and so
427 * if the page_mkwrite occurs between the writeback and the
428 * iomap_iter() call in the DIO path, then it will see the
429 * DELALLOC block that the page-mkwrite allocated.
431 pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n",
432 dio->iocb->ki_filp, current->comm);
441 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
442 * is being issued as AIO or not. This allows us to optimise pure data writes
443 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
444 * REQ_FLUSH post write. This is slightly tricky because a single request here
445 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
446 * may be pure data writes. In that case, we still need to do a full data sync
449 * Returns -ENOTBLK In case of a page invalidation invalidation failure for
450 * writes. The callers needs to fall back to buffered I/O in this case.
453 __iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
454 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
455 unsigned int dio_flags)
457 struct address_space *mapping = iocb->ki_filp->f_mapping;
458 struct inode *inode = file_inode(iocb->ki_filp);
459 struct iomap_iter iomi = {
462 .len = iov_iter_count(iter),
463 .flags = IOMAP_DIRECT,
465 loff_t end = iomi.pos + iomi.len - 1, ret = 0;
466 bool wait_for_completion =
467 is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT);
468 struct blk_plug plug;
469 struct iomap_dio *dio;
474 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
476 return ERR_PTR(-ENOMEM);
479 atomic_set(&dio->ref, 1);
481 dio->i_size = i_size_read(inode);
486 dio->submit.iter = iter;
487 dio->submit.waiter = current;
488 dio->submit.cookie = BLK_QC_T_NONE;
489 dio->submit.last_queue = NULL;
491 if (iov_iter_rw(iter) == READ) {
492 if (iomi.pos >= dio->i_size)
495 if (iocb->ki_flags & IOCB_NOWAIT) {
496 if (filemap_range_needs_writeback(mapping, iomi.pos,
501 iomi.flags |= IOMAP_NOWAIT;
504 if (iter_is_iovec(iter))
505 dio->flags |= IOMAP_DIO_DIRTY;
507 iomi.flags |= IOMAP_WRITE;
508 dio->flags |= IOMAP_DIO_WRITE;
510 if (iocb->ki_flags & IOCB_NOWAIT) {
511 if (filemap_range_has_page(mapping, iomi.pos, end)) {
515 iomi.flags |= IOMAP_NOWAIT;
518 /* for data sync or sync, we need sync completion processing */
519 if (iocb->ki_flags & IOCB_DSYNC)
520 dio->flags |= IOMAP_DIO_NEED_SYNC;
523 * For datasync only writes, we optimistically try using FUA for
524 * this IO. Any non-FUA write that occurs will clear this flag,
525 * hence we know before completion whether a cache flush is
528 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
529 dio->flags |= IOMAP_DIO_WRITE_FUA;
532 if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) {
534 if (iomi.pos >= dio->i_size ||
535 iomi.pos + iomi.len > dio->i_size)
537 iomi.flags |= IOMAP_OVERWRITE_ONLY;
540 ret = filemap_write_and_wait_range(mapping, iomi.pos, end);
544 if (iov_iter_rw(iter) == WRITE) {
546 * Try to invalidate cache pages for the range we are writing.
547 * If this invalidation fails, let the caller fall back to
550 if (invalidate_inode_pages2_range(mapping,
551 iomi.pos >> PAGE_SHIFT, end >> PAGE_SHIFT)) {
552 trace_iomap_dio_invalidate_fail(inode, iomi.pos,
558 if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
559 ret = sb_init_dio_done_wq(inode->i_sb);
565 inode_dio_begin(inode);
567 blk_start_plug(&plug);
568 while ((ret = iomap_iter(&iomi, ops)) > 0)
569 iomi.processed = iomap_dio_iter(&iomi, dio);
570 blk_finish_plug(&plug);
573 * We only report that we've read data up to i_size.
574 * Revert iter to a state corresponding to that as some callers (such
575 * as the splice code) rely on it.
577 if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size)
578 iov_iter_revert(iter, iomi.pos - dio->i_size);
580 /* magic error code to fall back to buffered I/O */
581 if (ret == -ENOTBLK) {
582 wait_for_completion = true;
586 iomap_dio_set_error(dio, ret);
589 * If all the writes we issued were FUA, we don't need to flush the
590 * cache on IO completion. Clear the sync flag for this case.
592 if (dio->flags & IOMAP_DIO_WRITE_FUA)
593 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
595 WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie);
596 WRITE_ONCE(iocb->private, dio->submit.last_queue);
599 * We are about to drop our additional submission reference, which
600 * might be the last reference to the dio. There are three different
601 * ways we can progress here:
603 * (a) If this is the last reference we will always complete and free
605 * (b) If this is not the last reference, and we serve an asynchronous
606 * iocb, we must never touch the dio after the decrement, the
607 * I/O completion handler will complete and free it.
608 * (c) If this is not the last reference, but we serve a synchronous
609 * iocb, the I/O completion handler will wake us up on the drop
610 * of the final reference, and we will complete and free it here
611 * after we got woken by the I/O completion handler.
613 dio->wait_for_completion = wait_for_completion;
614 if (!atomic_dec_and_test(&dio->ref)) {
615 if (!wait_for_completion)
616 return ERR_PTR(-EIOCBQUEUED);
619 set_current_state(TASK_UNINTERRUPTIBLE);
620 if (!READ_ONCE(dio->submit.waiter))
623 if (!(iocb->ki_flags & IOCB_HIPRI) ||
624 !dio->submit.last_queue ||
625 !blk_poll(dio->submit.last_queue,
626 dio->submit.cookie, true))
629 __set_current_state(TASK_RUNNING);
640 EXPORT_SYMBOL_GPL(__iomap_dio_rw);
643 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
644 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
645 unsigned int dio_flags)
647 struct iomap_dio *dio;
649 dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags);
650 if (IS_ERR_OR_NULL(dio))
651 return PTR_ERR_OR_ZERO(dio);
652 return iomap_dio_complete(dio);
654 EXPORT_SYMBOL_GPL(iomap_dio_rw);