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