1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (c) 2016-2018 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>
14 #include "../internal.h"
17 * Private flags for iomap_dio, must not overlap with the public ones in
20 #define IOMAP_DIO_WRITE_FUA (1 << 28)
21 #define IOMAP_DIO_NEED_SYNC (1 << 29)
22 #define IOMAP_DIO_WRITE (1 << 30)
23 #define IOMAP_DIO_DIRTY (1 << 31)
27 const struct iomap_dio_ops *dops;
33 bool wait_for_completion;
36 /* used during submission and for synchronous completion: */
38 struct iov_iter *iter;
39 struct task_struct *waiter;
40 struct request_queue *last_queue;
44 /* used for aio completion: */
46 struct work_struct work;
51 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin)
53 struct request_queue *q = READ_ONCE(kiocb->private);
57 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin);
59 EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
61 static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,
62 struct bio *bio, loff_t pos)
64 atomic_inc(&dio->ref);
66 if (dio->iocb->ki_flags & IOCB_HIPRI)
67 bio_set_polled(bio, dio->iocb);
69 dio->submit.last_queue = bdev_get_queue(iomap->bdev);
70 if (dio->dops && dio->dops->submit_io)
71 dio->submit.cookie = dio->dops->submit_io(
72 file_inode(dio->iocb->ki_filp),
75 dio->submit.cookie = submit_bio(bio);
78 static ssize_t iomap_dio_complete(struct iomap_dio *dio)
80 const struct iomap_dio_ops *dops = dio->dops;
81 struct kiocb *iocb = dio->iocb;
82 struct inode *inode = file_inode(iocb->ki_filp);
83 loff_t offset = iocb->ki_pos;
84 ssize_t ret = dio->error;
86 if (dops && dops->end_io)
87 ret = dops->end_io(iocb, dio->size, ret, dio->flags);
91 /* check for short read */
92 if (offset + ret > dio->i_size &&
93 !(dio->flags & IOMAP_DIO_WRITE))
94 ret = dio->i_size - offset;
99 * Try again to invalidate clean pages which might have been cached by
100 * non-direct readahead, or faulted in by get_user_pages() if the source
101 * of the write was an mmap'ed region of the file we're writing. Either
102 * one is a pretty crazy thing to do, so we don't support it 100%. If
103 * this invalidation fails, tough, the write still worked...
105 * And this page cache invalidation has to be after ->end_io(), as some
106 * filesystems convert unwritten extents to real allocations in
107 * ->end_io() when necessary, otherwise a racing buffer read would cache
108 * zeros from unwritten extents.
111 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
113 err = invalidate_inode_pages2_range(inode->i_mapping,
114 offset >> PAGE_SHIFT,
115 (offset + dio->size - 1) >> PAGE_SHIFT);
117 dio_warn_stale_pagecache(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);
127 inode_dio_end(file_inode(iocb->ki_filp));
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);
183 iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
186 struct page *page = ZERO_PAGE(0);
187 int flags = REQ_SYNC | REQ_IDLE;
190 bio = bio_alloc(GFP_KERNEL, 1);
191 bio_set_dev(bio, iomap->bdev);
192 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
193 bio->bi_private = dio;
194 bio->bi_end_io = iomap_dio_bio_end_io;
197 __bio_add_page(bio, page, len, 0);
198 bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
199 iomap_dio_submit_bio(dio, iomap, bio, pos);
203 iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
204 struct iomap_dio *dio, struct iomap *iomap)
206 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
207 unsigned int fs_block_size = i_blocksize(inode), pad;
208 unsigned int align = iov_iter_alignment(dio->submit.iter);
210 bool need_zeroout = false;
211 bool use_fua = false;
212 int nr_pages, ret = 0;
216 if ((pos | length | align) & ((1 << blkbits) - 1))
219 if (iomap->type == IOMAP_UNWRITTEN) {
220 dio->flags |= IOMAP_DIO_UNWRITTEN;
224 if (iomap->flags & IOMAP_F_SHARED)
225 dio->flags |= IOMAP_DIO_COW;
227 if (iomap->flags & IOMAP_F_NEW) {
229 } else if (iomap->type == IOMAP_MAPPED) {
231 * Use a FUA write if we need datasync semantics, this is a pure
232 * data IO that doesn't require any metadata updates (including
233 * after IO completion such as unwritten extent conversion) and
234 * the underlying device supports FUA. This allows us to avoid
235 * cache flushes on IO completion.
237 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
238 (dio->flags & IOMAP_DIO_WRITE_FUA) &&
239 blk_queue_fua(bdev_get_queue(iomap->bdev)))
244 * Save the original count and trim the iter to just the extent we
245 * are operating on right now. The iter will be re-expanded once
248 orig_count = iov_iter_count(dio->submit.iter);
249 iov_iter_truncate(dio->submit.iter, length);
251 nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
258 /* zero out from the start of the block to the write offset */
259 pad = pos & (fs_block_size - 1);
261 iomap_dio_zero(dio, iomap, pos - pad, pad);
267 iov_iter_revert(dio->submit.iter, copied);
272 bio = bio_alloc(GFP_KERNEL, nr_pages);
273 bio_set_dev(bio, iomap->bdev);
274 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
275 bio->bi_write_hint = dio->iocb->ki_hint;
276 bio->bi_ioprio = dio->iocb->ki_ioprio;
277 bio->bi_private = dio;
278 bio->bi_end_io = iomap_dio_bio_end_io;
280 ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
283 * We have to stop part way through an IO. We must fall
284 * through to the sub-block tail zeroing here, otherwise
285 * this short IO may expose stale data in the tail of
286 * the block we haven't written data to.
292 n = bio->bi_iter.bi_size;
293 if (dio->flags & IOMAP_DIO_WRITE) {
294 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
296 bio->bi_opf |= REQ_FUA;
298 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
299 task_io_account_write(n);
301 bio->bi_opf = REQ_OP_READ;
302 if (dio->flags & IOMAP_DIO_DIRTY)
303 bio_set_pages_dirty(bio);
309 nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
310 iomap_dio_submit_bio(dio, iomap, bio, pos);
315 * We need to zeroout the tail of a sub-block write if the extent type
316 * requires zeroing or the write extends beyond EOF. If we don't zero
317 * the block tail in the latter case, we can expose stale data via mmap
318 * reads of the EOF block.
322 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
323 /* zero out from the end of the write to the end of the block */
324 pad = pos & (fs_block_size - 1);
326 iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
329 /* Undo iter limitation to current extent */
330 iov_iter_reexpand(dio->submit.iter, orig_count - copied);
337 iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
339 length = iov_iter_zero(length, dio->submit.iter);
345 iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
346 struct iomap_dio *dio, struct iomap *iomap)
348 struct iov_iter *iter = dio->submit.iter;
351 BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
353 if (dio->flags & IOMAP_DIO_WRITE) {
354 loff_t size = inode->i_size;
357 memset(iomap->inline_data + size, 0, pos - size);
358 copied = copy_from_iter(iomap->inline_data + pos, length, iter);
360 if (pos + copied > size)
361 i_size_write(inode, pos + copied);
362 mark_inode_dirty(inode);
365 copied = copy_to_iter(iomap->inline_data + pos, length, iter);
372 iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
373 void *data, struct iomap *iomap, struct iomap *srcmap)
375 struct iomap_dio *dio = data;
377 switch (iomap->type) {
379 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
381 return iomap_dio_hole_actor(length, dio);
382 case IOMAP_UNWRITTEN:
383 if (!(dio->flags & IOMAP_DIO_WRITE))
384 return iomap_dio_hole_actor(length, dio);
385 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
387 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
389 return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
397 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
398 * is being issued as AIO or not. This allows us to optimise pure data writes
399 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
400 * REQ_FLUSH post write. This is slightly tricky because a single request here
401 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
402 * may be pure data writes. In that case, we still need to do a full data sync
406 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
407 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
408 bool wait_for_completion)
410 struct address_space *mapping = iocb->ki_filp->f_mapping;
411 struct inode *inode = file_inode(iocb->ki_filp);
412 size_t count = iov_iter_count(iter);
413 loff_t pos = iocb->ki_pos;
414 loff_t end = iocb->ki_pos + count - 1, ret = 0;
415 unsigned int flags = IOMAP_DIRECT;
416 struct blk_plug plug;
417 struct iomap_dio *dio;
422 if (WARN_ON(is_sync_kiocb(iocb) && !wait_for_completion))
425 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
430 atomic_set(&dio->ref, 1);
432 dio->i_size = i_size_read(inode);
437 dio->submit.iter = iter;
438 dio->submit.waiter = current;
439 dio->submit.cookie = BLK_QC_T_NONE;
440 dio->submit.last_queue = NULL;
442 if (iov_iter_rw(iter) == READ) {
443 if (pos >= dio->i_size)
446 if (iter_is_iovec(iter))
447 dio->flags |= IOMAP_DIO_DIRTY;
449 flags |= IOMAP_WRITE;
450 dio->flags |= IOMAP_DIO_WRITE;
452 /* for data sync or sync, we need sync completion processing */
453 if (iocb->ki_flags & IOCB_DSYNC)
454 dio->flags |= IOMAP_DIO_NEED_SYNC;
457 * For datasync only writes, we optimistically try using FUA for
458 * this IO. Any non-FUA write that occurs will clear this flag,
459 * hence we know before completion whether a cache flush is
462 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
463 dio->flags |= IOMAP_DIO_WRITE_FUA;
466 if (iocb->ki_flags & IOCB_NOWAIT) {
467 if (filemap_range_has_page(mapping, pos, end)) {
471 flags |= IOMAP_NOWAIT;
474 ret = filemap_write_and_wait_range(mapping, pos, end);
479 * Try to invalidate cache pages for the range we're direct
480 * writing. If this invalidation fails, tough, the write will
481 * still work, but racing two incompatible write paths is a
482 * pretty crazy thing to do, so we don't support it 100%.
484 ret = invalidate_inode_pages2_range(mapping,
485 pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
487 dio_warn_stale_pagecache(iocb->ki_filp);
490 if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&
491 !inode->i_sb->s_dio_done_wq) {
492 ret = sb_init_dio_done_wq(inode->i_sb);
497 inode_dio_begin(inode);
499 blk_start_plug(&plug);
501 ret = iomap_apply(inode, pos, count, flags, ops, dio,
504 /* magic error code to fall back to buffered I/O */
505 if (ret == -ENOTBLK) {
506 wait_for_completion = true;
513 if (iov_iter_rw(iter) == READ && pos >= dio->i_size) {
515 * We only report that we've read data up to i_size.
516 * Revert iter to a state corresponding to that as
517 * some callers (such as splice code) rely on it.
519 iov_iter_revert(iter, pos - dio->i_size);
522 } while ((count = iov_iter_count(iter)) > 0);
523 blk_finish_plug(&plug);
526 iomap_dio_set_error(dio, ret);
529 * If all the writes we issued were FUA, we don't need to flush the
530 * cache on IO completion. Clear the sync flag for this case.
532 if (dio->flags & IOMAP_DIO_WRITE_FUA)
533 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
535 WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie);
536 WRITE_ONCE(iocb->private, dio->submit.last_queue);
539 * We are about to drop our additional submission reference, which
540 * might be the last reference to the dio. There are three different
541 * ways we can progress here:
543 * (a) If this is the last reference we will always complete and free
545 * (b) If this is not the last reference, and we serve an asynchronous
546 * iocb, we must never touch the dio after the decrement, the
547 * I/O completion handler will complete and free it.
548 * (c) If this is not the last reference, but we serve a synchronous
549 * iocb, the I/O completion handler will wake us up on the drop
550 * of the final reference, and we will complete and free it here
551 * after we got woken by the I/O completion handler.
553 dio->wait_for_completion = wait_for_completion;
554 if (!atomic_dec_and_test(&dio->ref)) {
555 if (!wait_for_completion)
559 set_current_state(TASK_UNINTERRUPTIBLE);
560 if (!READ_ONCE(dio->submit.waiter))
563 if (!(iocb->ki_flags & IOCB_HIPRI) ||
564 !dio->submit.last_queue ||
565 !blk_poll(dio->submit.last_queue,
566 dio->submit.cookie, true))
569 __set_current_state(TASK_RUNNING);
572 return iomap_dio_complete(dio);
578 EXPORT_SYMBOL_GPL(iomap_dio_rw);