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