2 * Copyright (C) 2010 Red Hat, Inc.
3 * Copyright (c) 2016-2018 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>
20 #include <linux/migrate.h>
22 #include <linux/mm_inline.h>
23 #include <linux/swap.h>
24 #include <linux/pagemap.h>
25 #include <linux/pagevec.h>
26 #include <linux/file.h>
27 #include <linux/uio.h>
28 #include <linux/backing-dev.h>
29 #include <linux/buffer_head.h>
30 #include <linux/task_io_accounting_ops.h>
31 #include <linux/dax.h>
32 #include <linux/sched/signal.h>
37 * Execute a iomap write on a segment of the mapping that spans a
38 * contiguous range of pages that have identical block mapping state.
40 * This avoids the need to map pages individually, do individual allocations
41 * for each page and most importantly avoid the need for filesystem specific
42 * locking per page. Instead, all the operations are amortised over the entire
43 * range of pages. It is assumed that the filesystems will lock whatever
44 * resources they require in the iomap_begin call, and release them in the
48 iomap_apply(struct inode
*inode
, loff_t pos
, loff_t length
, unsigned flags
,
49 const struct iomap_ops
*ops
, void *data
, iomap_actor_t actor
)
51 struct iomap iomap
= { 0 };
52 loff_t written
= 0, ret
;
55 * Need to map a range from start position for length bytes. This can
56 * span multiple pages - it is only guaranteed to return a range of a
57 * single type of pages (e.g. all into a hole, all mapped or all
58 * unwritten). Failure at this point has nothing to undo.
60 * If allocation is required for this range, reserve the space now so
61 * that the allocation is guaranteed to succeed later on. Once we copy
62 * the data into the page cache pages, then we cannot fail otherwise we
63 * expose transient stale data. If the reserve fails, we can safely
64 * back out at this point as there is nothing to undo.
66 ret
= ops
->iomap_begin(inode
, pos
, length
, flags
, &iomap
);
69 if (WARN_ON(iomap
.offset
> pos
))
71 if (WARN_ON(iomap
.length
== 0))
75 * Cut down the length to the one actually provided by the filesystem,
76 * as it might not be able to give us the whole size that we requested.
78 if (iomap
.offset
+ iomap
.length
< pos
+ length
)
79 length
= iomap
.offset
+ iomap
.length
- pos
;
82 * Now that we have guaranteed that the space allocation will succeed.
83 * we can do the copy-in page by page without having to worry about
84 * failures exposing transient data.
86 written
= actor(inode
, pos
, length
, data
, &iomap
);
89 * Now the data has been copied, commit the range we've copied. This
90 * should not fail unless the filesystem has had a fatal error.
93 ret
= ops
->iomap_end(inode
, pos
, length
,
94 written
> 0 ? written
: 0,
98 return written
? written
: ret
;
102 iomap_sector(struct iomap
*iomap
, loff_t pos
)
104 return (iomap
->addr
+ pos
- iomap
->offset
) >> SECTOR_SHIFT
;
107 static struct iomap_page
*
108 iomap_page_create(struct inode
*inode
, struct page
*page
)
110 struct iomap_page
*iop
= to_iomap_page(page
);
112 if (iop
|| i_blocksize(inode
) == PAGE_SIZE
)
115 iop
= kmalloc(sizeof(*iop
), GFP_NOFS
| __GFP_NOFAIL
);
116 atomic_set(&iop
->read_count
, 0);
117 atomic_set(&iop
->write_count
, 0);
118 bitmap_zero(iop
->uptodate
, PAGE_SIZE
/ SECTOR_SIZE
);
119 set_page_private(page
, (unsigned long)iop
);
120 SetPagePrivate(page
);
125 iomap_page_release(struct page
*page
)
127 struct iomap_page
*iop
= to_iomap_page(page
);
131 WARN_ON_ONCE(atomic_read(&iop
->read_count
));
132 WARN_ON_ONCE(atomic_read(&iop
->write_count
));
133 ClearPagePrivate(page
);
134 set_page_private(page
, 0);
139 * Calculate the range inside the page that we actually need to read.
142 iomap_adjust_read_range(struct inode
*inode
, struct iomap_page
*iop
,
143 loff_t
*pos
, loff_t length
, unsigned *offp
, unsigned *lenp
)
145 loff_t orig_pos
= *pos
;
146 loff_t isize
= i_size_read(inode
);
147 unsigned block_bits
= inode
->i_blkbits
;
148 unsigned block_size
= (1 << block_bits
);
149 unsigned poff
= offset_in_page(*pos
);
150 unsigned plen
= min_t(loff_t
, PAGE_SIZE
- poff
, length
);
151 unsigned first
= poff
>> block_bits
;
152 unsigned last
= (poff
+ plen
- 1) >> block_bits
;
155 * If the block size is smaller than the page size we need to check the
156 * per-block uptodate status and adjust the offset and length if needed
157 * to avoid reading in already uptodate ranges.
162 /* move forward for each leading block marked uptodate */
163 for (i
= first
; i
<= last
; i
++) {
164 if (!test_bit(i
, iop
->uptodate
))
172 /* truncate len if we find any trailing uptodate block(s) */
173 for ( ; i
<= last
; i
++) {
174 if (test_bit(i
, iop
->uptodate
)) {
175 plen
-= (last
- i
+ 1) * block_size
;
183 * If the extent spans the block that contains the i_size we need to
184 * handle both halves separately so that we properly zero data in the
185 * page cache for blocks that are entirely outside of i_size.
187 if (orig_pos
<= isize
&& orig_pos
+ length
> isize
) {
188 unsigned end
= offset_in_page(isize
- 1) >> block_bits
;
190 if (first
<= end
&& last
> end
)
191 plen
-= (last
- end
) * block_size
;
199 iomap_set_range_uptodate(struct page
*page
, unsigned off
, unsigned len
)
201 struct iomap_page
*iop
= to_iomap_page(page
);
202 struct inode
*inode
= page
->mapping
->host
;
203 unsigned first
= off
>> inode
->i_blkbits
;
204 unsigned last
= (off
+ len
- 1) >> inode
->i_blkbits
;
206 bool uptodate
= true;
209 for (i
= 0; i
< PAGE_SIZE
/ i_blocksize(inode
); i
++) {
210 if (i
>= first
&& i
<= last
)
211 set_bit(i
, iop
->uptodate
);
212 else if (!test_bit(i
, iop
->uptodate
))
217 if (uptodate
&& !PageError(page
))
218 SetPageUptodate(page
);
222 iomap_read_finish(struct iomap_page
*iop
, struct page
*page
)
224 if (!iop
|| atomic_dec_and_test(&iop
->read_count
))
229 iomap_read_page_end_io(struct bio_vec
*bvec
, int error
)
231 struct page
*page
= bvec
->bv_page
;
232 struct iomap_page
*iop
= to_iomap_page(page
);
234 if (unlikely(error
)) {
235 ClearPageUptodate(page
);
238 iomap_set_range_uptodate(page
, bvec
->bv_offset
, bvec
->bv_len
);
241 iomap_read_finish(iop
, page
);
245 iomap_read_inline_data(struct inode
*inode
, struct page
*page
,
248 size_t size
= i_size_read(inode
);
251 if (PageUptodate(page
))
255 BUG_ON(size
> PAGE_SIZE
- offset_in_page(iomap
->inline_data
));
257 addr
= kmap_atomic(page
);
258 memcpy(addr
, iomap
->inline_data
, size
);
259 memset(addr
+ size
, 0, PAGE_SIZE
- size
);
261 SetPageUptodate(page
);
265 iomap_read_end_io(struct bio
*bio
)
267 int error
= blk_status_to_errno(bio
->bi_status
);
268 struct bio_vec
*bvec
;
271 bio_for_each_segment_all(bvec
, bio
, i
)
272 iomap_read_page_end_io(bvec
, error
);
276 struct iomap_readpage_ctx
{
277 struct page
*cur_page
;
278 bool cur_page_in_bio
;
281 struct list_head
*pages
;
285 iomap_readpage_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
288 struct iomap_readpage_ctx
*ctx
= data
;
289 struct page
*page
= ctx
->cur_page
;
290 struct iomap_page
*iop
= iomap_page_create(inode
, page
);
291 bool is_contig
= false;
292 loff_t orig_pos
= pos
;
296 if (iomap
->type
== IOMAP_INLINE
) {
298 iomap_read_inline_data(inode
, page
, iomap
);
302 /* zero post-eof blocks as the page may be mapped */
303 iomap_adjust_read_range(inode
, iop
, &pos
, length
, &poff
, &plen
);
307 if (iomap
->type
!= IOMAP_MAPPED
|| pos
>= i_size_read(inode
)) {
308 zero_user(page
, poff
, plen
);
309 iomap_set_range_uptodate(page
, poff
, plen
);
313 ctx
->cur_page_in_bio
= true;
316 * Try to merge into a previous segment if we can.
318 sector
= iomap_sector(iomap
, pos
);
319 if (ctx
->bio
&& bio_end_sector(ctx
->bio
) == sector
) {
320 if (__bio_try_merge_page(ctx
->bio
, page
, plen
, poff
))
326 * If we start a new segment we need to increase the read count, and we
327 * need to do so before submitting any previous full bio to make sure
328 * that we don't prematurely unlock the page.
331 atomic_inc(&iop
->read_count
);
333 if (!ctx
->bio
|| !is_contig
|| bio_full(ctx
->bio
)) {
334 gfp_t gfp
= mapping_gfp_constraint(page
->mapping
, GFP_KERNEL
);
335 int nr_vecs
= (length
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
338 submit_bio(ctx
->bio
);
340 if (ctx
->is_readahead
) /* same as readahead_gfp_mask */
341 gfp
|= __GFP_NORETRY
| __GFP_NOWARN
;
342 ctx
->bio
= bio_alloc(gfp
, min(BIO_MAX_PAGES
, nr_vecs
));
343 ctx
->bio
->bi_opf
= REQ_OP_READ
;
344 if (ctx
->is_readahead
)
345 ctx
->bio
->bi_opf
|= REQ_RAHEAD
;
346 ctx
->bio
->bi_iter
.bi_sector
= sector
;
347 bio_set_dev(ctx
->bio
, iomap
->bdev
);
348 ctx
->bio
->bi_end_io
= iomap_read_end_io
;
351 __bio_add_page(ctx
->bio
, page
, plen
, poff
);
354 * Move the caller beyond our range so that it keeps making progress.
355 * For that we have to include any leading non-uptodate ranges, but
356 * we can skip trailing ones as they will be handled in the next
359 return pos
- orig_pos
+ plen
;
363 iomap_readpage(struct page
*page
, const struct iomap_ops
*ops
)
365 struct iomap_readpage_ctx ctx
= { .cur_page
= page
};
366 struct inode
*inode
= page
->mapping
->host
;
370 for (poff
= 0; poff
< PAGE_SIZE
; poff
+= ret
) {
371 ret
= iomap_apply(inode
, page_offset(page
) + poff
,
372 PAGE_SIZE
- poff
, 0, ops
, &ctx
,
373 iomap_readpage_actor
);
375 WARN_ON_ONCE(ret
== 0);
383 WARN_ON_ONCE(!ctx
.cur_page_in_bio
);
385 WARN_ON_ONCE(ctx
.cur_page_in_bio
);
390 * Just like mpage_readpages and block_read_full_page we always
391 * return 0 and just mark the page as PageError on errors. This
392 * should be cleaned up all through the stack eventually.
396 EXPORT_SYMBOL_GPL(iomap_readpage
);
399 iomap_next_page(struct inode
*inode
, struct list_head
*pages
, loff_t pos
,
400 loff_t length
, loff_t
*done
)
402 while (!list_empty(pages
)) {
403 struct page
*page
= lru_to_page(pages
);
405 if (page_offset(page
) >= (u64
)pos
+ length
)
408 list_del(&page
->lru
);
409 if (!add_to_page_cache_lru(page
, inode
->i_mapping
, page
->index
,
414 * If we already have a page in the page cache at index we are
415 * done. Upper layers don't care if it is uptodate after the
416 * readpages call itself as every page gets checked again once
427 iomap_readpages_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
428 void *data
, struct iomap
*iomap
)
430 struct iomap_readpage_ctx
*ctx
= data
;
433 for (done
= 0; done
< length
; done
+= ret
) {
434 if (ctx
->cur_page
&& offset_in_page(pos
+ done
) == 0) {
435 if (!ctx
->cur_page_in_bio
)
436 unlock_page(ctx
->cur_page
);
437 put_page(ctx
->cur_page
);
438 ctx
->cur_page
= NULL
;
440 if (!ctx
->cur_page
) {
441 ctx
->cur_page
= iomap_next_page(inode
, ctx
->pages
,
445 ctx
->cur_page_in_bio
= false;
447 ret
= iomap_readpage_actor(inode
, pos
+ done
, length
- done
,
455 iomap_readpages(struct address_space
*mapping
, struct list_head
*pages
,
456 unsigned nr_pages
, const struct iomap_ops
*ops
)
458 struct iomap_readpage_ctx ctx
= {
460 .is_readahead
= true,
462 loff_t pos
= page_offset(list_entry(pages
->prev
, struct page
, lru
));
463 loff_t last
= page_offset(list_entry(pages
->next
, struct page
, lru
));
464 loff_t length
= last
- pos
+ PAGE_SIZE
, ret
= 0;
467 ret
= iomap_apply(mapping
->host
, pos
, length
, 0, ops
,
468 &ctx
, iomap_readpages_actor
);
470 WARN_ON_ONCE(ret
== 0);
481 if (!ctx
.cur_page_in_bio
)
482 unlock_page(ctx
.cur_page
);
483 put_page(ctx
.cur_page
);
487 * Check that we didn't lose a page due to the arcance calling
490 WARN_ON_ONCE(!ret
&& !list_empty(ctx
.pages
));
493 EXPORT_SYMBOL_GPL(iomap_readpages
);
496 * iomap_is_partially_uptodate checks whether blocks within a page are
499 * Returns true if all blocks which correspond to a file portion
500 * we want to read within the page are uptodate.
503 iomap_is_partially_uptodate(struct page
*page
, unsigned long from
,
506 struct iomap_page
*iop
= to_iomap_page(page
);
507 struct inode
*inode
= page
->mapping
->host
;
508 unsigned len
, first
, last
;
511 /* Limit range to one page */
512 len
= min_t(unsigned, PAGE_SIZE
- from
, count
);
514 /* First and last blocks in range within page */
515 first
= from
>> inode
->i_blkbits
;
516 last
= (from
+ len
- 1) >> inode
->i_blkbits
;
519 for (i
= first
; i
<= last
; i
++)
520 if (!test_bit(i
, iop
->uptodate
))
527 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate
);
530 iomap_releasepage(struct page
*page
, gfp_t gfp_mask
)
533 * mm accommodates an old ext3 case where clean pages might not have had
534 * the dirty bit cleared. Thus, it can send actual dirty pages to
535 * ->releasepage() via shrink_active_list(), skip those here.
537 if (PageDirty(page
) || PageWriteback(page
))
539 iomap_page_release(page
);
542 EXPORT_SYMBOL_GPL(iomap_releasepage
);
545 iomap_invalidatepage(struct page
*page
, unsigned int offset
, unsigned int len
)
548 * If we are invalidating the entire page, clear the dirty state from it
549 * and release it to avoid unnecessary buildup of the LRU.
551 if (offset
== 0 && len
== PAGE_SIZE
) {
552 WARN_ON_ONCE(PageWriteback(page
));
553 cancel_dirty_page(page
);
554 iomap_page_release(page
);
557 EXPORT_SYMBOL_GPL(iomap_invalidatepage
);
559 #ifdef CONFIG_MIGRATION
561 iomap_migrate_page(struct address_space
*mapping
, struct page
*newpage
,
562 struct page
*page
, enum migrate_mode mode
)
566 ret
= migrate_page_move_mapping(mapping
, newpage
, page
, mode
, 0);
567 if (ret
!= MIGRATEPAGE_SUCCESS
)
570 if (page_has_private(page
)) {
571 ClearPagePrivate(page
);
572 set_page_private(newpage
, page_private(page
));
573 set_page_private(page
, 0);
574 SetPagePrivate(newpage
);
577 if (mode
!= MIGRATE_SYNC_NO_COPY
)
578 migrate_page_copy(newpage
, page
);
580 migrate_page_states(newpage
, page
);
581 return MIGRATEPAGE_SUCCESS
;
583 EXPORT_SYMBOL_GPL(iomap_migrate_page
);
584 #endif /* CONFIG_MIGRATION */
587 iomap_write_failed(struct inode
*inode
, loff_t pos
, unsigned len
)
589 loff_t i_size
= i_size_read(inode
);
592 * Only truncate newly allocated pages beyoned EOF, even if the
593 * write started inside the existing inode size.
595 if (pos
+ len
> i_size
)
596 truncate_pagecache_range(inode
, max(pos
, i_size
), pos
+ len
);
600 iomap_read_page_sync(struct inode
*inode
, loff_t block_start
, struct page
*page
,
601 unsigned poff
, unsigned plen
, unsigned from
, unsigned to
,
607 if (iomap
->type
!= IOMAP_MAPPED
|| block_start
>= i_size_read(inode
)) {
608 zero_user_segments(page
, poff
, from
, to
, poff
+ plen
);
609 iomap_set_range_uptodate(page
, poff
, plen
);
613 bio_init(&bio
, &bvec
, 1);
614 bio
.bi_opf
= REQ_OP_READ
;
615 bio
.bi_iter
.bi_sector
= iomap_sector(iomap
, block_start
);
616 bio_set_dev(&bio
, iomap
->bdev
);
617 __bio_add_page(&bio
, page
, plen
, poff
);
618 return submit_bio_wait(&bio
);
622 __iomap_write_begin(struct inode
*inode
, loff_t pos
, unsigned len
,
623 struct page
*page
, struct iomap
*iomap
)
625 struct iomap_page
*iop
= iomap_page_create(inode
, page
);
626 loff_t block_size
= i_blocksize(inode
);
627 loff_t block_start
= pos
& ~(block_size
- 1);
628 loff_t block_end
= (pos
+ len
+ block_size
- 1) & ~(block_size
- 1);
629 unsigned from
= offset_in_page(pos
), to
= from
+ len
, poff
, plen
;
632 if (PageUptodate(page
))
636 iomap_adjust_read_range(inode
, iop
, &block_start
,
637 block_end
- block_start
, &poff
, &plen
);
641 if ((from
> poff
&& from
< poff
+ plen
) ||
642 (to
> poff
&& to
< poff
+ plen
)) {
643 status
= iomap_read_page_sync(inode
, block_start
, page
,
644 poff
, plen
, from
, to
, iomap
);
649 } while ((block_start
+= plen
) < block_end
);
655 iomap_write_begin(struct inode
*inode
, loff_t pos
, unsigned len
, unsigned flags
,
656 struct page
**pagep
, struct iomap
*iomap
)
658 pgoff_t index
= pos
>> PAGE_SHIFT
;
662 BUG_ON(pos
+ len
> iomap
->offset
+ iomap
->length
);
664 if (fatal_signal_pending(current
))
667 page
= grab_cache_page_write_begin(inode
->i_mapping
, index
, flags
);
671 if (iomap
->type
== IOMAP_INLINE
)
672 iomap_read_inline_data(inode
, page
, iomap
);
673 else if (iomap
->flags
& IOMAP_F_BUFFER_HEAD
)
674 status
= __block_write_begin_int(page
, pos
, len
, NULL
, iomap
);
676 status
= __iomap_write_begin(inode
, pos
, len
, page
, iomap
);
677 if (unlikely(status
)) {
682 iomap_write_failed(inode
, pos
, len
);
690 iomap_set_page_dirty(struct page
*page
)
692 struct address_space
*mapping
= page_mapping(page
);
695 if (unlikely(!mapping
))
696 return !TestSetPageDirty(page
);
699 * Lock out page->mem_cgroup migration to keep PageDirty
700 * synchronized with per-memcg dirty page counters.
702 lock_page_memcg(page
);
703 newly_dirty
= !TestSetPageDirty(page
);
705 __set_page_dirty(page
, mapping
, 0);
706 unlock_page_memcg(page
);
709 __mark_inode_dirty(mapping
->host
, I_DIRTY_PAGES
);
712 EXPORT_SYMBOL_GPL(iomap_set_page_dirty
);
715 __iomap_write_end(struct inode
*inode
, loff_t pos
, unsigned len
,
716 unsigned copied
, struct page
*page
, struct iomap
*iomap
)
718 flush_dcache_page(page
);
721 * The blocks that were entirely written will now be uptodate, so we
722 * don't have to worry about a readpage reading them and overwriting a
723 * partial write. However if we have encountered a short write and only
724 * partially written into a block, it will not be marked uptodate, so a
725 * readpage might come in and destroy our partial write.
727 * Do the simplest thing, and just treat any short write to a non
728 * uptodate page as a zero-length write, and force the caller to redo
731 if (unlikely(copied
< len
&& !PageUptodate(page
))) {
734 iomap_set_range_uptodate(page
, offset_in_page(pos
), len
);
735 iomap_set_page_dirty(page
);
737 return __generic_write_end(inode
, pos
, copied
, page
);
741 iomap_write_end_inline(struct inode
*inode
, struct page
*page
,
742 struct iomap
*iomap
, loff_t pos
, unsigned copied
)
746 WARN_ON_ONCE(!PageUptodate(page
));
747 BUG_ON(pos
+ copied
> PAGE_SIZE
- offset_in_page(iomap
->inline_data
));
749 addr
= kmap_atomic(page
);
750 memcpy(iomap
->inline_data
+ pos
, addr
+ pos
, copied
);
753 mark_inode_dirty(inode
);
754 __generic_write_end(inode
, pos
, copied
, page
);
759 iomap_write_end(struct inode
*inode
, loff_t pos
, unsigned len
,
760 unsigned copied
, struct page
*page
, struct iomap
*iomap
)
764 if (iomap
->type
== IOMAP_INLINE
) {
765 ret
= iomap_write_end_inline(inode
, page
, iomap
, pos
, copied
);
766 } else if (iomap
->flags
& IOMAP_F_BUFFER_HEAD
) {
767 ret
= generic_write_end(NULL
, inode
->i_mapping
, pos
, len
,
770 ret
= __iomap_write_end(inode
, pos
, len
, copied
, page
, iomap
);
773 if (iomap
->page_done
)
774 iomap
->page_done(inode
, pos
, copied
, page
, iomap
);
777 iomap_write_failed(inode
, pos
, len
);
782 iomap_write_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
785 struct iov_iter
*i
= data
;
788 unsigned int flags
= AOP_FLAG_NOFS
;
792 unsigned long offset
; /* Offset into pagecache page */
793 unsigned long bytes
; /* Bytes to write to page */
794 size_t copied
; /* Bytes copied from user */
796 offset
= offset_in_page(pos
);
797 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
804 * Bring in the user page that we will copy from _first_.
805 * Otherwise there's a nasty deadlock on copying from the
806 * same page as we're writing to, without it being marked
809 * Not only is this an optimisation, but it is also required
810 * to check that the address is actually valid, when atomic
811 * usercopies are used, below.
813 if (unlikely(iov_iter_fault_in_readable(i
, bytes
))) {
818 status
= iomap_write_begin(inode
, pos
, bytes
, flags
, &page
,
820 if (unlikely(status
))
823 if (mapping_writably_mapped(inode
->i_mapping
))
824 flush_dcache_page(page
);
826 copied
= iov_iter_copy_from_user_atomic(page
, i
, offset
, bytes
);
828 flush_dcache_page(page
);
830 status
= iomap_write_end(inode
, pos
, bytes
, copied
, page
,
832 if (unlikely(status
< 0))
838 iov_iter_advance(i
, copied
);
839 if (unlikely(copied
== 0)) {
841 * If we were unable to copy any data at all, we must
842 * fall back to a single segment length write.
844 * If we didn't fallback here, we could livelock
845 * because not all segments in the iov can be copied at
846 * once without a pagefault.
848 bytes
= min_t(unsigned long, PAGE_SIZE
- offset
,
849 iov_iter_single_seg_count(i
));
856 balance_dirty_pages_ratelimited(inode
->i_mapping
);
857 } while (iov_iter_count(i
) && length
);
859 return written
? written
: status
;
863 iomap_file_buffered_write(struct kiocb
*iocb
, struct iov_iter
*iter
,
864 const struct iomap_ops
*ops
)
866 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
867 loff_t pos
= iocb
->ki_pos
, ret
= 0, written
= 0;
869 while (iov_iter_count(iter
)) {
870 ret
= iomap_apply(inode
, pos
, iov_iter_count(iter
),
871 IOMAP_WRITE
, ops
, iter
, iomap_write_actor
);
878 return written
? written
: ret
;
880 EXPORT_SYMBOL_GPL(iomap_file_buffered_write
);
883 __iomap_read_page(struct inode
*inode
, loff_t offset
)
885 struct address_space
*mapping
= inode
->i_mapping
;
888 page
= read_mapping_page(mapping
, offset
>> PAGE_SHIFT
, NULL
);
891 if (!PageUptodate(page
)) {
893 return ERR_PTR(-EIO
);
899 iomap_dirty_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
906 struct page
*page
, *rpage
;
907 unsigned long offset
; /* Offset into pagecache page */
908 unsigned long bytes
; /* Bytes to write to page */
910 offset
= offset_in_page(pos
);
911 bytes
= min_t(loff_t
, PAGE_SIZE
- offset
, length
);
913 rpage
= __iomap_read_page(inode
, pos
);
915 return PTR_ERR(rpage
);
917 status
= iomap_write_begin(inode
, pos
, bytes
,
918 AOP_FLAG_NOFS
, &page
, iomap
);
920 if (unlikely(status
))
923 WARN_ON_ONCE(!PageUptodate(page
));
925 status
= iomap_write_end(inode
, pos
, bytes
, bytes
, page
, iomap
);
926 if (unlikely(status
<= 0)) {
927 if (WARN_ON_ONCE(status
== 0))
938 balance_dirty_pages_ratelimited(inode
->i_mapping
);
945 iomap_file_dirty(struct inode
*inode
, loff_t pos
, loff_t len
,
946 const struct iomap_ops
*ops
)
951 ret
= iomap_apply(inode
, pos
, len
, IOMAP_WRITE
, ops
, NULL
,
961 EXPORT_SYMBOL_GPL(iomap_file_dirty
);
963 static int iomap_zero(struct inode
*inode
, loff_t pos
, unsigned offset
,
964 unsigned bytes
, struct iomap
*iomap
)
969 status
= iomap_write_begin(inode
, pos
, bytes
, AOP_FLAG_NOFS
, &page
,
974 zero_user(page
, offset
, bytes
);
975 mark_page_accessed(page
);
977 return iomap_write_end(inode
, pos
, bytes
, bytes
, page
, iomap
);
980 static int iomap_dax_zero(loff_t pos
, unsigned offset
, unsigned bytes
,
983 return __dax_zero_page_range(iomap
->bdev
, iomap
->dax_dev
,
984 iomap_sector(iomap
, pos
& PAGE_MASK
), offset
, bytes
);
988 iomap_zero_range_actor(struct inode
*inode
, loff_t pos
, loff_t count
,
989 void *data
, struct iomap
*iomap
)
991 bool *did_zero
= data
;
995 /* already zeroed? we're done. */
996 if (iomap
->type
== IOMAP_HOLE
|| iomap
->type
== IOMAP_UNWRITTEN
)
1000 unsigned offset
, bytes
;
1002 offset
= offset_in_page(pos
);
1003 bytes
= min_t(loff_t
, PAGE_SIZE
- offset
, count
);
1006 status
= iomap_dax_zero(pos
, offset
, bytes
, iomap
);
1008 status
= iomap_zero(inode
, pos
, offset
, bytes
, iomap
);
1017 } while (count
> 0);
1023 iomap_zero_range(struct inode
*inode
, loff_t pos
, loff_t len
, bool *did_zero
,
1024 const struct iomap_ops
*ops
)
1029 ret
= iomap_apply(inode
, pos
, len
, IOMAP_ZERO
,
1030 ops
, did_zero
, iomap_zero_range_actor
);
1040 EXPORT_SYMBOL_GPL(iomap_zero_range
);
1043 iomap_truncate_page(struct inode
*inode
, loff_t pos
, bool *did_zero
,
1044 const struct iomap_ops
*ops
)
1046 unsigned int blocksize
= i_blocksize(inode
);
1047 unsigned int off
= pos
& (blocksize
- 1);
1049 /* Block boundary? Nothing to do */
1052 return iomap_zero_range(inode
, pos
, blocksize
- off
, did_zero
, ops
);
1054 EXPORT_SYMBOL_GPL(iomap_truncate_page
);
1057 iomap_page_mkwrite_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
1058 void *data
, struct iomap
*iomap
)
1060 struct page
*page
= data
;
1063 if (iomap
->flags
& IOMAP_F_BUFFER_HEAD
) {
1064 ret
= __block_write_begin_int(page
, pos
, length
, NULL
, iomap
);
1067 block_commit_write(page
, 0, length
);
1069 WARN_ON_ONCE(!PageUptodate(page
));
1070 iomap_page_create(inode
, page
);
1071 set_page_dirty(page
);
1077 vm_fault_t
iomap_page_mkwrite(struct vm_fault
*vmf
, const struct iomap_ops
*ops
)
1079 struct page
*page
= vmf
->page
;
1080 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
1081 unsigned long length
;
1082 loff_t offset
, size
;
1086 size
= i_size_read(inode
);
1087 if ((page
->mapping
!= inode
->i_mapping
) ||
1088 (page_offset(page
) > size
)) {
1089 /* We overload EFAULT to mean page got truncated */
1094 /* page is wholly or partially inside EOF */
1095 if (((page
->index
+ 1) << PAGE_SHIFT
) > size
)
1096 length
= offset_in_page(size
);
1100 offset
= page_offset(page
);
1101 while (length
> 0) {
1102 ret
= iomap_apply(inode
, offset
, length
,
1103 IOMAP_WRITE
| IOMAP_FAULT
, ops
, page
,
1104 iomap_page_mkwrite_actor
);
1105 if (unlikely(ret
<= 0))
1111 wait_for_stable_page(page
);
1112 return VM_FAULT_LOCKED
;
1115 return block_page_mkwrite_return(ret
);
1117 EXPORT_SYMBOL_GPL(iomap_page_mkwrite
);
1120 struct fiemap_extent_info
*fi
;
1124 static int iomap_to_fiemap(struct fiemap_extent_info
*fi
,
1125 struct iomap
*iomap
, u32 flags
)
1127 switch (iomap
->type
) {
1131 case IOMAP_DELALLOC
:
1132 flags
|= FIEMAP_EXTENT_DELALLOC
| FIEMAP_EXTENT_UNKNOWN
;
1136 case IOMAP_UNWRITTEN
:
1137 flags
|= FIEMAP_EXTENT_UNWRITTEN
;
1140 flags
|= FIEMAP_EXTENT_DATA_INLINE
;
1144 if (iomap
->flags
& IOMAP_F_MERGED
)
1145 flags
|= FIEMAP_EXTENT_MERGED
;
1146 if (iomap
->flags
& IOMAP_F_SHARED
)
1147 flags
|= FIEMAP_EXTENT_SHARED
;
1149 return fiemap_fill_next_extent(fi
, iomap
->offset
,
1150 iomap
->addr
!= IOMAP_NULL_ADDR
? iomap
->addr
: 0,
1151 iomap
->length
, flags
);
1155 iomap_fiemap_actor(struct inode
*inode
, loff_t pos
, loff_t length
, void *data
,
1156 struct iomap
*iomap
)
1158 struct fiemap_ctx
*ctx
= data
;
1159 loff_t ret
= length
;
1161 if (iomap
->type
== IOMAP_HOLE
)
1164 ret
= iomap_to_fiemap(ctx
->fi
, &ctx
->prev
, 0);
1167 case 0: /* success */
1169 case 1: /* extent array full */
1176 int iomap_fiemap(struct inode
*inode
, struct fiemap_extent_info
*fi
,
1177 loff_t start
, loff_t len
, const struct iomap_ops
*ops
)
1179 struct fiemap_ctx ctx
;
1182 memset(&ctx
, 0, sizeof(ctx
));
1184 ctx
.prev
.type
= IOMAP_HOLE
;
1186 ret
= fiemap_check_flags(fi
, FIEMAP_FLAG_SYNC
);
1190 if (fi
->fi_flags
& FIEMAP_FLAG_SYNC
) {
1191 ret
= filemap_write_and_wait(inode
->i_mapping
);
1197 ret
= iomap_apply(inode
, start
, len
, IOMAP_REPORT
, ops
, &ctx
,
1198 iomap_fiemap_actor
);
1199 /* inode with no (attribute) mapping will give ENOENT */
1211 if (ctx
.prev
.type
!= IOMAP_HOLE
) {
1212 ret
= iomap_to_fiemap(fi
, &ctx
.prev
, FIEMAP_EXTENT_LAST
);
1219 EXPORT_SYMBOL_GPL(iomap_fiemap
);
1222 * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
1223 * Returns true if found and updates @lastoff to the offset in file.
1226 page_seek_hole_data(struct inode
*inode
, struct page
*page
, loff_t
*lastoff
,
1229 const struct address_space_operations
*ops
= inode
->i_mapping
->a_ops
;
1230 unsigned int bsize
= i_blocksize(inode
), off
;
1231 bool seek_data
= whence
== SEEK_DATA
;
1232 loff_t poff
= page_offset(page
);
1234 if (WARN_ON_ONCE(*lastoff
>= poff
+ PAGE_SIZE
))
1237 if (*lastoff
< poff
) {
1239 * Last offset smaller than the start of the page means we found
1242 if (whence
== SEEK_HOLE
)
1248 * Just check the page unless we can and should check block ranges:
1250 if (bsize
== PAGE_SIZE
|| !ops
->is_partially_uptodate
)
1251 return PageUptodate(page
) == seek_data
;
1254 if (unlikely(page
->mapping
!= inode
->i_mapping
))
1255 goto out_unlock_not_found
;
1257 for (off
= 0; off
< PAGE_SIZE
; off
+= bsize
) {
1258 if (offset_in_page(*lastoff
) >= off
+ bsize
)
1260 if (ops
->is_partially_uptodate(page
, off
, bsize
) == seek_data
) {
1264 *lastoff
= poff
+ off
+ bsize
;
1267 out_unlock_not_found
:
1273 * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
1275 * Within unwritten extents, the page cache determines which parts are holes
1276 * and which are data: uptodate buffer heads count as data; everything else
1279 * Returns the resulting offset on successs, and -ENOENT otherwise.
1282 page_cache_seek_hole_data(struct inode
*inode
, loff_t offset
, loff_t length
,
1285 pgoff_t index
= offset
>> PAGE_SHIFT
;
1286 pgoff_t end
= DIV_ROUND_UP(offset
+ length
, PAGE_SIZE
);
1287 loff_t lastoff
= offset
;
1288 struct pagevec pvec
;
1293 pagevec_init(&pvec
);
1296 unsigned nr_pages
, i
;
1298 nr_pages
= pagevec_lookup_range(&pvec
, inode
->i_mapping
, &index
,
1303 for (i
= 0; i
< nr_pages
; i
++) {
1304 struct page
*page
= pvec
.pages
[i
];
1306 if (page_seek_hole_data(inode
, page
, &lastoff
, whence
))
1308 lastoff
= page_offset(page
) + PAGE_SIZE
;
1310 pagevec_release(&pvec
);
1311 } while (index
< end
);
1313 /* When no page at lastoff and we are not done, we found a hole. */
1314 if (whence
!= SEEK_HOLE
)
1318 if (lastoff
< offset
+ length
)
1323 pagevec_release(&pvec
);
1329 iomap_seek_hole_actor(struct inode
*inode
, loff_t offset
, loff_t length
,
1330 void *data
, struct iomap
*iomap
)
1332 switch (iomap
->type
) {
1333 case IOMAP_UNWRITTEN
:
1334 offset
= page_cache_seek_hole_data(inode
, offset
, length
,
1340 *(loff_t
*)data
= offset
;
1348 iomap_seek_hole(struct inode
*inode
, loff_t offset
, const struct iomap_ops
*ops
)
1350 loff_t size
= i_size_read(inode
);
1351 loff_t length
= size
- offset
;
1354 /* Nothing to be found before or beyond the end of the file. */
1355 if (offset
< 0 || offset
>= size
)
1358 while (length
> 0) {
1359 ret
= iomap_apply(inode
, offset
, length
, IOMAP_REPORT
, ops
,
1360 &offset
, iomap_seek_hole_actor
);
1372 EXPORT_SYMBOL_GPL(iomap_seek_hole
);
1375 iomap_seek_data_actor(struct inode
*inode
, loff_t offset
, loff_t length
,
1376 void *data
, struct iomap
*iomap
)
1378 switch (iomap
->type
) {
1381 case IOMAP_UNWRITTEN
:
1382 offset
= page_cache_seek_hole_data(inode
, offset
, length
,
1388 *(loff_t
*)data
= offset
;
1394 iomap_seek_data(struct inode
*inode
, loff_t offset
, const struct iomap_ops
*ops
)
1396 loff_t size
= i_size_read(inode
);
1397 loff_t length
= size
- offset
;
1400 /* Nothing to be found before or beyond the end of the file. */
1401 if (offset
< 0 || offset
>= size
)
1404 while (length
> 0) {
1405 ret
= iomap_apply(inode
, offset
, length
, IOMAP_REPORT
, ops
,
1406 &offset
, iomap_seek_data_actor
);
1420 EXPORT_SYMBOL_GPL(iomap_seek_data
);
1423 * Private flags for iomap_dio, must not overlap with the public ones in
1426 #define IOMAP_DIO_WRITE_FUA (1 << 28)
1427 #define IOMAP_DIO_NEED_SYNC (1 << 29)
1428 #define IOMAP_DIO_WRITE (1 << 30)
1429 #define IOMAP_DIO_DIRTY (1 << 31)
1433 iomap_dio_end_io_t
*end_io
;
1439 bool wait_for_completion
;
1442 /* used during submission and for synchronous completion: */
1444 struct iov_iter
*iter
;
1445 struct task_struct
*waiter
;
1446 struct request_queue
*last_queue
;
1450 /* used for aio completion: */
1452 struct work_struct work
;
1457 static ssize_t
iomap_dio_complete(struct iomap_dio
*dio
)
1459 struct kiocb
*iocb
= dio
->iocb
;
1460 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1461 loff_t offset
= iocb
->ki_pos
;
1465 ret
= dio
->end_io(iocb
,
1466 dio
->error
? dio
->error
: dio
->size
,
1474 /* check for short read */
1475 if (offset
+ ret
> dio
->i_size
&&
1476 !(dio
->flags
& IOMAP_DIO_WRITE
))
1477 ret
= dio
->i_size
- offset
;
1478 iocb
->ki_pos
+= ret
;
1482 * Try again to invalidate clean pages which might have been cached by
1483 * non-direct readahead, or faulted in by get_user_pages() if the source
1484 * of the write was an mmap'ed region of the file we're writing. Either
1485 * one is a pretty crazy thing to do, so we don't support it 100%. If
1486 * this invalidation fails, tough, the write still worked...
1488 * And this page cache invalidation has to be after dio->end_io(), as
1489 * some filesystems convert unwritten extents to real allocations in
1490 * end_io() when necessary, otherwise a racing buffer read would cache
1491 * zeros from unwritten extents.
1494 (dio
->flags
& IOMAP_DIO_WRITE
) && inode
->i_mapping
->nrpages
) {
1496 err
= invalidate_inode_pages2_range(inode
->i_mapping
,
1497 offset
>> PAGE_SHIFT
,
1498 (offset
+ dio
->size
- 1) >> PAGE_SHIFT
);
1500 dio_warn_stale_pagecache(iocb
->ki_filp
);
1504 * If this is a DSYNC write, make sure we push it to stable storage now
1505 * that we've written data.
1507 if (ret
> 0 && (dio
->flags
& IOMAP_DIO_NEED_SYNC
))
1508 ret
= generic_write_sync(iocb
, ret
);
1510 inode_dio_end(file_inode(iocb
->ki_filp
));
1516 static void iomap_dio_complete_work(struct work_struct
*work
)
1518 struct iomap_dio
*dio
= container_of(work
, struct iomap_dio
, aio
.work
);
1519 struct kiocb
*iocb
= dio
->iocb
;
1521 iocb
->ki_complete(iocb
, iomap_dio_complete(dio
), 0);
1525 * Set an error in the dio if none is set yet. We have to use cmpxchg
1526 * as the submission context and the completion context(s) can race to
1529 static inline void iomap_dio_set_error(struct iomap_dio
*dio
, int ret
)
1531 cmpxchg(&dio
->error
, 0, ret
);
1534 static void iomap_dio_bio_end_io(struct bio
*bio
)
1536 struct iomap_dio
*dio
= bio
->bi_private
;
1537 bool should_dirty
= (dio
->flags
& IOMAP_DIO_DIRTY
);
1540 iomap_dio_set_error(dio
, blk_status_to_errno(bio
->bi_status
));
1542 if (atomic_dec_and_test(&dio
->ref
)) {
1543 if (dio
->wait_for_completion
) {
1544 struct task_struct
*waiter
= dio
->submit
.waiter
;
1545 WRITE_ONCE(dio
->submit
.waiter
, NULL
);
1546 blk_wake_io_task(waiter
);
1547 } else if (dio
->flags
& IOMAP_DIO_WRITE
) {
1548 struct inode
*inode
= file_inode(dio
->iocb
->ki_filp
);
1550 INIT_WORK(&dio
->aio
.work
, iomap_dio_complete_work
);
1551 queue_work(inode
->i_sb
->s_dio_done_wq
, &dio
->aio
.work
);
1553 iomap_dio_complete_work(&dio
->aio
.work
);
1558 bio_check_pages_dirty(bio
);
1560 struct bio_vec
*bvec
;
1563 bio_for_each_segment_all(bvec
, bio
, i
)
1564 put_page(bvec
->bv_page
);
1570 iomap_dio_zero(struct iomap_dio
*dio
, struct iomap
*iomap
, loff_t pos
,
1573 struct page
*page
= ZERO_PAGE(0);
1574 int flags
= REQ_SYNC
| REQ_IDLE
;
1577 bio
= bio_alloc(GFP_KERNEL
, 1);
1578 bio_set_dev(bio
, iomap
->bdev
);
1579 bio
->bi_iter
.bi_sector
= iomap_sector(iomap
, pos
);
1580 bio
->bi_private
= dio
;
1581 bio
->bi_end_io
= iomap_dio_bio_end_io
;
1583 if (dio
->iocb
->ki_flags
& IOCB_HIPRI
)
1587 __bio_add_page(bio
, page
, len
, 0);
1588 bio_set_op_attrs(bio
, REQ_OP_WRITE
, flags
);
1590 atomic_inc(&dio
->ref
);
1591 return submit_bio(bio
);
1595 iomap_dio_bio_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
1596 struct iomap_dio
*dio
, struct iomap
*iomap
)
1598 unsigned int blkbits
= blksize_bits(bdev_logical_block_size(iomap
->bdev
));
1599 unsigned int fs_block_size
= i_blocksize(inode
), pad
;
1600 unsigned int align
= iov_iter_alignment(dio
->submit
.iter
);
1601 struct iov_iter iter
;
1603 bool need_zeroout
= false;
1604 bool use_fua
= false;
1605 int nr_pages
, ret
= 0;
1608 if ((pos
| length
| align
) & ((1 << blkbits
) - 1))
1611 if (iomap
->type
== IOMAP_UNWRITTEN
) {
1612 dio
->flags
|= IOMAP_DIO_UNWRITTEN
;
1613 need_zeroout
= true;
1616 if (iomap
->flags
& IOMAP_F_SHARED
)
1617 dio
->flags
|= IOMAP_DIO_COW
;
1619 if (iomap
->flags
& IOMAP_F_NEW
) {
1620 need_zeroout
= true;
1621 } else if (iomap
->type
== IOMAP_MAPPED
) {
1623 * Use a FUA write if we need datasync semantics, this is a pure
1624 * data IO that doesn't require any metadata updates (including
1625 * after IO completion such as unwritten extent conversion) and
1626 * the underlying device supports FUA. This allows us to avoid
1627 * cache flushes on IO completion.
1629 if (!(iomap
->flags
& (IOMAP_F_SHARED
|IOMAP_F_DIRTY
)) &&
1630 (dio
->flags
& IOMAP_DIO_WRITE_FUA
) &&
1631 blk_queue_fua(bdev_get_queue(iomap
->bdev
)))
1636 * Operate on a partial iter trimmed to the extent we were called for.
1637 * We'll update the iter in the dio once we're done with this extent.
1639 iter
= *dio
->submit
.iter
;
1640 iov_iter_truncate(&iter
, length
);
1642 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
1647 /* zero out from the start of the block to the write offset */
1648 pad
= pos
& (fs_block_size
- 1);
1650 iomap_dio_zero(dio
, iomap
, pos
- pad
, pad
);
1656 iov_iter_revert(dio
->submit
.iter
, copied
);
1660 bio
= bio_alloc(GFP_KERNEL
, nr_pages
);
1661 bio_set_dev(bio
, iomap
->bdev
);
1662 bio
->bi_iter
.bi_sector
= iomap_sector(iomap
, pos
);
1663 bio
->bi_write_hint
= dio
->iocb
->ki_hint
;
1664 bio
->bi_ioprio
= dio
->iocb
->ki_ioprio
;
1665 bio
->bi_private
= dio
;
1666 bio
->bi_end_io
= iomap_dio_bio_end_io
;
1668 ret
= bio_iov_iter_get_pages(bio
, &iter
);
1669 if (unlikely(ret
)) {
1671 * We have to stop part way through an IO. We must fall
1672 * through to the sub-block tail zeroing here, otherwise
1673 * this short IO may expose stale data in the tail of
1674 * the block we haven't written data to.
1680 n
= bio
->bi_iter
.bi_size
;
1681 if (dio
->flags
& IOMAP_DIO_WRITE
) {
1682 bio
->bi_opf
= REQ_OP_WRITE
| REQ_SYNC
| REQ_IDLE
;
1684 bio
->bi_opf
|= REQ_FUA
;
1686 dio
->flags
&= ~IOMAP_DIO_WRITE_FUA
;
1687 task_io_account_write(n
);
1689 bio
->bi_opf
= REQ_OP_READ
;
1690 if (dio
->flags
& IOMAP_DIO_DIRTY
)
1691 bio_set_pages_dirty(bio
);
1694 if (dio
->iocb
->ki_flags
& IOCB_HIPRI
)
1695 bio
->bi_opf
|= REQ_HIPRI
;
1697 iov_iter_advance(dio
->submit
.iter
, n
);
1703 nr_pages
= iov_iter_npages(&iter
, BIO_MAX_PAGES
);
1705 atomic_inc(&dio
->ref
);
1707 dio
->submit
.last_queue
= bdev_get_queue(iomap
->bdev
);
1708 dio
->submit
.cookie
= submit_bio(bio
);
1712 * We need to zeroout the tail of a sub-block write if the extent type
1713 * requires zeroing or the write extends beyond EOF. If we don't zero
1714 * the block tail in the latter case, we can expose stale data via mmap
1715 * reads of the EOF block.
1719 ((dio
->flags
& IOMAP_DIO_WRITE
) && pos
>= i_size_read(inode
))) {
1720 /* zero out from the end of the write to the end of the block */
1721 pad
= pos
& (fs_block_size
- 1);
1723 iomap_dio_zero(dio
, iomap
, pos
, fs_block_size
- pad
);
1725 return copied
? copied
: ret
;
1729 iomap_dio_hole_actor(loff_t length
, struct iomap_dio
*dio
)
1731 length
= iov_iter_zero(length
, dio
->submit
.iter
);
1732 dio
->size
+= length
;
1737 iomap_dio_inline_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
1738 struct iomap_dio
*dio
, struct iomap
*iomap
)
1740 struct iov_iter
*iter
= dio
->submit
.iter
;
1743 BUG_ON(pos
+ length
> PAGE_SIZE
- offset_in_page(iomap
->inline_data
));
1745 if (dio
->flags
& IOMAP_DIO_WRITE
) {
1746 loff_t size
= inode
->i_size
;
1749 memset(iomap
->inline_data
+ size
, 0, pos
- size
);
1750 copied
= copy_from_iter(iomap
->inline_data
+ pos
, length
, iter
);
1752 if (pos
+ copied
> size
)
1753 i_size_write(inode
, pos
+ copied
);
1754 mark_inode_dirty(inode
);
1757 copied
= copy_to_iter(iomap
->inline_data
+ pos
, length
, iter
);
1759 dio
->size
+= copied
;
1764 iomap_dio_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
1765 void *data
, struct iomap
*iomap
)
1767 struct iomap_dio
*dio
= data
;
1769 switch (iomap
->type
) {
1771 if (WARN_ON_ONCE(dio
->flags
& IOMAP_DIO_WRITE
))
1773 return iomap_dio_hole_actor(length
, dio
);
1774 case IOMAP_UNWRITTEN
:
1775 if (!(dio
->flags
& IOMAP_DIO_WRITE
))
1776 return iomap_dio_hole_actor(length
, dio
);
1777 return iomap_dio_bio_actor(inode
, pos
, length
, dio
, iomap
);
1779 return iomap_dio_bio_actor(inode
, pos
, length
, dio
, iomap
);
1781 return iomap_dio_inline_actor(inode
, pos
, length
, dio
, iomap
);
1789 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
1790 * is being issued as AIO or not. This allows us to optimise pure data writes
1791 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
1792 * REQ_FLUSH post write. This is slightly tricky because a single request here
1793 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
1794 * may be pure data writes. In that case, we still need to do a full data sync
1798 iomap_dio_rw(struct kiocb
*iocb
, struct iov_iter
*iter
,
1799 const struct iomap_ops
*ops
, iomap_dio_end_io_t end_io
)
1801 struct address_space
*mapping
= iocb
->ki_filp
->f_mapping
;
1802 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1803 size_t count
= iov_iter_count(iter
);
1804 loff_t pos
= iocb
->ki_pos
, start
= pos
;
1805 loff_t end
= iocb
->ki_pos
+ count
- 1, ret
= 0;
1806 unsigned int flags
= IOMAP_DIRECT
;
1807 struct blk_plug plug
;
1808 struct iomap_dio
*dio
;
1810 lockdep_assert_held(&inode
->i_rwsem
);
1815 dio
= kmalloc(sizeof(*dio
), GFP_KERNEL
);
1820 atomic_set(&dio
->ref
, 1);
1822 dio
->i_size
= i_size_read(inode
);
1823 dio
->end_io
= end_io
;
1826 dio
->wait_for_completion
= is_sync_kiocb(iocb
);
1828 dio
->submit
.iter
= iter
;
1829 dio
->submit
.waiter
= current
;
1830 dio
->submit
.cookie
= BLK_QC_T_NONE
;
1831 dio
->submit
.last_queue
= NULL
;
1833 if (iov_iter_rw(iter
) == READ
) {
1834 if (pos
>= dio
->i_size
)
1837 if (iter_is_iovec(iter
) && iov_iter_rw(iter
) == READ
)
1838 dio
->flags
|= IOMAP_DIO_DIRTY
;
1840 flags
|= IOMAP_WRITE
;
1841 dio
->flags
|= IOMAP_DIO_WRITE
;
1843 /* for data sync or sync, we need sync completion processing */
1844 if (iocb
->ki_flags
& IOCB_DSYNC
)
1845 dio
->flags
|= IOMAP_DIO_NEED_SYNC
;
1848 * For datasync only writes, we optimistically try using FUA for
1849 * this IO. Any non-FUA write that occurs will clear this flag,
1850 * hence we know before completion whether a cache flush is
1853 if ((iocb
->ki_flags
& (IOCB_DSYNC
| IOCB_SYNC
)) == IOCB_DSYNC
)
1854 dio
->flags
|= IOMAP_DIO_WRITE_FUA
;
1857 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
1858 if (filemap_range_has_page(mapping
, start
, end
)) {
1862 flags
|= IOMAP_NOWAIT
;
1865 ret
= filemap_write_and_wait_range(mapping
, start
, end
);
1870 * Try to invalidate cache pages for the range we're direct
1871 * writing. If this invalidation fails, tough, the write will
1872 * still work, but racing two incompatible write paths is a
1873 * pretty crazy thing to do, so we don't support it 100%.
1875 ret
= invalidate_inode_pages2_range(mapping
,
1876 start
>> PAGE_SHIFT
, end
>> PAGE_SHIFT
);
1878 dio_warn_stale_pagecache(iocb
->ki_filp
);
1881 if (iov_iter_rw(iter
) == WRITE
&& !dio
->wait_for_completion
&&
1882 !inode
->i_sb
->s_dio_done_wq
) {
1883 ret
= sb_init_dio_done_wq(inode
->i_sb
);
1888 inode_dio_begin(inode
);
1890 blk_start_plug(&plug
);
1892 ret
= iomap_apply(inode
, pos
, count
, flags
, ops
, dio
,
1895 /* magic error code to fall back to buffered I/O */
1896 if (ret
== -ENOTBLK
) {
1897 dio
->wait_for_completion
= true;
1904 if (iov_iter_rw(iter
) == READ
&& pos
>= dio
->i_size
)
1906 } while ((count
= iov_iter_count(iter
)) > 0);
1907 blk_finish_plug(&plug
);
1910 iomap_dio_set_error(dio
, ret
);
1913 * If all the writes we issued were FUA, we don't need to flush the
1914 * cache on IO completion. Clear the sync flag for this case.
1916 if (dio
->flags
& IOMAP_DIO_WRITE_FUA
)
1917 dio
->flags
&= ~IOMAP_DIO_NEED_SYNC
;
1919 if (!atomic_dec_and_test(&dio
->ref
)) {
1920 if (!dio
->wait_for_completion
)
1921 return -EIOCBQUEUED
;
1924 set_current_state(TASK_UNINTERRUPTIBLE
);
1925 if (!READ_ONCE(dio
->submit
.waiter
))
1928 if (!(iocb
->ki_flags
& IOCB_HIPRI
) ||
1929 !dio
->submit
.last_queue
||
1930 !blk_poll(dio
->submit
.last_queue
,
1931 dio
->submit
.cookie
, true))
1934 __set_current_state(TASK_RUNNING
);
1937 ret
= iomap_dio_complete(dio
);
1945 EXPORT_SYMBOL_GPL(iomap_dio_rw
);
1947 /* Swapfile activation */
1950 struct iomap_swapfile_info
{
1951 struct iomap iomap
; /* accumulated iomap */
1952 struct swap_info_struct
*sis
;
1953 uint64_t lowest_ppage
; /* lowest physical addr seen (pages) */
1954 uint64_t highest_ppage
; /* highest physical addr seen (pages) */
1955 unsigned long nr_pages
; /* number of pages collected */
1956 int nr_extents
; /* extent count */
1960 * Collect physical extents for this swap file. Physical extents reported to
1961 * the swap code must be trimmed to align to a page boundary. The logical
1962 * offset within the file is irrelevant since the swapfile code maps logical
1963 * page numbers of the swap device to the physical page-aligned extents.
1965 static int iomap_swapfile_add_extent(struct iomap_swapfile_info
*isi
)
1967 struct iomap
*iomap
= &isi
->iomap
;
1968 unsigned long nr_pages
;
1969 uint64_t first_ppage
;
1970 uint64_t first_ppage_reported
;
1971 uint64_t next_ppage
;
1975 * Round the start up and the end down so that the physical
1976 * extent aligns to a page boundary.
1978 first_ppage
= ALIGN(iomap
->addr
, PAGE_SIZE
) >> PAGE_SHIFT
;
1979 next_ppage
= ALIGN_DOWN(iomap
->addr
+ iomap
->length
, PAGE_SIZE
) >>
1982 /* Skip too-short physical extents. */
1983 if (first_ppage
>= next_ppage
)
1985 nr_pages
= next_ppage
- first_ppage
;
1988 * Calculate how much swap space we're adding; the first page contains
1989 * the swap header and doesn't count. The mm still wants that first
1990 * page fed to add_swap_extent, however.
1992 first_ppage_reported
= first_ppage
;
1993 if (iomap
->offset
== 0)
1994 first_ppage_reported
++;
1995 if (isi
->lowest_ppage
> first_ppage_reported
)
1996 isi
->lowest_ppage
= first_ppage_reported
;
1997 if (isi
->highest_ppage
< (next_ppage
- 1))
1998 isi
->highest_ppage
= next_ppage
- 1;
2000 /* Add extent, set up for the next call. */
2001 error
= add_swap_extent(isi
->sis
, isi
->nr_pages
, nr_pages
, first_ppage
);
2004 isi
->nr_extents
+= error
;
2005 isi
->nr_pages
+= nr_pages
;
2010 * Accumulate iomaps for this swap file. We have to accumulate iomaps because
2011 * swap only cares about contiguous page-aligned physical extents and makes no
2012 * distinction between written and unwritten extents.
2014 static loff_t
iomap_swapfile_activate_actor(struct inode
*inode
, loff_t pos
,
2015 loff_t count
, void *data
, struct iomap
*iomap
)
2017 struct iomap_swapfile_info
*isi
= data
;
2020 switch (iomap
->type
) {
2022 case IOMAP_UNWRITTEN
:
2023 /* Only real or unwritten extents. */
2026 /* No inline data. */
2027 pr_err("swapon: file is inline\n");
2030 pr_err("swapon: file has unallocated extents\n");
2034 /* No uncommitted metadata or shared blocks. */
2035 if (iomap
->flags
& IOMAP_F_DIRTY
) {
2036 pr_err("swapon: file is not committed\n");
2039 if (iomap
->flags
& IOMAP_F_SHARED
) {
2040 pr_err("swapon: file has shared extents\n");
2044 /* Only one bdev per swap file. */
2045 if (iomap
->bdev
!= isi
->sis
->bdev
) {
2046 pr_err("swapon: file is on multiple devices\n");
2050 if (isi
->iomap
.length
== 0) {
2051 /* No accumulated extent, so just store it. */
2052 memcpy(&isi
->iomap
, iomap
, sizeof(isi
->iomap
));
2053 } else if (isi
->iomap
.addr
+ isi
->iomap
.length
== iomap
->addr
) {
2054 /* Append this to the accumulated extent. */
2055 isi
->iomap
.length
+= iomap
->length
;
2057 /* Otherwise, add the retained iomap and store this one. */
2058 error
= iomap_swapfile_add_extent(isi
);
2061 memcpy(&isi
->iomap
, iomap
, sizeof(isi
->iomap
));
2067 * Iterate a swap file's iomaps to construct physical extents that can be
2068 * passed to the swapfile subsystem.
2070 int iomap_swapfile_activate(struct swap_info_struct
*sis
,
2071 struct file
*swap_file
, sector_t
*pagespan
,
2072 const struct iomap_ops
*ops
)
2074 struct iomap_swapfile_info isi
= {
2076 .lowest_ppage
= (sector_t
)-1ULL,
2078 struct address_space
*mapping
= swap_file
->f_mapping
;
2079 struct inode
*inode
= mapping
->host
;
2081 loff_t len
= ALIGN_DOWN(i_size_read(inode
), PAGE_SIZE
);
2085 * Persist all file mapping metadata so that we won't have any
2086 * IOMAP_F_DIRTY iomaps.
2088 ret
= vfs_fsync(swap_file
, 1);
2093 ret
= iomap_apply(inode
, pos
, len
, IOMAP_REPORT
,
2094 ops
, &isi
, iomap_swapfile_activate_actor
);
2102 if (isi
.iomap
.length
) {
2103 ret
= iomap_swapfile_add_extent(&isi
);
2108 *pagespan
= 1 + isi
.highest_ppage
- isi
.lowest_ppage
;
2109 sis
->max
= isi
.nr_pages
;
2110 sis
->pages
= isi
.nr_pages
- 1;
2111 sis
->highest_bit
= isi
.nr_pages
- 1;
2112 return isi
.nr_extents
;
2114 EXPORT_SYMBOL_GPL(iomap_swapfile_activate
);
2115 #endif /* CONFIG_SWAP */
2118 iomap_bmap_actor(struct inode
*inode
, loff_t pos
, loff_t length
,
2119 void *data
, struct iomap
*iomap
)
2121 sector_t
*bno
= data
, addr
;
2123 if (iomap
->type
== IOMAP_MAPPED
) {
2124 addr
= (pos
- iomap
->offset
+ iomap
->addr
) >> inode
->i_blkbits
;
2126 WARN(1, "would truncate bmap result\n");
2133 /* legacy ->bmap interface. 0 is the error return (!) */
2135 iomap_bmap(struct address_space
*mapping
, sector_t bno
,
2136 const struct iomap_ops
*ops
)
2138 struct inode
*inode
= mapping
->host
;
2139 loff_t pos
= bno
<< inode
->i_blkbits
;
2140 unsigned blocksize
= i_blocksize(inode
);
2142 if (filemap_write_and_wait(mapping
))
2146 iomap_apply(inode
, pos
, blocksize
, 0, ops
, &bno
, iomap_bmap_actor
);
2149 EXPORT_SYMBOL_GPL(iomap_bmap
);