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