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