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>
33 #include <linux/swap.h>
38 * Execute a iomap write on a segment of the mapping that spans a
39 * contiguous range of pages that have identical block mapping state.
41 * This avoids the need to map pages individually, do individual allocations
42 * for each page and most importantly avoid the need for filesystem specific
43 * locking per page. Instead, all the operations are amortised over the entire
44 * range of pages. It is assumed that the filesystems will lock whatever
45 * resources they require in the iomap_begin call, and release them in the
49 iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,
50 const struct iomap_ops *ops, void *data, iomap_actor_t actor)
52 struct iomap iomap = { 0 };
53 loff_t written = 0, ret;
56 * Need to map a range from start position for length bytes. This can
57 * span multiple pages - it is only guaranteed to return a range of a
58 * single type of pages (e.g. all into a hole, all mapped or all
59 * unwritten). Failure at this point has nothing to undo.
61 * If allocation is required for this range, reserve the space now so
62 * that the allocation is guaranteed to succeed later on. Once we copy
63 * the data into the page cache pages, then we cannot fail otherwise we
64 * expose transient stale data. If the reserve fails, we can safely
65 * back out at this point as there is nothing to undo.
67 ret = ops->iomap_begin(inode, pos, length, flags, &iomap);
70 if (WARN_ON(iomap.offset > pos))
72 if (WARN_ON(iomap.length == 0))
76 * Cut down the length to the one actually provided by the filesystem,
77 * as it might not be able to give us the whole size that we requested.
79 if (iomap.offset + iomap.length < pos + length)
80 length = iomap.offset + iomap.length - pos;
83 * Now that we have guaranteed that the space allocation will succeed.
84 * we can do the copy-in page by page without having to worry about
85 * failures exposing transient data.
87 written = actor(inode, pos, length, data, &iomap);
90 * Now the data has been copied, commit the range we've copied. This
91 * should not fail unless the filesystem has had a fatal error.
94 ret = ops->iomap_end(inode, pos, length,
95 written > 0 ? written : 0,
99 return written ? written : ret;
103 iomap_sector(struct iomap *iomap, loff_t pos)
105 return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
108 static struct iomap_page *
109 iomap_page_create(struct inode *inode, struct page *page)
111 struct iomap_page *iop = to_iomap_page(page);
113 if (iop || i_blocksize(inode) == PAGE_SIZE)
116 iop = kmalloc(sizeof(*iop), GFP_NOFS | __GFP_NOFAIL);
117 atomic_set(&iop->read_count, 0);
118 atomic_set(&iop->write_count, 0);
119 bitmap_zero(iop->uptodate, PAGE_SIZE / SECTOR_SIZE);
120 set_page_private(page, (unsigned long)iop);
121 SetPagePrivate(page);
126 iomap_page_release(struct page *page)
128 struct iomap_page *iop = to_iomap_page(page);
132 WARN_ON_ONCE(atomic_read(&iop->read_count));
133 WARN_ON_ONCE(atomic_read(&iop->write_count));
134 ClearPagePrivate(page);
135 set_page_private(page, 0);
140 * Calculate the range inside the page that we actually need to read.
143 iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
144 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
146 unsigned block_bits = inode->i_blkbits;
147 unsigned block_size = (1 << block_bits);
148 unsigned poff = offset_in_page(*pos);
149 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
150 unsigned first = poff >> block_bits;
151 unsigned last = (poff + plen - 1) >> block_bits;
152 unsigned end = offset_in_page(i_size_read(inode)) >> 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 (first <= end && last > end)
188 plen -= (last - end) * block_size;
195 iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
197 struct iomap_page *iop = to_iomap_page(page);
198 struct inode *inode = page->mapping->host;
199 unsigned first = off >> inode->i_blkbits;
200 unsigned last = (off + len - 1) >> inode->i_blkbits;
202 bool uptodate = true;
205 for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
206 if (i >= first && i <= last)
207 set_bit(i, iop->uptodate);
208 else if (!test_bit(i, iop->uptodate))
213 if (uptodate && !PageError(page))
214 SetPageUptodate(page);
218 iomap_read_finish(struct iomap_page *iop, struct page *page)
220 if (!iop || atomic_dec_and_test(&iop->read_count))
225 iomap_read_page_end_io(struct bio_vec *bvec, int error)
227 struct page *page = bvec->bv_page;
228 struct iomap_page *iop = to_iomap_page(page);
230 if (unlikely(error)) {
231 ClearPageUptodate(page);
234 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
237 iomap_read_finish(iop, page);
241 iomap_read_inline_data(struct inode *inode, struct page *page,
244 size_t size = i_size_read(inode);
247 if (PageUptodate(page))
251 BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
253 addr = kmap_atomic(page);
254 memcpy(addr, iomap->inline_data, size);
255 memset(addr + size, 0, PAGE_SIZE - size);
257 SetPageUptodate(page);
261 iomap_read_end_io(struct bio *bio)
263 int error = blk_status_to_errno(bio->bi_status);
264 struct bio_vec *bvec;
267 bio_for_each_segment_all(bvec, bio, i)
268 iomap_read_page_end_io(bvec, error);
272 struct iomap_readpage_ctx {
273 struct page *cur_page;
274 bool cur_page_in_bio;
277 struct list_head *pages;
281 iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
284 struct iomap_readpage_ctx *ctx = data;
285 struct page *page = ctx->cur_page;
286 struct iomap_page *iop = iomap_page_create(inode, page);
287 bool is_contig = false;
288 loff_t orig_pos = pos;
292 if (iomap->type == IOMAP_INLINE) {
294 iomap_read_inline_data(inode, page, iomap);
298 /* zero post-eof blocks as the page may be mapped */
299 iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
303 if (iomap->type != IOMAP_MAPPED || pos >= i_size_read(inode)) {
304 zero_user(page, poff, plen);
305 iomap_set_range_uptodate(page, poff, plen);
309 ctx->cur_page_in_bio = true;
312 * Try to merge into a previous segment if we can.
314 sector = iomap_sector(iomap, pos);
315 if (ctx->bio && bio_end_sector(ctx->bio) == sector) {
316 if (__bio_try_merge_page(ctx->bio, page, plen, poff))
322 * If we start a new segment we need to increase the read count, and we
323 * need to do so before submitting any previous full bio to make sure
324 * that we don't prematurely unlock the page.
327 atomic_inc(&iop->read_count);
329 if (!ctx->bio || !is_contig || bio_full(ctx->bio)) {
330 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
331 int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
334 submit_bio(ctx->bio);
336 if (ctx->is_readahead) /* same as readahead_gfp_mask */
337 gfp |= __GFP_NORETRY | __GFP_NOWARN;
338 ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
339 ctx->bio->bi_opf = REQ_OP_READ;
340 if (ctx->is_readahead)
341 ctx->bio->bi_opf |= REQ_RAHEAD;
342 ctx->bio->bi_iter.bi_sector = sector;
343 bio_set_dev(ctx->bio, iomap->bdev);
344 ctx->bio->bi_end_io = iomap_read_end_io;
347 __bio_add_page(ctx->bio, page, plen, poff);
350 * Move the caller beyond our range so that it keeps making progress.
351 * For that we have to include any leading non-uptodate ranges, but
352 * we can skip trailing ones as they will be handled in the next
355 return pos - orig_pos + plen;
359 iomap_readpage(struct page *page, const struct iomap_ops *ops)
361 struct iomap_readpage_ctx ctx = { .cur_page = page };
362 struct inode *inode = page->mapping->host;
366 for (poff = 0; poff < PAGE_SIZE; poff += ret) {
367 ret = iomap_apply(inode, page_offset(page) + poff,
368 PAGE_SIZE - poff, 0, ops, &ctx,
369 iomap_readpage_actor);
371 WARN_ON_ONCE(ret == 0);
379 WARN_ON_ONCE(!ctx.cur_page_in_bio);
381 WARN_ON_ONCE(ctx.cur_page_in_bio);
386 * Just like mpage_readpages and block_read_full_page we always
387 * return 0 and just mark the page as PageError on errors. This
388 * should be cleaned up all through the stack eventually.
392 EXPORT_SYMBOL_GPL(iomap_readpage);
395 iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
396 loff_t length, loff_t *done)
398 while (!list_empty(pages)) {
399 struct page *page = lru_to_page(pages);
401 if (page_offset(page) >= (u64)pos + length)
404 list_del(&page->lru);
405 if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
410 * If we already have a page in the page cache at index we are
411 * done. Upper layers don't care if it is uptodate after the
412 * readpages call itself as every page gets checked again once
423 iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
424 void *data, struct iomap *iomap)
426 struct iomap_readpage_ctx *ctx = data;
429 for (done = 0; done < length; done += ret) {
430 if (ctx->cur_page && offset_in_page(pos + done) == 0) {
431 if (!ctx->cur_page_in_bio)
432 unlock_page(ctx->cur_page);
433 put_page(ctx->cur_page);
434 ctx->cur_page = NULL;
436 if (!ctx->cur_page) {
437 ctx->cur_page = iomap_next_page(inode, ctx->pages,
441 ctx->cur_page_in_bio = false;
443 ret = iomap_readpage_actor(inode, pos + done, length - done,
451 iomap_readpages(struct address_space *mapping, struct list_head *pages,
452 unsigned nr_pages, const struct iomap_ops *ops)
454 struct iomap_readpage_ctx ctx = {
456 .is_readahead = true,
458 loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
459 loff_t last = page_offset(list_entry(pages->next, struct page, lru));
460 loff_t length = last - pos + PAGE_SIZE, ret = 0;
463 ret = iomap_apply(mapping->host, pos, length, 0, ops,
464 &ctx, iomap_readpages_actor);
466 WARN_ON_ONCE(ret == 0);
477 if (!ctx.cur_page_in_bio)
478 unlock_page(ctx.cur_page);
479 put_page(ctx.cur_page);
483 * Check that we didn't lose a page due to the arcance calling
486 WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
489 EXPORT_SYMBOL_GPL(iomap_readpages);
492 iomap_is_partially_uptodate(struct page *page, unsigned long from,
495 struct iomap_page *iop = to_iomap_page(page);
496 struct inode *inode = page->mapping->host;
497 unsigned first = from >> inode->i_blkbits;
498 unsigned last = (from + count - 1) >> inode->i_blkbits;
502 for (i = first; i <= last; i++)
503 if (!test_bit(i, iop->uptodate))
510 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
513 iomap_releasepage(struct page *page, gfp_t gfp_mask)
516 * mm accommodates an old ext3 case where clean pages might not have had
517 * the dirty bit cleared. Thus, it can send actual dirty pages to
518 * ->releasepage() via shrink_active_list(), skip those here.
520 if (PageDirty(page) || PageWriteback(page))
522 iomap_page_release(page);
525 EXPORT_SYMBOL_GPL(iomap_releasepage);
528 iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
531 * If we are invalidating the entire page, clear the dirty state from it
532 * and release it to avoid unnecessary buildup of the LRU.
534 if (offset == 0 && len == PAGE_SIZE) {
535 WARN_ON_ONCE(PageWriteback(page));
536 cancel_dirty_page(page);
537 iomap_page_release(page);
540 EXPORT_SYMBOL_GPL(iomap_invalidatepage);
542 #ifdef CONFIG_MIGRATION
544 iomap_migrate_page(struct address_space *mapping, struct page *newpage,
545 struct page *page, enum migrate_mode mode)
549 ret = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
550 if (ret != MIGRATEPAGE_SUCCESS)
553 if (page_has_private(page)) {
554 ClearPagePrivate(page);
555 set_page_private(newpage, page_private(page));
556 set_page_private(page, 0);
557 SetPagePrivate(newpage);
560 if (mode != MIGRATE_SYNC_NO_COPY)
561 migrate_page_copy(newpage, page);
563 migrate_page_states(newpage, page);
564 return MIGRATEPAGE_SUCCESS;
566 EXPORT_SYMBOL_GPL(iomap_migrate_page);
567 #endif /* CONFIG_MIGRATION */
570 iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
572 loff_t i_size = i_size_read(inode);
575 * Only truncate newly allocated pages beyoned EOF, even if the
576 * write started inside the existing inode size.
578 if (pos + len > i_size)
579 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
583 iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
584 unsigned poff, unsigned plen, unsigned from, unsigned to,
590 if (iomap->type != IOMAP_MAPPED || block_start >= i_size_read(inode)) {
591 zero_user_segments(page, poff, from, to, poff + plen);
592 iomap_set_range_uptodate(page, poff, plen);
596 bio_init(&bio, &bvec, 1);
597 bio.bi_opf = REQ_OP_READ;
598 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
599 bio_set_dev(&bio, iomap->bdev);
600 __bio_add_page(&bio, page, plen, poff);
601 return submit_bio_wait(&bio);
605 __iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
606 struct page *page, struct iomap *iomap)
608 struct iomap_page *iop = iomap_page_create(inode, page);
609 loff_t block_size = i_blocksize(inode);
610 loff_t block_start = pos & ~(block_size - 1);
611 loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
612 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
615 if (PageUptodate(page))
619 iomap_adjust_read_range(inode, iop, &block_start,
620 block_end - block_start, &poff, &plen);
624 if ((from > poff && from < poff + plen) ||
625 (to > poff && to < poff + plen)) {
626 status = iomap_read_page_sync(inode, block_start, page,
627 poff, plen, from, to, iomap);
632 } while ((block_start += plen) < block_end);
638 iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
639 struct page **pagep, struct iomap *iomap)
641 pgoff_t index = pos >> PAGE_SHIFT;
645 BUG_ON(pos + len > iomap->offset + iomap->length);
647 if (fatal_signal_pending(current))
650 page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
654 if (iomap->type == IOMAP_INLINE)
655 iomap_read_inline_data(inode, page, iomap);
656 else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
657 status = __block_write_begin_int(page, pos, len, NULL, iomap);
659 status = __iomap_write_begin(inode, pos, len, page, iomap);
660 if (unlikely(status)) {
665 iomap_write_failed(inode, pos, len);
673 iomap_set_page_dirty(struct page *page)
675 struct address_space *mapping = page_mapping(page);
678 if (unlikely(!mapping))
679 return !TestSetPageDirty(page);
682 * Lock out page->mem_cgroup migration to keep PageDirty
683 * synchronized with per-memcg dirty page counters.
685 lock_page_memcg(page);
686 newly_dirty = !TestSetPageDirty(page);
688 __set_page_dirty(page, mapping, 0);
689 unlock_page_memcg(page);
692 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
695 EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
698 __iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
699 unsigned copied, struct page *page, struct iomap *iomap)
701 flush_dcache_page(page);
704 * The blocks that were entirely written will now be uptodate, so we
705 * don't have to worry about a readpage reading them and overwriting a
706 * partial write. However if we have encountered a short write and only
707 * partially written into a block, it will not be marked uptodate, so a
708 * readpage might come in and destroy our partial write.
710 * Do the simplest thing, and just treat any short write to a non
711 * uptodate page as a zero-length write, and force the caller to redo
714 if (unlikely(copied < len && !PageUptodate(page))) {
717 iomap_set_range_uptodate(page, offset_in_page(pos), len);
718 iomap_set_page_dirty(page);
720 return __generic_write_end(inode, pos, copied, page);
724 iomap_write_end_inline(struct inode *inode, struct page *page,
725 struct iomap *iomap, loff_t pos, unsigned copied)
729 WARN_ON_ONCE(!PageUptodate(page));
730 BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
732 addr = kmap_atomic(page);
733 memcpy(iomap->inline_data + pos, addr + pos, copied);
736 mark_inode_dirty(inode);
737 __generic_write_end(inode, pos, copied, page);
742 iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
743 unsigned copied, struct page *page, struct iomap *iomap)
747 if (iomap->type == IOMAP_INLINE) {
748 ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
749 } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
750 ret = generic_write_end(NULL, inode->i_mapping, pos, len,
753 ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
756 if (iomap->page_done)
757 iomap->page_done(inode, pos, copied, page, iomap);
760 iomap_write_failed(inode, pos, len);
765 iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
768 struct iov_iter *i = data;
771 unsigned int flags = AOP_FLAG_NOFS;
775 unsigned long offset; /* Offset into pagecache page */
776 unsigned long bytes; /* Bytes to write to page */
777 size_t copied; /* Bytes copied from user */
779 offset = offset_in_page(pos);
780 bytes = min_t(unsigned long, PAGE_SIZE - offset,
787 * Bring in the user page that we will copy from _first_.
788 * Otherwise there's a nasty deadlock on copying from the
789 * same page as we're writing to, without it being marked
792 * Not only is this an optimisation, but it is also required
793 * to check that the address is actually valid, when atomic
794 * usercopies are used, below.
796 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
801 status = iomap_write_begin(inode, pos, bytes, flags, &page,
803 if (unlikely(status))
806 if (mapping_writably_mapped(inode->i_mapping))
807 flush_dcache_page(page);
809 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
811 flush_dcache_page(page);
813 status = iomap_write_end(inode, pos, bytes, copied, page,
815 if (unlikely(status < 0))
821 iov_iter_advance(i, copied);
822 if (unlikely(copied == 0)) {
824 * If we were unable to copy any data at all, we must
825 * fall back to a single segment length write.
827 * If we didn't fallback here, we could livelock
828 * because not all segments in the iov can be copied at
829 * once without a pagefault.
831 bytes = min_t(unsigned long, PAGE_SIZE - offset,
832 iov_iter_single_seg_count(i));
839 balance_dirty_pages_ratelimited(inode->i_mapping);
840 } while (iov_iter_count(i) && length);
842 return written ? written : status;
846 iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
847 const struct iomap_ops *ops)
849 struct inode *inode = iocb->ki_filp->f_mapping->host;
850 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
852 while (iov_iter_count(iter)) {
853 ret = iomap_apply(inode, pos, iov_iter_count(iter),
854 IOMAP_WRITE, ops, iter, iomap_write_actor);
861 return written ? written : ret;
863 EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
866 __iomap_read_page(struct inode *inode, loff_t offset)
868 struct address_space *mapping = inode->i_mapping;
871 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
874 if (!PageUptodate(page)) {
876 return ERR_PTR(-EIO);
882 iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
889 struct page *page, *rpage;
890 unsigned long offset; /* Offset into pagecache page */
891 unsigned long bytes; /* Bytes to write to page */
893 offset = offset_in_page(pos);
894 bytes = min_t(loff_t, PAGE_SIZE - offset, length);
896 rpage = __iomap_read_page(inode, pos);
898 return PTR_ERR(rpage);
900 status = iomap_write_begin(inode, pos, bytes,
901 AOP_FLAG_NOFS, &page, iomap);
903 if (unlikely(status))
906 WARN_ON_ONCE(!PageUptodate(page));
908 status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
909 if (unlikely(status <= 0)) {
910 if (WARN_ON_ONCE(status == 0))
921 balance_dirty_pages_ratelimited(inode->i_mapping);
928 iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
929 const struct iomap_ops *ops)
934 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
944 EXPORT_SYMBOL_GPL(iomap_file_dirty);
946 static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
947 unsigned bytes, struct iomap *iomap)
952 status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
957 zero_user(page, offset, bytes);
958 mark_page_accessed(page);
960 return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
963 static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
966 return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
967 iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
971 iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
972 void *data, struct iomap *iomap)
974 bool *did_zero = data;
978 /* already zeroed? we're done. */
979 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
983 unsigned offset, bytes;
985 offset = offset_in_page(pos);
986 bytes = min_t(loff_t, PAGE_SIZE - offset, count);
989 status = iomap_dax_zero(pos, offset, bytes, iomap);
991 status = iomap_zero(inode, pos, offset, bytes, iomap);
1000 } while (count > 0);
1006 iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1007 const struct iomap_ops *ops)
1012 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
1013 ops, did_zero, iomap_zero_range_actor);
1023 EXPORT_SYMBOL_GPL(iomap_zero_range);
1026 iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1027 const struct iomap_ops *ops)
1029 unsigned int blocksize = i_blocksize(inode);
1030 unsigned int off = pos & (blocksize - 1);
1032 /* Block boundary? Nothing to do */
1035 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1037 EXPORT_SYMBOL_GPL(iomap_truncate_page);
1040 iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
1041 void *data, struct iomap *iomap)
1043 struct page *page = data;
1046 if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
1047 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
1050 block_commit_write(page, 0, length);
1052 WARN_ON_ONCE(!PageUptodate(page));
1053 iomap_page_create(inode, page);
1054 set_page_dirty(page);
1060 int iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1062 struct page *page = vmf->page;
1063 struct inode *inode = file_inode(vmf->vma->vm_file);
1064 unsigned long length;
1065 loff_t offset, size;
1069 size = i_size_read(inode);
1070 if ((page->mapping != inode->i_mapping) ||
1071 (page_offset(page) > size)) {
1072 /* We overload EFAULT to mean page got truncated */
1077 /* page is wholly or partially inside EOF */
1078 if (((page->index + 1) << PAGE_SHIFT) > size)
1079 length = offset_in_page(size);
1083 offset = page_offset(page);
1084 while (length > 0) {
1085 ret = iomap_apply(inode, offset, length,
1086 IOMAP_WRITE | IOMAP_FAULT, ops, page,
1087 iomap_page_mkwrite_actor);
1088 if (unlikely(ret <= 0))
1094 wait_for_stable_page(page);
1095 return VM_FAULT_LOCKED;
1098 return block_page_mkwrite_return(ret);
1100 EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1103 struct fiemap_extent_info *fi;
1107 static int iomap_to_fiemap(struct fiemap_extent_info *fi,
1108 struct iomap *iomap, u32 flags)
1110 switch (iomap->type) {
1114 case IOMAP_DELALLOC:
1115 flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
1119 case IOMAP_UNWRITTEN:
1120 flags |= FIEMAP_EXTENT_UNWRITTEN;
1123 flags |= FIEMAP_EXTENT_DATA_INLINE;
1127 if (iomap->flags & IOMAP_F_MERGED)
1128 flags |= FIEMAP_EXTENT_MERGED;
1129 if (iomap->flags & IOMAP_F_SHARED)
1130 flags |= FIEMAP_EXTENT_SHARED;
1132 return fiemap_fill_next_extent(fi, iomap->offset,
1133 iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
1134 iomap->length, flags);
1138 iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1139 struct iomap *iomap)
1141 struct fiemap_ctx *ctx = data;
1142 loff_t ret = length;
1144 if (iomap->type == IOMAP_HOLE)
1147 ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
1150 case 0: /* success */
1152 case 1: /* extent array full */
1159 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
1160 loff_t start, loff_t len, const struct iomap_ops *ops)
1162 struct fiemap_ctx ctx;
1165 memset(&ctx, 0, sizeof(ctx));
1167 ctx.prev.type = IOMAP_HOLE;
1169 ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
1173 if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
1174 ret = filemap_write_and_wait(inode->i_mapping);
1180 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
1181 iomap_fiemap_actor);
1182 /* inode with no (attribute) mapping will give ENOENT */
1194 if (ctx.prev.type != IOMAP_HOLE) {
1195 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
1202 EXPORT_SYMBOL_GPL(iomap_fiemap);
1205 * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
1206 * Returns true if found and updates @lastoff to the offset in file.
1209 page_seek_hole_data(struct inode *inode, struct page *page, loff_t *lastoff,
1212 const struct address_space_operations *ops = inode->i_mapping->a_ops;
1213 unsigned int bsize = i_blocksize(inode), off;
1214 bool seek_data = whence == SEEK_DATA;
1215 loff_t poff = page_offset(page);
1217 if (WARN_ON_ONCE(*lastoff >= poff + PAGE_SIZE))
1220 if (*lastoff < poff) {
1222 * Last offset smaller than the start of the page means we found
1225 if (whence == SEEK_HOLE)
1231 * Just check the page unless we can and should check block ranges:
1233 if (bsize == PAGE_SIZE || !ops->is_partially_uptodate)
1234 return PageUptodate(page) == seek_data;
1237 if (unlikely(page->mapping != inode->i_mapping))
1238 goto out_unlock_not_found;
1240 for (off = 0; off < PAGE_SIZE; off += bsize) {
1241 if (offset_in_page(*lastoff) >= off + bsize)
1243 if (ops->is_partially_uptodate(page, off, bsize) == seek_data) {
1247 *lastoff = poff + off + bsize;
1250 out_unlock_not_found:
1256 * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
1258 * Within unwritten extents, the page cache determines which parts are holes
1259 * and which are data: uptodate buffer heads count as data; everything else
1262 * Returns the resulting offset on successs, and -ENOENT otherwise.
1265 page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,
1268 pgoff_t index = offset >> PAGE_SHIFT;
1269 pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1270 loff_t lastoff = offset;
1271 struct pagevec pvec;
1276 pagevec_init(&pvec);
1279 unsigned nr_pages, i;
1281 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index,
1286 for (i = 0; i < nr_pages; i++) {
1287 struct page *page = pvec.pages[i];
1289 if (page_seek_hole_data(inode, page, &lastoff, whence))
1291 lastoff = page_offset(page) + PAGE_SIZE;
1293 pagevec_release(&pvec);
1294 } while (index < end);
1296 /* When no page at lastoff and we are not done, we found a hole. */
1297 if (whence != SEEK_HOLE)
1301 if (lastoff < offset + length)
1306 pagevec_release(&pvec);
1312 iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length,
1313 void *data, struct iomap *iomap)
1315 switch (iomap->type) {
1316 case IOMAP_UNWRITTEN:
1317 offset = page_cache_seek_hole_data(inode, offset, length,
1323 *(loff_t *)data = offset;
1331 iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
1333 loff_t size = i_size_read(inode);
1334 loff_t length = size - offset;
1337 /* Nothing to be found before or beyond the end of the file. */
1338 if (offset < 0 || offset >= size)
1341 while (length > 0) {
1342 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
1343 &offset, iomap_seek_hole_actor);
1355 EXPORT_SYMBOL_GPL(iomap_seek_hole);
1358 iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length,
1359 void *data, struct iomap *iomap)
1361 switch (iomap->type) {
1364 case IOMAP_UNWRITTEN:
1365 offset = page_cache_seek_hole_data(inode, offset, length,
1371 *(loff_t *)data = offset;
1377 iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
1379 loff_t size = i_size_read(inode);
1380 loff_t length = size - offset;
1383 /* Nothing to be found before or beyond the end of the file. */
1384 if (offset < 0 || offset >= size)
1387 while (length > 0) {
1388 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
1389 &offset, iomap_seek_data_actor);
1403 EXPORT_SYMBOL_GPL(iomap_seek_data);
1406 * Private flags for iomap_dio, must not overlap with the public ones in
1409 #define IOMAP_DIO_WRITE_FUA (1 << 28)
1410 #define IOMAP_DIO_NEED_SYNC (1 << 29)
1411 #define IOMAP_DIO_WRITE (1 << 30)
1412 #define IOMAP_DIO_DIRTY (1 << 31)
1416 iomap_dio_end_io_t *end_io;
1422 bool wait_for_completion;
1425 /* used during submission and for synchronous completion: */
1427 struct iov_iter *iter;
1428 struct task_struct *waiter;
1429 struct request_queue *last_queue;
1433 /* used for aio completion: */
1435 struct work_struct work;
1440 static ssize_t iomap_dio_complete(struct iomap_dio *dio)
1442 struct kiocb *iocb = dio->iocb;
1443 struct inode *inode = file_inode(iocb->ki_filp);
1444 loff_t offset = iocb->ki_pos;
1448 ret = dio->end_io(iocb,
1449 dio->error ? dio->error : dio->size,
1457 /* check for short read */
1458 if (offset + ret > dio->i_size &&
1459 !(dio->flags & IOMAP_DIO_WRITE))
1460 ret = dio->i_size - offset;
1461 iocb->ki_pos += ret;
1465 * Try again to invalidate clean pages which might have been cached by
1466 * non-direct readahead, or faulted in by get_user_pages() if the source
1467 * of the write was an mmap'ed region of the file we're writing. Either
1468 * one is a pretty crazy thing to do, so we don't support it 100%. If
1469 * this invalidation fails, tough, the write still worked...
1471 * And this page cache invalidation has to be after dio->end_io(), as
1472 * some filesystems convert unwritten extents to real allocations in
1473 * end_io() when necessary, otherwise a racing buffer read would cache
1474 * zeros from unwritten extents.
1477 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
1479 err = invalidate_inode_pages2_range(inode->i_mapping,
1480 offset >> PAGE_SHIFT,
1481 (offset + dio->size - 1) >> PAGE_SHIFT);
1483 dio_warn_stale_pagecache(iocb->ki_filp);
1487 * If this is a DSYNC write, make sure we push it to stable storage now
1488 * that we've written data.
1490 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
1491 ret = generic_write_sync(iocb, ret);
1493 inode_dio_end(file_inode(iocb->ki_filp));
1499 static void iomap_dio_complete_work(struct work_struct *work)
1501 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
1502 struct kiocb *iocb = dio->iocb;
1504 iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
1508 * Set an error in the dio if none is set yet. We have to use cmpxchg
1509 * as the submission context and the completion context(s) can race to
1512 static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
1514 cmpxchg(&dio->error, 0, ret);
1517 static void iomap_dio_bio_end_io(struct bio *bio)
1519 struct iomap_dio *dio = bio->bi_private;
1520 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
1523 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
1525 if (atomic_dec_and_test(&dio->ref)) {
1526 if (dio->wait_for_completion) {
1527 struct task_struct *waiter = dio->submit.waiter;
1528 WRITE_ONCE(dio->submit.waiter, NULL);
1529 wake_up_process(waiter);
1530 } else if (dio->flags & IOMAP_DIO_WRITE) {
1531 struct inode *inode = file_inode(dio->iocb->ki_filp);
1533 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
1534 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
1536 iomap_dio_complete_work(&dio->aio.work);
1541 bio_check_pages_dirty(bio);
1543 struct bio_vec *bvec;
1546 bio_for_each_segment_all(bvec, bio, i)
1547 put_page(bvec->bv_page);
1553 iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
1556 struct page *page = ZERO_PAGE(0);
1559 bio = bio_alloc(GFP_KERNEL, 1);
1560 bio_set_dev(bio, iomap->bdev);
1561 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
1562 bio->bi_private = dio;
1563 bio->bi_end_io = iomap_dio_bio_end_io;
1566 __bio_add_page(bio, page, len, 0);
1567 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE);
1569 atomic_inc(&dio->ref);
1570 return submit_bio(bio);
1574 iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
1575 struct iomap_dio *dio, struct iomap *iomap)
1577 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
1578 unsigned int fs_block_size = i_blocksize(inode), pad;
1579 unsigned int align = iov_iter_alignment(dio->submit.iter);
1580 struct iov_iter iter;
1582 bool need_zeroout = false;
1583 bool use_fua = false;
1587 if ((pos | length | align) & ((1 << blkbits) - 1))
1590 if (iomap->type == IOMAP_UNWRITTEN) {
1591 dio->flags |= IOMAP_DIO_UNWRITTEN;
1592 need_zeroout = true;
1595 if (iomap->flags & IOMAP_F_SHARED)
1596 dio->flags |= IOMAP_DIO_COW;
1598 if (iomap->flags & IOMAP_F_NEW) {
1599 need_zeroout = true;
1602 * Use a FUA write if we need datasync semantics, this
1603 * is a pure data IO that doesn't require any metadata
1604 * updates and the underlying device supports FUA. This
1605 * allows us to avoid cache flushes on IO completion.
1607 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
1608 (dio->flags & IOMAP_DIO_WRITE_FUA) &&
1609 blk_queue_fua(bdev_get_queue(iomap->bdev)))
1614 * Operate on a partial iter trimmed to the extent we were called for.
1615 * We'll update the iter in the dio once we're done with this extent.
1617 iter = *dio->submit.iter;
1618 iov_iter_truncate(&iter, length);
1620 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
1625 /* zero out from the start of the block to the write offset */
1626 pad = pos & (fs_block_size - 1);
1628 iomap_dio_zero(dio, iomap, pos - pad, pad);
1634 iov_iter_revert(dio->submit.iter, copied);
1638 bio = bio_alloc(GFP_KERNEL, nr_pages);
1639 bio_set_dev(bio, iomap->bdev);
1640 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
1641 bio->bi_write_hint = dio->iocb->ki_hint;
1642 bio->bi_ioprio = dio->iocb->ki_ioprio;
1643 bio->bi_private = dio;
1644 bio->bi_end_io = iomap_dio_bio_end_io;
1646 ret = bio_iov_iter_get_pages(bio, &iter);
1647 if (unlikely(ret)) {
1649 return copied ? copied : ret;
1652 n = bio->bi_iter.bi_size;
1653 if (dio->flags & IOMAP_DIO_WRITE) {
1654 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
1656 bio->bi_opf |= REQ_FUA;
1658 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
1659 task_io_account_write(n);
1661 bio->bi_opf = REQ_OP_READ;
1662 if (dio->flags & IOMAP_DIO_DIRTY)
1663 bio_set_pages_dirty(bio);
1666 iov_iter_advance(dio->submit.iter, n);
1672 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
1674 atomic_inc(&dio->ref);
1676 dio->submit.last_queue = bdev_get_queue(iomap->bdev);
1677 dio->submit.cookie = submit_bio(bio);
1681 /* zero out from the end of the write to the end of the block */
1682 pad = pos & (fs_block_size - 1);
1684 iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
1690 iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
1692 length = iov_iter_zero(length, dio->submit.iter);
1693 dio->size += length;
1698 iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
1699 struct iomap_dio *dio, struct iomap *iomap)
1701 struct iov_iter *iter = dio->submit.iter;
1704 BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
1706 if (dio->flags & IOMAP_DIO_WRITE) {
1707 loff_t size = inode->i_size;
1710 memset(iomap->inline_data + size, 0, pos - size);
1711 copied = copy_from_iter(iomap->inline_data + pos, length, iter);
1713 if (pos + copied > size)
1714 i_size_write(inode, pos + copied);
1715 mark_inode_dirty(inode);
1718 copied = copy_to_iter(iomap->inline_data + pos, length, iter);
1720 dio->size += copied;
1725 iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
1726 void *data, struct iomap *iomap)
1728 struct iomap_dio *dio = data;
1730 switch (iomap->type) {
1732 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
1734 return iomap_dio_hole_actor(length, dio);
1735 case IOMAP_UNWRITTEN:
1736 if (!(dio->flags & IOMAP_DIO_WRITE))
1737 return iomap_dio_hole_actor(length, dio);
1738 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
1740 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
1742 return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
1750 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
1751 * is being issued as AIO or not. This allows us to optimise pure data writes
1752 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
1753 * REQ_FLUSH post write. This is slightly tricky because a single request here
1754 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
1755 * may be pure data writes. In that case, we still need to do a full data sync
1759 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
1760 const struct iomap_ops *ops, iomap_dio_end_io_t end_io)
1762 struct address_space *mapping = iocb->ki_filp->f_mapping;
1763 struct inode *inode = file_inode(iocb->ki_filp);
1764 size_t count = iov_iter_count(iter);
1765 loff_t pos = iocb->ki_pos, start = pos;
1766 loff_t end = iocb->ki_pos + count - 1, ret = 0;
1767 unsigned int flags = IOMAP_DIRECT;
1768 struct blk_plug plug;
1769 struct iomap_dio *dio;
1771 lockdep_assert_held(&inode->i_rwsem);
1776 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
1781 atomic_set(&dio->ref, 1);
1783 dio->i_size = i_size_read(inode);
1784 dio->end_io = end_io;
1787 dio->wait_for_completion = is_sync_kiocb(iocb);
1789 dio->submit.iter = iter;
1790 dio->submit.waiter = current;
1791 dio->submit.cookie = BLK_QC_T_NONE;
1792 dio->submit.last_queue = NULL;
1794 if (iov_iter_rw(iter) == READ) {
1795 if (pos >= dio->i_size)
1798 if (iter->type == ITER_IOVEC)
1799 dio->flags |= IOMAP_DIO_DIRTY;
1801 flags |= IOMAP_WRITE;
1802 dio->flags |= IOMAP_DIO_WRITE;
1804 /* for data sync or sync, we need sync completion processing */
1805 if (iocb->ki_flags & IOCB_DSYNC)
1806 dio->flags |= IOMAP_DIO_NEED_SYNC;
1809 * For datasync only writes, we optimistically try using FUA for
1810 * this IO. Any non-FUA write that occurs will clear this flag,
1811 * hence we know before completion whether a cache flush is
1814 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
1815 dio->flags |= IOMAP_DIO_WRITE_FUA;
1818 if (iocb->ki_flags & IOCB_NOWAIT) {
1819 if (filemap_range_has_page(mapping, start, end)) {
1823 flags |= IOMAP_NOWAIT;
1826 ret = filemap_write_and_wait_range(mapping, start, end);
1831 * Try to invalidate cache pages for the range we're direct
1832 * writing. If this invalidation fails, tough, the write will
1833 * still work, but racing two incompatible write paths is a
1834 * pretty crazy thing to do, so we don't support it 100%.
1836 ret = invalidate_inode_pages2_range(mapping,
1837 start >> PAGE_SHIFT, end >> PAGE_SHIFT);
1839 dio_warn_stale_pagecache(iocb->ki_filp);
1842 if (iov_iter_rw(iter) == WRITE && !dio->wait_for_completion &&
1843 !inode->i_sb->s_dio_done_wq) {
1844 ret = sb_init_dio_done_wq(inode->i_sb);
1849 inode_dio_begin(inode);
1851 blk_start_plug(&plug);
1853 ret = iomap_apply(inode, pos, count, flags, ops, dio,
1856 /* magic error code to fall back to buffered I/O */
1857 if (ret == -ENOTBLK) {
1858 dio->wait_for_completion = true;
1865 if (iov_iter_rw(iter) == READ && pos >= dio->i_size)
1867 } while ((count = iov_iter_count(iter)) > 0);
1868 blk_finish_plug(&plug);
1871 iomap_dio_set_error(dio, ret);
1874 * If all the writes we issued were FUA, we don't need to flush the
1875 * cache on IO completion. Clear the sync flag for this case.
1877 if (dio->flags & IOMAP_DIO_WRITE_FUA)
1878 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
1880 if (!atomic_dec_and_test(&dio->ref)) {
1881 if (!dio->wait_for_completion)
1882 return -EIOCBQUEUED;
1885 set_current_state(TASK_UNINTERRUPTIBLE);
1886 if (!READ_ONCE(dio->submit.waiter))
1889 if (!(iocb->ki_flags & IOCB_HIPRI) ||
1890 !dio->submit.last_queue ||
1891 !blk_poll(dio->submit.last_queue,
1892 dio->submit.cookie))
1895 __set_current_state(TASK_RUNNING);
1898 ret = iomap_dio_complete(dio);
1906 EXPORT_SYMBOL_GPL(iomap_dio_rw);
1908 /* Swapfile activation */
1911 struct iomap_swapfile_info {
1912 struct iomap iomap; /* accumulated iomap */
1913 struct swap_info_struct *sis;
1914 uint64_t lowest_ppage; /* lowest physical addr seen (pages) */
1915 uint64_t highest_ppage; /* highest physical addr seen (pages) */
1916 unsigned long nr_pages; /* number of pages collected */
1917 int nr_extents; /* extent count */
1921 * Collect physical extents for this swap file. Physical extents reported to
1922 * the swap code must be trimmed to align to a page boundary. The logical
1923 * offset within the file is irrelevant since the swapfile code maps logical
1924 * page numbers of the swap device to the physical page-aligned extents.
1926 static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi)
1928 struct iomap *iomap = &isi->iomap;
1929 unsigned long nr_pages;
1930 uint64_t first_ppage;
1931 uint64_t first_ppage_reported;
1932 uint64_t next_ppage;
1936 * Round the start up and the end down so that the physical
1937 * extent aligns to a page boundary.
1939 first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT;
1940 next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >>
1943 /* Skip too-short physical extents. */
1944 if (first_ppage >= next_ppage)
1946 nr_pages = next_ppage - first_ppage;
1949 * Calculate how much swap space we're adding; the first page contains
1950 * the swap header and doesn't count. The mm still wants that first
1951 * page fed to add_swap_extent, however.
1953 first_ppage_reported = first_ppage;
1954 if (iomap->offset == 0)
1955 first_ppage_reported++;
1956 if (isi->lowest_ppage > first_ppage_reported)
1957 isi->lowest_ppage = first_ppage_reported;
1958 if (isi->highest_ppage < (next_ppage - 1))
1959 isi->highest_ppage = next_ppage - 1;
1961 /* Add extent, set up for the next call. */
1962 error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage);
1965 isi->nr_extents += error;
1966 isi->nr_pages += nr_pages;
1971 * Accumulate iomaps for this swap file. We have to accumulate iomaps because
1972 * swap only cares about contiguous page-aligned physical extents and makes no
1973 * distinction between written and unwritten extents.
1975 static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
1976 loff_t count, void *data, struct iomap *iomap)
1978 struct iomap_swapfile_info *isi = data;
1981 switch (iomap->type) {
1983 case IOMAP_UNWRITTEN:
1984 /* Only real or unwritten extents. */
1987 /* No inline data. */
1988 pr_err("swapon: file is inline\n");
1991 pr_err("swapon: file has unallocated extents\n");
1995 /* No uncommitted metadata or shared blocks. */
1996 if (iomap->flags & IOMAP_F_DIRTY) {
1997 pr_err("swapon: file is not committed\n");
2000 if (iomap->flags & IOMAP_F_SHARED) {
2001 pr_err("swapon: file has shared extents\n");
2005 /* Only one bdev per swap file. */
2006 if (iomap->bdev != isi->sis->bdev) {
2007 pr_err("swapon: file is on multiple devices\n");
2011 if (isi->iomap.length == 0) {
2012 /* No accumulated extent, so just store it. */
2013 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
2014 } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
2015 /* Append this to the accumulated extent. */
2016 isi->iomap.length += iomap->length;
2018 /* Otherwise, add the retained iomap and store this one. */
2019 error = iomap_swapfile_add_extent(isi);
2022 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
2028 * Iterate a swap file's iomaps to construct physical extents that can be
2029 * passed to the swapfile subsystem.
2031 int iomap_swapfile_activate(struct swap_info_struct *sis,
2032 struct file *swap_file, sector_t *pagespan,
2033 const struct iomap_ops *ops)
2035 struct iomap_swapfile_info isi = {
2037 .lowest_ppage = (sector_t)-1ULL,
2039 struct address_space *mapping = swap_file->f_mapping;
2040 struct inode *inode = mapping->host;
2042 loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE);
2046 * Persist all file mapping metadata so that we won't have any
2047 * IOMAP_F_DIRTY iomaps.
2049 ret = vfs_fsync(swap_file, 1);
2054 ret = iomap_apply(inode, pos, len, IOMAP_REPORT,
2055 ops, &isi, iomap_swapfile_activate_actor);
2063 if (isi.iomap.length) {
2064 ret = iomap_swapfile_add_extent(&isi);
2069 *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage;
2070 sis->max = isi.nr_pages;
2071 sis->pages = isi.nr_pages - 1;
2072 sis->highest_bit = isi.nr_pages - 1;
2073 return isi.nr_extents;
2075 EXPORT_SYMBOL_GPL(iomap_swapfile_activate);
2076 #endif /* CONFIG_SWAP */
2079 iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
2080 void *data, struct iomap *iomap)
2082 sector_t *bno = data, addr;
2084 if (iomap->type == IOMAP_MAPPED) {
2085 addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
2087 WARN(1, "would truncate bmap result\n");
2094 /* legacy ->bmap interface. 0 is the error return (!) */
2096 iomap_bmap(struct address_space *mapping, sector_t bno,
2097 const struct iomap_ops *ops)
2099 struct inode *inode = mapping->host;
2100 loff_t pos = bno << inode->i_blkbits;
2101 unsigned blocksize = i_blocksize(inode);
2103 if (filemap_write_and_wait(mapping))
2107 iomap_apply(inode, pos, blocksize, 0, ops, &bno, iomap_bmap_actor);
2110 EXPORT_SYMBOL_GPL(iomap_bmap);