xref: /openbmc/linux/fs/iomap/buffered-io.c (revision cae2de69)
1afc51aaaSDarrick J. Wong // SPDX-License-Identifier: GPL-2.0
2afc51aaaSDarrick J. Wong /*
3afc51aaaSDarrick J. Wong  * Copyright (C) 2010 Red Hat, Inc.
4598ecfbaSChristoph Hellwig  * Copyright (C) 2016-2019 Christoph Hellwig.
5afc51aaaSDarrick J. Wong  */
6afc51aaaSDarrick J. Wong #include <linux/module.h>
7afc51aaaSDarrick J. Wong #include <linux/compiler.h>
8afc51aaaSDarrick J. Wong #include <linux/fs.h>
9afc51aaaSDarrick J. Wong #include <linux/iomap.h>
10afc51aaaSDarrick J. Wong #include <linux/pagemap.h>
11afc51aaaSDarrick J. Wong #include <linux/uio.h>
12afc51aaaSDarrick J. Wong #include <linux/buffer_head.h>
13afc51aaaSDarrick J. Wong #include <linux/dax.h>
14afc51aaaSDarrick J. Wong #include <linux/writeback.h>
15598ecfbaSChristoph Hellwig #include <linux/list_sort.h>
16afc51aaaSDarrick J. Wong #include <linux/swap.h>
17afc51aaaSDarrick J. Wong #include <linux/bio.h>
18afc51aaaSDarrick J. Wong #include <linux/sched/signal.h>
19afc51aaaSDarrick J. Wong #include <linux/migrate.h>
209e91c572SChristoph Hellwig #include "trace.h"
21afc51aaaSDarrick J. Wong 
22afc51aaaSDarrick J. Wong #include "../internal.h"
23afc51aaaSDarrick J. Wong 
24ebb7fb15SDave Chinner #define IOEND_BATCH_SIZE	4096
25ebb7fb15SDave Chinner 
26ab08b01eSChristoph Hellwig /*
2795c4cd05SMatthew Wilcox (Oracle)  * Structure allocated for each folio when block size < folio size
2895c4cd05SMatthew Wilcox (Oracle)  * to track sub-folio uptodate status and I/O completions.
29ab08b01eSChristoph Hellwig  */
30ab08b01eSChristoph Hellwig struct iomap_page {
317d636676SMatthew Wilcox (Oracle) 	atomic_t		read_bytes_pending;
320fb2d720SMatthew Wilcox (Oracle) 	atomic_t		write_bytes_pending;
331cea335dSChristoph Hellwig 	spinlock_t		uptodate_lock;
340a195b91SMatthew Wilcox (Oracle) 	unsigned long		uptodate[];
35ab08b01eSChristoph Hellwig };
36ab08b01eSChristoph Hellwig 
3795c4cd05SMatthew Wilcox (Oracle) static inline struct iomap_page *to_iomap_page(struct folio *folio)
38ab08b01eSChristoph Hellwig {
3995c4cd05SMatthew Wilcox (Oracle) 	if (folio_test_private(folio))
4095c4cd05SMatthew Wilcox (Oracle) 		return folio_get_private(folio);
41ab08b01eSChristoph Hellwig 	return NULL;
42ab08b01eSChristoph Hellwig }
43ab08b01eSChristoph Hellwig 
44598ecfbaSChristoph Hellwig static struct bio_set iomap_ioend_bioset;
45598ecfbaSChristoph Hellwig 
46afc51aaaSDarrick J. Wong static struct iomap_page *
479753b868SStefan Roesch iomap_page_create(struct inode *inode, struct folio *folio, unsigned int flags)
48afc51aaaSDarrick J. Wong {
4995c4cd05SMatthew Wilcox (Oracle) 	struct iomap_page *iop = to_iomap_page(folio);
50435d44b3SMatthew Wilcox (Oracle) 	unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
519753b868SStefan Roesch 	gfp_t gfp;
52afc51aaaSDarrick J. Wong 
530a195b91SMatthew Wilcox (Oracle) 	if (iop || nr_blocks <= 1)
54afc51aaaSDarrick J. Wong 		return iop;
55afc51aaaSDarrick J. Wong 
569753b868SStefan Roesch 	if (flags & IOMAP_NOWAIT)
579753b868SStefan Roesch 		gfp = GFP_NOWAIT;
589753b868SStefan Roesch 	else
599753b868SStefan Roesch 		gfp = GFP_NOFS | __GFP_NOFAIL;
609753b868SStefan Roesch 
610a195b91SMatthew Wilcox (Oracle) 	iop = kzalloc(struct_size(iop, uptodate, BITS_TO_LONGS(nr_blocks)),
629753b868SStefan Roesch 		      gfp);
639753b868SStefan Roesch 	if (iop) {
641cea335dSChristoph Hellwig 		spin_lock_init(&iop->uptodate_lock);
65435d44b3SMatthew Wilcox (Oracle) 		if (folio_test_uptodate(folio))
664595a298SMatthew Wilcox (Oracle) 			bitmap_fill(iop->uptodate, nr_blocks);
67435d44b3SMatthew Wilcox (Oracle) 		folio_attach_private(folio, iop);
689753b868SStefan Roesch 	}
69afc51aaaSDarrick J. Wong 	return iop;
70afc51aaaSDarrick J. Wong }
71afc51aaaSDarrick J. Wong 
72c46e8324SMatthew Wilcox (Oracle) static void iomap_page_release(struct folio *folio)
73afc51aaaSDarrick J. Wong {
74c46e8324SMatthew Wilcox (Oracle) 	struct iomap_page *iop = folio_detach_private(folio);
75c46e8324SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
76c46e8324SMatthew Wilcox (Oracle) 	unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
77afc51aaaSDarrick J. Wong 
78afc51aaaSDarrick J. Wong 	if (!iop)
79afc51aaaSDarrick J. Wong 		return;
807d636676SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(atomic_read(&iop->read_bytes_pending));
810fb2d720SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(atomic_read(&iop->write_bytes_pending));
820a195b91SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(bitmap_full(iop->uptodate, nr_blocks) !=
83c46e8324SMatthew Wilcox (Oracle) 			folio_test_uptodate(folio));
84afc51aaaSDarrick J. Wong 	kfree(iop);
85afc51aaaSDarrick J. Wong }
86afc51aaaSDarrick J. Wong 
87afc51aaaSDarrick J. Wong /*
88431c0566SMatthew Wilcox (Oracle)  * Calculate the range inside the folio that we actually need to read.
89afc51aaaSDarrick J. Wong  */
90431c0566SMatthew Wilcox (Oracle) static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
91431c0566SMatthew Wilcox (Oracle) 		loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
92afc51aaaSDarrick J. Wong {
93431c0566SMatthew Wilcox (Oracle) 	struct iomap_page *iop = to_iomap_page(folio);
94afc51aaaSDarrick J. Wong 	loff_t orig_pos = *pos;
95afc51aaaSDarrick J. Wong 	loff_t isize = i_size_read(inode);
96afc51aaaSDarrick J. Wong 	unsigned block_bits = inode->i_blkbits;
97afc51aaaSDarrick J. Wong 	unsigned block_size = (1 << block_bits);
98431c0566SMatthew Wilcox (Oracle) 	size_t poff = offset_in_folio(folio, *pos);
99431c0566SMatthew Wilcox (Oracle) 	size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
100afc51aaaSDarrick J. Wong 	unsigned first = poff >> block_bits;
101afc51aaaSDarrick J. Wong 	unsigned last = (poff + plen - 1) >> block_bits;
102afc51aaaSDarrick J. Wong 
103afc51aaaSDarrick J. Wong 	/*
104f1f264b4SAndreas Gruenbacher 	 * If the block size is smaller than the page size, we need to check the
105afc51aaaSDarrick J. Wong 	 * per-block uptodate status and adjust the offset and length if needed
106afc51aaaSDarrick J. Wong 	 * to avoid reading in already uptodate ranges.
107afc51aaaSDarrick J. Wong 	 */
108afc51aaaSDarrick J. Wong 	if (iop) {
109afc51aaaSDarrick J. Wong 		unsigned int i;
110afc51aaaSDarrick J. Wong 
111afc51aaaSDarrick J. Wong 		/* move forward for each leading block marked uptodate */
112afc51aaaSDarrick J. Wong 		for (i = first; i <= last; i++) {
113afc51aaaSDarrick J. Wong 			if (!test_bit(i, iop->uptodate))
114afc51aaaSDarrick J. Wong 				break;
115afc51aaaSDarrick J. Wong 			*pos += block_size;
116afc51aaaSDarrick J. Wong 			poff += block_size;
117afc51aaaSDarrick J. Wong 			plen -= block_size;
118afc51aaaSDarrick J. Wong 			first++;
119afc51aaaSDarrick J. Wong 		}
120afc51aaaSDarrick J. Wong 
121afc51aaaSDarrick J. Wong 		/* truncate len if we find any trailing uptodate block(s) */
122afc51aaaSDarrick J. Wong 		for ( ; i <= last; i++) {
123afc51aaaSDarrick J. Wong 			if (test_bit(i, iop->uptodate)) {
124afc51aaaSDarrick J. Wong 				plen -= (last - i + 1) * block_size;
125afc51aaaSDarrick J. Wong 				last = i - 1;
126afc51aaaSDarrick J. Wong 				break;
127afc51aaaSDarrick J. Wong 			}
128afc51aaaSDarrick J. Wong 		}
129afc51aaaSDarrick J. Wong 	}
130afc51aaaSDarrick J. Wong 
131afc51aaaSDarrick J. Wong 	/*
132f1f264b4SAndreas Gruenbacher 	 * If the extent spans the block that contains the i_size, we need to
133afc51aaaSDarrick J. Wong 	 * handle both halves separately so that we properly zero data in the
134afc51aaaSDarrick J. Wong 	 * page cache for blocks that are entirely outside of i_size.
135afc51aaaSDarrick J. Wong 	 */
136afc51aaaSDarrick J. Wong 	if (orig_pos <= isize && orig_pos + length > isize) {
137431c0566SMatthew Wilcox (Oracle) 		unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
138afc51aaaSDarrick J. Wong 
139afc51aaaSDarrick J. Wong 		if (first <= end && last > end)
140afc51aaaSDarrick J. Wong 			plen -= (last - end) * block_size;
141afc51aaaSDarrick J. Wong 	}
142afc51aaaSDarrick J. Wong 
143afc51aaaSDarrick J. Wong 	*offp = poff;
144afc51aaaSDarrick J. Wong 	*lenp = plen;
145afc51aaaSDarrick J. Wong }
146afc51aaaSDarrick J. Wong 
147431c0566SMatthew Wilcox (Oracle) static void iomap_iop_set_range_uptodate(struct folio *folio,
148431c0566SMatthew Wilcox (Oracle) 		struct iomap_page *iop, size_t off, size_t len)
149afc51aaaSDarrick J. Wong {
150431c0566SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
151afc51aaaSDarrick J. Wong 	unsigned first = off >> inode->i_blkbits;
152afc51aaaSDarrick J. Wong 	unsigned last = (off + len - 1) >> inode->i_blkbits;
1531cea335dSChristoph Hellwig 	unsigned long flags;
154afc51aaaSDarrick J. Wong 
1551cea335dSChristoph Hellwig 	spin_lock_irqsave(&iop->uptodate_lock, flags);
156b21866f5SMatthew Wilcox (Oracle) 	bitmap_set(iop->uptodate, first, last - first + 1);
157431c0566SMatthew Wilcox (Oracle) 	if (bitmap_full(iop->uptodate, i_blocks_per_folio(inode, folio)))
158431c0566SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
1591cea335dSChristoph Hellwig 	spin_unlock_irqrestore(&iop->uptodate_lock, flags);
160afc51aaaSDarrick J. Wong }
161afc51aaaSDarrick J. Wong 
162431c0566SMatthew Wilcox (Oracle) static void iomap_set_range_uptodate(struct folio *folio,
163431c0566SMatthew Wilcox (Oracle) 		struct iomap_page *iop, size_t off, size_t len)
1641cea335dSChristoph Hellwig {
165431c0566SMatthew Wilcox (Oracle) 	if (folio_test_error(folio))
1661cea335dSChristoph Hellwig 		return;
1671cea335dSChristoph Hellwig 
168cd1e5afeSMatthew Wilcox (Oracle) 	if (iop)
169431c0566SMatthew Wilcox (Oracle) 		iomap_iop_set_range_uptodate(folio, iop, off, len);
1701cea335dSChristoph Hellwig 	else
171431c0566SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
172afc51aaaSDarrick J. Wong }
173afc51aaaSDarrick J. Wong 
1748ffd74e9SMatthew Wilcox (Oracle) static void iomap_finish_folio_read(struct folio *folio, size_t offset,
1758ffd74e9SMatthew Wilcox (Oracle) 		size_t len, int error)
176afc51aaaSDarrick J. Wong {
17795c4cd05SMatthew Wilcox (Oracle) 	struct iomap_page *iop = to_iomap_page(folio);
178afc51aaaSDarrick J. Wong 
179afc51aaaSDarrick J. Wong 	if (unlikely(error)) {
1808ffd74e9SMatthew Wilcox (Oracle) 		folio_clear_uptodate(folio);
1818ffd74e9SMatthew Wilcox (Oracle) 		folio_set_error(folio);
182afc51aaaSDarrick J. Wong 	} else {
183431c0566SMatthew Wilcox (Oracle) 		iomap_set_range_uptodate(folio, iop, offset, len);
184afc51aaaSDarrick J. Wong 	}
185afc51aaaSDarrick J. Wong 
1868ffd74e9SMatthew Wilcox (Oracle) 	if (!iop || atomic_sub_and_test(len, &iop->read_bytes_pending))
1878ffd74e9SMatthew Wilcox (Oracle) 		folio_unlock(folio);
188afc51aaaSDarrick J. Wong }
189afc51aaaSDarrick J. Wong 
1908ffd74e9SMatthew Wilcox (Oracle) static void iomap_read_end_io(struct bio *bio)
191afc51aaaSDarrick J. Wong {
192afc51aaaSDarrick J. Wong 	int error = blk_status_to_errno(bio->bi_status);
1938ffd74e9SMatthew Wilcox (Oracle) 	struct folio_iter fi;
194afc51aaaSDarrick J. Wong 
1958ffd74e9SMatthew Wilcox (Oracle) 	bio_for_each_folio_all(fi, bio)
1968ffd74e9SMatthew Wilcox (Oracle) 		iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error);
197afc51aaaSDarrick J. Wong 	bio_put(bio);
198afc51aaaSDarrick J. Wong }
199afc51aaaSDarrick J. Wong 
200afc51aaaSDarrick J. Wong struct iomap_readpage_ctx {
2013aa9c659SMatthew Wilcox (Oracle) 	struct folio		*cur_folio;
2023aa9c659SMatthew Wilcox (Oracle) 	bool			cur_folio_in_bio;
203afc51aaaSDarrick J. Wong 	struct bio		*bio;
2049d24a13aSMatthew Wilcox (Oracle) 	struct readahead_control *rac;
205afc51aaaSDarrick J. Wong };
206afc51aaaSDarrick J. Wong 
2075ad448ceSAndreas Gruenbacher /**
2085ad448ceSAndreas Gruenbacher  * iomap_read_inline_data - copy inline data into the page cache
2095ad448ceSAndreas Gruenbacher  * @iter: iteration structure
210874628a2SMatthew Wilcox (Oracle)  * @folio: folio to copy to
2115ad448ceSAndreas Gruenbacher  *
212874628a2SMatthew Wilcox (Oracle)  * Copy the inline data in @iter into @folio and zero out the rest of the folio.
2135ad448ceSAndreas Gruenbacher  * Only a single IOMAP_INLINE extent is allowed at the end of each file.
2145ad448ceSAndreas Gruenbacher  * Returns zero for success to complete the read, or the usual negative errno.
2155ad448ceSAndreas Gruenbacher  */
2165ad448ceSAndreas Gruenbacher static int iomap_read_inline_data(const struct iomap_iter *iter,
217874628a2SMatthew Wilcox (Oracle) 		struct folio *folio)
218afc51aaaSDarrick J. Wong {
219cd1e5afeSMatthew Wilcox (Oracle) 	struct iomap_page *iop;
220fad0a1abSChristoph Hellwig 	const struct iomap *iomap = iomap_iter_srcmap(iter);
2211b5c1e36SChristoph Hellwig 	size_t size = i_size_read(iter->inode) - iomap->offset;
222b405435bSMatthew Wilcox (Oracle) 	size_t poff = offset_in_page(iomap->offset);
223431c0566SMatthew Wilcox (Oracle) 	size_t offset = offset_in_folio(folio, iomap->offset);
224afc51aaaSDarrick J. Wong 	void *addr;
225afc51aaaSDarrick J. Wong 
226874628a2SMatthew Wilcox (Oracle) 	if (folio_test_uptodate(folio))
2275ad448ceSAndreas Gruenbacher 		return 0;
228afc51aaaSDarrick J. Wong 
229ae44f9c2SMatthew Wilcox (Oracle) 	if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
230ae44f9c2SMatthew Wilcox (Oracle) 		return -EIO;
23169f4a26cSGao Xiang 	if (WARN_ON_ONCE(size > PAGE_SIZE -
23269f4a26cSGao Xiang 			 offset_in_page(iomap->inline_data)))
23369f4a26cSGao Xiang 		return -EIO;
23469f4a26cSGao Xiang 	if (WARN_ON_ONCE(size > iomap->length))
23569f4a26cSGao Xiang 		return -EIO;
236431c0566SMatthew Wilcox (Oracle) 	if (offset > 0)
2379753b868SStefan Roesch 		iop = iomap_page_create(iter->inode, folio, iter->flags);
238cd1e5afeSMatthew Wilcox (Oracle) 	else
239cd1e5afeSMatthew Wilcox (Oracle) 		iop = to_iomap_page(folio);
240afc51aaaSDarrick J. Wong 
241874628a2SMatthew Wilcox (Oracle) 	addr = kmap_local_folio(folio, offset);
242afc51aaaSDarrick J. Wong 	memcpy(addr, iomap->inline_data, size);
243b405435bSMatthew Wilcox (Oracle) 	memset(addr + size, 0, PAGE_SIZE - poff - size);
244ab069d5fSMatthew Wilcox (Oracle) 	kunmap_local(addr);
245431c0566SMatthew Wilcox (Oracle) 	iomap_set_range_uptodate(folio, iop, offset, PAGE_SIZE - poff);
2465ad448ceSAndreas Gruenbacher 	return 0;
247afc51aaaSDarrick J. Wong }
248afc51aaaSDarrick J. Wong 
249fad0a1abSChristoph Hellwig static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
2501b5c1e36SChristoph Hellwig 		loff_t pos)
251009d8d84SChristoph Hellwig {
252fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
2531b5c1e36SChristoph Hellwig 
2541b5c1e36SChristoph Hellwig 	return srcmap->type != IOMAP_MAPPED ||
2551b5c1e36SChristoph Hellwig 		(srcmap->flags & IOMAP_F_NEW) ||
2561b5c1e36SChristoph Hellwig 		pos >= i_size_read(iter->inode);
257009d8d84SChristoph Hellwig }
258009d8d84SChristoph Hellwig 
259fad0a1abSChristoph Hellwig static loff_t iomap_readpage_iter(const struct iomap_iter *iter,
260f6d48000SChristoph Hellwig 		struct iomap_readpage_ctx *ctx, loff_t offset)
261afc51aaaSDarrick J. Wong {
262fad0a1abSChristoph Hellwig 	const struct iomap *iomap = &iter->iomap;
263f6d48000SChristoph Hellwig 	loff_t pos = iter->pos + offset;
264f6d48000SChristoph Hellwig 	loff_t length = iomap_length(iter) - offset;
2653aa9c659SMatthew Wilcox (Oracle) 	struct folio *folio = ctx->cur_folio;
266637d3375SAndreas Gruenbacher 	struct iomap_page *iop;
267afc51aaaSDarrick J. Wong 	loff_t orig_pos = pos;
268431c0566SMatthew Wilcox (Oracle) 	size_t poff, plen;
269afc51aaaSDarrick J. Wong 	sector_t sector;
270afc51aaaSDarrick J. Wong 
2715ad448ceSAndreas Gruenbacher 	if (iomap->type == IOMAP_INLINE)
272874628a2SMatthew Wilcox (Oracle) 		return iomap_read_inline_data(iter, folio);
273afc51aaaSDarrick J. Wong 
274afc51aaaSDarrick J. Wong 	/* zero post-eof blocks as the page may be mapped */
2759753b868SStefan Roesch 	iop = iomap_page_create(iter->inode, folio, iter->flags);
276431c0566SMatthew Wilcox (Oracle) 	iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen);
277afc51aaaSDarrick J. Wong 	if (plen == 0)
278afc51aaaSDarrick J. Wong 		goto done;
279afc51aaaSDarrick J. Wong 
2801b5c1e36SChristoph Hellwig 	if (iomap_block_needs_zeroing(iter, pos)) {
281431c0566SMatthew Wilcox (Oracle) 		folio_zero_range(folio, poff, plen);
282431c0566SMatthew Wilcox (Oracle) 		iomap_set_range_uptodate(folio, iop, poff, plen);
283afc51aaaSDarrick J. Wong 		goto done;
284afc51aaaSDarrick J. Wong 	}
285afc51aaaSDarrick J. Wong 
2863aa9c659SMatthew Wilcox (Oracle) 	ctx->cur_folio_in_bio = true;
2877d636676SMatthew Wilcox (Oracle) 	if (iop)
2887d636676SMatthew Wilcox (Oracle) 		atomic_add(plen, &iop->read_bytes_pending);
289afc51aaaSDarrick J. Wong 
290afc51aaaSDarrick J. Wong 	sector = iomap_sector(iomap, pos);
291d0364f94SChristoph Hellwig 	if (!ctx->bio ||
292d0364f94SChristoph Hellwig 	    bio_end_sector(ctx->bio) != sector ||
293431c0566SMatthew Wilcox (Oracle) 	    !bio_add_folio(ctx->bio, folio, plen, poff)) {
2943aa9c659SMatthew Wilcox (Oracle) 		gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
295457df33eSMatthew Wilcox (Oracle) 		gfp_t orig_gfp = gfp;
2965f7136dbSMatthew Wilcox (Oracle) 		unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
297afc51aaaSDarrick J. Wong 
298afc51aaaSDarrick J. Wong 		if (ctx->bio)
299afc51aaaSDarrick J. Wong 			submit_bio(ctx->bio);
300afc51aaaSDarrick J. Wong 
3019d24a13aSMatthew Wilcox (Oracle) 		if (ctx->rac) /* same as readahead_gfp_mask */
302afc51aaaSDarrick J. Wong 			gfp |= __GFP_NORETRY | __GFP_NOWARN;
30307888c66SChristoph Hellwig 		ctx->bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs),
30407888c66SChristoph Hellwig 				     REQ_OP_READ, gfp);
305457df33eSMatthew Wilcox (Oracle) 		/*
306457df33eSMatthew Wilcox (Oracle) 		 * If the bio_alloc fails, try it again for a single page to
307457df33eSMatthew Wilcox (Oracle) 		 * avoid having to deal with partial page reads.  This emulates
308f132ab7dSMatthew Wilcox (Oracle) 		 * what do_mpage_read_folio does.
309457df33eSMatthew Wilcox (Oracle) 		 */
31007888c66SChristoph Hellwig 		if (!ctx->bio) {
31107888c66SChristoph Hellwig 			ctx->bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ,
31207888c66SChristoph Hellwig 					     orig_gfp);
31307888c66SChristoph Hellwig 		}
3149d24a13aSMatthew Wilcox (Oracle) 		if (ctx->rac)
315afc51aaaSDarrick J. Wong 			ctx->bio->bi_opf |= REQ_RAHEAD;
316afc51aaaSDarrick J. Wong 		ctx->bio->bi_iter.bi_sector = sector;
317afc51aaaSDarrick J. Wong 		ctx->bio->bi_end_io = iomap_read_end_io;
318431c0566SMatthew Wilcox (Oracle) 		bio_add_folio(ctx->bio, folio, plen, poff);
319afc51aaaSDarrick J. Wong 	}
320431c0566SMatthew Wilcox (Oracle) 
321afc51aaaSDarrick J. Wong done:
322afc51aaaSDarrick J. Wong 	/*
323afc51aaaSDarrick J. Wong 	 * Move the caller beyond our range so that it keeps making progress.
324f1f264b4SAndreas Gruenbacher 	 * For that, we have to include any leading non-uptodate ranges, but
325afc51aaaSDarrick J. Wong 	 * we can skip trailing ones as they will be handled in the next
326afc51aaaSDarrick J. Wong 	 * iteration.
327afc51aaaSDarrick J. Wong 	 */
328afc51aaaSDarrick J. Wong 	return pos - orig_pos + plen;
329afc51aaaSDarrick J. Wong }
330afc51aaaSDarrick J. Wong 
3317479c505SMatthew Wilcox (Oracle) int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops)
332afc51aaaSDarrick J. Wong {
333f6d48000SChristoph Hellwig 	struct iomap_iter iter = {
3343aa9c659SMatthew Wilcox (Oracle) 		.inode		= folio->mapping->host,
3353aa9c659SMatthew Wilcox (Oracle) 		.pos		= folio_pos(folio),
3363aa9c659SMatthew Wilcox (Oracle) 		.len		= folio_size(folio),
337f6d48000SChristoph Hellwig 	};
338f6d48000SChristoph Hellwig 	struct iomap_readpage_ctx ctx = {
3393aa9c659SMatthew Wilcox (Oracle) 		.cur_folio	= folio,
340f6d48000SChristoph Hellwig 	};
341f6d48000SChristoph Hellwig 	int ret;
342afc51aaaSDarrick J. Wong 
3433aa9c659SMatthew Wilcox (Oracle) 	trace_iomap_readpage(iter.inode, 1);
3449e91c572SChristoph Hellwig 
345f6d48000SChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
346f6d48000SChristoph Hellwig 		iter.processed = iomap_readpage_iter(&iter, &ctx, 0);
347f6d48000SChristoph Hellwig 
348f6d48000SChristoph Hellwig 	if (ret < 0)
3493aa9c659SMatthew Wilcox (Oracle) 		folio_set_error(folio);
350afc51aaaSDarrick J. Wong 
351afc51aaaSDarrick J. Wong 	if (ctx.bio) {
352afc51aaaSDarrick J. Wong 		submit_bio(ctx.bio);
3533aa9c659SMatthew Wilcox (Oracle) 		WARN_ON_ONCE(!ctx.cur_folio_in_bio);
354afc51aaaSDarrick J. Wong 	} else {
3553aa9c659SMatthew Wilcox (Oracle) 		WARN_ON_ONCE(ctx.cur_folio_in_bio);
3563aa9c659SMatthew Wilcox (Oracle) 		folio_unlock(folio);
357afc51aaaSDarrick J. Wong 	}
358afc51aaaSDarrick J. Wong 
359afc51aaaSDarrick J. Wong 	/*
3602c69e205SMatthew Wilcox (Oracle) 	 * Just like mpage_readahead and block_read_full_folio, we always
3617479c505SMatthew Wilcox (Oracle) 	 * return 0 and just set the folio error flag on errors.  This
362f1f264b4SAndreas Gruenbacher 	 * should be cleaned up throughout the stack eventually.
363afc51aaaSDarrick J. Wong 	 */
364afc51aaaSDarrick J. Wong 	return 0;
365afc51aaaSDarrick J. Wong }
3667479c505SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_read_folio);
367afc51aaaSDarrick J. Wong 
368fad0a1abSChristoph Hellwig static loff_t iomap_readahead_iter(const struct iomap_iter *iter,
369f6d48000SChristoph Hellwig 		struct iomap_readpage_ctx *ctx)
370afc51aaaSDarrick J. Wong {
371f6d48000SChristoph Hellwig 	loff_t length = iomap_length(iter);
372afc51aaaSDarrick J. Wong 	loff_t done, ret;
373afc51aaaSDarrick J. Wong 
374afc51aaaSDarrick J. Wong 	for (done = 0; done < length; done += ret) {
3753aa9c659SMatthew Wilcox (Oracle) 		if (ctx->cur_folio &&
3763aa9c659SMatthew Wilcox (Oracle) 		    offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) {
3773aa9c659SMatthew Wilcox (Oracle) 			if (!ctx->cur_folio_in_bio)
3783aa9c659SMatthew Wilcox (Oracle) 				folio_unlock(ctx->cur_folio);
3793aa9c659SMatthew Wilcox (Oracle) 			ctx->cur_folio = NULL;
380afc51aaaSDarrick J. Wong 		}
3813aa9c659SMatthew Wilcox (Oracle) 		if (!ctx->cur_folio) {
3823aa9c659SMatthew Wilcox (Oracle) 			ctx->cur_folio = readahead_folio(ctx->rac);
3833aa9c659SMatthew Wilcox (Oracle) 			ctx->cur_folio_in_bio = false;
384afc51aaaSDarrick J. Wong 		}
385f6d48000SChristoph Hellwig 		ret = iomap_readpage_iter(iter, ctx, done);
386d8af404fSAndreas Gruenbacher 		if (ret <= 0)
387d8af404fSAndreas Gruenbacher 			return ret;
388afc51aaaSDarrick J. Wong 	}
389afc51aaaSDarrick J. Wong 
390afc51aaaSDarrick J. Wong 	return done;
391afc51aaaSDarrick J. Wong }
392afc51aaaSDarrick J. Wong 
3939d24a13aSMatthew Wilcox (Oracle) /**
3949d24a13aSMatthew Wilcox (Oracle)  * iomap_readahead - Attempt to read pages from a file.
3959d24a13aSMatthew Wilcox (Oracle)  * @rac: Describes the pages to be read.
3969d24a13aSMatthew Wilcox (Oracle)  * @ops: The operations vector for the filesystem.
3979d24a13aSMatthew Wilcox (Oracle)  *
3989d24a13aSMatthew Wilcox (Oracle)  * This function is for filesystems to call to implement their readahead
3999d24a13aSMatthew Wilcox (Oracle)  * address_space operation.
4009d24a13aSMatthew Wilcox (Oracle)  *
4019d24a13aSMatthew Wilcox (Oracle)  * Context: The @ops callbacks may submit I/O (eg to read the addresses of
4029d24a13aSMatthew Wilcox (Oracle)  * blocks from disc), and may wait for it.  The caller may be trying to
4039d24a13aSMatthew Wilcox (Oracle)  * access a different page, and so sleeping excessively should be avoided.
4049d24a13aSMatthew Wilcox (Oracle)  * It may allocate memory, but should avoid costly allocations.  This
4059d24a13aSMatthew Wilcox (Oracle)  * function is called with memalloc_nofs set, so allocations will not cause
4069d24a13aSMatthew Wilcox (Oracle)  * the filesystem to be reentered.
4079d24a13aSMatthew Wilcox (Oracle)  */
4089d24a13aSMatthew Wilcox (Oracle) void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
409afc51aaaSDarrick J. Wong {
410f6d48000SChristoph Hellwig 	struct iomap_iter iter = {
411f6d48000SChristoph Hellwig 		.inode	= rac->mapping->host,
412f6d48000SChristoph Hellwig 		.pos	= readahead_pos(rac),
413f6d48000SChristoph Hellwig 		.len	= readahead_length(rac),
414f6d48000SChristoph Hellwig 	};
415afc51aaaSDarrick J. Wong 	struct iomap_readpage_ctx ctx = {
4169d24a13aSMatthew Wilcox (Oracle) 		.rac	= rac,
417afc51aaaSDarrick J. Wong 	};
418afc51aaaSDarrick J. Wong 
419f6d48000SChristoph Hellwig 	trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
4209e91c572SChristoph Hellwig 
421f6d48000SChristoph Hellwig 	while (iomap_iter(&iter, ops) > 0)
422f6d48000SChristoph Hellwig 		iter.processed = iomap_readahead_iter(&iter, &ctx);
4239d24a13aSMatthew Wilcox (Oracle) 
424afc51aaaSDarrick J. Wong 	if (ctx.bio)
425afc51aaaSDarrick J. Wong 		submit_bio(ctx.bio);
4263aa9c659SMatthew Wilcox (Oracle) 	if (ctx.cur_folio) {
4273aa9c659SMatthew Wilcox (Oracle) 		if (!ctx.cur_folio_in_bio)
4283aa9c659SMatthew Wilcox (Oracle) 			folio_unlock(ctx.cur_folio);
429afc51aaaSDarrick J. Wong 	}
430afc51aaaSDarrick J. Wong }
4319d24a13aSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_readahead);
432afc51aaaSDarrick J. Wong 
433afc51aaaSDarrick J. Wong /*
4342e7e80f7SMatthew Wilcox (Oracle)  * iomap_is_partially_uptodate checks whether blocks within a folio are
435afc51aaaSDarrick J. Wong  * uptodate or not.
436afc51aaaSDarrick J. Wong  *
4372e7e80f7SMatthew Wilcox (Oracle)  * Returns true if all blocks which correspond to the specified part
4382e7e80f7SMatthew Wilcox (Oracle)  * of the folio are uptodate.
439afc51aaaSDarrick J. Wong  */
4402e7e80f7SMatthew Wilcox (Oracle) bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
441afc51aaaSDarrick J. Wong {
44295c4cd05SMatthew Wilcox (Oracle) 	struct iomap_page *iop = to_iomap_page(folio);
4432e7e80f7SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
4442e7e80f7SMatthew Wilcox (Oracle) 	unsigned first, last, i;
445afc51aaaSDarrick J. Wong 
4462e7e80f7SMatthew Wilcox (Oracle) 	if (!iop)
4472e7e80f7SMatthew Wilcox (Oracle) 		return false;
4482e7e80f7SMatthew Wilcox (Oracle) 
4492756c818SMatthew Wilcox (Oracle) 	/* Caller's range may extend past the end of this folio */
4502756c818SMatthew Wilcox (Oracle) 	count = min(folio_size(folio) - from, count);
451afc51aaaSDarrick J. Wong 
4522756c818SMatthew Wilcox (Oracle) 	/* First and last blocks in range within folio */
453afc51aaaSDarrick J. Wong 	first = from >> inode->i_blkbits;
4542756c818SMatthew Wilcox (Oracle) 	last = (from + count - 1) >> inode->i_blkbits;
455afc51aaaSDarrick J. Wong 
456afc51aaaSDarrick J. Wong 	for (i = first; i <= last; i++)
457afc51aaaSDarrick J. Wong 		if (!test_bit(i, iop->uptodate))
4582e7e80f7SMatthew Wilcox (Oracle) 			return false;
4592e7e80f7SMatthew Wilcox (Oracle) 	return true;
460afc51aaaSDarrick J. Wong }
461afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
462afc51aaaSDarrick J. Wong 
4638597447dSMatthew Wilcox (Oracle) bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags)
464afc51aaaSDarrick J. Wong {
4658597447dSMatthew Wilcox (Oracle) 	trace_iomap_release_folio(folio->mapping->host, folio_pos(folio),
46639f16c83SMatthew Wilcox (Oracle) 			folio_size(folio));
4679e91c572SChristoph Hellwig 
468afc51aaaSDarrick J. Wong 	/*
4698597447dSMatthew Wilcox (Oracle) 	 * mm accommodates an old ext3 case where clean folios might
4708597447dSMatthew Wilcox (Oracle) 	 * not have had the dirty bit cleared.  Thus, it can send actual
4718597447dSMatthew Wilcox (Oracle) 	 * dirty folios to ->release_folio() via shrink_active_list();
4728597447dSMatthew Wilcox (Oracle) 	 * skip those here.
473afc51aaaSDarrick J. Wong 	 */
47439f16c83SMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio) || folio_test_writeback(folio))
4758597447dSMatthew Wilcox (Oracle) 		return false;
476c46e8324SMatthew Wilcox (Oracle) 	iomap_page_release(folio);
4778597447dSMatthew Wilcox (Oracle) 	return true;
478afc51aaaSDarrick J. Wong }
4798597447dSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_release_folio);
480afc51aaaSDarrick J. Wong 
4818306a5f5SMatthew Wilcox (Oracle) void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
482afc51aaaSDarrick J. Wong {
483d82354f6SMatthew Wilcox (Oracle) 	trace_iomap_invalidate_folio(folio->mapping->host,
4841241ebecSMatthew Wilcox (Oracle) 					folio_pos(folio) + offset, len);
4859e91c572SChristoph Hellwig 
486afc51aaaSDarrick J. Wong 	/*
48760d82310SMatthew Wilcox (Oracle) 	 * If we're invalidating the entire folio, clear the dirty state
48860d82310SMatthew Wilcox (Oracle) 	 * from it and release it to avoid unnecessary buildup of the LRU.
489afc51aaaSDarrick J. Wong 	 */
4908306a5f5SMatthew Wilcox (Oracle) 	if (offset == 0 && len == folio_size(folio)) {
4918306a5f5SMatthew Wilcox (Oracle) 		WARN_ON_ONCE(folio_test_writeback(folio));
4928306a5f5SMatthew Wilcox (Oracle) 		folio_cancel_dirty(folio);
493c46e8324SMatthew Wilcox (Oracle) 		iomap_page_release(folio);
49460d82310SMatthew Wilcox (Oracle) 	} else if (folio_test_large(folio)) {
49560d82310SMatthew Wilcox (Oracle) 		/* Must release the iop so the page can be split */
49660d82310SMatthew Wilcox (Oracle) 		WARN_ON_ONCE(!folio_test_uptodate(folio) &&
49760d82310SMatthew Wilcox (Oracle) 			     folio_test_dirty(folio));
49860d82310SMatthew Wilcox (Oracle) 		iomap_page_release(folio);
499afc51aaaSDarrick J. Wong 	}
500afc51aaaSDarrick J. Wong }
5018306a5f5SMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
5028306a5f5SMatthew Wilcox (Oracle) 
503afc51aaaSDarrick J. Wong #ifdef CONFIG_MIGRATION
504afc51aaaSDarrick J. Wong int
505afc51aaaSDarrick J. Wong iomap_migrate_page(struct address_space *mapping, struct page *newpage,
506afc51aaaSDarrick J. Wong 		struct page *page, enum migrate_mode mode)
507afc51aaaSDarrick J. Wong {
508589110e8SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
509589110e8SMatthew Wilcox (Oracle) 	struct folio *newfolio = page_folio(newpage);
510afc51aaaSDarrick J. Wong 	int ret;
511afc51aaaSDarrick J. Wong 
512589110e8SMatthew Wilcox (Oracle) 	ret = folio_migrate_mapping(mapping, newfolio, folio, 0);
513afc51aaaSDarrick J. Wong 	if (ret != MIGRATEPAGE_SUCCESS)
514afc51aaaSDarrick J. Wong 		return ret;
515afc51aaaSDarrick J. Wong 
516589110e8SMatthew Wilcox (Oracle) 	if (folio_test_private(folio))
517589110e8SMatthew Wilcox (Oracle) 		folio_attach_private(newfolio, folio_detach_private(folio));
518afc51aaaSDarrick J. Wong 
519afc51aaaSDarrick J. Wong 	if (mode != MIGRATE_SYNC_NO_COPY)
520589110e8SMatthew Wilcox (Oracle) 		folio_migrate_copy(newfolio, folio);
521afc51aaaSDarrick J. Wong 	else
522589110e8SMatthew Wilcox (Oracle) 		folio_migrate_flags(newfolio, folio);
523afc51aaaSDarrick J. Wong 	return MIGRATEPAGE_SUCCESS;
524afc51aaaSDarrick J. Wong }
525afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_migrate_page);
526afc51aaaSDarrick J. Wong #endif /* CONFIG_MIGRATION */
527afc51aaaSDarrick J. Wong 
528afc51aaaSDarrick J. Wong static void
529afc51aaaSDarrick J. Wong iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
530afc51aaaSDarrick J. Wong {
531afc51aaaSDarrick J. Wong 	loff_t i_size = i_size_read(inode);
532afc51aaaSDarrick J. Wong 
533afc51aaaSDarrick J. Wong 	/*
534afc51aaaSDarrick J. Wong 	 * Only truncate newly allocated pages beyoned EOF, even if the
535afc51aaaSDarrick J. Wong 	 * write started inside the existing inode size.
536afc51aaaSDarrick J. Wong 	 */
537afc51aaaSDarrick J. Wong 	if (pos + len > i_size)
538b71450e2SAndreas Gruenbacher 		truncate_pagecache_range(inode, max(pos, i_size),
539b71450e2SAndreas Gruenbacher 					 pos + len - 1);
540afc51aaaSDarrick J. Wong }
541afc51aaaSDarrick J. Wong 
542431c0566SMatthew Wilcox (Oracle) static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
543431c0566SMatthew Wilcox (Oracle) 		size_t poff, size_t plen, const struct iomap *iomap)
544afc51aaaSDarrick J. Wong {
545afc51aaaSDarrick J. Wong 	struct bio_vec bvec;
546afc51aaaSDarrick J. Wong 	struct bio bio;
547afc51aaaSDarrick J. Wong 
54849add496SChristoph Hellwig 	bio_init(&bio, iomap->bdev, &bvec, 1, REQ_OP_READ);
549afc51aaaSDarrick J. Wong 	bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
550431c0566SMatthew Wilcox (Oracle) 	bio_add_folio(&bio, folio, plen, poff);
551afc51aaaSDarrick J. Wong 	return submit_bio_wait(&bio);
552afc51aaaSDarrick J. Wong }
553afc51aaaSDarrick J. Wong 
554fad0a1abSChristoph Hellwig static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
555bc6123a8SMatthew Wilcox (Oracle) 		size_t len, struct folio *folio)
556afc51aaaSDarrick J. Wong {
557fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
5589753b868SStefan Roesch 	struct iomap_page *iop;
5591b5c1e36SChristoph Hellwig 	loff_t block_size = i_blocksize(iter->inode);
5606cc19c5fSNikolay Borisov 	loff_t block_start = round_down(pos, block_size);
5616cc19c5fSNikolay Borisov 	loff_t block_end = round_up(pos + len, block_size);
562*cae2de69SStefan Roesch 	unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio);
563431c0566SMatthew Wilcox (Oracle) 	size_t from = offset_in_folio(folio, pos), to = from + len;
564431c0566SMatthew Wilcox (Oracle) 	size_t poff, plen;
565afc51aaaSDarrick J. Wong 
566431c0566SMatthew Wilcox (Oracle) 	if (folio_test_uptodate(folio))
567afc51aaaSDarrick J. Wong 		return 0;
568431c0566SMatthew Wilcox (Oracle) 	folio_clear_error(folio);
569afc51aaaSDarrick J. Wong 
5709753b868SStefan Roesch 	iop = iomap_page_create(iter->inode, folio, iter->flags);
571*cae2de69SStefan Roesch 	if ((iter->flags & IOMAP_NOWAIT) && !iop && nr_blocks > 1)
572*cae2de69SStefan Roesch 		return -EAGAIN;
5739753b868SStefan Roesch 
574afc51aaaSDarrick J. Wong 	do {
575431c0566SMatthew Wilcox (Oracle) 		iomap_adjust_read_range(iter->inode, folio, &block_start,
576afc51aaaSDarrick J. Wong 				block_end - block_start, &poff, &plen);
577afc51aaaSDarrick J. Wong 		if (plen == 0)
578afc51aaaSDarrick J. Wong 			break;
579afc51aaaSDarrick J. Wong 
580b74b1293SChristoph Hellwig 		if (!(iter->flags & IOMAP_UNSHARE) &&
58132a38a49SChristoph Hellwig 		    (from <= poff || from >= poff + plen) &&
582d3b40439SChristoph Hellwig 		    (to <= poff || to >= poff + plen))
583d3b40439SChristoph Hellwig 			continue;
584d3b40439SChristoph Hellwig 
5851b5c1e36SChristoph Hellwig 		if (iomap_block_needs_zeroing(iter, block_start)) {
586b74b1293SChristoph Hellwig 			if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
58732a38a49SChristoph Hellwig 				return -EIO;
588431c0566SMatthew Wilcox (Oracle) 			folio_zero_segments(folio, poff, from, to, poff + plen);
58914284fedSMatthew Wilcox (Oracle) 		} else {
590*cae2de69SStefan Roesch 			int status;
591*cae2de69SStefan Roesch 
592*cae2de69SStefan Roesch 			if (iter->flags & IOMAP_NOWAIT)
593*cae2de69SStefan Roesch 				return -EAGAIN;
594*cae2de69SStefan Roesch 
595*cae2de69SStefan Roesch 			status = iomap_read_folio_sync(block_start, folio,
59614284fedSMatthew Wilcox (Oracle) 					poff, plen, srcmap);
597d3b40439SChristoph Hellwig 			if (status)
598d3b40439SChristoph Hellwig 				return status;
59914284fedSMatthew Wilcox (Oracle) 		}
600431c0566SMatthew Wilcox (Oracle) 		iomap_set_range_uptodate(folio, iop, poff, plen);
601afc51aaaSDarrick J. Wong 	} while ((block_start += plen) < block_end);
602afc51aaaSDarrick J. Wong 
603d3b40439SChristoph Hellwig 	return 0;
604afc51aaaSDarrick J. Wong }
605afc51aaaSDarrick J. Wong 
606fad0a1abSChristoph Hellwig static int iomap_write_begin_inline(const struct iomap_iter *iter,
607bc6123a8SMatthew Wilcox (Oracle) 		struct folio *folio)
60869f4a26cSGao Xiang {
60969f4a26cSGao Xiang 	/* needs more work for the tailpacking case; disable for now */
6101b5c1e36SChristoph Hellwig 	if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
61169f4a26cSGao Xiang 		return -EIO;
612874628a2SMatthew Wilcox (Oracle) 	return iomap_read_inline_data(iter, folio);
61369f4a26cSGao Xiang }
61469f4a26cSGao Xiang 
615fad0a1abSChristoph Hellwig static int iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
616bc6123a8SMatthew Wilcox (Oracle) 		size_t len, struct folio **foliop)
617afc51aaaSDarrick J. Wong {
6181b5c1e36SChristoph Hellwig 	const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
619fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
620d1bd0b4eSMatthew Wilcox (Oracle) 	struct folio *folio;
621bc6123a8SMatthew Wilcox (Oracle) 	unsigned fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE | FGP_NOFS;
622afc51aaaSDarrick J. Wong 	int status = 0;
623afc51aaaSDarrick J. Wong 
624*cae2de69SStefan Roesch 	if (iter->flags & IOMAP_NOWAIT)
625*cae2de69SStefan Roesch 		fgp |= FGP_NOWAIT;
626*cae2de69SStefan Roesch 
6271b5c1e36SChristoph Hellwig 	BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
6281b5c1e36SChristoph Hellwig 	if (srcmap != &iter->iomap)
629c039b997SGoldwyn Rodrigues 		BUG_ON(pos + len > srcmap->offset + srcmap->length);
630afc51aaaSDarrick J. Wong 
631afc51aaaSDarrick J. Wong 	if (fatal_signal_pending(current))
632afc51aaaSDarrick J. Wong 		return -EINTR;
633afc51aaaSDarrick J. Wong 
634d454ab82SMatthew Wilcox (Oracle) 	if (!mapping_large_folio_support(iter->inode->i_mapping))
635d454ab82SMatthew Wilcox (Oracle) 		len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
636d454ab82SMatthew Wilcox (Oracle) 
637afc51aaaSDarrick J. Wong 	if (page_ops && page_ops->page_prepare) {
6381b5c1e36SChristoph Hellwig 		status = page_ops->page_prepare(iter->inode, pos, len);
639afc51aaaSDarrick J. Wong 		if (status)
640afc51aaaSDarrick J. Wong 			return status;
641afc51aaaSDarrick J. Wong 	}
642afc51aaaSDarrick J. Wong 
643bc6123a8SMatthew Wilcox (Oracle) 	folio = __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
644bc6123a8SMatthew Wilcox (Oracle) 			fgp, mapping_gfp_mask(iter->inode->i_mapping));
645bc6123a8SMatthew Wilcox (Oracle) 	if (!folio) {
646*cae2de69SStefan Roesch 		status = (iter->flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOMEM;
647afc51aaaSDarrick J. Wong 		goto out_no_page;
648afc51aaaSDarrick J. Wong 	}
649d454ab82SMatthew Wilcox (Oracle) 	if (pos + len > folio_pos(folio) + folio_size(folio))
650d454ab82SMatthew Wilcox (Oracle) 		len = folio_pos(folio) + folio_size(folio) - pos;
651afc51aaaSDarrick J. Wong 
652c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_INLINE)
653bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_begin_inline(iter, folio);
6541b5c1e36SChristoph Hellwig 	else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
655d1bd0b4eSMatthew Wilcox (Oracle) 		status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
656afc51aaaSDarrick J. Wong 	else
657bc6123a8SMatthew Wilcox (Oracle) 		status = __iomap_write_begin(iter, pos, len, folio);
658afc51aaaSDarrick J. Wong 
659afc51aaaSDarrick J. Wong 	if (unlikely(status))
660afc51aaaSDarrick J. Wong 		goto out_unlock;
661afc51aaaSDarrick J. Wong 
662bc6123a8SMatthew Wilcox (Oracle) 	*foliop = folio;
663afc51aaaSDarrick J. Wong 	return 0;
664afc51aaaSDarrick J. Wong 
665afc51aaaSDarrick J. Wong out_unlock:
666bc6123a8SMatthew Wilcox (Oracle) 	folio_unlock(folio);
667bc6123a8SMatthew Wilcox (Oracle) 	folio_put(folio);
6681b5c1e36SChristoph Hellwig 	iomap_write_failed(iter->inode, pos, len);
669afc51aaaSDarrick J. Wong 
670afc51aaaSDarrick J. Wong out_no_page:
671afc51aaaSDarrick J. Wong 	if (page_ops && page_ops->page_done)
6721b5c1e36SChristoph Hellwig 		page_ops->page_done(iter->inode, pos, 0, NULL);
673afc51aaaSDarrick J. Wong 	return status;
674afc51aaaSDarrick J. Wong }
675afc51aaaSDarrick J. Wong 
676e25ba8cbSMatthew Wilcox (Oracle) static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
677bc6123a8SMatthew Wilcox (Oracle) 		size_t copied, struct folio *folio)
678afc51aaaSDarrick J. Wong {
679cd1e5afeSMatthew Wilcox (Oracle) 	struct iomap_page *iop = to_iomap_page(folio);
680bc6123a8SMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
681afc51aaaSDarrick J. Wong 
682afc51aaaSDarrick J. Wong 	/*
683afc51aaaSDarrick J. Wong 	 * The blocks that were entirely written will now be uptodate, so we
6847479c505SMatthew Wilcox (Oracle) 	 * don't have to worry about a read_folio reading them and overwriting a
685f1f264b4SAndreas Gruenbacher 	 * partial write.  However, if we've encountered a short write and only
686afc51aaaSDarrick J. Wong 	 * partially written into a block, it will not be marked uptodate, so a
6877479c505SMatthew Wilcox (Oracle) 	 * read_folio might come in and destroy our partial write.
688afc51aaaSDarrick J. Wong 	 *
689f1f264b4SAndreas Gruenbacher 	 * Do the simplest thing and just treat any short write to a
690f1f264b4SAndreas Gruenbacher 	 * non-uptodate page as a zero-length write, and force the caller to
691f1f264b4SAndreas Gruenbacher 	 * redo the whole thing.
692afc51aaaSDarrick J. Wong 	 */
693bc6123a8SMatthew Wilcox (Oracle) 	if (unlikely(copied < len && !folio_test_uptodate(folio)))
694afc51aaaSDarrick J. Wong 		return 0;
695431c0566SMatthew Wilcox (Oracle) 	iomap_set_range_uptodate(folio, iop, offset_in_folio(folio, pos), len);
696bc6123a8SMatthew Wilcox (Oracle) 	filemap_dirty_folio(inode->i_mapping, folio);
697afc51aaaSDarrick J. Wong 	return copied;
698afc51aaaSDarrick J. Wong }
699afc51aaaSDarrick J. Wong 
700fad0a1abSChristoph Hellwig static size_t iomap_write_end_inline(const struct iomap_iter *iter,
7019c4ce08dSMatthew Wilcox (Oracle) 		struct folio *folio, loff_t pos, size_t copied)
702afc51aaaSDarrick J. Wong {
703fad0a1abSChristoph Hellwig 	const struct iomap *iomap = &iter->iomap;
704afc51aaaSDarrick J. Wong 	void *addr;
705afc51aaaSDarrick J. Wong 
7069c4ce08dSMatthew Wilcox (Oracle) 	WARN_ON_ONCE(!folio_test_uptodate(folio));
70769f4a26cSGao Xiang 	BUG_ON(!iomap_inline_data_valid(iomap));
708afc51aaaSDarrick J. Wong 
7099c4ce08dSMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
7109c4ce08dSMatthew Wilcox (Oracle) 	addr = kmap_local_folio(folio, pos);
711ab069d5fSMatthew Wilcox (Oracle) 	memcpy(iomap_inline_data(iomap, pos), addr, copied);
712ab069d5fSMatthew Wilcox (Oracle) 	kunmap_local(addr);
713afc51aaaSDarrick J. Wong 
7141b5c1e36SChristoph Hellwig 	mark_inode_dirty(iter->inode);
715afc51aaaSDarrick J. Wong 	return copied;
716afc51aaaSDarrick J. Wong }
717afc51aaaSDarrick J. Wong 
718e25ba8cbSMatthew Wilcox (Oracle) /* Returns the number of bytes copied.  May be 0.  Cannot be an errno. */
7191b5c1e36SChristoph Hellwig static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len,
720bc6123a8SMatthew Wilcox (Oracle) 		size_t copied, struct folio *folio)
721afc51aaaSDarrick J. Wong {
7221b5c1e36SChristoph Hellwig 	const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
723fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
7241b5c1e36SChristoph Hellwig 	loff_t old_size = iter->inode->i_size;
725e25ba8cbSMatthew Wilcox (Oracle) 	size_t ret;
726afc51aaaSDarrick J. Wong 
727c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_INLINE) {
7289c4ce08dSMatthew Wilcox (Oracle) 		ret = iomap_write_end_inline(iter, folio, pos, copied);
729c039b997SGoldwyn Rodrigues 	} else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
7301b5c1e36SChristoph Hellwig 		ret = block_write_end(NULL, iter->inode->i_mapping, pos, len,
731bc6123a8SMatthew Wilcox (Oracle) 				copied, &folio->page, NULL);
732afc51aaaSDarrick J. Wong 	} else {
733bc6123a8SMatthew Wilcox (Oracle) 		ret = __iomap_write_end(iter->inode, pos, len, copied, folio);
734afc51aaaSDarrick J. Wong 	}
735afc51aaaSDarrick J. Wong 
736afc51aaaSDarrick J. Wong 	/*
737afc51aaaSDarrick J. Wong 	 * Update the in-memory inode size after copying the data into the page
738afc51aaaSDarrick J. Wong 	 * cache.  It's up to the file system to write the updated size to disk,
739afc51aaaSDarrick J. Wong 	 * preferably after I/O completion so that no stale data is exposed.
740afc51aaaSDarrick J. Wong 	 */
741afc51aaaSDarrick J. Wong 	if (pos + ret > old_size) {
7421b5c1e36SChristoph Hellwig 		i_size_write(iter->inode, pos + ret);
7431b5c1e36SChristoph Hellwig 		iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
744afc51aaaSDarrick J. Wong 	}
745bc6123a8SMatthew Wilcox (Oracle) 	folio_unlock(folio);
746afc51aaaSDarrick J. Wong 
747afc51aaaSDarrick J. Wong 	if (old_size < pos)
7481b5c1e36SChristoph Hellwig 		pagecache_isize_extended(iter->inode, old_size, pos);
749afc51aaaSDarrick J. Wong 	if (page_ops && page_ops->page_done)
750bc6123a8SMatthew Wilcox (Oracle) 		page_ops->page_done(iter->inode, pos, ret, &folio->page);
751bc6123a8SMatthew Wilcox (Oracle) 	folio_put(folio);
752afc51aaaSDarrick J. Wong 
753afc51aaaSDarrick J. Wong 	if (ret < len)
754d74999c8SAndreas Gruenbacher 		iomap_write_failed(iter->inode, pos + ret, len - ret);
755afc51aaaSDarrick J. Wong 	return ret;
756afc51aaaSDarrick J. Wong }
757afc51aaaSDarrick J. Wong 
758ce83a025SChristoph Hellwig static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
759afc51aaaSDarrick J. Wong {
760ce83a025SChristoph Hellwig 	loff_t length = iomap_length(iter);
761ce83a025SChristoph Hellwig 	loff_t pos = iter->pos;
762afc51aaaSDarrick J. Wong 	ssize_t written = 0;
763ce83a025SChristoph Hellwig 	long status = 0;
764*cae2de69SStefan Roesch 	struct address_space *mapping = iter->inode->i_mapping;
765*cae2de69SStefan Roesch 	unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0;
766afc51aaaSDarrick J. Wong 
767afc51aaaSDarrick J. Wong 	do {
768bc6123a8SMatthew Wilcox (Oracle) 		struct folio *folio;
769afc51aaaSDarrick J. Wong 		struct page *page;
770afc51aaaSDarrick J. Wong 		unsigned long offset;	/* Offset into pagecache page */
771afc51aaaSDarrick J. Wong 		unsigned long bytes;	/* Bytes to write to page */
772afc51aaaSDarrick J. Wong 		size_t copied;		/* Bytes copied from user */
773afc51aaaSDarrick J. Wong 
774afc51aaaSDarrick J. Wong 		offset = offset_in_page(pos);
775afc51aaaSDarrick J. Wong 		bytes = min_t(unsigned long, PAGE_SIZE - offset,
776afc51aaaSDarrick J. Wong 						iov_iter_count(i));
777afc51aaaSDarrick J. Wong again:
778*cae2de69SStefan Roesch 		status = balance_dirty_pages_ratelimited_flags(mapping,
779*cae2de69SStefan Roesch 							       bdp_flags);
780*cae2de69SStefan Roesch 		if (unlikely(status))
781*cae2de69SStefan Roesch 			break;
782*cae2de69SStefan Roesch 
783afc51aaaSDarrick J. Wong 		if (bytes > length)
784afc51aaaSDarrick J. Wong 			bytes = length;
785afc51aaaSDarrick J. Wong 
786afc51aaaSDarrick J. Wong 		/*
787f1f264b4SAndreas Gruenbacher 		 * Bring in the user page that we'll copy from _first_.
788afc51aaaSDarrick J. Wong 		 * Otherwise there's a nasty deadlock on copying from the
789afc51aaaSDarrick J. Wong 		 * same page as we're writing to, without it being marked
790afc51aaaSDarrick J. Wong 		 * up-to-date.
791*cae2de69SStefan Roesch 		 *
792*cae2de69SStefan Roesch 		 * For async buffered writes the assumption is that the user
793*cae2de69SStefan Roesch 		 * page has already been faulted in. This can be optimized by
794*cae2de69SStefan Roesch 		 * faulting the user page.
795afc51aaaSDarrick J. Wong 		 */
796631f871fSAndreas Gruenbacher 		if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
797afc51aaaSDarrick J. Wong 			status = -EFAULT;
798afc51aaaSDarrick J. Wong 			break;
799afc51aaaSDarrick J. Wong 		}
800afc51aaaSDarrick J. Wong 
801bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_begin(iter, pos, bytes, &folio);
802afc51aaaSDarrick J. Wong 		if (unlikely(status))
803afc51aaaSDarrick J. Wong 			break;
804afc51aaaSDarrick J. Wong 
805bc6123a8SMatthew Wilcox (Oracle) 		page = folio_file_page(folio, pos >> PAGE_SHIFT);
806*cae2de69SStefan Roesch 		if (mapping_writably_mapped(mapping))
807afc51aaaSDarrick J. Wong 			flush_dcache_page(page);
808afc51aaaSDarrick J. Wong 
809f0b65f39SAl Viro 		copied = copy_page_from_iter_atomic(page, offset, bytes, i);
810afc51aaaSDarrick J. Wong 
811bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_end(iter, pos, bytes, copied, folio);
812afc51aaaSDarrick J. Wong 
813f0b65f39SAl Viro 		if (unlikely(copied != status))
814f0b65f39SAl Viro 			iov_iter_revert(i, copied - status);
815afc51aaaSDarrick J. Wong 
816f0b65f39SAl Viro 		cond_resched();
817bc1bb416SAl Viro 		if (unlikely(status == 0)) {
818afc51aaaSDarrick J. Wong 			/*
819bc1bb416SAl Viro 			 * A short copy made iomap_write_end() reject the
820bc1bb416SAl Viro 			 * thing entirely.  Might be memory poisoning
821bc1bb416SAl Viro 			 * halfway through, might be a race with munmap,
822bc1bb416SAl Viro 			 * might be severe memory pressure.
823afc51aaaSDarrick J. Wong 			 */
824bc1bb416SAl Viro 			if (copied)
825bc1bb416SAl Viro 				bytes = copied;
826afc51aaaSDarrick J. Wong 			goto again;
827afc51aaaSDarrick J. Wong 		}
828f0b65f39SAl Viro 		pos += status;
829f0b65f39SAl Viro 		written += status;
830f0b65f39SAl Viro 		length -= status;
831afc51aaaSDarrick J. Wong 	} while (iov_iter_count(i) && length);
832afc51aaaSDarrick J. Wong 
833afc51aaaSDarrick J. Wong 	return written ? written : status;
834afc51aaaSDarrick J. Wong }
835afc51aaaSDarrick J. Wong 
836afc51aaaSDarrick J. Wong ssize_t
837ce83a025SChristoph Hellwig iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
838afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
839afc51aaaSDarrick J. Wong {
840ce83a025SChristoph Hellwig 	struct iomap_iter iter = {
841ce83a025SChristoph Hellwig 		.inode		= iocb->ki_filp->f_mapping->host,
842ce83a025SChristoph Hellwig 		.pos		= iocb->ki_pos,
843ce83a025SChristoph Hellwig 		.len		= iov_iter_count(i),
844ce83a025SChristoph Hellwig 		.flags		= IOMAP_WRITE,
845ce83a025SChristoph Hellwig 	};
846ce83a025SChristoph Hellwig 	int ret;
847afc51aaaSDarrick J. Wong 
848*cae2de69SStefan Roesch 	if (iocb->ki_flags & IOCB_NOWAIT)
849*cae2de69SStefan Roesch 		iter.flags |= IOMAP_NOWAIT;
850*cae2de69SStefan Roesch 
851ce83a025SChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
852ce83a025SChristoph Hellwig 		iter.processed = iomap_write_iter(&iter, i);
853ce83a025SChristoph Hellwig 	if (iter.pos == iocb->ki_pos)
854ce83a025SChristoph Hellwig 		return ret;
855ce83a025SChristoph Hellwig 	return iter.pos - iocb->ki_pos;
856afc51aaaSDarrick J. Wong }
857afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
858afc51aaaSDarrick J. Wong 
8598fc274d1SChristoph Hellwig static loff_t iomap_unshare_iter(struct iomap_iter *iter)
860afc51aaaSDarrick J. Wong {
8618fc274d1SChristoph Hellwig 	struct iomap *iomap = &iter->iomap;
862fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
8638fc274d1SChristoph Hellwig 	loff_t pos = iter->pos;
8648fc274d1SChristoph Hellwig 	loff_t length = iomap_length(iter);
865afc51aaaSDarrick J. Wong 	long status = 0;
866d4ff3b2eSMatthew Wilcox (Oracle) 	loff_t written = 0;
867afc51aaaSDarrick J. Wong 
8683590c4d8SChristoph Hellwig 	/* don't bother with blocks that are not shared to start with */
8693590c4d8SChristoph Hellwig 	if (!(iomap->flags & IOMAP_F_SHARED))
8703590c4d8SChristoph Hellwig 		return length;
8713590c4d8SChristoph Hellwig 	/* don't bother with holes or unwritten extents */
872c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
8733590c4d8SChristoph Hellwig 		return length;
8743590c4d8SChristoph Hellwig 
875afc51aaaSDarrick J. Wong 	do {
87632a38a49SChristoph Hellwig 		unsigned long offset = offset_in_page(pos);
87732a38a49SChristoph Hellwig 		unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length);
878bc6123a8SMatthew Wilcox (Oracle) 		struct folio *folio;
879afc51aaaSDarrick J. Wong 
880bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_begin(iter, pos, bytes, &folio);
881afc51aaaSDarrick J. Wong 		if (unlikely(status))
882afc51aaaSDarrick J. Wong 			return status;
883afc51aaaSDarrick J. Wong 
884bc6123a8SMatthew Wilcox (Oracle) 		status = iomap_write_end(iter, pos, bytes, bytes, folio);
885afc51aaaSDarrick J. Wong 		if (WARN_ON_ONCE(status == 0))
886afc51aaaSDarrick J. Wong 			return -EIO;
887afc51aaaSDarrick J. Wong 
888afc51aaaSDarrick J. Wong 		cond_resched();
889afc51aaaSDarrick J. Wong 
890afc51aaaSDarrick J. Wong 		pos += status;
891afc51aaaSDarrick J. Wong 		written += status;
892afc51aaaSDarrick J. Wong 		length -= status;
893afc51aaaSDarrick J. Wong 
8948fc274d1SChristoph Hellwig 		balance_dirty_pages_ratelimited(iter->inode->i_mapping);
895afc51aaaSDarrick J. Wong 	} while (length);
896afc51aaaSDarrick J. Wong 
897afc51aaaSDarrick J. Wong 	return written;
898afc51aaaSDarrick J. Wong }
899afc51aaaSDarrick J. Wong 
900afc51aaaSDarrick J. Wong int
9013590c4d8SChristoph Hellwig iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
902afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
903afc51aaaSDarrick J. Wong {
9048fc274d1SChristoph Hellwig 	struct iomap_iter iter = {
9058fc274d1SChristoph Hellwig 		.inode		= inode,
9068fc274d1SChristoph Hellwig 		.pos		= pos,
9078fc274d1SChristoph Hellwig 		.len		= len,
908b74b1293SChristoph Hellwig 		.flags		= IOMAP_WRITE | IOMAP_UNSHARE,
9098fc274d1SChristoph Hellwig 	};
9108fc274d1SChristoph Hellwig 	int ret;
911afc51aaaSDarrick J. Wong 
9128fc274d1SChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
9138fc274d1SChristoph Hellwig 		iter.processed = iomap_unshare_iter(&iter);
914afc51aaaSDarrick J. Wong 	return ret;
915afc51aaaSDarrick J. Wong }
9163590c4d8SChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_file_unshare);
917afc51aaaSDarrick J. Wong 
9182aa3048eSChristoph Hellwig static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
919afc51aaaSDarrick J. Wong {
920fad0a1abSChristoph Hellwig 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
9212aa3048eSChristoph Hellwig 	loff_t pos = iter->pos;
9222aa3048eSChristoph Hellwig 	loff_t length = iomap_length(iter);
923afc51aaaSDarrick J. Wong 	loff_t written = 0;
924afc51aaaSDarrick J. Wong 
925afc51aaaSDarrick J. Wong 	/* already zeroed?  we're done. */
926c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
92781ee8e52SMatthew Wilcox (Oracle) 		return length;
928afc51aaaSDarrick J. Wong 
929afc51aaaSDarrick J. Wong 	do {
9304d7bd0ebSMatthew Wilcox (Oracle) 		struct folio *folio;
9314d7bd0ebSMatthew Wilcox (Oracle) 		int status;
9324d7bd0ebSMatthew Wilcox (Oracle) 		size_t offset;
9334d7bd0ebSMatthew Wilcox (Oracle) 		size_t bytes = min_t(u64, SIZE_MAX, length);
934afc51aaaSDarrick J. Wong 
9354d7bd0ebSMatthew Wilcox (Oracle) 		status = iomap_write_begin(iter, pos, bytes, &folio);
9364d7bd0ebSMatthew Wilcox (Oracle) 		if (status)
9374d7bd0ebSMatthew Wilcox (Oracle) 			return status;
9384d7bd0ebSMatthew Wilcox (Oracle) 
9394d7bd0ebSMatthew Wilcox (Oracle) 		offset = offset_in_folio(folio, pos);
9404d7bd0ebSMatthew Wilcox (Oracle) 		if (bytes > folio_size(folio) - offset)
9414d7bd0ebSMatthew Wilcox (Oracle) 			bytes = folio_size(folio) - offset;
9424d7bd0ebSMatthew Wilcox (Oracle) 
9434d7bd0ebSMatthew Wilcox (Oracle) 		folio_zero_range(folio, offset, bytes);
9444d7bd0ebSMatthew Wilcox (Oracle) 		folio_mark_accessed(folio);
9454d7bd0ebSMatthew Wilcox (Oracle) 
9464d7bd0ebSMatthew Wilcox (Oracle) 		bytes = iomap_write_end(iter, pos, bytes, bytes, folio);
9474d7bd0ebSMatthew Wilcox (Oracle) 		if (WARN_ON_ONCE(bytes == 0))
9484d7bd0ebSMatthew Wilcox (Oracle) 			return -EIO;
949afc51aaaSDarrick J. Wong 
950afc51aaaSDarrick J. Wong 		pos += bytes;
95181ee8e52SMatthew Wilcox (Oracle) 		length -= bytes;
952afc51aaaSDarrick J. Wong 		written += bytes;
953afc51aaaSDarrick J. Wong 		if (did_zero)
954afc51aaaSDarrick J. Wong 			*did_zero = true;
95581ee8e52SMatthew Wilcox (Oracle) 	} while (length > 0);
956afc51aaaSDarrick J. Wong 
957afc51aaaSDarrick J. Wong 	return written;
958afc51aaaSDarrick J. Wong }
959afc51aaaSDarrick J. Wong 
960afc51aaaSDarrick J. Wong int
961afc51aaaSDarrick J. Wong iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
962afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
963afc51aaaSDarrick J. Wong {
9642aa3048eSChristoph Hellwig 	struct iomap_iter iter = {
9652aa3048eSChristoph Hellwig 		.inode		= inode,
9662aa3048eSChristoph Hellwig 		.pos		= pos,
9672aa3048eSChristoph Hellwig 		.len		= len,
9682aa3048eSChristoph Hellwig 		.flags		= IOMAP_ZERO,
9692aa3048eSChristoph Hellwig 	};
9702aa3048eSChristoph Hellwig 	int ret;
971afc51aaaSDarrick J. Wong 
9722aa3048eSChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
9732aa3048eSChristoph Hellwig 		iter.processed = iomap_zero_iter(&iter, did_zero);
974afc51aaaSDarrick J. Wong 	return ret;
975afc51aaaSDarrick J. Wong }
976afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_zero_range);
977afc51aaaSDarrick J. Wong 
978afc51aaaSDarrick J. Wong int
979afc51aaaSDarrick J. Wong iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
980afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
981afc51aaaSDarrick J. Wong {
982afc51aaaSDarrick J. Wong 	unsigned int blocksize = i_blocksize(inode);
983afc51aaaSDarrick J. Wong 	unsigned int off = pos & (blocksize - 1);
984afc51aaaSDarrick J. Wong 
985afc51aaaSDarrick J. Wong 	/* Block boundary? Nothing to do */
986afc51aaaSDarrick J. Wong 	if (!off)
987afc51aaaSDarrick J. Wong 		return 0;
988afc51aaaSDarrick J. Wong 	return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
989afc51aaaSDarrick J. Wong }
990afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_truncate_page);
991afc51aaaSDarrick J. Wong 
992ea0f843aSMatthew Wilcox (Oracle) static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter,
993ea0f843aSMatthew Wilcox (Oracle) 		struct folio *folio)
994afc51aaaSDarrick J. Wong {
995253564baSChristoph Hellwig 	loff_t length = iomap_length(iter);
996afc51aaaSDarrick J. Wong 	int ret;
997afc51aaaSDarrick J. Wong 
998253564baSChristoph Hellwig 	if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
999d1bd0b4eSMatthew Wilcox (Oracle) 		ret = __block_write_begin_int(folio, iter->pos, length, NULL,
1000253564baSChristoph Hellwig 					      &iter->iomap);
1001afc51aaaSDarrick J. Wong 		if (ret)
1002afc51aaaSDarrick J. Wong 			return ret;
1003ea0f843aSMatthew Wilcox (Oracle) 		block_commit_write(&folio->page, 0, length);
1004afc51aaaSDarrick J. Wong 	} else {
1005ea0f843aSMatthew Wilcox (Oracle) 		WARN_ON_ONCE(!folio_test_uptodate(folio));
1006ea0f843aSMatthew Wilcox (Oracle) 		folio_mark_dirty(folio);
1007afc51aaaSDarrick J. Wong 	}
1008afc51aaaSDarrick J. Wong 
1009afc51aaaSDarrick J. Wong 	return length;
1010afc51aaaSDarrick J. Wong }
1011afc51aaaSDarrick J. Wong 
1012afc51aaaSDarrick J. Wong vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1013afc51aaaSDarrick J. Wong {
1014253564baSChristoph Hellwig 	struct iomap_iter iter = {
1015253564baSChristoph Hellwig 		.inode		= file_inode(vmf->vma->vm_file),
1016253564baSChristoph Hellwig 		.flags		= IOMAP_WRITE | IOMAP_FAULT,
1017253564baSChristoph Hellwig 	};
1018ea0f843aSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
1019afc51aaaSDarrick J. Wong 	ssize_t ret;
1020afc51aaaSDarrick J. Wong 
1021ea0f843aSMatthew Wilcox (Oracle) 	folio_lock(folio);
1022ea0f843aSMatthew Wilcox (Oracle) 	ret = folio_mkwrite_check_truncate(folio, iter.inode);
1023243145bcSAndreas Gruenbacher 	if (ret < 0)
1024afc51aaaSDarrick J. Wong 		goto out_unlock;
1025ea0f843aSMatthew Wilcox (Oracle) 	iter.pos = folio_pos(folio);
1026253564baSChristoph Hellwig 	iter.len = ret;
1027253564baSChristoph Hellwig 	while ((ret = iomap_iter(&iter, ops)) > 0)
1028ea0f843aSMatthew Wilcox (Oracle) 		iter.processed = iomap_folio_mkwrite_iter(&iter, folio);
1029afc51aaaSDarrick J. Wong 
1030253564baSChristoph Hellwig 	if (ret < 0)
1031afc51aaaSDarrick J. Wong 		goto out_unlock;
1032ea0f843aSMatthew Wilcox (Oracle) 	folio_wait_stable(folio);
1033afc51aaaSDarrick J. Wong 	return VM_FAULT_LOCKED;
1034afc51aaaSDarrick J. Wong out_unlock:
1035ea0f843aSMatthew Wilcox (Oracle) 	folio_unlock(folio);
1036afc51aaaSDarrick J. Wong 	return block_page_mkwrite_return(ret);
1037afc51aaaSDarrick J. Wong }
1038afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1039598ecfbaSChristoph Hellwig 
10408ffd74e9SMatthew Wilcox (Oracle) static void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
10418ffd74e9SMatthew Wilcox (Oracle) 		size_t len, int error)
1042598ecfbaSChristoph Hellwig {
104395c4cd05SMatthew Wilcox (Oracle) 	struct iomap_page *iop = to_iomap_page(folio);
1044598ecfbaSChristoph Hellwig 
1045598ecfbaSChristoph Hellwig 	if (error) {
10468ffd74e9SMatthew Wilcox (Oracle) 		folio_set_error(folio);
1047b69eea82SDarrick J. Wong 		mapping_set_error(inode->i_mapping, error);
1048598ecfbaSChristoph Hellwig 	}
1049598ecfbaSChristoph Hellwig 
10508ffd74e9SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !iop);
10510fb2d720SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0);
1052598ecfbaSChristoph Hellwig 
10530fb2d720SMatthew Wilcox (Oracle) 	if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending))
10548ffd74e9SMatthew Wilcox (Oracle) 		folio_end_writeback(folio);
1055598ecfbaSChristoph Hellwig }
1056598ecfbaSChristoph Hellwig 
1057598ecfbaSChristoph Hellwig /*
1058598ecfbaSChristoph Hellwig  * We're now finished for good with this ioend structure.  Update the page
1059598ecfbaSChristoph Hellwig  * state, release holds on bios, and finally free up memory.  Do not use the
1060598ecfbaSChristoph Hellwig  * ioend after this.
1061598ecfbaSChristoph Hellwig  */
1062ebb7fb15SDave Chinner static u32
1063598ecfbaSChristoph Hellwig iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1064598ecfbaSChristoph Hellwig {
1065598ecfbaSChristoph Hellwig 	struct inode *inode = ioend->io_inode;
1066598ecfbaSChristoph Hellwig 	struct bio *bio = &ioend->io_inline_bio;
1067598ecfbaSChristoph Hellwig 	struct bio *last = ioend->io_bio, *next;
1068598ecfbaSChristoph Hellwig 	u64 start = bio->bi_iter.bi_sector;
1069c275779fSZorro Lang 	loff_t offset = ioend->io_offset;
1070598ecfbaSChristoph Hellwig 	bool quiet = bio_flagged(bio, BIO_QUIET);
1071ebb7fb15SDave Chinner 	u32 folio_count = 0;
1072598ecfbaSChristoph Hellwig 
1073598ecfbaSChristoph Hellwig 	for (bio = &ioend->io_inline_bio; bio; bio = next) {
10748ffd74e9SMatthew Wilcox (Oracle) 		struct folio_iter fi;
1075598ecfbaSChristoph Hellwig 
1076598ecfbaSChristoph Hellwig 		/*
1077598ecfbaSChristoph Hellwig 		 * For the last bio, bi_private points to the ioend, so we
1078598ecfbaSChristoph Hellwig 		 * need to explicitly end the iteration here.
1079598ecfbaSChristoph Hellwig 		 */
1080598ecfbaSChristoph Hellwig 		if (bio == last)
1081598ecfbaSChristoph Hellwig 			next = NULL;
1082598ecfbaSChristoph Hellwig 		else
1083598ecfbaSChristoph Hellwig 			next = bio->bi_private;
1084598ecfbaSChristoph Hellwig 
10858ffd74e9SMatthew Wilcox (Oracle) 		/* walk all folios in bio, ending page IO on them */
1086ebb7fb15SDave Chinner 		bio_for_each_folio_all(fi, bio) {
10878ffd74e9SMatthew Wilcox (Oracle) 			iomap_finish_folio_write(inode, fi.folio, fi.length,
10888ffd74e9SMatthew Wilcox (Oracle) 					error);
1089ebb7fb15SDave Chinner 			folio_count++;
1090ebb7fb15SDave Chinner 		}
1091598ecfbaSChristoph Hellwig 		bio_put(bio);
1092598ecfbaSChristoph Hellwig 	}
1093c275779fSZorro Lang 	/* The ioend has been freed by bio_put() */
1094598ecfbaSChristoph Hellwig 
1095598ecfbaSChristoph Hellwig 	if (unlikely(error && !quiet)) {
1096598ecfbaSChristoph Hellwig 		printk_ratelimited(KERN_ERR
10979cd0ed63SDarrick J. Wong "%s: writeback error on inode %lu, offset %lld, sector %llu",
1098c275779fSZorro Lang 			inode->i_sb->s_id, inode->i_ino, offset, start);
1099598ecfbaSChristoph Hellwig 	}
1100ebb7fb15SDave Chinner 	return folio_count;
1101598ecfbaSChristoph Hellwig }
1102598ecfbaSChristoph Hellwig 
1103ebb7fb15SDave Chinner /*
1104ebb7fb15SDave Chinner  * Ioend completion routine for merged bios. This can only be called from task
1105ebb7fb15SDave Chinner  * contexts as merged ioends can be of unbound length. Hence we have to break up
1106ebb7fb15SDave Chinner  * the writeback completions into manageable chunks to avoid long scheduler
1107ebb7fb15SDave Chinner  * holdoffs. We aim to keep scheduler holdoffs down below 10ms so that we get
1108ebb7fb15SDave Chinner  * good batch processing throughput without creating adverse scheduler latency
1109ebb7fb15SDave Chinner  * conditions.
1110ebb7fb15SDave Chinner  */
1111598ecfbaSChristoph Hellwig void
1112598ecfbaSChristoph Hellwig iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1113598ecfbaSChristoph Hellwig {
1114598ecfbaSChristoph Hellwig 	struct list_head tmp;
1115ebb7fb15SDave Chinner 	u32 completions;
1116ebb7fb15SDave Chinner 
1117ebb7fb15SDave Chinner 	might_sleep();
1118598ecfbaSChristoph Hellwig 
1119598ecfbaSChristoph Hellwig 	list_replace_init(&ioend->io_list, &tmp);
1120ebb7fb15SDave Chinner 	completions = iomap_finish_ioend(ioend, error);
1121598ecfbaSChristoph Hellwig 
1122598ecfbaSChristoph Hellwig 	while (!list_empty(&tmp)) {
1123ebb7fb15SDave Chinner 		if (completions > IOEND_BATCH_SIZE * 8) {
1124ebb7fb15SDave Chinner 			cond_resched();
1125ebb7fb15SDave Chinner 			completions = 0;
1126ebb7fb15SDave Chinner 		}
1127598ecfbaSChristoph Hellwig 		ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1128598ecfbaSChristoph Hellwig 		list_del_init(&ioend->io_list);
1129ebb7fb15SDave Chinner 		completions += iomap_finish_ioend(ioend, error);
1130598ecfbaSChristoph Hellwig 	}
1131598ecfbaSChristoph Hellwig }
1132598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1133598ecfbaSChristoph Hellwig 
1134598ecfbaSChristoph Hellwig /*
1135598ecfbaSChristoph Hellwig  * We can merge two adjacent ioends if they have the same set of work to do.
1136598ecfbaSChristoph Hellwig  */
1137598ecfbaSChristoph Hellwig static bool
1138598ecfbaSChristoph Hellwig iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1139598ecfbaSChristoph Hellwig {
1140598ecfbaSChristoph Hellwig 	if (ioend->io_bio->bi_status != next->io_bio->bi_status)
1141598ecfbaSChristoph Hellwig 		return false;
1142598ecfbaSChristoph Hellwig 	if ((ioend->io_flags & IOMAP_F_SHARED) ^
1143598ecfbaSChristoph Hellwig 	    (next->io_flags & IOMAP_F_SHARED))
1144598ecfbaSChristoph Hellwig 		return false;
1145598ecfbaSChristoph Hellwig 	if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1146598ecfbaSChristoph Hellwig 	    (next->io_type == IOMAP_UNWRITTEN))
1147598ecfbaSChristoph Hellwig 		return false;
1148598ecfbaSChristoph Hellwig 	if (ioend->io_offset + ioend->io_size != next->io_offset)
1149598ecfbaSChristoph Hellwig 		return false;
1150ebb7fb15SDave Chinner 	/*
1151ebb7fb15SDave Chinner 	 * Do not merge physically discontiguous ioends. The filesystem
1152ebb7fb15SDave Chinner 	 * completion functions will have to iterate the physical
1153ebb7fb15SDave Chinner 	 * discontiguities even if we merge the ioends at a logical level, so
1154ebb7fb15SDave Chinner 	 * we don't gain anything by merging physical discontiguities here.
1155ebb7fb15SDave Chinner 	 *
1156ebb7fb15SDave Chinner 	 * We cannot use bio->bi_iter.bi_sector here as it is modified during
1157ebb7fb15SDave Chinner 	 * submission so does not point to the start sector of the bio at
1158ebb7fb15SDave Chinner 	 * completion.
1159ebb7fb15SDave Chinner 	 */
1160ebb7fb15SDave Chinner 	if (ioend->io_sector + (ioend->io_size >> 9) != next->io_sector)
1161ebb7fb15SDave Chinner 		return false;
1162598ecfbaSChristoph Hellwig 	return true;
1163598ecfbaSChristoph Hellwig }
1164598ecfbaSChristoph Hellwig 
1165598ecfbaSChristoph Hellwig void
11666e552494SBrian Foster iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
1167598ecfbaSChristoph Hellwig {
1168598ecfbaSChristoph Hellwig 	struct iomap_ioend *next;
1169598ecfbaSChristoph Hellwig 
1170598ecfbaSChristoph Hellwig 	INIT_LIST_HEAD(&ioend->io_list);
1171598ecfbaSChristoph Hellwig 
1172598ecfbaSChristoph Hellwig 	while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1173598ecfbaSChristoph Hellwig 			io_list))) {
1174598ecfbaSChristoph Hellwig 		if (!iomap_ioend_can_merge(ioend, next))
1175598ecfbaSChristoph Hellwig 			break;
1176598ecfbaSChristoph Hellwig 		list_move_tail(&next->io_list, &ioend->io_list);
1177598ecfbaSChristoph Hellwig 		ioend->io_size += next->io_size;
1178598ecfbaSChristoph Hellwig 	}
1179598ecfbaSChristoph Hellwig }
1180598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1181598ecfbaSChristoph Hellwig 
1182598ecfbaSChristoph Hellwig static int
11834f0f586bSSami Tolvanen iomap_ioend_compare(void *priv, const struct list_head *a,
11844f0f586bSSami Tolvanen 		const struct list_head *b)
1185598ecfbaSChristoph Hellwig {
1186b3d423ecSChristoph Hellwig 	struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1187b3d423ecSChristoph Hellwig 	struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
1188598ecfbaSChristoph Hellwig 
1189598ecfbaSChristoph Hellwig 	if (ia->io_offset < ib->io_offset)
1190598ecfbaSChristoph Hellwig 		return -1;
1191b3d423ecSChristoph Hellwig 	if (ia->io_offset > ib->io_offset)
1192598ecfbaSChristoph Hellwig 		return 1;
1193598ecfbaSChristoph Hellwig 	return 0;
1194598ecfbaSChristoph Hellwig }
1195598ecfbaSChristoph Hellwig 
1196598ecfbaSChristoph Hellwig void
1197598ecfbaSChristoph Hellwig iomap_sort_ioends(struct list_head *ioend_list)
1198598ecfbaSChristoph Hellwig {
1199598ecfbaSChristoph Hellwig 	list_sort(NULL, ioend_list, iomap_ioend_compare);
1200598ecfbaSChristoph Hellwig }
1201598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1202598ecfbaSChristoph Hellwig 
1203598ecfbaSChristoph Hellwig static void iomap_writepage_end_bio(struct bio *bio)
1204598ecfbaSChristoph Hellwig {
1205598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend = bio->bi_private;
1206598ecfbaSChristoph Hellwig 
1207598ecfbaSChristoph Hellwig 	iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
1208598ecfbaSChristoph Hellwig }
1209598ecfbaSChristoph Hellwig 
1210598ecfbaSChristoph Hellwig /*
1211598ecfbaSChristoph Hellwig  * Submit the final bio for an ioend.
1212598ecfbaSChristoph Hellwig  *
1213598ecfbaSChristoph Hellwig  * If @error is non-zero, it means that we have a situation where some part of
1214f1f264b4SAndreas Gruenbacher  * the submission process has failed after we've marked pages for writeback
1215598ecfbaSChristoph Hellwig  * and unlocked them.  In this situation, we need to fail the bio instead of
1216598ecfbaSChristoph Hellwig  * submitting it.  This typically only happens on a filesystem shutdown.
1217598ecfbaSChristoph Hellwig  */
1218598ecfbaSChristoph Hellwig static int
1219598ecfbaSChristoph Hellwig iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
1220598ecfbaSChristoph Hellwig 		int error)
1221598ecfbaSChristoph Hellwig {
1222598ecfbaSChristoph Hellwig 	ioend->io_bio->bi_private = ioend;
1223598ecfbaSChristoph Hellwig 	ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
1224598ecfbaSChristoph Hellwig 
1225598ecfbaSChristoph Hellwig 	if (wpc->ops->prepare_ioend)
1226598ecfbaSChristoph Hellwig 		error = wpc->ops->prepare_ioend(ioend, error);
1227598ecfbaSChristoph Hellwig 	if (error) {
1228598ecfbaSChristoph Hellwig 		/*
1229f1f264b4SAndreas Gruenbacher 		 * If we're failing the IO now, just mark the ioend with an
1230598ecfbaSChristoph Hellwig 		 * error and finish it.  This will run IO completion immediately
1231598ecfbaSChristoph Hellwig 		 * as there is only one reference to the ioend at this point in
1232598ecfbaSChristoph Hellwig 		 * time.
1233598ecfbaSChristoph Hellwig 		 */
1234598ecfbaSChristoph Hellwig 		ioend->io_bio->bi_status = errno_to_blk_status(error);
1235598ecfbaSChristoph Hellwig 		bio_endio(ioend->io_bio);
1236598ecfbaSChristoph Hellwig 		return error;
1237598ecfbaSChristoph Hellwig 	}
1238598ecfbaSChristoph Hellwig 
1239598ecfbaSChristoph Hellwig 	submit_bio(ioend->io_bio);
1240598ecfbaSChristoph Hellwig 	return 0;
1241598ecfbaSChristoph Hellwig }
1242598ecfbaSChristoph Hellwig 
1243598ecfbaSChristoph Hellwig static struct iomap_ioend *
1244598ecfbaSChristoph Hellwig iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
1245598ecfbaSChristoph Hellwig 		loff_t offset, sector_t sector, struct writeback_control *wbc)
1246598ecfbaSChristoph Hellwig {
1247598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend;
1248598ecfbaSChristoph Hellwig 	struct bio *bio;
1249598ecfbaSChristoph Hellwig 
1250609be106SChristoph Hellwig 	bio = bio_alloc_bioset(wpc->iomap.bdev, BIO_MAX_VECS,
1251609be106SChristoph Hellwig 			       REQ_OP_WRITE | wbc_to_write_flags(wbc),
1252609be106SChristoph Hellwig 			       GFP_NOFS, &iomap_ioend_bioset);
1253598ecfbaSChristoph Hellwig 	bio->bi_iter.bi_sector = sector;
1254598ecfbaSChristoph Hellwig 	wbc_init_bio(wbc, bio);
1255598ecfbaSChristoph Hellwig 
1256598ecfbaSChristoph Hellwig 	ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
1257598ecfbaSChristoph Hellwig 	INIT_LIST_HEAD(&ioend->io_list);
1258598ecfbaSChristoph Hellwig 	ioend->io_type = wpc->iomap.type;
1259598ecfbaSChristoph Hellwig 	ioend->io_flags = wpc->iomap.flags;
1260598ecfbaSChristoph Hellwig 	ioend->io_inode = inode;
1261598ecfbaSChristoph Hellwig 	ioend->io_size = 0;
1262ebb7fb15SDave Chinner 	ioend->io_folios = 0;
1263598ecfbaSChristoph Hellwig 	ioend->io_offset = offset;
1264598ecfbaSChristoph Hellwig 	ioend->io_bio = bio;
1265ebb7fb15SDave Chinner 	ioend->io_sector = sector;
1266598ecfbaSChristoph Hellwig 	return ioend;
1267598ecfbaSChristoph Hellwig }
1268598ecfbaSChristoph Hellwig 
1269598ecfbaSChristoph Hellwig /*
1270598ecfbaSChristoph Hellwig  * Allocate a new bio, and chain the old bio to the new one.
1271598ecfbaSChristoph Hellwig  *
1272f1f264b4SAndreas Gruenbacher  * Note that we have to perform the chaining in this unintuitive order
1273598ecfbaSChristoph Hellwig  * so that the bi_private linkage is set up in the right direction for the
1274598ecfbaSChristoph Hellwig  * traversal in iomap_finish_ioend().
1275598ecfbaSChristoph Hellwig  */
1276598ecfbaSChristoph Hellwig static struct bio *
1277598ecfbaSChristoph Hellwig iomap_chain_bio(struct bio *prev)
1278598ecfbaSChristoph Hellwig {
1279598ecfbaSChristoph Hellwig 	struct bio *new;
1280598ecfbaSChristoph Hellwig 
128107888c66SChristoph Hellwig 	new = bio_alloc(prev->bi_bdev, BIO_MAX_VECS, prev->bi_opf, GFP_NOFS);
128207888c66SChristoph Hellwig 	bio_clone_blkg_association(new, prev);
1283598ecfbaSChristoph Hellwig 	new->bi_iter.bi_sector = bio_end_sector(prev);
1284598ecfbaSChristoph Hellwig 
1285598ecfbaSChristoph Hellwig 	bio_chain(prev, new);
1286598ecfbaSChristoph Hellwig 	bio_get(prev);		/* for iomap_finish_ioend */
1287598ecfbaSChristoph Hellwig 	submit_bio(prev);
1288598ecfbaSChristoph Hellwig 	return new;
1289598ecfbaSChristoph Hellwig }
1290598ecfbaSChristoph Hellwig 
1291598ecfbaSChristoph Hellwig static bool
1292598ecfbaSChristoph Hellwig iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
1293598ecfbaSChristoph Hellwig 		sector_t sector)
1294598ecfbaSChristoph Hellwig {
1295598ecfbaSChristoph Hellwig 	if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1296598ecfbaSChristoph Hellwig 	    (wpc->ioend->io_flags & IOMAP_F_SHARED))
1297598ecfbaSChristoph Hellwig 		return false;
1298598ecfbaSChristoph Hellwig 	if (wpc->iomap.type != wpc->ioend->io_type)
1299598ecfbaSChristoph Hellwig 		return false;
1300598ecfbaSChristoph Hellwig 	if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
1301598ecfbaSChristoph Hellwig 		return false;
1302598ecfbaSChristoph Hellwig 	if (sector != bio_end_sector(wpc->ioend->io_bio))
1303598ecfbaSChristoph Hellwig 		return false;
1304ebb7fb15SDave Chinner 	/*
1305ebb7fb15SDave Chinner 	 * Limit ioend bio chain lengths to minimise IO completion latency. This
1306ebb7fb15SDave Chinner 	 * also prevents long tight loops ending page writeback on all the
1307ebb7fb15SDave Chinner 	 * folios in the ioend.
1308ebb7fb15SDave Chinner 	 */
1309ebb7fb15SDave Chinner 	if (wpc->ioend->io_folios >= IOEND_BATCH_SIZE)
1310ebb7fb15SDave Chinner 		return false;
1311598ecfbaSChristoph Hellwig 	return true;
1312598ecfbaSChristoph Hellwig }
1313598ecfbaSChristoph Hellwig 
1314598ecfbaSChristoph Hellwig /*
1315598ecfbaSChristoph Hellwig  * Test to see if we have an existing ioend structure that we could append to
1316f1f264b4SAndreas Gruenbacher  * first; otherwise finish off the current ioend and start another.
1317598ecfbaSChristoph Hellwig  */
1318598ecfbaSChristoph Hellwig static void
1319e735c007SMatthew Wilcox (Oracle) iomap_add_to_ioend(struct inode *inode, loff_t pos, struct folio *folio,
1320598ecfbaSChristoph Hellwig 		struct iomap_page *iop, struct iomap_writepage_ctx *wpc,
1321598ecfbaSChristoph Hellwig 		struct writeback_control *wbc, struct list_head *iolist)
1322598ecfbaSChristoph Hellwig {
1323e735c007SMatthew Wilcox (Oracle) 	sector_t sector = iomap_sector(&wpc->iomap, pos);
1324598ecfbaSChristoph Hellwig 	unsigned len = i_blocksize(inode);
1325e735c007SMatthew Wilcox (Oracle) 	size_t poff = offset_in_folio(folio, pos);
1326598ecfbaSChristoph Hellwig 
1327e735c007SMatthew Wilcox (Oracle) 	if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, pos, sector)) {
1328598ecfbaSChristoph Hellwig 		if (wpc->ioend)
1329598ecfbaSChristoph Hellwig 			list_add(&wpc->ioend->io_list, iolist);
1330e735c007SMatthew Wilcox (Oracle) 		wpc->ioend = iomap_alloc_ioend(inode, wpc, pos, sector, wbc);
1331598ecfbaSChristoph Hellwig 	}
1332598ecfbaSChristoph Hellwig 
1333e735c007SMatthew Wilcox (Oracle) 	if (!bio_add_folio(wpc->ioend->io_bio, folio, len, poff)) {
1334c1b79f11SChristoph Hellwig 		wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio);
1335e735c007SMatthew Wilcox (Oracle) 		bio_add_folio(wpc->ioend->io_bio, folio, len, poff);
1336c1b79f11SChristoph Hellwig 	}
1337c1b79f11SChristoph Hellwig 
13380fb2d720SMatthew Wilcox (Oracle) 	if (iop)
13390fb2d720SMatthew Wilcox (Oracle) 		atomic_add(len, &iop->write_bytes_pending);
1340598ecfbaSChristoph Hellwig 	wpc->ioend->io_size += len;
1341e735c007SMatthew Wilcox (Oracle) 	wbc_account_cgroup_owner(wbc, &folio->page, len);
1342598ecfbaSChristoph Hellwig }
1343598ecfbaSChristoph Hellwig 
1344598ecfbaSChristoph Hellwig /*
1345598ecfbaSChristoph Hellwig  * We implement an immediate ioend submission policy here to avoid needing to
1346598ecfbaSChristoph Hellwig  * chain multiple ioends and hence nest mempool allocations which can violate
1347f1f264b4SAndreas Gruenbacher  * the forward progress guarantees we need to provide. The current ioend we're
1348f1f264b4SAndreas Gruenbacher  * adding blocks to is cached in the writepage context, and if the new block
1349f1f264b4SAndreas Gruenbacher  * doesn't append to the cached ioend, it will create a new ioend and cache that
1350598ecfbaSChristoph Hellwig  * instead.
1351598ecfbaSChristoph Hellwig  *
1352598ecfbaSChristoph Hellwig  * If a new ioend is created and cached, the old ioend is returned and queued
1353598ecfbaSChristoph Hellwig  * locally for submission once the entire page is processed or an error has been
1354598ecfbaSChristoph Hellwig  * detected.  While ioends are submitted immediately after they are completed,
1355598ecfbaSChristoph Hellwig  * batching optimisations are provided by higher level block plugging.
1356598ecfbaSChristoph Hellwig  *
1357598ecfbaSChristoph Hellwig  * At the end of a writeback pass, there will be a cached ioend remaining on the
1358598ecfbaSChristoph Hellwig  * writepage context that the caller will need to submit.
1359598ecfbaSChristoph Hellwig  */
1360598ecfbaSChristoph Hellwig static int
1361598ecfbaSChristoph Hellwig iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1362598ecfbaSChristoph Hellwig 		struct writeback_control *wbc, struct inode *inode,
1363e735c007SMatthew Wilcox (Oracle) 		struct folio *folio, u64 end_pos)
1364598ecfbaSChristoph Hellwig {
13659753b868SStefan Roesch 	struct iomap_page *iop = iomap_page_create(inode, folio, 0);
1366598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend, *next;
1367598ecfbaSChristoph Hellwig 	unsigned len = i_blocksize(inode);
136892655036SMatthew Wilcox (Oracle) 	unsigned nblocks = i_blocks_per_folio(inode, folio);
136992655036SMatthew Wilcox (Oracle) 	u64 pos = folio_pos(folio);
1370598ecfbaSChristoph Hellwig 	int error = 0, count = 0, i;
1371598ecfbaSChristoph Hellwig 	LIST_HEAD(submit_list);
1372598ecfbaSChristoph Hellwig 
13730fb2d720SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0);
1374598ecfbaSChristoph Hellwig 
1375598ecfbaSChristoph Hellwig 	/*
137692655036SMatthew Wilcox (Oracle) 	 * Walk through the folio to find areas to write back. If we
137792655036SMatthew Wilcox (Oracle) 	 * run off the end of the current map or find the current map
137892655036SMatthew Wilcox (Oracle) 	 * invalid, grab a new one.
1379598ecfbaSChristoph Hellwig 	 */
138092655036SMatthew Wilcox (Oracle) 	for (i = 0; i < nblocks && pos < end_pos; i++, pos += len) {
1381598ecfbaSChristoph Hellwig 		if (iop && !test_bit(i, iop->uptodate))
1382598ecfbaSChristoph Hellwig 			continue;
1383598ecfbaSChristoph Hellwig 
138492655036SMatthew Wilcox (Oracle) 		error = wpc->ops->map_blocks(wpc, inode, pos);
1385598ecfbaSChristoph Hellwig 		if (error)
1386598ecfbaSChristoph Hellwig 			break;
13873e19e6f3SChristoph Hellwig 		if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
13883e19e6f3SChristoph Hellwig 			continue;
1389598ecfbaSChristoph Hellwig 		if (wpc->iomap.type == IOMAP_HOLE)
1390598ecfbaSChristoph Hellwig 			continue;
1391e735c007SMatthew Wilcox (Oracle) 		iomap_add_to_ioend(inode, pos, folio, iop, wpc, wbc,
1392598ecfbaSChristoph Hellwig 				 &submit_list);
1393598ecfbaSChristoph Hellwig 		count++;
1394598ecfbaSChristoph Hellwig 	}
1395ebb7fb15SDave Chinner 	if (count)
1396ebb7fb15SDave Chinner 		wpc->ioend->io_folios++;
1397598ecfbaSChristoph Hellwig 
1398598ecfbaSChristoph Hellwig 	WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
1399e735c007SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(!folio_test_locked(folio));
1400e735c007SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(folio_test_writeback(folio));
1401e735c007SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(folio_test_dirty(folio));
1402598ecfbaSChristoph Hellwig 
1403598ecfbaSChristoph Hellwig 	/*
1404598ecfbaSChristoph Hellwig 	 * We cannot cancel the ioend directly here on error.  We may have
1405598ecfbaSChristoph Hellwig 	 * already set other pages under writeback and hence we have to run I/O
1406598ecfbaSChristoph Hellwig 	 * completion to mark the error state of the pages under writeback
1407598ecfbaSChristoph Hellwig 	 * appropriately.
1408598ecfbaSChristoph Hellwig 	 */
1409598ecfbaSChristoph Hellwig 	if (unlikely(error)) {
1410598ecfbaSChristoph Hellwig 		/*
1411763e4cdcSBrian Foster 		 * Let the filesystem know what portion of the current page
1412f1f264b4SAndreas Gruenbacher 		 * failed to map. If the page hasn't been added to ioend, it
1413763e4cdcSBrian Foster 		 * won't be affected by I/O completion and we must unlock it
1414763e4cdcSBrian Foster 		 * now.
1415598ecfbaSChristoph Hellwig 		 */
14166e478521SMatthew Wilcox (Oracle) 		if (wpc->ops->discard_folio)
141792655036SMatthew Wilcox (Oracle) 			wpc->ops->discard_folio(folio, pos);
1418763e4cdcSBrian Foster 		if (!count) {
1419e735c007SMatthew Wilcox (Oracle) 			folio_unlock(folio);
1420598ecfbaSChristoph Hellwig 			goto done;
1421598ecfbaSChristoph Hellwig 		}
1422598ecfbaSChristoph Hellwig 	}
1423598ecfbaSChristoph Hellwig 
1424e735c007SMatthew Wilcox (Oracle) 	folio_start_writeback(folio);
1425e735c007SMatthew Wilcox (Oracle) 	folio_unlock(folio);
1426598ecfbaSChristoph Hellwig 
1427598ecfbaSChristoph Hellwig 	/*
1428f1f264b4SAndreas Gruenbacher 	 * Preserve the original error if there was one; catch
1429598ecfbaSChristoph Hellwig 	 * submission errors here and propagate into subsequent ioend
1430598ecfbaSChristoph Hellwig 	 * submissions.
1431598ecfbaSChristoph Hellwig 	 */
1432598ecfbaSChristoph Hellwig 	list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
1433598ecfbaSChristoph Hellwig 		int error2;
1434598ecfbaSChristoph Hellwig 
1435598ecfbaSChristoph Hellwig 		list_del_init(&ioend->io_list);
1436598ecfbaSChristoph Hellwig 		error2 = iomap_submit_ioend(wpc, ioend, error);
1437598ecfbaSChristoph Hellwig 		if (error2 && !error)
1438598ecfbaSChristoph Hellwig 			error = error2;
1439598ecfbaSChristoph Hellwig 	}
1440598ecfbaSChristoph Hellwig 
1441598ecfbaSChristoph Hellwig 	/*
1442598ecfbaSChristoph Hellwig 	 * We can end up here with no error and nothing to write only if we race
1443598ecfbaSChristoph Hellwig 	 * with a partial page truncate on a sub-page block sized filesystem.
1444598ecfbaSChristoph Hellwig 	 */
1445598ecfbaSChristoph Hellwig 	if (!count)
1446e735c007SMatthew Wilcox (Oracle) 		folio_end_writeback(folio);
1447598ecfbaSChristoph Hellwig done:
1448e735c007SMatthew Wilcox (Oracle) 	mapping_set_error(folio->mapping, error);
1449598ecfbaSChristoph Hellwig 	return error;
1450598ecfbaSChristoph Hellwig }
1451598ecfbaSChristoph Hellwig 
1452598ecfbaSChristoph Hellwig /*
1453598ecfbaSChristoph Hellwig  * Write out a dirty page.
1454598ecfbaSChristoph Hellwig  *
1455f1f264b4SAndreas Gruenbacher  * For delalloc space on the page, we need to allocate space and flush it.
1456f1f264b4SAndreas Gruenbacher  * For unwritten space on the page, we need to start the conversion to
1457598ecfbaSChristoph Hellwig  * regular allocated space.
1458598ecfbaSChristoph Hellwig  */
1459598ecfbaSChristoph Hellwig static int
1460598ecfbaSChristoph Hellwig iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)
1461598ecfbaSChristoph Hellwig {
1462e735c007SMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(page);
1463598ecfbaSChristoph Hellwig 	struct iomap_writepage_ctx *wpc = data;
1464e735c007SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
146581d4782aSMatthew Wilcox (Oracle) 	u64 end_pos, isize;
1466598ecfbaSChristoph Hellwig 
1467e735c007SMatthew Wilcox (Oracle) 	trace_iomap_writepage(inode, folio_pos(folio), folio_size(folio));
1468598ecfbaSChristoph Hellwig 
1469598ecfbaSChristoph Hellwig 	/*
1470e735c007SMatthew Wilcox (Oracle) 	 * Refuse to write the folio out if we're called from reclaim context.
1471598ecfbaSChristoph Hellwig 	 *
1472598ecfbaSChristoph Hellwig 	 * This avoids stack overflows when called from deeply used stacks in
1473598ecfbaSChristoph Hellwig 	 * random callers for direct reclaim or memcg reclaim.  We explicitly
1474598ecfbaSChristoph Hellwig 	 * allow reclaim from kswapd as the stack usage there is relatively low.
1475598ecfbaSChristoph Hellwig 	 *
1476598ecfbaSChristoph Hellwig 	 * This should never happen except in the case of a VM regression so
1477598ecfbaSChristoph Hellwig 	 * warn about it.
1478598ecfbaSChristoph Hellwig 	 */
1479598ecfbaSChristoph Hellwig 	if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1480598ecfbaSChristoph Hellwig 			PF_MEMALLOC))
1481598ecfbaSChristoph Hellwig 		goto redirty;
1482598ecfbaSChristoph Hellwig 
1483598ecfbaSChristoph Hellwig 	/*
1484e735c007SMatthew Wilcox (Oracle) 	 * Is this folio beyond the end of the file?
1485598ecfbaSChristoph Hellwig 	 *
1486e735c007SMatthew Wilcox (Oracle) 	 * The folio index is less than the end_index, adjust the end_pos
1487e735c007SMatthew Wilcox (Oracle) 	 * to the highest offset that this folio should represent.
1488598ecfbaSChristoph Hellwig 	 * -----------------------------------------------------
1489598ecfbaSChristoph Hellwig 	 * |			file mapping	       | <EOF> |
1490598ecfbaSChristoph Hellwig 	 * -----------------------------------------------------
1491598ecfbaSChristoph Hellwig 	 * | Page ... | Page N-2 | Page N-1 |  Page N  |       |
1492598ecfbaSChristoph Hellwig 	 * ^--------------------------------^----------|--------
1493598ecfbaSChristoph Hellwig 	 * |     desired writeback range    |      see else    |
1494598ecfbaSChristoph Hellwig 	 * ---------------------------------^------------------|
1495598ecfbaSChristoph Hellwig 	 */
149681d4782aSMatthew Wilcox (Oracle) 	isize = i_size_read(inode);
1497e735c007SMatthew Wilcox (Oracle) 	end_pos = folio_pos(folio) + folio_size(folio);
149881d4782aSMatthew Wilcox (Oracle) 	if (end_pos > isize) {
1499598ecfbaSChristoph Hellwig 		/*
1500598ecfbaSChristoph Hellwig 		 * Check whether the page to write out is beyond or straddles
1501598ecfbaSChristoph Hellwig 		 * i_size or not.
1502598ecfbaSChristoph Hellwig 		 * -------------------------------------------------------
1503598ecfbaSChristoph Hellwig 		 * |		file mapping		        | <EOF>  |
1504598ecfbaSChristoph Hellwig 		 * -------------------------------------------------------
1505598ecfbaSChristoph Hellwig 		 * | Page ... | Page N-2 | Page N-1 |  Page N   | Beyond |
1506598ecfbaSChristoph Hellwig 		 * ^--------------------------------^-----------|---------
1507598ecfbaSChristoph Hellwig 		 * |				    |      Straddles     |
1508598ecfbaSChristoph Hellwig 		 * ---------------------------------^-----------|--------|
1509598ecfbaSChristoph Hellwig 		 */
1510e735c007SMatthew Wilcox (Oracle) 		size_t poff = offset_in_folio(folio, isize);
151181d4782aSMatthew Wilcox (Oracle) 		pgoff_t end_index = isize >> PAGE_SHIFT;
1512598ecfbaSChristoph Hellwig 
1513598ecfbaSChristoph Hellwig 		/*
1514f1f264b4SAndreas Gruenbacher 		 * Skip the page if it's fully outside i_size, e.g. due to a
1515f1f264b4SAndreas Gruenbacher 		 * truncate operation that's in progress. We must redirty the
1516598ecfbaSChristoph Hellwig 		 * page so that reclaim stops reclaiming it. Otherwise
15178597447dSMatthew Wilcox (Oracle) 		 * iomap_release_folio() is called on it and gets confused.
1518598ecfbaSChristoph Hellwig 		 *
1519f1f264b4SAndreas Gruenbacher 		 * Note that the end_index is unsigned long.  If the given
1520f1f264b4SAndreas Gruenbacher 		 * offset is greater than 16TB on a 32-bit system then if we
1521f1f264b4SAndreas Gruenbacher 		 * checked if the page is fully outside i_size with
1522f1f264b4SAndreas Gruenbacher 		 * "if (page->index >= end_index + 1)", "end_index + 1" would
1523f1f264b4SAndreas Gruenbacher 		 * overflow and evaluate to 0.  Hence this page would be
1524f1f264b4SAndreas Gruenbacher 		 * redirtied and written out repeatedly, which would result in
1525f1f264b4SAndreas Gruenbacher 		 * an infinite loop; the user program performing this operation
1526f1f264b4SAndreas Gruenbacher 		 * would hang.  Instead, we can detect this situation by
1527f1f264b4SAndreas Gruenbacher 		 * checking if the page is totally beyond i_size or if its
1528598ecfbaSChristoph Hellwig 		 * offset is just equal to the EOF.
1529598ecfbaSChristoph Hellwig 		 */
1530e735c007SMatthew Wilcox (Oracle) 		if (folio->index > end_index ||
1531e735c007SMatthew Wilcox (Oracle) 		    (folio->index == end_index && poff == 0))
1532598ecfbaSChristoph Hellwig 			goto redirty;
1533598ecfbaSChristoph Hellwig 
1534598ecfbaSChristoph Hellwig 		/*
1535598ecfbaSChristoph Hellwig 		 * The page straddles i_size.  It must be zeroed out on each
1536598ecfbaSChristoph Hellwig 		 * and every writepage invocation because it may be mmapped.
1537598ecfbaSChristoph Hellwig 		 * "A file is mapped in multiples of the page size.  For a file
1538598ecfbaSChristoph Hellwig 		 * that is not a multiple of the page size, the remaining
1539598ecfbaSChristoph Hellwig 		 * memory is zeroed when mapped, and writes to that region are
1540598ecfbaSChristoph Hellwig 		 * not written out to the file."
1541598ecfbaSChristoph Hellwig 		 */
1542e735c007SMatthew Wilcox (Oracle) 		folio_zero_segment(folio, poff, folio_size(folio));
154381d4782aSMatthew Wilcox (Oracle) 		end_pos = isize;
1544598ecfbaSChristoph Hellwig 	}
1545598ecfbaSChristoph Hellwig 
1546e735c007SMatthew Wilcox (Oracle) 	return iomap_writepage_map(wpc, wbc, inode, folio, end_pos);
1547598ecfbaSChristoph Hellwig 
1548598ecfbaSChristoph Hellwig redirty:
1549e735c007SMatthew Wilcox (Oracle) 	folio_redirty_for_writepage(wbc, folio);
1550e735c007SMatthew Wilcox (Oracle) 	folio_unlock(folio);
1551598ecfbaSChristoph Hellwig 	return 0;
1552598ecfbaSChristoph Hellwig }
1553598ecfbaSChristoph Hellwig 
1554598ecfbaSChristoph Hellwig int
1555598ecfbaSChristoph Hellwig iomap_writepage(struct page *page, struct writeback_control *wbc,
1556598ecfbaSChristoph Hellwig 		struct iomap_writepage_ctx *wpc,
1557598ecfbaSChristoph Hellwig 		const struct iomap_writeback_ops *ops)
1558598ecfbaSChristoph Hellwig {
1559598ecfbaSChristoph Hellwig 	int ret;
1560598ecfbaSChristoph Hellwig 
1561598ecfbaSChristoph Hellwig 	wpc->ops = ops;
1562598ecfbaSChristoph Hellwig 	ret = iomap_do_writepage(page, wbc, wpc);
1563598ecfbaSChristoph Hellwig 	if (!wpc->ioend)
1564598ecfbaSChristoph Hellwig 		return ret;
1565598ecfbaSChristoph Hellwig 	return iomap_submit_ioend(wpc, wpc->ioend, ret);
1566598ecfbaSChristoph Hellwig }
1567598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_writepage);
1568598ecfbaSChristoph Hellwig 
1569598ecfbaSChristoph Hellwig int
1570598ecfbaSChristoph Hellwig iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1571598ecfbaSChristoph Hellwig 		struct iomap_writepage_ctx *wpc,
1572598ecfbaSChristoph Hellwig 		const struct iomap_writeback_ops *ops)
1573598ecfbaSChristoph Hellwig {
1574598ecfbaSChristoph Hellwig 	int			ret;
1575598ecfbaSChristoph Hellwig 
1576598ecfbaSChristoph Hellwig 	wpc->ops = ops;
1577598ecfbaSChristoph Hellwig 	ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
1578598ecfbaSChristoph Hellwig 	if (!wpc->ioend)
1579598ecfbaSChristoph Hellwig 		return ret;
1580598ecfbaSChristoph Hellwig 	return iomap_submit_ioend(wpc, wpc->ioend, ret);
1581598ecfbaSChristoph Hellwig }
1582598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_writepages);
1583598ecfbaSChristoph Hellwig 
1584598ecfbaSChristoph Hellwig static int __init iomap_init(void)
1585598ecfbaSChristoph Hellwig {
1586598ecfbaSChristoph Hellwig 	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
1587598ecfbaSChristoph Hellwig 			   offsetof(struct iomap_ioend, io_inline_bio),
1588598ecfbaSChristoph Hellwig 			   BIOSET_NEED_BVECS);
1589598ecfbaSChristoph Hellwig }
1590598ecfbaSChristoph Hellwig fs_initcall(iomap_init);
1591