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