blob: 190967e87b69e4628803b3661e2b664aabbc462a [file] [log] [blame]
Darrick J. Wongdb074432019-07-15 08:50:59 -07001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
4 * Copyright (c) 2016-2018 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
14#include "../internal.h"
15
16/*
17 * Private flags for iomap_dio, must not overlap with the public ones in
18 * iomap.h:
19 */
20#define IOMAP_DIO_WRITE_FUA (1 << 28)
21#define IOMAP_DIO_NEED_SYNC (1 << 29)
22#define IOMAP_DIO_WRITE (1 << 30)
23#define IOMAP_DIO_DIRTY (1 << 31)
24
25struct iomap_dio {
26 struct kiocb *iocb;
Christoph Hellwig838c4f32019-09-19 15:32:45 -070027 const struct iomap_dio_ops *dops;
Darrick J. Wongdb074432019-07-15 08:50:59 -070028 loff_t i_size;
29 loff_t size;
30 atomic_t ref;
31 unsigned flags;
32 int error;
33 bool wait_for_completion;
34
35 union {
36 /* used during submission and for synchronous completion: */
37 struct {
38 struct iov_iter *iter;
39 struct task_struct *waiter;
40 struct request_queue *last_queue;
41 blk_qc_t cookie;
42 } submit;
43
44 /* used for aio completion: */
45 struct {
46 struct work_struct work;
47 } aio;
48 };
49};
50
51int iomap_dio_iopoll(struct kiocb *kiocb, bool spin)
52{
53 struct request_queue *q = READ_ONCE(kiocb->private);
54
55 if (!q)
56 return 0;
57 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin);
58}
59EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
60
61static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -050062 struct bio *bio, loff_t pos)
Darrick J. Wongdb074432019-07-15 08:50:59 -070063{
64 atomic_inc(&dio->ref);
65
66 if (dio->iocb->ki_flags & IOCB_HIPRI)
67 bio_set_polled(bio, dio->iocb);
68
69 dio->submit.last_queue = bdev_get_queue(iomap->bdev);
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -050070 if (dio->dops && dio->dops->submit_io)
71 dio->submit.cookie = dio->dops->submit_io(
72 file_inode(dio->iocb->ki_filp),
73 iomap, bio, pos);
74 else
75 dio->submit.cookie = submit_bio(bio);
Darrick J. Wongdb074432019-07-15 08:50:59 -070076}
77
78static ssize_t iomap_dio_complete(struct iomap_dio *dio)
79{
Christoph Hellwig838c4f32019-09-19 15:32:45 -070080 const struct iomap_dio_ops *dops = dio->dops;
Darrick J. Wongdb074432019-07-15 08:50:59 -070081 struct kiocb *iocb = dio->iocb;
82 struct inode *inode = file_inode(iocb->ki_filp);
83 loff_t offset = iocb->ki_pos;
Christoph Hellwig838c4f32019-09-19 15:32:45 -070084 ssize_t ret = dio->error;
Darrick J. Wongdb074432019-07-15 08:50:59 -070085
Christoph Hellwig838c4f32019-09-19 15:32:45 -070086 if (dops && dops->end_io)
87 ret = dops->end_io(iocb, dio->size, ret, dio->flags);
Darrick J. Wongdb074432019-07-15 08:50:59 -070088
89 if (likely(!ret)) {
90 ret = dio->size;
91 /* check for short read */
92 if (offset + ret > dio->i_size &&
93 !(dio->flags & IOMAP_DIO_WRITE))
94 ret = dio->i_size - offset;
95 iocb->ki_pos += ret;
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 *
Christoph Hellwig838c4f32019-09-19 15:32:45 -0700105 * 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
Darrick J. Wongdb074432019-07-15 08:50:59 -0700108 * zeros from unwritten extents.
109 */
110 if (!dio->error &&
111 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
112 int err;
113 err = invalidate_inode_pages2_range(inode->i_mapping,
114 offset >> PAGE_SHIFT,
115 (offset + dio->size - 1) >> PAGE_SHIFT);
116 if (err)
117 dio_warn_stale_pagecache(iocb->ki_filp);
118 }
119
120 /*
121 * If this is a DSYNC write, make sure we push it to stable storage now
122 * that we've written data.
123 */
124 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
125 ret = generic_write_sync(iocb, ret);
126
127 inode_dio_end(file_inode(iocb->ki_filp));
128 kfree(dio);
129
130 return ret;
131}
132
133static 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), 0);
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 */
146static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
147{
148 cmpxchg(&dio->error, 0, ret);
149}
150
151static 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
156 if (bio->bi_status)
157 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
158
159 if (atomic_dec_and_test(&dio->ref)) {
160 if (dio->wait_for_completion) {
161 struct task_struct *waiter = dio->submit.waiter;
162 WRITE_ONCE(dio->submit.waiter, NULL);
163 blk_wake_io_task(waiter);
164 } else if (dio->flags & IOMAP_DIO_WRITE) {
165 struct inode *inode = file_inode(dio->iocb->ki_filp);
166
167 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
168 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
169 } else {
170 iomap_dio_complete_work(&dio->aio.work);
171 }
172 }
173
174 if (should_dirty) {
175 bio_check_pages_dirty(bio);
176 } else {
177 bio_release_pages(bio, false);
178 bio_put(bio);
179 }
180}
181
182static void
183iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
184 unsigned len)
185{
186 struct page *page = ZERO_PAGE(0);
187 int flags = REQ_SYNC | REQ_IDLE;
188 struct bio *bio;
189
190 bio = bio_alloc(GFP_KERNEL, 1);
191 bio_set_dev(bio, iomap->bdev);
192 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
193 bio->bi_private = dio;
194 bio->bi_end_io = iomap_dio_bio_end_io;
195
196 get_page(page);
197 __bio_add_page(bio, page, len, 0);
198 bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -0500199 iomap_dio_submit_bio(dio, iomap, bio, pos);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700200}
201
202static loff_t
203iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
204 struct iomap_dio *dio, struct iomap *iomap)
205{
206 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
207 unsigned int fs_block_size = i_blocksize(inode), pad;
208 unsigned int align = iov_iter_alignment(dio->submit.iter);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700209 struct bio *bio;
210 bool need_zeroout = false;
211 bool use_fua = false;
212 int nr_pages, ret = 0;
213 size_t copied = 0;
Jan Karaf550ee92019-11-26 09:28:47 -0800214 size_t orig_count;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700215
216 if ((pos | length | align) & ((1 << blkbits) - 1))
217 return -EINVAL;
218
219 if (iomap->type == IOMAP_UNWRITTEN) {
220 dio->flags |= IOMAP_DIO_UNWRITTEN;
221 need_zeroout = true;
222 }
223
224 if (iomap->flags & IOMAP_F_SHARED)
225 dio->flags |= IOMAP_DIO_COW;
226
227 if (iomap->flags & IOMAP_F_NEW) {
228 need_zeroout = true;
229 } else if (iomap->type == IOMAP_MAPPED) {
230 /*
231 * Use a FUA write if we need datasync semantics, this is a pure
232 * data IO that doesn't require any metadata updates (including
233 * after IO completion such as unwritten extent conversion) and
234 * the underlying device supports FUA. This allows us to avoid
235 * cache flushes on IO completion.
236 */
237 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
238 (dio->flags & IOMAP_DIO_WRITE_FUA) &&
239 blk_queue_fua(bdev_get_queue(iomap->bdev)))
240 use_fua = true;
241 }
242
243 /*
Jan Karaf550ee92019-11-26 09:28:47 -0800244 * Save the original count and trim the iter to just the extent we
245 * are operating on right now. The iter will be re-expanded once
246 * we are done.
Darrick J. Wongdb074432019-07-15 08:50:59 -0700247 */
Jan Karaf550ee92019-11-26 09:28:47 -0800248 orig_count = iov_iter_count(dio->submit.iter);
249 iov_iter_truncate(dio->submit.iter, length);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700250
Jan Karaf550ee92019-11-26 09:28:47 -0800251 nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
252 if (nr_pages <= 0) {
253 ret = nr_pages;
254 goto out;
255 }
Darrick J. Wongdb074432019-07-15 08:50:59 -0700256
257 if (need_zeroout) {
258 /* zero out from the start of the block to the write offset */
259 pad = pos & (fs_block_size - 1);
260 if (pad)
261 iomap_dio_zero(dio, iomap, pos - pad, pad);
262 }
263
264 do {
265 size_t n;
266 if (dio->error) {
267 iov_iter_revert(dio->submit.iter, copied);
Jan Karaf550ee92019-11-26 09:28:47 -0800268 copied = ret = 0;
269 goto out;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700270 }
271
272 bio = bio_alloc(GFP_KERNEL, nr_pages);
273 bio_set_dev(bio, iomap->bdev);
274 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
275 bio->bi_write_hint = dio->iocb->ki_hint;
276 bio->bi_ioprio = dio->iocb->ki_ioprio;
277 bio->bi_private = dio;
278 bio->bi_end_io = iomap_dio_bio_end_io;
279
Jan Karaf550ee92019-11-26 09:28:47 -0800280 ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700281 if (unlikely(ret)) {
282 /*
283 * We have to stop part way through an IO. We must fall
284 * through to the sub-block tail zeroing here, otherwise
285 * this short IO may expose stale data in the tail of
286 * the block we haven't written data to.
287 */
288 bio_put(bio);
289 goto zero_tail;
290 }
291
292 n = bio->bi_iter.bi_size;
293 if (dio->flags & IOMAP_DIO_WRITE) {
294 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
295 if (use_fua)
296 bio->bi_opf |= REQ_FUA;
297 else
298 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
299 task_io_account_write(n);
300 } else {
301 bio->bi_opf = REQ_OP_READ;
302 if (dio->flags & IOMAP_DIO_DIRTY)
303 bio_set_pages_dirty(bio);
304 }
305
Darrick J. Wongdb074432019-07-15 08:50:59 -0700306 dio->size += n;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700307 copied += n;
308
Jan Karaf550ee92019-11-26 09:28:47 -0800309 nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -0500310 iomap_dio_submit_bio(dio, iomap, bio, pos);
311 pos += n;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700312 } while (nr_pages);
313
314 /*
315 * We need to zeroout the tail of a sub-block write if the extent type
316 * requires zeroing or the write extends beyond EOF. If we don't zero
317 * the block tail in the latter case, we can expose stale data via mmap
318 * reads of the EOF block.
319 */
320zero_tail:
321 if (need_zeroout ||
322 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
323 /* zero out from the end of the write to the end of the block */
324 pad = pos & (fs_block_size - 1);
325 if (pad)
326 iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
327 }
Jan Karaf550ee92019-11-26 09:28:47 -0800328out:
329 /* Undo iter limitation to current extent */
330 iov_iter_reexpand(dio->submit.iter, orig_count - copied);
Jan Stanceke9f930a2019-11-11 12:58:24 -0800331 if (copied)
332 return copied;
333 return ret;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700334}
335
336static loff_t
337iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
338{
339 length = iov_iter_zero(length, dio->submit.iter);
340 dio->size += length;
341 return length;
342}
343
344static loff_t
345iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
346 struct iomap_dio *dio, struct iomap *iomap)
347{
348 struct iov_iter *iter = dio->submit.iter;
349 size_t copied;
350
351 BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
352
353 if (dio->flags & IOMAP_DIO_WRITE) {
354 loff_t size = inode->i_size;
355
356 if (pos > size)
357 memset(iomap->inline_data + size, 0, pos - size);
358 copied = copy_from_iter(iomap->inline_data + pos, length, iter);
359 if (copied) {
360 if (pos + copied > size)
361 i_size_write(inode, pos + copied);
362 mark_inode_dirty(inode);
363 }
364 } else {
365 copied = copy_to_iter(iomap->inline_data + pos, length, iter);
366 }
367 dio->size += copied;
368 return copied;
369}
370
371static loff_t
372iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700373 void *data, struct iomap *iomap, struct iomap *srcmap)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700374{
375 struct iomap_dio *dio = data;
376
377 switch (iomap->type) {
378 case IOMAP_HOLE:
379 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
380 return -EIO;
381 return iomap_dio_hole_actor(length, dio);
382 case IOMAP_UNWRITTEN:
383 if (!(dio->flags & IOMAP_DIO_WRITE))
384 return iomap_dio_hole_actor(length, dio);
385 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
386 case IOMAP_MAPPED:
387 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
388 case IOMAP_INLINE:
389 return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
390 default:
391 WARN_ON_ONCE(1);
392 return -EIO;
393 }
394}
395
396/*
397 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
398 * is being issued as AIO or not. This allows us to optimise pure data writes
399 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
400 * REQ_FLUSH post write. This is slightly tricky because a single request here
401 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
402 * may be pure data writes. In that case, we still need to do a full data sync
403 * completion.
404 */
405ssize_t
406iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
Jan Kara13ef9542019-10-15 08:43:42 -0700407 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
408 bool wait_for_completion)
Darrick J. Wongdb074432019-07-15 08:50:59 -0700409{
410 struct address_space *mapping = iocb->ki_filp->f_mapping;
411 struct inode *inode = file_inode(iocb->ki_filp);
412 size_t count = iov_iter_count(iter);
Johannes Thumshirn88cfd302019-11-26 09:28:47 -0800413 loff_t pos = iocb->ki_pos;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700414 loff_t end = iocb->ki_pos + count - 1, ret = 0;
415 unsigned int flags = IOMAP_DIRECT;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700416 struct blk_plug plug;
417 struct iomap_dio *dio;
418
Darrick J. Wongdb074432019-07-15 08:50:59 -0700419 if (!count)
420 return 0;
421
Jan Kara13ef9542019-10-15 08:43:42 -0700422 if (WARN_ON(is_sync_kiocb(iocb) && !wait_for_completion))
423 return -EIO;
424
Darrick J. Wongdb074432019-07-15 08:50:59 -0700425 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
426 if (!dio)
427 return -ENOMEM;
428
429 dio->iocb = iocb;
430 atomic_set(&dio->ref, 1);
431 dio->size = 0;
432 dio->i_size = i_size_read(inode);
Christoph Hellwig838c4f32019-09-19 15:32:45 -0700433 dio->dops = dops;
Darrick J. Wongdb074432019-07-15 08:50:59 -0700434 dio->error = 0;
435 dio->flags = 0;
436
437 dio->submit.iter = iter;
438 dio->submit.waiter = current;
439 dio->submit.cookie = BLK_QC_T_NONE;
440 dio->submit.last_queue = NULL;
441
442 if (iov_iter_rw(iter) == READ) {
443 if (pos >= dio->i_size)
444 goto out_free_dio;
445
Joseph Qia9010042019-10-29 09:51:24 -0700446 if (iter_is_iovec(iter))
Darrick J. Wongdb074432019-07-15 08:50:59 -0700447 dio->flags |= IOMAP_DIO_DIRTY;
448 } else {
449 flags |= IOMAP_WRITE;
450 dio->flags |= IOMAP_DIO_WRITE;
451
452 /* for data sync or sync, we need sync completion processing */
453 if (iocb->ki_flags & IOCB_DSYNC)
454 dio->flags |= IOMAP_DIO_NEED_SYNC;
455
456 /*
457 * For datasync only writes, we optimistically try using FUA for
458 * this IO. Any non-FUA write that occurs will clear this flag,
459 * hence we know before completion whether a cache flush is
460 * necessary.
461 */
462 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
463 dio->flags |= IOMAP_DIO_WRITE_FUA;
464 }
465
466 if (iocb->ki_flags & IOCB_NOWAIT) {
Johannes Thumshirn88cfd302019-11-26 09:28:47 -0800467 if (filemap_range_has_page(mapping, pos, end)) {
Darrick J. Wongdb074432019-07-15 08:50:59 -0700468 ret = -EAGAIN;
469 goto out_free_dio;
470 }
471 flags |= IOMAP_NOWAIT;
472 }
473
Johannes Thumshirn88cfd302019-11-26 09:28:47 -0800474 ret = filemap_write_and_wait_range(mapping, pos, end);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700475 if (ret)
476 goto out_free_dio;
477
Dave Chinner54752de2020-07-23 22:45:58 -0700478 if (iov_iter_rw(iter) == WRITE) {
479 /*
480 * Try to invalidate cache pages for the range we are writing.
481 * If this invalidation fails, tough, the write will still work,
482 * but racing two incompatible write paths is a pretty crazy
483 * thing to do, so we don't support it 100%.
484 */
485 if (invalidate_inode_pages2_range(mapping, pos >> PAGE_SHIFT,
486 end >> PAGE_SHIFT))
487 dio_warn_stale_pagecache(iocb->ki_filp);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700488
Dave Chinner54752de2020-07-23 22:45:58 -0700489 if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) {
490 ret = sb_init_dio_done_wq(inode->i_sb);
491 if (ret < 0)
492 goto out_free_dio;
493 }
Darrick J. Wongdb074432019-07-15 08:50:59 -0700494 }
495
496 inode_dio_begin(inode);
497
498 blk_start_plug(&plug);
499 do {
500 ret = iomap_apply(inode, pos, count, flags, ops, dio,
501 iomap_dio_actor);
502 if (ret <= 0) {
503 /* magic error code to fall back to buffered I/O */
504 if (ret == -ENOTBLK) {
505 wait_for_completion = true;
506 ret = 0;
507 }
508 break;
509 }
510 pos += ret;
511
Jan Kara419e9c32019-11-21 16:14:38 -0800512 if (iov_iter_rw(iter) == READ && pos >= dio->i_size) {
513 /*
514 * We only report that we've read data up to i_size.
515 * Revert iter to a state corresponding to that as
516 * some callers (such as splice code) rely on it.
517 */
518 iov_iter_revert(iter, pos - dio->i_size);
Darrick J. Wongdb074432019-07-15 08:50:59 -0700519 break;
Jan Kara419e9c32019-11-21 16:14:38 -0800520 }
Darrick J. Wongdb074432019-07-15 08:50:59 -0700521 } while ((count = iov_iter_count(iter)) > 0);
522 blk_finish_plug(&plug);
523
524 if (ret < 0)
525 iomap_dio_set_error(dio, ret);
526
527 /*
528 * If all the writes we issued were FUA, we don't need to flush the
529 * cache on IO completion. Clear the sync flag for this case.
530 */
531 if (dio->flags & IOMAP_DIO_WRITE_FUA)
532 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
533
534 WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie);
535 WRITE_ONCE(iocb->private, dio->submit.last_queue);
536
537 /*
538 * We are about to drop our additional submission reference, which
yangerkund9973ce22020-03-18 08:04:36 -0700539 * might be the last reference to the dio. There are three different
540 * ways we can progress here:
Darrick J. Wongdb074432019-07-15 08:50:59 -0700541 *
542 * (a) If this is the last reference we will always complete and free
543 * the dio ourselves.
544 * (b) If this is not the last reference, and we serve an asynchronous
545 * iocb, we must never touch the dio after the decrement, the
546 * I/O completion handler will complete and free it.
547 * (c) If this is not the last reference, but we serve a synchronous
548 * iocb, the I/O completion handler will wake us up on the drop
549 * of the final reference, and we will complete and free it here
550 * after we got woken by the I/O completion handler.
551 */
552 dio->wait_for_completion = wait_for_completion;
553 if (!atomic_dec_and_test(&dio->ref)) {
554 if (!wait_for_completion)
555 return -EIOCBQUEUED;
556
557 for (;;) {
558 set_current_state(TASK_UNINTERRUPTIBLE);
559 if (!READ_ONCE(dio->submit.waiter))
560 break;
561
562 if (!(iocb->ki_flags & IOCB_HIPRI) ||
563 !dio->submit.last_queue ||
564 !blk_poll(dio->submit.last_queue,
565 dio->submit.cookie, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800566 blk_io_schedule();
Darrick J. Wongdb074432019-07-15 08:50:59 -0700567 }
568 __set_current_state(TASK_RUNNING);
569 }
570
571 return iomap_dio_complete(dio);
572
573out_free_dio:
574 kfree(dio);
575 return ret;
576}
577EXPORT_SYMBOL_GPL(iomap_dio_rw);