tools uapi asm: Update asm-generic/unistd.h copy
[linux/fpc-iii.git] / fs / iomap.c
blob5bc172f3dfe8c8f8600622cc66754406cdb3b3bf
1 /*
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
12 * more details.
14 #include <linux/module.h>
15 #include <linux/compiler.h>
16 #include <linux/fs.h>
17 #include <linux/iomap.h>
18 #include <linux/uaccess.h>
19 #include <linux/gfp.h>
20 #include <linux/migrate.h>
21 #include <linux/mm.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>
34 #include "internal.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
45 * iomap_end call.
47 loff_t
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);
67 if (ret)
68 return ret;
69 if (WARN_ON(iomap.offset > pos))
70 return -EIO;
71 if (WARN_ON(iomap.length == 0))
72 return -EIO;
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.
92 if (ops->iomap_end) {
93 ret = ops->iomap_end(inode, pos, length,
94 written > 0 ? written : 0,
95 flags, &iomap);
98 return written ? written : ret;
101 static sector_t
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)
113 return iop;
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);
121 * migrate_page_move_mapping() assumes that pages with private data have
122 * their count elevated by 1.
124 get_page(page);
125 set_page_private(page, (unsigned long)iop);
126 SetPagePrivate(page);
127 return iop;
130 static void
131 iomap_page_release(struct page *page)
133 struct iomap_page *iop = to_iomap_page(page);
135 if (!iop)
136 return;
137 WARN_ON_ONCE(atomic_read(&iop->read_count));
138 WARN_ON_ONCE(atomic_read(&iop->write_count));
139 ClearPagePrivate(page);
140 set_page_private(page, 0);
141 put_page(page);
142 kfree(iop);
146 * Calculate the range inside the page that we actually need to read.
148 static void
149 iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
150 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
152 loff_t orig_pos = *pos;
153 loff_t isize = i_size_read(inode);
154 unsigned block_bits = inode->i_blkbits;
155 unsigned block_size = (1 << block_bits);
156 unsigned poff = offset_in_page(*pos);
157 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
158 unsigned first = poff >> block_bits;
159 unsigned last = (poff + plen - 1) >> block_bits;
162 * If the block size is smaller than the page size we need to check the
163 * per-block uptodate status and adjust the offset and length if needed
164 * to avoid reading in already uptodate ranges.
166 if (iop) {
167 unsigned int i;
169 /* move forward for each leading block marked uptodate */
170 for (i = first; i <= last; i++) {
171 if (!test_bit(i, iop->uptodate))
172 break;
173 *pos += block_size;
174 poff += block_size;
175 plen -= block_size;
176 first++;
179 /* truncate len if we find any trailing uptodate block(s) */
180 for ( ; i <= last; i++) {
181 if (test_bit(i, iop->uptodate)) {
182 plen -= (last - i + 1) * block_size;
183 last = i - 1;
184 break;
190 * If the extent spans the block that contains the i_size we need to
191 * handle both halves separately so that we properly zero data in the
192 * page cache for blocks that are entirely outside of i_size.
194 if (orig_pos <= isize && orig_pos + length > isize) {
195 unsigned end = offset_in_page(isize - 1) >> block_bits;
197 if (first <= end && last > end)
198 plen -= (last - end) * block_size;
201 *offp = poff;
202 *lenp = plen;
205 static void
206 iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
208 struct iomap_page *iop = to_iomap_page(page);
209 struct inode *inode = page->mapping->host;
210 unsigned first = off >> inode->i_blkbits;
211 unsigned last = (off + len - 1) >> inode->i_blkbits;
212 unsigned int i;
213 bool uptodate = true;
215 if (iop) {
216 for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
217 if (i >= first && i <= last)
218 set_bit(i, iop->uptodate);
219 else if (!test_bit(i, iop->uptodate))
220 uptodate = false;
224 if (uptodate && !PageError(page))
225 SetPageUptodate(page);
228 static void
229 iomap_read_finish(struct iomap_page *iop, struct page *page)
231 if (!iop || atomic_dec_and_test(&iop->read_count))
232 unlock_page(page);
235 static void
236 iomap_read_page_end_io(struct bio_vec *bvec, int error)
238 struct page *page = bvec->bv_page;
239 struct iomap_page *iop = to_iomap_page(page);
241 if (unlikely(error)) {
242 ClearPageUptodate(page);
243 SetPageError(page);
244 } else {
245 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
248 iomap_read_finish(iop, page);
251 static void
252 iomap_read_inline_data(struct inode *inode, struct page *page,
253 struct iomap *iomap)
255 size_t size = i_size_read(inode);
256 void *addr;
258 if (PageUptodate(page))
259 return;
261 BUG_ON(page->index);
262 BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
264 addr = kmap_atomic(page);
265 memcpy(addr, iomap->inline_data, size);
266 memset(addr + size, 0, PAGE_SIZE - size);
267 kunmap_atomic(addr);
268 SetPageUptodate(page);
271 static void
272 iomap_read_end_io(struct bio *bio)
274 int error = blk_status_to_errno(bio->bi_status);
275 struct bio_vec *bvec;
276 int i;
278 bio_for_each_segment_all(bvec, bio, i)
279 iomap_read_page_end_io(bvec, error);
280 bio_put(bio);
283 struct iomap_readpage_ctx {
284 struct page *cur_page;
285 bool cur_page_in_bio;
286 bool is_readahead;
287 struct bio *bio;
288 struct list_head *pages;
291 static loff_t
292 iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
293 struct iomap *iomap)
295 struct iomap_readpage_ctx *ctx = data;
296 struct page *page = ctx->cur_page;
297 struct iomap_page *iop = iomap_page_create(inode, page);
298 bool is_contig = false;
299 loff_t orig_pos = pos;
300 unsigned poff, plen;
301 sector_t sector;
303 if (iomap->type == IOMAP_INLINE) {
304 WARN_ON_ONCE(pos);
305 iomap_read_inline_data(inode, page, iomap);
306 return PAGE_SIZE;
309 /* zero post-eof blocks as the page may be mapped */
310 iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
311 if (plen == 0)
312 goto done;
314 if (iomap->type != IOMAP_MAPPED || pos >= i_size_read(inode)) {
315 zero_user(page, poff, plen);
316 iomap_set_range_uptodate(page, poff, plen);
317 goto done;
320 ctx->cur_page_in_bio = true;
323 * Try to merge into a previous segment if we can.
325 sector = iomap_sector(iomap, pos);
326 if (ctx->bio && bio_end_sector(ctx->bio) == sector) {
327 if (__bio_try_merge_page(ctx->bio, page, plen, poff))
328 goto done;
329 is_contig = true;
333 * If we start a new segment we need to increase the read count, and we
334 * need to do so before submitting any previous full bio to make sure
335 * that we don't prematurely unlock the page.
337 if (iop)
338 atomic_inc(&iop->read_count);
340 if (!ctx->bio || !is_contig || bio_full(ctx->bio)) {
341 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
342 int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
344 if (ctx->bio)
345 submit_bio(ctx->bio);
347 if (ctx->is_readahead) /* same as readahead_gfp_mask */
348 gfp |= __GFP_NORETRY | __GFP_NOWARN;
349 ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
350 ctx->bio->bi_opf = REQ_OP_READ;
351 if (ctx->is_readahead)
352 ctx->bio->bi_opf |= REQ_RAHEAD;
353 ctx->bio->bi_iter.bi_sector = sector;
354 bio_set_dev(ctx->bio, iomap->bdev);
355 ctx->bio->bi_end_io = iomap_read_end_io;
358 __bio_add_page(ctx->bio, page, plen, poff);
359 done:
361 * Move the caller beyond our range so that it keeps making progress.
362 * For that we have to include any leading non-uptodate ranges, but
363 * we can skip trailing ones as they will be handled in the next
364 * iteration.
366 return pos - orig_pos + plen;
370 iomap_readpage(struct page *page, const struct iomap_ops *ops)
372 struct iomap_readpage_ctx ctx = { .cur_page = page };
373 struct inode *inode = page->mapping->host;
374 unsigned poff;
375 loff_t ret;
377 for (poff = 0; poff < PAGE_SIZE; poff += ret) {
378 ret = iomap_apply(inode, page_offset(page) + poff,
379 PAGE_SIZE - poff, 0, ops, &ctx,
380 iomap_readpage_actor);
381 if (ret <= 0) {
382 WARN_ON_ONCE(ret == 0);
383 SetPageError(page);
384 break;
388 if (ctx.bio) {
389 submit_bio(ctx.bio);
390 WARN_ON_ONCE(!ctx.cur_page_in_bio);
391 } else {
392 WARN_ON_ONCE(ctx.cur_page_in_bio);
393 unlock_page(page);
397 * Just like mpage_readpages and block_read_full_page we always
398 * return 0 and just mark the page as PageError on errors. This
399 * should be cleaned up all through the stack eventually.
401 return 0;
403 EXPORT_SYMBOL_GPL(iomap_readpage);
405 static struct page *
406 iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
407 loff_t length, loff_t *done)
409 while (!list_empty(pages)) {
410 struct page *page = lru_to_page(pages);
412 if (page_offset(page) >= (u64)pos + length)
413 break;
415 list_del(&page->lru);
416 if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
417 GFP_NOFS))
418 return page;
421 * If we already have a page in the page cache at index we are
422 * done. Upper layers don't care if it is uptodate after the
423 * readpages call itself as every page gets checked again once
424 * actually needed.
426 *done += PAGE_SIZE;
427 put_page(page);
430 return NULL;
433 static loff_t
434 iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
435 void *data, struct iomap *iomap)
437 struct iomap_readpage_ctx *ctx = data;
438 loff_t done, ret;
440 for (done = 0; done < length; done += ret) {
441 if (ctx->cur_page && offset_in_page(pos + done) == 0) {
442 if (!ctx->cur_page_in_bio)
443 unlock_page(ctx->cur_page);
444 put_page(ctx->cur_page);
445 ctx->cur_page = NULL;
447 if (!ctx->cur_page) {
448 ctx->cur_page = iomap_next_page(inode, ctx->pages,
449 pos, length, &done);
450 if (!ctx->cur_page)
451 break;
452 ctx->cur_page_in_bio = false;
454 ret = iomap_readpage_actor(inode, pos + done, length - done,
455 ctx, iomap);
458 return done;
462 iomap_readpages(struct address_space *mapping, struct list_head *pages,
463 unsigned nr_pages, const struct iomap_ops *ops)
465 struct iomap_readpage_ctx ctx = {
466 .pages = pages,
467 .is_readahead = true,
469 loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
470 loff_t last = page_offset(list_entry(pages->next, struct page, lru));
471 loff_t length = last - pos + PAGE_SIZE, ret = 0;
473 while (length > 0) {
474 ret = iomap_apply(mapping->host, pos, length, 0, ops,
475 &ctx, iomap_readpages_actor);
476 if (ret <= 0) {
477 WARN_ON_ONCE(ret == 0);
478 goto done;
480 pos += ret;
481 length -= ret;
483 ret = 0;
484 done:
485 if (ctx.bio)
486 submit_bio(ctx.bio);
487 if (ctx.cur_page) {
488 if (!ctx.cur_page_in_bio)
489 unlock_page(ctx.cur_page);
490 put_page(ctx.cur_page);
494 * Check that we didn't lose a page due to the arcance calling
495 * conventions..
497 WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
498 return ret;
500 EXPORT_SYMBOL_GPL(iomap_readpages);
503 iomap_is_partially_uptodate(struct page *page, unsigned long from,
504 unsigned long count)
506 struct iomap_page *iop = to_iomap_page(page);
507 struct inode *inode = page->mapping->host;
508 unsigned first = from >> inode->i_blkbits;
509 unsigned last = (from + count - 1) >> inode->i_blkbits;
510 unsigned i;
512 if (iop) {
513 for (i = first; i <= last; i++)
514 if (!test_bit(i, iop->uptodate))
515 return 0;
516 return 1;
519 return 0;
521 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
524 iomap_releasepage(struct page *page, gfp_t gfp_mask)
527 * mm accommodates an old ext3 case where clean pages might not have had
528 * the dirty bit cleared. Thus, it can send actual dirty pages to
529 * ->releasepage() via shrink_active_list(), skip those here.
531 if (PageDirty(page) || PageWriteback(page))
532 return 0;
533 iomap_page_release(page);
534 return 1;
536 EXPORT_SYMBOL_GPL(iomap_releasepage);
538 void
539 iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
542 * If we are invalidating the entire page, clear the dirty state from it
543 * and release it to avoid unnecessary buildup of the LRU.
545 if (offset == 0 && len == PAGE_SIZE) {
546 WARN_ON_ONCE(PageWriteback(page));
547 cancel_dirty_page(page);
548 iomap_page_release(page);
551 EXPORT_SYMBOL_GPL(iomap_invalidatepage);
553 #ifdef CONFIG_MIGRATION
555 iomap_migrate_page(struct address_space *mapping, struct page *newpage,
556 struct page *page, enum migrate_mode mode)
558 int ret;
560 ret = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
561 if (ret != MIGRATEPAGE_SUCCESS)
562 return ret;
564 if (page_has_private(page)) {
565 ClearPagePrivate(page);
566 set_page_private(newpage, page_private(page));
567 set_page_private(page, 0);
568 SetPagePrivate(newpage);
571 if (mode != MIGRATE_SYNC_NO_COPY)
572 migrate_page_copy(newpage, page);
573 else
574 migrate_page_states(newpage, page);
575 return MIGRATEPAGE_SUCCESS;
577 EXPORT_SYMBOL_GPL(iomap_migrate_page);
578 #endif /* CONFIG_MIGRATION */
580 static void
581 iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
583 loff_t i_size = i_size_read(inode);
586 * Only truncate newly allocated pages beyoned EOF, even if the
587 * write started inside the existing inode size.
589 if (pos + len > i_size)
590 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
593 static int
594 iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
595 unsigned poff, unsigned plen, unsigned from, unsigned to,
596 struct iomap *iomap)
598 struct bio_vec bvec;
599 struct bio bio;
601 if (iomap->type != IOMAP_MAPPED || block_start >= i_size_read(inode)) {
602 zero_user_segments(page, poff, from, to, poff + plen);
603 iomap_set_range_uptodate(page, poff, plen);
604 return 0;
607 bio_init(&bio, &bvec, 1);
608 bio.bi_opf = REQ_OP_READ;
609 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
610 bio_set_dev(&bio, iomap->bdev);
611 __bio_add_page(&bio, page, plen, poff);
612 return submit_bio_wait(&bio);
615 static int
616 __iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
617 struct page *page, struct iomap *iomap)
619 struct iomap_page *iop = iomap_page_create(inode, page);
620 loff_t block_size = i_blocksize(inode);
621 loff_t block_start = pos & ~(block_size - 1);
622 loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
623 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
624 int status = 0;
626 if (PageUptodate(page))
627 return 0;
629 do {
630 iomap_adjust_read_range(inode, iop, &block_start,
631 block_end - block_start, &poff, &plen);
632 if (plen == 0)
633 break;
635 if ((from > poff && from < poff + plen) ||
636 (to > poff && to < poff + plen)) {
637 status = iomap_read_page_sync(inode, block_start, page,
638 poff, plen, from, to, iomap);
639 if (status)
640 break;
643 } while ((block_start += plen) < block_end);
645 return status;
648 static int
649 iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
650 struct page **pagep, struct iomap *iomap)
652 pgoff_t index = pos >> PAGE_SHIFT;
653 struct page *page;
654 int status = 0;
656 BUG_ON(pos + len > iomap->offset + iomap->length);
658 if (fatal_signal_pending(current))
659 return -EINTR;
661 page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
662 if (!page)
663 return -ENOMEM;
665 if (iomap->type == IOMAP_INLINE)
666 iomap_read_inline_data(inode, page, iomap);
667 else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
668 status = __block_write_begin_int(page, pos, len, NULL, iomap);
669 else
670 status = __iomap_write_begin(inode, pos, len, page, iomap);
671 if (unlikely(status)) {
672 unlock_page(page);
673 put_page(page);
674 page = NULL;
676 iomap_write_failed(inode, pos, len);
679 *pagep = page;
680 return status;
684 iomap_set_page_dirty(struct page *page)
686 struct address_space *mapping = page_mapping(page);
687 int newly_dirty;
689 if (unlikely(!mapping))
690 return !TestSetPageDirty(page);
693 * Lock out page->mem_cgroup migration to keep PageDirty
694 * synchronized with per-memcg dirty page counters.
696 lock_page_memcg(page);
697 newly_dirty = !TestSetPageDirty(page);
698 if (newly_dirty)
699 __set_page_dirty(page, mapping, 0);
700 unlock_page_memcg(page);
702 if (newly_dirty)
703 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
704 return newly_dirty;
706 EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
708 static int
709 __iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
710 unsigned copied, struct page *page, struct iomap *iomap)
712 flush_dcache_page(page);
715 * The blocks that were entirely written will now be uptodate, so we
716 * don't have to worry about a readpage reading them and overwriting a
717 * partial write. However if we have encountered a short write and only
718 * partially written into a block, it will not be marked uptodate, so a
719 * readpage might come in and destroy our partial write.
721 * Do the simplest thing, and just treat any short write to a non
722 * uptodate page as a zero-length write, and force the caller to redo
723 * the whole thing.
725 if (unlikely(copied < len && !PageUptodate(page))) {
726 copied = 0;
727 } else {
728 iomap_set_range_uptodate(page, offset_in_page(pos), len);
729 iomap_set_page_dirty(page);
731 return __generic_write_end(inode, pos, copied, page);
734 static int
735 iomap_write_end_inline(struct inode *inode, struct page *page,
736 struct iomap *iomap, loff_t pos, unsigned copied)
738 void *addr;
740 WARN_ON_ONCE(!PageUptodate(page));
741 BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
743 addr = kmap_atomic(page);
744 memcpy(iomap->inline_data + pos, addr + pos, copied);
745 kunmap_atomic(addr);
747 mark_inode_dirty(inode);
748 __generic_write_end(inode, pos, copied, page);
749 return copied;
752 static int
753 iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
754 unsigned copied, struct page *page, struct iomap *iomap)
756 int ret;
758 if (iomap->type == IOMAP_INLINE) {
759 ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
760 } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
761 ret = generic_write_end(NULL, inode->i_mapping, pos, len,
762 copied, page, NULL);
763 } else {
764 ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
767 if (iomap->page_done)
768 iomap->page_done(inode, pos, copied, page, iomap);
770 if (ret < len)
771 iomap_write_failed(inode, pos, len);
772 return ret;
775 static loff_t
776 iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
777 struct iomap *iomap)
779 struct iov_iter *i = data;
780 long status = 0;
781 ssize_t written = 0;
782 unsigned int flags = AOP_FLAG_NOFS;
784 do {
785 struct page *page;
786 unsigned long offset; /* Offset into pagecache page */
787 unsigned long bytes; /* Bytes to write to page */
788 size_t copied; /* Bytes copied from user */
790 offset = offset_in_page(pos);
791 bytes = min_t(unsigned long, PAGE_SIZE - offset,
792 iov_iter_count(i));
793 again:
794 if (bytes > length)
795 bytes = length;
798 * Bring in the user page that we will copy from _first_.
799 * Otherwise there's a nasty deadlock on copying from the
800 * same page as we're writing to, without it being marked
801 * up-to-date.
803 * Not only is this an optimisation, but it is also required
804 * to check that the address is actually valid, when atomic
805 * usercopies are used, below.
807 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
808 status = -EFAULT;
809 break;
812 status = iomap_write_begin(inode, pos, bytes, flags, &page,
813 iomap);
814 if (unlikely(status))
815 break;
817 if (mapping_writably_mapped(inode->i_mapping))
818 flush_dcache_page(page);
820 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
822 flush_dcache_page(page);
824 status = iomap_write_end(inode, pos, bytes, copied, page,
825 iomap);
826 if (unlikely(status < 0))
827 break;
828 copied = status;
830 cond_resched();
832 iov_iter_advance(i, copied);
833 if (unlikely(copied == 0)) {
835 * If we were unable to copy any data at all, we must
836 * fall back to a single segment length write.
838 * If we didn't fallback here, we could livelock
839 * because not all segments in the iov can be copied at
840 * once without a pagefault.
842 bytes = min_t(unsigned long, PAGE_SIZE - offset,
843 iov_iter_single_seg_count(i));
844 goto again;
846 pos += copied;
847 written += copied;
848 length -= copied;
850 balance_dirty_pages_ratelimited(inode->i_mapping);
851 } while (iov_iter_count(i) && length);
853 return written ? written : status;
856 ssize_t
857 iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
858 const struct iomap_ops *ops)
860 struct inode *inode = iocb->ki_filp->f_mapping->host;
861 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
863 while (iov_iter_count(iter)) {
864 ret = iomap_apply(inode, pos, iov_iter_count(iter),
865 IOMAP_WRITE, ops, iter, iomap_write_actor);
866 if (ret <= 0)
867 break;
868 pos += ret;
869 written += ret;
872 return written ? written : ret;
874 EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
876 static struct page *
877 __iomap_read_page(struct inode *inode, loff_t offset)
879 struct address_space *mapping = inode->i_mapping;
880 struct page *page;
882 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
883 if (IS_ERR(page))
884 return page;
885 if (!PageUptodate(page)) {
886 put_page(page);
887 return ERR_PTR(-EIO);
889 return page;
892 static loff_t
893 iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
894 struct iomap *iomap)
896 long status = 0;
897 ssize_t written = 0;
899 do {
900 struct page *page, *rpage;
901 unsigned long offset; /* Offset into pagecache page */
902 unsigned long bytes; /* Bytes to write to page */
904 offset = offset_in_page(pos);
905 bytes = min_t(loff_t, PAGE_SIZE - offset, length);
907 rpage = __iomap_read_page(inode, pos);
908 if (IS_ERR(rpage))
909 return PTR_ERR(rpage);
911 status = iomap_write_begin(inode, pos, bytes,
912 AOP_FLAG_NOFS, &page, iomap);
913 put_page(rpage);
914 if (unlikely(status))
915 return status;
917 WARN_ON_ONCE(!PageUptodate(page));
919 status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
920 if (unlikely(status <= 0)) {
921 if (WARN_ON_ONCE(status == 0))
922 return -EIO;
923 return status;
926 cond_resched();
928 pos += status;
929 written += status;
930 length -= status;
932 balance_dirty_pages_ratelimited(inode->i_mapping);
933 } while (length);
935 return written;
939 iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
940 const struct iomap_ops *ops)
942 loff_t ret;
944 while (len) {
945 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
946 iomap_dirty_actor);
947 if (ret <= 0)
948 return ret;
949 pos += ret;
950 len -= ret;
953 return 0;
955 EXPORT_SYMBOL_GPL(iomap_file_dirty);
957 static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
958 unsigned bytes, struct iomap *iomap)
960 struct page *page;
961 int status;
963 status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
964 iomap);
965 if (status)
966 return status;
968 zero_user(page, offset, bytes);
969 mark_page_accessed(page);
971 return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
974 static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
975 struct iomap *iomap)
977 return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
978 iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
981 static loff_t
982 iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
983 void *data, struct iomap *iomap)
985 bool *did_zero = data;
986 loff_t written = 0;
987 int status;
989 /* already zeroed? we're done. */
990 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
991 return count;
993 do {
994 unsigned offset, bytes;
996 offset = offset_in_page(pos);
997 bytes = min_t(loff_t, PAGE_SIZE - offset, count);
999 if (IS_DAX(inode))
1000 status = iomap_dax_zero(pos, offset, bytes, iomap);
1001 else
1002 status = iomap_zero(inode, pos, offset, bytes, iomap);
1003 if (status < 0)
1004 return status;
1006 pos += bytes;
1007 count -= bytes;
1008 written += bytes;
1009 if (did_zero)
1010 *did_zero = true;
1011 } while (count > 0);
1013 return written;
1017 iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1018 const struct iomap_ops *ops)
1020 loff_t ret;
1022 while (len > 0) {
1023 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
1024 ops, did_zero, iomap_zero_range_actor);
1025 if (ret <= 0)
1026 return ret;
1028 pos += ret;
1029 len -= ret;
1032 return 0;
1034 EXPORT_SYMBOL_GPL(iomap_zero_range);
1037 iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1038 const struct iomap_ops *ops)
1040 unsigned int blocksize = i_blocksize(inode);
1041 unsigned int off = pos & (blocksize - 1);
1043 /* Block boundary? Nothing to do */
1044 if (!off)
1045 return 0;
1046 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1048 EXPORT_SYMBOL_GPL(iomap_truncate_page);
1050 static loff_t
1051 iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
1052 void *data, struct iomap *iomap)
1054 struct page *page = data;
1055 int ret;
1057 if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
1058 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
1059 if (ret)
1060 return ret;
1061 block_commit_write(page, 0, length);
1062 } else {
1063 WARN_ON_ONCE(!PageUptodate(page));
1064 iomap_page_create(inode, page);
1065 set_page_dirty(page);
1068 return length;
1071 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1073 struct page *page = vmf->page;
1074 struct inode *inode = file_inode(vmf->vma->vm_file);
1075 unsigned long length;
1076 loff_t offset, size;
1077 ssize_t ret;
1079 lock_page(page);
1080 size = i_size_read(inode);
1081 if ((page->mapping != inode->i_mapping) ||
1082 (page_offset(page) > size)) {
1083 /* We overload EFAULT to mean page got truncated */
1084 ret = -EFAULT;
1085 goto out_unlock;
1088 /* page is wholly or partially inside EOF */
1089 if (((page->index + 1) << PAGE_SHIFT) > size)
1090 length = offset_in_page(size);
1091 else
1092 length = PAGE_SIZE;
1094 offset = page_offset(page);
1095 while (length > 0) {
1096 ret = iomap_apply(inode, offset, length,
1097 IOMAP_WRITE | IOMAP_FAULT, ops, page,
1098 iomap_page_mkwrite_actor);
1099 if (unlikely(ret <= 0))
1100 goto out_unlock;
1101 offset += ret;
1102 length -= ret;
1105 wait_for_stable_page(page);
1106 return VM_FAULT_LOCKED;
1107 out_unlock:
1108 unlock_page(page);
1109 return block_page_mkwrite_return(ret);
1111 EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1113 struct fiemap_ctx {
1114 struct fiemap_extent_info *fi;
1115 struct iomap prev;
1118 static int iomap_to_fiemap(struct fiemap_extent_info *fi,
1119 struct iomap *iomap, u32 flags)
1121 switch (iomap->type) {
1122 case IOMAP_HOLE:
1123 /* skip holes */
1124 return 0;
1125 case IOMAP_DELALLOC:
1126 flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
1127 break;
1128 case IOMAP_MAPPED:
1129 break;
1130 case IOMAP_UNWRITTEN:
1131 flags |= FIEMAP_EXTENT_UNWRITTEN;
1132 break;
1133 case IOMAP_INLINE:
1134 flags |= FIEMAP_EXTENT_DATA_INLINE;
1135 break;
1138 if (iomap->flags & IOMAP_F_MERGED)
1139 flags |= FIEMAP_EXTENT_MERGED;
1140 if (iomap->flags & IOMAP_F_SHARED)
1141 flags |= FIEMAP_EXTENT_SHARED;
1143 return fiemap_fill_next_extent(fi, iomap->offset,
1144 iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
1145 iomap->length, flags);
1148 static loff_t
1149 iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1150 struct iomap *iomap)
1152 struct fiemap_ctx *ctx = data;
1153 loff_t ret = length;
1155 if (iomap->type == IOMAP_HOLE)
1156 return length;
1158 ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
1159 ctx->prev = *iomap;
1160 switch (ret) {
1161 case 0: /* success */
1162 return length;
1163 case 1: /* extent array full */
1164 return 0;
1165 default:
1166 return ret;
1170 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
1171 loff_t start, loff_t len, const struct iomap_ops *ops)
1173 struct fiemap_ctx ctx;
1174 loff_t ret;
1176 memset(&ctx, 0, sizeof(ctx));
1177 ctx.fi = fi;
1178 ctx.prev.type = IOMAP_HOLE;
1180 ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
1181 if (ret)
1182 return ret;
1184 if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
1185 ret = filemap_write_and_wait(inode->i_mapping);
1186 if (ret)
1187 return ret;
1190 while (len > 0) {
1191 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
1192 iomap_fiemap_actor);
1193 /* inode with no (attribute) mapping will give ENOENT */
1194 if (ret == -ENOENT)
1195 break;
1196 if (ret < 0)
1197 return ret;
1198 if (ret == 0)
1199 break;
1201 start += ret;
1202 len -= ret;
1205 if (ctx.prev.type != IOMAP_HOLE) {
1206 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
1207 if (ret < 0)
1208 return ret;
1211 return 0;
1213 EXPORT_SYMBOL_GPL(iomap_fiemap);
1216 * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
1217 * Returns true if found and updates @lastoff to the offset in file.
1219 static bool
1220 page_seek_hole_data(struct inode *inode, struct page *page, loff_t *lastoff,
1221 int whence)
1223 const struct address_space_operations *ops = inode->i_mapping->a_ops;
1224 unsigned int bsize = i_blocksize(inode), off;
1225 bool seek_data = whence == SEEK_DATA;
1226 loff_t poff = page_offset(page);
1228 if (WARN_ON_ONCE(*lastoff >= poff + PAGE_SIZE))
1229 return false;
1231 if (*lastoff < poff) {
1233 * Last offset smaller than the start of the page means we found
1234 * a hole:
1236 if (whence == SEEK_HOLE)
1237 return true;
1238 *lastoff = poff;
1242 * Just check the page unless we can and should check block ranges:
1244 if (bsize == PAGE_SIZE || !ops->is_partially_uptodate)
1245 return PageUptodate(page) == seek_data;
1247 lock_page(page);
1248 if (unlikely(page->mapping != inode->i_mapping))
1249 goto out_unlock_not_found;
1251 for (off = 0; off < PAGE_SIZE; off += bsize) {
1252 if (offset_in_page(*lastoff) >= off + bsize)
1253 continue;
1254 if (ops->is_partially_uptodate(page, off, bsize) == seek_data) {
1255 unlock_page(page);
1256 return true;
1258 *lastoff = poff + off + bsize;
1261 out_unlock_not_found:
1262 unlock_page(page);
1263 return false;
1267 * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
1269 * Within unwritten extents, the page cache determines which parts are holes
1270 * and which are data: uptodate buffer heads count as data; everything else
1271 * counts as a hole.
1273 * Returns the resulting offset on successs, and -ENOENT otherwise.
1275 static loff_t
1276 page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,
1277 int whence)
1279 pgoff_t index = offset >> PAGE_SHIFT;
1280 pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1281 loff_t lastoff = offset;
1282 struct pagevec pvec;
1284 if (length <= 0)
1285 return -ENOENT;
1287 pagevec_init(&pvec);
1289 do {
1290 unsigned nr_pages, i;
1292 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index,
1293 end - 1);
1294 if (nr_pages == 0)
1295 break;
1297 for (i = 0; i < nr_pages; i++) {
1298 struct page *page = pvec.pages[i];
1300 if (page_seek_hole_data(inode, page, &lastoff, whence))
1301 goto check_range;
1302 lastoff = page_offset(page) + PAGE_SIZE;
1304 pagevec_release(&pvec);
1305 } while (index < end);
1307 /* When no page at lastoff and we are not done, we found a hole. */
1308 if (whence != SEEK_HOLE)
1309 goto not_found;
1311 check_range:
1312 if (lastoff < offset + length)
1313 goto out;
1314 not_found:
1315 lastoff = -ENOENT;
1316 out:
1317 pagevec_release(&pvec);
1318 return lastoff;
1322 static loff_t
1323 iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length,
1324 void *data, struct iomap *iomap)
1326 switch (iomap->type) {
1327 case IOMAP_UNWRITTEN:
1328 offset = page_cache_seek_hole_data(inode, offset, length,
1329 SEEK_HOLE);
1330 if (offset < 0)
1331 return length;
1332 /* fall through */
1333 case IOMAP_HOLE:
1334 *(loff_t *)data = offset;
1335 return 0;
1336 default:
1337 return length;
1341 loff_t
1342 iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
1344 loff_t size = i_size_read(inode);
1345 loff_t length = size - offset;
1346 loff_t ret;
1348 /* Nothing to be found before or beyond the end of the file. */
1349 if (offset < 0 || offset >= size)
1350 return -ENXIO;
1352 while (length > 0) {
1353 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
1354 &offset, iomap_seek_hole_actor);
1355 if (ret < 0)
1356 return ret;
1357 if (ret == 0)
1358 break;
1360 offset += ret;
1361 length -= ret;
1364 return offset;
1366 EXPORT_SYMBOL_GPL(iomap_seek_hole);
1368 static loff_t
1369 iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length,
1370 void *data, struct iomap *iomap)
1372 switch (iomap->type) {
1373 case IOMAP_HOLE:
1374 return length;
1375 case IOMAP_UNWRITTEN:
1376 offset = page_cache_seek_hole_data(inode, offset, length,
1377 SEEK_DATA);
1378 if (offset < 0)
1379 return length;
1380 /*FALLTHRU*/
1381 default:
1382 *(loff_t *)data = offset;
1383 return 0;
1387 loff_t
1388 iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
1390 loff_t size = i_size_read(inode);
1391 loff_t length = size - offset;
1392 loff_t ret;
1394 /* Nothing to be found before or beyond the end of the file. */
1395 if (offset < 0 || offset >= size)
1396 return -ENXIO;
1398 while (length > 0) {
1399 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
1400 &offset, iomap_seek_data_actor);
1401 if (ret < 0)
1402 return ret;
1403 if (ret == 0)
1404 break;
1406 offset += ret;
1407 length -= ret;
1410 if (length <= 0)
1411 return -ENXIO;
1412 return offset;
1414 EXPORT_SYMBOL_GPL(iomap_seek_data);
1417 * Private flags for iomap_dio, must not overlap with the public ones in
1418 * iomap.h:
1420 #define IOMAP_DIO_WRITE_FUA (1 << 28)
1421 #define IOMAP_DIO_NEED_SYNC (1 << 29)
1422 #define IOMAP_DIO_WRITE (1 << 30)
1423 #define IOMAP_DIO_DIRTY (1 << 31)
1425 struct iomap_dio {
1426 struct kiocb *iocb;
1427 iomap_dio_end_io_t *end_io;
1428 loff_t i_size;
1429 loff_t size;
1430 atomic_t ref;
1431 unsigned flags;
1432 int error;
1433 bool wait_for_completion;
1435 union {
1436 /* used during submission and for synchronous completion: */
1437 struct {
1438 struct iov_iter *iter;
1439 struct task_struct *waiter;
1440 struct request_queue *last_queue;
1441 blk_qc_t cookie;
1442 } submit;
1444 /* used for aio completion: */
1445 struct {
1446 struct work_struct work;
1447 } aio;
1451 static ssize_t iomap_dio_complete(struct iomap_dio *dio)
1453 struct kiocb *iocb = dio->iocb;
1454 struct inode *inode = file_inode(iocb->ki_filp);
1455 loff_t offset = iocb->ki_pos;
1456 ssize_t ret;
1458 if (dio->end_io) {
1459 ret = dio->end_io(iocb,
1460 dio->error ? dio->error : dio->size,
1461 dio->flags);
1462 } else {
1463 ret = dio->error;
1466 if (likely(!ret)) {
1467 ret = dio->size;
1468 /* check for short read */
1469 if (offset + ret > dio->i_size &&
1470 !(dio->flags & IOMAP_DIO_WRITE))
1471 ret = dio->i_size - offset;
1472 iocb->ki_pos += ret;
1476 * Try again to invalidate clean pages which might have been cached by
1477 * non-direct readahead, or faulted in by get_user_pages() if the source
1478 * of the write was an mmap'ed region of the file we're writing. Either
1479 * one is a pretty crazy thing to do, so we don't support it 100%. If
1480 * this invalidation fails, tough, the write still worked...
1482 * And this page cache invalidation has to be after dio->end_io(), as
1483 * some filesystems convert unwritten extents to real allocations in
1484 * end_io() when necessary, otherwise a racing buffer read would cache
1485 * zeros from unwritten extents.
1487 if (!dio->error &&
1488 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
1489 int err;
1490 err = invalidate_inode_pages2_range(inode->i_mapping,
1491 offset >> PAGE_SHIFT,
1492 (offset + dio->size - 1) >> PAGE_SHIFT);
1493 if (err)
1494 dio_warn_stale_pagecache(iocb->ki_filp);
1498 * If this is a DSYNC write, make sure we push it to stable storage now
1499 * that we've written data.
1501 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
1502 ret = generic_write_sync(iocb, ret);
1504 inode_dio_end(file_inode(iocb->ki_filp));
1505 kfree(dio);
1507 return ret;
1510 static void iomap_dio_complete_work(struct work_struct *work)
1512 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
1513 struct kiocb *iocb = dio->iocb;
1515 iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
1519 * Set an error in the dio if none is set yet. We have to use cmpxchg
1520 * as the submission context and the completion context(s) can race to
1521 * update the error.
1523 static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
1525 cmpxchg(&dio->error, 0, ret);
1528 static void iomap_dio_bio_end_io(struct bio *bio)
1530 struct iomap_dio *dio = bio->bi_private;
1531 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
1533 if (bio->bi_status)
1534 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
1536 if (atomic_dec_and_test(&dio->ref)) {
1537 if (dio->wait_for_completion) {
1538 struct task_struct *waiter = dio->submit.waiter;
1539 WRITE_ONCE(dio->submit.waiter, NULL);
1540 wake_up_process(waiter);
1541 } else if (dio->flags & IOMAP_DIO_WRITE) {
1542 struct inode *inode = file_inode(dio->iocb->ki_filp);
1544 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
1545 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
1546 } else {
1547 iomap_dio_complete_work(&dio->aio.work);
1551 if (should_dirty) {
1552 bio_check_pages_dirty(bio);
1553 } else {
1554 struct bio_vec *bvec;
1555 int i;
1557 bio_for_each_segment_all(bvec, bio, i)
1558 put_page(bvec->bv_page);
1559 bio_put(bio);
1563 static blk_qc_t
1564 iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
1565 unsigned len)
1567 struct page *page = ZERO_PAGE(0);
1568 struct bio *bio;
1570 bio = bio_alloc(GFP_KERNEL, 1);
1571 bio_set_dev(bio, iomap->bdev);
1572 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
1573 bio->bi_private = dio;
1574 bio->bi_end_io = iomap_dio_bio_end_io;
1576 get_page(page);
1577 __bio_add_page(bio, page, len, 0);
1578 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE);
1580 atomic_inc(&dio->ref);
1581 return submit_bio(bio);
1584 static loff_t
1585 iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
1586 struct iomap_dio *dio, struct iomap *iomap)
1588 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
1589 unsigned int fs_block_size = i_blocksize(inode), pad;
1590 unsigned int align = iov_iter_alignment(dio->submit.iter);
1591 struct iov_iter iter;
1592 struct bio *bio;
1593 bool need_zeroout = false;
1594 bool use_fua = false;
1595 int nr_pages, ret = 0;
1596 size_t copied = 0;
1598 if ((pos | length | align) & ((1 << blkbits) - 1))
1599 return -EINVAL;
1601 if (iomap->type == IOMAP_UNWRITTEN) {
1602 dio->flags |= IOMAP_DIO_UNWRITTEN;
1603 need_zeroout = true;
1606 if (iomap->flags & IOMAP_F_SHARED)
1607 dio->flags |= IOMAP_DIO_COW;
1609 if (iomap->flags & IOMAP_F_NEW) {
1610 need_zeroout = true;
1611 } else if (iomap->type == IOMAP_MAPPED) {
1613 * Use a FUA write if we need datasync semantics, this is a pure
1614 * data IO that doesn't require any metadata updates (including
1615 * after IO completion such as unwritten extent conversion) and
1616 * the underlying device supports FUA. This allows us to avoid
1617 * cache flushes on IO completion.
1619 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
1620 (dio->flags & IOMAP_DIO_WRITE_FUA) &&
1621 blk_queue_fua(bdev_get_queue(iomap->bdev)))
1622 use_fua = true;
1626 * Operate on a partial iter trimmed to the extent we were called for.
1627 * We'll update the iter in the dio once we're done with this extent.
1629 iter = *dio->submit.iter;
1630 iov_iter_truncate(&iter, length);
1632 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
1633 if (nr_pages <= 0)
1634 return nr_pages;
1636 if (need_zeroout) {
1637 /* zero out from the start of the block to the write offset */
1638 pad = pos & (fs_block_size - 1);
1639 if (pad)
1640 iomap_dio_zero(dio, iomap, pos - pad, pad);
1643 do {
1644 size_t n;
1645 if (dio->error) {
1646 iov_iter_revert(dio->submit.iter, copied);
1647 return 0;
1650 bio = bio_alloc(GFP_KERNEL, nr_pages);
1651 bio_set_dev(bio, iomap->bdev);
1652 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
1653 bio->bi_write_hint = dio->iocb->ki_hint;
1654 bio->bi_ioprio = dio->iocb->ki_ioprio;
1655 bio->bi_private = dio;
1656 bio->bi_end_io = iomap_dio_bio_end_io;
1658 ret = bio_iov_iter_get_pages(bio, &iter);
1659 if (unlikely(ret)) {
1661 * We have to stop part way through an IO. We must fall
1662 * through to the sub-block tail zeroing here, otherwise
1663 * this short IO may expose stale data in the tail of
1664 * the block we haven't written data to.
1666 bio_put(bio);
1667 goto zero_tail;
1670 n = bio->bi_iter.bi_size;
1671 if (dio->flags & IOMAP_DIO_WRITE) {
1672 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
1673 if (use_fua)
1674 bio->bi_opf |= REQ_FUA;
1675 else
1676 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
1677 task_io_account_write(n);
1678 } else {
1679 bio->bi_opf = REQ_OP_READ;
1680 if (dio->flags & IOMAP_DIO_DIRTY)
1681 bio_set_pages_dirty(bio);
1684 iov_iter_advance(dio->submit.iter, n);
1686 dio->size += n;
1687 pos += n;
1688 copied += n;
1690 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
1692 atomic_inc(&dio->ref);
1694 dio->submit.last_queue = bdev_get_queue(iomap->bdev);
1695 dio->submit.cookie = submit_bio(bio);
1696 } while (nr_pages);
1699 * We need to zeroout the tail of a sub-block write if the extent type
1700 * requires zeroing or the write extends beyond EOF. If we don't zero
1701 * the block tail in the latter case, we can expose stale data via mmap
1702 * reads of the EOF block.
1704 zero_tail:
1705 if (need_zeroout ||
1706 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
1707 /* zero out from the end of the write to the end of the block */
1708 pad = pos & (fs_block_size - 1);
1709 if (pad)
1710 iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
1712 return copied ? copied : ret;
1715 static loff_t
1716 iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
1718 length = iov_iter_zero(length, dio->submit.iter);
1719 dio->size += length;
1720 return length;
1723 static loff_t
1724 iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
1725 struct iomap_dio *dio, struct iomap *iomap)
1727 struct iov_iter *iter = dio->submit.iter;
1728 size_t copied;
1730 BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
1732 if (dio->flags & IOMAP_DIO_WRITE) {
1733 loff_t size = inode->i_size;
1735 if (pos > size)
1736 memset(iomap->inline_data + size, 0, pos - size);
1737 copied = copy_from_iter(iomap->inline_data + pos, length, iter);
1738 if (copied) {
1739 if (pos + copied > size)
1740 i_size_write(inode, pos + copied);
1741 mark_inode_dirty(inode);
1743 } else {
1744 copied = copy_to_iter(iomap->inline_data + pos, length, iter);
1746 dio->size += copied;
1747 return copied;
1750 static loff_t
1751 iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
1752 void *data, struct iomap *iomap)
1754 struct iomap_dio *dio = data;
1756 switch (iomap->type) {
1757 case IOMAP_HOLE:
1758 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
1759 return -EIO;
1760 return iomap_dio_hole_actor(length, dio);
1761 case IOMAP_UNWRITTEN:
1762 if (!(dio->flags & IOMAP_DIO_WRITE))
1763 return iomap_dio_hole_actor(length, dio);
1764 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
1765 case IOMAP_MAPPED:
1766 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
1767 case IOMAP_INLINE:
1768 return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
1769 default:
1770 WARN_ON_ONCE(1);
1771 return -EIO;
1776 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
1777 * is being issued as AIO or not. This allows us to optimise pure data writes
1778 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
1779 * REQ_FLUSH post write. This is slightly tricky because a single request here
1780 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
1781 * may be pure data writes. In that case, we still need to do a full data sync
1782 * completion.
1784 ssize_t
1785 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
1786 const struct iomap_ops *ops, iomap_dio_end_io_t end_io)
1788 struct address_space *mapping = iocb->ki_filp->f_mapping;
1789 struct inode *inode = file_inode(iocb->ki_filp);
1790 size_t count = iov_iter_count(iter);
1791 loff_t pos = iocb->ki_pos, start = pos;
1792 loff_t end = iocb->ki_pos + count - 1, ret = 0;
1793 unsigned int flags = IOMAP_DIRECT;
1794 struct blk_plug plug;
1795 struct iomap_dio *dio;
1797 lockdep_assert_held(&inode->i_rwsem);
1799 if (!count)
1800 return 0;
1802 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
1803 if (!dio)
1804 return -ENOMEM;
1806 dio->iocb = iocb;
1807 atomic_set(&dio->ref, 1);
1808 dio->size = 0;
1809 dio->i_size = i_size_read(inode);
1810 dio->end_io = end_io;
1811 dio->error = 0;
1812 dio->flags = 0;
1813 dio->wait_for_completion = is_sync_kiocb(iocb);
1815 dio->submit.iter = iter;
1816 dio->submit.waiter = current;
1817 dio->submit.cookie = BLK_QC_T_NONE;
1818 dio->submit.last_queue = NULL;
1820 if (iov_iter_rw(iter) == READ) {
1821 if (pos >= dio->i_size)
1822 goto out_free_dio;
1824 if (iter_is_iovec(iter) && iov_iter_rw(iter) == READ)
1825 dio->flags |= IOMAP_DIO_DIRTY;
1826 } else {
1827 flags |= IOMAP_WRITE;
1828 dio->flags |= IOMAP_DIO_WRITE;
1830 /* for data sync or sync, we need sync completion processing */
1831 if (iocb->ki_flags & IOCB_DSYNC)
1832 dio->flags |= IOMAP_DIO_NEED_SYNC;
1835 * For datasync only writes, we optimistically try using FUA for
1836 * this IO. Any non-FUA write that occurs will clear this flag,
1837 * hence we know before completion whether a cache flush is
1838 * necessary.
1840 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
1841 dio->flags |= IOMAP_DIO_WRITE_FUA;
1844 if (iocb->ki_flags & IOCB_NOWAIT) {
1845 if (filemap_range_has_page(mapping, start, end)) {
1846 ret = -EAGAIN;
1847 goto out_free_dio;
1849 flags |= IOMAP_NOWAIT;
1852 ret = filemap_write_and_wait_range(mapping, start, end);
1853 if (ret)
1854 goto out_free_dio;
1857 * Try to invalidate cache pages for the range we're direct
1858 * writing. If this invalidation fails, tough, the write will
1859 * still work, but racing two incompatible write paths is a
1860 * pretty crazy thing to do, so we don't support it 100%.
1862 ret = invalidate_inode_pages2_range(mapping,
1863 start >> PAGE_SHIFT, end >> PAGE_SHIFT);
1864 if (ret)
1865 dio_warn_stale_pagecache(iocb->ki_filp);
1866 ret = 0;
1868 if (iov_iter_rw(iter) == WRITE && !dio->wait_for_completion &&
1869 !inode->i_sb->s_dio_done_wq) {
1870 ret = sb_init_dio_done_wq(inode->i_sb);
1871 if (ret < 0)
1872 goto out_free_dio;
1875 inode_dio_begin(inode);
1877 blk_start_plug(&plug);
1878 do {
1879 ret = iomap_apply(inode, pos, count, flags, ops, dio,
1880 iomap_dio_actor);
1881 if (ret <= 0) {
1882 /* magic error code to fall back to buffered I/O */
1883 if (ret == -ENOTBLK) {
1884 dio->wait_for_completion = true;
1885 ret = 0;
1887 break;
1889 pos += ret;
1891 if (iov_iter_rw(iter) == READ && pos >= dio->i_size)
1892 break;
1893 } while ((count = iov_iter_count(iter)) > 0);
1894 blk_finish_plug(&plug);
1896 if (ret < 0)
1897 iomap_dio_set_error(dio, ret);
1900 * If all the writes we issued were FUA, we don't need to flush the
1901 * cache on IO completion. Clear the sync flag for this case.
1903 if (dio->flags & IOMAP_DIO_WRITE_FUA)
1904 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
1906 if (!atomic_dec_and_test(&dio->ref)) {
1907 if (!dio->wait_for_completion)
1908 return -EIOCBQUEUED;
1910 for (;;) {
1911 set_current_state(TASK_UNINTERRUPTIBLE);
1912 if (!READ_ONCE(dio->submit.waiter))
1913 break;
1915 if (!(iocb->ki_flags & IOCB_HIPRI) ||
1916 !dio->submit.last_queue ||
1917 !blk_poll(dio->submit.last_queue,
1918 dio->submit.cookie))
1919 io_schedule();
1921 __set_current_state(TASK_RUNNING);
1924 ret = iomap_dio_complete(dio);
1926 return ret;
1928 out_free_dio:
1929 kfree(dio);
1930 return ret;
1932 EXPORT_SYMBOL_GPL(iomap_dio_rw);
1934 /* Swapfile activation */
1936 #ifdef CONFIG_SWAP
1937 struct iomap_swapfile_info {
1938 struct iomap iomap; /* accumulated iomap */
1939 struct swap_info_struct *sis;
1940 uint64_t lowest_ppage; /* lowest physical addr seen (pages) */
1941 uint64_t highest_ppage; /* highest physical addr seen (pages) */
1942 unsigned long nr_pages; /* number of pages collected */
1943 int nr_extents; /* extent count */
1947 * Collect physical extents for this swap file. Physical extents reported to
1948 * the swap code must be trimmed to align to a page boundary. The logical
1949 * offset within the file is irrelevant since the swapfile code maps logical
1950 * page numbers of the swap device to the physical page-aligned extents.
1952 static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi)
1954 struct iomap *iomap = &isi->iomap;
1955 unsigned long nr_pages;
1956 uint64_t first_ppage;
1957 uint64_t first_ppage_reported;
1958 uint64_t next_ppage;
1959 int error;
1962 * Round the start up and the end down so that the physical
1963 * extent aligns to a page boundary.
1965 first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT;
1966 next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >>
1967 PAGE_SHIFT;
1969 /* Skip too-short physical extents. */
1970 if (first_ppage >= next_ppage)
1971 return 0;
1972 nr_pages = next_ppage - first_ppage;
1975 * Calculate how much swap space we're adding; the first page contains
1976 * the swap header and doesn't count. The mm still wants that first
1977 * page fed to add_swap_extent, however.
1979 first_ppage_reported = first_ppage;
1980 if (iomap->offset == 0)
1981 first_ppage_reported++;
1982 if (isi->lowest_ppage > first_ppage_reported)
1983 isi->lowest_ppage = first_ppage_reported;
1984 if (isi->highest_ppage < (next_ppage - 1))
1985 isi->highest_ppage = next_ppage - 1;
1987 /* Add extent, set up for the next call. */
1988 error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage);
1989 if (error < 0)
1990 return error;
1991 isi->nr_extents += error;
1992 isi->nr_pages += nr_pages;
1993 return 0;
1997 * Accumulate iomaps for this swap file. We have to accumulate iomaps because
1998 * swap only cares about contiguous page-aligned physical extents and makes no
1999 * distinction between written and unwritten extents.
2001 static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
2002 loff_t count, void *data, struct iomap *iomap)
2004 struct iomap_swapfile_info *isi = data;
2005 int error;
2007 switch (iomap->type) {
2008 case IOMAP_MAPPED:
2009 case IOMAP_UNWRITTEN:
2010 /* Only real or unwritten extents. */
2011 break;
2012 case IOMAP_INLINE:
2013 /* No inline data. */
2014 pr_err("swapon: file is inline\n");
2015 return -EINVAL;
2016 default:
2017 pr_err("swapon: file has unallocated extents\n");
2018 return -EINVAL;
2021 /* No uncommitted metadata or shared blocks. */
2022 if (iomap->flags & IOMAP_F_DIRTY) {
2023 pr_err("swapon: file is not committed\n");
2024 return -EINVAL;
2026 if (iomap->flags & IOMAP_F_SHARED) {
2027 pr_err("swapon: file has shared extents\n");
2028 return -EINVAL;
2031 /* Only one bdev per swap file. */
2032 if (iomap->bdev != isi->sis->bdev) {
2033 pr_err("swapon: file is on multiple devices\n");
2034 return -EINVAL;
2037 if (isi->iomap.length == 0) {
2038 /* No accumulated extent, so just store it. */
2039 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
2040 } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
2041 /* Append this to the accumulated extent. */
2042 isi->iomap.length += iomap->length;
2043 } else {
2044 /* Otherwise, add the retained iomap and store this one. */
2045 error = iomap_swapfile_add_extent(isi);
2046 if (error)
2047 return error;
2048 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
2050 return count;
2054 * Iterate a swap file's iomaps to construct physical extents that can be
2055 * passed to the swapfile subsystem.
2057 int iomap_swapfile_activate(struct swap_info_struct *sis,
2058 struct file *swap_file, sector_t *pagespan,
2059 const struct iomap_ops *ops)
2061 struct iomap_swapfile_info isi = {
2062 .sis = sis,
2063 .lowest_ppage = (sector_t)-1ULL,
2065 struct address_space *mapping = swap_file->f_mapping;
2066 struct inode *inode = mapping->host;
2067 loff_t pos = 0;
2068 loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE);
2069 loff_t ret;
2072 * Persist all file mapping metadata so that we won't have any
2073 * IOMAP_F_DIRTY iomaps.
2075 ret = vfs_fsync(swap_file, 1);
2076 if (ret)
2077 return ret;
2079 while (len > 0) {
2080 ret = iomap_apply(inode, pos, len, IOMAP_REPORT,
2081 ops, &isi, iomap_swapfile_activate_actor);
2082 if (ret <= 0)
2083 return ret;
2085 pos += ret;
2086 len -= ret;
2089 if (isi.iomap.length) {
2090 ret = iomap_swapfile_add_extent(&isi);
2091 if (ret)
2092 return ret;
2095 *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage;
2096 sis->max = isi.nr_pages;
2097 sis->pages = isi.nr_pages - 1;
2098 sis->highest_bit = isi.nr_pages - 1;
2099 return isi.nr_extents;
2101 EXPORT_SYMBOL_GPL(iomap_swapfile_activate);
2102 #endif /* CONFIG_SWAP */
2104 static loff_t
2105 iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
2106 void *data, struct iomap *iomap)
2108 sector_t *bno = data, addr;
2110 if (iomap->type == IOMAP_MAPPED) {
2111 addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
2112 if (addr > INT_MAX)
2113 WARN(1, "would truncate bmap result\n");
2114 else
2115 *bno = addr;
2117 return 0;
2120 /* legacy ->bmap interface. 0 is the error return (!) */
2121 sector_t
2122 iomap_bmap(struct address_space *mapping, sector_t bno,
2123 const struct iomap_ops *ops)
2125 struct inode *inode = mapping->host;
2126 loff_t pos = bno << inode->i_blkbits;
2127 unsigned blocksize = i_blocksize(inode);
2129 if (filemap_write_and_wait(mapping))
2130 return 0;
2132 bno = 0;
2133 iomap_apply(inode, pos, blocksize, 0, ops, &bno, iomap_bmap_actor);
2134 return bno;
2136 EXPORT_SYMBOL_GPL(iomap_bmap);