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/pagevec.h>
24 #include <linux/file.h>
25 #include <linux/uio.h>
26 #include <linux/backing-dev.h>
27 #include <linux/buffer_head.h>
28 #include <linux/task_io_accounting_ops.h>
29 #include <linux/dax.h>
30 #include <linux/sched/signal.h>
31 #include <linux/swap.h>
36 * Execute a iomap write on a segment of the mapping that spans a
37 * contiguous range of pages that have identical block mapping state.
39 * This avoids the need to map pages individually, do individual allocations
40 * for each page and most importantly avoid the need for filesystem specific
41 * locking per page. Instead, all the operations are amortised over the entire
42 * range of pages. It is assumed that the filesystems will lock whatever
43 * resources they require in the iomap_begin call, and release them in the
47 iomap_apply(struct inode
*inode
, loff_t pos
, loff_t length
, unsigned flags
,
48 const struct iomap_ops
*ops
, void *data
, iomap_actor_t actor
)
50 struct iomap iomap
= { 0 };
51 loff_t written
= 0, ret
;
54 * Need to map a range from start position for length bytes. This can
55 * span multiple pages - it is only guaranteed to return a range of a
56 * single type of pages (e.g. all into a hole, all mapped or all
57 * unwritten). Failure at this point has nothing to undo.
59 * If allocation is required for this range, reserve the space now so
60 * that the allocation is guaranteed to succeed later on. Once we copy
61 * the data into the page cache pages, then we cannot fail otherwise we
62 * expose transient stale data. If the reserve fails, we can safely
63 * back out at this point as there is nothing to undo.
65 ret
= ops
->iomap_begin(inode
, pos
, length
, flags
, &iomap
);
68 if (WARN_ON(iomap
.offset
> pos
))
70 if (WARN_ON(iomap
.length
== 0))
74 * Cut down the length to the one actually provided by the filesystem,
75 * as it might not be able to give us the whole size that we requested.
77 if (iomap
.offset
+ iomap
.length
< pos
+ length
)
78 length
= iomap
.offset
+ iomap
.length
- pos
;
81 * Now that we have guaranteed that the space allocation will succeed.
82 * we can do the copy-in page by page without having to worry about
83 * failures exposing transient data.
85 written
= actor(inode
, pos
, length
, data
, &iomap
);
88 * Now the data has been copied, commit the range we've copied. This
89 * should not fail unless the filesystem has had a fatal error.
92 ret
= ops
->iomap_end(inode
, pos
, length
,
93 written
> 0 ? written
: 0,
97 return written
? written
: ret
;
101 iomap_sector(struct iomap
*iomap
, loff_t pos
)
103 return (iomap
->addr
+ pos
- iomap
->offset
) >> SECTOR_SHIFT
;
107 iomap_write_failed(struct inode
*inode
, loff_t pos
, unsigned len
)
109 loff_t i_size
= i_size_read(inode
);
112 * Only truncate newly allocated pages beyoned EOF, even if the
113 * write started inside the existing inode size.
115 if (pos
+ len
> i_size
)
116 truncate_pagecache_range(inode
, max(pos
, i_size
), pos
+ len
);
120 iomap_write_begin(struct inode
*inode
, loff_t pos
, unsigned len
, unsigned flags
,
121 struct page
**pagep
, struct iomap
*iomap
)
123 pgoff_t index
= pos
>> PAGE_SHIFT
;
127 BUG_ON(pos
+ len
> iomap
->offset
+ iomap
->length
);
129 if (fatal_signal_pending(current
))
132 page
= grab_cache_page_write_begin(inode
->i_mapping
, index
, flags
);
136 status
= __block_write_begin_int(page
, pos
, len
, NULL
, iomap
);
137 if (unlikely(status
)) {
142 iomap_write_failed(inode
, pos
, len
);
150 iomap_write_end(struct inode
*inode
, loff_t pos
, unsigned len
,
151 unsigned copied
, struct page
*page
)
155 ret
= generic_write_end(NULL
, inode
->i_mapping
, pos
, len
,
158 iomap_write_failed(inode
, pos
, len
);
163 iomap_write_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
166 struct iov_iter
*i
= data
;
169 unsigned int flags
= AOP_FLAG_NOFS
;
173 unsigned long offset
; /* Offset into pagecache page */
174 unsigned long bytes
; /* Bytes to write to page */
175 size_t copied
; /* Bytes copied from user */
177 offset
= (pos
& (PAGE_SIZE
- 1));
178 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
185 * Bring in the user page that we will copy from _first_.
186 * Otherwise there's a nasty deadlock on copying from the
187 * same page as we're writing to, without it being marked
190 * Not only is this an optimisation, but it is also required
191 * to check that the address is actually valid, when atomic
192 * usercopies are used, below.
194 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
199 status
= iomap_write_begin(inode
, pos
, bytes
, flags
, &page
,
201 if (unlikely(status
))
204 if (mapping_writably_mapped(inode
->i_mapping
))
205 flush_dcache_page(page
);
207 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
209 flush_dcache_page(page
);
211 status
= iomap_write_end(inode
, pos
, bytes
, copied
, page
);
212 if (unlikely(status
< 0))
218 iov_iter_advance(i
, copied
);
219 if (unlikely(copied
== 0)) {
221 * If we were unable to copy any data at all, we must
222 * fall back to a single segment length write.
224 * If we didn't fallback here, we could livelock
225 * because not all segments in the iov can be copied at
226 * once without a pagefault.
228 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
229 iov_iter_single_seg_count(i
));
236 balance_dirty_pages_ratelimited(inode
->i_mapping
);
237 } while (iov_iter_count(i
) && length
);
239 return written
? written
: status
;
243 iomap_file_buffered_write(struct kiocb
*iocb
, struct iov_iter
*iter
,
244 const struct iomap_ops
*ops
)
246 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
247 loff_t pos
= iocb
->ki_pos
, ret
= 0, written
= 0;
249 while (iov_iter_count(iter
)) {
250 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
),
251 IOMAP_WRITE
, ops
, iter
, iomap_write_actor
);
258 return written
? written
: ret
;
260 EXPORT_SYMBOL_GPL(iomap_file_buffered_write
);
263 __iomap_read_page(struct inode
*inode
, loff_t offset
)
265 struct address_space
*mapping
= inode
->i_mapping
;
268 page
= read_mapping_page(mapping
, offset
>> PAGE_SHIFT
, NULL
);
271 if (!PageUptodate(page
)) {
273 return ERR_PTR(-EIO
);
279 iomap_dirty_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
286 struct page
*page
, *rpage
;
287 unsigned long offset
; /* Offset into pagecache page */
288 unsigned long bytes
; /* Bytes to write to page */
290 offset
= (pos
& (PAGE_SIZE
- 1));
291 bytes
= min_t(loff_t
, PAGE_SIZE
- offset
, length
);
293 rpage
= __iomap_read_page(inode
, pos
);
295 return PTR_ERR(rpage
);
297 status
= iomap_write_begin(inode
, pos
, bytes
,
298 AOP_FLAG_NOFS
, &page
, iomap
);
300 if (unlikely(status
))
303 WARN_ON_ONCE(!PageUptodate(page
));
305 status
= iomap_write_end(inode
, pos
, bytes
, bytes
, page
);
306 if (unlikely(status
<= 0)) {
307 if (WARN_ON_ONCE(status
== 0))
318 balance_dirty_pages_ratelimited(inode
->i_mapping
);
325 iomap_file_dirty(struct inode
*inode
, loff_t pos
, loff_t len
,
326 const struct iomap_ops
*ops
)
331 ret
= iomap_apply(inode
, pos
, len
, IOMAP_WRITE
, ops
, NULL
,
341 EXPORT_SYMBOL_GPL(iomap_file_dirty
);
343 static int iomap_zero(struct inode
*inode
, loff_t pos
, unsigned offset
,
344 unsigned bytes
, struct iomap
*iomap
)
349 status
= iomap_write_begin(inode
, pos
, bytes
, AOP_FLAG_NOFS
, &page
,
354 zero_user(page
, offset
, bytes
);
355 mark_page_accessed(page
);
357 return iomap_write_end(inode
, pos
, bytes
, bytes
, page
);
360 static int iomap_dax_zero(loff_t pos
, unsigned offset
, unsigned bytes
,
363 return __dax_zero_page_range(iomap
->bdev
, iomap
->dax_dev
,
364 iomap_sector(iomap
, pos
& PAGE_MASK
), offset
, bytes
);
368 iomap_zero_range_actor(struct inode
*inode
, loff_t pos
, loff_t count
,
369 void *data
, struct iomap
*iomap
)
371 bool *did_zero
= data
;
375 /* already zeroed? we're done. */
376 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
380 unsigned offset
, bytes
;
382 offset
= pos
& (PAGE_SIZE
- 1); /* Within page */
383 bytes
= min_t(loff_t
, PAGE_SIZE
- offset
, count
);
386 status
= iomap_dax_zero(pos
, offset
, bytes
, iomap
);
388 status
= iomap_zero(inode
, pos
, offset
, bytes
, iomap
);
403 iomap_zero_range(struct inode
*inode
, loff_t pos
, loff_t len
, bool *did_zero
,
404 const struct iomap_ops
*ops
)
409 ret
= iomap_apply(inode
, pos
, len
, IOMAP_ZERO
,
410 ops
, did_zero
, iomap_zero_range_actor
);
420 EXPORT_SYMBOL_GPL(iomap_zero_range
);
423 iomap_truncate_page(struct inode
*inode
, loff_t pos
, bool *did_zero
,
424 const struct iomap_ops
*ops
)
426 unsigned int blocksize
= i_blocksize(inode
);
427 unsigned int off
= pos
& (blocksize
- 1);
429 /* Block boundary? Nothing to do */
432 return iomap_zero_range(inode
, pos
, blocksize
- off
, did_zero
, ops
);
434 EXPORT_SYMBOL_GPL(iomap_truncate_page
);
437 iomap_page_mkwrite_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
438 void *data
, struct iomap
*iomap
)
440 struct page
*page
= data
;
443 ret
= __block_write_begin_int(page
, pos
, length
, NULL
, iomap
);
447 block_commit_write(page
, 0, length
);
451 int iomap_page_mkwrite(struct vm_fault
*vmf
, const struct iomap_ops
*ops
)
453 struct page
*page
= vmf
->page
;
454 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
455 unsigned long length
;
460 size
= i_size_read(inode
);
461 if ((page
->mapping
!= inode
->i_mapping
) ||
462 (page_offset(page
) > size
)) {
463 /* We overload EFAULT to mean page got truncated */
468 /* page is wholly or partially inside EOF */
469 if (((page
->index
+ 1) << PAGE_SHIFT
) > size
)
470 length
= size
& ~PAGE_MASK
;
474 offset
= page_offset(page
);
476 ret
= iomap_apply(inode
, offset
, length
,
477 IOMAP_WRITE
| IOMAP_FAULT
, ops
, page
,
478 iomap_page_mkwrite_actor
);
479 if (unlikely(ret
<= 0))
485 set_page_dirty(page
);
486 wait_for_stable_page(page
);
487 return VM_FAULT_LOCKED
;
490 return block_page_mkwrite_return(ret
);
492 EXPORT_SYMBOL_GPL(iomap_page_mkwrite
);
495 struct fiemap_extent_info
*fi
;
499 static int iomap_to_fiemap(struct fiemap_extent_info
*fi
,
500 struct iomap
*iomap
, u32 flags
)
502 switch (iomap
->type
) {
507 flags
|= FIEMAP_EXTENT_DELALLOC
| FIEMAP_EXTENT_UNKNOWN
;
511 case IOMAP_UNWRITTEN
:
512 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
515 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
519 if (iomap
->flags
& IOMAP_F_MERGED
)
520 flags
|= FIEMAP_EXTENT_MERGED
;
521 if (iomap
->flags
& IOMAP_F_SHARED
)
522 flags
|= FIEMAP_EXTENT_SHARED
;
524 return fiemap_fill_next_extent(fi
, iomap
->offset
,
525 iomap
->addr
!= IOMAP_NULL_ADDR
? iomap
->addr
: 0,
526 iomap
->length
, flags
);
530 iomap_fiemap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
533 struct fiemap_ctx
*ctx
= data
;
536 if (iomap
->type
== IOMAP_HOLE
)
539 ret
= iomap_to_fiemap(ctx
->fi
, &ctx
->prev
, 0);
542 case 0: /* success */
544 case 1: /* extent array full */
551 int iomap_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fi
,
552 loff_t start
, loff_t len
, const struct iomap_ops
*ops
)
554 struct fiemap_ctx ctx
;
557 memset(&ctx
, 0, sizeof(ctx
));
559 ctx
.prev
.type
= IOMAP_HOLE
;
561 ret
= fiemap_check_flags(fi
, FIEMAP_FLAG_SYNC
);
565 if (fi
->fi_flags
& FIEMAP_FLAG_SYNC
) {
566 ret
= filemap_write_and_wait(inode
->i_mapping
);
572 ret
= iomap_apply(inode
, start
, len
, IOMAP_REPORT
, ops
, &ctx
,
574 /* inode with no (attribute) mapping will give ENOENT */
586 if (ctx
.prev
.type
!= IOMAP_HOLE
) {
587 ret
= iomap_to_fiemap(fi
, &ctx
.prev
, FIEMAP_EXTENT_LAST
);
594 EXPORT_SYMBOL_GPL(iomap_fiemap
);
597 * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
598 * Returns true if found and updates @lastoff to the offset in file.
601 page_seek_hole_data(struct inode
*inode
, struct page
*page
, loff_t
*lastoff
,
604 const struct address_space_operations
*ops
= inode
->i_mapping
->a_ops
;
605 unsigned int bsize
= i_blocksize(inode
), off
;
606 bool seek_data
= whence
== SEEK_DATA
;
607 loff_t poff
= page_offset(page
);
609 if (WARN_ON_ONCE(*lastoff
>= poff
+ PAGE_SIZE
))
612 if (*lastoff
< poff
) {
614 * Last offset smaller than the start of the page means we found
617 if (whence
== SEEK_HOLE
)
623 * Just check the page unless we can and should check block ranges:
625 if (bsize
== PAGE_SIZE
|| !ops
->is_partially_uptodate
)
626 return PageUptodate(page
) == seek_data
;
629 if (unlikely(page
->mapping
!= inode
->i_mapping
))
630 goto out_unlock_not_found
;
632 for (off
= 0; off
< PAGE_SIZE
; off
+= bsize
) {
633 if ((*lastoff
& ~PAGE_MASK
) >= off
+ bsize
)
635 if (ops
->is_partially_uptodate(page
, off
, bsize
) == seek_data
) {
639 *lastoff
= poff
+ off
+ bsize
;
642 out_unlock_not_found
:
648 * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
650 * Within unwritten extents, the page cache determines which parts are holes
651 * and which are data: uptodate buffer heads count as data; everything else
654 * Returns the resulting offset on successs, and -ENOENT otherwise.
657 page_cache_seek_hole_data(struct inode
*inode
, loff_t offset
, loff_t length
,
660 pgoff_t index
= offset
>> PAGE_SHIFT
;
661 pgoff_t end
= DIV_ROUND_UP(offset
+ length
, PAGE_SIZE
);
662 loff_t lastoff
= offset
;
671 unsigned nr_pages
, i
;
673 nr_pages
= pagevec_lookup_range(&pvec
, inode
->i_mapping
, &index
,
678 for (i
= 0; i
< nr_pages
; i
++) {
679 struct page
*page
= pvec
.pages
[i
];
681 if (page_seek_hole_data(inode
, page
, &lastoff
, whence
))
683 lastoff
= page_offset(page
) + PAGE_SIZE
;
685 pagevec_release(&pvec
);
686 } while (index
< end
);
688 /* When no page at lastoff and we are not done, we found a hole. */
689 if (whence
!= SEEK_HOLE
)
693 if (lastoff
< offset
+ length
)
698 pagevec_release(&pvec
);
704 iomap_seek_hole_actor(struct inode
*inode
, loff_t offset
, loff_t length
,
705 void *data
, struct iomap
*iomap
)
707 switch (iomap
->type
) {
708 case IOMAP_UNWRITTEN
:
709 offset
= page_cache_seek_hole_data(inode
, offset
, length
,
715 *(loff_t
*)data
= offset
;
723 iomap_seek_hole(struct inode
*inode
, loff_t offset
, const struct iomap_ops
*ops
)
725 loff_t size
= i_size_read(inode
);
726 loff_t length
= size
- offset
;
729 /* Nothing to be found before or beyond the end of the file. */
730 if (offset
< 0 || offset
>= size
)
734 ret
= iomap_apply(inode
, offset
, length
, IOMAP_REPORT
, ops
,
735 &offset
, iomap_seek_hole_actor
);
747 EXPORT_SYMBOL_GPL(iomap_seek_hole
);
750 iomap_seek_data_actor(struct inode
*inode
, loff_t offset
, loff_t length
,
751 void *data
, struct iomap
*iomap
)
753 switch (iomap
->type
) {
756 case IOMAP_UNWRITTEN
:
757 offset
= page_cache_seek_hole_data(inode
, offset
, length
,
763 *(loff_t
*)data
= offset
;
769 iomap_seek_data(struct inode
*inode
, loff_t offset
, const struct iomap_ops
*ops
)
771 loff_t size
= i_size_read(inode
);
772 loff_t length
= size
- offset
;
775 /* Nothing to be found before or beyond the end of the file. */
776 if (offset
< 0 || offset
>= size
)
780 ret
= iomap_apply(inode
, offset
, length
, IOMAP_REPORT
, ops
,
781 &offset
, iomap_seek_data_actor
);
795 EXPORT_SYMBOL_GPL(iomap_seek_data
);
798 * Private flags for iomap_dio, must not overlap with the public ones in
801 #define IOMAP_DIO_WRITE_FUA (1 << 28)
802 #define IOMAP_DIO_NEED_SYNC (1 << 29)
803 #define IOMAP_DIO_WRITE (1 << 30)
804 #define IOMAP_DIO_DIRTY (1 << 31)
808 iomap_dio_end_io_t
*end_io
;
816 /* used during submission and for synchronous completion: */
818 struct iov_iter
*iter
;
819 struct task_struct
*waiter
;
820 struct request_queue
*last_queue
;
824 /* used for aio completion: */
826 struct work_struct work
;
831 static ssize_t
iomap_dio_complete(struct iomap_dio
*dio
)
833 struct kiocb
*iocb
= dio
->iocb
;
834 struct inode
*inode
= file_inode(iocb
->ki_filp
);
835 loff_t offset
= iocb
->ki_pos
;
839 ret
= dio
->end_io(iocb
,
840 dio
->error
? dio
->error
: dio
->size
,
848 /* check for short read */
849 if (offset
+ ret
> dio
->i_size
&&
850 !(dio
->flags
& IOMAP_DIO_WRITE
))
851 ret
= dio
->i_size
- offset
;
856 * Try again to invalidate clean pages which might have been cached by
857 * non-direct readahead, or faulted in by get_user_pages() if the source
858 * of the write was an mmap'ed region of the file we're writing. Either
859 * one is a pretty crazy thing to do, so we don't support it 100%. If
860 * this invalidation fails, tough, the write still worked...
862 * And this page cache invalidation has to be after dio->end_io(), as
863 * some filesystems convert unwritten extents to real allocations in
864 * end_io() when necessary, otherwise a racing buffer read would cache
865 * zeros from unwritten extents.
868 (dio
->flags
& IOMAP_DIO_WRITE
) && inode
->i_mapping
->nrpages
) {
870 err
= invalidate_inode_pages2_range(inode
->i_mapping
,
871 offset
>> PAGE_SHIFT
,
872 (offset
+ dio
->size
- 1) >> PAGE_SHIFT
);
874 dio_warn_stale_pagecache(iocb
->ki_filp
);
878 * If this is a DSYNC write, make sure we push it to stable storage now
879 * that we've written data.
881 if (ret
> 0 && (dio
->flags
& IOMAP_DIO_NEED_SYNC
))
882 ret
= generic_write_sync(iocb
, ret
);
884 inode_dio_end(file_inode(iocb
->ki_filp
));
890 static void iomap_dio_complete_work(struct work_struct
*work
)
892 struct iomap_dio
*dio
= container_of(work
, struct iomap_dio
, aio
.work
);
893 struct kiocb
*iocb
= dio
->iocb
;
895 iocb
->ki_complete(iocb
, iomap_dio_complete(dio
), 0);
899 * Set an error in the dio if none is set yet. We have to use cmpxchg
900 * as the submission context and the completion context(s) can race to
903 static inline void iomap_dio_set_error(struct iomap_dio
*dio
, int ret
)
905 cmpxchg(&dio
->error
, 0, ret
);
908 static void iomap_dio_bio_end_io(struct bio
*bio
)
910 struct iomap_dio
*dio
= bio
->bi_private
;
911 bool should_dirty
= (dio
->flags
& IOMAP_DIO_DIRTY
);
914 iomap_dio_set_error(dio
, blk_status_to_errno(bio
->bi_status
));
916 if (atomic_dec_and_test(&dio
->ref
)) {
917 if (is_sync_kiocb(dio
->iocb
)) {
918 struct task_struct
*waiter
= dio
->submit
.waiter
;
920 WRITE_ONCE(dio
->submit
.waiter
, NULL
);
921 wake_up_process(waiter
);
922 } else if (dio
->flags
& IOMAP_DIO_WRITE
) {
923 struct inode
*inode
= file_inode(dio
->iocb
->ki_filp
);
925 INIT_WORK(&dio
->aio
.work
, iomap_dio_complete_work
);
926 queue_work(inode
->i_sb
->s_dio_done_wq
, &dio
->aio
.work
);
928 iomap_dio_complete_work(&dio
->aio
.work
);
933 bio_check_pages_dirty(bio
);
935 struct bio_vec
*bvec
;
938 bio_for_each_segment_all(bvec
, bio
, i
)
939 put_page(bvec
->bv_page
);
945 iomap_dio_zero(struct iomap_dio
*dio
, struct iomap
*iomap
, loff_t pos
,
948 struct page
*page
= ZERO_PAGE(0);
951 bio
= bio_alloc(GFP_KERNEL
, 1);
952 bio_set_dev(bio
, iomap
->bdev
);
953 bio
->bi_iter
.bi_sector
= iomap_sector(iomap
, pos
);
954 bio
->bi_private
= dio
;
955 bio
->bi_end_io
= iomap_dio_bio_end_io
;
958 __bio_add_page(bio
, page
, len
, 0);
959 bio_set_op_attrs(bio
, REQ_OP_WRITE
, REQ_SYNC
| REQ_IDLE
);
961 atomic_inc(&dio
->ref
);
962 return submit_bio(bio
);
966 iomap_dio_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
967 void *data
, struct iomap
*iomap
)
969 struct iomap_dio
*dio
= data
;
970 unsigned int blkbits
= blksize_bits(bdev_logical_block_size(iomap
->bdev
));
971 unsigned int fs_block_size
= i_blocksize(inode
), pad
;
972 unsigned int align
= iov_iter_alignment(dio
->submit
.iter
);
973 struct iov_iter iter
;
975 bool need_zeroout
= false;
976 bool use_fua
= false;
980 if ((pos
| length
| align
) & ((1 << blkbits
) - 1))
983 switch (iomap
->type
) {
985 if (WARN_ON_ONCE(dio
->flags
& IOMAP_DIO_WRITE
))
988 case IOMAP_UNWRITTEN
:
989 if (!(dio
->flags
& IOMAP_DIO_WRITE
)) {
990 length
= iov_iter_zero(length
, dio
->submit
.iter
);
994 dio
->flags
|= IOMAP_DIO_UNWRITTEN
;
998 if (iomap
->flags
& IOMAP_F_SHARED
)
999 dio
->flags
|= IOMAP_DIO_COW
;
1000 if (iomap
->flags
& IOMAP_F_NEW
) {
1001 need_zeroout
= true;
1004 * Use a FUA write if we need datasync semantics, this
1005 * is a pure data IO that doesn't require any metadata
1006 * updates and the underlying device supports FUA. This
1007 * allows us to avoid cache flushes on IO completion.
1009 if (!(iomap
->flags
& (IOMAP_F_SHARED
|IOMAP_F_DIRTY
)) &&
1010 (dio
->flags
& IOMAP_DIO_WRITE_FUA
) &&
1011 blk_queue_fua(bdev_get_queue(iomap
->bdev
)))
1021 * Operate on a partial iter trimmed to the extent we were called for.
1022 * We'll update the iter in the dio once we're done with this extent.
1024 iter
= *dio
->submit
.iter
;
1025 iov_iter_truncate(&iter
, length
);
1027 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
1032 /* zero out from the start of the block to the write offset */
1033 pad
= pos
& (fs_block_size
- 1);
1035 iomap_dio_zero(dio
, iomap
, pos
- pad
, pad
);
1041 iov_iter_revert(dio
->submit
.iter
, copied
);
1045 bio
= bio_alloc(GFP_KERNEL
, nr_pages
);
1046 bio_set_dev(bio
, iomap
->bdev
);
1047 bio
->bi_iter
.bi_sector
= iomap_sector(iomap
, pos
);
1048 bio
->bi_write_hint
= dio
->iocb
->ki_hint
;
1049 bio
->bi_ioprio
= dio
->iocb
->ki_ioprio
;
1050 bio
->bi_private
= dio
;
1051 bio
->bi_end_io
= iomap_dio_bio_end_io
;
1053 ret
= bio_iov_iter_get_pages(bio
, &iter
);
1054 if (unlikely(ret
)) {
1056 return copied
? copied
: ret
;
1059 n
= bio
->bi_iter
.bi_size
;
1060 if (dio
->flags
& IOMAP_DIO_WRITE
) {
1061 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_IDLE
;
1063 bio
->bi_opf
|= REQ_FUA
;
1065 dio
->flags
&= ~IOMAP_DIO_WRITE_FUA
;
1066 task_io_account_write(n
);
1068 bio
->bi_opf
= REQ_OP_READ
;
1069 if (dio
->flags
& IOMAP_DIO_DIRTY
)
1070 bio_set_pages_dirty(bio
);
1073 iov_iter_advance(dio
->submit
.iter
, n
);
1079 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
1081 atomic_inc(&dio
->ref
);
1083 dio
->submit
.last_queue
= bdev_get_queue(iomap
->bdev
);
1084 dio
->submit
.cookie
= submit_bio(bio
);
1088 /* zero out from the end of the write to the end of the block */
1089 pad
= pos
& (fs_block_size
- 1);
1091 iomap_dio_zero(dio
, iomap
, pos
, fs_block_size
- pad
);
1097 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
1098 * is being issued as AIO or not. This allows us to optimise pure data writes
1099 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
1100 * REQ_FLUSH post write. This is slightly tricky because a single request here
1101 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
1102 * may be pure data writes. In that case, we still need to do a full data sync
1106 iomap_dio_rw(struct kiocb
*iocb
, struct iov_iter
*iter
,
1107 const struct iomap_ops
*ops
, iomap_dio_end_io_t end_io
)
1109 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1110 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1111 size_t count
= iov_iter_count(iter
);
1112 loff_t pos
= iocb
->ki_pos
, start
= pos
;
1113 loff_t end
= iocb
->ki_pos
+ count
- 1, ret
= 0;
1114 unsigned int flags
= IOMAP_DIRECT
;
1115 struct blk_plug plug
;
1116 struct iomap_dio
*dio
;
1118 lockdep_assert_held(&inode
->i_rwsem
);
1123 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
1128 atomic_set(&dio
->ref
, 1);
1130 dio
->i_size
= i_size_read(inode
);
1131 dio
->end_io
= end_io
;
1135 dio
->submit
.iter
= iter
;
1136 if (is_sync_kiocb(iocb
)) {
1137 dio
->submit
.waiter
= current
;
1138 dio
->submit
.cookie
= BLK_QC_T_NONE
;
1139 dio
->submit
.last_queue
= NULL
;
1142 if (iov_iter_rw(iter
) == READ
) {
1143 if (pos
>= dio
->i_size
)
1146 if (iter
->type
== ITER_IOVEC
)
1147 dio
->flags
|= IOMAP_DIO_DIRTY
;
1149 flags
|= IOMAP_WRITE
;
1150 dio
->flags
|= IOMAP_DIO_WRITE
;
1152 /* for data sync or sync, we need sync completion processing */
1153 if (iocb
->ki_flags
& IOCB_DSYNC
)
1154 dio
->flags
|= IOMAP_DIO_NEED_SYNC
;
1157 * For datasync only writes, we optimistically try using FUA for
1158 * this IO. Any non-FUA write that occurs will clear this flag,
1159 * hence we know before completion whether a cache flush is
1162 if ((iocb
->ki_flags
& (IOCB_DSYNC
| IOCB_SYNC
)) == IOCB_DSYNC
)
1163 dio
->flags
|= IOMAP_DIO_WRITE_FUA
;
1166 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
1167 if (filemap_range_has_page(mapping
, start
, end
)) {
1171 flags
|= IOMAP_NOWAIT
;
1174 ret
= filemap_write_and_wait_range(mapping
, start
, end
);
1179 * Try to invalidate cache pages for the range we're direct
1180 * writing. If this invalidation fails, tough, the write will
1181 * still work, but racing two incompatible write paths is a
1182 * pretty crazy thing to do, so we don't support it 100%.
1184 ret
= invalidate_inode_pages2_range(mapping
,
1185 start
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
);
1187 dio_warn_stale_pagecache(iocb
->ki_filp
);
1190 if (iov_iter_rw(iter
) == WRITE
&& !is_sync_kiocb(iocb
) &&
1191 !inode
->i_sb
->s_dio_done_wq
) {
1192 ret
= sb_init_dio_done_wq(inode
->i_sb
);
1197 inode_dio_begin(inode
);
1199 blk_start_plug(&plug
);
1201 ret
= iomap_apply(inode
, pos
, count
, flags
, ops
, dio
,
1204 /* magic error code to fall back to buffered I/O */
1205 if (ret
== -ENOTBLK
)
1211 if (iov_iter_rw(iter
) == READ
&& pos
>= dio
->i_size
)
1213 } while ((count
= iov_iter_count(iter
)) > 0);
1214 blk_finish_plug(&plug
);
1217 iomap_dio_set_error(dio
, ret
);
1220 * If all the writes we issued were FUA, we don't need to flush the
1221 * cache on IO completion. Clear the sync flag for this case.
1223 if (dio
->flags
& IOMAP_DIO_WRITE_FUA
)
1224 dio
->flags
&= ~IOMAP_DIO_NEED_SYNC
;
1226 if (!atomic_dec_and_test(&dio
->ref
)) {
1227 if (!is_sync_kiocb(iocb
))
1228 return -EIOCBQUEUED
;
1231 set_current_state(TASK_UNINTERRUPTIBLE
);
1232 if (!READ_ONCE(dio
->submit
.waiter
))
1235 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
1236 !dio
->submit
.last_queue
||
1237 !blk_poll(dio
->submit
.last_queue
,
1238 dio
->submit
.cookie
))
1241 __set_current_state(TASK_RUNNING
);
1244 ret
= iomap_dio_complete(dio
);
1252 EXPORT_SYMBOL_GPL(iomap_dio_rw
);
1254 /* Swapfile activation */
1257 struct iomap_swapfile_info
{
1258 struct iomap iomap
; /* accumulated iomap */
1259 struct swap_info_struct
*sis
;
1260 uint64_t lowest_ppage
; /* lowest physical addr seen (pages) */
1261 uint64_t highest_ppage
; /* highest physical addr seen (pages) */
1262 unsigned long nr_pages
; /* number of pages collected */
1263 int nr_extents
; /* extent count */
1267 * Collect physical extents for this swap file. Physical extents reported to
1268 * the swap code must be trimmed to align to a page boundary. The logical
1269 * offset within the file is irrelevant since the swapfile code maps logical
1270 * page numbers of the swap device to the physical page-aligned extents.
1272 static int iomap_swapfile_add_extent(struct iomap_swapfile_info
*isi
)
1274 struct iomap
*iomap
= &isi
->iomap
;
1275 unsigned long nr_pages
;
1276 uint64_t first_ppage
;
1277 uint64_t first_ppage_reported
;
1278 uint64_t next_ppage
;
1282 * Round the start up and the end down so that the physical
1283 * extent aligns to a page boundary.
1285 first_ppage
= ALIGN(iomap
->addr
, PAGE_SIZE
) >> PAGE_SHIFT
;
1286 next_ppage
= ALIGN_DOWN(iomap
->addr
+ iomap
->length
, PAGE_SIZE
) >>
1289 /* Skip too-short physical extents. */
1290 if (first_ppage
>= next_ppage
)
1292 nr_pages
= next_ppage
- first_ppage
;
1295 * Calculate how much swap space we're adding; the first page contains
1296 * the swap header and doesn't count. The mm still wants that first
1297 * page fed to add_swap_extent, however.
1299 first_ppage_reported
= first_ppage
;
1300 if (iomap
->offset
== 0)
1301 first_ppage_reported
++;
1302 if (isi
->lowest_ppage
> first_ppage_reported
)
1303 isi
->lowest_ppage
= first_ppage_reported
;
1304 if (isi
->highest_ppage
< (next_ppage
- 1))
1305 isi
->highest_ppage
= next_ppage
- 1;
1307 /* Add extent, set up for the next call. */
1308 error
= add_swap_extent(isi
->sis
, isi
->nr_pages
, nr_pages
, first_ppage
);
1311 isi
->nr_extents
+= error
;
1312 isi
->nr_pages
+= nr_pages
;
1317 * Accumulate iomaps for this swap file. We have to accumulate iomaps because
1318 * swap only cares about contiguous page-aligned physical extents and makes no
1319 * distinction between written and unwritten extents.
1321 static loff_t
iomap_swapfile_activate_actor(struct inode
*inode
, loff_t pos
,
1322 loff_t count
, void *data
, struct iomap
*iomap
)
1324 struct iomap_swapfile_info
*isi
= data
;
1327 switch (iomap
->type
) {
1329 case IOMAP_UNWRITTEN
:
1330 /* Only real or unwritten extents. */
1333 /* No inline data. */
1334 pr_err("swapon: file is inline\n");
1337 pr_err("swapon: file has unallocated extents\n");
1341 /* No uncommitted metadata or shared blocks. */
1342 if (iomap
->flags
& IOMAP_F_DIRTY
) {
1343 pr_err("swapon: file is not committed\n");
1346 if (iomap
->flags
& IOMAP_F_SHARED
) {
1347 pr_err("swapon: file has shared extents\n");
1351 /* Only one bdev per swap file. */
1352 if (iomap
->bdev
!= isi
->sis
->bdev
) {
1353 pr_err("swapon: file is on multiple devices\n");
1357 if (isi
->iomap
.length
== 0) {
1358 /* No accumulated extent, so just store it. */
1359 memcpy(&isi
->iomap
, iomap
, sizeof(isi
->iomap
));
1360 } else if (isi
->iomap
.addr
+ isi
->iomap
.length
== iomap
->addr
) {
1361 /* Append this to the accumulated extent. */
1362 isi
->iomap
.length
+= iomap
->length
;
1364 /* Otherwise, add the retained iomap and store this one. */
1365 error
= iomap_swapfile_add_extent(isi
);
1368 memcpy(&isi
->iomap
, iomap
, sizeof(isi
->iomap
));
1374 * Iterate a swap file's iomaps to construct physical extents that can be
1375 * passed to the swapfile subsystem.
1377 int iomap_swapfile_activate(struct swap_info_struct
*sis
,
1378 struct file
*swap_file
, sector_t
*pagespan
,
1379 const struct iomap_ops
*ops
)
1381 struct iomap_swapfile_info isi
= {
1383 .lowest_ppage
= (sector_t
)-1ULL,
1385 struct address_space
*mapping
= swap_file
->f_mapping
;
1386 struct inode
*inode
= mapping
->host
;
1388 loff_t len
= ALIGN_DOWN(i_size_read(inode
), PAGE_SIZE
);
1392 * Persist all file mapping metadata so that we won't have any
1393 * IOMAP_F_DIRTY iomaps.
1395 ret
= vfs_fsync(swap_file
, 1);
1400 ret
= iomap_apply(inode
, pos
, len
, IOMAP_REPORT
,
1401 ops
, &isi
, iomap_swapfile_activate_actor
);
1409 if (isi
.iomap
.length
) {
1410 ret
= iomap_swapfile_add_extent(&isi
);
1415 *pagespan
= 1 + isi
.highest_ppage
- isi
.lowest_ppage
;
1416 sis
->max
= isi
.nr_pages
;
1417 sis
->pages
= isi
.nr_pages
- 1;
1418 sis
->highest_bit
= isi
.nr_pages
- 1;
1419 return isi
.nr_extents
;
1421 EXPORT_SYMBOL_GPL(iomap_swapfile_activate
);
1422 #endif /* CONFIG_SWAP */
1425 iomap_bmap_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
1426 void *data
, struct iomap
*iomap
)
1428 sector_t
*bno
= data
, addr
;
1430 if (iomap
->type
== IOMAP_MAPPED
) {
1431 addr
= (pos
- iomap
->offset
+ iomap
->addr
) >> inode
->i_blkbits
;
1433 WARN(1, "would truncate bmap result\n");
1440 /* legacy ->bmap interface. 0 is the error return (!) */
1442 iomap_bmap(struct address_space
*mapping
, sector_t bno
,
1443 const struct iomap_ops
*ops
)
1445 struct inode
*inode
= mapping
->host
;
1446 loff_t pos
= bno
>> inode
->i_blkbits
;
1447 unsigned blocksize
= i_blocksize(inode
);
1449 if (filemap_write_and_wait(mapping
))
1453 iomap_apply(inode
, pos
, blocksize
, 0, ops
, &bno
, iomap_bmap_actor
);
1456 EXPORT_SYMBOL_GPL(iomap_bmap
);