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>
29 #include <linux/sched/signal.h>
34 * Execute a iomap write on a segment of the mapping that spans a
35 * contiguous range of pages that have identical block mapping state.
37 * This avoids the need to map pages individually, do individual allocations
38 * for each page and most importantly avoid the need for filesystem specific
39 * locking per page. Instead, all the operations are amortised over the entire
40 * range of pages. It is assumed that the filesystems will lock whatever
41 * resources they require in the iomap_begin call, and release them in the
45 iomap_apply(struct inode
*inode
, loff_t pos
, loff_t length
, unsigned flags
,
46 const struct iomap_ops
*ops
, void *data
, iomap_actor_t actor
)
48 struct iomap iomap
= { 0 };
49 loff_t written
= 0, ret
;
52 * Need to map a range from start position for length bytes. This can
53 * span multiple pages - it is only guaranteed to return a range of a
54 * single type of pages (e.g. all into a hole, all mapped or all
55 * unwritten). Failure at this point has nothing to undo.
57 * If allocation is required for this range, reserve the space now so
58 * that the allocation is guaranteed to succeed later on. Once we copy
59 * the data into the page cache pages, then we cannot fail otherwise we
60 * expose transient stale data. If the reserve fails, we can safely
61 * back out at this point as there is nothing to undo.
63 ret
= ops
->iomap_begin(inode
, pos
, length
, flags
, &iomap
);
66 if (WARN_ON(iomap
.offset
> pos
))
70 * Cut down the length to the one actually provided by the filesystem,
71 * as it might not be able to give us the whole size that we requested.
73 if (iomap
.offset
+ iomap
.length
< pos
+ length
)
74 length
= iomap
.offset
+ iomap
.length
- pos
;
77 * Now that we have guaranteed that the space allocation will succeed.
78 * we can do the copy-in page by page without having to worry about
79 * failures exposing transient data.
81 written
= actor(inode
, pos
, length
, data
, &iomap
);
84 * Now the data has been copied, commit the range we've copied. This
85 * should not fail unless the filesystem has had a fatal error.
88 ret
= ops
->iomap_end(inode
, pos
, length
,
89 written
> 0 ? written
: 0,
93 return written
? written
: ret
;
97 iomap_write_failed(struct inode
*inode
, loff_t pos
, unsigned len
)
99 loff_t i_size
= i_size_read(inode
);
102 * Only truncate newly allocated pages beyoned EOF, even if the
103 * write started inside the existing inode size.
105 if (pos
+ len
> i_size
)
106 truncate_pagecache_range(inode
, max(pos
, i_size
), pos
+ len
);
110 iomap_write_begin(struct inode
*inode
, loff_t pos
, unsigned len
, unsigned flags
,
111 struct page
**pagep
, struct iomap
*iomap
)
113 pgoff_t index
= pos
>> PAGE_SHIFT
;
117 BUG_ON(pos
+ len
> iomap
->offset
+ iomap
->length
);
119 if (fatal_signal_pending(current
))
122 page
= grab_cache_page_write_begin(inode
->i_mapping
, index
, flags
);
126 status
= __block_write_begin_int(page
, pos
, len
, NULL
, iomap
);
127 if (unlikely(status
)) {
132 iomap_write_failed(inode
, pos
, len
);
140 iomap_write_end(struct inode
*inode
, loff_t pos
, unsigned len
,
141 unsigned copied
, struct page
*page
)
145 ret
= generic_write_end(NULL
, inode
->i_mapping
, pos
, len
,
148 iomap_write_failed(inode
, pos
, len
);
153 iomap_write_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
156 struct iov_iter
*i
= data
;
159 unsigned int flags
= AOP_FLAG_NOFS
;
163 unsigned long offset
; /* Offset into pagecache page */
164 unsigned long bytes
; /* Bytes to write to page */
165 size_t copied
; /* Bytes copied from user */
167 offset
= (pos
& (PAGE_SIZE
- 1));
168 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
175 * Bring in the user page that we will copy from _first_.
176 * Otherwise there's a nasty deadlock on copying from the
177 * same page as we're writing to, without it being marked
180 * Not only is this an optimisation, but it is also required
181 * to check that the address is actually valid, when atomic
182 * usercopies are used, below.
184 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
189 status
= iomap_write_begin(inode
, pos
, bytes
, flags
, &page
,
191 if (unlikely(status
))
194 if (mapping_writably_mapped(inode
->i_mapping
))
195 flush_dcache_page(page
);
197 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
199 flush_dcache_page(page
);
201 status
= iomap_write_end(inode
, pos
, bytes
, copied
, page
);
202 if (unlikely(status
< 0))
208 iov_iter_advance(i
, copied
);
209 if (unlikely(copied
== 0)) {
211 * If we were unable to copy any data at all, we must
212 * fall back to a single segment length write.
214 * If we didn't fallback here, we could livelock
215 * because not all segments in the iov can be copied at
216 * once without a pagefault.
218 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
219 iov_iter_single_seg_count(i
));
226 balance_dirty_pages_ratelimited(inode
->i_mapping
);
227 } while (iov_iter_count(i
) && length
);
229 return written
? written
: status
;
233 iomap_file_buffered_write(struct kiocb
*iocb
, struct iov_iter
*iter
,
234 const struct iomap_ops
*ops
)
236 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
237 loff_t pos
= iocb
->ki_pos
, ret
= 0, written
= 0;
239 while (iov_iter_count(iter
)) {
240 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
),
241 IOMAP_WRITE
, ops
, iter
, iomap_write_actor
);
248 return written
? written
: ret
;
250 EXPORT_SYMBOL_GPL(iomap_file_buffered_write
);
253 __iomap_read_page(struct inode
*inode
, loff_t offset
)
255 struct address_space
*mapping
= inode
->i_mapping
;
258 page
= read_mapping_page(mapping
, offset
>> PAGE_SHIFT
, NULL
);
261 if (!PageUptodate(page
)) {
263 return ERR_PTR(-EIO
);
269 iomap_dirty_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
276 struct page
*page
, *rpage
;
277 unsigned long offset
; /* Offset into pagecache page */
278 unsigned long bytes
; /* Bytes to write to page */
280 offset
= (pos
& (PAGE_SIZE
- 1));
281 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
, length
);
283 rpage
= __iomap_read_page(inode
, pos
);
285 return PTR_ERR(rpage
);
287 status
= iomap_write_begin(inode
, pos
, bytes
,
288 AOP_FLAG_NOFS
, &page
, iomap
);
290 if (unlikely(status
))
293 WARN_ON_ONCE(!PageUptodate(page
));
295 status
= iomap_write_end(inode
, pos
, bytes
, bytes
, page
);
296 if (unlikely(status
<= 0)) {
297 if (WARN_ON_ONCE(status
== 0))
308 balance_dirty_pages_ratelimited(inode
->i_mapping
);
315 iomap_file_dirty(struct inode
*inode
, loff_t pos
, loff_t len
,
316 const struct iomap_ops
*ops
)
321 ret
= iomap_apply(inode
, pos
, len
, IOMAP_WRITE
, ops
, NULL
,
331 EXPORT_SYMBOL_GPL(iomap_file_dirty
);
333 static int iomap_zero(struct inode
*inode
, loff_t pos
, unsigned offset
,
334 unsigned bytes
, struct iomap
*iomap
)
339 status
= iomap_write_begin(inode
, pos
, bytes
, AOP_FLAG_NOFS
, &page
,
344 zero_user(page
, offset
, bytes
);
345 mark_page_accessed(page
);
347 return iomap_write_end(inode
, pos
, bytes
, bytes
, page
);
350 static int iomap_dax_zero(loff_t pos
, unsigned offset
, unsigned bytes
,
353 sector_t sector
= iomap
->blkno
+
354 (((pos
& ~(PAGE_SIZE
- 1)) - iomap
->offset
) >> 9);
356 return __dax_zero_page_range(iomap
->bdev
, iomap
->dax_dev
, sector
,
361 iomap_zero_range_actor(struct inode
*inode
, loff_t pos
, loff_t count
,
362 void *data
, struct iomap
*iomap
)
364 bool *did_zero
= data
;
368 /* already zeroed? we're done. */
369 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
373 unsigned offset
, bytes
;
375 offset
= pos
& (PAGE_SIZE
- 1); /* Within page */
376 bytes
= min_t(unsigned, PAGE_SIZE
- offset
, count
);
379 status
= iomap_dax_zero(pos
, offset
, bytes
, iomap
);
381 status
= iomap_zero(inode
, pos
, offset
, bytes
, iomap
);
396 iomap_zero_range(struct inode
*inode
, loff_t pos
, loff_t len
, bool *did_zero
,
397 const struct iomap_ops
*ops
)
402 ret
= iomap_apply(inode
, pos
, len
, IOMAP_ZERO
,
403 ops
, did_zero
, iomap_zero_range_actor
);
413 EXPORT_SYMBOL_GPL(iomap_zero_range
);
416 iomap_truncate_page(struct inode
*inode
, loff_t pos
, bool *did_zero
,
417 const struct iomap_ops
*ops
)
419 unsigned int blocksize
= i_blocksize(inode
);
420 unsigned int off
= pos
& (blocksize
- 1);
422 /* Block boundary? Nothing to do */
425 return iomap_zero_range(inode
, pos
, blocksize
- off
, did_zero
, ops
);
427 EXPORT_SYMBOL_GPL(iomap_truncate_page
);
430 iomap_page_mkwrite_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
431 void *data
, struct iomap
*iomap
)
433 struct page
*page
= data
;
436 ret
= __block_write_begin_int(page
, pos
, length
, NULL
, iomap
);
440 block_commit_write(page
, 0, length
);
444 int iomap_page_mkwrite(struct vm_fault
*vmf
, const struct iomap_ops
*ops
)
446 struct page
*page
= vmf
->page
;
447 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
448 unsigned long length
;
453 size
= i_size_read(inode
);
454 if ((page
->mapping
!= inode
->i_mapping
) ||
455 (page_offset(page
) > size
)) {
456 /* We overload EFAULT to mean page got truncated */
461 /* page is wholly or partially inside EOF */
462 if (((page
->index
+ 1) << PAGE_SHIFT
) > size
)
463 length
= size
& ~PAGE_MASK
;
467 offset
= page_offset(page
);
469 ret
= iomap_apply(inode
, offset
, length
,
470 IOMAP_WRITE
| IOMAP_FAULT
, ops
, page
,
471 iomap_page_mkwrite_actor
);
472 if (unlikely(ret
<= 0))
478 set_page_dirty(page
);
479 wait_for_stable_page(page
);
485 EXPORT_SYMBOL_GPL(iomap_page_mkwrite
);
488 struct fiemap_extent_info
*fi
;
492 static int iomap_to_fiemap(struct fiemap_extent_info
*fi
,
493 struct iomap
*iomap
, u32 flags
)
495 switch (iomap
->type
) {
500 flags
|= FIEMAP_EXTENT_DELALLOC
| FIEMAP_EXTENT_UNKNOWN
;
502 case IOMAP_UNWRITTEN
:
503 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
509 if (iomap
->flags
& IOMAP_F_MERGED
)
510 flags
|= FIEMAP_EXTENT_MERGED
;
511 if (iomap
->flags
& IOMAP_F_SHARED
)
512 flags
|= FIEMAP_EXTENT_SHARED
;
514 return fiemap_fill_next_extent(fi
, iomap
->offset
,
515 iomap
->blkno
!= IOMAP_NULL_BLOCK
? iomap
->blkno
<< 9: 0,
516 iomap
->length
, flags
);
521 iomap_fiemap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
524 struct fiemap_ctx
*ctx
= data
;
527 if (iomap
->type
== IOMAP_HOLE
)
530 ret
= iomap_to_fiemap(ctx
->fi
, &ctx
->prev
, 0);
533 case 0: /* success */
535 case 1: /* extent array full */
542 int iomap_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fi
,
543 loff_t start
, loff_t len
, const struct iomap_ops
*ops
)
545 struct fiemap_ctx ctx
;
548 memset(&ctx
, 0, sizeof(ctx
));
550 ctx
.prev
.type
= IOMAP_HOLE
;
552 ret
= fiemap_check_flags(fi
, FIEMAP_FLAG_SYNC
);
556 if (fi
->fi_flags
& FIEMAP_FLAG_SYNC
) {
557 ret
= filemap_write_and_wait(inode
->i_mapping
);
563 ret
= iomap_apply(inode
, start
, len
, IOMAP_REPORT
, ops
, &ctx
,
565 /* inode with no (attribute) mapping will give ENOENT */
577 if (ctx
.prev
.type
!= IOMAP_HOLE
) {
578 ret
= iomap_to_fiemap(fi
, &ctx
.prev
, FIEMAP_EXTENT_LAST
);
585 EXPORT_SYMBOL_GPL(iomap_fiemap
);
588 iomap_seek_hole_actor(struct inode
*inode
, loff_t offset
, loff_t length
,
589 void *data
, struct iomap
*iomap
)
591 switch (iomap
->type
) {
592 case IOMAP_UNWRITTEN
:
593 offset
= page_cache_seek_hole_data(inode
, offset
, length
,
599 *(loff_t
*)data
= offset
;
607 iomap_seek_hole(struct inode
*inode
, loff_t offset
, const struct iomap_ops
*ops
)
609 loff_t size
= i_size_read(inode
);
610 loff_t length
= size
- offset
;
613 /* Nothing to be found before or beyond the end of the file. */
614 if (offset
< 0 || offset
>= size
)
618 ret
= iomap_apply(inode
, offset
, length
, IOMAP_REPORT
, ops
,
619 &offset
, iomap_seek_hole_actor
);
631 EXPORT_SYMBOL_GPL(iomap_seek_hole
);
634 iomap_seek_data_actor(struct inode
*inode
, loff_t offset
, loff_t length
,
635 void *data
, struct iomap
*iomap
)
637 switch (iomap
->type
) {
640 case IOMAP_UNWRITTEN
:
641 offset
= page_cache_seek_hole_data(inode
, offset
, length
,
647 *(loff_t
*)data
= offset
;
653 iomap_seek_data(struct inode
*inode
, loff_t offset
, const struct iomap_ops
*ops
)
655 loff_t size
= i_size_read(inode
);
656 loff_t length
= size
- offset
;
659 /* Nothing to be found before or beyond the end of the file. */
660 if (offset
< 0 || offset
>= size
)
664 ret
= iomap_apply(inode
, offset
, length
, IOMAP_REPORT
, ops
,
665 &offset
, iomap_seek_data_actor
);
679 EXPORT_SYMBOL_GPL(iomap_seek_data
);
682 * Private flags for iomap_dio, must not overlap with the public ones in
685 #define IOMAP_DIO_WRITE (1 << 30)
686 #define IOMAP_DIO_DIRTY (1 << 31)
690 iomap_dio_end_io_t
*end_io
;
698 /* used during submission and for synchronous completion: */
700 struct iov_iter
*iter
;
701 struct task_struct
*waiter
;
702 struct request_queue
*last_queue
;
706 /* used for aio completion: */
708 struct work_struct work
;
713 static ssize_t
iomap_dio_complete(struct iomap_dio
*dio
)
715 struct kiocb
*iocb
= dio
->iocb
;
719 ret
= dio
->end_io(iocb
,
720 dio
->error
? dio
->error
: dio
->size
,
728 /* check for short read */
729 if (iocb
->ki_pos
+ ret
> dio
->i_size
&&
730 !(dio
->flags
& IOMAP_DIO_WRITE
))
731 ret
= dio
->i_size
- iocb
->ki_pos
;
735 inode_dio_end(file_inode(iocb
->ki_filp
));
741 static void iomap_dio_complete_work(struct work_struct
*work
)
743 struct iomap_dio
*dio
= container_of(work
, struct iomap_dio
, aio
.work
);
744 struct kiocb
*iocb
= dio
->iocb
;
745 bool is_write
= (dio
->flags
& IOMAP_DIO_WRITE
);
748 ret
= iomap_dio_complete(dio
);
749 if (is_write
&& ret
> 0)
750 ret
= generic_write_sync(iocb
, ret
);
751 iocb
->ki_complete(iocb
, ret
, 0);
755 * Set an error in the dio if none is set yet. We have to use cmpxchg
756 * as the submission context and the completion context(s) can race to
759 static inline void iomap_dio_set_error(struct iomap_dio
*dio
, int ret
)
761 cmpxchg(&dio
->error
, 0, ret
);
764 static void iomap_dio_bio_end_io(struct bio
*bio
)
766 struct iomap_dio
*dio
= bio
->bi_private
;
767 bool should_dirty
= (dio
->flags
& IOMAP_DIO_DIRTY
);
770 iomap_dio_set_error(dio
, blk_status_to_errno(bio
->bi_status
));
772 if (atomic_dec_and_test(&dio
->ref
)) {
773 if (is_sync_kiocb(dio
->iocb
)) {
774 struct task_struct
*waiter
= dio
->submit
.waiter
;
776 WRITE_ONCE(dio
->submit
.waiter
, NULL
);
777 wake_up_process(waiter
);
778 } else if (dio
->flags
& IOMAP_DIO_WRITE
) {
779 struct inode
*inode
= file_inode(dio
->iocb
->ki_filp
);
781 INIT_WORK(&dio
->aio
.work
, iomap_dio_complete_work
);
782 queue_work(inode
->i_sb
->s_dio_done_wq
, &dio
->aio
.work
);
784 iomap_dio_complete_work(&dio
->aio
.work
);
789 bio_check_pages_dirty(bio
);
791 struct bio_vec
*bvec
;
794 bio_for_each_segment_all(bvec
, bio
, i
)
795 put_page(bvec
->bv_page
);
801 iomap_dio_zero(struct iomap_dio
*dio
, struct iomap
*iomap
, loff_t pos
,
804 struct page
*page
= ZERO_PAGE(0);
807 bio
= bio_alloc(GFP_KERNEL
, 1);
808 bio
->bi_bdev
= iomap
->bdev
;
809 bio
->bi_iter
.bi_sector
=
810 iomap
->blkno
+ ((pos
- iomap
->offset
) >> 9);
811 bio
->bi_private
= dio
;
812 bio
->bi_end_io
= iomap_dio_bio_end_io
;
815 if (bio_add_page(bio
, page
, len
, 0) != len
)
817 bio_set_op_attrs(bio
, REQ_OP_WRITE
, REQ_SYNC
| REQ_IDLE
);
819 atomic_inc(&dio
->ref
);
820 return submit_bio(bio
);
824 iomap_dio_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
825 void *data
, struct iomap
*iomap
)
827 struct iomap_dio
*dio
= data
;
828 unsigned int blkbits
= blksize_bits(bdev_logical_block_size(iomap
->bdev
));
829 unsigned int fs_block_size
= i_blocksize(inode
), pad
;
830 unsigned int align
= iov_iter_alignment(dio
->submit
.iter
);
831 struct iov_iter iter
;
833 bool need_zeroout
= false;
836 if ((pos
| length
| align
) & ((1 << blkbits
) - 1))
839 switch (iomap
->type
) {
841 if (WARN_ON_ONCE(dio
->flags
& IOMAP_DIO_WRITE
))
844 case IOMAP_UNWRITTEN
:
845 if (!(dio
->flags
& IOMAP_DIO_WRITE
)) {
846 iov_iter_zero(length
, dio
->submit
.iter
);
850 dio
->flags
|= IOMAP_DIO_UNWRITTEN
;
854 if (iomap
->flags
& IOMAP_F_SHARED
)
855 dio
->flags
|= IOMAP_DIO_COW
;
856 if (iomap
->flags
& IOMAP_F_NEW
)
865 * Operate on a partial iter trimmed to the extent we were called for.
866 * We'll update the iter in the dio once we're done with this extent.
868 iter
= *dio
->submit
.iter
;
869 iov_iter_truncate(&iter
, length
);
871 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
876 /* zero out from the start of the block to the write offset */
877 pad
= pos
& (fs_block_size
- 1);
879 iomap_dio_zero(dio
, iomap
, pos
- pad
, pad
);
886 bio
= bio_alloc(GFP_KERNEL
, nr_pages
);
887 bio
->bi_bdev
= iomap
->bdev
;
888 bio
->bi_iter
.bi_sector
=
889 iomap
->blkno
+ ((pos
- iomap
->offset
) >> 9);
890 bio
->bi_write_hint
= dio
->iocb
->ki_hint
;
891 bio
->bi_private
= dio
;
892 bio
->bi_end_io
= iomap_dio_bio_end_io
;
894 ret
= bio_iov_iter_get_pages(bio
, &iter
);
900 if (dio
->flags
& IOMAP_DIO_WRITE
) {
901 bio_set_op_attrs(bio
, REQ_OP_WRITE
, REQ_SYNC
| REQ_IDLE
);
902 task_io_account_write(bio
->bi_iter
.bi_size
);
904 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
905 if (dio
->flags
& IOMAP_DIO_DIRTY
)
906 bio_set_pages_dirty(bio
);
909 dio
->size
+= bio
->bi_iter
.bi_size
;
910 pos
+= bio
->bi_iter
.bi_size
;
912 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
914 atomic_inc(&dio
->ref
);
916 dio
->submit
.last_queue
= bdev_get_queue(iomap
->bdev
);
917 dio
->submit
.cookie
= submit_bio(bio
);
921 /* zero out from the end of the write to the end of the block */
922 pad
= pos
& (fs_block_size
- 1);
924 iomap_dio_zero(dio
, iomap
, pos
, fs_block_size
- pad
);
927 iov_iter_advance(dio
->submit
.iter
, length
);
932 iomap_dio_rw(struct kiocb
*iocb
, struct iov_iter
*iter
,
933 const struct iomap_ops
*ops
, iomap_dio_end_io_t end_io
)
935 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
936 struct inode
*inode
= file_inode(iocb
->ki_filp
);
937 size_t count
= iov_iter_count(iter
);
938 loff_t pos
= iocb
->ki_pos
, start
= pos
;
939 loff_t end
= iocb
->ki_pos
+ count
- 1, ret
= 0;
940 unsigned int flags
= IOMAP_DIRECT
;
941 struct blk_plug plug
;
942 struct iomap_dio
*dio
;
944 lockdep_assert_held(&inode
->i_rwsem
);
949 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
954 atomic_set(&dio
->ref
, 1);
956 dio
->i_size
= i_size_read(inode
);
957 dio
->end_io
= end_io
;
961 dio
->submit
.iter
= iter
;
962 if (is_sync_kiocb(iocb
)) {
963 dio
->submit
.waiter
= current
;
964 dio
->submit
.cookie
= BLK_QC_T_NONE
;
965 dio
->submit
.last_queue
= NULL
;
968 if (iov_iter_rw(iter
) == READ
) {
969 if (pos
>= dio
->i_size
)
972 if (iter
->type
== ITER_IOVEC
)
973 dio
->flags
|= IOMAP_DIO_DIRTY
;
975 dio
->flags
|= IOMAP_DIO_WRITE
;
976 flags
|= IOMAP_WRITE
;
979 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
980 if (filemap_range_has_page(mapping
, start
, end
)) {
984 flags
|= IOMAP_NOWAIT
;
987 ret
= filemap_write_and_wait_range(mapping
, start
, end
);
991 ret
= invalidate_inode_pages2_range(mapping
,
992 start
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
);
996 inode_dio_begin(inode
);
998 blk_start_plug(&plug
);
1000 ret
= iomap_apply(inode
, pos
, count
, flags
, ops
, dio
,
1003 /* magic error code to fall back to buffered I/O */
1004 if (ret
== -ENOTBLK
)
1010 if (iov_iter_rw(iter
) == READ
&& pos
>= dio
->i_size
)
1012 } while ((count
= iov_iter_count(iter
)) > 0);
1013 blk_finish_plug(&plug
);
1016 iomap_dio_set_error(dio
, ret
);
1018 if (ret
>= 0 && iov_iter_rw(iter
) == WRITE
&& !is_sync_kiocb(iocb
) &&
1019 !inode
->i_sb
->s_dio_done_wq
) {
1020 ret
= sb_init_dio_done_wq(inode
->i_sb
);
1022 iomap_dio_set_error(dio
, ret
);
1025 if (!atomic_dec_and_test(&dio
->ref
)) {
1026 if (!is_sync_kiocb(iocb
))
1027 return -EIOCBQUEUED
;
1030 set_current_state(TASK_UNINTERRUPTIBLE
);
1031 if (!READ_ONCE(dio
->submit
.waiter
))
1034 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
1035 !dio
->submit
.last_queue
||
1036 !blk_mq_poll(dio
->submit
.last_queue
,
1037 dio
->submit
.cookie
))
1040 __set_current_state(TASK_RUNNING
);
1043 ret
= iomap_dio_complete(dio
);
1046 * Try again to invalidate clean pages which might have been cached by
1047 * non-direct readahead, or faulted in by get_user_pages() if the source
1048 * of the write was an mmap'ed region of the file we're writing. Either
1049 * one is a pretty crazy thing to do, so we don't support it 100%. If
1050 * this invalidation fails, tough, the write still worked...
1052 if (iov_iter_rw(iter
) == WRITE
) {
1053 int err
= invalidate_inode_pages2_range(mapping
,
1054 start
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
);
1064 EXPORT_SYMBOL_GPL(iomap_dio_rw
);