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