1afc51aaaSDarrick J. Wong // SPDX-License-Identifier: GPL-2.0 2afc51aaaSDarrick J. Wong /* 3afc51aaaSDarrick J. Wong * Copyright (C) 2010 Red Hat, Inc. 4598ecfbaSChristoph Hellwig * Copyright (C) 2016-2019 Christoph Hellwig. 5afc51aaaSDarrick J. Wong */ 6afc51aaaSDarrick J. Wong #include <linux/module.h> 7afc51aaaSDarrick J. Wong #include <linux/compiler.h> 8afc51aaaSDarrick J. Wong #include <linux/fs.h> 9afc51aaaSDarrick J. Wong #include <linux/iomap.h> 10afc51aaaSDarrick J. Wong #include <linux/pagemap.h> 11afc51aaaSDarrick J. Wong #include <linux/uio.h> 12afc51aaaSDarrick J. Wong #include <linux/buffer_head.h> 13afc51aaaSDarrick J. Wong #include <linux/dax.h> 14afc51aaaSDarrick J. Wong #include <linux/writeback.h> 15598ecfbaSChristoph Hellwig #include <linux/list_sort.h> 16afc51aaaSDarrick J. Wong #include <linux/swap.h> 17afc51aaaSDarrick J. Wong #include <linux/bio.h> 18afc51aaaSDarrick J. Wong #include <linux/sched/signal.h> 19afc51aaaSDarrick J. Wong #include <linux/migrate.h> 209e91c572SChristoph Hellwig #include "trace.h" 21afc51aaaSDarrick J. Wong 22afc51aaaSDarrick J. Wong #include "../internal.h" 23afc51aaaSDarrick J. Wong 24ebb7fb15SDave Chinner #define IOEND_BATCH_SIZE 4096 25ebb7fb15SDave Chinner 26ab08b01eSChristoph Hellwig /* 2795c4cd05SMatthew Wilcox (Oracle) * Structure allocated for each folio when block size < folio size 2895c4cd05SMatthew Wilcox (Oracle) * to track sub-folio uptodate status and I/O completions. 29ab08b01eSChristoph Hellwig */ 30ab08b01eSChristoph Hellwig struct iomap_page { 317d636676SMatthew Wilcox (Oracle) atomic_t read_bytes_pending; 320fb2d720SMatthew Wilcox (Oracle) atomic_t write_bytes_pending; 331cea335dSChristoph Hellwig spinlock_t uptodate_lock; 340a195b91SMatthew Wilcox (Oracle) unsigned long uptodate[]; 35ab08b01eSChristoph Hellwig }; 36ab08b01eSChristoph Hellwig 3795c4cd05SMatthew Wilcox (Oracle) static inline struct iomap_page *to_iomap_page(struct folio *folio) 38ab08b01eSChristoph Hellwig { 3995c4cd05SMatthew Wilcox (Oracle) if (folio_test_private(folio)) 4095c4cd05SMatthew Wilcox (Oracle) return folio_get_private(folio); 41ab08b01eSChristoph Hellwig return NULL; 42ab08b01eSChristoph Hellwig } 43ab08b01eSChristoph Hellwig 44598ecfbaSChristoph Hellwig static struct bio_set iomap_ioend_bioset; 45598ecfbaSChristoph Hellwig 46afc51aaaSDarrick J. Wong static struct iomap_page * 479753b868SStefan Roesch iomap_page_create(struct inode *inode, struct folio *folio, unsigned int flags) 48afc51aaaSDarrick J. Wong { 4995c4cd05SMatthew Wilcox (Oracle) struct iomap_page *iop = to_iomap_page(folio); 50435d44b3SMatthew Wilcox (Oracle) unsigned int nr_blocks = i_blocks_per_folio(inode, folio); 519753b868SStefan Roesch gfp_t gfp; 52afc51aaaSDarrick J. Wong 530a195b91SMatthew Wilcox (Oracle) if (iop || nr_blocks <= 1) 54afc51aaaSDarrick J. Wong return iop; 55afc51aaaSDarrick J. Wong 569753b868SStefan Roesch if (flags & IOMAP_NOWAIT) 579753b868SStefan Roesch gfp = GFP_NOWAIT; 589753b868SStefan Roesch else 599753b868SStefan Roesch gfp = GFP_NOFS | __GFP_NOFAIL; 609753b868SStefan Roesch 610a195b91SMatthew Wilcox (Oracle) iop = kzalloc(struct_size(iop, uptodate, BITS_TO_LONGS(nr_blocks)), 629753b868SStefan Roesch gfp); 639753b868SStefan Roesch if (iop) { 641cea335dSChristoph Hellwig spin_lock_init(&iop->uptodate_lock); 65435d44b3SMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) 664595a298SMatthew Wilcox (Oracle) bitmap_fill(iop->uptodate, nr_blocks); 67435d44b3SMatthew Wilcox (Oracle) folio_attach_private(folio, iop); 689753b868SStefan Roesch } 69afc51aaaSDarrick J. Wong return iop; 70afc51aaaSDarrick J. Wong } 71afc51aaaSDarrick J. Wong 72c46e8324SMatthew Wilcox (Oracle) static void iomap_page_release(struct folio *folio) 73afc51aaaSDarrick J. Wong { 74c46e8324SMatthew Wilcox (Oracle) struct iomap_page *iop = folio_detach_private(folio); 75c46e8324SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host; 76c46e8324SMatthew Wilcox (Oracle) unsigned int nr_blocks = i_blocks_per_folio(inode, folio); 77afc51aaaSDarrick J. Wong 78afc51aaaSDarrick J. Wong if (!iop) 79afc51aaaSDarrick J. Wong return; 807d636676SMatthew Wilcox (Oracle) WARN_ON_ONCE(atomic_read(&iop->read_bytes_pending)); 810fb2d720SMatthew Wilcox (Oracle) WARN_ON_ONCE(atomic_read(&iop->write_bytes_pending)); 820a195b91SMatthew Wilcox (Oracle) WARN_ON_ONCE(bitmap_full(iop->uptodate, nr_blocks) != 83c46e8324SMatthew Wilcox (Oracle) folio_test_uptodate(folio)); 84afc51aaaSDarrick J. Wong kfree(iop); 85afc51aaaSDarrick J. Wong } 86afc51aaaSDarrick J. Wong 87afc51aaaSDarrick J. Wong /* 88431c0566SMatthew Wilcox (Oracle) * Calculate the range inside the folio that we actually need to read. 89afc51aaaSDarrick J. Wong */ 90431c0566SMatthew Wilcox (Oracle) static void iomap_adjust_read_range(struct inode *inode, struct folio *folio, 91431c0566SMatthew Wilcox (Oracle) loff_t *pos, loff_t length, size_t *offp, size_t *lenp) 92afc51aaaSDarrick J. Wong { 93431c0566SMatthew Wilcox (Oracle) struct iomap_page *iop = to_iomap_page(folio); 94afc51aaaSDarrick J. Wong loff_t orig_pos = *pos; 95afc51aaaSDarrick J. Wong loff_t isize = i_size_read(inode); 96afc51aaaSDarrick J. Wong unsigned block_bits = inode->i_blkbits; 97afc51aaaSDarrick J. Wong unsigned block_size = (1 << block_bits); 98431c0566SMatthew Wilcox (Oracle) size_t poff = offset_in_folio(folio, *pos); 99431c0566SMatthew Wilcox (Oracle) size_t plen = min_t(loff_t, folio_size(folio) - poff, length); 100afc51aaaSDarrick J. Wong unsigned first = poff >> block_bits; 101afc51aaaSDarrick J. Wong unsigned last = (poff + plen - 1) >> block_bits; 102afc51aaaSDarrick J. Wong 103afc51aaaSDarrick J. Wong /* 104f1f264b4SAndreas Gruenbacher * If the block size is smaller than the page size, we need to check the 105afc51aaaSDarrick J. Wong * per-block uptodate status and adjust the offset and length if needed 106afc51aaaSDarrick J. Wong * to avoid reading in already uptodate ranges. 107afc51aaaSDarrick J. Wong */ 108afc51aaaSDarrick J. Wong if (iop) { 109afc51aaaSDarrick J. Wong unsigned int i; 110afc51aaaSDarrick J. Wong 111afc51aaaSDarrick J. Wong /* move forward for each leading block marked uptodate */ 112afc51aaaSDarrick J. Wong for (i = first; i <= last; i++) { 113afc51aaaSDarrick J. Wong if (!test_bit(i, iop->uptodate)) 114afc51aaaSDarrick J. Wong break; 115afc51aaaSDarrick J. Wong *pos += block_size; 116afc51aaaSDarrick J. Wong poff += block_size; 117afc51aaaSDarrick J. Wong plen -= block_size; 118afc51aaaSDarrick J. Wong first++; 119afc51aaaSDarrick J. Wong } 120afc51aaaSDarrick J. Wong 121afc51aaaSDarrick J. Wong /* truncate len if we find any trailing uptodate block(s) */ 122afc51aaaSDarrick J. Wong for ( ; i <= last; i++) { 123afc51aaaSDarrick J. Wong if (test_bit(i, iop->uptodate)) { 124afc51aaaSDarrick J. Wong plen -= (last - i + 1) * block_size; 125afc51aaaSDarrick J. Wong last = i - 1; 126afc51aaaSDarrick J. Wong break; 127afc51aaaSDarrick J. Wong } 128afc51aaaSDarrick J. Wong } 129afc51aaaSDarrick J. Wong } 130afc51aaaSDarrick J. Wong 131afc51aaaSDarrick J. Wong /* 132f1f264b4SAndreas Gruenbacher * If the extent spans the block that contains the i_size, we need to 133afc51aaaSDarrick J. Wong * handle both halves separately so that we properly zero data in the 134afc51aaaSDarrick J. Wong * page cache for blocks that are entirely outside of i_size. 135afc51aaaSDarrick J. Wong */ 136afc51aaaSDarrick J. Wong if (orig_pos <= isize && orig_pos + length > isize) { 137431c0566SMatthew Wilcox (Oracle) unsigned end = offset_in_folio(folio, isize - 1) >> block_bits; 138afc51aaaSDarrick J. Wong 139afc51aaaSDarrick J. Wong if (first <= end && last > end) 140afc51aaaSDarrick J. Wong plen -= (last - end) * block_size; 141afc51aaaSDarrick J. Wong } 142afc51aaaSDarrick J. Wong 143afc51aaaSDarrick J. Wong *offp = poff; 144afc51aaaSDarrick J. Wong *lenp = plen; 145afc51aaaSDarrick J. Wong } 146afc51aaaSDarrick J. Wong 147431c0566SMatthew Wilcox (Oracle) static void iomap_iop_set_range_uptodate(struct folio *folio, 148431c0566SMatthew Wilcox (Oracle) struct iomap_page *iop, size_t off, size_t len) 149afc51aaaSDarrick J. Wong { 150431c0566SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host; 151afc51aaaSDarrick J. Wong unsigned first = off >> inode->i_blkbits; 152afc51aaaSDarrick J. Wong unsigned last = (off + len - 1) >> inode->i_blkbits; 1531cea335dSChristoph Hellwig unsigned long flags; 154afc51aaaSDarrick J. Wong 1551cea335dSChristoph Hellwig spin_lock_irqsave(&iop->uptodate_lock, flags); 156b21866f5SMatthew Wilcox (Oracle) bitmap_set(iop->uptodate, first, last - first + 1); 157431c0566SMatthew Wilcox (Oracle) if (bitmap_full(iop->uptodate, i_blocks_per_folio(inode, folio))) 158431c0566SMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 1591cea335dSChristoph Hellwig spin_unlock_irqrestore(&iop->uptodate_lock, flags); 160afc51aaaSDarrick J. Wong } 161afc51aaaSDarrick J. Wong 162431c0566SMatthew Wilcox (Oracle) static void iomap_set_range_uptodate(struct folio *folio, 163431c0566SMatthew Wilcox (Oracle) struct iomap_page *iop, size_t off, size_t len) 1641cea335dSChristoph Hellwig { 165cd1e5afeSMatthew Wilcox (Oracle) if (iop) 166431c0566SMatthew Wilcox (Oracle) iomap_iop_set_range_uptodate(folio, iop, off, len); 1671cea335dSChristoph Hellwig else 168431c0566SMatthew Wilcox (Oracle) folio_mark_uptodate(folio); 169afc51aaaSDarrick J. Wong } 170afc51aaaSDarrick J. Wong 1718ffd74e9SMatthew Wilcox (Oracle) static void iomap_finish_folio_read(struct folio *folio, size_t offset, 1728ffd74e9SMatthew Wilcox (Oracle) size_t len, int error) 173afc51aaaSDarrick J. Wong { 17495c4cd05SMatthew Wilcox (Oracle) struct iomap_page *iop = to_iomap_page(folio); 175afc51aaaSDarrick J. Wong 176afc51aaaSDarrick J. Wong if (unlikely(error)) { 1778ffd74e9SMatthew Wilcox (Oracle) folio_clear_uptodate(folio); 1788ffd74e9SMatthew Wilcox (Oracle) folio_set_error(folio); 179afc51aaaSDarrick J. Wong } else { 180431c0566SMatthew Wilcox (Oracle) iomap_set_range_uptodate(folio, iop, offset, len); 181afc51aaaSDarrick J. Wong } 182afc51aaaSDarrick J. Wong 1838ffd74e9SMatthew Wilcox (Oracle) if (!iop || atomic_sub_and_test(len, &iop->read_bytes_pending)) 1848ffd74e9SMatthew Wilcox (Oracle) folio_unlock(folio); 185afc51aaaSDarrick J. Wong } 186afc51aaaSDarrick J. Wong 1878ffd74e9SMatthew Wilcox (Oracle) static void iomap_read_end_io(struct bio *bio) 188afc51aaaSDarrick J. Wong { 189afc51aaaSDarrick J. Wong int error = blk_status_to_errno(bio->bi_status); 1908ffd74e9SMatthew Wilcox (Oracle) struct folio_iter fi; 191afc51aaaSDarrick J. Wong 1928ffd74e9SMatthew Wilcox (Oracle) bio_for_each_folio_all(fi, bio) 1938ffd74e9SMatthew Wilcox (Oracle) iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error); 194afc51aaaSDarrick J. Wong bio_put(bio); 195afc51aaaSDarrick J. Wong } 196afc51aaaSDarrick J. Wong 197afc51aaaSDarrick J. Wong struct iomap_readpage_ctx { 1983aa9c659SMatthew Wilcox (Oracle) struct folio *cur_folio; 1993aa9c659SMatthew Wilcox (Oracle) bool cur_folio_in_bio; 200afc51aaaSDarrick J. Wong struct bio *bio; 2019d24a13aSMatthew Wilcox (Oracle) struct readahead_control *rac; 202afc51aaaSDarrick J. Wong }; 203afc51aaaSDarrick J. Wong 2045ad448ceSAndreas Gruenbacher /** 2055ad448ceSAndreas Gruenbacher * iomap_read_inline_data - copy inline data into the page cache 2065ad448ceSAndreas Gruenbacher * @iter: iteration structure 207874628a2SMatthew Wilcox (Oracle) * @folio: folio to copy to 2085ad448ceSAndreas Gruenbacher * 209874628a2SMatthew Wilcox (Oracle) * Copy the inline data in @iter into @folio and zero out the rest of the folio. 2105ad448ceSAndreas Gruenbacher * Only a single IOMAP_INLINE extent is allowed at the end of each file. 2115ad448ceSAndreas Gruenbacher * Returns zero for success to complete the read, or the usual negative errno. 2125ad448ceSAndreas Gruenbacher */ 2135ad448ceSAndreas Gruenbacher static int iomap_read_inline_data(const struct iomap_iter *iter, 214874628a2SMatthew Wilcox (Oracle) struct folio *folio) 215afc51aaaSDarrick J. Wong { 216cd1e5afeSMatthew Wilcox (Oracle) struct iomap_page *iop; 217fad0a1abSChristoph Hellwig const struct iomap *iomap = iomap_iter_srcmap(iter); 2181b5c1e36SChristoph Hellwig size_t size = i_size_read(iter->inode) - iomap->offset; 219b405435bSMatthew Wilcox (Oracle) size_t poff = offset_in_page(iomap->offset); 220431c0566SMatthew Wilcox (Oracle) size_t offset = offset_in_folio(folio, iomap->offset); 221afc51aaaSDarrick J. Wong void *addr; 222afc51aaaSDarrick J. Wong 223874628a2SMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) 2245ad448ceSAndreas Gruenbacher return 0; 225afc51aaaSDarrick J. Wong 226ae44f9c2SMatthew Wilcox (Oracle) if (WARN_ON_ONCE(size > PAGE_SIZE - poff)) 227ae44f9c2SMatthew Wilcox (Oracle) return -EIO; 22869f4a26cSGao Xiang if (WARN_ON_ONCE(size > PAGE_SIZE - 22969f4a26cSGao Xiang offset_in_page(iomap->inline_data))) 23069f4a26cSGao Xiang return -EIO; 23169f4a26cSGao Xiang if (WARN_ON_ONCE(size > iomap->length)) 23269f4a26cSGao Xiang return -EIO; 233431c0566SMatthew Wilcox (Oracle) if (offset > 0) 2349753b868SStefan Roesch iop = iomap_page_create(iter->inode, folio, iter->flags); 235cd1e5afeSMatthew Wilcox (Oracle) else 236cd1e5afeSMatthew Wilcox (Oracle) iop = to_iomap_page(folio); 237afc51aaaSDarrick J. Wong 238874628a2SMatthew Wilcox (Oracle) addr = kmap_local_folio(folio, offset); 239afc51aaaSDarrick J. Wong memcpy(addr, iomap->inline_data, size); 240b405435bSMatthew Wilcox (Oracle) memset(addr + size, 0, PAGE_SIZE - poff - size); 241ab069d5fSMatthew Wilcox (Oracle) kunmap_local(addr); 242431c0566SMatthew Wilcox (Oracle) iomap_set_range_uptodate(folio, iop, offset, PAGE_SIZE - poff); 2435ad448ceSAndreas Gruenbacher return 0; 244afc51aaaSDarrick J. Wong } 245afc51aaaSDarrick J. Wong 246fad0a1abSChristoph Hellwig static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter, 2471b5c1e36SChristoph Hellwig loff_t pos) 248009d8d84SChristoph Hellwig { 249fad0a1abSChristoph Hellwig const struct iomap *srcmap = iomap_iter_srcmap(iter); 2501b5c1e36SChristoph Hellwig 2511b5c1e36SChristoph Hellwig return srcmap->type != IOMAP_MAPPED || 2521b5c1e36SChristoph Hellwig (srcmap->flags & IOMAP_F_NEW) || 2531b5c1e36SChristoph Hellwig pos >= i_size_read(iter->inode); 254009d8d84SChristoph Hellwig } 255009d8d84SChristoph Hellwig 256fad0a1abSChristoph Hellwig static loff_t iomap_readpage_iter(const struct iomap_iter *iter, 257f6d48000SChristoph Hellwig struct iomap_readpage_ctx *ctx, loff_t offset) 258afc51aaaSDarrick J. Wong { 259fad0a1abSChristoph Hellwig const struct iomap *iomap = &iter->iomap; 260f6d48000SChristoph Hellwig loff_t pos = iter->pos + offset; 261f6d48000SChristoph Hellwig loff_t length = iomap_length(iter) - offset; 2623aa9c659SMatthew Wilcox (Oracle) struct folio *folio = ctx->cur_folio; 263637d3375SAndreas Gruenbacher struct iomap_page *iop; 264afc51aaaSDarrick J. Wong loff_t orig_pos = pos; 265431c0566SMatthew Wilcox (Oracle) size_t poff, plen; 266afc51aaaSDarrick J. Wong sector_t sector; 267afc51aaaSDarrick J. Wong 2685ad448ceSAndreas Gruenbacher if (iomap->type == IOMAP_INLINE) 269874628a2SMatthew Wilcox (Oracle) return iomap_read_inline_data(iter, folio); 270afc51aaaSDarrick J. Wong 271afc51aaaSDarrick J. Wong /* zero post-eof blocks as the page may be mapped */ 2729753b868SStefan Roesch iop = iomap_page_create(iter->inode, folio, iter->flags); 273431c0566SMatthew Wilcox (Oracle) iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen); 274afc51aaaSDarrick J. Wong if (plen == 0) 275afc51aaaSDarrick J. Wong goto done; 276afc51aaaSDarrick J. Wong 2771b5c1e36SChristoph Hellwig if (iomap_block_needs_zeroing(iter, pos)) { 278431c0566SMatthew Wilcox (Oracle) folio_zero_range(folio, poff, plen); 279431c0566SMatthew Wilcox (Oracle) iomap_set_range_uptodate(folio, iop, poff, plen); 280afc51aaaSDarrick J. Wong goto done; 281afc51aaaSDarrick J. Wong } 282afc51aaaSDarrick J. Wong 2833aa9c659SMatthew Wilcox (Oracle) ctx->cur_folio_in_bio = true; 2847d636676SMatthew Wilcox (Oracle) if (iop) 2857d636676SMatthew Wilcox (Oracle) atomic_add(plen, &iop->read_bytes_pending); 286afc51aaaSDarrick J. Wong 287afc51aaaSDarrick J. Wong sector = iomap_sector(iomap, pos); 288d0364f94SChristoph Hellwig if (!ctx->bio || 289d0364f94SChristoph Hellwig bio_end_sector(ctx->bio) != sector || 290431c0566SMatthew Wilcox (Oracle) !bio_add_folio(ctx->bio, folio, plen, poff)) { 2913aa9c659SMatthew Wilcox (Oracle) gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL); 292457df33eSMatthew Wilcox (Oracle) gfp_t orig_gfp = gfp; 2935f7136dbSMatthew Wilcox (Oracle) unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE); 294afc51aaaSDarrick J. Wong 295afc51aaaSDarrick J. Wong if (ctx->bio) 296afc51aaaSDarrick J. Wong submit_bio(ctx->bio); 297afc51aaaSDarrick J. Wong 2989d24a13aSMatthew Wilcox (Oracle) if (ctx->rac) /* same as readahead_gfp_mask */ 299afc51aaaSDarrick J. Wong gfp |= __GFP_NORETRY | __GFP_NOWARN; 30007888c66SChristoph Hellwig ctx->bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs), 30107888c66SChristoph Hellwig REQ_OP_READ, gfp); 302457df33eSMatthew Wilcox (Oracle) /* 303457df33eSMatthew Wilcox (Oracle) * If the bio_alloc fails, try it again for a single page to 304457df33eSMatthew Wilcox (Oracle) * avoid having to deal with partial page reads. This emulates 305f132ab7dSMatthew Wilcox (Oracle) * what do_mpage_read_folio does. 306457df33eSMatthew Wilcox (Oracle) */ 30707888c66SChristoph Hellwig if (!ctx->bio) { 30807888c66SChristoph Hellwig ctx->bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ, 30907888c66SChristoph Hellwig orig_gfp); 31007888c66SChristoph Hellwig } 3119d24a13aSMatthew Wilcox (Oracle) if (ctx->rac) 312afc51aaaSDarrick J. Wong ctx->bio->bi_opf |= REQ_RAHEAD; 313afc51aaaSDarrick J. Wong ctx->bio->bi_iter.bi_sector = sector; 314afc51aaaSDarrick J. Wong ctx->bio->bi_end_io = iomap_read_end_io; 315431c0566SMatthew Wilcox (Oracle) bio_add_folio(ctx->bio, folio, plen, poff); 316afc51aaaSDarrick J. Wong } 317431c0566SMatthew Wilcox (Oracle) 318afc51aaaSDarrick J. Wong done: 319afc51aaaSDarrick J. Wong /* 320afc51aaaSDarrick J. Wong * Move the caller beyond our range so that it keeps making progress. 321f1f264b4SAndreas Gruenbacher * For that, we have to include any leading non-uptodate ranges, but 322afc51aaaSDarrick J. Wong * we can skip trailing ones as they will be handled in the next 323afc51aaaSDarrick J. Wong * iteration. 324afc51aaaSDarrick J. Wong */ 325afc51aaaSDarrick J. Wong return pos - orig_pos + plen; 326afc51aaaSDarrick J. Wong } 327afc51aaaSDarrick J. Wong 3287479c505SMatthew Wilcox (Oracle) int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops) 329afc51aaaSDarrick J. Wong { 330f6d48000SChristoph Hellwig struct iomap_iter iter = { 3313aa9c659SMatthew Wilcox (Oracle) .inode = folio->mapping->host, 3323aa9c659SMatthew Wilcox (Oracle) .pos = folio_pos(folio), 3333aa9c659SMatthew Wilcox (Oracle) .len = folio_size(folio), 334f6d48000SChristoph Hellwig }; 335f6d48000SChristoph Hellwig struct iomap_readpage_ctx ctx = { 3363aa9c659SMatthew Wilcox (Oracle) .cur_folio = folio, 337f6d48000SChristoph Hellwig }; 338f6d48000SChristoph Hellwig int ret; 339afc51aaaSDarrick J. Wong 3403aa9c659SMatthew Wilcox (Oracle) trace_iomap_readpage(iter.inode, 1); 3419e91c572SChristoph Hellwig 342f6d48000SChristoph Hellwig while ((ret = iomap_iter(&iter, ops)) > 0) 343f6d48000SChristoph Hellwig iter.processed = iomap_readpage_iter(&iter, &ctx, 0); 344f6d48000SChristoph Hellwig 345f6d48000SChristoph Hellwig if (ret < 0) 3463aa9c659SMatthew Wilcox (Oracle) folio_set_error(folio); 347afc51aaaSDarrick J. Wong 348afc51aaaSDarrick J. Wong if (ctx.bio) { 349afc51aaaSDarrick J. Wong submit_bio(ctx.bio); 3503aa9c659SMatthew Wilcox (Oracle) WARN_ON_ONCE(!ctx.cur_folio_in_bio); 351afc51aaaSDarrick J. Wong } else { 3523aa9c659SMatthew Wilcox (Oracle) WARN_ON_ONCE(ctx.cur_folio_in_bio); 3533aa9c659SMatthew Wilcox (Oracle) folio_unlock(folio); 354afc51aaaSDarrick J. Wong } 355afc51aaaSDarrick J. Wong 356afc51aaaSDarrick J. Wong /* 3572c69e205SMatthew Wilcox (Oracle) * Just like mpage_readahead and block_read_full_folio, we always 3587479c505SMatthew Wilcox (Oracle) * return 0 and just set the folio error flag on errors. This 359f1f264b4SAndreas Gruenbacher * should be cleaned up throughout the stack eventually. 360afc51aaaSDarrick J. Wong */ 361afc51aaaSDarrick J. Wong return 0; 362afc51aaaSDarrick J. Wong } 3637479c505SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_read_folio); 364afc51aaaSDarrick J. Wong 365fad0a1abSChristoph Hellwig static loff_t iomap_readahead_iter(const struct iomap_iter *iter, 366f6d48000SChristoph Hellwig struct iomap_readpage_ctx *ctx) 367afc51aaaSDarrick J. Wong { 368f6d48000SChristoph Hellwig loff_t length = iomap_length(iter); 369afc51aaaSDarrick J. Wong loff_t done, ret; 370afc51aaaSDarrick J. Wong 371afc51aaaSDarrick J. Wong for (done = 0; done < length; done += ret) { 3723aa9c659SMatthew Wilcox (Oracle) if (ctx->cur_folio && 3733aa9c659SMatthew Wilcox (Oracle) offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) { 3743aa9c659SMatthew Wilcox (Oracle) if (!ctx->cur_folio_in_bio) 3753aa9c659SMatthew Wilcox (Oracle) folio_unlock(ctx->cur_folio); 3763aa9c659SMatthew Wilcox (Oracle) ctx->cur_folio = NULL; 377afc51aaaSDarrick J. Wong } 3783aa9c659SMatthew Wilcox (Oracle) if (!ctx->cur_folio) { 3793aa9c659SMatthew Wilcox (Oracle) ctx->cur_folio = readahead_folio(ctx->rac); 3803aa9c659SMatthew Wilcox (Oracle) ctx->cur_folio_in_bio = false; 381afc51aaaSDarrick J. Wong } 382f6d48000SChristoph Hellwig ret = iomap_readpage_iter(iter, ctx, done); 383d8af404fSAndreas Gruenbacher if (ret <= 0) 384d8af404fSAndreas Gruenbacher return ret; 385afc51aaaSDarrick J. Wong } 386afc51aaaSDarrick J. Wong 387afc51aaaSDarrick J. Wong return done; 388afc51aaaSDarrick J. Wong } 389afc51aaaSDarrick J. Wong 3909d24a13aSMatthew Wilcox (Oracle) /** 3919d24a13aSMatthew Wilcox (Oracle) * iomap_readahead - Attempt to read pages from a file. 3929d24a13aSMatthew Wilcox (Oracle) * @rac: Describes the pages to be read. 3939d24a13aSMatthew Wilcox (Oracle) * @ops: The operations vector for the filesystem. 3949d24a13aSMatthew Wilcox (Oracle) * 3959d24a13aSMatthew Wilcox (Oracle) * This function is for filesystems to call to implement their readahead 3969d24a13aSMatthew Wilcox (Oracle) * address_space operation. 3979d24a13aSMatthew Wilcox (Oracle) * 3989d24a13aSMatthew Wilcox (Oracle) * Context: The @ops callbacks may submit I/O (eg to read the addresses of 3999d24a13aSMatthew Wilcox (Oracle) * blocks from disc), and may wait for it. The caller may be trying to 4009d24a13aSMatthew Wilcox (Oracle) * access a different page, and so sleeping excessively should be avoided. 4019d24a13aSMatthew Wilcox (Oracle) * It may allocate memory, but should avoid costly allocations. This 4029d24a13aSMatthew Wilcox (Oracle) * function is called with memalloc_nofs set, so allocations will not cause 4039d24a13aSMatthew Wilcox (Oracle) * the filesystem to be reentered. 4049d24a13aSMatthew Wilcox (Oracle) */ 4059d24a13aSMatthew Wilcox (Oracle) void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops) 406afc51aaaSDarrick J. Wong { 407f6d48000SChristoph Hellwig struct iomap_iter iter = { 408f6d48000SChristoph Hellwig .inode = rac->mapping->host, 409f6d48000SChristoph Hellwig .pos = readahead_pos(rac), 410f6d48000SChristoph Hellwig .len = readahead_length(rac), 411f6d48000SChristoph Hellwig }; 412afc51aaaSDarrick J. Wong struct iomap_readpage_ctx ctx = { 4139d24a13aSMatthew Wilcox (Oracle) .rac = rac, 414afc51aaaSDarrick J. Wong }; 415afc51aaaSDarrick J. Wong 416f6d48000SChristoph Hellwig trace_iomap_readahead(rac->mapping->host, readahead_count(rac)); 4179e91c572SChristoph Hellwig 418f6d48000SChristoph Hellwig while (iomap_iter(&iter, ops) > 0) 419f6d48000SChristoph Hellwig iter.processed = iomap_readahead_iter(&iter, &ctx); 4209d24a13aSMatthew Wilcox (Oracle) 421afc51aaaSDarrick J. Wong if (ctx.bio) 422afc51aaaSDarrick J. Wong submit_bio(ctx.bio); 4233aa9c659SMatthew Wilcox (Oracle) if (ctx.cur_folio) { 4243aa9c659SMatthew Wilcox (Oracle) if (!ctx.cur_folio_in_bio) 4253aa9c659SMatthew Wilcox (Oracle) folio_unlock(ctx.cur_folio); 426afc51aaaSDarrick J. Wong } 427afc51aaaSDarrick J. Wong } 4289d24a13aSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_readahead); 429afc51aaaSDarrick J. Wong 430afc51aaaSDarrick J. Wong /* 4312e7e80f7SMatthew Wilcox (Oracle) * iomap_is_partially_uptodate checks whether blocks within a folio are 432afc51aaaSDarrick J. Wong * uptodate or not. 433afc51aaaSDarrick J. Wong * 4342e7e80f7SMatthew Wilcox (Oracle) * Returns true if all blocks which correspond to the specified part 4352e7e80f7SMatthew Wilcox (Oracle) * of the folio are uptodate. 436afc51aaaSDarrick J. Wong */ 4372e7e80f7SMatthew Wilcox (Oracle) bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count) 438afc51aaaSDarrick J. Wong { 43995c4cd05SMatthew Wilcox (Oracle) struct iomap_page *iop = to_iomap_page(folio); 4402e7e80f7SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host; 4412e7e80f7SMatthew Wilcox (Oracle) unsigned first, last, i; 442afc51aaaSDarrick J. Wong 4432e7e80f7SMatthew Wilcox (Oracle) if (!iop) 4442e7e80f7SMatthew Wilcox (Oracle) return false; 4452e7e80f7SMatthew Wilcox (Oracle) 4462756c818SMatthew Wilcox (Oracle) /* Caller's range may extend past the end of this folio */ 4472756c818SMatthew Wilcox (Oracle) count = min(folio_size(folio) - from, count); 448afc51aaaSDarrick J. Wong 4492756c818SMatthew Wilcox (Oracle) /* First and last blocks in range within folio */ 450afc51aaaSDarrick J. Wong first = from >> inode->i_blkbits; 4512756c818SMatthew Wilcox (Oracle) last = (from + count - 1) >> inode->i_blkbits; 452afc51aaaSDarrick J. Wong 453afc51aaaSDarrick J. Wong for (i = first; i <= last; i++) 454afc51aaaSDarrick J. Wong if (!test_bit(i, iop->uptodate)) 4552e7e80f7SMatthew Wilcox (Oracle) return false; 4562e7e80f7SMatthew Wilcox (Oracle) return true; 457afc51aaaSDarrick J. Wong } 458afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate); 459afc51aaaSDarrick J. Wong 4608597447dSMatthew Wilcox (Oracle) bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags) 461afc51aaaSDarrick J. Wong { 4628597447dSMatthew Wilcox (Oracle) trace_iomap_release_folio(folio->mapping->host, folio_pos(folio), 46339f16c83SMatthew Wilcox (Oracle) folio_size(folio)); 4649e91c572SChristoph Hellwig 465afc51aaaSDarrick J. Wong /* 4668597447dSMatthew Wilcox (Oracle) * mm accommodates an old ext3 case where clean folios might 4678597447dSMatthew Wilcox (Oracle) * not have had the dirty bit cleared. Thus, it can send actual 4688597447dSMatthew Wilcox (Oracle) * dirty folios to ->release_folio() via shrink_active_list(); 4698597447dSMatthew Wilcox (Oracle) * skip those here. 470afc51aaaSDarrick J. Wong */ 47139f16c83SMatthew Wilcox (Oracle) if (folio_test_dirty(folio) || folio_test_writeback(folio)) 4728597447dSMatthew Wilcox (Oracle) return false; 473c46e8324SMatthew Wilcox (Oracle) iomap_page_release(folio); 4748597447dSMatthew Wilcox (Oracle) return true; 475afc51aaaSDarrick J. Wong } 4768597447dSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_release_folio); 477afc51aaaSDarrick J. Wong 4788306a5f5SMatthew Wilcox (Oracle) void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len) 479afc51aaaSDarrick J. Wong { 480d82354f6SMatthew Wilcox (Oracle) trace_iomap_invalidate_folio(folio->mapping->host, 4811241ebecSMatthew Wilcox (Oracle) folio_pos(folio) + offset, len); 4829e91c572SChristoph Hellwig 483afc51aaaSDarrick J. Wong /* 48460d82310SMatthew Wilcox (Oracle) * If we're invalidating the entire folio, clear the dirty state 48560d82310SMatthew Wilcox (Oracle) * from it and release it to avoid unnecessary buildup of the LRU. 486afc51aaaSDarrick J. Wong */ 4878306a5f5SMatthew Wilcox (Oracle) if (offset == 0 && len == folio_size(folio)) { 4888306a5f5SMatthew Wilcox (Oracle) WARN_ON_ONCE(folio_test_writeback(folio)); 4898306a5f5SMatthew Wilcox (Oracle) folio_cancel_dirty(folio); 490c46e8324SMatthew Wilcox (Oracle) iomap_page_release(folio); 49160d82310SMatthew Wilcox (Oracle) } else if (folio_test_large(folio)) { 49260d82310SMatthew Wilcox (Oracle) /* Must release the iop so the page can be split */ 49360d82310SMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_uptodate(folio) && 49460d82310SMatthew Wilcox (Oracle) folio_test_dirty(folio)); 49560d82310SMatthew Wilcox (Oracle) iomap_page_release(folio); 496afc51aaaSDarrick J. Wong } 497afc51aaaSDarrick J. Wong } 4988306a5f5SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_invalidate_folio); 4998306a5f5SMatthew Wilcox (Oracle) 500afc51aaaSDarrick J. Wong static void 501afc51aaaSDarrick J. Wong iomap_write_failed(struct inode *inode, loff_t pos, unsigned len) 502afc51aaaSDarrick J. Wong { 503afc51aaaSDarrick J. Wong loff_t i_size = i_size_read(inode); 504afc51aaaSDarrick J. Wong 505afc51aaaSDarrick J. Wong /* 506afc51aaaSDarrick J. Wong * Only truncate newly allocated pages beyoned EOF, even if the 507afc51aaaSDarrick J. Wong * write started inside the existing inode size. 508afc51aaaSDarrick J. Wong */ 509afc51aaaSDarrick J. Wong if (pos + len > i_size) 510b71450e2SAndreas Gruenbacher truncate_pagecache_range(inode, max(pos, i_size), 511b71450e2SAndreas Gruenbacher pos + len - 1); 512afc51aaaSDarrick J. Wong } 513afc51aaaSDarrick J. Wong 514431c0566SMatthew Wilcox (Oracle) static int iomap_read_folio_sync(loff_t block_start, struct folio *folio, 515431c0566SMatthew Wilcox (Oracle) size_t poff, size_t plen, const struct iomap *iomap) 516afc51aaaSDarrick J. Wong { 517afc51aaaSDarrick J. Wong struct bio_vec bvec; 518afc51aaaSDarrick J. Wong struct bio bio; 519afc51aaaSDarrick J. Wong 52049add496SChristoph Hellwig bio_init(&bio, iomap->bdev, &bvec, 1, REQ_OP_READ); 521afc51aaaSDarrick J. Wong bio.bi_iter.bi_sector = iomap_sector(iomap, block_start); 522431c0566SMatthew Wilcox (Oracle) bio_add_folio(&bio, folio, plen, poff); 523afc51aaaSDarrick J. Wong return submit_bio_wait(&bio); 524afc51aaaSDarrick J. Wong } 525afc51aaaSDarrick J. Wong 526fad0a1abSChristoph Hellwig static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos, 527bc6123a8SMatthew Wilcox (Oracle) size_t len, struct folio *folio) 528afc51aaaSDarrick J. Wong { 529fad0a1abSChristoph Hellwig const struct iomap *srcmap = iomap_iter_srcmap(iter); 5309753b868SStefan Roesch struct iomap_page *iop; 5311b5c1e36SChristoph Hellwig loff_t block_size = i_blocksize(iter->inode); 5326cc19c5fSNikolay Borisov loff_t block_start = round_down(pos, block_size); 5336cc19c5fSNikolay Borisov loff_t block_end = round_up(pos + len, block_size); 534cae2de69SStefan Roesch unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio); 535431c0566SMatthew Wilcox (Oracle) size_t from = offset_in_folio(folio, pos), to = from + len; 536431c0566SMatthew Wilcox (Oracle) size_t poff, plen; 537afc51aaaSDarrick J. Wong 538431c0566SMatthew Wilcox (Oracle) if (folio_test_uptodate(folio)) 539afc51aaaSDarrick J. Wong return 0; 540431c0566SMatthew Wilcox (Oracle) folio_clear_error(folio); 541afc51aaaSDarrick J. Wong 5429753b868SStefan Roesch iop = iomap_page_create(iter->inode, folio, iter->flags); 543cae2de69SStefan Roesch if ((iter->flags & IOMAP_NOWAIT) && !iop && nr_blocks > 1) 544cae2de69SStefan Roesch return -EAGAIN; 5459753b868SStefan Roesch 546afc51aaaSDarrick J. Wong do { 547431c0566SMatthew Wilcox (Oracle) iomap_adjust_read_range(iter->inode, folio, &block_start, 548afc51aaaSDarrick J. Wong block_end - block_start, &poff, &plen); 549afc51aaaSDarrick J. Wong if (plen == 0) 550afc51aaaSDarrick J. Wong break; 551afc51aaaSDarrick J. Wong 552b74b1293SChristoph Hellwig if (!(iter->flags & IOMAP_UNSHARE) && 55332a38a49SChristoph Hellwig (from <= poff || from >= poff + plen) && 554d3b40439SChristoph Hellwig (to <= poff || to >= poff + plen)) 555d3b40439SChristoph Hellwig continue; 556d3b40439SChristoph Hellwig 5571b5c1e36SChristoph Hellwig if (iomap_block_needs_zeroing(iter, block_start)) { 558b74b1293SChristoph Hellwig if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE)) 55932a38a49SChristoph Hellwig return -EIO; 560431c0566SMatthew Wilcox (Oracle) folio_zero_segments(folio, poff, from, to, poff + plen); 56114284fedSMatthew Wilcox (Oracle) } else { 562cae2de69SStefan Roesch int status; 563cae2de69SStefan Roesch 564cae2de69SStefan Roesch if (iter->flags & IOMAP_NOWAIT) 565cae2de69SStefan Roesch return -EAGAIN; 566cae2de69SStefan Roesch 567cae2de69SStefan Roesch status = iomap_read_folio_sync(block_start, folio, 56814284fedSMatthew Wilcox (Oracle) poff, plen, srcmap); 569d3b40439SChristoph Hellwig if (status) 570d3b40439SChristoph Hellwig return status; 57114284fedSMatthew Wilcox (Oracle) } 572431c0566SMatthew Wilcox (Oracle) iomap_set_range_uptodate(folio, iop, poff, plen); 573afc51aaaSDarrick J. Wong } while ((block_start += plen) < block_end); 574afc51aaaSDarrick J. Wong 575d3b40439SChristoph Hellwig return 0; 576afc51aaaSDarrick J. Wong } 577afc51aaaSDarrick J. Wong 578fad0a1abSChristoph Hellwig static int iomap_write_begin_inline(const struct iomap_iter *iter, 579bc6123a8SMatthew Wilcox (Oracle) struct folio *folio) 58069f4a26cSGao Xiang { 58169f4a26cSGao Xiang /* needs more work for the tailpacking case; disable for now */ 5821b5c1e36SChristoph Hellwig if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0)) 58369f4a26cSGao Xiang return -EIO; 584874628a2SMatthew Wilcox (Oracle) return iomap_read_inline_data(iter, folio); 58569f4a26cSGao Xiang } 58669f4a26cSGao Xiang 587fad0a1abSChristoph Hellwig static int iomap_write_begin(const struct iomap_iter *iter, loff_t pos, 588bc6123a8SMatthew Wilcox (Oracle) size_t len, struct folio **foliop) 589afc51aaaSDarrick J. Wong { 5901b5c1e36SChristoph Hellwig const struct iomap_page_ops *page_ops = iter->iomap.page_ops; 591fad0a1abSChristoph Hellwig const struct iomap *srcmap = iomap_iter_srcmap(iter); 592d1bd0b4eSMatthew Wilcox (Oracle) struct folio *folio; 593bc6123a8SMatthew Wilcox (Oracle) unsigned fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE | FGP_NOFS; 594afc51aaaSDarrick J. Wong int status = 0; 595afc51aaaSDarrick J. Wong 596cae2de69SStefan Roesch if (iter->flags & IOMAP_NOWAIT) 597cae2de69SStefan Roesch fgp |= FGP_NOWAIT; 598cae2de69SStefan Roesch 5991b5c1e36SChristoph Hellwig BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length); 6001b5c1e36SChristoph Hellwig if (srcmap != &iter->iomap) 601c039b997SGoldwyn Rodrigues BUG_ON(pos + len > srcmap->offset + srcmap->length); 602afc51aaaSDarrick J. Wong 603afc51aaaSDarrick J. Wong if (fatal_signal_pending(current)) 604afc51aaaSDarrick J. Wong return -EINTR; 605afc51aaaSDarrick J. Wong 606d454ab82SMatthew Wilcox (Oracle) if (!mapping_large_folio_support(iter->inode->i_mapping)) 607d454ab82SMatthew Wilcox (Oracle) len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos)); 608d454ab82SMatthew Wilcox (Oracle) 609afc51aaaSDarrick J. Wong if (page_ops && page_ops->page_prepare) { 6101b5c1e36SChristoph Hellwig status = page_ops->page_prepare(iter->inode, pos, len); 611afc51aaaSDarrick J. Wong if (status) 612afc51aaaSDarrick J. Wong return status; 613afc51aaaSDarrick J. Wong } 614afc51aaaSDarrick J. Wong 615bc6123a8SMatthew Wilcox (Oracle) folio = __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT, 616bc6123a8SMatthew Wilcox (Oracle) fgp, mapping_gfp_mask(iter->inode->i_mapping)); 617bc6123a8SMatthew Wilcox (Oracle) if (!folio) { 618cae2de69SStefan Roesch status = (iter->flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOMEM; 619afc51aaaSDarrick J. Wong goto out_no_page; 620afc51aaaSDarrick J. Wong } 621d454ab82SMatthew Wilcox (Oracle) if (pos + len > folio_pos(folio) + folio_size(folio)) 622d454ab82SMatthew Wilcox (Oracle) len = folio_pos(folio) + folio_size(folio) - pos; 623afc51aaaSDarrick J. Wong 624c039b997SGoldwyn Rodrigues if (srcmap->type == IOMAP_INLINE) 625bc6123a8SMatthew Wilcox (Oracle) status = iomap_write_begin_inline(iter, folio); 6261b5c1e36SChristoph Hellwig else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) 627d1bd0b4eSMatthew Wilcox (Oracle) status = __block_write_begin_int(folio, pos, len, NULL, srcmap); 628afc51aaaSDarrick J. Wong else 629bc6123a8SMatthew Wilcox (Oracle) status = __iomap_write_begin(iter, pos, len, folio); 630afc51aaaSDarrick J. Wong 631afc51aaaSDarrick J. Wong if (unlikely(status)) 632afc51aaaSDarrick J. Wong goto out_unlock; 633afc51aaaSDarrick J. Wong 634bc6123a8SMatthew Wilcox (Oracle) *foliop = folio; 635afc51aaaSDarrick J. Wong return 0; 636afc51aaaSDarrick J. Wong 637afc51aaaSDarrick J. Wong out_unlock: 638bc6123a8SMatthew Wilcox (Oracle) folio_unlock(folio); 639bc6123a8SMatthew Wilcox (Oracle) folio_put(folio); 6401b5c1e36SChristoph Hellwig iomap_write_failed(iter->inode, pos, len); 641afc51aaaSDarrick J. Wong 642afc51aaaSDarrick J. Wong out_no_page: 643afc51aaaSDarrick J. Wong if (page_ops && page_ops->page_done) 6441b5c1e36SChristoph Hellwig page_ops->page_done(iter->inode, pos, 0, NULL); 645afc51aaaSDarrick J. Wong return status; 646afc51aaaSDarrick J. Wong } 647afc51aaaSDarrick J. Wong 648e25ba8cbSMatthew Wilcox (Oracle) static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len, 649bc6123a8SMatthew Wilcox (Oracle) size_t copied, struct folio *folio) 650afc51aaaSDarrick J. Wong { 651cd1e5afeSMatthew Wilcox (Oracle) struct iomap_page *iop = to_iomap_page(folio); 652bc6123a8SMatthew Wilcox (Oracle) flush_dcache_folio(folio); 653afc51aaaSDarrick J. Wong 654afc51aaaSDarrick J. Wong /* 655afc51aaaSDarrick J. Wong * The blocks that were entirely written will now be uptodate, so we 6567479c505SMatthew Wilcox (Oracle) * don't have to worry about a read_folio reading them and overwriting a 657f1f264b4SAndreas Gruenbacher * partial write. However, if we've encountered a short write and only 658afc51aaaSDarrick J. Wong * partially written into a block, it will not be marked uptodate, so a 6597479c505SMatthew Wilcox (Oracle) * read_folio might come in and destroy our partial write. 660afc51aaaSDarrick J. Wong * 661f1f264b4SAndreas Gruenbacher * Do the simplest thing and just treat any short write to a 662f1f264b4SAndreas Gruenbacher * non-uptodate page as a zero-length write, and force the caller to 663f1f264b4SAndreas Gruenbacher * redo the whole thing. 664afc51aaaSDarrick J. Wong */ 665bc6123a8SMatthew Wilcox (Oracle) if (unlikely(copied < len && !folio_test_uptodate(folio))) 666afc51aaaSDarrick J. Wong return 0; 667431c0566SMatthew Wilcox (Oracle) iomap_set_range_uptodate(folio, iop, offset_in_folio(folio, pos), len); 668bc6123a8SMatthew Wilcox (Oracle) filemap_dirty_folio(inode->i_mapping, folio); 669afc51aaaSDarrick J. Wong return copied; 670afc51aaaSDarrick J. Wong } 671afc51aaaSDarrick J. Wong 672fad0a1abSChristoph Hellwig static size_t iomap_write_end_inline(const struct iomap_iter *iter, 6739c4ce08dSMatthew Wilcox (Oracle) struct folio *folio, loff_t pos, size_t copied) 674afc51aaaSDarrick J. Wong { 675fad0a1abSChristoph Hellwig const struct iomap *iomap = &iter->iomap; 676afc51aaaSDarrick J. Wong void *addr; 677afc51aaaSDarrick J. Wong 6789c4ce08dSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_uptodate(folio)); 67969f4a26cSGao Xiang BUG_ON(!iomap_inline_data_valid(iomap)); 680afc51aaaSDarrick J. Wong 6819c4ce08dSMatthew Wilcox (Oracle) flush_dcache_folio(folio); 6829c4ce08dSMatthew Wilcox (Oracle) addr = kmap_local_folio(folio, pos); 683ab069d5fSMatthew Wilcox (Oracle) memcpy(iomap_inline_data(iomap, pos), addr, copied); 684ab069d5fSMatthew Wilcox (Oracle) kunmap_local(addr); 685afc51aaaSDarrick J. Wong 6861b5c1e36SChristoph Hellwig mark_inode_dirty(iter->inode); 687afc51aaaSDarrick J. Wong return copied; 688afc51aaaSDarrick J. Wong } 689afc51aaaSDarrick J. Wong 690e25ba8cbSMatthew Wilcox (Oracle) /* Returns the number of bytes copied. May be 0. Cannot be an errno. */ 6911b5c1e36SChristoph Hellwig static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len, 692bc6123a8SMatthew Wilcox (Oracle) size_t copied, struct folio *folio) 693afc51aaaSDarrick J. Wong { 6941b5c1e36SChristoph Hellwig const struct iomap_page_ops *page_ops = iter->iomap.page_ops; 695fad0a1abSChristoph Hellwig const struct iomap *srcmap = iomap_iter_srcmap(iter); 6961b5c1e36SChristoph Hellwig loff_t old_size = iter->inode->i_size; 697e25ba8cbSMatthew Wilcox (Oracle) size_t ret; 698afc51aaaSDarrick J. Wong 699c039b997SGoldwyn Rodrigues if (srcmap->type == IOMAP_INLINE) { 7009c4ce08dSMatthew Wilcox (Oracle) ret = iomap_write_end_inline(iter, folio, pos, copied); 701c039b997SGoldwyn Rodrigues } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) { 7021b5c1e36SChristoph Hellwig ret = block_write_end(NULL, iter->inode->i_mapping, pos, len, 703bc6123a8SMatthew Wilcox (Oracle) copied, &folio->page, NULL); 704afc51aaaSDarrick J. Wong } else { 705bc6123a8SMatthew Wilcox (Oracle) ret = __iomap_write_end(iter->inode, pos, len, copied, folio); 706afc51aaaSDarrick J. Wong } 707afc51aaaSDarrick J. Wong 708afc51aaaSDarrick J. Wong /* 709afc51aaaSDarrick J. Wong * Update the in-memory inode size after copying the data into the page 710afc51aaaSDarrick J. Wong * cache. It's up to the file system to write the updated size to disk, 711afc51aaaSDarrick J. Wong * preferably after I/O completion so that no stale data is exposed. 712afc51aaaSDarrick J. Wong */ 713afc51aaaSDarrick J. Wong if (pos + ret > old_size) { 7141b5c1e36SChristoph Hellwig i_size_write(iter->inode, pos + ret); 7151b5c1e36SChristoph Hellwig iter->iomap.flags |= IOMAP_F_SIZE_CHANGED; 716afc51aaaSDarrick J. Wong } 717bc6123a8SMatthew Wilcox (Oracle) folio_unlock(folio); 718afc51aaaSDarrick J. Wong 719afc51aaaSDarrick J. Wong if (old_size < pos) 7201b5c1e36SChristoph Hellwig pagecache_isize_extended(iter->inode, old_size, pos); 721afc51aaaSDarrick J. Wong if (page_ops && page_ops->page_done) 722bc6123a8SMatthew Wilcox (Oracle) page_ops->page_done(iter->inode, pos, ret, &folio->page); 723bc6123a8SMatthew Wilcox (Oracle) folio_put(folio); 724afc51aaaSDarrick J. Wong 725afc51aaaSDarrick J. Wong if (ret < len) 726d74999c8SAndreas Gruenbacher iomap_write_failed(iter->inode, pos + ret, len - ret); 727afc51aaaSDarrick J. Wong return ret; 728afc51aaaSDarrick J. Wong } 729afc51aaaSDarrick J. Wong 730ce83a025SChristoph Hellwig static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i) 731afc51aaaSDarrick J. Wong { 732ce83a025SChristoph Hellwig loff_t length = iomap_length(iter); 733ce83a025SChristoph Hellwig loff_t pos = iter->pos; 734afc51aaaSDarrick J. Wong ssize_t written = 0; 735ce83a025SChristoph Hellwig long status = 0; 736cae2de69SStefan Roesch struct address_space *mapping = iter->inode->i_mapping; 737cae2de69SStefan Roesch unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0; 738afc51aaaSDarrick J. Wong 739afc51aaaSDarrick J. Wong do { 740bc6123a8SMatthew Wilcox (Oracle) struct folio *folio; 741afc51aaaSDarrick J. Wong struct page *page; 742afc51aaaSDarrick J. Wong unsigned long offset; /* Offset into pagecache page */ 743afc51aaaSDarrick J. Wong unsigned long bytes; /* Bytes to write to page */ 744afc51aaaSDarrick J. Wong size_t copied; /* Bytes copied from user */ 745afc51aaaSDarrick J. Wong 746afc51aaaSDarrick J. Wong offset = offset_in_page(pos); 747afc51aaaSDarrick J. Wong bytes = min_t(unsigned long, PAGE_SIZE - offset, 748afc51aaaSDarrick J. Wong iov_iter_count(i)); 749afc51aaaSDarrick J. Wong again: 750cae2de69SStefan Roesch status = balance_dirty_pages_ratelimited_flags(mapping, 751cae2de69SStefan Roesch bdp_flags); 752cae2de69SStefan Roesch if (unlikely(status)) 753cae2de69SStefan Roesch break; 754cae2de69SStefan Roesch 755afc51aaaSDarrick J. Wong if (bytes > length) 756afc51aaaSDarrick J. Wong bytes = length; 757afc51aaaSDarrick J. Wong 758afc51aaaSDarrick J. Wong /* 759f1f264b4SAndreas Gruenbacher * Bring in the user page that we'll copy from _first_. 760afc51aaaSDarrick J. Wong * Otherwise there's a nasty deadlock on copying from the 761afc51aaaSDarrick J. Wong * same page as we're writing to, without it being marked 762afc51aaaSDarrick J. Wong * up-to-date. 763cae2de69SStefan Roesch * 764cae2de69SStefan Roesch * For async buffered writes the assumption is that the user 765cae2de69SStefan Roesch * page has already been faulted in. This can be optimized by 766cae2de69SStefan Roesch * faulting the user page. 767afc51aaaSDarrick J. Wong */ 768631f871fSAndreas Gruenbacher if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) { 769afc51aaaSDarrick J. Wong status = -EFAULT; 770afc51aaaSDarrick J. Wong break; 771afc51aaaSDarrick J. Wong } 772afc51aaaSDarrick J. Wong 773bc6123a8SMatthew Wilcox (Oracle) status = iomap_write_begin(iter, pos, bytes, &folio); 774afc51aaaSDarrick J. Wong if (unlikely(status)) 775afc51aaaSDarrick J. Wong break; 776afc51aaaSDarrick J. Wong 777bc6123a8SMatthew Wilcox (Oracle) page = folio_file_page(folio, pos >> PAGE_SHIFT); 778cae2de69SStefan Roesch if (mapping_writably_mapped(mapping)) 779afc51aaaSDarrick J. Wong flush_dcache_page(page); 780afc51aaaSDarrick J. Wong 781f0b65f39SAl Viro copied = copy_page_from_iter_atomic(page, offset, bytes, i); 782afc51aaaSDarrick J. Wong 783bc6123a8SMatthew Wilcox (Oracle) status = iomap_write_end(iter, pos, bytes, copied, folio); 784afc51aaaSDarrick J. Wong 785f0b65f39SAl Viro if (unlikely(copied != status)) 786f0b65f39SAl Viro iov_iter_revert(i, copied - status); 787afc51aaaSDarrick J. Wong 788f0b65f39SAl Viro cond_resched(); 789bc1bb416SAl Viro if (unlikely(status == 0)) { 790afc51aaaSDarrick J. Wong /* 791bc1bb416SAl Viro * A short copy made iomap_write_end() reject the 792bc1bb416SAl Viro * thing entirely. Might be memory poisoning 793bc1bb416SAl Viro * halfway through, might be a race with munmap, 794bc1bb416SAl Viro * might be severe memory pressure. 795afc51aaaSDarrick J. Wong */ 796bc1bb416SAl Viro if (copied) 797bc1bb416SAl Viro bytes = copied; 798afc51aaaSDarrick J. Wong goto again; 799afc51aaaSDarrick J. Wong } 800f0b65f39SAl Viro pos += status; 801f0b65f39SAl Viro written += status; 802f0b65f39SAl Viro length -= status; 803afc51aaaSDarrick J. Wong } while (iov_iter_count(i) && length); 804afc51aaaSDarrick J. Wong 80518e419f6SStefan Roesch if (status == -EAGAIN) { 80618e419f6SStefan Roesch iov_iter_revert(i, written); 80718e419f6SStefan Roesch return -EAGAIN; 80818e419f6SStefan Roesch } 809afc51aaaSDarrick J. Wong return written ? written : status; 810afc51aaaSDarrick J. Wong } 811afc51aaaSDarrick J. Wong 812afc51aaaSDarrick J. Wong ssize_t 813ce83a025SChristoph Hellwig iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i, 814afc51aaaSDarrick J. Wong const struct iomap_ops *ops) 815afc51aaaSDarrick J. Wong { 816ce83a025SChristoph Hellwig struct iomap_iter iter = { 817ce83a025SChristoph Hellwig .inode = iocb->ki_filp->f_mapping->host, 818ce83a025SChristoph Hellwig .pos = iocb->ki_pos, 819ce83a025SChristoph Hellwig .len = iov_iter_count(i), 820ce83a025SChristoph Hellwig .flags = IOMAP_WRITE, 821ce83a025SChristoph Hellwig }; 822ce83a025SChristoph Hellwig int ret; 823afc51aaaSDarrick J. Wong 824cae2de69SStefan Roesch if (iocb->ki_flags & IOCB_NOWAIT) 825cae2de69SStefan Roesch iter.flags |= IOMAP_NOWAIT; 826cae2de69SStefan Roesch 827ce83a025SChristoph Hellwig while ((ret = iomap_iter(&iter, ops)) > 0) 828ce83a025SChristoph Hellwig iter.processed = iomap_write_iter(&iter, i); 829ce83a025SChristoph Hellwig if (iter.pos == iocb->ki_pos) 830ce83a025SChristoph Hellwig return ret; 831ce83a025SChristoph Hellwig return iter.pos - iocb->ki_pos; 832afc51aaaSDarrick J. Wong } 833afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_file_buffered_write); 834afc51aaaSDarrick J. Wong 8359c7babf9SDave Chinner /* 836*f43dc4dcSDave Chinner * Scan the data range passed to us for dirty page cache folios. If we find a 837*f43dc4dcSDave Chinner * dirty folio, punch out the preceeding range and update the offset from which 838*f43dc4dcSDave Chinner * the next punch will start from. 839*f43dc4dcSDave Chinner * 840*f43dc4dcSDave Chinner * We can punch out storage reservations under clean pages because they either 841*f43dc4dcSDave Chinner * contain data that has been written back - in which case the delalloc punch 842*f43dc4dcSDave Chinner * over that range is a no-op - or they have been read faults in which case they 843*f43dc4dcSDave Chinner * contain zeroes and we can remove the delalloc backing range and any new 844*f43dc4dcSDave Chinner * writes to those pages will do the normal hole filling operation... 845*f43dc4dcSDave Chinner * 846*f43dc4dcSDave Chinner * This makes the logic simple: we only need to keep the delalloc extents only 847*f43dc4dcSDave Chinner * over the dirty ranges of the page cache. 848*f43dc4dcSDave Chinner * 849*f43dc4dcSDave Chinner * This function uses [start_byte, end_byte) intervals (i.e. open ended) to 850*f43dc4dcSDave Chinner * simplify range iterations. 851*f43dc4dcSDave Chinner */ 852*f43dc4dcSDave Chinner static int iomap_write_delalloc_scan(struct inode *inode, 853*f43dc4dcSDave Chinner loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte, 854*f43dc4dcSDave Chinner int (*punch)(struct inode *inode, loff_t offset, loff_t length)) 855*f43dc4dcSDave Chinner { 856*f43dc4dcSDave Chinner while (start_byte < end_byte) { 857*f43dc4dcSDave Chinner struct folio *folio; 858*f43dc4dcSDave Chinner 859*f43dc4dcSDave Chinner /* grab locked page */ 860*f43dc4dcSDave Chinner folio = filemap_lock_folio(inode->i_mapping, 861*f43dc4dcSDave Chinner start_byte >> PAGE_SHIFT); 862*f43dc4dcSDave Chinner if (!folio) { 863*f43dc4dcSDave Chinner start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) + 864*f43dc4dcSDave Chinner PAGE_SIZE; 865*f43dc4dcSDave Chinner continue; 866*f43dc4dcSDave Chinner } 867*f43dc4dcSDave Chinner 868*f43dc4dcSDave Chinner /* if dirty, punch up to offset */ 869*f43dc4dcSDave Chinner if (folio_test_dirty(folio)) { 870*f43dc4dcSDave Chinner if (start_byte > *punch_start_byte) { 871*f43dc4dcSDave Chinner int error; 872*f43dc4dcSDave Chinner 873*f43dc4dcSDave Chinner error = punch(inode, *punch_start_byte, 874*f43dc4dcSDave Chinner start_byte - *punch_start_byte); 875*f43dc4dcSDave Chinner if (error) { 876*f43dc4dcSDave Chinner folio_unlock(folio); 877*f43dc4dcSDave Chinner folio_put(folio); 878*f43dc4dcSDave Chinner return error; 879*f43dc4dcSDave Chinner } 880*f43dc4dcSDave Chinner } 881*f43dc4dcSDave Chinner 882*f43dc4dcSDave Chinner /* 883*f43dc4dcSDave Chinner * Make sure the next punch start is correctly bound to 884*f43dc4dcSDave Chinner * the end of this data range, not the end of the folio. 885*f43dc4dcSDave Chinner */ 886*f43dc4dcSDave Chinner *punch_start_byte = min_t(loff_t, end_byte, 887*f43dc4dcSDave Chinner folio_next_index(folio) << PAGE_SHIFT); 888*f43dc4dcSDave Chinner } 889*f43dc4dcSDave Chinner 890*f43dc4dcSDave Chinner /* move offset to start of next folio in range */ 891*f43dc4dcSDave Chinner start_byte = folio_next_index(folio) << PAGE_SHIFT; 892*f43dc4dcSDave Chinner folio_unlock(folio); 893*f43dc4dcSDave Chinner folio_put(folio); 894*f43dc4dcSDave Chinner } 895*f43dc4dcSDave Chinner return 0; 896*f43dc4dcSDave Chinner } 897*f43dc4dcSDave Chinner 898*f43dc4dcSDave Chinner /* 899*f43dc4dcSDave Chinner * Punch out all the delalloc blocks in the range given except for those that 900*f43dc4dcSDave Chinner * have dirty data still pending in the page cache - those are going to be 901*f43dc4dcSDave Chinner * written and so must still retain the delalloc backing for writeback. 902*f43dc4dcSDave Chinner * 903*f43dc4dcSDave Chinner * As we are scanning the page cache for data, we don't need to reimplement the 904*f43dc4dcSDave Chinner * wheel - mapping_seek_hole_data() does exactly what we need to identify the 905*f43dc4dcSDave Chinner * start and end of data ranges correctly even for sub-folio block sizes. This 906*f43dc4dcSDave Chinner * byte range based iteration is especially convenient because it means we 907*f43dc4dcSDave Chinner * don't have to care about variable size folios, nor where the start or end of 908*f43dc4dcSDave Chinner * the data range lies within a folio, if they lie within the same folio or even 909*f43dc4dcSDave Chinner * if there are multiple discontiguous data ranges within the folio. 910*f43dc4dcSDave Chinner * 911*f43dc4dcSDave Chinner * It should be noted that mapping_seek_hole_data() is not aware of EOF, and so 912*f43dc4dcSDave Chinner * can return data ranges that exist in the cache beyond EOF. e.g. a page fault 913*f43dc4dcSDave Chinner * spanning EOF will initialise the post-EOF data to zeroes and mark it up to 914*f43dc4dcSDave Chinner * date. A write page fault can then mark it dirty. If we then fail a write() 915*f43dc4dcSDave Chinner * beyond EOF into that up to date cached range, we allocate a delalloc block 916*f43dc4dcSDave Chinner * beyond EOF and then have to punch it out. Because the range is up to date, 917*f43dc4dcSDave Chinner * mapping_seek_hole_data() will return it, and we will skip the punch because 918*f43dc4dcSDave Chinner * the folio is dirty. THis is incorrect - we always need to punch out delalloc 919*f43dc4dcSDave Chinner * beyond EOF in this case as writeback will never write back and covert that 920*f43dc4dcSDave Chinner * delalloc block beyond EOF. Hence we limit the cached data scan range to EOF, 921*f43dc4dcSDave Chinner * resulting in always punching out the range from the EOF to the end of the 922*f43dc4dcSDave Chinner * range the iomap spans. 923*f43dc4dcSDave Chinner * 924*f43dc4dcSDave Chinner * Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it 925*f43dc4dcSDave Chinner * matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA 926*f43dc4dcSDave Chinner * returns the start of a data range (start_byte), and SEEK_HOLE(start_byte) 927*f43dc4dcSDave Chinner * returns the end of the data range (data_end). Using closed intervals would 928*f43dc4dcSDave Chinner * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose 929*f43dc4dcSDave Chinner * the code to subtle off-by-one bugs.... 930*f43dc4dcSDave Chinner */ 931*f43dc4dcSDave Chinner static int iomap_write_delalloc_release(struct inode *inode, 932*f43dc4dcSDave Chinner loff_t start_byte, loff_t end_byte, 933*f43dc4dcSDave Chinner int (*punch)(struct inode *inode, loff_t pos, loff_t length)) 934*f43dc4dcSDave Chinner { 935*f43dc4dcSDave Chinner loff_t punch_start_byte = start_byte; 936*f43dc4dcSDave Chinner loff_t scan_end_byte = min(i_size_read(inode), end_byte); 937*f43dc4dcSDave Chinner int error = 0; 938*f43dc4dcSDave Chinner 939*f43dc4dcSDave Chinner /* 940*f43dc4dcSDave Chinner * Lock the mapping to avoid races with page faults re-instantiating 941*f43dc4dcSDave Chinner * folios and dirtying them via ->page_mkwrite whilst we walk the 942*f43dc4dcSDave Chinner * cache and perform delalloc extent removal. Failing to do this can 943*f43dc4dcSDave Chinner * leave dirty pages with no space reservation in the cache. 944*f43dc4dcSDave Chinner */ 945*f43dc4dcSDave Chinner filemap_invalidate_lock(inode->i_mapping); 946*f43dc4dcSDave Chinner while (start_byte < scan_end_byte) { 947*f43dc4dcSDave Chinner loff_t data_end; 948*f43dc4dcSDave Chinner 949*f43dc4dcSDave Chinner start_byte = mapping_seek_hole_data(inode->i_mapping, 950*f43dc4dcSDave Chinner start_byte, scan_end_byte, SEEK_DATA); 951*f43dc4dcSDave Chinner /* 952*f43dc4dcSDave Chinner * If there is no more data to scan, all that is left is to 953*f43dc4dcSDave Chinner * punch out the remaining range. 954*f43dc4dcSDave Chinner */ 955*f43dc4dcSDave Chinner if (start_byte == -ENXIO || start_byte == scan_end_byte) 956*f43dc4dcSDave Chinner break; 957*f43dc4dcSDave Chinner if (start_byte < 0) { 958*f43dc4dcSDave Chinner error = start_byte; 959*f43dc4dcSDave Chinner goto out_unlock; 960*f43dc4dcSDave Chinner } 961*f43dc4dcSDave Chinner WARN_ON_ONCE(start_byte < punch_start_byte); 962*f43dc4dcSDave Chinner WARN_ON_ONCE(start_byte > scan_end_byte); 963*f43dc4dcSDave Chinner 964*f43dc4dcSDave Chinner /* 965*f43dc4dcSDave Chinner * We find the end of this contiguous cached data range by 966*f43dc4dcSDave Chinner * seeking from start_byte to the beginning of the next hole. 967*f43dc4dcSDave Chinner */ 968*f43dc4dcSDave Chinner data_end = mapping_seek_hole_data(inode->i_mapping, start_byte, 969*f43dc4dcSDave Chinner scan_end_byte, SEEK_HOLE); 970*f43dc4dcSDave Chinner if (data_end < 0) { 971*f43dc4dcSDave Chinner error = data_end; 972*f43dc4dcSDave Chinner goto out_unlock; 973*f43dc4dcSDave Chinner } 974*f43dc4dcSDave Chinner WARN_ON_ONCE(data_end <= start_byte); 975*f43dc4dcSDave Chinner WARN_ON_ONCE(data_end > scan_end_byte); 976*f43dc4dcSDave Chinner 977*f43dc4dcSDave Chinner error = iomap_write_delalloc_scan(inode, &punch_start_byte, 978*f43dc4dcSDave Chinner start_byte, data_end, punch); 979*f43dc4dcSDave Chinner if (error) 980*f43dc4dcSDave Chinner goto out_unlock; 981*f43dc4dcSDave Chinner 982*f43dc4dcSDave Chinner /* The next data search starts at the end of this one. */ 983*f43dc4dcSDave Chinner start_byte = data_end; 984*f43dc4dcSDave Chinner } 985*f43dc4dcSDave Chinner 986*f43dc4dcSDave Chinner if (punch_start_byte < end_byte) 987*f43dc4dcSDave Chinner error = punch(inode, punch_start_byte, 988*f43dc4dcSDave Chinner end_byte - punch_start_byte); 989*f43dc4dcSDave Chinner out_unlock: 990*f43dc4dcSDave Chinner filemap_invalidate_unlock(inode->i_mapping); 991*f43dc4dcSDave Chinner return error; 992*f43dc4dcSDave Chinner } 993*f43dc4dcSDave Chinner 994*f43dc4dcSDave Chinner /* 9959c7babf9SDave Chinner * When a short write occurs, the filesystem may need to remove reserved space 9969c7babf9SDave Chinner * that was allocated in ->iomap_begin from it's ->iomap_end method. For 9979c7babf9SDave Chinner * filesystems that use delayed allocation, we need to punch out delalloc 9989c7babf9SDave Chinner * extents from the range that are not dirty in the page cache. As the write can 9999c7babf9SDave Chinner * race with page faults, there can be dirty pages over the delalloc extent 10009c7babf9SDave Chinner * outside the range of a short write but still within the delalloc extent 10019c7babf9SDave Chinner * allocated for this iomap. 10029c7babf9SDave Chinner * 10039c7babf9SDave Chinner * This function uses [start_byte, end_byte) intervals (i.e. open ended) to 1004*f43dc4dcSDave Chinner * simplify range iterations. 1005*f43dc4dcSDave Chinner * 1006*f43dc4dcSDave Chinner * The punch() callback *must* only punch delalloc extents in the range passed 1007*f43dc4dcSDave Chinner * to it. It must skip over all other types of extents in the range and leave 1008*f43dc4dcSDave Chinner * them completely unchanged. It must do this punch atomically with respect to 1009*f43dc4dcSDave Chinner * other extent modifications. 1010*f43dc4dcSDave Chinner * 1011*f43dc4dcSDave Chinner * The punch() callback may be called with a folio locked to prevent writeback 1012*f43dc4dcSDave Chinner * extent allocation racing at the edge of the range we are currently punching. 1013*f43dc4dcSDave Chinner * The locked folio may or may not cover the range being punched, so it is not 1014*f43dc4dcSDave Chinner * safe for the punch() callback to lock folios itself. 1015*f43dc4dcSDave Chinner * 1016*f43dc4dcSDave Chinner * Lock order is: 1017*f43dc4dcSDave Chinner * 1018*f43dc4dcSDave Chinner * inode->i_rwsem (shared or exclusive) 1019*f43dc4dcSDave Chinner * inode->i_mapping->invalidate_lock (exclusive) 1020*f43dc4dcSDave Chinner * folio_lock() 1021*f43dc4dcSDave Chinner * ->punch 1022*f43dc4dcSDave Chinner * internal filesystem allocation lock 10239c7babf9SDave Chinner */ 10249c7babf9SDave Chinner int iomap_file_buffered_write_punch_delalloc(struct inode *inode, 10259c7babf9SDave Chinner struct iomap *iomap, loff_t pos, loff_t length, 10269c7babf9SDave Chinner ssize_t written, 10279c7babf9SDave Chinner int (*punch)(struct inode *inode, loff_t pos, loff_t length)) 10289c7babf9SDave Chinner { 10299c7babf9SDave Chinner loff_t start_byte; 10309c7babf9SDave Chinner loff_t end_byte; 10319c7babf9SDave Chinner int blocksize = i_blocksize(inode); 10329c7babf9SDave Chinner 10339c7babf9SDave Chinner if (iomap->type != IOMAP_DELALLOC) 10349c7babf9SDave Chinner return 0; 10359c7babf9SDave Chinner 10369c7babf9SDave Chinner /* If we didn't reserve the blocks, we're not allowed to punch them. */ 10379c7babf9SDave Chinner if (!(iomap->flags & IOMAP_F_NEW)) 10389c7babf9SDave Chinner return 0; 10399c7babf9SDave Chinner 10409c7babf9SDave Chinner /* 10419c7babf9SDave Chinner * start_byte refers to the first unused block after a short write. If 10429c7babf9SDave Chinner * nothing was written, round offset down to point at the first block in 10439c7babf9SDave Chinner * the range. 10449c7babf9SDave Chinner */ 10459c7babf9SDave Chinner if (unlikely(!written)) 10469c7babf9SDave Chinner start_byte = round_down(pos, blocksize); 10479c7babf9SDave Chinner else 10489c7babf9SDave Chinner start_byte = round_up(pos + written, blocksize); 10499c7babf9SDave Chinner end_byte = round_up(pos + length, blocksize); 10509c7babf9SDave Chinner 10519c7babf9SDave Chinner /* Nothing to do if we've written the entire delalloc extent */ 10529c7babf9SDave Chinner if (start_byte >= end_byte) 10539c7babf9SDave Chinner return 0; 10549c7babf9SDave Chinner 1055*f43dc4dcSDave Chinner return iomap_write_delalloc_release(inode, start_byte, end_byte, 1056*f43dc4dcSDave Chinner punch); 10579c7babf9SDave Chinner } 10589c7babf9SDave Chinner EXPORT_SYMBOL_GPL(iomap_file_buffered_write_punch_delalloc); 10599c7babf9SDave Chinner 10608fc274d1SChristoph Hellwig static loff_t iomap_unshare_iter(struct iomap_iter *iter) 1061afc51aaaSDarrick J. Wong { 10628fc274d1SChristoph Hellwig struct iomap *iomap = &iter->iomap; 1063fad0a1abSChristoph Hellwig const struct iomap *srcmap = iomap_iter_srcmap(iter); 10648fc274d1SChristoph Hellwig loff_t pos = iter->pos; 10658fc274d1SChristoph Hellwig loff_t length = iomap_length(iter); 1066afc51aaaSDarrick J. Wong long status = 0; 1067d4ff3b2eSMatthew Wilcox (Oracle) loff_t written = 0; 1068afc51aaaSDarrick J. Wong 10693590c4d8SChristoph Hellwig /* don't bother with blocks that are not shared to start with */ 10703590c4d8SChristoph Hellwig if (!(iomap->flags & IOMAP_F_SHARED)) 10713590c4d8SChristoph Hellwig return length; 10723590c4d8SChristoph Hellwig /* don't bother with holes or unwritten extents */ 1073c039b997SGoldwyn Rodrigues if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN) 10743590c4d8SChristoph Hellwig return length; 10753590c4d8SChristoph Hellwig 1076afc51aaaSDarrick J. Wong do { 107732a38a49SChristoph Hellwig unsigned long offset = offset_in_page(pos); 107832a38a49SChristoph Hellwig unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length); 1079bc6123a8SMatthew Wilcox (Oracle) struct folio *folio; 1080afc51aaaSDarrick J. Wong 1081bc6123a8SMatthew Wilcox (Oracle) status = iomap_write_begin(iter, pos, bytes, &folio); 1082afc51aaaSDarrick J. Wong if (unlikely(status)) 1083afc51aaaSDarrick J. Wong return status; 1084afc51aaaSDarrick J. Wong 1085bc6123a8SMatthew Wilcox (Oracle) status = iomap_write_end(iter, pos, bytes, bytes, folio); 1086afc51aaaSDarrick J. Wong if (WARN_ON_ONCE(status == 0)) 1087afc51aaaSDarrick J. Wong return -EIO; 1088afc51aaaSDarrick J. Wong 1089afc51aaaSDarrick J. Wong cond_resched(); 1090afc51aaaSDarrick J. Wong 1091afc51aaaSDarrick J. Wong pos += status; 1092afc51aaaSDarrick J. Wong written += status; 1093afc51aaaSDarrick J. Wong length -= status; 1094afc51aaaSDarrick J. Wong 10958fc274d1SChristoph Hellwig balance_dirty_pages_ratelimited(iter->inode->i_mapping); 1096afc51aaaSDarrick J. Wong } while (length); 1097afc51aaaSDarrick J. Wong 1098afc51aaaSDarrick J. Wong return written; 1099afc51aaaSDarrick J. Wong } 1100afc51aaaSDarrick J. Wong 1101afc51aaaSDarrick J. Wong int 11023590c4d8SChristoph Hellwig iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len, 1103afc51aaaSDarrick J. Wong const struct iomap_ops *ops) 1104afc51aaaSDarrick J. Wong { 11058fc274d1SChristoph Hellwig struct iomap_iter iter = { 11068fc274d1SChristoph Hellwig .inode = inode, 11078fc274d1SChristoph Hellwig .pos = pos, 11088fc274d1SChristoph Hellwig .len = len, 1109b74b1293SChristoph Hellwig .flags = IOMAP_WRITE | IOMAP_UNSHARE, 11108fc274d1SChristoph Hellwig }; 11118fc274d1SChristoph Hellwig int ret; 1112afc51aaaSDarrick J. Wong 11138fc274d1SChristoph Hellwig while ((ret = iomap_iter(&iter, ops)) > 0) 11148fc274d1SChristoph Hellwig iter.processed = iomap_unshare_iter(&iter); 1115afc51aaaSDarrick J. Wong return ret; 1116afc51aaaSDarrick J. Wong } 11173590c4d8SChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_file_unshare); 1118afc51aaaSDarrick J. Wong 11192aa3048eSChristoph Hellwig static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero) 1120afc51aaaSDarrick J. Wong { 1121fad0a1abSChristoph Hellwig const struct iomap *srcmap = iomap_iter_srcmap(iter); 11222aa3048eSChristoph Hellwig loff_t pos = iter->pos; 11232aa3048eSChristoph Hellwig loff_t length = iomap_length(iter); 1124afc51aaaSDarrick J. Wong loff_t written = 0; 1125afc51aaaSDarrick J. Wong 1126afc51aaaSDarrick J. Wong /* already zeroed? we're done. */ 1127c039b997SGoldwyn Rodrigues if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN) 112881ee8e52SMatthew Wilcox (Oracle) return length; 1129afc51aaaSDarrick J. Wong 1130afc51aaaSDarrick J. Wong do { 11314d7bd0ebSMatthew Wilcox (Oracle) struct folio *folio; 11324d7bd0ebSMatthew Wilcox (Oracle) int status; 11334d7bd0ebSMatthew Wilcox (Oracle) size_t offset; 11344d7bd0ebSMatthew Wilcox (Oracle) size_t bytes = min_t(u64, SIZE_MAX, length); 1135afc51aaaSDarrick J. Wong 11364d7bd0ebSMatthew Wilcox (Oracle) status = iomap_write_begin(iter, pos, bytes, &folio); 11374d7bd0ebSMatthew Wilcox (Oracle) if (status) 11384d7bd0ebSMatthew Wilcox (Oracle) return status; 11394d7bd0ebSMatthew Wilcox (Oracle) 11404d7bd0ebSMatthew Wilcox (Oracle) offset = offset_in_folio(folio, pos); 11414d7bd0ebSMatthew Wilcox (Oracle) if (bytes > folio_size(folio) - offset) 11424d7bd0ebSMatthew Wilcox (Oracle) bytes = folio_size(folio) - offset; 11434d7bd0ebSMatthew Wilcox (Oracle) 11444d7bd0ebSMatthew Wilcox (Oracle) folio_zero_range(folio, offset, bytes); 11454d7bd0ebSMatthew Wilcox (Oracle) folio_mark_accessed(folio); 11464d7bd0ebSMatthew Wilcox (Oracle) 11474d7bd0ebSMatthew Wilcox (Oracle) bytes = iomap_write_end(iter, pos, bytes, bytes, folio); 11484d7bd0ebSMatthew Wilcox (Oracle) if (WARN_ON_ONCE(bytes == 0)) 11494d7bd0ebSMatthew Wilcox (Oracle) return -EIO; 1150afc51aaaSDarrick J. Wong 1151afc51aaaSDarrick J. Wong pos += bytes; 115281ee8e52SMatthew Wilcox (Oracle) length -= bytes; 1153afc51aaaSDarrick J. Wong written += bytes; 115481ee8e52SMatthew Wilcox (Oracle) } while (length > 0); 1155afc51aaaSDarrick J. Wong 115698eb8d95SKaixu Xia if (did_zero) 115798eb8d95SKaixu Xia *did_zero = true; 1158afc51aaaSDarrick J. Wong return written; 1159afc51aaaSDarrick J. Wong } 1160afc51aaaSDarrick J. Wong 1161afc51aaaSDarrick J. Wong int 1162afc51aaaSDarrick J. Wong iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, 1163afc51aaaSDarrick J. Wong const struct iomap_ops *ops) 1164afc51aaaSDarrick J. Wong { 11652aa3048eSChristoph Hellwig struct iomap_iter iter = { 11662aa3048eSChristoph Hellwig .inode = inode, 11672aa3048eSChristoph Hellwig .pos = pos, 11682aa3048eSChristoph Hellwig .len = len, 11692aa3048eSChristoph Hellwig .flags = IOMAP_ZERO, 11702aa3048eSChristoph Hellwig }; 11712aa3048eSChristoph Hellwig int ret; 1172afc51aaaSDarrick J. Wong 11732aa3048eSChristoph Hellwig while ((ret = iomap_iter(&iter, ops)) > 0) 11742aa3048eSChristoph Hellwig iter.processed = iomap_zero_iter(&iter, did_zero); 1175afc51aaaSDarrick J. Wong return ret; 1176afc51aaaSDarrick J. Wong } 1177afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_zero_range); 1178afc51aaaSDarrick J. Wong 1179afc51aaaSDarrick J. Wong int 1180afc51aaaSDarrick J. Wong iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, 1181afc51aaaSDarrick J. Wong const struct iomap_ops *ops) 1182afc51aaaSDarrick J. Wong { 1183afc51aaaSDarrick J. Wong unsigned int blocksize = i_blocksize(inode); 1184afc51aaaSDarrick J. Wong unsigned int off = pos & (blocksize - 1); 1185afc51aaaSDarrick J. Wong 1186afc51aaaSDarrick J. Wong /* Block boundary? Nothing to do */ 1187afc51aaaSDarrick J. Wong if (!off) 1188afc51aaaSDarrick J. Wong return 0; 1189afc51aaaSDarrick J. Wong return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops); 1190afc51aaaSDarrick J. Wong } 1191afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_truncate_page); 1192afc51aaaSDarrick J. Wong 1193ea0f843aSMatthew Wilcox (Oracle) static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter, 1194ea0f843aSMatthew Wilcox (Oracle) struct folio *folio) 1195afc51aaaSDarrick J. Wong { 1196253564baSChristoph Hellwig loff_t length = iomap_length(iter); 1197afc51aaaSDarrick J. Wong int ret; 1198afc51aaaSDarrick J. Wong 1199253564baSChristoph Hellwig if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) { 1200d1bd0b4eSMatthew Wilcox (Oracle) ret = __block_write_begin_int(folio, iter->pos, length, NULL, 1201253564baSChristoph Hellwig &iter->iomap); 1202afc51aaaSDarrick J. Wong if (ret) 1203afc51aaaSDarrick J. Wong return ret; 1204ea0f843aSMatthew Wilcox (Oracle) block_commit_write(&folio->page, 0, length); 1205afc51aaaSDarrick J. Wong } else { 1206ea0f843aSMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_uptodate(folio)); 1207ea0f843aSMatthew Wilcox (Oracle) folio_mark_dirty(folio); 1208afc51aaaSDarrick J. Wong } 1209afc51aaaSDarrick J. Wong 1210afc51aaaSDarrick J. Wong return length; 1211afc51aaaSDarrick J. Wong } 1212afc51aaaSDarrick J. Wong 1213afc51aaaSDarrick J. Wong vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops) 1214afc51aaaSDarrick J. Wong { 1215253564baSChristoph Hellwig struct iomap_iter iter = { 1216253564baSChristoph Hellwig .inode = file_inode(vmf->vma->vm_file), 1217253564baSChristoph Hellwig .flags = IOMAP_WRITE | IOMAP_FAULT, 1218253564baSChristoph Hellwig }; 1219ea0f843aSMatthew Wilcox (Oracle) struct folio *folio = page_folio(vmf->page); 1220afc51aaaSDarrick J. Wong ssize_t ret; 1221afc51aaaSDarrick J. Wong 1222ea0f843aSMatthew Wilcox (Oracle) folio_lock(folio); 1223ea0f843aSMatthew Wilcox (Oracle) ret = folio_mkwrite_check_truncate(folio, iter.inode); 1224243145bcSAndreas Gruenbacher if (ret < 0) 1225afc51aaaSDarrick J. Wong goto out_unlock; 1226ea0f843aSMatthew Wilcox (Oracle) iter.pos = folio_pos(folio); 1227253564baSChristoph Hellwig iter.len = ret; 1228253564baSChristoph Hellwig while ((ret = iomap_iter(&iter, ops)) > 0) 1229ea0f843aSMatthew Wilcox (Oracle) iter.processed = iomap_folio_mkwrite_iter(&iter, folio); 1230afc51aaaSDarrick J. Wong 1231253564baSChristoph Hellwig if (ret < 0) 1232afc51aaaSDarrick J. Wong goto out_unlock; 1233ea0f843aSMatthew Wilcox (Oracle) folio_wait_stable(folio); 1234afc51aaaSDarrick J. Wong return VM_FAULT_LOCKED; 1235afc51aaaSDarrick J. Wong out_unlock: 1236ea0f843aSMatthew Wilcox (Oracle) folio_unlock(folio); 1237afc51aaaSDarrick J. Wong return block_page_mkwrite_return(ret); 1238afc51aaaSDarrick J. Wong } 1239afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_page_mkwrite); 1240598ecfbaSChristoph Hellwig 12418ffd74e9SMatthew Wilcox (Oracle) static void iomap_finish_folio_write(struct inode *inode, struct folio *folio, 12428ffd74e9SMatthew Wilcox (Oracle) size_t len, int error) 1243598ecfbaSChristoph Hellwig { 124495c4cd05SMatthew Wilcox (Oracle) struct iomap_page *iop = to_iomap_page(folio); 1245598ecfbaSChristoph Hellwig 1246598ecfbaSChristoph Hellwig if (error) { 12478ffd74e9SMatthew Wilcox (Oracle) folio_set_error(folio); 1248b69eea82SDarrick J. Wong mapping_set_error(inode->i_mapping, error); 1249598ecfbaSChristoph Hellwig } 1250598ecfbaSChristoph Hellwig 12518ffd74e9SMatthew Wilcox (Oracle) WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !iop); 12520fb2d720SMatthew Wilcox (Oracle) WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0); 1253598ecfbaSChristoph Hellwig 12540fb2d720SMatthew Wilcox (Oracle) if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending)) 12558ffd74e9SMatthew Wilcox (Oracle) folio_end_writeback(folio); 1256598ecfbaSChristoph Hellwig } 1257598ecfbaSChristoph Hellwig 1258598ecfbaSChristoph Hellwig /* 1259598ecfbaSChristoph Hellwig * We're now finished for good with this ioend structure. Update the page 1260598ecfbaSChristoph Hellwig * state, release holds on bios, and finally free up memory. Do not use the 1261598ecfbaSChristoph Hellwig * ioend after this. 1262598ecfbaSChristoph Hellwig */ 1263ebb7fb15SDave Chinner static u32 1264598ecfbaSChristoph Hellwig iomap_finish_ioend(struct iomap_ioend *ioend, int error) 1265598ecfbaSChristoph Hellwig { 1266598ecfbaSChristoph Hellwig struct inode *inode = ioend->io_inode; 1267598ecfbaSChristoph Hellwig struct bio *bio = &ioend->io_inline_bio; 1268598ecfbaSChristoph Hellwig struct bio *last = ioend->io_bio, *next; 1269598ecfbaSChristoph Hellwig u64 start = bio->bi_iter.bi_sector; 1270c275779fSZorro Lang loff_t offset = ioend->io_offset; 1271598ecfbaSChristoph Hellwig bool quiet = bio_flagged(bio, BIO_QUIET); 1272ebb7fb15SDave Chinner u32 folio_count = 0; 1273598ecfbaSChristoph Hellwig 1274598ecfbaSChristoph Hellwig for (bio = &ioend->io_inline_bio; bio; bio = next) { 12758ffd74e9SMatthew Wilcox (Oracle) struct folio_iter fi; 1276598ecfbaSChristoph Hellwig 1277598ecfbaSChristoph Hellwig /* 1278598ecfbaSChristoph Hellwig * For the last bio, bi_private points to the ioend, so we 1279598ecfbaSChristoph Hellwig * need to explicitly end the iteration here. 1280598ecfbaSChristoph Hellwig */ 1281598ecfbaSChristoph Hellwig if (bio == last) 1282598ecfbaSChristoph Hellwig next = NULL; 1283598ecfbaSChristoph Hellwig else 1284598ecfbaSChristoph Hellwig next = bio->bi_private; 1285598ecfbaSChristoph Hellwig 12868ffd74e9SMatthew Wilcox (Oracle) /* walk all folios in bio, ending page IO on them */ 1287ebb7fb15SDave Chinner bio_for_each_folio_all(fi, bio) { 12888ffd74e9SMatthew Wilcox (Oracle) iomap_finish_folio_write(inode, fi.folio, fi.length, 12898ffd74e9SMatthew Wilcox (Oracle) error); 1290ebb7fb15SDave Chinner folio_count++; 1291ebb7fb15SDave Chinner } 1292598ecfbaSChristoph Hellwig bio_put(bio); 1293598ecfbaSChristoph Hellwig } 1294c275779fSZorro Lang /* The ioend has been freed by bio_put() */ 1295598ecfbaSChristoph Hellwig 1296598ecfbaSChristoph Hellwig if (unlikely(error && !quiet)) { 1297598ecfbaSChristoph Hellwig printk_ratelimited(KERN_ERR 12989cd0ed63SDarrick J. Wong "%s: writeback error on inode %lu, offset %lld, sector %llu", 1299c275779fSZorro Lang inode->i_sb->s_id, inode->i_ino, offset, start); 1300598ecfbaSChristoph Hellwig } 1301ebb7fb15SDave Chinner return folio_count; 1302598ecfbaSChristoph Hellwig } 1303598ecfbaSChristoph Hellwig 1304ebb7fb15SDave Chinner /* 1305ebb7fb15SDave Chinner * Ioend completion routine for merged bios. This can only be called from task 1306ebb7fb15SDave Chinner * contexts as merged ioends can be of unbound length. Hence we have to break up 1307ebb7fb15SDave Chinner * the writeback completions into manageable chunks to avoid long scheduler 1308ebb7fb15SDave Chinner * holdoffs. We aim to keep scheduler holdoffs down below 10ms so that we get 1309ebb7fb15SDave Chinner * good batch processing throughput without creating adverse scheduler latency 1310ebb7fb15SDave Chinner * conditions. 1311ebb7fb15SDave Chinner */ 1312598ecfbaSChristoph Hellwig void 1313598ecfbaSChristoph Hellwig iomap_finish_ioends(struct iomap_ioend *ioend, int error) 1314598ecfbaSChristoph Hellwig { 1315598ecfbaSChristoph Hellwig struct list_head tmp; 1316ebb7fb15SDave Chinner u32 completions; 1317ebb7fb15SDave Chinner 1318ebb7fb15SDave Chinner might_sleep(); 1319598ecfbaSChristoph Hellwig 1320598ecfbaSChristoph Hellwig list_replace_init(&ioend->io_list, &tmp); 1321ebb7fb15SDave Chinner completions = iomap_finish_ioend(ioend, error); 1322598ecfbaSChristoph Hellwig 1323598ecfbaSChristoph Hellwig while (!list_empty(&tmp)) { 1324ebb7fb15SDave Chinner if (completions > IOEND_BATCH_SIZE * 8) { 1325ebb7fb15SDave Chinner cond_resched(); 1326ebb7fb15SDave Chinner completions = 0; 1327ebb7fb15SDave Chinner } 1328598ecfbaSChristoph Hellwig ioend = list_first_entry(&tmp, struct iomap_ioend, io_list); 1329598ecfbaSChristoph Hellwig list_del_init(&ioend->io_list); 1330ebb7fb15SDave Chinner completions += iomap_finish_ioend(ioend, error); 1331598ecfbaSChristoph Hellwig } 1332598ecfbaSChristoph Hellwig } 1333598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_finish_ioends); 1334598ecfbaSChristoph Hellwig 1335598ecfbaSChristoph Hellwig /* 1336598ecfbaSChristoph Hellwig * We can merge two adjacent ioends if they have the same set of work to do. 1337598ecfbaSChristoph Hellwig */ 1338598ecfbaSChristoph Hellwig static bool 1339598ecfbaSChristoph Hellwig iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next) 1340598ecfbaSChristoph Hellwig { 1341598ecfbaSChristoph Hellwig if (ioend->io_bio->bi_status != next->io_bio->bi_status) 1342598ecfbaSChristoph Hellwig return false; 1343598ecfbaSChristoph Hellwig if ((ioend->io_flags & IOMAP_F_SHARED) ^ 1344598ecfbaSChristoph Hellwig (next->io_flags & IOMAP_F_SHARED)) 1345598ecfbaSChristoph Hellwig return false; 1346598ecfbaSChristoph Hellwig if ((ioend->io_type == IOMAP_UNWRITTEN) ^ 1347598ecfbaSChristoph Hellwig (next->io_type == IOMAP_UNWRITTEN)) 1348598ecfbaSChristoph Hellwig return false; 1349598ecfbaSChristoph Hellwig if (ioend->io_offset + ioend->io_size != next->io_offset) 1350598ecfbaSChristoph Hellwig return false; 1351ebb7fb15SDave Chinner /* 1352ebb7fb15SDave Chinner * Do not merge physically discontiguous ioends. The filesystem 1353ebb7fb15SDave Chinner * completion functions will have to iterate the physical 1354ebb7fb15SDave Chinner * discontiguities even if we merge the ioends at a logical level, so 1355ebb7fb15SDave Chinner * we don't gain anything by merging physical discontiguities here. 1356ebb7fb15SDave Chinner * 1357ebb7fb15SDave Chinner * We cannot use bio->bi_iter.bi_sector here as it is modified during 1358ebb7fb15SDave Chinner * submission so does not point to the start sector of the bio at 1359ebb7fb15SDave Chinner * completion. 1360ebb7fb15SDave Chinner */ 1361ebb7fb15SDave Chinner if (ioend->io_sector + (ioend->io_size >> 9) != next->io_sector) 1362ebb7fb15SDave Chinner return false; 1363598ecfbaSChristoph Hellwig return true; 1364598ecfbaSChristoph Hellwig } 1365598ecfbaSChristoph Hellwig 1366598ecfbaSChristoph Hellwig void 13676e552494SBrian Foster iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends) 1368598ecfbaSChristoph Hellwig { 1369598ecfbaSChristoph Hellwig struct iomap_ioend *next; 1370598ecfbaSChristoph Hellwig 1371598ecfbaSChristoph Hellwig INIT_LIST_HEAD(&ioend->io_list); 1372598ecfbaSChristoph Hellwig 1373598ecfbaSChristoph Hellwig while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend, 1374598ecfbaSChristoph Hellwig io_list))) { 1375598ecfbaSChristoph Hellwig if (!iomap_ioend_can_merge(ioend, next)) 1376598ecfbaSChristoph Hellwig break; 1377598ecfbaSChristoph Hellwig list_move_tail(&next->io_list, &ioend->io_list); 1378598ecfbaSChristoph Hellwig ioend->io_size += next->io_size; 1379598ecfbaSChristoph Hellwig } 1380598ecfbaSChristoph Hellwig } 1381598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_ioend_try_merge); 1382598ecfbaSChristoph Hellwig 1383598ecfbaSChristoph Hellwig static int 13844f0f586bSSami Tolvanen iomap_ioend_compare(void *priv, const struct list_head *a, 13854f0f586bSSami Tolvanen const struct list_head *b) 1386598ecfbaSChristoph Hellwig { 1387b3d423ecSChristoph Hellwig struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list); 1388b3d423ecSChristoph Hellwig struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list); 1389598ecfbaSChristoph Hellwig 1390598ecfbaSChristoph Hellwig if (ia->io_offset < ib->io_offset) 1391598ecfbaSChristoph Hellwig return -1; 1392b3d423ecSChristoph Hellwig if (ia->io_offset > ib->io_offset) 1393598ecfbaSChristoph Hellwig return 1; 1394598ecfbaSChristoph Hellwig return 0; 1395598ecfbaSChristoph Hellwig } 1396598ecfbaSChristoph Hellwig 1397598ecfbaSChristoph Hellwig void 1398598ecfbaSChristoph Hellwig iomap_sort_ioends(struct list_head *ioend_list) 1399598ecfbaSChristoph Hellwig { 1400598ecfbaSChristoph Hellwig list_sort(NULL, ioend_list, iomap_ioend_compare); 1401598ecfbaSChristoph Hellwig } 1402598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_sort_ioends); 1403598ecfbaSChristoph Hellwig 1404598ecfbaSChristoph Hellwig static void iomap_writepage_end_bio(struct bio *bio) 1405598ecfbaSChristoph Hellwig { 1406598ecfbaSChristoph Hellwig struct iomap_ioend *ioend = bio->bi_private; 1407598ecfbaSChristoph Hellwig 1408598ecfbaSChristoph Hellwig iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status)); 1409598ecfbaSChristoph Hellwig } 1410598ecfbaSChristoph Hellwig 1411598ecfbaSChristoph Hellwig /* 1412598ecfbaSChristoph Hellwig * Submit the final bio for an ioend. 1413598ecfbaSChristoph Hellwig * 1414598ecfbaSChristoph Hellwig * If @error is non-zero, it means that we have a situation where some part of 1415f1f264b4SAndreas Gruenbacher * the submission process has failed after we've marked pages for writeback 1416598ecfbaSChristoph Hellwig * and unlocked them. In this situation, we need to fail the bio instead of 1417598ecfbaSChristoph Hellwig * submitting it. This typically only happens on a filesystem shutdown. 1418598ecfbaSChristoph Hellwig */ 1419598ecfbaSChristoph Hellwig static int 1420598ecfbaSChristoph Hellwig iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend, 1421598ecfbaSChristoph Hellwig int error) 1422598ecfbaSChristoph Hellwig { 1423598ecfbaSChristoph Hellwig ioend->io_bio->bi_private = ioend; 1424598ecfbaSChristoph Hellwig ioend->io_bio->bi_end_io = iomap_writepage_end_bio; 1425598ecfbaSChristoph Hellwig 1426598ecfbaSChristoph Hellwig if (wpc->ops->prepare_ioend) 1427598ecfbaSChristoph Hellwig error = wpc->ops->prepare_ioend(ioend, error); 1428598ecfbaSChristoph Hellwig if (error) { 1429598ecfbaSChristoph Hellwig /* 1430f1f264b4SAndreas Gruenbacher * If we're failing the IO now, just mark the ioend with an 1431598ecfbaSChristoph Hellwig * error and finish it. This will run IO completion immediately 1432598ecfbaSChristoph Hellwig * as there is only one reference to the ioend at this point in 1433598ecfbaSChristoph Hellwig * time. 1434598ecfbaSChristoph Hellwig */ 1435598ecfbaSChristoph Hellwig ioend->io_bio->bi_status = errno_to_blk_status(error); 1436598ecfbaSChristoph Hellwig bio_endio(ioend->io_bio); 1437598ecfbaSChristoph Hellwig return error; 1438598ecfbaSChristoph Hellwig } 1439598ecfbaSChristoph Hellwig 1440598ecfbaSChristoph Hellwig submit_bio(ioend->io_bio); 1441598ecfbaSChristoph Hellwig return 0; 1442598ecfbaSChristoph Hellwig } 1443598ecfbaSChristoph Hellwig 1444598ecfbaSChristoph Hellwig static struct iomap_ioend * 1445598ecfbaSChristoph Hellwig iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc, 1446598ecfbaSChristoph Hellwig loff_t offset, sector_t sector, struct writeback_control *wbc) 1447598ecfbaSChristoph Hellwig { 1448598ecfbaSChristoph Hellwig struct iomap_ioend *ioend; 1449598ecfbaSChristoph Hellwig struct bio *bio; 1450598ecfbaSChristoph Hellwig 1451609be106SChristoph Hellwig bio = bio_alloc_bioset(wpc->iomap.bdev, BIO_MAX_VECS, 1452609be106SChristoph Hellwig REQ_OP_WRITE | wbc_to_write_flags(wbc), 1453609be106SChristoph Hellwig GFP_NOFS, &iomap_ioend_bioset); 1454598ecfbaSChristoph Hellwig bio->bi_iter.bi_sector = sector; 1455598ecfbaSChristoph Hellwig wbc_init_bio(wbc, bio); 1456598ecfbaSChristoph Hellwig 1457598ecfbaSChristoph Hellwig ioend = container_of(bio, struct iomap_ioend, io_inline_bio); 1458598ecfbaSChristoph Hellwig INIT_LIST_HEAD(&ioend->io_list); 1459598ecfbaSChristoph Hellwig ioend->io_type = wpc->iomap.type; 1460598ecfbaSChristoph Hellwig ioend->io_flags = wpc->iomap.flags; 1461598ecfbaSChristoph Hellwig ioend->io_inode = inode; 1462598ecfbaSChristoph Hellwig ioend->io_size = 0; 1463ebb7fb15SDave Chinner ioend->io_folios = 0; 1464598ecfbaSChristoph Hellwig ioend->io_offset = offset; 1465598ecfbaSChristoph Hellwig ioend->io_bio = bio; 1466ebb7fb15SDave Chinner ioend->io_sector = sector; 1467598ecfbaSChristoph Hellwig return ioend; 1468598ecfbaSChristoph Hellwig } 1469598ecfbaSChristoph Hellwig 1470598ecfbaSChristoph Hellwig /* 1471598ecfbaSChristoph Hellwig * Allocate a new bio, and chain the old bio to the new one. 1472598ecfbaSChristoph Hellwig * 1473f1f264b4SAndreas Gruenbacher * Note that we have to perform the chaining in this unintuitive order 1474598ecfbaSChristoph Hellwig * so that the bi_private linkage is set up in the right direction for the 1475598ecfbaSChristoph Hellwig * traversal in iomap_finish_ioend(). 1476598ecfbaSChristoph Hellwig */ 1477598ecfbaSChristoph Hellwig static struct bio * 1478598ecfbaSChristoph Hellwig iomap_chain_bio(struct bio *prev) 1479598ecfbaSChristoph Hellwig { 1480598ecfbaSChristoph Hellwig struct bio *new; 1481598ecfbaSChristoph Hellwig 148207888c66SChristoph Hellwig new = bio_alloc(prev->bi_bdev, BIO_MAX_VECS, prev->bi_opf, GFP_NOFS); 148307888c66SChristoph Hellwig bio_clone_blkg_association(new, prev); 1484598ecfbaSChristoph Hellwig new->bi_iter.bi_sector = bio_end_sector(prev); 1485598ecfbaSChristoph Hellwig 1486598ecfbaSChristoph Hellwig bio_chain(prev, new); 1487598ecfbaSChristoph Hellwig bio_get(prev); /* for iomap_finish_ioend */ 1488598ecfbaSChristoph Hellwig submit_bio(prev); 1489598ecfbaSChristoph Hellwig return new; 1490598ecfbaSChristoph Hellwig } 1491598ecfbaSChristoph Hellwig 1492598ecfbaSChristoph Hellwig static bool 1493598ecfbaSChristoph Hellwig iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset, 1494598ecfbaSChristoph Hellwig sector_t sector) 1495598ecfbaSChristoph Hellwig { 1496598ecfbaSChristoph Hellwig if ((wpc->iomap.flags & IOMAP_F_SHARED) != 1497598ecfbaSChristoph Hellwig (wpc->ioend->io_flags & IOMAP_F_SHARED)) 1498598ecfbaSChristoph Hellwig return false; 1499598ecfbaSChristoph Hellwig if (wpc->iomap.type != wpc->ioend->io_type) 1500598ecfbaSChristoph Hellwig return false; 1501598ecfbaSChristoph Hellwig if (offset != wpc->ioend->io_offset + wpc->ioend->io_size) 1502598ecfbaSChristoph Hellwig return false; 1503598ecfbaSChristoph Hellwig if (sector != bio_end_sector(wpc->ioend->io_bio)) 1504598ecfbaSChristoph Hellwig return false; 1505ebb7fb15SDave Chinner /* 1506ebb7fb15SDave Chinner * Limit ioend bio chain lengths to minimise IO completion latency. This 1507ebb7fb15SDave Chinner * also prevents long tight loops ending page writeback on all the 1508ebb7fb15SDave Chinner * folios in the ioend. 1509ebb7fb15SDave Chinner */ 1510ebb7fb15SDave Chinner if (wpc->ioend->io_folios >= IOEND_BATCH_SIZE) 1511ebb7fb15SDave Chinner return false; 1512598ecfbaSChristoph Hellwig return true; 1513598ecfbaSChristoph Hellwig } 1514598ecfbaSChristoph Hellwig 1515598ecfbaSChristoph Hellwig /* 1516598ecfbaSChristoph Hellwig * Test to see if we have an existing ioend structure that we could append to 1517f1f264b4SAndreas Gruenbacher * first; otherwise finish off the current ioend and start another. 1518598ecfbaSChristoph Hellwig */ 1519598ecfbaSChristoph Hellwig static void 1520e735c007SMatthew Wilcox (Oracle) iomap_add_to_ioend(struct inode *inode, loff_t pos, struct folio *folio, 1521598ecfbaSChristoph Hellwig struct iomap_page *iop, struct iomap_writepage_ctx *wpc, 1522598ecfbaSChristoph Hellwig struct writeback_control *wbc, struct list_head *iolist) 1523598ecfbaSChristoph Hellwig { 1524e735c007SMatthew Wilcox (Oracle) sector_t sector = iomap_sector(&wpc->iomap, pos); 1525598ecfbaSChristoph Hellwig unsigned len = i_blocksize(inode); 1526e735c007SMatthew Wilcox (Oracle) size_t poff = offset_in_folio(folio, pos); 1527598ecfbaSChristoph Hellwig 1528e735c007SMatthew Wilcox (Oracle) if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, pos, sector)) { 1529598ecfbaSChristoph Hellwig if (wpc->ioend) 1530598ecfbaSChristoph Hellwig list_add(&wpc->ioend->io_list, iolist); 1531e735c007SMatthew Wilcox (Oracle) wpc->ioend = iomap_alloc_ioend(inode, wpc, pos, sector, wbc); 1532598ecfbaSChristoph Hellwig } 1533598ecfbaSChristoph Hellwig 1534e735c007SMatthew Wilcox (Oracle) if (!bio_add_folio(wpc->ioend->io_bio, folio, len, poff)) { 1535c1b79f11SChristoph Hellwig wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio); 1536e735c007SMatthew Wilcox (Oracle) bio_add_folio(wpc->ioend->io_bio, folio, len, poff); 1537c1b79f11SChristoph Hellwig } 1538c1b79f11SChristoph Hellwig 15390fb2d720SMatthew Wilcox (Oracle) if (iop) 15400fb2d720SMatthew Wilcox (Oracle) atomic_add(len, &iop->write_bytes_pending); 1541598ecfbaSChristoph Hellwig wpc->ioend->io_size += len; 1542e735c007SMatthew Wilcox (Oracle) wbc_account_cgroup_owner(wbc, &folio->page, len); 1543598ecfbaSChristoph Hellwig } 1544598ecfbaSChristoph Hellwig 1545598ecfbaSChristoph Hellwig /* 1546598ecfbaSChristoph Hellwig * We implement an immediate ioend submission policy here to avoid needing to 1547598ecfbaSChristoph Hellwig * chain multiple ioends and hence nest mempool allocations which can violate 1548f1f264b4SAndreas Gruenbacher * the forward progress guarantees we need to provide. The current ioend we're 1549f1f264b4SAndreas Gruenbacher * adding blocks to is cached in the writepage context, and if the new block 1550f1f264b4SAndreas Gruenbacher * doesn't append to the cached ioend, it will create a new ioend and cache that 1551598ecfbaSChristoph Hellwig * instead. 1552598ecfbaSChristoph Hellwig * 1553598ecfbaSChristoph Hellwig * If a new ioend is created and cached, the old ioend is returned and queued 1554598ecfbaSChristoph Hellwig * locally for submission once the entire page is processed or an error has been 1555598ecfbaSChristoph Hellwig * detected. While ioends are submitted immediately after they are completed, 1556598ecfbaSChristoph Hellwig * batching optimisations are provided by higher level block plugging. 1557598ecfbaSChristoph Hellwig * 1558598ecfbaSChristoph Hellwig * At the end of a writeback pass, there will be a cached ioend remaining on the 1559598ecfbaSChristoph Hellwig * writepage context that the caller will need to submit. 1560598ecfbaSChristoph Hellwig */ 1561598ecfbaSChristoph Hellwig static int 1562598ecfbaSChristoph Hellwig iomap_writepage_map(struct iomap_writepage_ctx *wpc, 1563598ecfbaSChristoph Hellwig struct writeback_control *wbc, struct inode *inode, 1564e735c007SMatthew Wilcox (Oracle) struct folio *folio, u64 end_pos) 1565598ecfbaSChristoph Hellwig { 15669753b868SStefan Roesch struct iomap_page *iop = iomap_page_create(inode, folio, 0); 1567598ecfbaSChristoph Hellwig struct iomap_ioend *ioend, *next; 1568598ecfbaSChristoph Hellwig unsigned len = i_blocksize(inode); 156992655036SMatthew Wilcox (Oracle) unsigned nblocks = i_blocks_per_folio(inode, folio); 157092655036SMatthew Wilcox (Oracle) u64 pos = folio_pos(folio); 1571598ecfbaSChristoph Hellwig int error = 0, count = 0, i; 1572598ecfbaSChristoph Hellwig LIST_HEAD(submit_list); 1573598ecfbaSChristoph Hellwig 15740fb2d720SMatthew Wilcox (Oracle) WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0); 1575598ecfbaSChristoph Hellwig 1576598ecfbaSChristoph Hellwig /* 157792655036SMatthew Wilcox (Oracle) * Walk through the folio to find areas to write back. If we 157892655036SMatthew Wilcox (Oracle) * run off the end of the current map or find the current map 157992655036SMatthew Wilcox (Oracle) * invalid, grab a new one. 1580598ecfbaSChristoph Hellwig */ 158192655036SMatthew Wilcox (Oracle) for (i = 0; i < nblocks && pos < end_pos; i++, pos += len) { 1582598ecfbaSChristoph Hellwig if (iop && !test_bit(i, iop->uptodate)) 1583598ecfbaSChristoph Hellwig continue; 1584598ecfbaSChristoph Hellwig 158592655036SMatthew Wilcox (Oracle) error = wpc->ops->map_blocks(wpc, inode, pos); 1586598ecfbaSChristoph Hellwig if (error) 1587598ecfbaSChristoph Hellwig break; 1588adc9c2e5SDarrick J. Wong trace_iomap_writepage_map(inode, &wpc->iomap); 15893e19e6f3SChristoph Hellwig if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE)) 15903e19e6f3SChristoph Hellwig continue; 1591598ecfbaSChristoph Hellwig if (wpc->iomap.type == IOMAP_HOLE) 1592598ecfbaSChristoph Hellwig continue; 1593e735c007SMatthew Wilcox (Oracle) iomap_add_to_ioend(inode, pos, folio, iop, wpc, wbc, 1594598ecfbaSChristoph Hellwig &submit_list); 1595598ecfbaSChristoph Hellwig count++; 1596598ecfbaSChristoph Hellwig } 1597ebb7fb15SDave Chinner if (count) 1598ebb7fb15SDave Chinner wpc->ioend->io_folios++; 1599598ecfbaSChristoph Hellwig 1600598ecfbaSChristoph Hellwig WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list)); 1601e735c007SMatthew Wilcox (Oracle) WARN_ON_ONCE(!folio_test_locked(folio)); 1602e735c007SMatthew Wilcox (Oracle) WARN_ON_ONCE(folio_test_writeback(folio)); 1603e735c007SMatthew Wilcox (Oracle) WARN_ON_ONCE(folio_test_dirty(folio)); 1604598ecfbaSChristoph Hellwig 1605598ecfbaSChristoph Hellwig /* 1606598ecfbaSChristoph Hellwig * We cannot cancel the ioend directly here on error. We may have 1607598ecfbaSChristoph Hellwig * already set other pages under writeback and hence we have to run I/O 1608598ecfbaSChristoph Hellwig * completion to mark the error state of the pages under writeback 1609598ecfbaSChristoph Hellwig * appropriately. 1610598ecfbaSChristoph Hellwig */ 1611598ecfbaSChristoph Hellwig if (unlikely(error)) { 1612598ecfbaSChristoph Hellwig /* 1613763e4cdcSBrian Foster * Let the filesystem know what portion of the current page 1614f1f264b4SAndreas Gruenbacher * failed to map. If the page hasn't been added to ioend, it 1615763e4cdcSBrian Foster * won't be affected by I/O completion and we must unlock it 1616763e4cdcSBrian Foster * now. 1617598ecfbaSChristoph Hellwig */ 16186e478521SMatthew Wilcox (Oracle) if (wpc->ops->discard_folio) 161992655036SMatthew Wilcox (Oracle) wpc->ops->discard_folio(folio, pos); 1620763e4cdcSBrian Foster if (!count) { 1621e735c007SMatthew Wilcox (Oracle) folio_unlock(folio); 1622598ecfbaSChristoph Hellwig goto done; 1623598ecfbaSChristoph Hellwig } 1624598ecfbaSChristoph Hellwig } 1625598ecfbaSChristoph Hellwig 1626e735c007SMatthew Wilcox (Oracle) folio_start_writeback(folio); 1627e735c007SMatthew Wilcox (Oracle) folio_unlock(folio); 1628598ecfbaSChristoph Hellwig 1629598ecfbaSChristoph Hellwig /* 1630f1f264b4SAndreas Gruenbacher * Preserve the original error if there was one; catch 1631598ecfbaSChristoph Hellwig * submission errors here and propagate into subsequent ioend 1632598ecfbaSChristoph Hellwig * submissions. 1633598ecfbaSChristoph Hellwig */ 1634598ecfbaSChristoph Hellwig list_for_each_entry_safe(ioend, next, &submit_list, io_list) { 1635598ecfbaSChristoph Hellwig int error2; 1636598ecfbaSChristoph Hellwig 1637598ecfbaSChristoph Hellwig list_del_init(&ioend->io_list); 1638598ecfbaSChristoph Hellwig error2 = iomap_submit_ioend(wpc, ioend, error); 1639598ecfbaSChristoph Hellwig if (error2 && !error) 1640598ecfbaSChristoph Hellwig error = error2; 1641598ecfbaSChristoph Hellwig } 1642598ecfbaSChristoph Hellwig 1643598ecfbaSChristoph Hellwig /* 1644598ecfbaSChristoph Hellwig * We can end up here with no error and nothing to write only if we race 1645598ecfbaSChristoph Hellwig * with a partial page truncate on a sub-page block sized filesystem. 1646598ecfbaSChristoph Hellwig */ 1647598ecfbaSChristoph Hellwig if (!count) 1648e735c007SMatthew Wilcox (Oracle) folio_end_writeback(folio); 1649598ecfbaSChristoph Hellwig done: 16503d5f3ba1SDarrick J. Wong mapping_set_error(inode->i_mapping, error); 1651598ecfbaSChristoph Hellwig return error; 1652598ecfbaSChristoph Hellwig } 1653598ecfbaSChristoph Hellwig 1654598ecfbaSChristoph Hellwig /* 1655598ecfbaSChristoph Hellwig * Write out a dirty page. 1656598ecfbaSChristoph Hellwig * 1657f1f264b4SAndreas Gruenbacher * For delalloc space on the page, we need to allocate space and flush it. 1658f1f264b4SAndreas Gruenbacher * For unwritten space on the page, we need to start the conversion to 1659598ecfbaSChristoph Hellwig * regular allocated space. 1660598ecfbaSChristoph Hellwig */ 1661598ecfbaSChristoph Hellwig static int 1662598ecfbaSChristoph Hellwig iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data) 1663598ecfbaSChristoph Hellwig { 1664e735c007SMatthew Wilcox (Oracle) struct folio *folio = page_folio(page); 1665598ecfbaSChristoph Hellwig struct iomap_writepage_ctx *wpc = data; 1666e735c007SMatthew Wilcox (Oracle) struct inode *inode = folio->mapping->host; 166781d4782aSMatthew Wilcox (Oracle) u64 end_pos, isize; 1668598ecfbaSChristoph Hellwig 1669e735c007SMatthew Wilcox (Oracle) trace_iomap_writepage(inode, folio_pos(folio), folio_size(folio)); 1670598ecfbaSChristoph Hellwig 1671598ecfbaSChristoph Hellwig /* 1672e735c007SMatthew Wilcox (Oracle) * Refuse to write the folio out if we're called from reclaim context. 1673598ecfbaSChristoph Hellwig * 1674598ecfbaSChristoph Hellwig * This avoids stack overflows when called from deeply used stacks in 1675598ecfbaSChristoph Hellwig * random callers for direct reclaim or memcg reclaim. We explicitly 1676598ecfbaSChristoph Hellwig * allow reclaim from kswapd as the stack usage there is relatively low. 1677598ecfbaSChristoph Hellwig * 1678598ecfbaSChristoph Hellwig * This should never happen except in the case of a VM regression so 1679598ecfbaSChristoph Hellwig * warn about it. 1680598ecfbaSChristoph Hellwig */ 1681598ecfbaSChristoph Hellwig if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == 1682598ecfbaSChristoph Hellwig PF_MEMALLOC)) 1683598ecfbaSChristoph Hellwig goto redirty; 1684598ecfbaSChristoph Hellwig 1685598ecfbaSChristoph Hellwig /* 1686e735c007SMatthew Wilcox (Oracle) * Is this folio beyond the end of the file? 1687598ecfbaSChristoph Hellwig * 1688e735c007SMatthew Wilcox (Oracle) * The folio index is less than the end_index, adjust the end_pos 1689e735c007SMatthew Wilcox (Oracle) * to the highest offset that this folio should represent. 1690598ecfbaSChristoph Hellwig * ----------------------------------------------------- 1691598ecfbaSChristoph Hellwig * | file mapping | <EOF> | 1692598ecfbaSChristoph Hellwig * ----------------------------------------------------- 1693598ecfbaSChristoph Hellwig * | Page ... | Page N-2 | Page N-1 | Page N | | 1694598ecfbaSChristoph Hellwig * ^--------------------------------^----------|-------- 1695598ecfbaSChristoph Hellwig * | desired writeback range | see else | 1696598ecfbaSChristoph Hellwig * ---------------------------------^------------------| 1697598ecfbaSChristoph Hellwig */ 169881d4782aSMatthew Wilcox (Oracle) isize = i_size_read(inode); 1699e735c007SMatthew Wilcox (Oracle) end_pos = folio_pos(folio) + folio_size(folio); 170081d4782aSMatthew Wilcox (Oracle) if (end_pos > isize) { 1701598ecfbaSChristoph Hellwig /* 1702598ecfbaSChristoph Hellwig * Check whether the page to write out is beyond or straddles 1703598ecfbaSChristoph Hellwig * i_size or not. 1704598ecfbaSChristoph Hellwig * ------------------------------------------------------- 1705598ecfbaSChristoph Hellwig * | file mapping | <EOF> | 1706598ecfbaSChristoph Hellwig * ------------------------------------------------------- 1707598ecfbaSChristoph Hellwig * | Page ... | Page N-2 | Page N-1 | Page N | Beyond | 1708598ecfbaSChristoph Hellwig * ^--------------------------------^-----------|--------- 1709598ecfbaSChristoph Hellwig * | | Straddles | 1710598ecfbaSChristoph Hellwig * ---------------------------------^-----------|--------| 1711598ecfbaSChristoph Hellwig */ 1712e735c007SMatthew Wilcox (Oracle) size_t poff = offset_in_folio(folio, isize); 171381d4782aSMatthew Wilcox (Oracle) pgoff_t end_index = isize >> PAGE_SHIFT; 1714598ecfbaSChristoph Hellwig 1715598ecfbaSChristoph Hellwig /* 1716d58562caSChris Mason * Skip the page if it's fully outside i_size, e.g. 1717d58562caSChris Mason * due to a truncate operation that's in progress. We've 1718d58562caSChris Mason * cleaned this page and truncate will finish things off for 1719d58562caSChris Mason * us. 1720598ecfbaSChristoph Hellwig * 1721f1f264b4SAndreas Gruenbacher * Note that the end_index is unsigned long. If the given 1722f1f264b4SAndreas Gruenbacher * offset is greater than 16TB on a 32-bit system then if we 1723f1f264b4SAndreas Gruenbacher * checked if the page is fully outside i_size with 1724f1f264b4SAndreas Gruenbacher * "if (page->index >= end_index + 1)", "end_index + 1" would 1725f1f264b4SAndreas Gruenbacher * overflow and evaluate to 0. Hence this page would be 1726f1f264b4SAndreas Gruenbacher * redirtied and written out repeatedly, which would result in 1727f1f264b4SAndreas Gruenbacher * an infinite loop; the user program performing this operation 1728f1f264b4SAndreas Gruenbacher * would hang. Instead, we can detect this situation by 1729f1f264b4SAndreas Gruenbacher * checking if the page is totally beyond i_size or if its 1730598ecfbaSChristoph Hellwig * offset is just equal to the EOF. 1731598ecfbaSChristoph Hellwig */ 1732e735c007SMatthew Wilcox (Oracle) if (folio->index > end_index || 1733e735c007SMatthew Wilcox (Oracle) (folio->index == end_index && poff == 0)) 1734d58562caSChris Mason goto unlock; 1735598ecfbaSChristoph Hellwig 1736598ecfbaSChristoph Hellwig /* 1737598ecfbaSChristoph Hellwig * The page straddles i_size. It must be zeroed out on each 1738598ecfbaSChristoph Hellwig * and every writepage invocation because it may be mmapped. 1739598ecfbaSChristoph Hellwig * "A file is mapped in multiples of the page size. For a file 1740598ecfbaSChristoph Hellwig * that is not a multiple of the page size, the remaining 1741598ecfbaSChristoph Hellwig * memory is zeroed when mapped, and writes to that region are 1742598ecfbaSChristoph Hellwig * not written out to the file." 1743598ecfbaSChristoph Hellwig */ 1744e735c007SMatthew Wilcox (Oracle) folio_zero_segment(folio, poff, folio_size(folio)); 174581d4782aSMatthew Wilcox (Oracle) end_pos = isize; 1746598ecfbaSChristoph Hellwig } 1747598ecfbaSChristoph Hellwig 1748e735c007SMatthew Wilcox (Oracle) return iomap_writepage_map(wpc, wbc, inode, folio, end_pos); 1749598ecfbaSChristoph Hellwig 1750598ecfbaSChristoph Hellwig redirty: 1751e735c007SMatthew Wilcox (Oracle) folio_redirty_for_writepage(wbc, folio); 1752d58562caSChris Mason unlock: 1753e735c007SMatthew Wilcox (Oracle) folio_unlock(folio); 1754598ecfbaSChristoph Hellwig return 0; 1755598ecfbaSChristoph Hellwig } 1756598ecfbaSChristoph Hellwig 1757598ecfbaSChristoph Hellwig int 1758598ecfbaSChristoph Hellwig iomap_writepages(struct address_space *mapping, struct writeback_control *wbc, 1759598ecfbaSChristoph Hellwig struct iomap_writepage_ctx *wpc, 1760598ecfbaSChristoph Hellwig const struct iomap_writeback_ops *ops) 1761598ecfbaSChristoph Hellwig { 1762598ecfbaSChristoph Hellwig int ret; 1763598ecfbaSChristoph Hellwig 1764598ecfbaSChristoph Hellwig wpc->ops = ops; 1765598ecfbaSChristoph Hellwig ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc); 1766598ecfbaSChristoph Hellwig if (!wpc->ioend) 1767598ecfbaSChristoph Hellwig return ret; 1768598ecfbaSChristoph Hellwig return iomap_submit_ioend(wpc, wpc->ioend, ret); 1769598ecfbaSChristoph Hellwig } 1770598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_writepages); 1771598ecfbaSChristoph Hellwig 1772598ecfbaSChristoph Hellwig static int __init iomap_init(void) 1773598ecfbaSChristoph Hellwig { 1774598ecfbaSChristoph Hellwig return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE), 1775598ecfbaSChristoph Hellwig offsetof(struct iomap_ioend, io_inline_bio), 1776598ecfbaSChristoph Hellwig BIOSET_NEED_BVECS); 1777598ecfbaSChristoph Hellwig } 1778598ecfbaSChristoph Hellwig fs_initcall(iomap_init); 1779