xref: /openbmc/linux/fs/iomap/buffered-io.c (revision aad29a73199b7fbccfbabea3f1ee627ad1924f52)
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 
260af2b37dSRitesh Harjani (IBM) typedef int (*iomap_punch_t)(struct inode *inode, loff_t offset, loff_t length);
27ab08b01eSChristoph Hellwig /*
284ce02c67SRitesh Harjani (IBM)  * Structure allocated for each folio to track per-block uptodate, dirty state
2904f52c4eSRitesh Harjani (IBM)  * and I/O completions.
30ab08b01eSChristoph Hellwig  */
3104f52c4eSRitesh Harjani (IBM) struct iomap_folio_state {
327d636676SMatthew Wilcox (Oracle) 	atomic_t		read_bytes_pending;
330fb2d720SMatthew Wilcox (Oracle) 	atomic_t		write_bytes_pending;
3404f52c4eSRitesh Harjani (IBM) 	spinlock_t		state_lock;
354ce02c67SRitesh Harjani (IBM) 
364ce02c67SRitesh Harjani (IBM) 	/*
374ce02c67SRitesh Harjani (IBM) 	 * Each block has two bits in this bitmap:
384ce02c67SRitesh Harjani (IBM) 	 * Bits [0..blocks_per_folio) has the uptodate status.
394ce02c67SRitesh Harjani (IBM) 	 * Bits [b_p_f...(2*b_p_f))   has the dirty status.
404ce02c67SRitesh Harjani (IBM) 	 */
4104f52c4eSRitesh Harjani (IBM) 	unsigned long		state[];
42ab08b01eSChristoph Hellwig };
43ab08b01eSChristoph Hellwig 
44598ecfbaSChristoph Hellwig static struct bio_set iomap_ioend_bioset;
45598ecfbaSChristoph Hellwig 
ifs_is_fully_uptodate(struct folio * folio,struct iomap_folio_state * ifs)46cc86181aSRitesh Harjani (IBM) static inline bool ifs_is_fully_uptodate(struct folio *folio,
47cc86181aSRitesh Harjani (IBM) 		struct iomap_folio_state *ifs)
48cc86181aSRitesh Harjani (IBM) {
49cc86181aSRitesh Harjani (IBM) 	struct inode *inode = folio->mapping->host;
50cc86181aSRitesh Harjani (IBM) 
51cc86181aSRitesh Harjani (IBM) 	return bitmap_full(ifs->state, i_blocks_per_folio(inode, folio));
52cc86181aSRitesh Harjani (IBM) }
53cc86181aSRitesh Harjani (IBM) 
ifs_block_is_uptodate(struct iomap_folio_state * ifs,unsigned int block)54cc86181aSRitesh Harjani (IBM) static inline bool ifs_block_is_uptodate(struct iomap_folio_state *ifs,
55cc86181aSRitesh Harjani (IBM) 		unsigned int block)
56cc86181aSRitesh Harjani (IBM) {
57cc86181aSRitesh Harjani (IBM) 	return test_bit(block, ifs->state);
58cc86181aSRitesh Harjani (IBM) }
59cc86181aSRitesh Harjani (IBM) 
ifs_set_range_uptodate(struct folio * folio,struct iomap_folio_state * ifs,size_t off,size_t len)603ea5c76cSRitesh Harjani (IBM) static void ifs_set_range_uptodate(struct folio *folio,
613ea5c76cSRitesh Harjani (IBM) 		struct iomap_folio_state *ifs, size_t off, size_t len)
623ea5c76cSRitesh Harjani (IBM) {
633ea5c76cSRitesh Harjani (IBM) 	struct inode *inode = folio->mapping->host;
643ea5c76cSRitesh Harjani (IBM) 	unsigned int first_blk = off >> inode->i_blkbits;
653ea5c76cSRitesh Harjani (IBM) 	unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
663ea5c76cSRitesh Harjani (IBM) 	unsigned int nr_blks = last_blk - first_blk + 1;
673ea5c76cSRitesh Harjani (IBM) 	unsigned long flags;
683ea5c76cSRitesh Harjani (IBM) 
693ea5c76cSRitesh Harjani (IBM) 	spin_lock_irqsave(&ifs->state_lock, flags);
703ea5c76cSRitesh Harjani (IBM) 	bitmap_set(ifs->state, first_blk, nr_blks);
71cc86181aSRitesh Harjani (IBM) 	if (ifs_is_fully_uptodate(folio, ifs))
723ea5c76cSRitesh Harjani (IBM) 		folio_mark_uptodate(folio);
733ea5c76cSRitesh Harjani (IBM) 	spin_unlock_irqrestore(&ifs->state_lock, flags);
743ea5c76cSRitesh Harjani (IBM) }
753ea5c76cSRitesh Harjani (IBM) 
iomap_set_range_uptodate(struct folio * folio,size_t off,size_t len)763ea5c76cSRitesh Harjani (IBM) static void iomap_set_range_uptodate(struct folio *folio, size_t off,
773ea5c76cSRitesh Harjani (IBM) 		size_t len)
783ea5c76cSRitesh Harjani (IBM) {
793ea5c76cSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
803ea5c76cSRitesh Harjani (IBM) 
813ea5c76cSRitesh Harjani (IBM) 	if (ifs)
823ea5c76cSRitesh Harjani (IBM) 		ifs_set_range_uptodate(folio, ifs, off, len);
833ea5c76cSRitesh Harjani (IBM) 	else
843ea5c76cSRitesh Harjani (IBM) 		folio_mark_uptodate(folio);
853ea5c76cSRitesh Harjani (IBM) }
863ea5c76cSRitesh Harjani (IBM) 
ifs_block_is_dirty(struct folio * folio,struct iomap_folio_state * ifs,int block)874ce02c67SRitesh Harjani (IBM) static inline bool ifs_block_is_dirty(struct folio *folio,
884ce02c67SRitesh Harjani (IBM) 		struct iomap_folio_state *ifs, int block)
894ce02c67SRitesh Harjani (IBM) {
904ce02c67SRitesh Harjani (IBM) 	struct inode *inode = folio->mapping->host;
914ce02c67SRitesh Harjani (IBM) 	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
924ce02c67SRitesh Harjani (IBM) 
934ce02c67SRitesh Harjani (IBM) 	return test_bit(block + blks_per_folio, ifs->state);
944ce02c67SRitesh Harjani (IBM) }
954ce02c67SRitesh Harjani (IBM) 
ifs_clear_range_dirty(struct folio * folio,struct iomap_folio_state * ifs,size_t off,size_t len)964ce02c67SRitesh Harjani (IBM) static void ifs_clear_range_dirty(struct folio *folio,
974ce02c67SRitesh Harjani (IBM) 		struct iomap_folio_state *ifs, size_t off, size_t len)
984ce02c67SRitesh Harjani (IBM) {
994ce02c67SRitesh Harjani (IBM) 	struct inode *inode = folio->mapping->host;
1004ce02c67SRitesh Harjani (IBM) 	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
1014ce02c67SRitesh Harjani (IBM) 	unsigned int first_blk = (off >> inode->i_blkbits);
1024ce02c67SRitesh Harjani (IBM) 	unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
1034ce02c67SRitesh Harjani (IBM) 	unsigned int nr_blks = last_blk - first_blk + 1;
1044ce02c67SRitesh Harjani (IBM) 	unsigned long flags;
1054ce02c67SRitesh Harjani (IBM) 
1064ce02c67SRitesh Harjani (IBM) 	spin_lock_irqsave(&ifs->state_lock, flags);
1074ce02c67SRitesh Harjani (IBM) 	bitmap_clear(ifs->state, first_blk + blks_per_folio, nr_blks);
1084ce02c67SRitesh Harjani (IBM) 	spin_unlock_irqrestore(&ifs->state_lock, flags);
1094ce02c67SRitesh Harjani (IBM) }
1104ce02c67SRitesh Harjani (IBM) 
iomap_clear_range_dirty(struct folio * folio,size_t off,size_t len)1114ce02c67SRitesh Harjani (IBM) static void iomap_clear_range_dirty(struct folio *folio, size_t off, size_t len)
1124ce02c67SRitesh Harjani (IBM) {
1134ce02c67SRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
1144ce02c67SRitesh Harjani (IBM) 
1154ce02c67SRitesh Harjani (IBM) 	if (ifs)
1164ce02c67SRitesh Harjani (IBM) 		ifs_clear_range_dirty(folio, ifs, off, len);
1174ce02c67SRitesh Harjani (IBM) }
1184ce02c67SRitesh Harjani (IBM) 
ifs_set_range_dirty(struct folio * folio,struct iomap_folio_state * ifs,size_t off,size_t len)1194ce02c67SRitesh Harjani (IBM) static void ifs_set_range_dirty(struct folio *folio,
1204ce02c67SRitesh Harjani (IBM) 		struct iomap_folio_state *ifs, size_t off, size_t len)
1214ce02c67SRitesh Harjani (IBM) {
1224ce02c67SRitesh Harjani (IBM) 	struct inode *inode = folio->mapping->host;
1234ce02c67SRitesh Harjani (IBM) 	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
1244ce02c67SRitesh Harjani (IBM) 	unsigned int first_blk = (off >> inode->i_blkbits);
1254ce02c67SRitesh Harjani (IBM) 	unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
1264ce02c67SRitesh Harjani (IBM) 	unsigned int nr_blks = last_blk - first_blk + 1;
1274ce02c67SRitesh Harjani (IBM) 	unsigned long flags;
1284ce02c67SRitesh Harjani (IBM) 
1294ce02c67SRitesh Harjani (IBM) 	spin_lock_irqsave(&ifs->state_lock, flags);
1304ce02c67SRitesh Harjani (IBM) 	bitmap_set(ifs->state, first_blk + blks_per_folio, nr_blks);
1314ce02c67SRitesh Harjani (IBM) 	spin_unlock_irqrestore(&ifs->state_lock, flags);
1324ce02c67SRitesh Harjani (IBM) }
1334ce02c67SRitesh Harjani (IBM) 
iomap_set_range_dirty(struct folio * folio,size_t off,size_t len)1344ce02c67SRitesh Harjani (IBM) static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
1354ce02c67SRitesh Harjani (IBM) {
1364ce02c67SRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
1374ce02c67SRitesh Harjani (IBM) 
1384ce02c67SRitesh Harjani (IBM) 	if (ifs)
1394ce02c67SRitesh Harjani (IBM) 		ifs_set_range_dirty(folio, ifs, off, len);
1404ce02c67SRitesh Harjani (IBM) }
1414ce02c67SRitesh Harjani (IBM) 
ifs_alloc(struct inode * inode,struct folio * folio,unsigned int flags)14204f52c4eSRitesh Harjani (IBM) static struct iomap_folio_state *ifs_alloc(struct inode *inode,
14304f52c4eSRitesh Harjani (IBM) 		struct folio *folio, unsigned int flags)
144afc51aaaSDarrick J. Wong {
14504f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
146435d44b3SMatthew Wilcox (Oracle) 	unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
1479753b868SStefan Roesch 	gfp_t gfp;
148afc51aaaSDarrick J. Wong 
14904f52c4eSRitesh Harjani (IBM) 	if (ifs || nr_blocks <= 1)
15004f52c4eSRitesh Harjani (IBM) 		return ifs;
151afc51aaaSDarrick J. Wong 
1529753b868SStefan Roesch 	if (flags & IOMAP_NOWAIT)
1539753b868SStefan Roesch 		gfp = GFP_NOWAIT;
1549753b868SStefan Roesch 	else
1559753b868SStefan Roesch 		gfp = GFP_NOFS | __GFP_NOFAIL;
1569753b868SStefan Roesch 
1574ce02c67SRitesh Harjani (IBM) 	/*
1584ce02c67SRitesh Harjani (IBM) 	 * ifs->state tracks two sets of state flags when the
1594ce02c67SRitesh Harjani (IBM) 	 * filesystem block size is smaller than the folio size.
1604ce02c67SRitesh Harjani (IBM) 	 * The first state tracks per-block uptodate and the
1614ce02c67SRitesh Harjani (IBM) 	 * second tracks per-block dirty state.
1624ce02c67SRitesh Harjani (IBM) 	 */
1634ce02c67SRitesh Harjani (IBM) 	ifs = kzalloc(struct_size(ifs, state,
1644ce02c67SRitesh Harjani (IBM) 		      BITS_TO_LONGS(2 * nr_blocks)), gfp);
1654ce02c67SRitesh Harjani (IBM) 	if (!ifs)
1664ce02c67SRitesh Harjani (IBM) 		return ifs;
1674ce02c67SRitesh Harjani (IBM) 
16804f52c4eSRitesh Harjani (IBM) 	spin_lock_init(&ifs->state_lock);
169435d44b3SMatthew Wilcox (Oracle) 	if (folio_test_uptodate(folio))
1704ce02c67SRitesh Harjani (IBM) 		bitmap_set(ifs->state, 0, nr_blocks);
1714ce02c67SRitesh Harjani (IBM) 	if (folio_test_dirty(folio))
1724ce02c67SRitesh Harjani (IBM) 		bitmap_set(ifs->state, nr_blocks, nr_blocks);
17304f52c4eSRitesh Harjani (IBM) 	folio_attach_private(folio, ifs);
1744ce02c67SRitesh Harjani (IBM) 
17504f52c4eSRitesh Harjani (IBM) 	return ifs;
176afc51aaaSDarrick J. Wong }
177afc51aaaSDarrick J. Wong 
ifs_free(struct folio * folio)17804f52c4eSRitesh Harjani (IBM) static void ifs_free(struct folio *folio)
179afc51aaaSDarrick J. Wong {
18004f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio_detach_private(folio);
181afc51aaaSDarrick J. Wong 
18204f52c4eSRitesh Harjani (IBM) 	if (!ifs)
183afc51aaaSDarrick J. Wong 		return;
18404f52c4eSRitesh Harjani (IBM) 	WARN_ON_ONCE(atomic_read(&ifs->read_bytes_pending));
18504f52c4eSRitesh Harjani (IBM) 	WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending));
186cc86181aSRitesh Harjani (IBM) 	WARN_ON_ONCE(ifs_is_fully_uptodate(folio, ifs) !=
187c46e8324SMatthew Wilcox (Oracle) 			folio_test_uptodate(folio));
18804f52c4eSRitesh Harjani (IBM) 	kfree(ifs);
189afc51aaaSDarrick J. Wong }
190afc51aaaSDarrick J. Wong 
191afc51aaaSDarrick J. Wong /*
192431c0566SMatthew Wilcox (Oracle)  * Calculate the range inside the folio that we actually need to read.
193afc51aaaSDarrick J. Wong  */
iomap_adjust_read_range(struct inode * inode,struct folio * folio,loff_t * pos,loff_t length,size_t * offp,size_t * lenp)194431c0566SMatthew Wilcox (Oracle) static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
195431c0566SMatthew Wilcox (Oracle) 		loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
196afc51aaaSDarrick J. Wong {
19704f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
198afc51aaaSDarrick J. Wong 	loff_t orig_pos = *pos;
199afc51aaaSDarrick J. Wong 	loff_t isize = i_size_read(inode);
200afc51aaaSDarrick J. Wong 	unsigned block_bits = inode->i_blkbits;
201afc51aaaSDarrick J. Wong 	unsigned block_size = (1 << block_bits);
202431c0566SMatthew Wilcox (Oracle) 	size_t poff = offset_in_folio(folio, *pos);
203431c0566SMatthew Wilcox (Oracle) 	size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
20424be4091SRitesh Harjani (IBM) 	size_t orig_plen = plen;
205afc51aaaSDarrick J. Wong 	unsigned first = poff >> block_bits;
206afc51aaaSDarrick J. Wong 	unsigned last = (poff + plen - 1) >> block_bits;
207afc51aaaSDarrick J. Wong 
208afc51aaaSDarrick J. Wong 	/*
209f1f264b4SAndreas Gruenbacher 	 * If the block size is smaller than the page size, we need to check the
210afc51aaaSDarrick J. Wong 	 * per-block uptodate status and adjust the offset and length if needed
211afc51aaaSDarrick J. Wong 	 * to avoid reading in already uptodate ranges.
212afc51aaaSDarrick J. Wong 	 */
21304f52c4eSRitesh Harjani (IBM) 	if (ifs) {
214afc51aaaSDarrick J. Wong 		unsigned int i;
215afc51aaaSDarrick J. Wong 
216afc51aaaSDarrick J. Wong 		/* move forward for each leading block marked uptodate */
217afc51aaaSDarrick J. Wong 		for (i = first; i <= last; i++) {
218cc86181aSRitesh Harjani (IBM) 			if (!ifs_block_is_uptodate(ifs, i))
219afc51aaaSDarrick J. Wong 				break;
220afc51aaaSDarrick J. Wong 			*pos += block_size;
221afc51aaaSDarrick J. Wong 			poff += block_size;
222afc51aaaSDarrick J. Wong 			plen -= block_size;
223afc51aaaSDarrick J. Wong 			first++;
224afc51aaaSDarrick J. Wong 		}
225afc51aaaSDarrick J. Wong 
226afc51aaaSDarrick J. Wong 		/* truncate len if we find any trailing uptodate block(s) */
227afc51aaaSDarrick J. Wong 		for ( ; i <= last; i++) {
228cc86181aSRitesh Harjani (IBM) 			if (ifs_block_is_uptodate(ifs, i)) {
229afc51aaaSDarrick J. Wong 				plen -= (last - i + 1) * block_size;
230afc51aaaSDarrick J. Wong 				last = i - 1;
231afc51aaaSDarrick J. Wong 				break;
232afc51aaaSDarrick J. Wong 			}
233afc51aaaSDarrick J. Wong 		}
234afc51aaaSDarrick J. Wong 	}
235afc51aaaSDarrick J. Wong 
236afc51aaaSDarrick J. Wong 	/*
237f1f264b4SAndreas Gruenbacher 	 * If the extent spans the block that contains the i_size, we need to
238afc51aaaSDarrick J. Wong 	 * handle both halves separately so that we properly zero data in the
239afc51aaaSDarrick J. Wong 	 * page cache for blocks that are entirely outside of i_size.
240afc51aaaSDarrick J. Wong 	 */
24124be4091SRitesh Harjani (IBM) 	if (orig_pos <= isize && orig_pos + orig_plen > isize) {
242431c0566SMatthew Wilcox (Oracle) 		unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
243afc51aaaSDarrick J. Wong 
244afc51aaaSDarrick J. Wong 		if (first <= end && last > end)
245afc51aaaSDarrick J. Wong 			plen -= (last - end) * block_size;
246afc51aaaSDarrick J. Wong 	}
247afc51aaaSDarrick J. Wong 
248afc51aaaSDarrick J. Wong 	*offp = poff;
249afc51aaaSDarrick J. Wong 	*lenp = plen;
250afc51aaaSDarrick J. Wong }
251afc51aaaSDarrick J. Wong 
iomap_finish_folio_read(struct folio * folio,size_t offset,size_t len,int error)2528ffd74e9SMatthew Wilcox (Oracle) static void iomap_finish_folio_read(struct folio *folio, size_t offset,
2538ffd74e9SMatthew Wilcox (Oracle) 		size_t len, int error)
254afc51aaaSDarrick J. Wong {
25504f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
256afc51aaaSDarrick J. Wong 
257afc51aaaSDarrick J. Wong 	if (unlikely(error)) {
2588ffd74e9SMatthew Wilcox (Oracle) 		folio_clear_uptodate(folio);
2598ffd74e9SMatthew Wilcox (Oracle) 		folio_set_error(folio);
260afc51aaaSDarrick J. Wong 	} else {
2613ea5c76cSRitesh Harjani (IBM) 		iomap_set_range_uptodate(folio, offset, len);
262afc51aaaSDarrick J. Wong 	}
263afc51aaaSDarrick J. Wong 
26404f52c4eSRitesh Harjani (IBM) 	if (!ifs || atomic_sub_and_test(len, &ifs->read_bytes_pending))
2658ffd74e9SMatthew Wilcox (Oracle) 		folio_unlock(folio);
266afc51aaaSDarrick J. Wong }
267afc51aaaSDarrick J. Wong 
iomap_read_end_io(struct bio * bio)2688ffd74e9SMatthew Wilcox (Oracle) static void iomap_read_end_io(struct bio *bio)
269afc51aaaSDarrick J. Wong {
270afc51aaaSDarrick J. Wong 	int error = blk_status_to_errno(bio->bi_status);
2718ffd74e9SMatthew Wilcox (Oracle) 	struct folio_iter fi;
272afc51aaaSDarrick J. Wong 
2738ffd74e9SMatthew Wilcox (Oracle) 	bio_for_each_folio_all(fi, bio)
2748ffd74e9SMatthew Wilcox (Oracle) 		iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error);
275afc51aaaSDarrick J. Wong 	bio_put(bio);
276afc51aaaSDarrick J. Wong }
277afc51aaaSDarrick J. Wong 
278afc51aaaSDarrick J. Wong struct iomap_readpage_ctx {
2793aa9c659SMatthew Wilcox (Oracle) 	struct folio		*cur_folio;
2803aa9c659SMatthew Wilcox (Oracle) 	bool			cur_folio_in_bio;
281afc51aaaSDarrick J. Wong 	struct bio		*bio;
2829d24a13aSMatthew Wilcox (Oracle) 	struct readahead_control *rac;
283afc51aaaSDarrick J. Wong };
284afc51aaaSDarrick J. Wong 
2855ad448ceSAndreas Gruenbacher /**
2865ad448ceSAndreas Gruenbacher  * iomap_read_inline_data - copy inline data into the page cache
2875ad448ceSAndreas Gruenbacher  * @iter: iteration structure
288874628a2SMatthew Wilcox (Oracle)  * @folio: folio to copy to
2895ad448ceSAndreas Gruenbacher  *
290874628a2SMatthew Wilcox (Oracle)  * Copy the inline data in @iter into @folio and zero out the rest of the folio.
2915ad448ceSAndreas Gruenbacher  * Only a single IOMAP_INLINE extent is allowed at the end of each file.
2925ad448ceSAndreas Gruenbacher  * Returns zero for success to complete the read, or the usual negative errno.
2935ad448ceSAndreas Gruenbacher  */
iomap_read_inline_data(const struct iomap_iter * iter,struct folio * folio)2945ad448ceSAndreas Gruenbacher static int iomap_read_inline_data(const struct iomap_iter *iter,
295874628a2SMatthew Wilcox (Oracle) 		struct folio *folio)
296afc51aaaSDarrick J. Wong {
297fad0a1abSChristoph Hellwig 	const struct iomap *iomap = iomap_iter_srcmap(iter);
2981b5c1e36SChristoph Hellwig 	size_t size = i_size_read(iter->inode) - iomap->offset;
299b405435bSMatthew Wilcox (Oracle) 	size_t poff = offset_in_page(iomap->offset);
300431c0566SMatthew Wilcox (Oracle) 	size_t offset = offset_in_folio(folio, iomap->offset);
301afc51aaaSDarrick J. Wong 	void *addr;
302afc51aaaSDarrick J. Wong 
303874628a2SMatthew Wilcox (Oracle) 	if (folio_test_uptodate(folio))
3045ad448ceSAndreas Gruenbacher 		return 0;
305afc51aaaSDarrick J. Wong 
306ae44f9c2SMatthew Wilcox (Oracle) 	if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
307ae44f9c2SMatthew Wilcox (Oracle) 		return -EIO;
30869f4a26cSGao Xiang 	if (WARN_ON_ONCE(size > PAGE_SIZE -
30969f4a26cSGao Xiang 			 offset_in_page(iomap->inline_data)))
31069f4a26cSGao Xiang 		return -EIO;
31169f4a26cSGao Xiang 	if (WARN_ON_ONCE(size > iomap->length))
31269f4a26cSGao Xiang 		return -EIO;
313431c0566SMatthew Wilcox (Oracle) 	if (offset > 0)
3143ea5c76cSRitesh Harjani (IBM) 		ifs_alloc(iter->inode, folio, iter->flags);
315afc51aaaSDarrick J. Wong 
316874628a2SMatthew Wilcox (Oracle) 	addr = kmap_local_folio(folio, offset);
317afc51aaaSDarrick J. Wong 	memcpy(addr, iomap->inline_data, size);
318b405435bSMatthew Wilcox (Oracle) 	memset(addr + size, 0, PAGE_SIZE - poff - size);
319ab069d5fSMatthew Wilcox (Oracle) 	kunmap_local(addr);
3203ea5c76cSRitesh Harjani (IBM) 	iomap_set_range_uptodate(folio, offset, PAGE_SIZE - poff);
3215ad448ceSAndreas Gruenbacher 	return 0;
322afc51aaaSDarrick J. Wong }
323afc51aaaSDarrick J. Wong 
iomap_block_needs_zeroing(const struct iomap_iter * iter,loff_t pos)324fad0a1abSChristoph Hellwig static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
3251b5c1e36SChristoph Hellwig 		loff_t pos)
326009d8d84SChristoph Hellwig {
327fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
3281b5c1e36SChristoph Hellwig 
3291b5c1e36SChristoph Hellwig 	return srcmap->type != IOMAP_MAPPED ||
3301b5c1e36SChristoph Hellwig 		(srcmap->flags & IOMAP_F_NEW) ||
3311b5c1e36SChristoph Hellwig 		pos >= i_size_read(iter->inode);
332009d8d84SChristoph Hellwig }
333009d8d84SChristoph Hellwig 
iomap_readpage_iter(const struct iomap_iter * iter,struct iomap_readpage_ctx * ctx,loff_t offset)334fad0a1abSChristoph Hellwig static loff_t iomap_readpage_iter(const struct iomap_iter *iter,
335f6d48000SChristoph Hellwig 		struct iomap_readpage_ctx *ctx, loff_t offset)
336afc51aaaSDarrick J. Wong {
337fad0a1abSChristoph Hellwig 	const struct iomap *iomap = &iter->iomap;
338f6d48000SChristoph Hellwig 	loff_t pos = iter->pos + offset;
339f6d48000SChristoph Hellwig 	loff_t length = iomap_length(iter) - offset;
3403aa9c659SMatthew Wilcox (Oracle) 	struct folio *folio = ctx->cur_folio;
34104f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs;
342afc51aaaSDarrick J. Wong 	loff_t orig_pos = pos;
343431c0566SMatthew Wilcox (Oracle) 	size_t poff, plen;
344afc51aaaSDarrick J. Wong 	sector_t sector;
345afc51aaaSDarrick J. Wong 
3465ad448ceSAndreas Gruenbacher 	if (iomap->type == IOMAP_INLINE)
347874628a2SMatthew Wilcox (Oracle) 		return iomap_read_inline_data(iter, folio);
348afc51aaaSDarrick J. Wong 
349afc51aaaSDarrick J. Wong 	/* zero post-eof blocks as the page may be mapped */
35004f52c4eSRitesh Harjani (IBM) 	ifs = ifs_alloc(iter->inode, folio, iter->flags);
351431c0566SMatthew Wilcox (Oracle) 	iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen);
352afc51aaaSDarrick J. Wong 	if (plen == 0)
353afc51aaaSDarrick J. Wong 		goto done;
354afc51aaaSDarrick J. Wong 
3551b5c1e36SChristoph Hellwig 	if (iomap_block_needs_zeroing(iter, pos)) {
356431c0566SMatthew Wilcox (Oracle) 		folio_zero_range(folio, poff, plen);
3573ea5c76cSRitesh Harjani (IBM) 		iomap_set_range_uptodate(folio, poff, plen);
358afc51aaaSDarrick J. Wong 		goto done;
359afc51aaaSDarrick J. Wong 	}
360afc51aaaSDarrick J. Wong 
3613aa9c659SMatthew Wilcox (Oracle) 	ctx->cur_folio_in_bio = true;
36204f52c4eSRitesh Harjani (IBM) 	if (ifs)
36304f52c4eSRitesh Harjani (IBM) 		atomic_add(plen, &ifs->read_bytes_pending);
364afc51aaaSDarrick J. Wong 
365afc51aaaSDarrick J. Wong 	sector = iomap_sector(iomap, pos);
366d0364f94SChristoph Hellwig 	if (!ctx->bio ||
367d0364f94SChristoph Hellwig 	    bio_end_sector(ctx->bio) != sector ||
368431c0566SMatthew Wilcox (Oracle) 	    !bio_add_folio(ctx->bio, folio, plen, poff)) {
3693aa9c659SMatthew Wilcox (Oracle) 		gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
370457df33eSMatthew Wilcox (Oracle) 		gfp_t orig_gfp = gfp;
3715f7136dbSMatthew Wilcox (Oracle) 		unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
372afc51aaaSDarrick J. Wong 
373afc51aaaSDarrick J. Wong 		if (ctx->bio)
374afc51aaaSDarrick J. Wong 			submit_bio(ctx->bio);
375afc51aaaSDarrick J. Wong 
3769d24a13aSMatthew Wilcox (Oracle) 		if (ctx->rac) /* same as readahead_gfp_mask */
377afc51aaaSDarrick J. Wong 			gfp |= __GFP_NORETRY | __GFP_NOWARN;
37807888c66SChristoph Hellwig 		ctx->bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs),
37907888c66SChristoph Hellwig 				     REQ_OP_READ, gfp);
380457df33eSMatthew Wilcox (Oracle) 		/*
381457df33eSMatthew Wilcox (Oracle) 		 * If the bio_alloc fails, try it again for a single page to
382457df33eSMatthew Wilcox (Oracle) 		 * avoid having to deal with partial page reads.  This emulates
383f132ab7dSMatthew Wilcox (Oracle) 		 * what do_mpage_read_folio does.
384457df33eSMatthew Wilcox (Oracle) 		 */
38507888c66SChristoph Hellwig 		if (!ctx->bio) {
38607888c66SChristoph Hellwig 			ctx->bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ,
38707888c66SChristoph Hellwig 					     orig_gfp);
38807888c66SChristoph Hellwig 		}
3899d24a13aSMatthew Wilcox (Oracle) 		if (ctx->rac)
390afc51aaaSDarrick J. Wong 			ctx->bio->bi_opf |= REQ_RAHEAD;
391afc51aaaSDarrick J. Wong 		ctx->bio->bi_iter.bi_sector = sector;
392afc51aaaSDarrick J. Wong 		ctx->bio->bi_end_io = iomap_read_end_io;
393c2478469SJohannes Thumshirn 		bio_add_folio_nofail(ctx->bio, folio, plen, poff);
394afc51aaaSDarrick J. Wong 	}
395431c0566SMatthew Wilcox (Oracle) 
396afc51aaaSDarrick J. Wong done:
397afc51aaaSDarrick J. Wong 	/*
398afc51aaaSDarrick J. Wong 	 * Move the caller beyond our range so that it keeps making progress.
399f1f264b4SAndreas Gruenbacher 	 * For that, we have to include any leading non-uptodate ranges, but
400afc51aaaSDarrick J. Wong 	 * we can skip trailing ones as they will be handled in the next
401afc51aaaSDarrick J. Wong 	 * iteration.
402afc51aaaSDarrick J. Wong 	 */
403afc51aaaSDarrick J. Wong 	return pos - orig_pos + plen;
404afc51aaaSDarrick J. Wong }
405afc51aaaSDarrick J. Wong 
iomap_read_folio(struct folio * folio,const struct iomap_ops * ops)4067479c505SMatthew Wilcox (Oracle) int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops)
407afc51aaaSDarrick J. Wong {
408f6d48000SChristoph Hellwig 	struct iomap_iter iter = {
4093aa9c659SMatthew Wilcox (Oracle) 		.inode		= folio->mapping->host,
4103aa9c659SMatthew Wilcox (Oracle) 		.pos		= folio_pos(folio),
4113aa9c659SMatthew Wilcox (Oracle) 		.len		= folio_size(folio),
412f6d48000SChristoph Hellwig 	};
413f6d48000SChristoph Hellwig 	struct iomap_readpage_ctx ctx = {
4143aa9c659SMatthew Wilcox (Oracle) 		.cur_folio	= folio,
415f6d48000SChristoph Hellwig 	};
416f6d48000SChristoph Hellwig 	int ret;
417afc51aaaSDarrick J. Wong 
4183aa9c659SMatthew Wilcox (Oracle) 	trace_iomap_readpage(iter.inode, 1);
4199e91c572SChristoph Hellwig 
420f6d48000SChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
421f6d48000SChristoph Hellwig 		iter.processed = iomap_readpage_iter(&iter, &ctx, 0);
422f6d48000SChristoph Hellwig 
423f6d48000SChristoph Hellwig 	if (ret < 0)
4243aa9c659SMatthew Wilcox (Oracle) 		folio_set_error(folio);
425afc51aaaSDarrick J. Wong 
426afc51aaaSDarrick J. Wong 	if (ctx.bio) {
427afc51aaaSDarrick J. Wong 		submit_bio(ctx.bio);
4283aa9c659SMatthew Wilcox (Oracle) 		WARN_ON_ONCE(!ctx.cur_folio_in_bio);
429afc51aaaSDarrick J. Wong 	} else {
4303aa9c659SMatthew Wilcox (Oracle) 		WARN_ON_ONCE(ctx.cur_folio_in_bio);
4313aa9c659SMatthew Wilcox (Oracle) 		folio_unlock(folio);
432afc51aaaSDarrick J. Wong 	}
433afc51aaaSDarrick J. Wong 
434afc51aaaSDarrick J. Wong 	/*
4352c69e205SMatthew Wilcox (Oracle) 	 * Just like mpage_readahead and block_read_full_folio, we always
4367479c505SMatthew Wilcox (Oracle) 	 * return 0 and just set the folio error flag on errors.  This
437f1f264b4SAndreas Gruenbacher 	 * should be cleaned up throughout the stack eventually.
438afc51aaaSDarrick J. Wong 	 */
439afc51aaaSDarrick J. Wong 	return 0;
440afc51aaaSDarrick J. Wong }
4417479c505SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_read_folio);
442afc51aaaSDarrick J. Wong 
iomap_readahead_iter(const struct iomap_iter * iter,struct iomap_readpage_ctx * ctx)443fad0a1abSChristoph Hellwig static loff_t iomap_readahead_iter(const struct iomap_iter *iter,
444f6d48000SChristoph Hellwig 		struct iomap_readpage_ctx *ctx)
445afc51aaaSDarrick J. Wong {
446f6d48000SChristoph Hellwig 	loff_t length = iomap_length(iter);
447afc51aaaSDarrick J. Wong 	loff_t done, ret;
448afc51aaaSDarrick J. Wong 
449afc51aaaSDarrick J. Wong 	for (done = 0; done < length; done += ret) {
4503aa9c659SMatthew Wilcox (Oracle) 		if (ctx->cur_folio &&
4513aa9c659SMatthew Wilcox (Oracle) 		    offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) {
4523aa9c659SMatthew Wilcox (Oracle) 			if (!ctx->cur_folio_in_bio)
4533aa9c659SMatthew Wilcox (Oracle) 				folio_unlock(ctx->cur_folio);
4543aa9c659SMatthew Wilcox (Oracle) 			ctx->cur_folio = NULL;
455afc51aaaSDarrick J. Wong 		}
4563aa9c659SMatthew Wilcox (Oracle) 		if (!ctx->cur_folio) {
4573aa9c659SMatthew Wilcox (Oracle) 			ctx->cur_folio = readahead_folio(ctx->rac);
4583aa9c659SMatthew Wilcox (Oracle) 			ctx->cur_folio_in_bio = false;
459afc51aaaSDarrick J. Wong 		}
460f6d48000SChristoph Hellwig 		ret = iomap_readpage_iter(iter, ctx, done);
461d8af404fSAndreas Gruenbacher 		if (ret <= 0)
462d8af404fSAndreas Gruenbacher 			return ret;
463afc51aaaSDarrick J. Wong 	}
464afc51aaaSDarrick J. Wong 
465afc51aaaSDarrick J. Wong 	return done;
466afc51aaaSDarrick J. Wong }
467afc51aaaSDarrick J. Wong 
4689d24a13aSMatthew Wilcox (Oracle) /**
4699d24a13aSMatthew Wilcox (Oracle)  * iomap_readahead - Attempt to read pages from a file.
4709d24a13aSMatthew Wilcox (Oracle)  * @rac: Describes the pages to be read.
4719d24a13aSMatthew Wilcox (Oracle)  * @ops: The operations vector for the filesystem.
4729d24a13aSMatthew Wilcox (Oracle)  *
4739d24a13aSMatthew Wilcox (Oracle)  * This function is for filesystems to call to implement their readahead
4749d24a13aSMatthew Wilcox (Oracle)  * address_space operation.
4759d24a13aSMatthew Wilcox (Oracle)  *
4769d24a13aSMatthew Wilcox (Oracle)  * Context: The @ops callbacks may submit I/O (eg to read the addresses of
4779d24a13aSMatthew Wilcox (Oracle)  * blocks from disc), and may wait for it.  The caller may be trying to
4789d24a13aSMatthew Wilcox (Oracle)  * access a different page, and so sleeping excessively should be avoided.
4799d24a13aSMatthew Wilcox (Oracle)  * It may allocate memory, but should avoid costly allocations.  This
4809d24a13aSMatthew Wilcox (Oracle)  * function is called with memalloc_nofs set, so allocations will not cause
4819d24a13aSMatthew Wilcox (Oracle)  * the filesystem to be reentered.
4829d24a13aSMatthew Wilcox (Oracle)  */
iomap_readahead(struct readahead_control * rac,const struct iomap_ops * ops)4839d24a13aSMatthew Wilcox (Oracle) void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
484afc51aaaSDarrick J. Wong {
485f6d48000SChristoph Hellwig 	struct iomap_iter iter = {
486f6d48000SChristoph Hellwig 		.inode	= rac->mapping->host,
487f6d48000SChristoph Hellwig 		.pos	= readahead_pos(rac),
488f6d48000SChristoph Hellwig 		.len	= readahead_length(rac),
489f6d48000SChristoph Hellwig 	};
490afc51aaaSDarrick J. Wong 	struct iomap_readpage_ctx ctx = {
4919d24a13aSMatthew Wilcox (Oracle) 		.rac	= rac,
492afc51aaaSDarrick J. Wong 	};
493afc51aaaSDarrick J. Wong 
494f6d48000SChristoph Hellwig 	trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
4959e91c572SChristoph Hellwig 
496f6d48000SChristoph Hellwig 	while (iomap_iter(&iter, ops) > 0)
497f6d48000SChristoph Hellwig 		iter.processed = iomap_readahead_iter(&iter, &ctx);
4989d24a13aSMatthew Wilcox (Oracle) 
499afc51aaaSDarrick J. Wong 	if (ctx.bio)
500afc51aaaSDarrick J. Wong 		submit_bio(ctx.bio);
5013aa9c659SMatthew Wilcox (Oracle) 	if (ctx.cur_folio) {
5023aa9c659SMatthew Wilcox (Oracle) 		if (!ctx.cur_folio_in_bio)
5033aa9c659SMatthew Wilcox (Oracle) 			folio_unlock(ctx.cur_folio);
504afc51aaaSDarrick J. Wong 	}
505afc51aaaSDarrick J. Wong }
5069d24a13aSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_readahead);
507afc51aaaSDarrick J. Wong 
508afc51aaaSDarrick J. Wong /*
5092e7e80f7SMatthew Wilcox (Oracle)  * iomap_is_partially_uptodate checks whether blocks within a folio are
510afc51aaaSDarrick J. Wong  * uptodate or not.
511afc51aaaSDarrick J. Wong  *
5122e7e80f7SMatthew Wilcox (Oracle)  * Returns true if all blocks which correspond to the specified part
5132e7e80f7SMatthew Wilcox (Oracle)  * of the folio are uptodate.
514afc51aaaSDarrick J. Wong  */
iomap_is_partially_uptodate(struct folio * folio,size_t from,size_t count)5152e7e80f7SMatthew Wilcox (Oracle) bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
516afc51aaaSDarrick J. Wong {
51704f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
5182e7e80f7SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
5192e7e80f7SMatthew Wilcox (Oracle) 	unsigned first, last, i;
520afc51aaaSDarrick J. Wong 
52104f52c4eSRitesh Harjani (IBM) 	if (!ifs)
5222e7e80f7SMatthew Wilcox (Oracle) 		return false;
5232e7e80f7SMatthew Wilcox (Oracle) 
5242756c818SMatthew Wilcox (Oracle) 	/* Caller's range may extend past the end of this folio */
5252756c818SMatthew Wilcox (Oracle) 	count = min(folio_size(folio) - from, count);
526afc51aaaSDarrick J. Wong 
5272756c818SMatthew Wilcox (Oracle) 	/* First and last blocks in range within folio */
528afc51aaaSDarrick J. Wong 	first = from >> inode->i_blkbits;
5292756c818SMatthew Wilcox (Oracle) 	last = (from + count - 1) >> inode->i_blkbits;
530afc51aaaSDarrick J. Wong 
531afc51aaaSDarrick J. Wong 	for (i = first; i <= last; i++)
532cc86181aSRitesh Harjani (IBM) 		if (!ifs_block_is_uptodate(ifs, i))
5332e7e80f7SMatthew Wilcox (Oracle) 			return false;
5342e7e80f7SMatthew Wilcox (Oracle) 	return true;
535afc51aaaSDarrick J. Wong }
536afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
537afc51aaaSDarrick J. Wong 
53898321b51SAndreas Gruenbacher /**
53998321b51SAndreas Gruenbacher  * iomap_get_folio - get a folio reference for writing
54098321b51SAndreas Gruenbacher  * @iter: iteration structure
54198321b51SAndreas Gruenbacher  * @pos: start offset of write
542d6bb59a9SMatthew Wilcox (Oracle)  * @len: Suggested size of folio to create.
54398321b51SAndreas Gruenbacher  *
54498321b51SAndreas Gruenbacher  * Returns a locked reference to the folio at @pos, or an error pointer if the
54598321b51SAndreas Gruenbacher  * folio could not be obtained.
54698321b51SAndreas Gruenbacher  */
iomap_get_folio(struct iomap_iter * iter,loff_t pos,size_t len)547d6bb59a9SMatthew Wilcox (Oracle) struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len)
54898321b51SAndreas Gruenbacher {
549ffc143dbSMatthew Wilcox (Oracle) 	fgf_t fgp = FGP_WRITEBEGIN | FGP_NOFS;
55098321b51SAndreas Gruenbacher 
55198321b51SAndreas Gruenbacher 	if (iter->flags & IOMAP_NOWAIT)
55298321b51SAndreas Gruenbacher 		fgp |= FGP_NOWAIT;
553d6bb59a9SMatthew Wilcox (Oracle) 	fgp |= fgf_set_order(len);
55498321b51SAndreas Gruenbacher 
55566dabbb6SChristoph Hellwig 	return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
55698321b51SAndreas Gruenbacher 			fgp, mapping_gfp_mask(iter->inode->i_mapping));
55798321b51SAndreas Gruenbacher }
55898321b51SAndreas Gruenbacher EXPORT_SYMBOL_GPL(iomap_get_folio);
55998321b51SAndreas Gruenbacher 
iomap_release_folio(struct folio * folio,gfp_t gfp_flags)5608597447dSMatthew Wilcox (Oracle) bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags)
561afc51aaaSDarrick J. Wong {
5628597447dSMatthew Wilcox (Oracle) 	trace_iomap_release_folio(folio->mapping->host, folio_pos(folio),
56339f16c83SMatthew Wilcox (Oracle) 			folio_size(folio));
5649e91c572SChristoph Hellwig 
565afc51aaaSDarrick J. Wong 	/*
5667a8eb01bSMatthew Wilcox (Oracle) 	 * If the folio is dirty, we refuse to release our metadata because
5677a8eb01bSMatthew Wilcox (Oracle) 	 * it may be partially dirty.  Once we track per-block dirty state,
5687a8eb01bSMatthew Wilcox (Oracle) 	 * we can release the metadata if every block is dirty.
569afc51aaaSDarrick J. Wong 	 */
5707a8eb01bSMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio))
5718597447dSMatthew Wilcox (Oracle) 		return false;
57204f52c4eSRitesh Harjani (IBM) 	ifs_free(folio);
5738597447dSMatthew Wilcox (Oracle) 	return true;
574afc51aaaSDarrick J. Wong }
5758597447dSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_release_folio);
576afc51aaaSDarrick J. Wong 
iomap_invalidate_folio(struct folio * folio,size_t offset,size_t len)5778306a5f5SMatthew Wilcox (Oracle) void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
578afc51aaaSDarrick J. Wong {
579d82354f6SMatthew Wilcox (Oracle) 	trace_iomap_invalidate_folio(folio->mapping->host,
5801241ebecSMatthew Wilcox (Oracle) 					folio_pos(folio) + offset, len);
5819e91c572SChristoph Hellwig 
582afc51aaaSDarrick J. Wong 	/*
58360d82310SMatthew Wilcox (Oracle) 	 * If we're invalidating the entire folio, clear the dirty state
58460d82310SMatthew Wilcox (Oracle) 	 * from it and release it to avoid unnecessary buildup of the LRU.
585afc51aaaSDarrick J. Wong 	 */
5868306a5f5SMatthew Wilcox (Oracle) 	if (offset == 0 && len == folio_size(folio)) {
5878306a5f5SMatthew Wilcox (Oracle) 		WARN_ON_ONCE(folio_test_writeback(folio));
5888306a5f5SMatthew Wilcox (Oracle) 		folio_cancel_dirty(folio);
58904f52c4eSRitesh Harjani (IBM) 		ifs_free(folio);
590afc51aaaSDarrick J. Wong 	}
591afc51aaaSDarrick J. Wong }
5928306a5f5SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
5938306a5f5SMatthew Wilcox (Oracle) 
iomap_dirty_folio(struct address_space * mapping,struct folio * folio)5944ce02c67SRitesh Harjani (IBM) bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
5954ce02c67SRitesh Harjani (IBM) {
5964ce02c67SRitesh Harjani (IBM) 	struct inode *inode = mapping->host;
5974ce02c67SRitesh Harjani (IBM) 	size_t len = folio_size(folio);
5984ce02c67SRitesh Harjani (IBM) 
5994ce02c67SRitesh Harjani (IBM) 	ifs_alloc(inode, folio, 0);
6004ce02c67SRitesh Harjani (IBM) 	iomap_set_range_dirty(folio, 0, len);
6014ce02c67SRitesh Harjani (IBM) 	return filemap_dirty_folio(mapping, folio);
6024ce02c67SRitesh Harjani (IBM) }
6034ce02c67SRitesh Harjani (IBM) EXPORT_SYMBOL_GPL(iomap_dirty_folio);
6044ce02c67SRitesh Harjani (IBM) 
605afc51aaaSDarrick J. Wong static void
iomap_write_failed(struct inode * inode,loff_t pos,unsigned len)606afc51aaaSDarrick J. Wong iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
607afc51aaaSDarrick J. Wong {
608afc51aaaSDarrick J. Wong 	loff_t i_size = i_size_read(inode);
609afc51aaaSDarrick J. Wong 
610afc51aaaSDarrick J. Wong 	/*
611afc51aaaSDarrick J. Wong 	 * Only truncate newly allocated pages beyoned EOF, even if the
612afc51aaaSDarrick J. Wong 	 * write started inside the existing inode size.
613afc51aaaSDarrick J. Wong 	 */
614afc51aaaSDarrick J. Wong 	if (pos + len > i_size)
615b71450e2SAndreas Gruenbacher 		truncate_pagecache_range(inode, max(pos, i_size),
616b71450e2SAndreas Gruenbacher 					 pos + len - 1);
617afc51aaaSDarrick J. Wong }
618afc51aaaSDarrick J. Wong 
iomap_read_folio_sync(loff_t block_start,struct folio * folio,size_t poff,size_t plen,const struct iomap * iomap)619431c0566SMatthew Wilcox (Oracle) static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
620431c0566SMatthew Wilcox (Oracle) 		size_t poff, size_t plen, const struct iomap *iomap)
621afc51aaaSDarrick J. Wong {
622afc51aaaSDarrick J. Wong 	struct bio_vec bvec;
623afc51aaaSDarrick J. Wong 	struct bio bio;
624afc51aaaSDarrick J. Wong 
62549add496SChristoph Hellwig 	bio_init(&bio, iomap->bdev, &bvec, 1, REQ_OP_READ);
626afc51aaaSDarrick J. Wong 	bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
627c2478469SJohannes Thumshirn 	bio_add_folio_nofail(&bio, folio, plen, poff);
628afc51aaaSDarrick J. Wong 	return submit_bio_wait(&bio);
629afc51aaaSDarrick J. Wong }
630afc51aaaSDarrick J. Wong 
__iomap_write_begin(const struct iomap_iter * iter,loff_t pos,size_t len,struct folio * folio)631fad0a1abSChristoph Hellwig static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
632bc6123a8SMatthew Wilcox (Oracle) 		size_t len, struct folio *folio)
633afc51aaaSDarrick J. Wong {
634fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
63504f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs;
6361b5c1e36SChristoph Hellwig 	loff_t block_size = i_blocksize(iter->inode);
6376cc19c5fSNikolay Borisov 	loff_t block_start = round_down(pos, block_size);
6386cc19c5fSNikolay Borisov 	loff_t block_end = round_up(pos + len, block_size);
639cae2de69SStefan Roesch 	unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio);
640431c0566SMatthew Wilcox (Oracle) 	size_t from = offset_in_folio(folio, pos), to = from + len;
641431c0566SMatthew Wilcox (Oracle) 	size_t poff, plen;
642afc51aaaSDarrick J. Wong 
643a01b8f22SRitesh Harjani (IBM) 	/*
64435d30c9cSDarrick J. Wong 	 * If the write or zeroing completely overlaps the current folio, then
645a01b8f22SRitesh Harjani (IBM) 	 * entire folio will be dirtied so there is no need for
646a01b8f22SRitesh Harjani (IBM) 	 * per-block state tracking structures to be attached to this folio.
64735d30c9cSDarrick J. Wong 	 * For the unshare case, we must read in the ondisk contents because we
64835d30c9cSDarrick J. Wong 	 * are not changing pagecache contents.
649a01b8f22SRitesh Harjani (IBM) 	 */
65035d30c9cSDarrick J. Wong 	if (!(iter->flags & IOMAP_UNSHARE) && pos <= folio_pos(folio) &&
651a01b8f22SRitesh Harjani (IBM) 	    pos + len >= folio_pos(folio) + folio_size(folio))
652afc51aaaSDarrick J. Wong 		return 0;
653afc51aaaSDarrick J. Wong 
65404f52c4eSRitesh Harjani (IBM) 	ifs = ifs_alloc(iter->inode, folio, iter->flags);
65504f52c4eSRitesh Harjani (IBM) 	if ((iter->flags & IOMAP_NOWAIT) && !ifs && nr_blocks > 1)
656cae2de69SStefan Roesch 		return -EAGAIN;
6579753b868SStefan Roesch 
658a01b8f22SRitesh Harjani (IBM) 	if (folio_test_uptodate(folio))
659a01b8f22SRitesh Harjani (IBM) 		return 0;
660a01b8f22SRitesh Harjani (IBM) 	folio_clear_error(folio);
661a01b8f22SRitesh Harjani (IBM) 
662afc51aaaSDarrick J. Wong 	do {
663431c0566SMatthew Wilcox (Oracle) 		iomap_adjust_read_range(iter->inode, folio, &block_start,
664afc51aaaSDarrick J. Wong 				block_end - block_start, &poff, &plen);
665afc51aaaSDarrick J. Wong 		if (plen == 0)
666afc51aaaSDarrick J. Wong 			break;
667afc51aaaSDarrick J. Wong 
668b74b1293SChristoph Hellwig 		if (!(iter->flags & IOMAP_UNSHARE) &&
66932a38a49SChristoph Hellwig 		    (from <= poff || from >= poff + plen) &&
670d3b40439SChristoph Hellwig 		    (to <= poff || to >= poff + plen))
671d3b40439SChristoph Hellwig 			continue;
672d3b40439SChristoph Hellwig 
6731b5c1e36SChristoph Hellwig 		if (iomap_block_needs_zeroing(iter, block_start)) {
674b74b1293SChristoph Hellwig 			if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
67532a38a49SChristoph Hellwig 				return -EIO;
676431c0566SMatthew Wilcox (Oracle) 			folio_zero_segments(folio, poff, from, to, poff + plen);
67714284fedSMatthew Wilcox (Oracle) 		} else {
678cae2de69SStefan Roesch 			int status;
679cae2de69SStefan Roesch 
680cae2de69SStefan Roesch 			if (iter->flags & IOMAP_NOWAIT)
681cae2de69SStefan Roesch 				return -EAGAIN;
682cae2de69SStefan Roesch 
683cae2de69SStefan Roesch 			status = iomap_read_folio_sync(block_start, folio,
68414284fedSMatthew Wilcox (Oracle) 					poff, plen, srcmap);
685d3b40439SChristoph Hellwig 			if (status)
686d3b40439SChristoph Hellwig 				return status;
68714284fedSMatthew Wilcox (Oracle) 		}
6883ea5c76cSRitesh Harjani (IBM) 		iomap_set_range_uptodate(folio, poff, plen);
689afc51aaaSDarrick J. Wong 	} while ((block_start += plen) < block_end);
690afc51aaaSDarrick J. Wong 
691d3b40439SChristoph Hellwig 	return 0;
692afc51aaaSDarrick J. Wong }
693afc51aaaSDarrick J. Wong 
__iomap_get_folio(struct iomap_iter * iter,loff_t pos,size_t len)69407c22b56SAndreas Gruenbacher static struct folio *__iomap_get_folio(struct iomap_iter *iter, loff_t pos,
69507c22b56SAndreas Gruenbacher 		size_t len)
69607c22b56SAndreas Gruenbacher {
697471859f5SAndreas Gruenbacher 	const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
69807c22b56SAndreas Gruenbacher 
699471859f5SAndreas Gruenbacher 	if (folio_ops && folio_ops->get_folio)
700471859f5SAndreas Gruenbacher 		return folio_ops->get_folio(iter, pos, len);
70107c22b56SAndreas Gruenbacher 	else
702d6bb59a9SMatthew Wilcox (Oracle) 		return iomap_get_folio(iter, pos, len);
70307c22b56SAndreas Gruenbacher }
70407c22b56SAndreas Gruenbacher 
__iomap_put_folio(struct iomap_iter * iter,loff_t pos,size_t ret,struct folio * folio)7057a70a508SAndreas Gruenbacher static void __iomap_put_folio(struct iomap_iter *iter, loff_t pos, size_t ret,
7067a70a508SAndreas Gruenbacher 		struct folio *folio)
7077a70a508SAndreas Gruenbacher {
708471859f5SAndreas Gruenbacher 	const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
7097a70a508SAndreas Gruenbacher 
710471859f5SAndreas Gruenbacher 	if (folio_ops && folio_ops->put_folio) {
711471859f5SAndreas Gruenbacher 		folio_ops->put_folio(iter->inode, pos, ret, folio);
7129060bc4dSAndreas Gruenbacher 	} else {
7137a70a508SAndreas Gruenbacher 		folio_unlock(folio);
7147a70a508SAndreas Gruenbacher 		folio_put(folio);
7157a70a508SAndreas Gruenbacher 	}
71680baab88SAndreas Gruenbacher }
7177a70a508SAndreas Gruenbacher 
iomap_write_begin_inline(const struct iomap_iter * iter,struct folio * folio)718fad0a1abSChristoph Hellwig static int iomap_write_begin_inline(const struct iomap_iter *iter,
719bc6123a8SMatthew Wilcox (Oracle) 		struct folio *folio)
72069f4a26cSGao Xiang {
72169f4a26cSGao Xiang 	/* needs more work for the tailpacking case; disable for now */
7221b5c1e36SChristoph Hellwig 	if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
72369f4a26cSGao Xiang 		return -EIO;
724874628a2SMatthew Wilcox (Oracle) 	return iomap_read_inline_data(iter, folio);
72569f4a26cSGao Xiang }
72669f4a26cSGao Xiang 
iomap_write_begin(struct iomap_iter * iter,loff_t pos,size_t len,struct folio ** foliop)727d7b64041SDave Chinner static int iomap_write_begin(struct iomap_iter *iter, loff_t pos,
728bc6123a8SMatthew Wilcox (Oracle) 		size_t len, struct folio **foliop)
729afc51aaaSDarrick J. Wong {
730471859f5SAndreas Gruenbacher 	const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
731fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
732d1bd0b4eSMatthew Wilcox (Oracle) 	struct folio *folio;
733afc51aaaSDarrick J. Wong 	int status = 0;
734afc51aaaSDarrick J. Wong 
7351b5c1e36SChristoph Hellwig 	BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
7361b5c1e36SChristoph Hellwig 	if (srcmap != &iter->iomap)
737c039b997SGoldwyn Rodrigues 		BUG_ON(pos + len > srcmap->offset + srcmap->length);
738afc51aaaSDarrick J. Wong 
739afc51aaaSDarrick J. Wong 	if (fatal_signal_pending(current))
740afc51aaaSDarrick J. Wong 		return -EINTR;
741afc51aaaSDarrick J. Wong 
742d454ab82SMatthew Wilcox (Oracle) 	if (!mapping_large_folio_support(iter->inode->i_mapping))
743d454ab82SMatthew Wilcox (Oracle) 		len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
744d454ab82SMatthew Wilcox (Oracle) 
74507c22b56SAndreas Gruenbacher 	folio = __iomap_get_folio(iter, pos, len);
7469060bc4dSAndreas Gruenbacher 	if (IS_ERR(folio))
74798321b51SAndreas Gruenbacher 		return PTR_ERR(folio);
748d7b64041SDave Chinner 
749d7b64041SDave Chinner 	/*
750d7b64041SDave Chinner 	 * Now we have a locked folio, before we do anything with it we need to
751d7b64041SDave Chinner 	 * check that the iomap we have cached is not stale. The inode extent
752d7b64041SDave Chinner 	 * mapping can change due to concurrent IO in flight (e.g.
753d7b64041SDave Chinner 	 * IOMAP_UNWRITTEN state can change and memory reclaim could have
754d7b64041SDave Chinner 	 * reclaimed a previously partially written page at this index after IO
755d7b64041SDave Chinner 	 * completion before this write reaches this file offset) and hence we
756d7b64041SDave Chinner 	 * could do the wrong thing here (zero a page range incorrectly or fail
757d7b64041SDave Chinner 	 * to zero) and corrupt data.
758d7b64041SDave Chinner 	 */
759471859f5SAndreas Gruenbacher 	if (folio_ops && folio_ops->iomap_valid) {
760471859f5SAndreas Gruenbacher 		bool iomap_valid = folio_ops->iomap_valid(iter->inode,
761d7b64041SDave Chinner 							 &iter->iomap);
762d7b64041SDave Chinner 		if (!iomap_valid) {
763d7b64041SDave Chinner 			iter->iomap.flags |= IOMAP_F_STALE;
764d7b64041SDave Chinner 			status = 0;
765d7b64041SDave Chinner 			goto out_unlock;
766d7b64041SDave Chinner 		}
767d7b64041SDave Chinner 	}
768d7b64041SDave Chinner 
769d454ab82SMatthew Wilcox (Oracle) 	if (pos + len > folio_pos(folio) + folio_size(folio))
770d454ab82SMatthew Wilcox (Oracle) 		len = folio_pos(folio) + folio_size(folio) - pos;
771afc51aaaSDarrick J. Wong 
772c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_INLINE)
773bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_begin_inline(iter, folio);
7741b5c1e36SChristoph Hellwig 	else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
775d1bd0b4eSMatthew Wilcox (Oracle) 		status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
776afc51aaaSDarrick J. Wong 	else
777bc6123a8SMatthew Wilcox (Oracle) 		status = __iomap_write_begin(iter, pos, len, folio);
778afc51aaaSDarrick J. Wong 
779afc51aaaSDarrick J. Wong 	if (unlikely(status))
780afc51aaaSDarrick J. Wong 		goto out_unlock;
781afc51aaaSDarrick J. Wong 
782bc6123a8SMatthew Wilcox (Oracle) 	*foliop = folio;
783afc51aaaSDarrick J. Wong 	return 0;
784afc51aaaSDarrick J. Wong 
785afc51aaaSDarrick J. Wong out_unlock:
7867a70a508SAndreas Gruenbacher 	__iomap_put_folio(iter, pos, 0, folio);
7871b5c1e36SChristoph Hellwig 	iomap_write_failed(iter->inode, pos, len);
788afc51aaaSDarrick J. Wong 
789afc51aaaSDarrick J. Wong 	return status;
790afc51aaaSDarrick J. Wong }
791afc51aaaSDarrick J. Wong 
__iomap_write_end(struct inode * inode,loff_t pos,size_t len,size_t copied,struct folio * folio)792e25ba8cbSMatthew Wilcox (Oracle) static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
793bc6123a8SMatthew Wilcox (Oracle) 		size_t copied, struct folio *folio)
794afc51aaaSDarrick J. Wong {
795bc6123a8SMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
796afc51aaaSDarrick J. Wong 
797afc51aaaSDarrick J. Wong 	/*
798afc51aaaSDarrick J. Wong 	 * The blocks that were entirely written will now be uptodate, so we
7997479c505SMatthew Wilcox (Oracle) 	 * don't have to worry about a read_folio reading them and overwriting a
800f1f264b4SAndreas Gruenbacher 	 * partial write.  However, if we've encountered a short write and only
801afc51aaaSDarrick J. Wong 	 * partially written into a block, it will not be marked uptodate, so a
8027479c505SMatthew Wilcox (Oracle) 	 * read_folio might come in and destroy our partial write.
803afc51aaaSDarrick J. Wong 	 *
804f1f264b4SAndreas Gruenbacher 	 * Do the simplest thing and just treat any short write to a
805f1f264b4SAndreas Gruenbacher 	 * non-uptodate page as a zero-length write, and force the caller to
806f1f264b4SAndreas Gruenbacher 	 * redo the whole thing.
807afc51aaaSDarrick J. Wong 	 */
808bc6123a8SMatthew Wilcox (Oracle) 	if (unlikely(copied < len && !folio_test_uptodate(folio)))
809afc51aaaSDarrick J. Wong 		return 0;
8103ea5c76cSRitesh Harjani (IBM) 	iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
8114ce02c67SRitesh Harjani (IBM) 	iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
812bc6123a8SMatthew Wilcox (Oracle) 	filemap_dirty_folio(inode->i_mapping, folio);
813afc51aaaSDarrick J. Wong 	return copied;
814afc51aaaSDarrick J. Wong }
815afc51aaaSDarrick J. Wong 
iomap_write_end_inline(const struct iomap_iter * iter,struct folio * folio,loff_t pos,size_t copied)816fad0a1abSChristoph Hellwig static size_t iomap_write_end_inline(const struct iomap_iter *iter,
8179c4ce08dSMatthew Wilcox (Oracle) 		struct folio *folio, loff_t pos, size_t copied)
818afc51aaaSDarrick J. Wong {
819fad0a1abSChristoph Hellwig 	const struct iomap *iomap = &iter->iomap;
820afc51aaaSDarrick J. Wong 	void *addr;
821afc51aaaSDarrick J. Wong 
8229c4ce08dSMatthew Wilcox (Oracle) 	WARN_ON_ONCE(!folio_test_uptodate(folio));
82369f4a26cSGao Xiang 	BUG_ON(!iomap_inline_data_valid(iomap));
824afc51aaaSDarrick J. Wong 
8259c4ce08dSMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
8269c4ce08dSMatthew Wilcox (Oracle) 	addr = kmap_local_folio(folio, pos);
827ab069d5fSMatthew Wilcox (Oracle) 	memcpy(iomap_inline_data(iomap, pos), addr, copied);
828ab069d5fSMatthew Wilcox (Oracle) 	kunmap_local(addr);
829afc51aaaSDarrick J. Wong 
8301b5c1e36SChristoph Hellwig 	mark_inode_dirty(iter->inode);
831afc51aaaSDarrick J. Wong 	return copied;
832afc51aaaSDarrick J. Wong }
833afc51aaaSDarrick J. Wong 
834e25ba8cbSMatthew Wilcox (Oracle) /* Returns the number of bytes copied.  May be 0.  Cannot be an errno. */
iomap_write_end(struct iomap_iter * iter,loff_t pos,size_t len,size_t copied,struct folio * folio)8351b5c1e36SChristoph Hellwig static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len,
836bc6123a8SMatthew Wilcox (Oracle) 		size_t copied, struct folio *folio)
837afc51aaaSDarrick J. Wong {
838fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
8391b5c1e36SChristoph Hellwig 	loff_t old_size = iter->inode->i_size;
840e25ba8cbSMatthew Wilcox (Oracle) 	size_t ret;
841afc51aaaSDarrick J. Wong 
842c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_INLINE) {
8439c4ce08dSMatthew Wilcox (Oracle) 		ret = iomap_write_end_inline(iter, folio, pos, copied);
844c039b997SGoldwyn Rodrigues 	} else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
8451b5c1e36SChristoph Hellwig 		ret = block_write_end(NULL, iter->inode->i_mapping, pos, len,
846bc6123a8SMatthew Wilcox (Oracle) 				copied, &folio->page, NULL);
847afc51aaaSDarrick J. Wong 	} else {
848bc6123a8SMatthew Wilcox (Oracle) 		ret = __iomap_write_end(iter->inode, pos, len, copied, folio);
849afc51aaaSDarrick J. Wong 	}
850afc51aaaSDarrick J. Wong 
851afc51aaaSDarrick J. Wong 	/*
852afc51aaaSDarrick J. Wong 	 * Update the in-memory inode size after copying the data into the page
853afc51aaaSDarrick J. Wong 	 * cache.  It's up to the file system to write the updated size to disk,
854afc51aaaSDarrick J. Wong 	 * preferably after I/O completion so that no stale data is exposed.
855afc51aaaSDarrick J. Wong 	 */
856afc51aaaSDarrick J. Wong 	if (pos + ret > old_size) {
8571b5c1e36SChristoph Hellwig 		i_size_write(iter->inode, pos + ret);
8581b5c1e36SChristoph Hellwig 		iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
859afc51aaaSDarrick J. Wong 	}
8607a70a508SAndreas Gruenbacher 	__iomap_put_folio(iter, pos, ret, folio);
861afc51aaaSDarrick J. Wong 
862afc51aaaSDarrick J. Wong 	if (old_size < pos)
8631b5c1e36SChristoph Hellwig 		pagecache_isize_extended(iter->inode, old_size, pos);
864afc51aaaSDarrick J. Wong 	if (ret < len)
865d74999c8SAndreas Gruenbacher 		iomap_write_failed(iter->inode, pos + ret, len - ret);
866afc51aaaSDarrick J. Wong 	return ret;
867afc51aaaSDarrick J. Wong }
868afc51aaaSDarrick J. Wong 
iomap_write_iter(struct iomap_iter * iter,struct iov_iter * i)869ce83a025SChristoph Hellwig static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
870afc51aaaSDarrick J. Wong {
871ce83a025SChristoph Hellwig 	loff_t length = iomap_length(iter);
872ce83a025SChristoph Hellwig 	loff_t pos = iter->pos;
873afc51aaaSDarrick J. Wong 	ssize_t written = 0;
874ce83a025SChristoph Hellwig 	long status = 0;
875cae2de69SStefan Roesch 	struct address_space *mapping = iter->inode->i_mapping;
8769ee7a77cSXu Yang 	size_t chunk = mapping_max_folio_size(mapping);
877cae2de69SStefan Roesch 	unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0;
878afc51aaaSDarrick J. Wong 
879afc51aaaSDarrick J. Wong 	do {
880bc6123a8SMatthew Wilcox (Oracle) 		struct folio *folio;
8815d8edfb9SMatthew Wilcox (Oracle) 		size_t offset;		/* Offset into folio */
8825d8edfb9SMatthew Wilcox (Oracle) 		size_t bytes;		/* Bytes to write to folio */
883afc51aaaSDarrick J. Wong 		size_t copied;		/* Bytes copied from user */
884afc51aaaSDarrick J. Wong 
8853ac97479SJan Stancek 		bytes = iov_iter_count(i);
8863ac97479SJan Stancek retry:
8875d8edfb9SMatthew Wilcox (Oracle) 		offset = pos & (chunk - 1);
8883ac97479SJan Stancek 		bytes = min(chunk - offset, bytes);
889cae2de69SStefan Roesch 		status = balance_dirty_pages_ratelimited_flags(mapping,
890cae2de69SStefan Roesch 							       bdp_flags);
891cae2de69SStefan Roesch 		if (unlikely(status))
892cae2de69SStefan Roesch 			break;
893cae2de69SStefan Roesch 
894afc51aaaSDarrick J. Wong 		if (bytes > length)
895afc51aaaSDarrick J. Wong 			bytes = length;
896afc51aaaSDarrick J. Wong 
897afc51aaaSDarrick J. Wong 		/*
898f1f264b4SAndreas Gruenbacher 		 * Bring in the user page that we'll copy from _first_.
899afc51aaaSDarrick J. Wong 		 * Otherwise there's a nasty deadlock on copying from the
900afc51aaaSDarrick J. Wong 		 * same page as we're writing to, without it being marked
901afc51aaaSDarrick J. Wong 		 * up-to-date.
902cae2de69SStefan Roesch 		 *
903cae2de69SStefan Roesch 		 * For async buffered writes the assumption is that the user
904cae2de69SStefan Roesch 		 * page has already been faulted in. This can be optimized by
905cae2de69SStefan Roesch 		 * faulting the user page.
906afc51aaaSDarrick J. Wong 		 */
907631f871fSAndreas Gruenbacher 		if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
908afc51aaaSDarrick J. Wong 			status = -EFAULT;
909afc51aaaSDarrick J. Wong 			break;
910afc51aaaSDarrick J. Wong 		}
911afc51aaaSDarrick J. Wong 
912bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_begin(iter, pos, bytes, &folio);
913afc51aaaSDarrick J. Wong 		if (unlikely(status))
914afc51aaaSDarrick J. Wong 			break;
915d7b64041SDave Chinner 		if (iter->iomap.flags & IOMAP_F_STALE)
916d7b64041SDave Chinner 			break;
917afc51aaaSDarrick J. Wong 
9185d8edfb9SMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, pos);
9195d8edfb9SMatthew Wilcox (Oracle) 		if (bytes > folio_size(folio) - offset)
9205d8edfb9SMatthew Wilcox (Oracle) 			bytes = folio_size(folio) - offset;
9215d8edfb9SMatthew Wilcox (Oracle) 
922cae2de69SStefan Roesch 		if (mapping_writably_mapped(mapping))
9235d8edfb9SMatthew Wilcox (Oracle) 			flush_dcache_folio(folio);
924afc51aaaSDarrick J. Wong 
9255d8edfb9SMatthew Wilcox (Oracle) 		copied = copy_folio_from_iter_atomic(folio, offset, bytes, i);
926bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_end(iter, pos, bytes, copied, folio);
927afc51aaaSDarrick J. Wong 
928f0b65f39SAl Viro 		if (unlikely(copied != status))
929f0b65f39SAl Viro 			iov_iter_revert(i, copied - status);
930afc51aaaSDarrick J. Wong 
931f0b65f39SAl Viro 		cond_resched();
932bc1bb416SAl Viro 		if (unlikely(status == 0)) {
933afc51aaaSDarrick J. Wong 			/*
934bc1bb416SAl Viro 			 * A short copy made iomap_write_end() reject the
935bc1bb416SAl Viro 			 * thing entirely.  Might be memory poisoning
936bc1bb416SAl Viro 			 * halfway through, might be a race with munmap,
937bc1bb416SAl Viro 			 * might be severe memory pressure.
938afc51aaaSDarrick J. Wong 			 */
9395d8edfb9SMatthew Wilcox (Oracle) 			if (chunk > PAGE_SIZE)
9405d8edfb9SMatthew Wilcox (Oracle) 				chunk /= 2;
9413ac97479SJan Stancek 			if (copied) {
9423ac97479SJan Stancek 				bytes = copied;
9433ac97479SJan Stancek 				goto retry;
9443ac97479SJan Stancek 			}
9455d8edfb9SMatthew Wilcox (Oracle) 		} else {
946f0b65f39SAl Viro 			pos += status;
947f0b65f39SAl Viro 			written += status;
948f0b65f39SAl Viro 			length -= status;
9495d8edfb9SMatthew Wilcox (Oracle) 		}
950afc51aaaSDarrick J. Wong 	} while (iov_iter_count(i) && length);
951afc51aaaSDarrick J. Wong 
95218e419f6SStefan Roesch 	if (status == -EAGAIN) {
95318e419f6SStefan Roesch 		iov_iter_revert(i, written);
95418e419f6SStefan Roesch 		return -EAGAIN;
95518e419f6SStefan Roesch 	}
956afc51aaaSDarrick J. Wong 	return written ? written : status;
957afc51aaaSDarrick J. Wong }
958afc51aaaSDarrick J. Wong 
959afc51aaaSDarrick J. Wong ssize_t
iomap_file_buffered_write(struct kiocb * iocb,struct iov_iter * i,const struct iomap_ops * ops)960ce83a025SChristoph Hellwig iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
961afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
962afc51aaaSDarrick J. Wong {
963ce83a025SChristoph Hellwig 	struct iomap_iter iter = {
964ce83a025SChristoph Hellwig 		.inode		= iocb->ki_filp->f_mapping->host,
965ce83a025SChristoph Hellwig 		.pos		= iocb->ki_pos,
966ce83a025SChristoph Hellwig 		.len		= iov_iter_count(i),
967ce83a025SChristoph Hellwig 		.flags		= IOMAP_WRITE,
968ce83a025SChristoph Hellwig 	};
969219580eeSChristoph Hellwig 	ssize_t ret;
970afc51aaaSDarrick J. Wong 
971cae2de69SStefan Roesch 	if (iocb->ki_flags & IOCB_NOWAIT)
972cae2de69SStefan Roesch 		iter.flags |= IOMAP_NOWAIT;
973cae2de69SStefan Roesch 
974ce83a025SChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
975ce83a025SChristoph Hellwig 		iter.processed = iomap_write_iter(&iter, i);
976219580eeSChristoph Hellwig 
97720c64ec8SChristoph Hellwig 	if (unlikely(iter.pos == iocb->ki_pos))
978ce83a025SChristoph Hellwig 		return ret;
979219580eeSChristoph Hellwig 	ret = iter.pos - iocb->ki_pos;
980efa96cc9SChristoph Hellwig 	iocb->ki_pos = iter.pos;
981219580eeSChristoph Hellwig 	return ret;
982afc51aaaSDarrick J. Wong }
983afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
984afc51aaaSDarrick J. Wong 
iomap_write_delalloc_ifs_punch(struct inode * inode,struct folio * folio,loff_t start_byte,loff_t end_byte,iomap_punch_t punch)9854ce02c67SRitesh Harjani (IBM) static int iomap_write_delalloc_ifs_punch(struct inode *inode,
9864ce02c67SRitesh Harjani (IBM) 		struct folio *folio, loff_t start_byte, loff_t end_byte,
9874ce02c67SRitesh Harjani (IBM) 		iomap_punch_t punch)
9884ce02c67SRitesh Harjani (IBM) {
9894ce02c67SRitesh Harjani (IBM) 	unsigned int first_blk, last_blk, i;
9904ce02c67SRitesh Harjani (IBM) 	loff_t last_byte;
9914ce02c67SRitesh Harjani (IBM) 	u8 blkbits = inode->i_blkbits;
9924ce02c67SRitesh Harjani (IBM) 	struct iomap_folio_state *ifs;
9934ce02c67SRitesh Harjani (IBM) 	int ret = 0;
9944ce02c67SRitesh Harjani (IBM) 
9954ce02c67SRitesh Harjani (IBM) 	/*
9964ce02c67SRitesh Harjani (IBM) 	 * When we have per-block dirty tracking, there can be
9974ce02c67SRitesh Harjani (IBM) 	 * blocks within a folio which are marked uptodate
9984ce02c67SRitesh Harjani (IBM) 	 * but not dirty. In that case it is necessary to punch
9994ce02c67SRitesh Harjani (IBM) 	 * out such blocks to avoid leaking any delalloc blocks.
10004ce02c67SRitesh Harjani (IBM) 	 */
10014ce02c67SRitesh Harjani (IBM) 	ifs = folio->private;
10024ce02c67SRitesh Harjani (IBM) 	if (!ifs)
10034ce02c67SRitesh Harjani (IBM) 		return ret;
10044ce02c67SRitesh Harjani (IBM) 
10054ce02c67SRitesh Harjani (IBM) 	last_byte = min_t(loff_t, end_byte - 1,
10064ce02c67SRitesh Harjani (IBM) 			folio_pos(folio) + folio_size(folio) - 1);
10074ce02c67SRitesh Harjani (IBM) 	first_blk = offset_in_folio(folio, start_byte) >> blkbits;
10084ce02c67SRitesh Harjani (IBM) 	last_blk = offset_in_folio(folio, last_byte) >> blkbits;
10094ce02c67SRitesh Harjani (IBM) 	for (i = first_blk; i <= last_blk; i++) {
10104ce02c67SRitesh Harjani (IBM) 		if (!ifs_block_is_dirty(folio, ifs, i)) {
10114ce02c67SRitesh Harjani (IBM) 			ret = punch(inode, folio_pos(folio) + (i << blkbits),
10124ce02c67SRitesh Harjani (IBM) 				    1 << blkbits);
10134ce02c67SRitesh Harjani (IBM) 			if (ret)
10144ce02c67SRitesh Harjani (IBM) 				return ret;
10154ce02c67SRitesh Harjani (IBM) 		}
10164ce02c67SRitesh Harjani (IBM) 	}
10174ce02c67SRitesh Harjani (IBM) 
10184ce02c67SRitesh Harjani (IBM) 	return ret;
10194ce02c67SRitesh Harjani (IBM) }
10204ce02c67SRitesh Harjani (IBM) 
10214ce02c67SRitesh Harjani (IBM) 
iomap_write_delalloc_punch(struct inode * inode,struct folio * folio,loff_t * punch_start_byte,loff_t start_byte,loff_t end_byte,iomap_punch_t punch)10227f79d85bSRitesh Harjani (IBM) static int iomap_write_delalloc_punch(struct inode *inode, struct folio *folio,
10237f79d85bSRitesh Harjani (IBM) 		loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
10247f79d85bSRitesh Harjani (IBM) 		iomap_punch_t punch)
10257f79d85bSRitesh Harjani (IBM) {
10267f79d85bSRitesh Harjani (IBM) 	int ret = 0;
10277f79d85bSRitesh Harjani (IBM) 
10287f79d85bSRitesh Harjani (IBM) 	if (!folio_test_dirty(folio))
10297f79d85bSRitesh Harjani (IBM) 		return ret;
10307f79d85bSRitesh Harjani (IBM) 
10317f79d85bSRitesh Harjani (IBM) 	/* if dirty, punch up to offset */
10327f79d85bSRitesh Harjani (IBM) 	if (start_byte > *punch_start_byte) {
10337f79d85bSRitesh Harjani (IBM) 		ret = punch(inode, *punch_start_byte,
10347f79d85bSRitesh Harjani (IBM) 				start_byte - *punch_start_byte);
10357f79d85bSRitesh Harjani (IBM) 		if (ret)
10367f79d85bSRitesh Harjani (IBM) 			return ret;
10377f79d85bSRitesh Harjani (IBM) 	}
10387f79d85bSRitesh Harjani (IBM) 
10394ce02c67SRitesh Harjani (IBM) 	/* Punch non-dirty blocks within folio */
10404ce02c67SRitesh Harjani (IBM) 	ret = iomap_write_delalloc_ifs_punch(inode, folio, start_byte,
10414ce02c67SRitesh Harjani (IBM) 			end_byte, punch);
10424ce02c67SRitesh Harjani (IBM) 	if (ret)
10434ce02c67SRitesh Harjani (IBM) 		return ret;
10444ce02c67SRitesh Harjani (IBM) 
10457f79d85bSRitesh Harjani (IBM) 	/*
10467f79d85bSRitesh Harjani (IBM) 	 * Make sure the next punch start is correctly bound to
10477f79d85bSRitesh Harjani (IBM) 	 * the end of this data range, not the end of the folio.
10487f79d85bSRitesh Harjani (IBM) 	 */
10497f79d85bSRitesh Harjani (IBM) 	*punch_start_byte = min_t(loff_t, end_byte,
10507f79d85bSRitesh Harjani (IBM) 				folio_pos(folio) + folio_size(folio));
10517f79d85bSRitesh Harjani (IBM) 
10527f79d85bSRitesh Harjani (IBM) 	return ret;
10537f79d85bSRitesh Harjani (IBM) }
10547f79d85bSRitesh Harjani (IBM) 
10559c7babf9SDave Chinner /*
1056f43dc4dcSDave Chinner  * Scan the data range passed to us for dirty page cache folios. If we find a
1057684f7e6dSGeert Uytterhoeven  * dirty folio, punch out the preceding range and update the offset from which
1058f43dc4dcSDave Chinner  * the next punch will start from.
1059f43dc4dcSDave Chinner  *
1060f43dc4dcSDave Chinner  * We can punch out storage reservations under clean pages because they either
1061f43dc4dcSDave Chinner  * contain data that has been written back - in which case the delalloc punch
1062f43dc4dcSDave Chinner  * over that range is a no-op - or they have been read faults in which case they
1063f43dc4dcSDave Chinner  * contain zeroes and we can remove the delalloc backing range and any new
1064f43dc4dcSDave Chinner  * writes to those pages will do the normal hole filling operation...
1065f43dc4dcSDave Chinner  *
1066f43dc4dcSDave Chinner  * This makes the logic simple: we only need to keep the delalloc extents only
1067f43dc4dcSDave Chinner  * over the dirty ranges of the page cache.
1068f43dc4dcSDave Chinner  *
1069f43dc4dcSDave Chinner  * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
1070f43dc4dcSDave Chinner  * simplify range iterations.
1071f43dc4dcSDave Chinner  */
iomap_write_delalloc_scan(struct inode * inode,loff_t * punch_start_byte,loff_t start_byte,loff_t end_byte,iomap_punch_t punch)1072f43dc4dcSDave Chinner static int iomap_write_delalloc_scan(struct inode *inode,
1073f43dc4dcSDave Chinner 		loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
10740af2b37dSRitesh Harjani (IBM) 		iomap_punch_t punch)
1075f43dc4dcSDave Chinner {
1076f43dc4dcSDave Chinner 	while (start_byte < end_byte) {
1077f43dc4dcSDave Chinner 		struct folio	*folio;
10787f79d85bSRitesh Harjani (IBM) 		int ret;
1079f43dc4dcSDave Chinner 
1080f43dc4dcSDave Chinner 		/* grab locked page */
1081f43dc4dcSDave Chinner 		folio = filemap_lock_folio(inode->i_mapping,
1082f43dc4dcSDave Chinner 				start_byte >> PAGE_SHIFT);
108366dabbb6SChristoph Hellwig 		if (IS_ERR(folio)) {
1084f43dc4dcSDave Chinner 			start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) +
1085f43dc4dcSDave Chinner 					PAGE_SIZE;
1086f43dc4dcSDave Chinner 			continue;
1087f43dc4dcSDave Chinner 		}
1088f43dc4dcSDave Chinner 
10897f79d85bSRitesh Harjani (IBM) 		ret = iomap_write_delalloc_punch(inode, folio, punch_start_byte,
10907f79d85bSRitesh Harjani (IBM) 						 start_byte, end_byte, punch);
10917f79d85bSRitesh Harjani (IBM) 		if (ret) {
1092f43dc4dcSDave Chinner 			folio_unlock(folio);
1093f43dc4dcSDave Chinner 			folio_put(folio);
10947f79d85bSRitesh Harjani (IBM) 			return ret;
1095f43dc4dcSDave Chinner 		}
1096f43dc4dcSDave Chinner 
1097f43dc4dcSDave Chinner 		/* move offset to start of next folio in range */
1098*91371922SMarco Nelissen 		start_byte = folio_pos(folio) + folio_size(folio);
1099f43dc4dcSDave Chinner 		folio_unlock(folio);
1100f43dc4dcSDave Chinner 		folio_put(folio);
1101f43dc4dcSDave Chinner 	}
1102f43dc4dcSDave Chinner 	return 0;
1103f43dc4dcSDave Chinner }
1104f43dc4dcSDave Chinner 
1105f43dc4dcSDave Chinner /*
1106f43dc4dcSDave Chinner  * Punch out all the delalloc blocks in the range given except for those that
1107f43dc4dcSDave Chinner  * have dirty data still pending in the page cache - those are going to be
1108f43dc4dcSDave Chinner  * written and so must still retain the delalloc backing for writeback.
1109f43dc4dcSDave Chinner  *
1110f43dc4dcSDave Chinner  * As we are scanning the page cache for data, we don't need to reimplement the
1111f43dc4dcSDave Chinner  * wheel - mapping_seek_hole_data() does exactly what we need to identify the
1112f43dc4dcSDave Chinner  * start and end of data ranges correctly even for sub-folio block sizes. This
1113f43dc4dcSDave Chinner  * byte range based iteration is especially convenient because it means we
1114f43dc4dcSDave Chinner  * don't have to care about variable size folios, nor where the start or end of
1115f43dc4dcSDave Chinner  * the data range lies within a folio, if they lie within the same folio or even
1116f43dc4dcSDave Chinner  * if there are multiple discontiguous data ranges within the folio.
1117f43dc4dcSDave Chinner  *
1118f43dc4dcSDave Chinner  * It should be noted that mapping_seek_hole_data() is not aware of EOF, and so
1119f43dc4dcSDave Chinner  * can return data ranges that exist in the cache beyond EOF. e.g. a page fault
1120f43dc4dcSDave Chinner  * spanning EOF will initialise the post-EOF data to zeroes and mark it up to
1121f43dc4dcSDave Chinner  * date. A write page fault can then mark it dirty. If we then fail a write()
1122f43dc4dcSDave Chinner  * beyond EOF into that up to date cached range, we allocate a delalloc block
1123f43dc4dcSDave Chinner  * beyond EOF and then have to punch it out. Because the range is up to date,
1124f43dc4dcSDave Chinner  * mapping_seek_hole_data() will return it, and we will skip the punch because
1125f43dc4dcSDave Chinner  * the folio is dirty. THis is incorrect - we always need to punch out delalloc
1126f43dc4dcSDave Chinner  * beyond EOF in this case as writeback will never write back and covert that
1127f43dc4dcSDave Chinner  * delalloc block beyond EOF. Hence we limit the cached data scan range to EOF,
1128f43dc4dcSDave Chinner  * resulting in always punching out the range from the EOF to the end of the
1129f43dc4dcSDave Chinner  * range the iomap spans.
1130f43dc4dcSDave Chinner  *
1131f43dc4dcSDave Chinner  * Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it
1132f43dc4dcSDave Chinner  * matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA
1133f43dc4dcSDave Chinner  * returns the start of a data range (start_byte), and SEEK_HOLE(start_byte)
1134f43dc4dcSDave Chinner  * returns the end of the data range (data_end). Using closed intervals would
1135f43dc4dcSDave Chinner  * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose
1136f43dc4dcSDave Chinner  * the code to subtle off-by-one bugs....
1137f43dc4dcSDave Chinner  */
iomap_write_delalloc_release(struct inode * inode,loff_t start_byte,loff_t end_byte,iomap_punch_t punch)1138f43dc4dcSDave Chinner static int iomap_write_delalloc_release(struct inode *inode,
11390af2b37dSRitesh Harjani (IBM) 		loff_t start_byte, loff_t end_byte, iomap_punch_t punch)
1140f43dc4dcSDave Chinner {
1141f43dc4dcSDave Chinner 	loff_t punch_start_byte = start_byte;
1142f43dc4dcSDave Chinner 	loff_t scan_end_byte = min(i_size_read(inode), end_byte);
1143f43dc4dcSDave Chinner 	int error = 0;
1144f43dc4dcSDave Chinner 
1145f43dc4dcSDave Chinner 	/*
1146f43dc4dcSDave Chinner 	 * Lock the mapping to avoid races with page faults re-instantiating
1147f43dc4dcSDave Chinner 	 * folios and dirtying them via ->page_mkwrite whilst we walk the
1148f43dc4dcSDave Chinner 	 * cache and perform delalloc extent removal. Failing to do this can
1149f43dc4dcSDave Chinner 	 * leave dirty pages with no space reservation in the cache.
1150f43dc4dcSDave Chinner 	 */
1151f43dc4dcSDave Chinner 	filemap_invalidate_lock(inode->i_mapping);
1152f43dc4dcSDave Chinner 	while (start_byte < scan_end_byte) {
1153f43dc4dcSDave Chinner 		loff_t		data_end;
1154f43dc4dcSDave Chinner 
1155f43dc4dcSDave Chinner 		start_byte = mapping_seek_hole_data(inode->i_mapping,
1156f43dc4dcSDave Chinner 				start_byte, scan_end_byte, SEEK_DATA);
1157f43dc4dcSDave Chinner 		/*
1158f43dc4dcSDave Chinner 		 * If there is no more data to scan, all that is left is to
1159f43dc4dcSDave Chinner 		 * punch out the remaining range.
1160f43dc4dcSDave Chinner 		 */
1161f43dc4dcSDave Chinner 		if (start_byte == -ENXIO || start_byte == scan_end_byte)
1162f43dc4dcSDave Chinner 			break;
1163f43dc4dcSDave Chinner 		if (start_byte < 0) {
1164f43dc4dcSDave Chinner 			error = start_byte;
1165f43dc4dcSDave Chinner 			goto out_unlock;
1166f43dc4dcSDave Chinner 		}
1167f43dc4dcSDave Chinner 		WARN_ON_ONCE(start_byte < punch_start_byte);
1168f43dc4dcSDave Chinner 		WARN_ON_ONCE(start_byte > scan_end_byte);
1169f43dc4dcSDave Chinner 
1170f43dc4dcSDave Chinner 		/*
1171f43dc4dcSDave Chinner 		 * We find the end of this contiguous cached data range by
1172f43dc4dcSDave Chinner 		 * seeking from start_byte to the beginning of the next hole.
1173f43dc4dcSDave Chinner 		 */
1174f43dc4dcSDave Chinner 		data_end = mapping_seek_hole_data(inode->i_mapping, start_byte,
1175f43dc4dcSDave Chinner 				scan_end_byte, SEEK_HOLE);
1176f43dc4dcSDave Chinner 		if (data_end < 0) {
1177f43dc4dcSDave Chinner 			error = data_end;
1178f43dc4dcSDave Chinner 			goto out_unlock;
1179f43dc4dcSDave Chinner 		}
1180390b9e54SChristoph Hellwig 
1181390b9e54SChristoph Hellwig 		/*
1182390b9e54SChristoph Hellwig 		 * If we race with post-direct I/O invalidation of the page cache,
1183390b9e54SChristoph Hellwig 		 * there might be no data left at start_byte.
1184390b9e54SChristoph Hellwig 		 */
1185390b9e54SChristoph Hellwig 		if (data_end == start_byte)
1186390b9e54SChristoph Hellwig 			continue;
1187390b9e54SChristoph Hellwig 
1188390b9e54SChristoph Hellwig 		WARN_ON_ONCE(data_end < start_byte);
1189f43dc4dcSDave Chinner 		WARN_ON_ONCE(data_end > scan_end_byte);
1190f43dc4dcSDave Chinner 
1191f43dc4dcSDave Chinner 		error = iomap_write_delalloc_scan(inode, &punch_start_byte,
1192f43dc4dcSDave Chinner 				start_byte, data_end, punch);
1193f43dc4dcSDave Chinner 		if (error)
1194f43dc4dcSDave Chinner 			goto out_unlock;
1195f43dc4dcSDave Chinner 
1196f43dc4dcSDave Chinner 		/* The next data search starts at the end of this one. */
1197f43dc4dcSDave Chinner 		start_byte = data_end;
1198f43dc4dcSDave Chinner 	}
1199f43dc4dcSDave Chinner 
1200f43dc4dcSDave Chinner 	if (punch_start_byte < end_byte)
1201f43dc4dcSDave Chinner 		error = punch(inode, punch_start_byte,
1202f43dc4dcSDave Chinner 				end_byte - punch_start_byte);
1203f43dc4dcSDave Chinner out_unlock:
1204f43dc4dcSDave Chinner 	filemap_invalidate_unlock(inode->i_mapping);
1205f43dc4dcSDave Chinner 	return error;
1206f43dc4dcSDave Chinner }
1207f43dc4dcSDave Chinner 
1208f43dc4dcSDave Chinner /*
12099c7babf9SDave Chinner  * When a short write occurs, the filesystem may need to remove reserved space
12109c7babf9SDave Chinner  * that was allocated in ->iomap_begin from it's ->iomap_end method. For
12119c7babf9SDave Chinner  * filesystems that use delayed allocation, we need to punch out delalloc
12129c7babf9SDave Chinner  * extents from the range that are not dirty in the page cache. As the write can
12139c7babf9SDave Chinner  * race with page faults, there can be dirty pages over the delalloc extent
12149c7babf9SDave Chinner  * outside the range of a short write but still within the delalloc extent
12159c7babf9SDave Chinner  * allocated for this iomap.
12169c7babf9SDave Chinner  *
12179c7babf9SDave Chinner  * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
1218f43dc4dcSDave Chinner  * simplify range iterations.
1219f43dc4dcSDave Chinner  *
1220f43dc4dcSDave Chinner  * The punch() callback *must* only punch delalloc extents in the range passed
1221f43dc4dcSDave Chinner  * to it. It must skip over all other types of extents in the range and leave
1222f43dc4dcSDave Chinner  * them completely unchanged. It must do this punch atomically with respect to
1223f43dc4dcSDave Chinner  * other extent modifications.
1224f43dc4dcSDave Chinner  *
1225f43dc4dcSDave Chinner  * The punch() callback may be called with a folio locked to prevent writeback
1226f43dc4dcSDave Chinner  * extent allocation racing at the edge of the range we are currently punching.
1227f43dc4dcSDave Chinner  * The locked folio may or may not cover the range being punched, so it is not
1228f43dc4dcSDave Chinner  * safe for the punch() callback to lock folios itself.
1229f43dc4dcSDave Chinner  *
1230f43dc4dcSDave Chinner  * Lock order is:
1231f43dc4dcSDave Chinner  *
1232f43dc4dcSDave Chinner  * inode->i_rwsem (shared or exclusive)
1233f43dc4dcSDave Chinner  *   inode->i_mapping->invalidate_lock (exclusive)
1234f43dc4dcSDave Chinner  *     folio_lock()
1235f43dc4dcSDave Chinner  *       ->punch
1236f43dc4dcSDave Chinner  *         internal filesystem allocation lock
12379c7babf9SDave Chinner  */
iomap_file_buffered_write_punch_delalloc(struct inode * inode,struct iomap * iomap,loff_t pos,loff_t length,ssize_t written,iomap_punch_t punch)12389c7babf9SDave Chinner int iomap_file_buffered_write_punch_delalloc(struct inode *inode,
12399c7babf9SDave Chinner 		struct iomap *iomap, loff_t pos, loff_t length,
12400af2b37dSRitesh Harjani (IBM) 		ssize_t written, iomap_punch_t punch)
12419c7babf9SDave Chinner {
12429c7babf9SDave Chinner 	loff_t			start_byte;
12439c7babf9SDave Chinner 	loff_t			end_byte;
1244302efbefSLu Hongfei 	unsigned int		blocksize = i_blocksize(inode);
12459c7babf9SDave Chinner 
12469c7babf9SDave Chinner 	if (iomap->type != IOMAP_DELALLOC)
12479c7babf9SDave Chinner 		return 0;
12489c7babf9SDave Chinner 
12499c7babf9SDave Chinner 	/* If we didn't reserve the blocks, we're not allowed to punch them. */
12509c7babf9SDave Chinner 	if (!(iomap->flags & IOMAP_F_NEW))
12519c7babf9SDave Chinner 		return 0;
12529c7babf9SDave Chinner 
12539c7babf9SDave Chinner 	/*
12549c7babf9SDave Chinner 	 * start_byte refers to the first unused block after a short write. If
12559c7babf9SDave Chinner 	 * nothing was written, round offset down to point at the first block in
12569c7babf9SDave Chinner 	 * the range.
12579c7babf9SDave Chinner 	 */
12589c7babf9SDave Chinner 	if (unlikely(!written))
12599c7babf9SDave Chinner 		start_byte = round_down(pos, blocksize);
12609c7babf9SDave Chinner 	else
12619c7babf9SDave Chinner 		start_byte = round_up(pos + written, blocksize);
12629c7babf9SDave Chinner 	end_byte = round_up(pos + length, blocksize);
12639c7babf9SDave Chinner 
12649c7babf9SDave Chinner 	/* Nothing to do if we've written the entire delalloc extent */
12659c7babf9SDave Chinner 	if (start_byte >= end_byte)
12669c7babf9SDave Chinner 		return 0;
12679c7babf9SDave Chinner 
1268f43dc4dcSDave Chinner 	return iomap_write_delalloc_release(inode, start_byte, end_byte,
1269f43dc4dcSDave Chinner 					punch);
12709c7babf9SDave Chinner }
12719c7babf9SDave Chinner EXPORT_SYMBOL_GPL(iomap_file_buffered_write_punch_delalloc);
12729c7babf9SDave Chinner 
iomap_unshare_iter(struct iomap_iter * iter)1273451b0a27SDarrick J. Wong static loff_t iomap_unshare_iter(struct iomap_iter *iter)
1274451b0a27SDarrick J. Wong {
1275451b0a27SDarrick J. Wong 	struct iomap *iomap = &iter->iomap;
1276451b0a27SDarrick J. Wong 	loff_t pos = iter->pos;
1277451b0a27SDarrick J. Wong 	loff_t length = iomap_length(iter);
1278451b0a27SDarrick J. Wong 	loff_t written = 0;
1279451b0a27SDarrick J. Wong 
1280451b0a27SDarrick J. Wong 	if (!iomap_want_unshare_iter(iter))
12813590c4d8SChristoph Hellwig 		return length;
12823590c4d8SChristoph Hellwig 
1283afc51aaaSDarrick J. Wong 	do {
1284bc6123a8SMatthew Wilcox (Oracle) 		struct folio *folio;
1285a5f31a50SDarrick J. Wong 		int status;
1286a5f31a50SDarrick J. Wong 		size_t offset;
1287a5f31a50SDarrick J. Wong 		size_t bytes = min_t(u64, SIZE_MAX, length);
1288afc51aaaSDarrick J. Wong 
1289bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_begin(iter, pos, bytes, &folio);
1290afc51aaaSDarrick J. Wong 		if (unlikely(status))
1291afc51aaaSDarrick J. Wong 			return status;
1292a5f31a50SDarrick J. Wong 		if (iomap->flags & IOMAP_F_STALE)
1293d7b64041SDave Chinner 			break;
1294afc51aaaSDarrick J. Wong 
1295a5f31a50SDarrick J. Wong 		offset = offset_in_folio(folio, pos);
1296a5f31a50SDarrick J. Wong 		if (bytes > folio_size(folio) - offset)
1297a5f31a50SDarrick J. Wong 			bytes = folio_size(folio) - offset;
1298a5f31a50SDarrick J. Wong 
1299a5f31a50SDarrick J. Wong 		bytes = iomap_write_end(iter, pos, bytes, bytes, folio);
1300a5f31a50SDarrick J. Wong 		if (WARN_ON_ONCE(bytes == 0))
1301afc51aaaSDarrick J. Wong 			return -EIO;
1302afc51aaaSDarrick J. Wong 
1303afc51aaaSDarrick J. Wong 		cond_resched();
1304afc51aaaSDarrick J. Wong 
1305a5f31a50SDarrick J. Wong 		pos += bytes;
1306a5f31a50SDarrick J. Wong 		written += bytes;
1307a5f31a50SDarrick J. Wong 		length -= bytes;
1308afc51aaaSDarrick J. Wong 
13098fc274d1SChristoph Hellwig 		balance_dirty_pages_ratelimited(iter->inode->i_mapping);
1310a5f31a50SDarrick J. Wong 	} while (length > 0);
1311afc51aaaSDarrick J. Wong 
1312afc51aaaSDarrick J. Wong 	return written;
1313afc51aaaSDarrick J. Wong }
1314afc51aaaSDarrick J. Wong 
1315afc51aaaSDarrick J. Wong int
iomap_file_unshare(struct inode * inode,loff_t pos,loff_t len,const struct iomap_ops * ops)13163590c4d8SChristoph Hellwig iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
1317afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
1318afc51aaaSDarrick J. Wong {
13198fc274d1SChristoph Hellwig 	struct iomap_iter iter = {
13208fc274d1SChristoph Hellwig 		.inode		= inode,
13218fc274d1SChristoph Hellwig 		.pos		= pos,
1322b74b1293SChristoph Hellwig 		.flags		= IOMAP_WRITE | IOMAP_UNSHARE,
13238fc274d1SChristoph Hellwig 	};
13241372c757SDarrick J. Wong 	loff_t size = i_size_read(inode);
13258fc274d1SChristoph Hellwig 	int ret;
1326afc51aaaSDarrick J. Wong 
13271372c757SDarrick J. Wong 	if (pos < 0 || pos >= size)
13281372c757SDarrick J. Wong 		return 0;
13291372c757SDarrick J. Wong 
13301372c757SDarrick J. Wong 	iter.len = min(len, size - pos);
13318fc274d1SChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
13328fc274d1SChristoph Hellwig 		iter.processed = iomap_unshare_iter(&iter);
1333afc51aaaSDarrick J. Wong 	return ret;
1334afc51aaaSDarrick J. Wong }
13353590c4d8SChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_file_unshare);
1336afc51aaaSDarrick J. Wong 
iomap_zero_iter(struct iomap_iter * iter,bool * did_zero)13372aa3048eSChristoph Hellwig static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
1338afc51aaaSDarrick J. Wong {
1339fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
13402aa3048eSChristoph Hellwig 	loff_t pos = iter->pos;
13412aa3048eSChristoph Hellwig 	loff_t length = iomap_length(iter);
1342afc51aaaSDarrick J. Wong 	loff_t written = 0;
1343afc51aaaSDarrick J. Wong 
1344afc51aaaSDarrick J. Wong 	/* already zeroed?  we're done. */
1345c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
134681ee8e52SMatthew Wilcox (Oracle) 		return length;
1347afc51aaaSDarrick J. Wong 
1348afc51aaaSDarrick J. Wong 	do {
13494d7bd0ebSMatthew Wilcox (Oracle) 		struct folio *folio;
13504d7bd0ebSMatthew Wilcox (Oracle) 		int status;
13514d7bd0ebSMatthew Wilcox (Oracle) 		size_t offset;
13524d7bd0ebSMatthew Wilcox (Oracle) 		size_t bytes = min_t(u64, SIZE_MAX, length);
1353afc51aaaSDarrick J. Wong 
13544d7bd0ebSMatthew Wilcox (Oracle) 		status = iomap_write_begin(iter, pos, bytes, &folio);
13554d7bd0ebSMatthew Wilcox (Oracle) 		if (status)
13564d7bd0ebSMatthew Wilcox (Oracle) 			return status;
1357d7b64041SDave Chinner 		if (iter->iomap.flags & IOMAP_F_STALE)
1358d7b64041SDave Chinner 			break;
13594d7bd0ebSMatthew Wilcox (Oracle) 
13604d7bd0ebSMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, pos);
13614d7bd0ebSMatthew Wilcox (Oracle) 		if (bytes > folio_size(folio) - offset)
13624d7bd0ebSMatthew Wilcox (Oracle) 			bytes = folio_size(folio) - offset;
13634d7bd0ebSMatthew Wilcox (Oracle) 
13644d7bd0ebSMatthew Wilcox (Oracle) 		folio_zero_range(folio, offset, bytes);
13654d7bd0ebSMatthew Wilcox (Oracle) 		folio_mark_accessed(folio);
13664d7bd0ebSMatthew Wilcox (Oracle) 
13674d7bd0ebSMatthew Wilcox (Oracle) 		bytes = iomap_write_end(iter, pos, bytes, bytes, folio);
13684d7bd0ebSMatthew Wilcox (Oracle) 		if (WARN_ON_ONCE(bytes == 0))
13694d7bd0ebSMatthew Wilcox (Oracle) 			return -EIO;
1370afc51aaaSDarrick J. Wong 
1371afc51aaaSDarrick J. Wong 		pos += bytes;
137281ee8e52SMatthew Wilcox (Oracle) 		length -= bytes;
1373afc51aaaSDarrick J. Wong 		written += bytes;
137481ee8e52SMatthew Wilcox (Oracle) 	} while (length > 0);
1375afc51aaaSDarrick J. Wong 
137698eb8d95SKaixu Xia 	if (did_zero)
137798eb8d95SKaixu Xia 		*did_zero = true;
1378afc51aaaSDarrick J. Wong 	return written;
1379afc51aaaSDarrick J. Wong }
1380afc51aaaSDarrick J. Wong 
1381afc51aaaSDarrick J. Wong int
iomap_zero_range(struct inode * inode,loff_t pos,loff_t len,bool * did_zero,const struct iomap_ops * ops)1382afc51aaaSDarrick J. Wong iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1383afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
1384afc51aaaSDarrick J. Wong {
13852aa3048eSChristoph Hellwig 	struct iomap_iter iter = {
13862aa3048eSChristoph Hellwig 		.inode		= inode,
13872aa3048eSChristoph Hellwig 		.pos		= pos,
13882aa3048eSChristoph Hellwig 		.len		= len,
13892aa3048eSChristoph Hellwig 		.flags		= IOMAP_ZERO,
13902aa3048eSChristoph Hellwig 	};
13912aa3048eSChristoph Hellwig 	int ret;
1392afc51aaaSDarrick J. Wong 
13932aa3048eSChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
13942aa3048eSChristoph Hellwig 		iter.processed = iomap_zero_iter(&iter, did_zero);
1395afc51aaaSDarrick J. Wong 	return ret;
1396afc51aaaSDarrick J. Wong }
1397afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_zero_range);
1398afc51aaaSDarrick J. Wong 
1399afc51aaaSDarrick J. Wong int
iomap_truncate_page(struct inode * inode,loff_t pos,bool * did_zero,const struct iomap_ops * ops)1400afc51aaaSDarrick J. Wong iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1401afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
1402afc51aaaSDarrick J. Wong {
1403afc51aaaSDarrick J. Wong 	unsigned int blocksize = i_blocksize(inode);
1404afc51aaaSDarrick J. Wong 	unsigned int off = pos & (blocksize - 1);
1405afc51aaaSDarrick J. Wong 
1406afc51aaaSDarrick J. Wong 	/* Block boundary? Nothing to do */
1407afc51aaaSDarrick J. Wong 	if (!off)
1408afc51aaaSDarrick J. Wong 		return 0;
1409afc51aaaSDarrick J. Wong 	return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1410afc51aaaSDarrick J. Wong }
1411afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_truncate_page);
1412afc51aaaSDarrick J. Wong 
iomap_folio_mkwrite_iter(struct iomap_iter * iter,struct folio * folio)1413ea0f843aSMatthew Wilcox (Oracle) static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter,
1414ea0f843aSMatthew Wilcox (Oracle) 		struct folio *folio)
1415afc51aaaSDarrick J. Wong {
1416253564baSChristoph Hellwig 	loff_t length = iomap_length(iter);
1417afc51aaaSDarrick J. Wong 	int ret;
1418afc51aaaSDarrick J. Wong 
1419253564baSChristoph Hellwig 	if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
1420d1bd0b4eSMatthew Wilcox (Oracle) 		ret = __block_write_begin_int(folio, iter->pos, length, NULL,
1421253564baSChristoph Hellwig 					      &iter->iomap);
1422afc51aaaSDarrick J. Wong 		if (ret)
1423afc51aaaSDarrick J. Wong 			return ret;
1424ea0f843aSMatthew Wilcox (Oracle) 		block_commit_write(&folio->page, 0, length);
1425afc51aaaSDarrick J. Wong 	} else {
1426ea0f843aSMatthew Wilcox (Oracle) 		WARN_ON_ONCE(!folio_test_uptodate(folio));
1427ea0f843aSMatthew Wilcox (Oracle) 		folio_mark_dirty(folio);
1428afc51aaaSDarrick J. Wong 	}
1429afc51aaaSDarrick J. Wong 
1430afc51aaaSDarrick J. Wong 	return length;
1431afc51aaaSDarrick J. Wong }
1432afc51aaaSDarrick J. Wong 
iomap_page_mkwrite(struct vm_fault * vmf,const struct iomap_ops * ops)1433afc51aaaSDarrick J. Wong vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1434afc51aaaSDarrick J. Wong {
1435253564baSChristoph Hellwig 	struct iomap_iter iter = {
1436253564baSChristoph Hellwig 		.inode		= file_inode(vmf->vma->vm_file),
1437253564baSChristoph Hellwig 		.flags		= IOMAP_WRITE | IOMAP_FAULT,
1438253564baSChristoph Hellwig 	};
1439ea0f843aSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
1440afc51aaaSDarrick J. Wong 	ssize_t ret;
1441afc51aaaSDarrick J. Wong 
1442ea0f843aSMatthew Wilcox (Oracle) 	folio_lock(folio);
1443ea0f843aSMatthew Wilcox (Oracle) 	ret = folio_mkwrite_check_truncate(folio, iter.inode);
1444243145bcSAndreas Gruenbacher 	if (ret < 0)
1445afc51aaaSDarrick J. Wong 		goto out_unlock;
1446ea0f843aSMatthew Wilcox (Oracle) 	iter.pos = folio_pos(folio);
1447253564baSChristoph Hellwig 	iter.len = ret;
1448253564baSChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
1449ea0f843aSMatthew Wilcox (Oracle) 		iter.processed = iomap_folio_mkwrite_iter(&iter, folio);
1450afc51aaaSDarrick J. Wong 
1451253564baSChristoph Hellwig 	if (ret < 0)
1452afc51aaaSDarrick J. Wong 		goto out_unlock;
1453ea0f843aSMatthew Wilcox (Oracle) 	folio_wait_stable(folio);
1454afc51aaaSDarrick J. Wong 	return VM_FAULT_LOCKED;
1455afc51aaaSDarrick J. Wong out_unlock:
1456ea0f843aSMatthew Wilcox (Oracle) 	folio_unlock(folio);
14572ba39cc4SChristoph Hellwig 	return vmf_fs_error(ret);
1458afc51aaaSDarrick J. Wong }
1459afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1460598ecfbaSChristoph Hellwig 
iomap_finish_folio_write(struct inode * inode,struct folio * folio,size_t len,int error)14618ffd74e9SMatthew Wilcox (Oracle) static void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
14628ffd74e9SMatthew Wilcox (Oracle) 		size_t len, int error)
1463598ecfbaSChristoph Hellwig {
146404f52c4eSRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
1465598ecfbaSChristoph Hellwig 
1466598ecfbaSChristoph Hellwig 	if (error) {
14678ffd74e9SMatthew Wilcox (Oracle) 		folio_set_error(folio);
1468b69eea82SDarrick J. Wong 		mapping_set_error(inode->i_mapping, error);
1469598ecfbaSChristoph Hellwig 	}
1470598ecfbaSChristoph Hellwig 
147104f52c4eSRitesh Harjani (IBM) 	WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs);
147204f52c4eSRitesh Harjani (IBM) 	WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
1473598ecfbaSChristoph Hellwig 
147404f52c4eSRitesh Harjani (IBM) 	if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
14758ffd74e9SMatthew Wilcox (Oracle) 		folio_end_writeback(folio);
1476598ecfbaSChristoph Hellwig }
1477598ecfbaSChristoph Hellwig 
1478598ecfbaSChristoph Hellwig /*
1479598ecfbaSChristoph Hellwig  * We're now finished for good with this ioend structure.  Update the page
1480598ecfbaSChristoph Hellwig  * state, release holds on bios, and finally free up memory.  Do not use the
1481598ecfbaSChristoph Hellwig  * ioend after this.
1482598ecfbaSChristoph Hellwig  */
1483ebb7fb15SDave Chinner static u32
iomap_finish_ioend(struct iomap_ioend * ioend,int error)1484598ecfbaSChristoph Hellwig iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1485598ecfbaSChristoph Hellwig {
1486598ecfbaSChristoph Hellwig 	struct inode *inode = ioend->io_inode;
1487598ecfbaSChristoph Hellwig 	struct bio *bio = &ioend->io_inline_bio;
1488598ecfbaSChristoph Hellwig 	struct bio *last = ioend->io_bio, *next;
1489598ecfbaSChristoph Hellwig 	u64 start = bio->bi_iter.bi_sector;
1490c275779fSZorro Lang 	loff_t offset = ioend->io_offset;
1491598ecfbaSChristoph Hellwig 	bool quiet = bio_flagged(bio, BIO_QUIET);
1492ebb7fb15SDave Chinner 	u32 folio_count = 0;
1493598ecfbaSChristoph Hellwig 
1494598ecfbaSChristoph Hellwig 	for (bio = &ioend->io_inline_bio; bio; bio = next) {
14958ffd74e9SMatthew Wilcox (Oracle) 		struct folio_iter fi;
1496598ecfbaSChristoph Hellwig 
1497598ecfbaSChristoph Hellwig 		/*
1498598ecfbaSChristoph Hellwig 		 * For the last bio, bi_private points to the ioend, so we
1499598ecfbaSChristoph Hellwig 		 * need to explicitly end the iteration here.
1500598ecfbaSChristoph Hellwig 		 */
1501598ecfbaSChristoph Hellwig 		if (bio == last)
1502598ecfbaSChristoph Hellwig 			next = NULL;
1503598ecfbaSChristoph Hellwig 		else
1504598ecfbaSChristoph Hellwig 			next = bio->bi_private;
1505598ecfbaSChristoph Hellwig 
15068ffd74e9SMatthew Wilcox (Oracle) 		/* walk all folios in bio, ending page IO on them */
1507ebb7fb15SDave Chinner 		bio_for_each_folio_all(fi, bio) {
15088ffd74e9SMatthew Wilcox (Oracle) 			iomap_finish_folio_write(inode, fi.folio, fi.length,
15098ffd74e9SMatthew Wilcox (Oracle) 					error);
1510ebb7fb15SDave Chinner 			folio_count++;
1511ebb7fb15SDave Chinner 		}
1512598ecfbaSChristoph Hellwig 		bio_put(bio);
1513598ecfbaSChristoph Hellwig 	}
1514c275779fSZorro Lang 	/* The ioend has been freed by bio_put() */
1515598ecfbaSChristoph Hellwig 
1516598ecfbaSChristoph Hellwig 	if (unlikely(error && !quiet)) {
1517598ecfbaSChristoph Hellwig 		printk_ratelimited(KERN_ERR
15189cd0ed63SDarrick J. Wong "%s: writeback error on inode %lu, offset %lld, sector %llu",
1519c275779fSZorro Lang 			inode->i_sb->s_id, inode->i_ino, offset, start);
1520598ecfbaSChristoph Hellwig 	}
1521ebb7fb15SDave Chinner 	return folio_count;
1522598ecfbaSChristoph Hellwig }
1523598ecfbaSChristoph Hellwig 
1524ebb7fb15SDave Chinner /*
1525ebb7fb15SDave Chinner  * Ioend completion routine for merged bios. This can only be called from task
1526ebb7fb15SDave Chinner  * contexts as merged ioends can be of unbound length. Hence we have to break up
1527ebb7fb15SDave Chinner  * the writeback completions into manageable chunks to avoid long scheduler
1528ebb7fb15SDave Chinner  * holdoffs. We aim to keep scheduler holdoffs down below 10ms so that we get
1529ebb7fb15SDave Chinner  * good batch processing throughput without creating adverse scheduler latency
1530ebb7fb15SDave Chinner  * conditions.
1531ebb7fb15SDave Chinner  */
1532598ecfbaSChristoph Hellwig void
iomap_finish_ioends(struct iomap_ioend * ioend,int error)1533598ecfbaSChristoph Hellwig iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1534598ecfbaSChristoph Hellwig {
1535598ecfbaSChristoph Hellwig 	struct list_head tmp;
1536ebb7fb15SDave Chinner 	u32 completions;
1537ebb7fb15SDave Chinner 
1538ebb7fb15SDave Chinner 	might_sleep();
1539598ecfbaSChristoph Hellwig 
1540598ecfbaSChristoph Hellwig 	list_replace_init(&ioend->io_list, &tmp);
1541ebb7fb15SDave Chinner 	completions = iomap_finish_ioend(ioend, error);
1542598ecfbaSChristoph Hellwig 
1543598ecfbaSChristoph Hellwig 	while (!list_empty(&tmp)) {
1544ebb7fb15SDave Chinner 		if (completions > IOEND_BATCH_SIZE * 8) {
1545ebb7fb15SDave Chinner 			cond_resched();
1546ebb7fb15SDave Chinner 			completions = 0;
1547ebb7fb15SDave Chinner 		}
1548598ecfbaSChristoph Hellwig 		ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1549598ecfbaSChristoph Hellwig 		list_del_init(&ioend->io_list);
1550ebb7fb15SDave Chinner 		completions += iomap_finish_ioend(ioend, error);
1551598ecfbaSChristoph Hellwig 	}
1552598ecfbaSChristoph Hellwig }
1553598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1554598ecfbaSChristoph Hellwig 
1555598ecfbaSChristoph Hellwig /*
1556598ecfbaSChristoph Hellwig  * We can merge two adjacent ioends if they have the same set of work to do.
1557598ecfbaSChristoph Hellwig  */
1558598ecfbaSChristoph Hellwig static bool
iomap_ioend_can_merge(struct iomap_ioend * ioend,struct iomap_ioend * next)1559598ecfbaSChristoph Hellwig iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1560598ecfbaSChristoph Hellwig {
1561598ecfbaSChristoph Hellwig 	if (ioend->io_bio->bi_status != next->io_bio->bi_status)
1562598ecfbaSChristoph Hellwig 		return false;
1563598ecfbaSChristoph Hellwig 	if ((ioend->io_flags & IOMAP_F_SHARED) ^
1564598ecfbaSChristoph Hellwig 	    (next->io_flags & IOMAP_F_SHARED))
1565598ecfbaSChristoph Hellwig 		return false;
1566598ecfbaSChristoph Hellwig 	if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1567598ecfbaSChristoph Hellwig 	    (next->io_type == IOMAP_UNWRITTEN))
1568598ecfbaSChristoph Hellwig 		return false;
1569598ecfbaSChristoph Hellwig 	if (ioend->io_offset + ioend->io_size != next->io_offset)
1570598ecfbaSChristoph Hellwig 		return false;
1571ebb7fb15SDave Chinner 	/*
1572ebb7fb15SDave Chinner 	 * Do not merge physically discontiguous ioends. The filesystem
1573ebb7fb15SDave Chinner 	 * completion functions will have to iterate the physical
1574ebb7fb15SDave Chinner 	 * discontiguities even if we merge the ioends at a logical level, so
1575ebb7fb15SDave Chinner 	 * we don't gain anything by merging physical discontiguities here.
1576ebb7fb15SDave Chinner 	 *
1577ebb7fb15SDave Chinner 	 * We cannot use bio->bi_iter.bi_sector here as it is modified during
1578ebb7fb15SDave Chinner 	 * submission so does not point to the start sector of the bio at
1579ebb7fb15SDave Chinner 	 * completion.
1580ebb7fb15SDave Chinner 	 */
1581ebb7fb15SDave Chinner 	if (ioend->io_sector + (ioend->io_size >> 9) != next->io_sector)
1582ebb7fb15SDave Chinner 		return false;
1583598ecfbaSChristoph Hellwig 	return true;
1584598ecfbaSChristoph Hellwig }
1585598ecfbaSChristoph Hellwig 
1586598ecfbaSChristoph Hellwig void
iomap_ioend_try_merge(struct iomap_ioend * ioend,struct list_head * more_ioends)15876e552494SBrian Foster iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
1588598ecfbaSChristoph Hellwig {
1589598ecfbaSChristoph Hellwig 	struct iomap_ioend *next;
1590598ecfbaSChristoph Hellwig 
1591598ecfbaSChristoph Hellwig 	INIT_LIST_HEAD(&ioend->io_list);
1592598ecfbaSChristoph Hellwig 
1593598ecfbaSChristoph Hellwig 	while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1594598ecfbaSChristoph Hellwig 			io_list))) {
1595598ecfbaSChristoph Hellwig 		if (!iomap_ioend_can_merge(ioend, next))
1596598ecfbaSChristoph Hellwig 			break;
1597598ecfbaSChristoph Hellwig 		list_move_tail(&next->io_list, &ioend->io_list);
1598598ecfbaSChristoph Hellwig 		ioend->io_size += next->io_size;
1599598ecfbaSChristoph Hellwig 	}
1600598ecfbaSChristoph Hellwig }
1601598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1602598ecfbaSChristoph Hellwig 
1603598ecfbaSChristoph Hellwig static int
iomap_ioend_compare(void * priv,const struct list_head * a,const struct list_head * b)16044f0f586bSSami Tolvanen iomap_ioend_compare(void *priv, const struct list_head *a,
16054f0f586bSSami Tolvanen 		const struct list_head *b)
1606598ecfbaSChristoph Hellwig {
1607b3d423ecSChristoph Hellwig 	struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1608b3d423ecSChristoph Hellwig 	struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
1609598ecfbaSChristoph Hellwig 
1610598ecfbaSChristoph Hellwig 	if (ia->io_offset < ib->io_offset)
1611598ecfbaSChristoph Hellwig 		return -1;
1612b3d423ecSChristoph Hellwig 	if (ia->io_offset > ib->io_offset)
1613598ecfbaSChristoph Hellwig 		return 1;
1614598ecfbaSChristoph Hellwig 	return 0;
1615598ecfbaSChristoph Hellwig }
1616598ecfbaSChristoph Hellwig 
1617598ecfbaSChristoph Hellwig void
iomap_sort_ioends(struct list_head * ioend_list)1618598ecfbaSChristoph Hellwig iomap_sort_ioends(struct list_head *ioend_list)
1619598ecfbaSChristoph Hellwig {
1620598ecfbaSChristoph Hellwig 	list_sort(NULL, ioend_list, iomap_ioend_compare);
1621598ecfbaSChristoph Hellwig }
1622598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1623598ecfbaSChristoph Hellwig 
iomap_writepage_end_bio(struct bio * bio)1624598ecfbaSChristoph Hellwig static void iomap_writepage_end_bio(struct bio *bio)
1625598ecfbaSChristoph Hellwig {
1626598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend = bio->bi_private;
1627598ecfbaSChristoph Hellwig 
1628598ecfbaSChristoph Hellwig 	iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
1629598ecfbaSChristoph Hellwig }
1630598ecfbaSChristoph Hellwig 
1631598ecfbaSChristoph Hellwig /*
1632598ecfbaSChristoph Hellwig  * Submit the final bio for an ioend.
1633598ecfbaSChristoph Hellwig  *
1634598ecfbaSChristoph Hellwig  * If @error is non-zero, it means that we have a situation where some part of
1635f1f264b4SAndreas Gruenbacher  * the submission process has failed after we've marked pages for writeback
1636598ecfbaSChristoph Hellwig  * and unlocked them.  In this situation, we need to fail the bio instead of
1637598ecfbaSChristoph Hellwig  * submitting it.  This typically only happens on a filesystem shutdown.
1638598ecfbaSChristoph Hellwig  */
1639598ecfbaSChristoph Hellwig static int
iomap_submit_ioend(struct iomap_writepage_ctx * wpc,struct iomap_ioend * ioend,int error)1640598ecfbaSChristoph Hellwig iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
1641598ecfbaSChristoph Hellwig 		int error)
1642598ecfbaSChristoph Hellwig {
1643598ecfbaSChristoph Hellwig 	ioend->io_bio->bi_private = ioend;
1644598ecfbaSChristoph Hellwig 	ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
1645598ecfbaSChristoph Hellwig 
1646598ecfbaSChristoph Hellwig 	if (wpc->ops->prepare_ioend)
1647598ecfbaSChristoph Hellwig 		error = wpc->ops->prepare_ioend(ioend, error);
1648598ecfbaSChristoph Hellwig 	if (error) {
1649598ecfbaSChristoph Hellwig 		/*
1650f1f264b4SAndreas Gruenbacher 		 * If we're failing the IO now, just mark the ioend with an
1651598ecfbaSChristoph Hellwig 		 * error and finish it.  This will run IO completion immediately
1652598ecfbaSChristoph Hellwig 		 * as there is only one reference to the ioend at this point in
1653598ecfbaSChristoph Hellwig 		 * time.
1654598ecfbaSChristoph Hellwig 		 */
1655598ecfbaSChristoph Hellwig 		ioend->io_bio->bi_status = errno_to_blk_status(error);
1656598ecfbaSChristoph Hellwig 		bio_endio(ioend->io_bio);
1657598ecfbaSChristoph Hellwig 		return error;
1658598ecfbaSChristoph Hellwig 	}
1659598ecfbaSChristoph Hellwig 
1660598ecfbaSChristoph Hellwig 	submit_bio(ioend->io_bio);
1661598ecfbaSChristoph Hellwig 	return 0;
1662598ecfbaSChristoph Hellwig }
1663598ecfbaSChristoph Hellwig 
1664598ecfbaSChristoph Hellwig static struct iomap_ioend *
iomap_alloc_ioend(struct inode * inode,struct iomap_writepage_ctx * wpc,loff_t offset,sector_t sector,struct writeback_control * wbc)1665598ecfbaSChristoph Hellwig iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
1666598ecfbaSChristoph Hellwig 		loff_t offset, sector_t sector, struct writeback_control *wbc)
1667598ecfbaSChristoph Hellwig {
1668598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend;
1669598ecfbaSChristoph Hellwig 	struct bio *bio;
1670598ecfbaSChristoph Hellwig 
1671609be106SChristoph Hellwig 	bio = bio_alloc_bioset(wpc->iomap.bdev, BIO_MAX_VECS,
1672609be106SChristoph Hellwig 			       REQ_OP_WRITE | wbc_to_write_flags(wbc),
1673609be106SChristoph Hellwig 			       GFP_NOFS, &iomap_ioend_bioset);
1674598ecfbaSChristoph Hellwig 	bio->bi_iter.bi_sector = sector;
1675598ecfbaSChristoph Hellwig 	wbc_init_bio(wbc, bio);
1676598ecfbaSChristoph Hellwig 
1677598ecfbaSChristoph Hellwig 	ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
1678598ecfbaSChristoph Hellwig 	INIT_LIST_HEAD(&ioend->io_list);
1679598ecfbaSChristoph Hellwig 	ioend->io_type = wpc->iomap.type;
1680598ecfbaSChristoph Hellwig 	ioend->io_flags = wpc->iomap.flags;
1681598ecfbaSChristoph Hellwig 	ioend->io_inode = inode;
1682598ecfbaSChristoph Hellwig 	ioend->io_size = 0;
1683ebb7fb15SDave Chinner 	ioend->io_folios = 0;
1684598ecfbaSChristoph Hellwig 	ioend->io_offset = offset;
1685598ecfbaSChristoph Hellwig 	ioend->io_bio = bio;
1686ebb7fb15SDave Chinner 	ioend->io_sector = sector;
1687598ecfbaSChristoph Hellwig 	return ioend;
1688598ecfbaSChristoph Hellwig }
1689598ecfbaSChristoph Hellwig 
1690598ecfbaSChristoph Hellwig /*
1691598ecfbaSChristoph Hellwig  * Allocate a new bio, and chain the old bio to the new one.
1692598ecfbaSChristoph Hellwig  *
1693f1f264b4SAndreas Gruenbacher  * Note that we have to perform the chaining in this unintuitive order
1694598ecfbaSChristoph Hellwig  * so that the bi_private linkage is set up in the right direction for the
1695598ecfbaSChristoph Hellwig  * traversal in iomap_finish_ioend().
1696598ecfbaSChristoph Hellwig  */
1697598ecfbaSChristoph Hellwig static struct bio *
iomap_chain_bio(struct bio * prev)1698598ecfbaSChristoph Hellwig iomap_chain_bio(struct bio *prev)
1699598ecfbaSChristoph Hellwig {
1700598ecfbaSChristoph Hellwig 	struct bio *new;
1701598ecfbaSChristoph Hellwig 
170207888c66SChristoph Hellwig 	new = bio_alloc(prev->bi_bdev, BIO_MAX_VECS, prev->bi_opf, GFP_NOFS);
170307888c66SChristoph Hellwig 	bio_clone_blkg_association(new, prev);
1704598ecfbaSChristoph Hellwig 	new->bi_iter.bi_sector = bio_end_sector(prev);
1705598ecfbaSChristoph Hellwig 
1706598ecfbaSChristoph Hellwig 	bio_chain(prev, new);
1707598ecfbaSChristoph Hellwig 	bio_get(prev);		/* for iomap_finish_ioend */
1708598ecfbaSChristoph Hellwig 	submit_bio(prev);
1709598ecfbaSChristoph Hellwig 	return new;
1710598ecfbaSChristoph Hellwig }
1711598ecfbaSChristoph Hellwig 
1712598ecfbaSChristoph Hellwig static bool
iomap_can_add_to_ioend(struct iomap_writepage_ctx * wpc,loff_t offset,sector_t sector)1713598ecfbaSChristoph Hellwig iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
1714598ecfbaSChristoph Hellwig 		sector_t sector)
1715598ecfbaSChristoph Hellwig {
1716598ecfbaSChristoph Hellwig 	if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1717598ecfbaSChristoph Hellwig 	    (wpc->ioend->io_flags & IOMAP_F_SHARED))
1718598ecfbaSChristoph Hellwig 		return false;
1719598ecfbaSChristoph Hellwig 	if (wpc->iomap.type != wpc->ioend->io_type)
1720598ecfbaSChristoph Hellwig 		return false;
1721598ecfbaSChristoph Hellwig 	if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
1722598ecfbaSChristoph Hellwig 		return false;
1723598ecfbaSChristoph Hellwig 	if (sector != bio_end_sector(wpc->ioend->io_bio))
1724598ecfbaSChristoph Hellwig 		return false;
1725ebb7fb15SDave Chinner 	/*
1726ebb7fb15SDave Chinner 	 * Limit ioend bio chain lengths to minimise IO completion latency. This
1727ebb7fb15SDave Chinner 	 * also prevents long tight loops ending page writeback on all the
1728ebb7fb15SDave Chinner 	 * folios in the ioend.
1729ebb7fb15SDave Chinner 	 */
1730ebb7fb15SDave Chinner 	if (wpc->ioend->io_folios >= IOEND_BATCH_SIZE)
1731ebb7fb15SDave Chinner 		return false;
1732598ecfbaSChristoph Hellwig 	return true;
1733598ecfbaSChristoph Hellwig }
1734598ecfbaSChristoph Hellwig 
1735598ecfbaSChristoph Hellwig /*
1736598ecfbaSChristoph Hellwig  * Test to see if we have an existing ioend structure that we could append to
1737f1f264b4SAndreas Gruenbacher  * first; otherwise finish off the current ioend and start another.
1738598ecfbaSChristoph Hellwig  */
1739598ecfbaSChristoph Hellwig static void
iomap_add_to_ioend(struct inode * inode,loff_t pos,struct folio * folio,struct iomap_folio_state * ifs,struct iomap_writepage_ctx * wpc,struct writeback_control * wbc,struct list_head * iolist)1740e735c007SMatthew Wilcox (Oracle) iomap_add_to_ioend(struct inode *inode, loff_t pos, struct folio *folio,
174104f52c4eSRitesh Harjani (IBM) 		struct iomap_folio_state *ifs, struct iomap_writepage_ctx *wpc,
1742598ecfbaSChristoph Hellwig 		struct writeback_control *wbc, struct list_head *iolist)
1743598ecfbaSChristoph Hellwig {
1744e735c007SMatthew Wilcox (Oracle) 	sector_t sector = iomap_sector(&wpc->iomap, pos);
1745598ecfbaSChristoph Hellwig 	unsigned len = i_blocksize(inode);
1746e735c007SMatthew Wilcox (Oracle) 	size_t poff = offset_in_folio(folio, pos);
1747598ecfbaSChristoph Hellwig 
1748e735c007SMatthew Wilcox (Oracle) 	if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, pos, sector)) {
1749598ecfbaSChristoph Hellwig 		if (wpc->ioend)
1750598ecfbaSChristoph Hellwig 			list_add(&wpc->ioend->io_list, iolist);
1751e735c007SMatthew Wilcox (Oracle) 		wpc->ioend = iomap_alloc_ioend(inode, wpc, pos, sector, wbc);
1752598ecfbaSChristoph Hellwig 	}
1753598ecfbaSChristoph Hellwig 
1754e735c007SMatthew Wilcox (Oracle) 	if (!bio_add_folio(wpc->ioend->io_bio, folio, len, poff)) {
1755c1b79f11SChristoph Hellwig 		wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio);
1756c2478469SJohannes Thumshirn 		bio_add_folio_nofail(wpc->ioend->io_bio, folio, len, poff);
1757c1b79f11SChristoph Hellwig 	}
1758c1b79f11SChristoph Hellwig 
175904f52c4eSRitesh Harjani (IBM) 	if (ifs)
176004f52c4eSRitesh Harjani (IBM) 		atomic_add(len, &ifs->write_bytes_pending);
1761598ecfbaSChristoph Hellwig 	wpc->ioend->io_size += len;
1762e735c007SMatthew Wilcox (Oracle) 	wbc_account_cgroup_owner(wbc, &folio->page, len);
1763598ecfbaSChristoph Hellwig }
1764598ecfbaSChristoph Hellwig 
1765598ecfbaSChristoph Hellwig /*
1766598ecfbaSChristoph Hellwig  * We implement an immediate ioend submission policy here to avoid needing to
1767598ecfbaSChristoph Hellwig  * chain multiple ioends and hence nest mempool allocations which can violate
1768f1f264b4SAndreas Gruenbacher  * the forward progress guarantees we need to provide. The current ioend we're
1769f1f264b4SAndreas Gruenbacher  * adding blocks to is cached in the writepage context, and if the new block
1770f1f264b4SAndreas Gruenbacher  * doesn't append to the cached ioend, it will create a new ioend and cache that
1771598ecfbaSChristoph Hellwig  * instead.
1772598ecfbaSChristoph Hellwig  *
1773598ecfbaSChristoph Hellwig  * If a new ioend is created and cached, the old ioend is returned and queued
1774598ecfbaSChristoph Hellwig  * locally for submission once the entire page is processed or an error has been
1775598ecfbaSChristoph Hellwig  * detected.  While ioends are submitted immediately after they are completed,
1776598ecfbaSChristoph Hellwig  * batching optimisations are provided by higher level block plugging.
1777598ecfbaSChristoph Hellwig  *
1778598ecfbaSChristoph Hellwig  * At the end of a writeback pass, there will be a cached ioend remaining on the
1779598ecfbaSChristoph Hellwig  * writepage context that the caller will need to submit.
1780598ecfbaSChristoph Hellwig  */
1781598ecfbaSChristoph Hellwig static int
iomap_writepage_map(struct iomap_writepage_ctx * wpc,struct writeback_control * wbc,struct inode * inode,struct folio * folio,u64 end_pos)1782598ecfbaSChristoph Hellwig iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1783598ecfbaSChristoph Hellwig 		struct writeback_control *wbc, struct inode *inode,
1784e735c007SMatthew Wilcox (Oracle) 		struct folio *folio, u64 end_pos)
1785598ecfbaSChristoph Hellwig {
17864ce02c67SRitesh Harjani (IBM) 	struct iomap_folio_state *ifs = folio->private;
1787598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend, *next;
1788598ecfbaSChristoph Hellwig 	unsigned len = i_blocksize(inode);
178992655036SMatthew Wilcox (Oracle) 	unsigned nblocks = i_blocks_per_folio(inode, folio);
179092655036SMatthew Wilcox (Oracle) 	u64 pos = folio_pos(folio);
1791598ecfbaSChristoph Hellwig 	int error = 0, count = 0, i;
1792598ecfbaSChristoph Hellwig 	LIST_HEAD(submit_list);
1793598ecfbaSChristoph Hellwig 
17944ce02c67SRitesh Harjani (IBM) 	WARN_ON_ONCE(end_pos <= pos);
17954ce02c67SRitesh Harjani (IBM) 
17964ce02c67SRitesh Harjani (IBM) 	if (!ifs && nblocks > 1) {
17974ce02c67SRitesh Harjani (IBM) 		ifs = ifs_alloc(inode, folio, 0);
17984ce02c67SRitesh Harjani (IBM) 		iomap_set_range_dirty(folio, 0, end_pos - pos);
17994ce02c67SRitesh Harjani (IBM) 	}
18004ce02c67SRitesh Harjani (IBM) 
180104f52c4eSRitesh Harjani (IBM) 	WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) != 0);
1802598ecfbaSChristoph Hellwig 
1803598ecfbaSChristoph Hellwig 	/*
180492655036SMatthew Wilcox (Oracle) 	 * Walk through the folio to find areas to write back. If we
180592655036SMatthew Wilcox (Oracle) 	 * run off the end of the current map or find the current map
180692655036SMatthew Wilcox (Oracle) 	 * invalid, grab a new one.
1807598ecfbaSChristoph Hellwig 	 */
180892655036SMatthew Wilcox (Oracle) 	for (i = 0; i < nblocks && pos < end_pos; i++, pos += len) {
18094ce02c67SRitesh Harjani (IBM) 		if (ifs && !ifs_block_is_dirty(folio, ifs, i))
1810598ecfbaSChristoph Hellwig 			continue;
1811598ecfbaSChristoph Hellwig 
181292655036SMatthew Wilcox (Oracle) 		error = wpc->ops->map_blocks(wpc, inode, pos);
1813598ecfbaSChristoph Hellwig 		if (error)
1814598ecfbaSChristoph Hellwig 			break;
1815adc9c2e5SDarrick J. Wong 		trace_iomap_writepage_map(inode, &wpc->iomap);
18163e19e6f3SChristoph Hellwig 		if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
18173e19e6f3SChristoph Hellwig 			continue;
1818598ecfbaSChristoph Hellwig 		if (wpc->iomap.type == IOMAP_HOLE)
1819598ecfbaSChristoph Hellwig 			continue;
182004f52c4eSRitesh Harjani (IBM) 		iomap_add_to_ioend(inode, pos, folio, ifs, wpc, wbc,
1821598ecfbaSChristoph Hellwig 				 &submit_list);
1822598ecfbaSChristoph Hellwig 		count++;
1823598ecfbaSChristoph Hellwig 	}
1824ebb7fb15SDave Chinner 	if (count)
1825ebb7fb15SDave Chinner 		wpc->ioend->io_folios++;
1826598ecfbaSChristoph Hellwig 
1827598ecfbaSChristoph Hellwig 	WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
1828e735c007SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(!folio_test_locked(folio));
1829e735c007SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(folio_test_writeback(folio));
1830e735c007SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(folio_test_dirty(folio));
1831598ecfbaSChristoph Hellwig 
1832598ecfbaSChristoph Hellwig 	/*
1833598ecfbaSChristoph Hellwig 	 * We cannot cancel the ioend directly here on error.  We may have
1834598ecfbaSChristoph Hellwig 	 * already set other pages under writeback and hence we have to run I/O
1835598ecfbaSChristoph Hellwig 	 * completion to mark the error state of the pages under writeback
1836598ecfbaSChristoph Hellwig 	 * appropriately.
1837598ecfbaSChristoph Hellwig 	 */
1838598ecfbaSChristoph Hellwig 	if (unlikely(error)) {
1839598ecfbaSChristoph Hellwig 		/*
1840763e4cdcSBrian Foster 		 * Let the filesystem know what portion of the current page
18410ab2a85cSChristoph Hellwig 		 * failed to map.
1842598ecfbaSChristoph Hellwig 		 */
18436e478521SMatthew Wilcox (Oracle) 		if (wpc->ops->discard_folio)
184492655036SMatthew Wilcox (Oracle) 			wpc->ops->discard_folio(folio, pos);
1845598ecfbaSChristoph Hellwig 	}
1846598ecfbaSChristoph Hellwig 
18474ce02c67SRitesh Harjani (IBM) 	/*
18484ce02c67SRitesh Harjani (IBM) 	 * We can have dirty bits set past end of file in page_mkwrite path
18494ce02c67SRitesh Harjani (IBM) 	 * while mapping the last partial folio. Hence it's better to clear
18504ce02c67SRitesh Harjani (IBM) 	 * all the dirty bits in the folio here.
18514ce02c67SRitesh Harjani (IBM) 	 */
18524ce02c67SRitesh Harjani (IBM) 	iomap_clear_range_dirty(folio, 0, folio_size(folio));
18530ab2a85cSChristoph Hellwig 
18540ab2a85cSChristoph Hellwig 	/*
18550ab2a85cSChristoph Hellwig 	 * If the page hasn't been added to the ioend, it won't be affected by
18560ab2a85cSChristoph Hellwig 	 * I/O completion and we must unlock it now.
18570ab2a85cSChristoph Hellwig 	 */
18580ab2a85cSChristoph Hellwig 	if (error && !count) {
18590ab2a85cSChristoph Hellwig 		folio_unlock(folio);
18600ab2a85cSChristoph Hellwig 		goto done;
18610ab2a85cSChristoph Hellwig 	}
18620ab2a85cSChristoph Hellwig 
1863e735c007SMatthew Wilcox (Oracle) 	folio_start_writeback(folio);
1864e735c007SMatthew Wilcox (Oracle) 	folio_unlock(folio);
1865598ecfbaSChristoph Hellwig 
1866598ecfbaSChristoph Hellwig 	/*
1867f1f264b4SAndreas Gruenbacher 	 * Preserve the original error if there was one; catch
1868598ecfbaSChristoph Hellwig 	 * submission errors here and propagate into subsequent ioend
1869598ecfbaSChristoph Hellwig 	 * submissions.
1870598ecfbaSChristoph Hellwig 	 */
1871598ecfbaSChristoph Hellwig 	list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
1872598ecfbaSChristoph Hellwig 		int error2;
1873598ecfbaSChristoph Hellwig 
1874598ecfbaSChristoph Hellwig 		list_del_init(&ioend->io_list);
1875598ecfbaSChristoph Hellwig 		error2 = iomap_submit_ioend(wpc, ioend, error);
1876598ecfbaSChristoph Hellwig 		if (error2 && !error)
1877598ecfbaSChristoph Hellwig 			error = error2;
1878598ecfbaSChristoph Hellwig 	}
1879598ecfbaSChristoph Hellwig 
1880598ecfbaSChristoph Hellwig 	/*
1881598ecfbaSChristoph Hellwig 	 * We can end up here with no error and nothing to write only if we race
1882598ecfbaSChristoph Hellwig 	 * with a partial page truncate on a sub-page block sized filesystem.
1883598ecfbaSChristoph Hellwig 	 */
1884598ecfbaSChristoph Hellwig 	if (!count)
1885e735c007SMatthew Wilcox (Oracle) 		folio_end_writeback(folio);
1886598ecfbaSChristoph Hellwig done:
18873d5f3ba1SDarrick J. Wong 	mapping_set_error(inode->i_mapping, error);
1888598ecfbaSChristoph Hellwig 	return error;
1889598ecfbaSChristoph Hellwig }
1890598ecfbaSChristoph Hellwig 
1891598ecfbaSChristoph Hellwig /*
1892598ecfbaSChristoph Hellwig  * Write out a dirty page.
1893598ecfbaSChristoph Hellwig  *
1894f1f264b4SAndreas Gruenbacher  * For delalloc space on the page, we need to allocate space and flush it.
1895f1f264b4SAndreas Gruenbacher  * For unwritten space on the page, we need to start the conversion to
1896598ecfbaSChristoph Hellwig  * regular allocated space.
1897598ecfbaSChristoph Hellwig  */
iomap_do_writepage(struct folio * folio,struct writeback_control * wbc,void * data)1898d585bdbeSMatthew Wilcox (Oracle) static int iomap_do_writepage(struct folio *folio,
1899d585bdbeSMatthew Wilcox (Oracle) 		struct writeback_control *wbc, void *data)
1900598ecfbaSChristoph Hellwig {
1901598ecfbaSChristoph Hellwig 	struct iomap_writepage_ctx *wpc = data;
1902e735c007SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
190381d4782aSMatthew Wilcox (Oracle) 	u64 end_pos, isize;
1904598ecfbaSChristoph Hellwig 
1905e735c007SMatthew Wilcox (Oracle) 	trace_iomap_writepage(inode, folio_pos(folio), folio_size(folio));
1906598ecfbaSChristoph Hellwig 
1907598ecfbaSChristoph Hellwig 	/*
1908e735c007SMatthew Wilcox (Oracle) 	 * Refuse to write the folio out if we're called from reclaim context.
1909598ecfbaSChristoph Hellwig 	 *
1910598ecfbaSChristoph Hellwig 	 * This avoids stack overflows when called from deeply used stacks in
1911598ecfbaSChristoph Hellwig 	 * random callers for direct reclaim or memcg reclaim.  We explicitly
1912598ecfbaSChristoph Hellwig 	 * allow reclaim from kswapd as the stack usage there is relatively low.
1913598ecfbaSChristoph Hellwig 	 *
1914598ecfbaSChristoph Hellwig 	 * This should never happen except in the case of a VM regression so
1915598ecfbaSChristoph Hellwig 	 * warn about it.
1916598ecfbaSChristoph Hellwig 	 */
1917598ecfbaSChristoph Hellwig 	if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1918598ecfbaSChristoph Hellwig 			PF_MEMALLOC))
1919598ecfbaSChristoph Hellwig 		goto redirty;
1920598ecfbaSChristoph Hellwig 
1921598ecfbaSChristoph Hellwig 	/*
1922e735c007SMatthew Wilcox (Oracle) 	 * Is this folio beyond the end of the file?
1923598ecfbaSChristoph Hellwig 	 *
1924e735c007SMatthew Wilcox (Oracle) 	 * The folio index is less than the end_index, adjust the end_pos
1925e735c007SMatthew Wilcox (Oracle) 	 * to the highest offset that this folio should represent.
1926598ecfbaSChristoph Hellwig 	 * -----------------------------------------------------
1927598ecfbaSChristoph Hellwig 	 * |			file mapping	       | <EOF> |
1928598ecfbaSChristoph Hellwig 	 * -----------------------------------------------------
1929598ecfbaSChristoph Hellwig 	 * | Page ... | Page N-2 | Page N-1 |  Page N  |       |
1930598ecfbaSChristoph Hellwig 	 * ^--------------------------------^----------|--------
1931598ecfbaSChristoph Hellwig 	 * |     desired writeback range    |      see else    |
1932598ecfbaSChristoph Hellwig 	 * ---------------------------------^------------------|
1933598ecfbaSChristoph Hellwig 	 */
193481d4782aSMatthew Wilcox (Oracle) 	isize = i_size_read(inode);
1935e735c007SMatthew Wilcox (Oracle) 	end_pos = folio_pos(folio) + folio_size(folio);
193681d4782aSMatthew Wilcox (Oracle) 	if (end_pos > isize) {
1937598ecfbaSChristoph Hellwig 		/*
1938598ecfbaSChristoph Hellwig 		 * Check whether the page to write out is beyond or straddles
1939598ecfbaSChristoph Hellwig 		 * i_size or not.
1940598ecfbaSChristoph Hellwig 		 * -------------------------------------------------------
1941598ecfbaSChristoph Hellwig 		 * |		file mapping		        | <EOF>  |
1942598ecfbaSChristoph Hellwig 		 * -------------------------------------------------------
1943598ecfbaSChristoph Hellwig 		 * | Page ... | Page N-2 | Page N-1 |  Page N   | Beyond |
1944598ecfbaSChristoph Hellwig 		 * ^--------------------------------^-----------|---------
1945598ecfbaSChristoph Hellwig 		 * |				    |      Straddles     |
1946598ecfbaSChristoph Hellwig 		 * ---------------------------------^-----------|--------|
1947598ecfbaSChristoph Hellwig 		 */
1948e735c007SMatthew Wilcox (Oracle) 		size_t poff = offset_in_folio(folio, isize);
194981d4782aSMatthew Wilcox (Oracle) 		pgoff_t end_index = isize >> PAGE_SHIFT;
1950598ecfbaSChristoph Hellwig 
1951598ecfbaSChristoph Hellwig 		/*
1952d58562caSChris Mason 		 * Skip the page if it's fully outside i_size, e.g.
1953d58562caSChris Mason 		 * due to a truncate operation that's in progress.  We've
1954d58562caSChris Mason 		 * cleaned this page and truncate will finish things off for
1955d58562caSChris Mason 		 * us.
1956598ecfbaSChristoph Hellwig 		 *
1957f1f264b4SAndreas Gruenbacher 		 * Note that the end_index is unsigned long.  If the given
1958f1f264b4SAndreas Gruenbacher 		 * offset is greater than 16TB on a 32-bit system then if we
1959f1f264b4SAndreas Gruenbacher 		 * checked if the page is fully outside i_size with
1960f1f264b4SAndreas Gruenbacher 		 * "if (page->index >= end_index + 1)", "end_index + 1" would
1961f1f264b4SAndreas Gruenbacher 		 * overflow and evaluate to 0.  Hence this page would be
1962f1f264b4SAndreas Gruenbacher 		 * redirtied and written out repeatedly, which would result in
1963f1f264b4SAndreas Gruenbacher 		 * an infinite loop; the user program performing this operation
1964f1f264b4SAndreas Gruenbacher 		 * would hang.  Instead, we can detect this situation by
1965f1f264b4SAndreas Gruenbacher 		 * checking if the page is totally beyond i_size or if its
1966598ecfbaSChristoph Hellwig 		 * offset is just equal to the EOF.
1967598ecfbaSChristoph Hellwig 		 */
1968e735c007SMatthew Wilcox (Oracle) 		if (folio->index > end_index ||
1969e735c007SMatthew Wilcox (Oracle) 		    (folio->index == end_index && poff == 0))
1970d58562caSChris Mason 			goto unlock;
1971598ecfbaSChristoph Hellwig 
1972598ecfbaSChristoph Hellwig 		/*
1973598ecfbaSChristoph Hellwig 		 * The page straddles i_size.  It must be zeroed out on each
1974598ecfbaSChristoph Hellwig 		 * and every writepage invocation because it may be mmapped.
1975598ecfbaSChristoph Hellwig 		 * "A file is mapped in multiples of the page size.  For a file
1976598ecfbaSChristoph Hellwig 		 * that is not a multiple of the page size, the remaining
1977598ecfbaSChristoph Hellwig 		 * memory is zeroed when mapped, and writes to that region are
1978598ecfbaSChristoph Hellwig 		 * not written out to the file."
1979598ecfbaSChristoph Hellwig 		 */
1980e735c007SMatthew Wilcox (Oracle) 		folio_zero_segment(folio, poff, folio_size(folio));
198181d4782aSMatthew Wilcox (Oracle) 		end_pos = isize;
1982598ecfbaSChristoph Hellwig 	}
1983598ecfbaSChristoph Hellwig 
1984e735c007SMatthew Wilcox (Oracle) 	return iomap_writepage_map(wpc, wbc, inode, folio, end_pos);
1985598ecfbaSChristoph Hellwig 
1986598ecfbaSChristoph Hellwig redirty:
1987e735c007SMatthew Wilcox (Oracle) 	folio_redirty_for_writepage(wbc, folio);
1988d58562caSChris Mason unlock:
1989e735c007SMatthew Wilcox (Oracle) 	folio_unlock(folio);
1990598ecfbaSChristoph Hellwig 	return 0;
1991598ecfbaSChristoph Hellwig }
1992598ecfbaSChristoph Hellwig 
1993598ecfbaSChristoph Hellwig int
iomap_writepages(struct address_space * mapping,struct writeback_control * wbc,struct iomap_writepage_ctx * wpc,const struct iomap_writeback_ops * ops)1994598ecfbaSChristoph Hellwig iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1995598ecfbaSChristoph Hellwig 		struct iomap_writepage_ctx *wpc,
1996598ecfbaSChristoph Hellwig 		const struct iomap_writeback_ops *ops)
1997598ecfbaSChristoph Hellwig {
1998598ecfbaSChristoph Hellwig 	int			ret;
1999598ecfbaSChristoph Hellwig 
2000598ecfbaSChristoph Hellwig 	wpc->ops = ops;
2001598ecfbaSChristoph Hellwig 	ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
2002598ecfbaSChristoph Hellwig 	if (!wpc->ioend)
2003598ecfbaSChristoph Hellwig 		return ret;
2004598ecfbaSChristoph Hellwig 	return iomap_submit_ioend(wpc, wpc->ioend, ret);
2005598ecfbaSChristoph Hellwig }
2006598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_writepages);
2007598ecfbaSChristoph Hellwig 
iomap_init(void)2008598ecfbaSChristoph Hellwig static int __init iomap_init(void)
2009598ecfbaSChristoph Hellwig {
2010598ecfbaSChristoph Hellwig 	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
2011598ecfbaSChristoph Hellwig 			   offsetof(struct iomap_ioend, io_inline_bio),
2012598ecfbaSChristoph Hellwig 			   BIOSET_NEED_BVECS);
2013598ecfbaSChristoph Hellwig }
2014598ecfbaSChristoph Hellwig fs_initcall(iomap_init);
2015