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