2 * Copyright (C) 2010 Red Hat, Inc.
3 * Copyright (c) 2016 Christoph Hellwig.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 #include <linux/module.h>
15 #include <linux/compiler.h>
17 #include <linux/iomap.h>
18 #include <linux/uaccess.h>
19 #include <linux/gfp.h>
21 #include <linux/swap.h>
22 #include <linux/pagemap.h>
23 #include <linux/file.h>
24 #include <linux/uio.h>
25 #include <linux/backing-dev.h>
26 #include <linux/buffer_head.h>
27 #include <linux/task_io_accounting_ops.h>
28 #include <linux/dax.h>
32 * Execute a iomap write on a segment of the mapping that spans a
33 * contiguous range of pages that have identical block mapping state.
35 * This avoids the need to map pages individually, do individual allocations
36 * for each page and most importantly avoid the need for filesystem specific
37 * locking per page. Instead, all the operations are amortised over the entire
38 * range of pages. It is assumed that the filesystems will lock whatever
39 * resources they require in the iomap_begin call, and release them in the
43 iomap_apply(struct inode
*inode
, loff_t pos
, loff_t length
, unsigned flags
,
44 struct iomap_ops
*ops
, void *data
, iomap_actor_t actor
)
46 struct iomap iomap
= { 0 };
47 loff_t written
= 0, ret
;
50 * Need to map a range from start position for length bytes. This can
51 * span multiple pages - it is only guaranteed to return a range of a
52 * single type of pages (e.g. all into a hole, all mapped or all
53 * unwritten). Failure at this point has nothing to undo.
55 * If allocation is required for this range, reserve the space now so
56 * that the allocation is guaranteed to succeed later on. Once we copy
57 * the data into the page cache pages, then we cannot fail otherwise we
58 * expose transient stale data. If the reserve fails, we can safely
59 * back out at this point as there is nothing to undo.
61 ret
= ops
->iomap_begin(inode
, pos
, length
, flags
, &iomap
);
64 if (WARN_ON(iomap
.offset
> pos
))
68 * Cut down the length to the one actually provided by the filesystem,
69 * as it might not be able to give us the whole size that we requested.
71 if (iomap
.offset
+ iomap
.length
< pos
+ length
)
72 length
= iomap
.offset
+ iomap
.length
- pos
;
75 * Now that we have guaranteed that the space allocation will succeed.
76 * we can do the copy-in page by page without having to worry about
77 * failures exposing transient data.
79 written
= actor(inode
, pos
, length
, data
, &iomap
);
82 * Now the data has been copied, commit the range we've copied. This
83 * should not fail unless the filesystem has had a fatal error.
86 ret
= ops
->iomap_end(inode
, pos
, length
,
87 written
> 0 ? written
: 0,
91 return written
? written
: ret
;
95 iomap_write_failed(struct inode
*inode
, loff_t pos
, unsigned len
)
97 loff_t i_size
= i_size_read(inode
);
100 * Only truncate newly allocated pages beyoned EOF, even if the
101 * write started inside the existing inode size.
103 if (pos
+ len
> i_size
)
104 truncate_pagecache_range(inode
, max(pos
, i_size
), pos
+ len
);
108 iomap_write_begin(struct inode
*inode
, loff_t pos
, unsigned len
, unsigned flags
,
109 struct page
**pagep
, struct iomap
*iomap
)
111 pgoff_t index
= pos
>> PAGE_SHIFT
;
115 BUG_ON(pos
+ len
> iomap
->offset
+ iomap
->length
);
117 page
= grab_cache_page_write_begin(inode
->i_mapping
, index
, flags
);
121 status
= __block_write_begin_int(page
, pos
, len
, NULL
, iomap
);
122 if (unlikely(status
)) {
127 iomap_write_failed(inode
, pos
, len
);
135 iomap_write_end(struct inode
*inode
, loff_t pos
, unsigned len
,
136 unsigned copied
, struct page
*page
)
140 ret
= generic_write_end(NULL
, inode
->i_mapping
, pos
, len
,
143 iomap_write_failed(inode
, pos
, len
);
148 iomap_write_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
151 struct iov_iter
*i
= data
;
154 unsigned int flags
= AOP_FLAG_NOFS
;
157 * Copies from kernel address space cannot fail (NFSD is a big user).
159 if (!iter_is_iovec(i
))
160 flags
|= AOP_FLAG_UNINTERRUPTIBLE
;
164 unsigned long offset
; /* Offset into pagecache page */
165 unsigned long bytes
; /* Bytes to write to page */
166 size_t copied
; /* Bytes copied from user */
168 offset
= (pos
& (PAGE_SIZE
- 1));
169 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
176 * Bring in the user page that we will copy from _first_.
177 * Otherwise there's a nasty deadlock on copying from the
178 * same page as we're writing to, without it being marked
181 * Not only is this an optimisation, but it is also required
182 * to check that the address is actually valid, when atomic
183 * usercopies are used, below.
185 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
190 status
= iomap_write_begin(inode
, pos
, bytes
, flags
, &page
,
192 if (unlikely(status
))
195 if (mapping_writably_mapped(inode
->i_mapping
))
196 flush_dcache_page(page
);
198 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
200 flush_dcache_page(page
);
202 status
= iomap_write_end(inode
, pos
, bytes
, copied
, page
);
203 if (unlikely(status
< 0))
209 iov_iter_advance(i
, copied
);
210 if (unlikely(copied
== 0)) {
212 * If we were unable to copy any data at all, we must
213 * fall back to a single segment length write.
215 * If we didn't fallback here, we could livelock
216 * because not all segments in the iov can be copied at
217 * once without a pagefault.
219 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
220 iov_iter_single_seg_count(i
));
227 balance_dirty_pages_ratelimited(inode
->i_mapping
);
228 } while (iov_iter_count(i
) && length
);
230 return written
? written
: status
;
234 iomap_file_buffered_write(struct kiocb
*iocb
, struct iov_iter
*iter
,
235 struct iomap_ops
*ops
)
237 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
238 loff_t pos
= iocb
->ki_pos
, ret
= 0, written
= 0;
240 while (iov_iter_count(iter
)) {
241 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
),
242 IOMAP_WRITE
, ops
, iter
, iomap_write_actor
);
249 return written
? written
: ret
;
251 EXPORT_SYMBOL_GPL(iomap_file_buffered_write
);
254 __iomap_read_page(struct inode
*inode
, loff_t offset
)
256 struct address_space
*mapping
= inode
->i_mapping
;
259 page
= read_mapping_page(mapping
, offset
>> PAGE_SHIFT
, NULL
);
262 if (!PageUptodate(page
)) {
264 return ERR_PTR(-EIO
);
270 iomap_dirty_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
277 struct page
*page
, *rpage
;
278 unsigned long offset
; /* Offset into pagecache page */
279 unsigned long bytes
; /* Bytes to write to page */
281 offset
= (pos
& (PAGE_SIZE
- 1));
282 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
, length
);
284 rpage
= __iomap_read_page(inode
, pos
);
286 return PTR_ERR(rpage
);
288 status
= iomap_write_begin(inode
, pos
, bytes
,
289 AOP_FLAG_NOFS
| AOP_FLAG_UNINTERRUPTIBLE
,
292 if (unlikely(status
))
295 WARN_ON_ONCE(!PageUptodate(page
));
297 status
= iomap_write_end(inode
, pos
, bytes
, bytes
, page
);
298 if (unlikely(status
<= 0)) {
299 if (WARN_ON_ONCE(status
== 0))
310 balance_dirty_pages_ratelimited(inode
->i_mapping
);
317 iomap_file_dirty(struct inode
*inode
, loff_t pos
, loff_t len
,
318 struct iomap_ops
*ops
)
323 ret
= iomap_apply(inode
, pos
, len
, IOMAP_WRITE
, ops
, NULL
,
333 EXPORT_SYMBOL_GPL(iomap_file_dirty
);
335 static int iomap_zero(struct inode
*inode
, loff_t pos
, unsigned offset
,
336 unsigned bytes
, struct iomap
*iomap
)
341 status
= iomap_write_begin(inode
, pos
, bytes
,
342 AOP_FLAG_UNINTERRUPTIBLE
| AOP_FLAG_NOFS
, &page
, iomap
);
346 zero_user(page
, offset
, bytes
);
347 mark_page_accessed(page
);
349 return iomap_write_end(inode
, pos
, bytes
, bytes
, page
);
352 static int iomap_dax_zero(loff_t pos
, unsigned offset
, unsigned bytes
,
355 sector_t sector
= iomap
->blkno
+
356 (((pos
& ~(PAGE_SIZE
- 1)) - iomap
->offset
) >> 9);
358 return __dax_zero_page_range(iomap
->bdev
, sector
, offset
, bytes
);
362 iomap_zero_range_actor(struct inode
*inode
, loff_t pos
, loff_t count
,
363 void *data
, struct iomap
*iomap
)
365 bool *did_zero
= data
;
369 /* already zeroed? we're done. */
370 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
374 unsigned offset
, bytes
;
376 offset
= pos
& (PAGE_SIZE
- 1); /* Within page */
377 bytes
= min_t(unsigned, PAGE_SIZE
- offset
, count
);
380 status
= iomap_dax_zero(pos
, offset
, bytes
, iomap
);
382 status
= iomap_zero(inode
, pos
, offset
, bytes
, iomap
);
397 iomap_zero_range(struct inode
*inode
, loff_t pos
, loff_t len
, bool *did_zero
,
398 struct iomap_ops
*ops
)
403 ret
= iomap_apply(inode
, pos
, len
, IOMAP_ZERO
,
404 ops
, did_zero
, iomap_zero_range_actor
);
414 EXPORT_SYMBOL_GPL(iomap_zero_range
);
417 iomap_truncate_page(struct inode
*inode
, loff_t pos
, bool *did_zero
,
418 struct iomap_ops
*ops
)
420 unsigned blocksize
= (1 << inode
->i_blkbits
);
421 unsigned off
= pos
& (blocksize
- 1);
423 /* Block boundary? Nothing to do */
426 return iomap_zero_range(inode
, pos
, blocksize
- off
, did_zero
, ops
);
428 EXPORT_SYMBOL_GPL(iomap_truncate_page
);
431 iomap_page_mkwrite_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
432 void *data
, struct iomap
*iomap
)
434 struct page
*page
= data
;
437 ret
= __block_write_begin_int(page
, pos
, length
, NULL
, iomap
);
441 block_commit_write(page
, 0, length
);
445 int iomap_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
,
446 struct iomap_ops
*ops
)
448 struct page
*page
= vmf
->page
;
449 struct inode
*inode
= file_inode(vma
->vm_file
);
450 unsigned long length
;
455 size
= i_size_read(inode
);
456 if ((page
->mapping
!= inode
->i_mapping
) ||
457 (page_offset(page
) > size
)) {
458 /* We overload EFAULT to mean page got truncated */
463 /* page is wholly or partially inside EOF */
464 if (((page
->index
+ 1) << PAGE_SHIFT
) > size
)
465 length
= size
& ~PAGE_MASK
;
469 offset
= page_offset(page
);
471 ret
= iomap_apply(inode
, offset
, length
,
472 IOMAP_WRITE
| IOMAP_FAULT
, ops
, page
,
473 iomap_page_mkwrite_actor
);
474 if (unlikely(ret
<= 0))
480 set_page_dirty(page
);
481 wait_for_stable_page(page
);
487 EXPORT_SYMBOL_GPL(iomap_page_mkwrite
);
490 struct fiemap_extent_info
*fi
;
494 static int iomap_to_fiemap(struct fiemap_extent_info
*fi
,
495 struct iomap
*iomap
, u32 flags
)
497 switch (iomap
->type
) {
502 flags
|= FIEMAP_EXTENT_DELALLOC
| FIEMAP_EXTENT_UNKNOWN
;
504 case IOMAP_UNWRITTEN
:
505 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
511 if (iomap
->flags
& IOMAP_F_MERGED
)
512 flags
|= FIEMAP_EXTENT_MERGED
;
513 if (iomap
->flags
& IOMAP_F_SHARED
)
514 flags
|= FIEMAP_EXTENT_SHARED
;
516 return fiemap_fill_next_extent(fi
, iomap
->offset
,
517 iomap
->blkno
!= IOMAP_NULL_BLOCK
? iomap
->blkno
<< 9: 0,
518 iomap
->length
, flags
);
523 iomap_fiemap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
526 struct fiemap_ctx
*ctx
= data
;
529 if (iomap
->type
== IOMAP_HOLE
)
532 ret
= iomap_to_fiemap(ctx
->fi
, &ctx
->prev
, 0);
535 case 0: /* success */
537 case 1: /* extent array full */
544 int iomap_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fi
,
545 loff_t start
, loff_t len
, struct iomap_ops
*ops
)
547 struct fiemap_ctx ctx
;
550 memset(&ctx
, 0, sizeof(ctx
));
552 ctx
.prev
.type
= IOMAP_HOLE
;
554 ret
= fiemap_check_flags(fi
, FIEMAP_FLAG_SYNC
);
558 if (fi
->fi_flags
& FIEMAP_FLAG_SYNC
) {
559 ret
= filemap_write_and_wait(inode
->i_mapping
);
565 ret
= iomap_apply(inode
, start
, len
, IOMAP_REPORT
, ops
, &ctx
,
567 /* inode with no (attribute) mapping will give ENOENT */
579 if (ctx
.prev
.type
!= IOMAP_HOLE
) {
580 ret
= iomap_to_fiemap(fi
, &ctx
.prev
, FIEMAP_EXTENT_LAST
);
587 EXPORT_SYMBOL_GPL(iomap_fiemap
);
590 * Private flags for iomap_dio, must not overlap with the public ones in
593 #define IOMAP_DIO_WRITE (1 << 30)
594 #define IOMAP_DIO_DIRTY (1 << 31)
598 iomap_dio_end_io_t
*end_io
;
606 /* used during submission and for synchronous completion: */
608 struct iov_iter
*iter
;
609 struct task_struct
*waiter
;
610 struct request_queue
*last_queue
;
614 /* used for aio completion: */
616 struct work_struct work
;
621 static ssize_t
iomap_dio_complete(struct iomap_dio
*dio
)
623 struct kiocb
*iocb
= dio
->iocb
;
627 ret
= dio
->end_io(iocb
,
628 dio
->error
? dio
->error
: dio
->size
,
636 /* check for short read */
637 if (iocb
->ki_pos
+ ret
> dio
->i_size
&&
638 !(dio
->flags
& IOMAP_DIO_WRITE
))
639 ret
= dio
->i_size
- iocb
->ki_pos
;
643 inode_dio_end(file_inode(iocb
->ki_filp
));
649 static void iomap_dio_complete_work(struct work_struct
*work
)
651 struct iomap_dio
*dio
= container_of(work
, struct iomap_dio
, aio
.work
);
652 struct kiocb
*iocb
= dio
->iocb
;
653 bool is_write
= (dio
->flags
& IOMAP_DIO_WRITE
);
656 ret
= iomap_dio_complete(dio
);
657 if (is_write
&& ret
> 0)
658 ret
= generic_write_sync(iocb
, ret
);
659 iocb
->ki_complete(iocb
, ret
, 0);
663 * Set an error in the dio if none is set yet. We have to use cmpxchg
664 * as the submission context and the completion context(s) can race to
667 static inline void iomap_dio_set_error(struct iomap_dio
*dio
, int ret
)
669 cmpxchg(&dio
->error
, 0, ret
);
672 static void iomap_dio_bio_end_io(struct bio
*bio
)
674 struct iomap_dio
*dio
= bio
->bi_private
;
675 bool should_dirty
= (dio
->flags
& IOMAP_DIO_DIRTY
);
678 iomap_dio_set_error(dio
, bio
->bi_error
);
680 if (atomic_dec_and_test(&dio
->ref
)) {
681 if (is_sync_kiocb(dio
->iocb
)) {
682 struct task_struct
*waiter
= dio
->submit
.waiter
;
684 WRITE_ONCE(dio
->submit
.waiter
, NULL
);
685 wake_up_process(waiter
);
686 } else if (dio
->flags
& IOMAP_DIO_WRITE
) {
687 struct inode
*inode
= file_inode(dio
->iocb
->ki_filp
);
689 INIT_WORK(&dio
->aio
.work
, iomap_dio_complete_work
);
690 queue_work(inode
->i_sb
->s_dio_done_wq
, &dio
->aio
.work
);
692 iomap_dio_complete_work(&dio
->aio
.work
);
697 bio_check_pages_dirty(bio
);
699 struct bio_vec
*bvec
;
702 bio_for_each_segment_all(bvec
, bio
, i
)
703 put_page(bvec
->bv_page
);
709 iomap_dio_zero(struct iomap_dio
*dio
, struct iomap
*iomap
, loff_t pos
,
712 struct page
*page
= ZERO_PAGE(0);
715 bio
= bio_alloc(GFP_KERNEL
, 1);
716 bio
->bi_bdev
= iomap
->bdev
;
717 bio
->bi_iter
.bi_sector
=
718 iomap
->blkno
+ ((pos
- iomap
->offset
) >> 9);
719 bio
->bi_private
= dio
;
720 bio
->bi_end_io
= iomap_dio_bio_end_io
;
723 if (bio_add_page(bio
, page
, len
, 0) != len
)
725 bio_set_op_attrs(bio
, REQ_OP_WRITE
, REQ_SYNC
| REQ_IDLE
);
727 atomic_inc(&dio
->ref
);
728 return submit_bio(bio
);
732 iomap_dio_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
733 void *data
, struct iomap
*iomap
)
735 struct iomap_dio
*dio
= data
;
736 unsigned blkbits
= blksize_bits(bdev_logical_block_size(iomap
->bdev
));
737 unsigned fs_block_size
= (1 << inode
->i_blkbits
), pad
;
738 unsigned align
= iov_iter_alignment(dio
->submit
.iter
);
739 struct iov_iter iter
;
741 bool need_zeroout
= false;
744 if ((pos
| length
| align
) & ((1 << blkbits
) - 1))
747 switch (iomap
->type
) {
749 if (WARN_ON_ONCE(dio
->flags
& IOMAP_DIO_WRITE
))
752 case IOMAP_UNWRITTEN
:
753 if (!(dio
->flags
& IOMAP_DIO_WRITE
)) {
754 iov_iter_zero(length
, dio
->submit
.iter
);
758 dio
->flags
|= IOMAP_DIO_UNWRITTEN
;
762 if (iomap
->flags
& IOMAP_F_SHARED
)
763 dio
->flags
|= IOMAP_DIO_COW
;
764 if (iomap
->flags
& IOMAP_F_NEW
)
773 * Operate on a partial iter trimmed to the extent we were called for.
774 * We'll update the iter in the dio once we're done with this extent.
776 iter
= *dio
->submit
.iter
;
777 iov_iter_truncate(&iter
, length
);
779 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
784 /* zero out from the start of the block to the write offset */
785 pad
= pos
& (fs_block_size
- 1);
787 iomap_dio_zero(dio
, iomap
, pos
- pad
, pad
);
794 bio
= bio_alloc(GFP_KERNEL
, nr_pages
);
795 bio
->bi_bdev
= iomap
->bdev
;
796 bio
->bi_iter
.bi_sector
=
797 iomap
->blkno
+ ((pos
- iomap
->offset
) >> 9);
798 bio
->bi_private
= dio
;
799 bio
->bi_end_io
= iomap_dio_bio_end_io
;
801 ret
= bio_iov_iter_get_pages(bio
, &iter
);
807 if (dio
->flags
& IOMAP_DIO_WRITE
) {
808 bio_set_op_attrs(bio
, REQ_OP_WRITE
, REQ_SYNC
| REQ_IDLE
);
809 task_io_account_write(bio
->bi_iter
.bi_size
);
811 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
812 if (dio
->flags
& IOMAP_DIO_DIRTY
)
813 bio_set_pages_dirty(bio
);
816 dio
->size
+= bio
->bi_iter
.bi_size
;
817 pos
+= bio
->bi_iter
.bi_size
;
819 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
821 atomic_inc(&dio
->ref
);
823 dio
->submit
.last_queue
= bdev_get_queue(iomap
->bdev
);
824 dio
->submit
.cookie
= submit_bio(bio
);
828 /* zero out from the end of the write to the end of the block */
829 pad
= pos
& (fs_block_size
- 1);
831 iomap_dio_zero(dio
, iomap
, pos
, fs_block_size
- pad
);
834 iov_iter_advance(dio
->submit
.iter
, length
);
839 iomap_dio_rw(struct kiocb
*iocb
, struct iov_iter
*iter
, struct iomap_ops
*ops
,
840 iomap_dio_end_io_t end_io
)
842 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
843 struct inode
*inode
= file_inode(iocb
->ki_filp
);
844 size_t count
= iov_iter_count(iter
);
845 loff_t pos
= iocb
->ki_pos
, end
= iocb
->ki_pos
+ count
- 1, ret
= 0;
846 unsigned int flags
= IOMAP_DIRECT
;
847 struct blk_plug plug
;
848 struct iomap_dio
*dio
;
850 lockdep_assert_held(&inode
->i_rwsem
);
855 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
860 atomic_set(&dio
->ref
, 1);
862 dio
->i_size
= i_size_read(inode
);
863 dio
->end_io
= end_io
;
867 dio
->submit
.iter
= iter
;
868 if (is_sync_kiocb(iocb
)) {
869 dio
->submit
.waiter
= current
;
870 dio
->submit
.cookie
= BLK_QC_T_NONE
;
871 dio
->submit
.last_queue
= NULL
;
874 if (iov_iter_rw(iter
) == READ
) {
875 if (pos
>= dio
->i_size
)
878 if (iter
->type
== ITER_IOVEC
)
879 dio
->flags
|= IOMAP_DIO_DIRTY
;
881 dio
->flags
|= IOMAP_DIO_WRITE
;
882 flags
|= IOMAP_WRITE
;
885 if (mapping
->nrpages
) {
886 ret
= filemap_write_and_wait_range(mapping
, iocb
->ki_pos
, end
);
890 ret
= invalidate_inode_pages2_range(mapping
,
891 iocb
->ki_pos
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
);
896 inode_dio_begin(inode
);
898 blk_start_plug(&plug
);
900 ret
= iomap_apply(inode
, pos
, count
, flags
, ops
, dio
,
903 /* magic error code to fall back to buffered I/O */
909 } while ((count
= iov_iter_count(iter
)) > 0);
910 blk_finish_plug(&plug
);
913 iomap_dio_set_error(dio
, ret
);
915 if (ret
>= 0 && iov_iter_rw(iter
) == WRITE
&& !is_sync_kiocb(iocb
) &&
916 !inode
->i_sb
->s_dio_done_wq
) {
917 ret
= sb_init_dio_done_wq(inode
->i_sb
);
919 iomap_dio_set_error(dio
, ret
);
922 if (!atomic_dec_and_test(&dio
->ref
)) {
923 if (!is_sync_kiocb(iocb
))
927 set_current_state(TASK_UNINTERRUPTIBLE
);
928 if (!READ_ONCE(dio
->submit
.waiter
))
931 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
932 !dio
->submit
.last_queue
||
933 !blk_mq_poll(dio
->submit
.last_queue
,
937 __set_current_state(TASK_RUNNING
);
941 * Try again to invalidate clean pages which might have been cached by
942 * non-direct readahead, or faulted in by get_user_pages() if the source
943 * of the write was an mmap'ed region of the file we're writing. Either
944 * one is a pretty crazy thing to do, so we don't support it 100%. If
945 * this invalidation fails, tough, the write still worked...
947 if (iov_iter_rw(iter
) == WRITE
&& mapping
->nrpages
) {
948 ret
= invalidate_inode_pages2_range(mapping
,
949 iocb
->ki_pos
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
);
953 return iomap_dio_complete(dio
);
959 EXPORT_SYMBOL_GPL(iomap_dio_rw
);