bnxt_en: Reset rings if ring reservation fails during open()
[linux/fpc-iii.git] / fs / iomap.c
blob03edf62633dcc4d0a6160b8a0f0874dc831374fd
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>
33 #include <linux/swap.h>
35 #include "internal.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
46 * iomap_end call.
48 loff_t
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);
68 if (ret)
69 return ret;
70 if (WARN_ON(iomap.offset > pos))
71 return -EIO;
72 if (WARN_ON(iomap.length == 0))
73 return -EIO;
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.
93 if (ops->iomap_end) {
94 ret = ops->iomap_end(inode, pos, length,
95 written > 0 ? written : 0,
96 flags, &iomap);
99 return written ? written : ret;
102 static sector_t
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)
114 return iop;
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);
122 * migrate_page_move_mapping() assumes that pages with private data have
123 * their count elevated by 1.
125 get_page(page);
126 set_page_private(page, (unsigned long)iop);
127 SetPagePrivate(page);
128 return iop;
131 static void
132 iomap_page_release(struct page *page)
134 struct iomap_page *iop = to_iomap_page(page);
136 if (!iop)
137 return;
138 WARN_ON_ONCE(atomic_read(&iop->read_count));
139 WARN_ON_ONCE(atomic_read(&iop->write_count));
140 ClearPagePrivate(page);
141 set_page_private(page, 0);
142 put_page(page);
143 kfree(iop);
147 * Calculate the range inside the page that we actually need to read.
149 static void
150 iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
151 loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
153 loff_t orig_pos = *pos;
154 loff_t isize = i_size_read(inode);
155 unsigned block_bits = inode->i_blkbits;
156 unsigned block_size = (1 << block_bits);
157 unsigned poff = offset_in_page(*pos);
158 unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
159 unsigned first = poff >> block_bits;
160 unsigned last = (poff + plen - 1) >> block_bits;
163 * If the block size is smaller than the page size we need to check the
164 * per-block uptodate status and adjust the offset and length if needed
165 * to avoid reading in already uptodate ranges.
167 if (iop) {
168 unsigned int i;
170 /* move forward for each leading block marked uptodate */
171 for (i = first; i <= last; i++) {
172 if (!test_bit(i, iop->uptodate))
173 break;
174 *pos += block_size;
175 poff += block_size;
176 plen -= block_size;
177 first++;
180 /* truncate len if we find any trailing uptodate block(s) */
181 for ( ; i <= last; i++) {
182 if (test_bit(i, iop->uptodate)) {
183 plen -= (last - i + 1) * block_size;
184 last = i - 1;
185 break;
191 * If the extent spans the block that contains the i_size we need to
192 * handle both halves separately so that we properly zero data in the
193 * page cache for blocks that are entirely outside of i_size.
195 if (orig_pos <= isize && orig_pos + length > isize) {
196 unsigned end = offset_in_page(isize - 1) >> block_bits;
198 if (first <= end && last > end)
199 plen -= (last - end) * block_size;
202 *offp = poff;
203 *lenp = plen;
206 static void
207 iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
209 struct iomap_page *iop = to_iomap_page(page);
210 struct inode *inode = page->mapping->host;
211 unsigned first = off >> inode->i_blkbits;
212 unsigned last = (off + len - 1) >> inode->i_blkbits;
213 unsigned int i;
214 bool uptodate = true;
216 if (iop) {
217 for (i = 0; i < PAGE_SIZE / i_blocksize(inode); i++) {
218 if (i >= first && i <= last)
219 set_bit(i, iop->uptodate);
220 else if (!test_bit(i, iop->uptodate))
221 uptodate = false;
225 if (uptodate && !PageError(page))
226 SetPageUptodate(page);
229 static void
230 iomap_read_finish(struct iomap_page *iop, struct page *page)
232 if (!iop || atomic_dec_and_test(&iop->read_count))
233 unlock_page(page);
236 static void
237 iomap_read_page_end_io(struct bio_vec *bvec, int error)
239 struct page *page = bvec->bv_page;
240 struct iomap_page *iop = to_iomap_page(page);
242 if (unlikely(error)) {
243 ClearPageUptodate(page);
244 SetPageError(page);
245 } else {
246 iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
249 iomap_read_finish(iop, page);
252 static void
253 iomap_read_inline_data(struct inode *inode, struct page *page,
254 struct iomap *iomap)
256 size_t size = i_size_read(inode);
257 void *addr;
259 if (PageUptodate(page))
260 return;
262 BUG_ON(page->index);
263 BUG_ON(size > PAGE_SIZE - offset_in_page(iomap->inline_data));
265 addr = kmap_atomic(page);
266 memcpy(addr, iomap->inline_data, size);
267 memset(addr + size, 0, PAGE_SIZE - size);
268 kunmap_atomic(addr);
269 SetPageUptodate(page);
272 static void
273 iomap_read_end_io(struct bio *bio)
275 int error = blk_status_to_errno(bio->bi_status);
276 struct bio_vec *bvec;
277 int i;
279 bio_for_each_segment_all(bvec, bio, i)
280 iomap_read_page_end_io(bvec, error);
281 bio_put(bio);
284 struct iomap_readpage_ctx {
285 struct page *cur_page;
286 bool cur_page_in_bio;
287 bool is_readahead;
288 struct bio *bio;
289 struct list_head *pages;
292 static loff_t
293 iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
294 struct iomap *iomap)
296 struct iomap_readpage_ctx *ctx = data;
297 struct page *page = ctx->cur_page;
298 struct iomap_page *iop = iomap_page_create(inode, page);
299 bool is_contig = false;
300 loff_t orig_pos = pos;
301 unsigned poff, plen;
302 sector_t sector;
304 if (iomap->type == IOMAP_INLINE) {
305 WARN_ON_ONCE(pos);
306 iomap_read_inline_data(inode, page, iomap);
307 return PAGE_SIZE;
310 /* zero post-eof blocks as the page may be mapped */
311 iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
312 if (plen == 0)
313 goto done;
315 if (iomap->type != IOMAP_MAPPED || pos >= i_size_read(inode)) {
316 zero_user(page, poff, plen);
317 iomap_set_range_uptodate(page, poff, plen);
318 goto done;
321 ctx->cur_page_in_bio = true;
324 * Try to merge into a previous segment if we can.
326 sector = iomap_sector(iomap, pos);
327 if (ctx->bio && bio_end_sector(ctx->bio) == sector) {
328 if (__bio_try_merge_page(ctx->bio, page, plen, poff))
329 goto done;
330 is_contig = true;
334 * If we start a new segment we need to increase the read count, and we
335 * need to do so before submitting any previous full bio to make sure
336 * that we don't prematurely unlock the page.
338 if (iop)
339 atomic_inc(&iop->read_count);
341 if (!ctx->bio || !is_contig || bio_full(ctx->bio)) {
342 gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
343 int nr_vecs = (length + PAGE_SIZE - 1) >> PAGE_SHIFT;
345 if (ctx->bio)
346 submit_bio(ctx->bio);
348 if (ctx->is_readahead) /* same as readahead_gfp_mask */
349 gfp |= __GFP_NORETRY | __GFP_NOWARN;
350 ctx->bio = bio_alloc(gfp, min(BIO_MAX_PAGES, nr_vecs));
351 ctx->bio->bi_opf = REQ_OP_READ;
352 if (ctx->is_readahead)
353 ctx->bio->bi_opf |= REQ_RAHEAD;
354 ctx->bio->bi_iter.bi_sector = sector;
355 bio_set_dev(ctx->bio, iomap->bdev);
356 ctx->bio->bi_end_io = iomap_read_end_io;
359 __bio_add_page(ctx->bio, page, plen, poff);
360 done:
362 * Move the caller beyond our range so that it keeps making progress.
363 * For that we have to include any leading non-uptodate ranges, but
364 * we can skip trailing ones as they will be handled in the next
365 * iteration.
367 return pos - orig_pos + plen;
371 iomap_readpage(struct page *page, const struct iomap_ops *ops)
373 struct iomap_readpage_ctx ctx = { .cur_page = page };
374 struct inode *inode = page->mapping->host;
375 unsigned poff;
376 loff_t ret;
378 for (poff = 0; poff < PAGE_SIZE; poff += ret) {
379 ret = iomap_apply(inode, page_offset(page) + poff,
380 PAGE_SIZE - poff, 0, ops, &ctx,
381 iomap_readpage_actor);
382 if (ret <= 0) {
383 WARN_ON_ONCE(ret == 0);
384 SetPageError(page);
385 break;
389 if (ctx.bio) {
390 submit_bio(ctx.bio);
391 WARN_ON_ONCE(!ctx.cur_page_in_bio);
392 } else {
393 WARN_ON_ONCE(ctx.cur_page_in_bio);
394 unlock_page(page);
398 * Just like mpage_readpages and block_read_full_page we always
399 * return 0 and just mark the page as PageError on errors. This
400 * should be cleaned up all through the stack eventually.
402 return 0;
404 EXPORT_SYMBOL_GPL(iomap_readpage);
406 static struct page *
407 iomap_next_page(struct inode *inode, struct list_head *pages, loff_t pos,
408 loff_t length, loff_t *done)
410 while (!list_empty(pages)) {
411 struct page *page = lru_to_page(pages);
413 if (page_offset(page) >= (u64)pos + length)
414 break;
416 list_del(&page->lru);
417 if (!add_to_page_cache_lru(page, inode->i_mapping, page->index,
418 GFP_NOFS))
419 return page;
422 * If we already have a page in the page cache at index we are
423 * done. Upper layers don't care if it is uptodate after the
424 * readpages call itself as every page gets checked again once
425 * actually needed.
427 *done += PAGE_SIZE;
428 put_page(page);
431 return NULL;
434 static loff_t
435 iomap_readpages_actor(struct inode *inode, loff_t pos, loff_t length,
436 void *data, struct iomap *iomap)
438 struct iomap_readpage_ctx *ctx = data;
439 loff_t done, ret;
441 for (done = 0; done < length; done += ret) {
442 if (ctx->cur_page && offset_in_page(pos + done) == 0) {
443 if (!ctx->cur_page_in_bio)
444 unlock_page(ctx->cur_page);
445 put_page(ctx->cur_page);
446 ctx->cur_page = NULL;
448 if (!ctx->cur_page) {
449 ctx->cur_page = iomap_next_page(inode, ctx->pages,
450 pos, length, &done);
451 if (!ctx->cur_page)
452 break;
453 ctx->cur_page_in_bio = false;
455 ret = iomap_readpage_actor(inode, pos + done, length - done,
456 ctx, iomap);
459 return done;
463 iomap_readpages(struct address_space *mapping, struct list_head *pages,
464 unsigned nr_pages, const struct iomap_ops *ops)
466 struct iomap_readpage_ctx ctx = {
467 .pages = pages,
468 .is_readahead = true,
470 loff_t pos = page_offset(list_entry(pages->prev, struct page, lru));
471 loff_t last = page_offset(list_entry(pages->next, struct page, lru));
472 loff_t length = last - pos + PAGE_SIZE, ret = 0;
474 while (length > 0) {
475 ret = iomap_apply(mapping->host, pos, length, 0, ops,
476 &ctx, iomap_readpages_actor);
477 if (ret <= 0) {
478 WARN_ON_ONCE(ret == 0);
479 goto done;
481 pos += ret;
482 length -= ret;
484 ret = 0;
485 done:
486 if (ctx.bio)
487 submit_bio(ctx.bio);
488 if (ctx.cur_page) {
489 if (!ctx.cur_page_in_bio)
490 unlock_page(ctx.cur_page);
491 put_page(ctx.cur_page);
495 * Check that we didn't lose a page due to the arcance calling
496 * conventions..
498 WARN_ON_ONCE(!ret && !list_empty(ctx.pages));
499 return ret;
501 EXPORT_SYMBOL_GPL(iomap_readpages);
504 * iomap_is_partially_uptodate checks whether blocks within a page are
505 * uptodate or not.
507 * Returns true if all blocks which correspond to a file portion
508 * we want to read within the page are uptodate.
511 iomap_is_partially_uptodate(struct page *page, unsigned long from,
512 unsigned long count)
514 struct iomap_page *iop = to_iomap_page(page);
515 struct inode *inode = page->mapping->host;
516 unsigned len, first, last;
517 unsigned i;
519 /* Limit range to one page */
520 len = min_t(unsigned, PAGE_SIZE - from, count);
522 /* First and last blocks in range within page */
523 first = from >> inode->i_blkbits;
524 last = (from + len - 1) >> inode->i_blkbits;
526 if (iop) {
527 for (i = first; i <= last; i++)
528 if (!test_bit(i, iop->uptodate))
529 return 0;
530 return 1;
533 return 0;
535 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
538 iomap_releasepage(struct page *page, gfp_t gfp_mask)
541 * mm accommodates an old ext3 case where clean pages might not have had
542 * the dirty bit cleared. Thus, it can send actual dirty pages to
543 * ->releasepage() via shrink_active_list(), skip those here.
545 if (PageDirty(page) || PageWriteback(page))
546 return 0;
547 iomap_page_release(page);
548 return 1;
550 EXPORT_SYMBOL_GPL(iomap_releasepage);
552 void
553 iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
556 * If we are invalidating the entire page, clear the dirty state from it
557 * and release it to avoid unnecessary buildup of the LRU.
559 if (offset == 0 && len == PAGE_SIZE) {
560 WARN_ON_ONCE(PageWriteback(page));
561 cancel_dirty_page(page);
562 iomap_page_release(page);
565 EXPORT_SYMBOL_GPL(iomap_invalidatepage);
567 #ifdef CONFIG_MIGRATION
569 iomap_migrate_page(struct address_space *mapping, struct page *newpage,
570 struct page *page, enum migrate_mode mode)
572 int ret;
574 ret = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
575 if (ret != MIGRATEPAGE_SUCCESS)
576 return ret;
578 if (page_has_private(page)) {
579 ClearPagePrivate(page);
580 get_page(newpage);
581 set_page_private(newpage, page_private(page));
582 set_page_private(page, 0);
583 put_page(page);
584 SetPagePrivate(newpage);
587 if (mode != MIGRATE_SYNC_NO_COPY)
588 migrate_page_copy(newpage, page);
589 else
590 migrate_page_states(newpage, page);
591 return MIGRATEPAGE_SUCCESS;
593 EXPORT_SYMBOL_GPL(iomap_migrate_page);
594 #endif /* CONFIG_MIGRATION */
596 static void
597 iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
599 loff_t i_size = i_size_read(inode);
602 * Only truncate newly allocated pages beyoned EOF, even if the
603 * write started inside the existing inode size.
605 if (pos + len > i_size)
606 truncate_pagecache_range(inode, max(pos, i_size), pos + len);
609 static int
610 iomap_read_page_sync(struct inode *inode, loff_t block_start, struct page *page,
611 unsigned poff, unsigned plen, unsigned from, unsigned to,
612 struct iomap *iomap)
614 struct bio_vec bvec;
615 struct bio bio;
617 if (iomap->type != IOMAP_MAPPED || block_start >= i_size_read(inode)) {
618 zero_user_segments(page, poff, from, to, poff + plen);
619 iomap_set_range_uptodate(page, poff, plen);
620 return 0;
623 bio_init(&bio, &bvec, 1);
624 bio.bi_opf = REQ_OP_READ;
625 bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
626 bio_set_dev(&bio, iomap->bdev);
627 __bio_add_page(&bio, page, plen, poff);
628 return submit_bio_wait(&bio);
631 static int
632 __iomap_write_begin(struct inode *inode, loff_t pos, unsigned len,
633 struct page *page, struct iomap *iomap)
635 struct iomap_page *iop = iomap_page_create(inode, page);
636 loff_t block_size = i_blocksize(inode);
637 loff_t block_start = pos & ~(block_size - 1);
638 loff_t block_end = (pos + len + block_size - 1) & ~(block_size - 1);
639 unsigned from = offset_in_page(pos), to = from + len, poff, plen;
640 int status = 0;
642 if (PageUptodate(page))
643 return 0;
645 do {
646 iomap_adjust_read_range(inode, iop, &block_start,
647 block_end - block_start, &poff, &plen);
648 if (plen == 0)
649 break;
651 if ((from > poff && from < poff + plen) ||
652 (to > poff && to < poff + plen)) {
653 status = iomap_read_page_sync(inode, block_start, page,
654 poff, plen, from, to, iomap);
655 if (status)
656 break;
659 } while ((block_start += plen) < block_end);
661 return status;
664 static int
665 iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
666 struct page **pagep, struct iomap *iomap)
668 pgoff_t index = pos >> PAGE_SHIFT;
669 struct page *page;
670 int status = 0;
672 BUG_ON(pos + len > iomap->offset + iomap->length);
674 if (fatal_signal_pending(current))
675 return -EINTR;
677 page = grab_cache_page_write_begin(inode->i_mapping, index, flags);
678 if (!page)
679 return -ENOMEM;
681 if (iomap->type == IOMAP_INLINE)
682 iomap_read_inline_data(inode, page, iomap);
683 else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
684 status = __block_write_begin_int(page, pos, len, NULL, iomap);
685 else
686 status = __iomap_write_begin(inode, pos, len, page, iomap);
687 if (unlikely(status)) {
688 unlock_page(page);
689 put_page(page);
690 page = NULL;
692 iomap_write_failed(inode, pos, len);
695 *pagep = page;
696 return status;
700 iomap_set_page_dirty(struct page *page)
702 struct address_space *mapping = page_mapping(page);
703 int newly_dirty;
705 if (unlikely(!mapping))
706 return !TestSetPageDirty(page);
709 * Lock out page->mem_cgroup migration to keep PageDirty
710 * synchronized with per-memcg dirty page counters.
712 lock_page_memcg(page);
713 newly_dirty = !TestSetPageDirty(page);
714 if (newly_dirty)
715 __set_page_dirty(page, mapping, 0);
716 unlock_page_memcg(page);
718 if (newly_dirty)
719 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
720 return newly_dirty;
722 EXPORT_SYMBOL_GPL(iomap_set_page_dirty);
724 static int
725 __iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
726 unsigned copied, struct page *page, struct iomap *iomap)
728 flush_dcache_page(page);
731 * The blocks that were entirely written will now be uptodate, so we
732 * don't have to worry about a readpage reading them and overwriting a
733 * partial write. However if we have encountered a short write and only
734 * partially written into a block, it will not be marked uptodate, so a
735 * readpage might come in and destroy our partial write.
737 * Do the simplest thing, and just treat any short write to a non
738 * uptodate page as a zero-length write, and force the caller to redo
739 * the whole thing.
741 if (unlikely(copied < len && !PageUptodate(page))) {
742 copied = 0;
743 } else {
744 iomap_set_range_uptodate(page, offset_in_page(pos), len);
745 iomap_set_page_dirty(page);
747 return __generic_write_end(inode, pos, copied, page);
750 static int
751 iomap_write_end_inline(struct inode *inode, struct page *page,
752 struct iomap *iomap, loff_t pos, unsigned copied)
754 void *addr;
756 WARN_ON_ONCE(!PageUptodate(page));
757 BUG_ON(pos + copied > PAGE_SIZE - offset_in_page(iomap->inline_data));
759 addr = kmap_atomic(page);
760 memcpy(iomap->inline_data + pos, addr + pos, copied);
761 kunmap_atomic(addr);
763 mark_inode_dirty(inode);
764 __generic_write_end(inode, pos, copied, page);
765 return copied;
768 static int
769 iomap_write_end(struct inode *inode, loff_t pos, unsigned len,
770 unsigned copied, struct page *page, struct iomap *iomap)
772 int ret;
774 if (iomap->type == IOMAP_INLINE) {
775 ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
776 } else if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
777 ret = generic_write_end(NULL, inode->i_mapping, pos, len,
778 copied, page, NULL);
779 } else {
780 ret = __iomap_write_end(inode, pos, len, copied, page, iomap);
783 if (iomap->page_done)
784 iomap->page_done(inode, pos, copied, page, iomap);
786 if (ret < len)
787 iomap_write_failed(inode, pos, len);
788 return ret;
791 static loff_t
792 iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
793 struct iomap *iomap)
795 struct iov_iter *i = data;
796 long status = 0;
797 ssize_t written = 0;
798 unsigned int flags = AOP_FLAG_NOFS;
800 do {
801 struct page *page;
802 unsigned long offset; /* Offset into pagecache page */
803 unsigned long bytes; /* Bytes to write to page */
804 size_t copied; /* Bytes copied from user */
806 offset = offset_in_page(pos);
807 bytes = min_t(unsigned long, PAGE_SIZE - offset,
808 iov_iter_count(i));
809 again:
810 if (bytes > length)
811 bytes = length;
814 * Bring in the user page that we will copy from _first_.
815 * Otherwise there's a nasty deadlock on copying from the
816 * same page as we're writing to, without it being marked
817 * up-to-date.
819 * Not only is this an optimisation, but it is also required
820 * to check that the address is actually valid, when atomic
821 * usercopies are used, below.
823 if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
824 status = -EFAULT;
825 break;
828 status = iomap_write_begin(inode, pos, bytes, flags, &page,
829 iomap);
830 if (unlikely(status))
831 break;
833 if (mapping_writably_mapped(inode->i_mapping))
834 flush_dcache_page(page);
836 copied = iov_iter_copy_from_user_atomic(page, i, offset, bytes);
838 flush_dcache_page(page);
840 status = iomap_write_end(inode, pos, bytes, copied, page,
841 iomap);
842 if (unlikely(status < 0))
843 break;
844 copied = status;
846 cond_resched();
848 iov_iter_advance(i, copied);
849 if (unlikely(copied == 0)) {
851 * If we were unable to copy any data at all, we must
852 * fall back to a single segment length write.
854 * If we didn't fallback here, we could livelock
855 * because not all segments in the iov can be copied at
856 * once without a pagefault.
858 bytes = min_t(unsigned long, PAGE_SIZE - offset,
859 iov_iter_single_seg_count(i));
860 goto again;
862 pos += copied;
863 written += copied;
864 length -= copied;
866 balance_dirty_pages_ratelimited(inode->i_mapping);
867 } while (iov_iter_count(i) && length);
869 return written ? written : status;
872 ssize_t
873 iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
874 const struct iomap_ops *ops)
876 struct inode *inode = iocb->ki_filp->f_mapping->host;
877 loff_t pos = iocb->ki_pos, ret = 0, written = 0;
879 while (iov_iter_count(iter)) {
880 ret = iomap_apply(inode, pos, iov_iter_count(iter),
881 IOMAP_WRITE, ops, iter, iomap_write_actor);
882 if (ret <= 0)
883 break;
884 pos += ret;
885 written += ret;
888 return written ? written : ret;
890 EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
892 static struct page *
893 __iomap_read_page(struct inode *inode, loff_t offset)
895 struct address_space *mapping = inode->i_mapping;
896 struct page *page;
898 page = read_mapping_page(mapping, offset >> PAGE_SHIFT, NULL);
899 if (IS_ERR(page))
900 return page;
901 if (!PageUptodate(page)) {
902 put_page(page);
903 return ERR_PTR(-EIO);
905 return page;
908 static loff_t
909 iomap_dirty_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
910 struct iomap *iomap)
912 long status = 0;
913 ssize_t written = 0;
915 do {
916 struct page *page, *rpage;
917 unsigned long offset; /* Offset into pagecache page */
918 unsigned long bytes; /* Bytes to write to page */
920 offset = offset_in_page(pos);
921 bytes = min_t(loff_t, PAGE_SIZE - offset, length);
923 rpage = __iomap_read_page(inode, pos);
924 if (IS_ERR(rpage))
925 return PTR_ERR(rpage);
927 status = iomap_write_begin(inode, pos, bytes,
928 AOP_FLAG_NOFS, &page, iomap);
929 put_page(rpage);
930 if (unlikely(status))
931 return status;
933 WARN_ON_ONCE(!PageUptodate(page));
935 status = iomap_write_end(inode, pos, bytes, bytes, page, iomap);
936 if (unlikely(status <= 0)) {
937 if (WARN_ON_ONCE(status == 0))
938 return -EIO;
939 return status;
942 cond_resched();
944 pos += status;
945 written += status;
946 length -= status;
948 balance_dirty_pages_ratelimited(inode->i_mapping);
949 } while (length);
951 return written;
955 iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
956 const struct iomap_ops *ops)
958 loff_t ret;
960 while (len) {
961 ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
962 iomap_dirty_actor);
963 if (ret <= 0)
964 return ret;
965 pos += ret;
966 len -= ret;
969 return 0;
971 EXPORT_SYMBOL_GPL(iomap_file_dirty);
973 static int iomap_zero(struct inode *inode, loff_t pos, unsigned offset,
974 unsigned bytes, struct iomap *iomap)
976 struct page *page;
977 int status;
979 status = iomap_write_begin(inode, pos, bytes, AOP_FLAG_NOFS, &page,
980 iomap);
981 if (status)
982 return status;
984 zero_user(page, offset, bytes);
985 mark_page_accessed(page);
987 return iomap_write_end(inode, pos, bytes, bytes, page, iomap);
990 static int iomap_dax_zero(loff_t pos, unsigned offset, unsigned bytes,
991 struct iomap *iomap)
993 return __dax_zero_page_range(iomap->bdev, iomap->dax_dev,
994 iomap_sector(iomap, pos & PAGE_MASK), offset, bytes);
997 static loff_t
998 iomap_zero_range_actor(struct inode *inode, loff_t pos, loff_t count,
999 void *data, struct iomap *iomap)
1001 bool *did_zero = data;
1002 loff_t written = 0;
1003 int status;
1005 /* already zeroed? we're done. */
1006 if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
1007 return count;
1009 do {
1010 unsigned offset, bytes;
1012 offset = offset_in_page(pos);
1013 bytes = min_t(loff_t, PAGE_SIZE - offset, count);
1015 if (IS_DAX(inode))
1016 status = iomap_dax_zero(pos, offset, bytes, iomap);
1017 else
1018 status = iomap_zero(inode, pos, offset, bytes, iomap);
1019 if (status < 0)
1020 return status;
1022 pos += bytes;
1023 count -= bytes;
1024 written += bytes;
1025 if (did_zero)
1026 *did_zero = true;
1027 } while (count > 0);
1029 return written;
1033 iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1034 const struct iomap_ops *ops)
1036 loff_t ret;
1038 while (len > 0) {
1039 ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
1040 ops, did_zero, iomap_zero_range_actor);
1041 if (ret <= 0)
1042 return ret;
1044 pos += ret;
1045 len -= ret;
1048 return 0;
1050 EXPORT_SYMBOL_GPL(iomap_zero_range);
1053 iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1054 const struct iomap_ops *ops)
1056 unsigned int blocksize = i_blocksize(inode);
1057 unsigned int off = pos & (blocksize - 1);
1059 /* Block boundary? Nothing to do */
1060 if (!off)
1061 return 0;
1062 return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1064 EXPORT_SYMBOL_GPL(iomap_truncate_page);
1066 static loff_t
1067 iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
1068 void *data, struct iomap *iomap)
1070 struct page *page = data;
1071 int ret;
1073 if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
1074 ret = __block_write_begin_int(page, pos, length, NULL, iomap);
1075 if (ret)
1076 return ret;
1077 block_commit_write(page, 0, length);
1078 } else {
1079 WARN_ON_ONCE(!PageUptodate(page));
1080 iomap_page_create(inode, page);
1081 set_page_dirty(page);
1084 return length;
1087 int iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1089 struct page *page = vmf->page;
1090 struct inode *inode = file_inode(vmf->vma->vm_file);
1091 unsigned long length;
1092 loff_t offset, size;
1093 ssize_t ret;
1095 lock_page(page);
1096 size = i_size_read(inode);
1097 if ((page->mapping != inode->i_mapping) ||
1098 (page_offset(page) > size)) {
1099 /* We overload EFAULT to mean page got truncated */
1100 ret = -EFAULT;
1101 goto out_unlock;
1104 /* page is wholly or partially inside EOF */
1105 if (((page->index + 1) << PAGE_SHIFT) > size)
1106 length = offset_in_page(size);
1107 else
1108 length = PAGE_SIZE;
1110 offset = page_offset(page);
1111 while (length > 0) {
1112 ret = iomap_apply(inode, offset, length,
1113 IOMAP_WRITE | IOMAP_FAULT, ops, page,
1114 iomap_page_mkwrite_actor);
1115 if (unlikely(ret <= 0))
1116 goto out_unlock;
1117 offset += ret;
1118 length -= ret;
1121 wait_for_stable_page(page);
1122 return VM_FAULT_LOCKED;
1123 out_unlock:
1124 unlock_page(page);
1125 return block_page_mkwrite_return(ret);
1127 EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1129 struct fiemap_ctx {
1130 struct fiemap_extent_info *fi;
1131 struct iomap prev;
1134 static int iomap_to_fiemap(struct fiemap_extent_info *fi,
1135 struct iomap *iomap, u32 flags)
1137 switch (iomap->type) {
1138 case IOMAP_HOLE:
1139 /* skip holes */
1140 return 0;
1141 case IOMAP_DELALLOC:
1142 flags |= FIEMAP_EXTENT_DELALLOC | FIEMAP_EXTENT_UNKNOWN;
1143 break;
1144 case IOMAP_MAPPED:
1145 break;
1146 case IOMAP_UNWRITTEN:
1147 flags |= FIEMAP_EXTENT_UNWRITTEN;
1148 break;
1149 case IOMAP_INLINE:
1150 flags |= FIEMAP_EXTENT_DATA_INLINE;
1151 break;
1154 if (iomap->flags & IOMAP_F_MERGED)
1155 flags |= FIEMAP_EXTENT_MERGED;
1156 if (iomap->flags & IOMAP_F_SHARED)
1157 flags |= FIEMAP_EXTENT_SHARED;
1159 return fiemap_fill_next_extent(fi, iomap->offset,
1160 iomap->addr != IOMAP_NULL_ADDR ? iomap->addr : 0,
1161 iomap->length, flags);
1164 static loff_t
1165 iomap_fiemap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
1166 struct iomap *iomap)
1168 struct fiemap_ctx *ctx = data;
1169 loff_t ret = length;
1171 if (iomap->type == IOMAP_HOLE)
1172 return length;
1174 ret = iomap_to_fiemap(ctx->fi, &ctx->prev, 0);
1175 ctx->prev = *iomap;
1176 switch (ret) {
1177 case 0: /* success */
1178 return length;
1179 case 1: /* extent array full */
1180 return 0;
1181 default:
1182 return ret;
1186 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fi,
1187 loff_t start, loff_t len, const struct iomap_ops *ops)
1189 struct fiemap_ctx ctx;
1190 loff_t ret;
1192 memset(&ctx, 0, sizeof(ctx));
1193 ctx.fi = fi;
1194 ctx.prev.type = IOMAP_HOLE;
1196 ret = fiemap_check_flags(fi, FIEMAP_FLAG_SYNC);
1197 if (ret)
1198 return ret;
1200 if (fi->fi_flags & FIEMAP_FLAG_SYNC) {
1201 ret = filemap_write_and_wait(inode->i_mapping);
1202 if (ret)
1203 return ret;
1206 while (len > 0) {
1207 ret = iomap_apply(inode, start, len, IOMAP_REPORT, ops, &ctx,
1208 iomap_fiemap_actor);
1209 /* inode with no (attribute) mapping will give ENOENT */
1210 if (ret == -ENOENT)
1211 break;
1212 if (ret < 0)
1213 return ret;
1214 if (ret == 0)
1215 break;
1217 start += ret;
1218 len -= ret;
1221 if (ctx.prev.type != IOMAP_HOLE) {
1222 ret = iomap_to_fiemap(fi, &ctx.prev, FIEMAP_EXTENT_LAST);
1223 if (ret < 0)
1224 return ret;
1227 return 0;
1229 EXPORT_SYMBOL_GPL(iomap_fiemap);
1232 * Seek for SEEK_DATA / SEEK_HOLE within @page, starting at @lastoff.
1233 * Returns true if found and updates @lastoff to the offset in file.
1235 static bool
1236 page_seek_hole_data(struct inode *inode, struct page *page, loff_t *lastoff,
1237 int whence)
1239 const struct address_space_operations *ops = inode->i_mapping->a_ops;
1240 unsigned int bsize = i_blocksize(inode), off;
1241 bool seek_data = whence == SEEK_DATA;
1242 loff_t poff = page_offset(page);
1244 if (WARN_ON_ONCE(*lastoff >= poff + PAGE_SIZE))
1245 return false;
1247 if (*lastoff < poff) {
1249 * Last offset smaller than the start of the page means we found
1250 * a hole:
1252 if (whence == SEEK_HOLE)
1253 return true;
1254 *lastoff = poff;
1258 * Just check the page unless we can and should check block ranges:
1260 if (bsize == PAGE_SIZE || !ops->is_partially_uptodate)
1261 return PageUptodate(page) == seek_data;
1263 lock_page(page);
1264 if (unlikely(page->mapping != inode->i_mapping))
1265 goto out_unlock_not_found;
1267 for (off = 0; off < PAGE_SIZE; off += bsize) {
1268 if (offset_in_page(*lastoff) >= off + bsize)
1269 continue;
1270 if (ops->is_partially_uptodate(page, off, bsize) == seek_data) {
1271 unlock_page(page);
1272 return true;
1274 *lastoff = poff + off + bsize;
1277 out_unlock_not_found:
1278 unlock_page(page);
1279 return false;
1283 * Seek for SEEK_DATA / SEEK_HOLE in the page cache.
1285 * Within unwritten extents, the page cache determines which parts are holes
1286 * and which are data: uptodate buffer heads count as data; everything else
1287 * counts as a hole.
1289 * Returns the resulting offset on successs, and -ENOENT otherwise.
1291 static loff_t
1292 page_cache_seek_hole_data(struct inode *inode, loff_t offset, loff_t length,
1293 int whence)
1295 pgoff_t index = offset >> PAGE_SHIFT;
1296 pgoff_t end = DIV_ROUND_UP(offset + length, PAGE_SIZE);
1297 loff_t lastoff = offset;
1298 struct pagevec pvec;
1300 if (length <= 0)
1301 return -ENOENT;
1303 pagevec_init(&pvec);
1305 do {
1306 unsigned nr_pages, i;
1308 nr_pages = pagevec_lookup_range(&pvec, inode->i_mapping, &index,
1309 end - 1);
1310 if (nr_pages == 0)
1311 break;
1313 for (i = 0; i < nr_pages; i++) {
1314 struct page *page = pvec.pages[i];
1316 if (page_seek_hole_data(inode, page, &lastoff, whence))
1317 goto check_range;
1318 lastoff = page_offset(page) + PAGE_SIZE;
1320 pagevec_release(&pvec);
1321 } while (index < end);
1323 /* When no page at lastoff and we are not done, we found a hole. */
1324 if (whence != SEEK_HOLE)
1325 goto not_found;
1327 check_range:
1328 if (lastoff < offset + length)
1329 goto out;
1330 not_found:
1331 lastoff = -ENOENT;
1332 out:
1333 pagevec_release(&pvec);
1334 return lastoff;
1338 static loff_t
1339 iomap_seek_hole_actor(struct inode *inode, loff_t offset, loff_t length,
1340 void *data, struct iomap *iomap)
1342 switch (iomap->type) {
1343 case IOMAP_UNWRITTEN:
1344 offset = page_cache_seek_hole_data(inode, offset, length,
1345 SEEK_HOLE);
1346 if (offset < 0)
1347 return length;
1348 /* fall through */
1349 case IOMAP_HOLE:
1350 *(loff_t *)data = offset;
1351 return 0;
1352 default:
1353 return length;
1357 loff_t
1358 iomap_seek_hole(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
1360 loff_t size = i_size_read(inode);
1361 loff_t length = size - offset;
1362 loff_t ret;
1364 /* Nothing to be found before or beyond the end of the file. */
1365 if (offset < 0 || offset >= size)
1366 return -ENXIO;
1368 while (length > 0) {
1369 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
1370 &offset, iomap_seek_hole_actor);
1371 if (ret < 0)
1372 return ret;
1373 if (ret == 0)
1374 break;
1376 offset += ret;
1377 length -= ret;
1380 return offset;
1382 EXPORT_SYMBOL_GPL(iomap_seek_hole);
1384 static loff_t
1385 iomap_seek_data_actor(struct inode *inode, loff_t offset, loff_t length,
1386 void *data, struct iomap *iomap)
1388 switch (iomap->type) {
1389 case IOMAP_HOLE:
1390 return length;
1391 case IOMAP_UNWRITTEN:
1392 offset = page_cache_seek_hole_data(inode, offset, length,
1393 SEEK_DATA);
1394 if (offset < 0)
1395 return length;
1396 /*FALLTHRU*/
1397 default:
1398 *(loff_t *)data = offset;
1399 return 0;
1403 loff_t
1404 iomap_seek_data(struct inode *inode, loff_t offset, const struct iomap_ops *ops)
1406 loff_t size = i_size_read(inode);
1407 loff_t length = size - offset;
1408 loff_t ret;
1410 /* Nothing to be found before or beyond the end of the file. */
1411 if (offset < 0 || offset >= size)
1412 return -ENXIO;
1414 while (length > 0) {
1415 ret = iomap_apply(inode, offset, length, IOMAP_REPORT, ops,
1416 &offset, iomap_seek_data_actor);
1417 if (ret < 0)
1418 return ret;
1419 if (ret == 0)
1420 break;
1422 offset += ret;
1423 length -= ret;
1426 if (length <= 0)
1427 return -ENXIO;
1428 return offset;
1430 EXPORT_SYMBOL_GPL(iomap_seek_data);
1433 * Private flags for iomap_dio, must not overlap with the public ones in
1434 * iomap.h:
1436 #define IOMAP_DIO_WRITE_FUA (1 << 28)
1437 #define IOMAP_DIO_NEED_SYNC (1 << 29)
1438 #define IOMAP_DIO_WRITE (1 << 30)
1439 #define IOMAP_DIO_DIRTY (1 << 31)
1441 struct iomap_dio {
1442 struct kiocb *iocb;
1443 iomap_dio_end_io_t *end_io;
1444 loff_t i_size;
1445 loff_t size;
1446 atomic_t ref;
1447 unsigned flags;
1448 int error;
1449 bool wait_for_completion;
1451 union {
1452 /* used during submission and for synchronous completion: */
1453 struct {
1454 struct iov_iter *iter;
1455 struct task_struct *waiter;
1456 struct request_queue *last_queue;
1457 blk_qc_t cookie;
1458 } submit;
1460 /* used for aio completion: */
1461 struct {
1462 struct work_struct work;
1463 } aio;
1467 static ssize_t iomap_dio_complete(struct iomap_dio *dio)
1469 struct kiocb *iocb = dio->iocb;
1470 struct inode *inode = file_inode(iocb->ki_filp);
1471 loff_t offset = iocb->ki_pos;
1472 ssize_t ret;
1474 if (dio->end_io) {
1475 ret = dio->end_io(iocb,
1476 dio->error ? dio->error : dio->size,
1477 dio->flags);
1478 } else {
1479 ret = dio->error;
1482 if (likely(!ret)) {
1483 ret = dio->size;
1484 /* check for short read */
1485 if (offset + ret > dio->i_size &&
1486 !(dio->flags & IOMAP_DIO_WRITE))
1487 ret = dio->i_size - offset;
1488 iocb->ki_pos += ret;
1492 * Try again to invalidate clean pages which might have been cached by
1493 * non-direct readahead, or faulted in by get_user_pages() if the source
1494 * of the write was an mmap'ed region of the file we're writing. Either
1495 * one is a pretty crazy thing to do, so we don't support it 100%. If
1496 * this invalidation fails, tough, the write still worked...
1498 * And this page cache invalidation has to be after dio->end_io(), as
1499 * some filesystems convert unwritten extents to real allocations in
1500 * end_io() when necessary, otherwise a racing buffer read would cache
1501 * zeros from unwritten extents.
1503 if (!dio->error &&
1504 (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
1505 int err;
1506 err = invalidate_inode_pages2_range(inode->i_mapping,
1507 offset >> PAGE_SHIFT,
1508 (offset + dio->size - 1) >> PAGE_SHIFT);
1509 if (err)
1510 dio_warn_stale_pagecache(iocb->ki_filp);
1514 * If this is a DSYNC write, make sure we push it to stable storage now
1515 * that we've written data.
1517 if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
1518 ret = generic_write_sync(iocb, ret);
1520 inode_dio_end(file_inode(iocb->ki_filp));
1521 kfree(dio);
1523 return ret;
1526 static void iomap_dio_complete_work(struct work_struct *work)
1528 struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
1529 struct kiocb *iocb = dio->iocb;
1531 iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
1535 * Set an error in the dio if none is set yet. We have to use cmpxchg
1536 * as the submission context and the completion context(s) can race to
1537 * update the error.
1539 static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
1541 cmpxchg(&dio->error, 0, ret);
1544 static void iomap_dio_bio_end_io(struct bio *bio)
1546 struct iomap_dio *dio = bio->bi_private;
1547 bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
1549 if (bio->bi_status)
1550 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
1552 if (atomic_dec_and_test(&dio->ref)) {
1553 if (dio->wait_for_completion) {
1554 struct task_struct *waiter = dio->submit.waiter;
1555 WRITE_ONCE(dio->submit.waiter, NULL);
1556 wake_up_process(waiter);
1557 } else if (dio->flags & IOMAP_DIO_WRITE) {
1558 struct inode *inode = file_inode(dio->iocb->ki_filp);
1560 INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
1561 queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
1562 } else {
1563 iomap_dio_complete_work(&dio->aio.work);
1567 if (should_dirty) {
1568 bio_check_pages_dirty(bio);
1569 } else {
1570 struct bio_vec *bvec;
1571 int i;
1573 bio_for_each_segment_all(bvec, bio, i)
1574 put_page(bvec->bv_page);
1575 bio_put(bio);
1579 static blk_qc_t
1580 iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
1581 unsigned len)
1583 struct page *page = ZERO_PAGE(0);
1584 struct bio *bio;
1586 bio = bio_alloc(GFP_KERNEL, 1);
1587 bio_set_dev(bio, iomap->bdev);
1588 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
1589 bio->bi_private = dio;
1590 bio->bi_end_io = iomap_dio_bio_end_io;
1592 get_page(page);
1593 __bio_add_page(bio, page, len, 0);
1594 bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE);
1596 atomic_inc(&dio->ref);
1597 return submit_bio(bio);
1600 static loff_t
1601 iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
1602 struct iomap_dio *dio, struct iomap *iomap)
1604 unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
1605 unsigned int fs_block_size = i_blocksize(inode), pad;
1606 unsigned int align = iov_iter_alignment(dio->submit.iter);
1607 struct iov_iter iter;
1608 struct bio *bio;
1609 bool need_zeroout = false;
1610 bool use_fua = false;
1611 int nr_pages, ret = 0;
1612 size_t copied = 0;
1614 if ((pos | length | align) & ((1 << blkbits) - 1))
1615 return -EINVAL;
1617 if (iomap->type == IOMAP_UNWRITTEN) {
1618 dio->flags |= IOMAP_DIO_UNWRITTEN;
1619 need_zeroout = true;
1622 if (iomap->flags & IOMAP_F_SHARED)
1623 dio->flags |= IOMAP_DIO_COW;
1625 if (iomap->flags & IOMAP_F_NEW) {
1626 need_zeroout = true;
1627 } else if (iomap->type == IOMAP_MAPPED) {
1629 * Use a FUA write if we need datasync semantics, this is a pure
1630 * data IO that doesn't require any metadata updates (including
1631 * after IO completion such as unwritten extent conversion) and
1632 * the underlying device supports FUA. This allows us to avoid
1633 * cache flushes on IO completion.
1635 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
1636 (dio->flags & IOMAP_DIO_WRITE_FUA) &&
1637 blk_queue_fua(bdev_get_queue(iomap->bdev)))
1638 use_fua = true;
1642 * Operate on a partial iter trimmed to the extent we were called for.
1643 * We'll update the iter in the dio once we're done with this extent.
1645 iter = *dio->submit.iter;
1646 iov_iter_truncate(&iter, length);
1648 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
1649 if (nr_pages <= 0)
1650 return nr_pages;
1652 if (need_zeroout) {
1653 /* zero out from the start of the block to the write offset */
1654 pad = pos & (fs_block_size - 1);
1655 if (pad)
1656 iomap_dio_zero(dio, iomap, pos - pad, pad);
1659 do {
1660 size_t n;
1661 if (dio->error) {
1662 iov_iter_revert(dio->submit.iter, copied);
1663 return 0;
1666 bio = bio_alloc(GFP_KERNEL, nr_pages);
1667 bio_set_dev(bio, iomap->bdev);
1668 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
1669 bio->bi_write_hint = dio->iocb->ki_hint;
1670 bio->bi_ioprio = dio->iocb->ki_ioprio;
1671 bio->bi_private = dio;
1672 bio->bi_end_io = iomap_dio_bio_end_io;
1674 ret = bio_iov_iter_get_pages(bio, &iter);
1675 if (unlikely(ret)) {
1677 * We have to stop part way through an IO. We must fall
1678 * through to the sub-block tail zeroing here, otherwise
1679 * this short IO may expose stale data in the tail of
1680 * the block we haven't written data to.
1682 bio_put(bio);
1683 goto zero_tail;
1686 n = bio->bi_iter.bi_size;
1687 if (dio->flags & IOMAP_DIO_WRITE) {
1688 bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
1689 if (use_fua)
1690 bio->bi_opf |= REQ_FUA;
1691 else
1692 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
1693 task_io_account_write(n);
1694 } else {
1695 bio->bi_opf = REQ_OP_READ;
1696 if (dio->flags & IOMAP_DIO_DIRTY)
1697 bio_set_pages_dirty(bio);
1700 iov_iter_advance(dio->submit.iter, n);
1702 dio->size += n;
1703 pos += n;
1704 copied += n;
1706 nr_pages = iov_iter_npages(&iter, BIO_MAX_PAGES);
1708 atomic_inc(&dio->ref);
1710 dio->submit.last_queue = bdev_get_queue(iomap->bdev);
1711 dio->submit.cookie = submit_bio(bio);
1712 } while (nr_pages);
1715 * We need to zeroout the tail of a sub-block write if the extent type
1716 * requires zeroing or the write extends beyond EOF. If we don't zero
1717 * the block tail in the latter case, we can expose stale data via mmap
1718 * reads of the EOF block.
1720 zero_tail:
1721 if (need_zeroout ||
1722 ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
1723 /* zero out from the end of the write to the end of the block */
1724 pad = pos & (fs_block_size - 1);
1725 if (pad)
1726 iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
1728 return copied ? copied : ret;
1731 static loff_t
1732 iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
1734 length = iov_iter_zero(length, dio->submit.iter);
1735 dio->size += length;
1736 return length;
1739 static loff_t
1740 iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
1741 struct iomap_dio *dio, struct iomap *iomap)
1743 struct iov_iter *iter = dio->submit.iter;
1744 size_t copied;
1746 BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
1748 if (dio->flags & IOMAP_DIO_WRITE) {
1749 loff_t size = inode->i_size;
1751 if (pos > size)
1752 memset(iomap->inline_data + size, 0, pos - size);
1753 copied = copy_from_iter(iomap->inline_data + pos, length, iter);
1754 if (copied) {
1755 if (pos + copied > size)
1756 i_size_write(inode, pos + copied);
1757 mark_inode_dirty(inode);
1759 } else {
1760 copied = copy_to_iter(iomap->inline_data + pos, length, iter);
1762 dio->size += copied;
1763 return copied;
1766 static loff_t
1767 iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
1768 void *data, struct iomap *iomap)
1770 struct iomap_dio *dio = data;
1772 switch (iomap->type) {
1773 case IOMAP_HOLE:
1774 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
1775 return -EIO;
1776 return iomap_dio_hole_actor(length, dio);
1777 case IOMAP_UNWRITTEN:
1778 if (!(dio->flags & IOMAP_DIO_WRITE))
1779 return iomap_dio_hole_actor(length, dio);
1780 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
1781 case IOMAP_MAPPED:
1782 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
1783 case IOMAP_INLINE:
1784 return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
1785 default:
1786 WARN_ON_ONCE(1);
1787 return -EIO;
1792 * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
1793 * is being issued as AIO or not. This allows us to optimise pure data writes
1794 * to use REQ_FUA rather than requiring generic_write_sync() to issue a
1795 * REQ_FLUSH post write. This is slightly tricky because a single request here
1796 * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
1797 * may be pure data writes. In that case, we still need to do a full data sync
1798 * completion.
1800 ssize_t
1801 iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
1802 const struct iomap_ops *ops, iomap_dio_end_io_t end_io)
1804 struct address_space *mapping = iocb->ki_filp->f_mapping;
1805 struct inode *inode = file_inode(iocb->ki_filp);
1806 size_t count = iov_iter_count(iter);
1807 loff_t pos = iocb->ki_pos, start = pos;
1808 loff_t end = iocb->ki_pos + count - 1, ret = 0;
1809 unsigned int flags = IOMAP_DIRECT;
1810 bool wait_for_completion = is_sync_kiocb(iocb);
1811 struct blk_plug plug;
1812 struct iomap_dio *dio;
1814 lockdep_assert_held(&inode->i_rwsem);
1816 if (!count)
1817 return 0;
1819 dio = kmalloc(sizeof(*dio), GFP_KERNEL);
1820 if (!dio)
1821 return -ENOMEM;
1823 dio->iocb = iocb;
1824 atomic_set(&dio->ref, 1);
1825 dio->size = 0;
1826 dio->i_size = i_size_read(inode);
1827 dio->end_io = end_io;
1828 dio->error = 0;
1829 dio->flags = 0;
1831 dio->submit.iter = iter;
1832 dio->submit.waiter = current;
1833 dio->submit.cookie = BLK_QC_T_NONE;
1834 dio->submit.last_queue = NULL;
1836 if (iov_iter_rw(iter) == READ) {
1837 if (pos >= dio->i_size)
1838 goto out_free_dio;
1840 if (iter->type == ITER_IOVEC)
1841 dio->flags |= IOMAP_DIO_DIRTY;
1842 } else {
1843 flags |= IOMAP_WRITE;
1844 dio->flags |= IOMAP_DIO_WRITE;
1846 /* for data sync or sync, we need sync completion processing */
1847 if (iocb->ki_flags & IOCB_DSYNC)
1848 dio->flags |= IOMAP_DIO_NEED_SYNC;
1851 * For datasync only writes, we optimistically try using FUA for
1852 * this IO. Any non-FUA write that occurs will clear this flag,
1853 * hence we know before completion whether a cache flush is
1854 * necessary.
1856 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
1857 dio->flags |= IOMAP_DIO_WRITE_FUA;
1860 if (iocb->ki_flags & IOCB_NOWAIT) {
1861 if (filemap_range_has_page(mapping, start, end)) {
1862 ret = -EAGAIN;
1863 goto out_free_dio;
1865 flags |= IOMAP_NOWAIT;
1868 ret = filemap_write_and_wait_range(mapping, start, end);
1869 if (ret)
1870 goto out_free_dio;
1873 * Try to invalidate cache pages for the range we're direct
1874 * writing. If this invalidation fails, tough, the write will
1875 * still work, but racing two incompatible write paths is a
1876 * pretty crazy thing to do, so we don't support it 100%.
1878 ret = invalidate_inode_pages2_range(mapping,
1879 start >> PAGE_SHIFT, end >> PAGE_SHIFT);
1880 if (ret)
1881 dio_warn_stale_pagecache(iocb->ki_filp);
1882 ret = 0;
1884 if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&
1885 !inode->i_sb->s_dio_done_wq) {
1886 ret = sb_init_dio_done_wq(inode->i_sb);
1887 if (ret < 0)
1888 goto out_free_dio;
1891 inode_dio_begin(inode);
1893 blk_start_plug(&plug);
1894 do {
1895 ret = iomap_apply(inode, pos, count, flags, ops, dio,
1896 iomap_dio_actor);
1897 if (ret <= 0) {
1898 /* magic error code to fall back to buffered I/O */
1899 if (ret == -ENOTBLK) {
1900 wait_for_completion = true;
1901 ret = 0;
1903 break;
1905 pos += ret;
1907 if (iov_iter_rw(iter) == READ && pos >= dio->i_size) {
1909 * We only report that we've read data up to i_size.
1910 * Revert iter to a state corresponding to that as
1911 * some callers (such as splice code) rely on it.
1913 iov_iter_revert(iter, pos - dio->i_size);
1914 break;
1916 } while ((count = iov_iter_count(iter)) > 0);
1917 blk_finish_plug(&plug);
1919 if (ret < 0)
1920 iomap_dio_set_error(dio, ret);
1923 * If all the writes we issued were FUA, we don't need to flush the
1924 * cache on IO completion. Clear the sync flag for this case.
1926 if (dio->flags & IOMAP_DIO_WRITE_FUA)
1927 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
1930 * We are about to drop our additional submission reference, which
1931 * might be the last reference to the dio. There are three three
1932 * different ways we can progress here:
1934 * (a) If this is the last reference we will always complete and free
1935 * the dio ourselves.
1936 * (b) If this is not the last reference, and we serve an asynchronous
1937 * iocb, we must never touch the dio after the decrement, the
1938 * I/O completion handler will complete and free it.
1939 * (c) If this is not the last reference, but we serve a synchronous
1940 * iocb, the I/O completion handler will wake us up on the drop
1941 * of the final reference, and we will complete and free it here
1942 * after we got woken by the I/O completion handler.
1944 dio->wait_for_completion = wait_for_completion;
1945 if (!atomic_dec_and_test(&dio->ref)) {
1946 if (!wait_for_completion)
1947 return -EIOCBQUEUED;
1949 for (;;) {
1950 set_current_state(TASK_UNINTERRUPTIBLE);
1951 if (!READ_ONCE(dio->submit.waiter))
1952 break;
1954 if (!(iocb->ki_flags & IOCB_HIPRI) ||
1955 !dio->submit.last_queue ||
1956 !blk_poll(dio->submit.last_queue,
1957 dio->submit.cookie))
1958 io_schedule();
1960 __set_current_state(TASK_RUNNING);
1963 return iomap_dio_complete(dio);
1965 out_free_dio:
1966 kfree(dio);
1967 return ret;
1969 EXPORT_SYMBOL_GPL(iomap_dio_rw);
1971 /* Swapfile activation */
1973 #ifdef CONFIG_SWAP
1974 struct iomap_swapfile_info {
1975 struct iomap iomap; /* accumulated iomap */
1976 struct swap_info_struct *sis;
1977 uint64_t lowest_ppage; /* lowest physical addr seen (pages) */
1978 uint64_t highest_ppage; /* highest physical addr seen (pages) */
1979 unsigned long nr_pages; /* number of pages collected */
1980 int nr_extents; /* extent count */
1984 * Collect physical extents for this swap file. Physical extents reported to
1985 * the swap code must be trimmed to align to a page boundary. The logical
1986 * offset within the file is irrelevant since the swapfile code maps logical
1987 * page numbers of the swap device to the physical page-aligned extents.
1989 static int iomap_swapfile_add_extent(struct iomap_swapfile_info *isi)
1991 struct iomap *iomap = &isi->iomap;
1992 unsigned long nr_pages;
1993 uint64_t first_ppage;
1994 uint64_t first_ppage_reported;
1995 uint64_t next_ppage;
1996 int error;
1999 * Round the start up and the end down so that the physical
2000 * extent aligns to a page boundary.
2002 first_ppage = ALIGN(iomap->addr, PAGE_SIZE) >> PAGE_SHIFT;
2003 next_ppage = ALIGN_DOWN(iomap->addr + iomap->length, PAGE_SIZE) >>
2004 PAGE_SHIFT;
2006 /* Skip too-short physical extents. */
2007 if (first_ppage >= next_ppage)
2008 return 0;
2009 nr_pages = next_ppage - first_ppage;
2012 * Calculate how much swap space we're adding; the first page contains
2013 * the swap header and doesn't count. The mm still wants that first
2014 * page fed to add_swap_extent, however.
2016 first_ppage_reported = first_ppage;
2017 if (iomap->offset == 0)
2018 first_ppage_reported++;
2019 if (isi->lowest_ppage > first_ppage_reported)
2020 isi->lowest_ppage = first_ppage_reported;
2021 if (isi->highest_ppage < (next_ppage - 1))
2022 isi->highest_ppage = next_ppage - 1;
2024 /* Add extent, set up for the next call. */
2025 error = add_swap_extent(isi->sis, isi->nr_pages, nr_pages, first_ppage);
2026 if (error < 0)
2027 return error;
2028 isi->nr_extents += error;
2029 isi->nr_pages += nr_pages;
2030 return 0;
2034 * Accumulate iomaps for this swap file. We have to accumulate iomaps because
2035 * swap only cares about contiguous page-aligned physical extents and makes no
2036 * distinction between written and unwritten extents.
2038 static loff_t iomap_swapfile_activate_actor(struct inode *inode, loff_t pos,
2039 loff_t count, void *data, struct iomap *iomap)
2041 struct iomap_swapfile_info *isi = data;
2042 int error;
2044 switch (iomap->type) {
2045 case IOMAP_MAPPED:
2046 case IOMAP_UNWRITTEN:
2047 /* Only real or unwritten extents. */
2048 break;
2049 case IOMAP_INLINE:
2050 /* No inline data. */
2051 pr_err("swapon: file is inline\n");
2052 return -EINVAL;
2053 default:
2054 pr_err("swapon: file has unallocated extents\n");
2055 return -EINVAL;
2058 /* No uncommitted metadata or shared blocks. */
2059 if (iomap->flags & IOMAP_F_DIRTY) {
2060 pr_err("swapon: file is not committed\n");
2061 return -EINVAL;
2063 if (iomap->flags & IOMAP_F_SHARED) {
2064 pr_err("swapon: file has shared extents\n");
2065 return -EINVAL;
2068 /* Only one bdev per swap file. */
2069 if (iomap->bdev != isi->sis->bdev) {
2070 pr_err("swapon: file is on multiple devices\n");
2071 return -EINVAL;
2074 if (isi->iomap.length == 0) {
2075 /* No accumulated extent, so just store it. */
2076 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
2077 } else if (isi->iomap.addr + isi->iomap.length == iomap->addr) {
2078 /* Append this to the accumulated extent. */
2079 isi->iomap.length += iomap->length;
2080 } else {
2081 /* Otherwise, add the retained iomap and store this one. */
2082 error = iomap_swapfile_add_extent(isi);
2083 if (error)
2084 return error;
2085 memcpy(&isi->iomap, iomap, sizeof(isi->iomap));
2087 return count;
2091 * Iterate a swap file's iomaps to construct physical extents that can be
2092 * passed to the swapfile subsystem.
2094 int iomap_swapfile_activate(struct swap_info_struct *sis,
2095 struct file *swap_file, sector_t *pagespan,
2096 const struct iomap_ops *ops)
2098 struct iomap_swapfile_info isi = {
2099 .sis = sis,
2100 .lowest_ppage = (sector_t)-1ULL,
2102 struct address_space *mapping = swap_file->f_mapping;
2103 struct inode *inode = mapping->host;
2104 loff_t pos = 0;
2105 loff_t len = ALIGN_DOWN(i_size_read(inode), PAGE_SIZE);
2106 loff_t ret;
2109 * Persist all file mapping metadata so that we won't have any
2110 * IOMAP_F_DIRTY iomaps.
2112 ret = vfs_fsync(swap_file, 1);
2113 if (ret)
2114 return ret;
2116 while (len > 0) {
2117 ret = iomap_apply(inode, pos, len, IOMAP_REPORT,
2118 ops, &isi, iomap_swapfile_activate_actor);
2119 if (ret <= 0)
2120 return ret;
2122 pos += ret;
2123 len -= ret;
2126 if (isi.iomap.length) {
2127 ret = iomap_swapfile_add_extent(&isi);
2128 if (ret)
2129 return ret;
2132 *pagespan = 1 + isi.highest_ppage - isi.lowest_ppage;
2133 sis->max = isi.nr_pages;
2134 sis->pages = isi.nr_pages - 1;
2135 sis->highest_bit = isi.nr_pages - 1;
2136 return isi.nr_extents;
2138 EXPORT_SYMBOL_GPL(iomap_swapfile_activate);
2139 #endif /* CONFIG_SWAP */
2141 static loff_t
2142 iomap_bmap_actor(struct inode *inode, loff_t pos, loff_t length,
2143 void *data, struct iomap *iomap)
2145 sector_t *bno = data, addr;
2147 if (iomap->type == IOMAP_MAPPED) {
2148 addr = (pos - iomap->offset + iomap->addr) >> inode->i_blkbits;
2149 if (addr > INT_MAX)
2150 WARN(1, "would truncate bmap result\n");
2151 else
2152 *bno = addr;
2154 return 0;
2157 /* legacy ->bmap interface. 0 is the error return (!) */
2158 sector_t
2159 iomap_bmap(struct address_space *mapping, sector_t bno,
2160 const struct iomap_ops *ops)
2162 struct inode *inode = mapping->host;
2163 loff_t pos = bno << inode->i_blkbits;
2164 unsigned blocksize = i_blocksize(inode);
2166 if (filemap_write_and_wait(mapping))
2167 return 0;
2169 bno = 0;
2170 iomap_apply(inode, pos, blocksize, 0, ops, &bno, iomap_bmap_actor);
2171 return bno;
2173 EXPORT_SYMBOL_GPL(iomap_bmap);