xref: /openbmc/linux/fs/iomap/buffered-io.c (revision ab069d5f)
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 
24ab08b01eSChristoph Hellwig /*
250a195b91SMatthew Wilcox (Oracle)  * Structure allocated for each page or THP when block size < page size
260a195b91SMatthew Wilcox (Oracle)  * to track sub-page uptodate status and I/O completions.
27ab08b01eSChristoph Hellwig  */
28ab08b01eSChristoph Hellwig struct iomap_page {
297d636676SMatthew Wilcox (Oracle) 	atomic_t		read_bytes_pending;
300fb2d720SMatthew Wilcox (Oracle) 	atomic_t		write_bytes_pending;
311cea335dSChristoph Hellwig 	spinlock_t		uptodate_lock;
320a195b91SMatthew Wilcox (Oracle) 	unsigned long		uptodate[];
33ab08b01eSChristoph Hellwig };
34ab08b01eSChristoph Hellwig 
35ab08b01eSChristoph Hellwig static inline struct iomap_page *to_iomap_page(struct page *page)
36ab08b01eSChristoph Hellwig {
370a195b91SMatthew Wilcox (Oracle) 	/*
380a195b91SMatthew Wilcox (Oracle) 	 * per-block data is stored in the head page.  Callers should
39f1f264b4SAndreas Gruenbacher 	 * not be dealing with tail pages, and if they are, they can
400a195b91SMatthew Wilcox (Oracle) 	 * call thp_head() first.
410a195b91SMatthew Wilcox (Oracle) 	 */
420a195b91SMatthew Wilcox (Oracle) 	VM_BUG_ON_PGFLAGS(PageTail(page), page);
430a195b91SMatthew Wilcox (Oracle) 
44ab08b01eSChristoph Hellwig 	if (page_has_private(page))
45ab08b01eSChristoph Hellwig 		return (struct iomap_page *)page_private(page);
46ab08b01eSChristoph Hellwig 	return NULL;
47ab08b01eSChristoph Hellwig }
48ab08b01eSChristoph Hellwig 
49598ecfbaSChristoph Hellwig static struct bio_set iomap_ioend_bioset;
50598ecfbaSChristoph Hellwig 
51afc51aaaSDarrick J. Wong static struct iomap_page *
52afc51aaaSDarrick J. Wong iomap_page_create(struct inode *inode, struct page *page)
53afc51aaaSDarrick J. Wong {
54afc51aaaSDarrick J. Wong 	struct iomap_page *iop = to_iomap_page(page);
550a195b91SMatthew Wilcox (Oracle) 	unsigned int nr_blocks = i_blocks_per_page(inode, page);
56afc51aaaSDarrick J. Wong 
570a195b91SMatthew Wilcox (Oracle) 	if (iop || nr_blocks <= 1)
58afc51aaaSDarrick J. Wong 		return iop;
59afc51aaaSDarrick J. Wong 
600a195b91SMatthew Wilcox (Oracle) 	iop = kzalloc(struct_size(iop, uptodate, BITS_TO_LONGS(nr_blocks)),
610a195b91SMatthew Wilcox (Oracle) 			GFP_NOFS | __GFP_NOFAIL);
621cea335dSChristoph Hellwig 	spin_lock_init(&iop->uptodate_lock);
634595a298SMatthew Wilcox (Oracle) 	if (PageUptodate(page))
644595a298SMatthew Wilcox (Oracle) 		bitmap_fill(iop->uptodate, nr_blocks);
6558aeb731SGuoqing Jiang 	attach_page_private(page, iop);
66afc51aaaSDarrick J. Wong 	return iop;
67afc51aaaSDarrick J. Wong }
68afc51aaaSDarrick J. Wong 
69afc51aaaSDarrick J. Wong static void
70afc51aaaSDarrick J. Wong iomap_page_release(struct page *page)
71afc51aaaSDarrick J. Wong {
7258aeb731SGuoqing Jiang 	struct iomap_page *iop = detach_page_private(page);
730a195b91SMatthew Wilcox (Oracle) 	unsigned int nr_blocks = i_blocks_per_page(page->mapping->host, page);
74afc51aaaSDarrick J. Wong 
75afc51aaaSDarrick J. Wong 	if (!iop)
76afc51aaaSDarrick J. Wong 		return;
777d636676SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(atomic_read(&iop->read_bytes_pending));
780fb2d720SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(atomic_read(&iop->write_bytes_pending));
790a195b91SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(bitmap_full(iop->uptodate, nr_blocks) !=
800a195b91SMatthew Wilcox (Oracle) 			PageUptodate(page));
81afc51aaaSDarrick J. Wong 	kfree(iop);
82afc51aaaSDarrick J. Wong }
83afc51aaaSDarrick J. Wong 
84afc51aaaSDarrick J. Wong /*
85afc51aaaSDarrick J. Wong  * Calculate the range inside the page that we actually need to read.
86afc51aaaSDarrick J. Wong  */
87afc51aaaSDarrick J. Wong static void
88afc51aaaSDarrick J. Wong iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
89afc51aaaSDarrick J. Wong 		loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
90afc51aaaSDarrick J. Wong {
91afc51aaaSDarrick J. Wong 	loff_t orig_pos = *pos;
92afc51aaaSDarrick J. Wong 	loff_t isize = i_size_read(inode);
93afc51aaaSDarrick J. Wong 	unsigned block_bits = inode->i_blkbits;
94afc51aaaSDarrick J. Wong 	unsigned block_size = (1 << block_bits);
95afc51aaaSDarrick J. Wong 	unsigned poff = offset_in_page(*pos);
96afc51aaaSDarrick J. Wong 	unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
97afc51aaaSDarrick J. Wong 	unsigned first = poff >> block_bits;
98afc51aaaSDarrick J. Wong 	unsigned last = (poff + plen - 1) >> block_bits;
99afc51aaaSDarrick J. Wong 
100afc51aaaSDarrick J. Wong 	/*
101f1f264b4SAndreas Gruenbacher 	 * If the block size is smaller than the page size, we need to check the
102afc51aaaSDarrick J. Wong 	 * per-block uptodate status and adjust the offset and length if needed
103afc51aaaSDarrick J. Wong 	 * to avoid reading in already uptodate ranges.
104afc51aaaSDarrick J. Wong 	 */
105afc51aaaSDarrick J. Wong 	if (iop) {
106afc51aaaSDarrick J. Wong 		unsigned int i;
107afc51aaaSDarrick J. Wong 
108afc51aaaSDarrick J. Wong 		/* move forward for each leading block marked uptodate */
109afc51aaaSDarrick J. Wong 		for (i = first; i <= last; i++) {
110afc51aaaSDarrick J. Wong 			if (!test_bit(i, iop->uptodate))
111afc51aaaSDarrick J. Wong 				break;
112afc51aaaSDarrick J. Wong 			*pos += block_size;
113afc51aaaSDarrick J. Wong 			poff += block_size;
114afc51aaaSDarrick J. Wong 			plen -= block_size;
115afc51aaaSDarrick J. Wong 			first++;
116afc51aaaSDarrick J. Wong 		}
117afc51aaaSDarrick J. Wong 
118afc51aaaSDarrick J. Wong 		/* truncate len if we find any trailing uptodate block(s) */
119afc51aaaSDarrick J. Wong 		for ( ; i <= last; i++) {
120afc51aaaSDarrick J. Wong 			if (test_bit(i, iop->uptodate)) {
121afc51aaaSDarrick J. Wong 				plen -= (last - i + 1) * block_size;
122afc51aaaSDarrick J. Wong 				last = i - 1;
123afc51aaaSDarrick J. Wong 				break;
124afc51aaaSDarrick J. Wong 			}
125afc51aaaSDarrick J. Wong 		}
126afc51aaaSDarrick J. Wong 	}
127afc51aaaSDarrick J. Wong 
128afc51aaaSDarrick J. Wong 	/*
129f1f264b4SAndreas Gruenbacher 	 * If the extent spans the block that contains the i_size, we need to
130afc51aaaSDarrick J. Wong 	 * handle both halves separately so that we properly zero data in the
131afc51aaaSDarrick J. Wong 	 * page cache for blocks that are entirely outside of i_size.
132afc51aaaSDarrick J. Wong 	 */
133afc51aaaSDarrick J. Wong 	if (orig_pos <= isize && orig_pos + length > isize) {
134afc51aaaSDarrick J. Wong 		unsigned end = offset_in_page(isize - 1) >> block_bits;
135afc51aaaSDarrick J. Wong 
136afc51aaaSDarrick J. Wong 		if (first <= end && last > end)
137afc51aaaSDarrick J. Wong 			plen -= (last - end) * block_size;
138afc51aaaSDarrick J. Wong 	}
139afc51aaaSDarrick J. Wong 
140afc51aaaSDarrick J. Wong 	*offp = poff;
141afc51aaaSDarrick J. Wong 	*lenp = plen;
142afc51aaaSDarrick J. Wong }
143afc51aaaSDarrick J. Wong 
144afc51aaaSDarrick J. Wong static void
1451cea335dSChristoph Hellwig iomap_iop_set_range_uptodate(struct page *page, unsigned off, unsigned len)
146afc51aaaSDarrick J. Wong {
147afc51aaaSDarrick J. Wong 	struct iomap_page *iop = to_iomap_page(page);
148afc51aaaSDarrick J. Wong 	struct inode *inode = page->mapping->host;
149afc51aaaSDarrick J. Wong 	unsigned first = off >> inode->i_blkbits;
150afc51aaaSDarrick J. Wong 	unsigned last = (off + len - 1) >> inode->i_blkbits;
1511cea335dSChristoph Hellwig 	unsigned long flags;
152afc51aaaSDarrick J. Wong 
1531cea335dSChristoph Hellwig 	spin_lock_irqsave(&iop->uptodate_lock, flags);
154b21866f5SMatthew Wilcox (Oracle) 	bitmap_set(iop->uptodate, first, last - first + 1);
155b21866f5SMatthew Wilcox (Oracle) 	if (bitmap_full(iop->uptodate, i_blocks_per_page(inode, page)))
1561cea335dSChristoph Hellwig 		SetPageUptodate(page);
1571cea335dSChristoph Hellwig 	spin_unlock_irqrestore(&iop->uptodate_lock, flags);
158afc51aaaSDarrick J. Wong }
159afc51aaaSDarrick J. Wong 
1601cea335dSChristoph Hellwig static void
1611cea335dSChristoph Hellwig iomap_set_range_uptodate(struct page *page, unsigned off, unsigned len)
1621cea335dSChristoph Hellwig {
1631cea335dSChristoph Hellwig 	if (PageError(page))
1641cea335dSChristoph Hellwig 		return;
1651cea335dSChristoph Hellwig 
1661cea335dSChristoph Hellwig 	if (page_has_private(page))
1671cea335dSChristoph Hellwig 		iomap_iop_set_range_uptodate(page, off, len);
1681cea335dSChristoph Hellwig 	else
169afc51aaaSDarrick J. Wong 		SetPageUptodate(page);
170afc51aaaSDarrick J. Wong }
171afc51aaaSDarrick J. Wong 
172afc51aaaSDarrick J. Wong static void
173afc51aaaSDarrick J. Wong iomap_read_page_end_io(struct bio_vec *bvec, int error)
174afc51aaaSDarrick J. Wong {
175afc51aaaSDarrick J. Wong 	struct page *page = bvec->bv_page;
176afc51aaaSDarrick J. Wong 	struct iomap_page *iop = to_iomap_page(page);
177afc51aaaSDarrick J. Wong 
178afc51aaaSDarrick J. Wong 	if (unlikely(error)) {
179afc51aaaSDarrick J. Wong 		ClearPageUptodate(page);
180afc51aaaSDarrick J. Wong 		SetPageError(page);
181afc51aaaSDarrick J. Wong 	} else {
182afc51aaaSDarrick J. Wong 		iomap_set_range_uptodate(page, bvec->bv_offset, bvec->bv_len);
183afc51aaaSDarrick J. Wong 	}
184afc51aaaSDarrick J. Wong 
1857d636676SMatthew Wilcox (Oracle) 	if (!iop || atomic_sub_and_test(bvec->bv_len, &iop->read_bytes_pending))
1867d636676SMatthew Wilcox (Oracle) 		unlock_page(page);
187afc51aaaSDarrick J. Wong }
188afc51aaaSDarrick J. Wong 
189afc51aaaSDarrick J. Wong static void
190afc51aaaSDarrick J. Wong iomap_read_end_io(struct bio *bio)
191afc51aaaSDarrick J. Wong {
192afc51aaaSDarrick J. Wong 	int error = blk_status_to_errno(bio->bi_status);
193afc51aaaSDarrick J. Wong 	struct bio_vec *bvec;
194afc51aaaSDarrick J. Wong 	struct bvec_iter_all iter_all;
195afc51aaaSDarrick J. Wong 
196afc51aaaSDarrick J. Wong 	bio_for_each_segment_all(bvec, bio, iter_all)
197afc51aaaSDarrick J. Wong 		iomap_read_page_end_io(bvec, error);
198afc51aaaSDarrick J. Wong 	bio_put(bio);
199afc51aaaSDarrick J. Wong }
200afc51aaaSDarrick J. Wong 
201afc51aaaSDarrick J. Wong struct iomap_readpage_ctx {
202afc51aaaSDarrick J. Wong 	struct page		*cur_page;
203afc51aaaSDarrick J. Wong 	bool			cur_page_in_bio;
204afc51aaaSDarrick J. Wong 	struct bio		*bio;
2059d24a13aSMatthew Wilcox (Oracle) 	struct readahead_control *rac;
206afc51aaaSDarrick J. Wong };
207afc51aaaSDarrick J. Wong 
20869f4a26cSGao Xiang static int iomap_read_inline_data(struct inode *inode, struct page *page,
209afc51aaaSDarrick J. Wong 		struct iomap *iomap)
210afc51aaaSDarrick J. Wong {
21169f4a26cSGao Xiang 	size_t size = i_size_read(inode) - iomap->offset;
212b405435bSMatthew Wilcox (Oracle) 	size_t poff = offset_in_page(iomap->offset);
213afc51aaaSDarrick J. Wong 	void *addr;
214afc51aaaSDarrick J. Wong 
215afc51aaaSDarrick J. Wong 	if (PageUptodate(page))
216b405435bSMatthew Wilcox (Oracle) 		return PAGE_SIZE - poff;
217afc51aaaSDarrick J. Wong 
21869f4a26cSGao Xiang 	if (WARN_ON_ONCE(size > PAGE_SIZE -
21969f4a26cSGao Xiang 			 offset_in_page(iomap->inline_data)))
22069f4a26cSGao Xiang 		return -EIO;
22169f4a26cSGao Xiang 	if (WARN_ON_ONCE(size > iomap->length))
22269f4a26cSGao Xiang 		return -EIO;
223b405435bSMatthew Wilcox (Oracle) 	if (poff > 0)
224b405435bSMatthew Wilcox (Oracle) 		iomap_page_create(inode, page);
225afc51aaaSDarrick J. Wong 
226*ab069d5fSMatthew Wilcox (Oracle) 	addr = kmap_local_page(page) + poff;
227afc51aaaSDarrick J. Wong 	memcpy(addr, iomap->inline_data, size);
228b405435bSMatthew Wilcox (Oracle) 	memset(addr + size, 0, PAGE_SIZE - poff - size);
229*ab069d5fSMatthew Wilcox (Oracle) 	kunmap_local(addr);
230b405435bSMatthew Wilcox (Oracle) 	iomap_set_range_uptodate(page, poff, PAGE_SIZE - poff);
231b405435bSMatthew Wilcox (Oracle) 	return PAGE_SIZE - poff;
232afc51aaaSDarrick J. Wong }
233afc51aaaSDarrick J. Wong 
234009d8d84SChristoph Hellwig static inline bool iomap_block_needs_zeroing(struct inode *inode,
235009d8d84SChristoph Hellwig 		struct iomap *iomap, loff_t pos)
236009d8d84SChristoph Hellwig {
237009d8d84SChristoph Hellwig 	return iomap->type != IOMAP_MAPPED ||
238009d8d84SChristoph Hellwig 		(iomap->flags & IOMAP_F_NEW) ||
239009d8d84SChristoph Hellwig 		pos >= i_size_read(inode);
240009d8d84SChristoph Hellwig }
241009d8d84SChristoph Hellwig 
242afc51aaaSDarrick J. Wong static loff_t
243afc51aaaSDarrick J. Wong iomap_readpage_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
244c039b997SGoldwyn Rodrigues 		struct iomap *iomap, struct iomap *srcmap)
245afc51aaaSDarrick J. Wong {
246afc51aaaSDarrick J. Wong 	struct iomap_readpage_ctx *ctx = data;
247afc51aaaSDarrick J. Wong 	struct page *page = ctx->cur_page;
248637d3375SAndreas Gruenbacher 	struct iomap_page *iop;
249afc51aaaSDarrick J. Wong 	loff_t orig_pos = pos;
250afc51aaaSDarrick J. Wong 	unsigned poff, plen;
251afc51aaaSDarrick J. Wong 	sector_t sector;
252afc51aaaSDarrick J. Wong 
253b405435bSMatthew Wilcox (Oracle) 	if (iomap->type == IOMAP_INLINE)
254b405435bSMatthew Wilcox (Oracle) 		return iomap_read_inline_data(inode, page, iomap);
255afc51aaaSDarrick J. Wong 
256afc51aaaSDarrick J. Wong 	/* zero post-eof blocks as the page may be mapped */
257637d3375SAndreas Gruenbacher 	iop = iomap_page_create(inode, page);
258afc51aaaSDarrick J. Wong 	iomap_adjust_read_range(inode, iop, &pos, length, &poff, &plen);
259afc51aaaSDarrick J. Wong 	if (plen == 0)
260afc51aaaSDarrick J. Wong 		goto done;
261afc51aaaSDarrick J. Wong 
262009d8d84SChristoph Hellwig 	if (iomap_block_needs_zeroing(inode, iomap, pos)) {
263afc51aaaSDarrick J. Wong 		zero_user(page, poff, plen);
264afc51aaaSDarrick J. Wong 		iomap_set_range_uptodate(page, poff, plen);
265afc51aaaSDarrick J. Wong 		goto done;
266afc51aaaSDarrick J. Wong 	}
267afc51aaaSDarrick J. Wong 
268afc51aaaSDarrick J. Wong 	ctx->cur_page_in_bio = true;
2697d636676SMatthew Wilcox (Oracle) 	if (iop)
2707d636676SMatthew Wilcox (Oracle) 		atomic_add(plen, &iop->read_bytes_pending);
271afc51aaaSDarrick J. Wong 
272afc51aaaSDarrick J. Wong 	sector = iomap_sector(iomap, pos);
273d0364f94SChristoph Hellwig 	if (!ctx->bio ||
274d0364f94SChristoph Hellwig 	    bio_end_sector(ctx->bio) != sector ||
275d0364f94SChristoph Hellwig 	    bio_add_page(ctx->bio, page, plen, poff) != plen) {
276afc51aaaSDarrick J. Wong 		gfp_t gfp = mapping_gfp_constraint(page->mapping, GFP_KERNEL);
277457df33eSMatthew Wilcox (Oracle) 		gfp_t orig_gfp = gfp;
2785f7136dbSMatthew Wilcox (Oracle) 		unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
279afc51aaaSDarrick J. Wong 
280afc51aaaSDarrick J. Wong 		if (ctx->bio)
281afc51aaaSDarrick J. Wong 			submit_bio(ctx->bio);
282afc51aaaSDarrick J. Wong 
2839d24a13aSMatthew Wilcox (Oracle) 		if (ctx->rac) /* same as readahead_gfp_mask */
284afc51aaaSDarrick J. Wong 			gfp |= __GFP_NORETRY | __GFP_NOWARN;
2855f7136dbSMatthew Wilcox (Oracle) 		ctx->bio = bio_alloc(gfp, bio_max_segs(nr_vecs));
286457df33eSMatthew Wilcox (Oracle) 		/*
287457df33eSMatthew Wilcox (Oracle) 		 * If the bio_alloc fails, try it again for a single page to
288457df33eSMatthew Wilcox (Oracle) 		 * avoid having to deal with partial page reads.  This emulates
289457df33eSMatthew Wilcox (Oracle) 		 * what do_mpage_readpage does.
290457df33eSMatthew Wilcox (Oracle) 		 */
291457df33eSMatthew Wilcox (Oracle) 		if (!ctx->bio)
292457df33eSMatthew Wilcox (Oracle) 			ctx->bio = bio_alloc(orig_gfp, 1);
293afc51aaaSDarrick J. Wong 		ctx->bio->bi_opf = REQ_OP_READ;
2949d24a13aSMatthew Wilcox (Oracle) 		if (ctx->rac)
295afc51aaaSDarrick J. Wong 			ctx->bio->bi_opf |= REQ_RAHEAD;
296afc51aaaSDarrick J. Wong 		ctx->bio->bi_iter.bi_sector = sector;
297afc51aaaSDarrick J. Wong 		bio_set_dev(ctx->bio, iomap->bdev);
298afc51aaaSDarrick J. Wong 		ctx->bio->bi_end_io = iomap_read_end_io;
299d0364f94SChristoph Hellwig 		__bio_add_page(ctx->bio, page, plen, poff);
300afc51aaaSDarrick J. Wong 	}
301afc51aaaSDarrick J. Wong done:
302afc51aaaSDarrick J. Wong 	/*
303afc51aaaSDarrick J. Wong 	 * Move the caller beyond our range so that it keeps making progress.
304f1f264b4SAndreas Gruenbacher 	 * For that, we have to include any leading non-uptodate ranges, but
305afc51aaaSDarrick J. Wong 	 * we can skip trailing ones as they will be handled in the next
306afc51aaaSDarrick J. Wong 	 * iteration.
307afc51aaaSDarrick J. Wong 	 */
308afc51aaaSDarrick J. Wong 	return pos - orig_pos + plen;
309afc51aaaSDarrick J. Wong }
310afc51aaaSDarrick J. Wong 
311afc51aaaSDarrick J. Wong int
312afc51aaaSDarrick J. Wong iomap_readpage(struct page *page, const struct iomap_ops *ops)
313afc51aaaSDarrick J. Wong {
314afc51aaaSDarrick J. Wong 	struct iomap_readpage_ctx ctx = { .cur_page = page };
315afc51aaaSDarrick J. Wong 	struct inode *inode = page->mapping->host;
316afc51aaaSDarrick J. Wong 	unsigned poff;
317afc51aaaSDarrick J. Wong 	loff_t ret;
318afc51aaaSDarrick J. Wong 
3199e91c572SChristoph Hellwig 	trace_iomap_readpage(page->mapping->host, 1);
3209e91c572SChristoph Hellwig 
321afc51aaaSDarrick J. Wong 	for (poff = 0; poff < PAGE_SIZE; poff += ret) {
322afc51aaaSDarrick J. Wong 		ret = iomap_apply(inode, page_offset(page) + poff,
323afc51aaaSDarrick J. Wong 				PAGE_SIZE - poff, 0, ops, &ctx,
324afc51aaaSDarrick J. Wong 				iomap_readpage_actor);
325afc51aaaSDarrick J. Wong 		if (ret <= 0) {
326afc51aaaSDarrick J. Wong 			WARN_ON_ONCE(ret == 0);
327afc51aaaSDarrick J. Wong 			SetPageError(page);
328afc51aaaSDarrick J. Wong 			break;
329afc51aaaSDarrick J. Wong 		}
330afc51aaaSDarrick J. Wong 	}
331afc51aaaSDarrick J. Wong 
332afc51aaaSDarrick J. Wong 	if (ctx.bio) {
333afc51aaaSDarrick J. Wong 		submit_bio(ctx.bio);
334afc51aaaSDarrick J. Wong 		WARN_ON_ONCE(!ctx.cur_page_in_bio);
335afc51aaaSDarrick J. Wong 	} else {
336afc51aaaSDarrick J. Wong 		WARN_ON_ONCE(ctx.cur_page_in_bio);
337afc51aaaSDarrick J. Wong 		unlock_page(page);
338afc51aaaSDarrick J. Wong 	}
339afc51aaaSDarrick J. Wong 
340afc51aaaSDarrick J. Wong 	/*
341f1f264b4SAndreas Gruenbacher 	 * Just like mpage_readahead and block_read_full_page, we always
342afc51aaaSDarrick J. Wong 	 * return 0 and just mark the page as PageError on errors.  This
343f1f264b4SAndreas Gruenbacher 	 * should be cleaned up throughout the stack eventually.
344afc51aaaSDarrick J. Wong 	 */
345afc51aaaSDarrick J. Wong 	return 0;
346afc51aaaSDarrick J. Wong }
347afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_readpage);
348afc51aaaSDarrick J. Wong 
349afc51aaaSDarrick J. Wong static loff_t
3509d24a13aSMatthew Wilcox (Oracle) iomap_readahead_actor(struct inode *inode, loff_t pos, loff_t length,
351c039b997SGoldwyn Rodrigues 		void *data, struct iomap *iomap, struct iomap *srcmap)
352afc51aaaSDarrick J. Wong {
353afc51aaaSDarrick J. Wong 	struct iomap_readpage_ctx *ctx = data;
354afc51aaaSDarrick J. Wong 	loff_t done, ret;
355afc51aaaSDarrick J. Wong 
356afc51aaaSDarrick J. Wong 	for (done = 0; done < length; done += ret) {
357afc51aaaSDarrick J. Wong 		if (ctx->cur_page && offset_in_page(pos + done) == 0) {
358afc51aaaSDarrick J. Wong 			if (!ctx->cur_page_in_bio)
359afc51aaaSDarrick J. Wong 				unlock_page(ctx->cur_page);
360afc51aaaSDarrick J. Wong 			put_page(ctx->cur_page);
361afc51aaaSDarrick J. Wong 			ctx->cur_page = NULL;
362afc51aaaSDarrick J. Wong 		}
363afc51aaaSDarrick J. Wong 		if (!ctx->cur_page) {
3649d24a13aSMatthew Wilcox (Oracle) 			ctx->cur_page = readahead_page(ctx->rac);
365afc51aaaSDarrick J. Wong 			ctx->cur_page_in_bio = false;
366afc51aaaSDarrick J. Wong 		}
367afc51aaaSDarrick J. Wong 		ret = iomap_readpage_actor(inode, pos + done, length - done,
368c039b997SGoldwyn Rodrigues 				ctx, iomap, srcmap);
369afc51aaaSDarrick J. Wong 	}
370afc51aaaSDarrick J. Wong 
371afc51aaaSDarrick J. Wong 	return done;
372afc51aaaSDarrick J. Wong }
373afc51aaaSDarrick J. Wong 
3749d24a13aSMatthew Wilcox (Oracle) /**
3759d24a13aSMatthew Wilcox (Oracle)  * iomap_readahead - Attempt to read pages from a file.
3769d24a13aSMatthew Wilcox (Oracle)  * @rac: Describes the pages to be read.
3779d24a13aSMatthew Wilcox (Oracle)  * @ops: The operations vector for the filesystem.
3789d24a13aSMatthew Wilcox (Oracle)  *
3799d24a13aSMatthew Wilcox (Oracle)  * This function is for filesystems to call to implement their readahead
3809d24a13aSMatthew Wilcox (Oracle)  * address_space operation.
3819d24a13aSMatthew Wilcox (Oracle)  *
3829d24a13aSMatthew Wilcox (Oracle)  * Context: The @ops callbacks may submit I/O (eg to read the addresses of
3839d24a13aSMatthew Wilcox (Oracle)  * blocks from disc), and may wait for it.  The caller may be trying to
3849d24a13aSMatthew Wilcox (Oracle)  * access a different page, and so sleeping excessively should be avoided.
3859d24a13aSMatthew Wilcox (Oracle)  * It may allocate memory, but should avoid costly allocations.  This
3869d24a13aSMatthew Wilcox (Oracle)  * function is called with memalloc_nofs set, so allocations will not cause
3879d24a13aSMatthew Wilcox (Oracle)  * the filesystem to be reentered.
3889d24a13aSMatthew Wilcox (Oracle)  */
3899d24a13aSMatthew Wilcox (Oracle) void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
390afc51aaaSDarrick J. Wong {
3919d24a13aSMatthew Wilcox (Oracle) 	struct inode *inode = rac->mapping->host;
3929d24a13aSMatthew Wilcox (Oracle) 	loff_t pos = readahead_pos(rac);
393076171a6SMatthew Wilcox (Oracle) 	size_t length = readahead_length(rac);
394afc51aaaSDarrick J. Wong 	struct iomap_readpage_ctx ctx = {
3959d24a13aSMatthew Wilcox (Oracle) 		.rac	= rac,
396afc51aaaSDarrick J. Wong 	};
397afc51aaaSDarrick J. Wong 
3989d24a13aSMatthew Wilcox (Oracle) 	trace_iomap_readahead(inode, readahead_count(rac));
3999e91c572SChristoph Hellwig 
400afc51aaaSDarrick J. Wong 	while (length > 0) {
401076171a6SMatthew Wilcox (Oracle) 		ssize_t ret = iomap_apply(inode, pos, length, 0, ops,
4029d24a13aSMatthew Wilcox (Oracle) 				&ctx, iomap_readahead_actor);
403afc51aaaSDarrick J. Wong 		if (ret <= 0) {
404afc51aaaSDarrick J. Wong 			WARN_ON_ONCE(ret == 0);
4059d24a13aSMatthew Wilcox (Oracle) 			break;
406afc51aaaSDarrick J. Wong 		}
407afc51aaaSDarrick J. Wong 		pos += ret;
408afc51aaaSDarrick J. Wong 		length -= ret;
409afc51aaaSDarrick J. Wong 	}
4109d24a13aSMatthew Wilcox (Oracle) 
411afc51aaaSDarrick J. Wong 	if (ctx.bio)
412afc51aaaSDarrick J. Wong 		submit_bio(ctx.bio);
413afc51aaaSDarrick J. Wong 	if (ctx.cur_page) {
414afc51aaaSDarrick J. Wong 		if (!ctx.cur_page_in_bio)
415afc51aaaSDarrick J. Wong 			unlock_page(ctx.cur_page);
416afc51aaaSDarrick J. Wong 		put_page(ctx.cur_page);
417afc51aaaSDarrick J. Wong 	}
418afc51aaaSDarrick J. Wong }
4199d24a13aSMatthew Wilcox (Oracle) EXPORT_SYMBOL_GPL(iomap_readahead);
420afc51aaaSDarrick J. Wong 
421afc51aaaSDarrick J. Wong /*
422afc51aaaSDarrick J. Wong  * iomap_is_partially_uptodate checks whether blocks within a page are
423afc51aaaSDarrick J. Wong  * uptodate or not.
424afc51aaaSDarrick J. Wong  *
425afc51aaaSDarrick J. Wong  * Returns true if all blocks which correspond to a file portion
426afc51aaaSDarrick J. Wong  * we want to read within the page are uptodate.
427afc51aaaSDarrick J. Wong  */
428afc51aaaSDarrick J. Wong int
429afc51aaaSDarrick J. Wong iomap_is_partially_uptodate(struct page *page, unsigned long from,
430afc51aaaSDarrick J. Wong 		unsigned long count)
431afc51aaaSDarrick J. Wong {
432afc51aaaSDarrick J. Wong 	struct iomap_page *iop = to_iomap_page(page);
433afc51aaaSDarrick J. Wong 	struct inode *inode = page->mapping->host;
434afc51aaaSDarrick J. Wong 	unsigned len, first, last;
435afc51aaaSDarrick J. Wong 	unsigned i;
436afc51aaaSDarrick J. Wong 
437afc51aaaSDarrick J. Wong 	/* Limit range to one page */
438afc51aaaSDarrick J. Wong 	len = min_t(unsigned, PAGE_SIZE - from, count);
439afc51aaaSDarrick J. Wong 
440afc51aaaSDarrick J. Wong 	/* First and last blocks in range within page */
441afc51aaaSDarrick J. Wong 	first = from >> inode->i_blkbits;
442afc51aaaSDarrick J. Wong 	last = (from + len - 1) >> inode->i_blkbits;
443afc51aaaSDarrick J. Wong 
444afc51aaaSDarrick J. Wong 	if (iop) {
445afc51aaaSDarrick J. Wong 		for (i = first; i <= last; i++)
446afc51aaaSDarrick J. Wong 			if (!test_bit(i, iop->uptodate))
447afc51aaaSDarrick J. Wong 				return 0;
448afc51aaaSDarrick J. Wong 		return 1;
449afc51aaaSDarrick J. Wong 	}
450afc51aaaSDarrick J. Wong 
451afc51aaaSDarrick J. Wong 	return 0;
452afc51aaaSDarrick J. Wong }
453afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
454afc51aaaSDarrick J. Wong 
455afc51aaaSDarrick J. Wong int
456afc51aaaSDarrick J. Wong iomap_releasepage(struct page *page, gfp_t gfp_mask)
457afc51aaaSDarrick J. Wong {
4581ac99452SMatthew Wilcox (Oracle) 	trace_iomap_releasepage(page->mapping->host, page_offset(page),
4591ac99452SMatthew Wilcox (Oracle) 			PAGE_SIZE);
4609e91c572SChristoph Hellwig 
461afc51aaaSDarrick J. Wong 	/*
462afc51aaaSDarrick J. Wong 	 * mm accommodates an old ext3 case where clean pages might not have had
463afc51aaaSDarrick J. Wong 	 * the dirty bit cleared. Thus, it can send actual dirty pages to
464f1f264b4SAndreas Gruenbacher 	 * ->releasepage() via shrink_active_list(); skip those here.
465afc51aaaSDarrick J. Wong 	 */
466afc51aaaSDarrick J. Wong 	if (PageDirty(page) || PageWriteback(page))
467afc51aaaSDarrick J. Wong 		return 0;
468afc51aaaSDarrick J. Wong 	iomap_page_release(page);
469afc51aaaSDarrick J. Wong 	return 1;
470afc51aaaSDarrick J. Wong }
471afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_releasepage);
472afc51aaaSDarrick J. Wong 
473afc51aaaSDarrick J. Wong void
474afc51aaaSDarrick J. Wong iomap_invalidatepage(struct page *page, unsigned int offset, unsigned int len)
475afc51aaaSDarrick J. Wong {
4761ac99452SMatthew Wilcox (Oracle) 	trace_iomap_invalidatepage(page->mapping->host, offset, len);
4779e91c572SChristoph Hellwig 
478afc51aaaSDarrick J. Wong 	/*
479f1f264b4SAndreas Gruenbacher 	 * If we're invalidating the entire page, clear the dirty state from it
480afc51aaaSDarrick J. Wong 	 * and release it to avoid unnecessary buildup of the LRU.
481afc51aaaSDarrick J. Wong 	 */
482afc51aaaSDarrick J. Wong 	if (offset == 0 && len == PAGE_SIZE) {
483afc51aaaSDarrick J. Wong 		WARN_ON_ONCE(PageWriteback(page));
484afc51aaaSDarrick J. Wong 		cancel_dirty_page(page);
485afc51aaaSDarrick J. Wong 		iomap_page_release(page);
486afc51aaaSDarrick J. Wong 	}
487afc51aaaSDarrick J. Wong }
488afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_invalidatepage);
489afc51aaaSDarrick J. Wong 
490afc51aaaSDarrick J. Wong #ifdef CONFIG_MIGRATION
491afc51aaaSDarrick J. Wong int
492afc51aaaSDarrick J. Wong iomap_migrate_page(struct address_space *mapping, struct page *newpage,
493afc51aaaSDarrick J. Wong 		struct page *page, enum migrate_mode mode)
494afc51aaaSDarrick J. Wong {
495afc51aaaSDarrick J. Wong 	int ret;
496afc51aaaSDarrick J. Wong 
49726473f83SLinus Torvalds 	ret = migrate_page_move_mapping(mapping, newpage, page, 0);
498afc51aaaSDarrick J. Wong 	if (ret != MIGRATEPAGE_SUCCESS)
499afc51aaaSDarrick J. Wong 		return ret;
500afc51aaaSDarrick J. Wong 
50158aeb731SGuoqing Jiang 	if (page_has_private(page))
50258aeb731SGuoqing Jiang 		attach_page_private(newpage, detach_page_private(page));
503afc51aaaSDarrick J. Wong 
504afc51aaaSDarrick J. Wong 	if (mode != MIGRATE_SYNC_NO_COPY)
505afc51aaaSDarrick J. Wong 		migrate_page_copy(newpage, page);
506afc51aaaSDarrick J. Wong 	else
507afc51aaaSDarrick J. Wong 		migrate_page_states(newpage, page);
508afc51aaaSDarrick J. Wong 	return MIGRATEPAGE_SUCCESS;
509afc51aaaSDarrick J. Wong }
510afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_migrate_page);
511afc51aaaSDarrick J. Wong #endif /* CONFIG_MIGRATION */
512afc51aaaSDarrick J. Wong 
51332a38a49SChristoph Hellwig enum {
51432a38a49SChristoph Hellwig 	IOMAP_WRITE_F_UNSHARE		= (1 << 0),
51532a38a49SChristoph Hellwig };
51632a38a49SChristoph Hellwig 
517afc51aaaSDarrick J. Wong static void
518afc51aaaSDarrick J. Wong iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
519afc51aaaSDarrick J. Wong {
520afc51aaaSDarrick J. Wong 	loff_t i_size = i_size_read(inode);
521afc51aaaSDarrick J. Wong 
522afc51aaaSDarrick J. Wong 	/*
523afc51aaaSDarrick J. Wong 	 * Only truncate newly allocated pages beyoned EOF, even if the
524afc51aaaSDarrick J. Wong 	 * write started inside the existing inode size.
525afc51aaaSDarrick J. Wong 	 */
526afc51aaaSDarrick J. Wong 	if (pos + len > i_size)
527afc51aaaSDarrick J. Wong 		truncate_pagecache_range(inode, max(pos, i_size), pos + len);
528afc51aaaSDarrick J. Wong }
529afc51aaaSDarrick J. Wong 
530afc51aaaSDarrick J. Wong static int
531d3b40439SChristoph Hellwig iomap_read_page_sync(loff_t block_start, struct page *page, unsigned poff,
532d3b40439SChristoph Hellwig 		unsigned plen, struct iomap *iomap)
533afc51aaaSDarrick J. Wong {
534afc51aaaSDarrick J. Wong 	struct bio_vec bvec;
535afc51aaaSDarrick J. Wong 	struct bio bio;
536afc51aaaSDarrick J. Wong 
537afc51aaaSDarrick J. Wong 	bio_init(&bio, &bvec, 1);
538afc51aaaSDarrick J. Wong 	bio.bi_opf = REQ_OP_READ;
539afc51aaaSDarrick J. Wong 	bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
540afc51aaaSDarrick J. Wong 	bio_set_dev(&bio, iomap->bdev);
541afc51aaaSDarrick J. Wong 	__bio_add_page(&bio, page, plen, poff);
542afc51aaaSDarrick J. Wong 	return submit_bio_wait(&bio);
543afc51aaaSDarrick J. Wong }
544afc51aaaSDarrick J. Wong 
545afc51aaaSDarrick J. Wong static int
54632a38a49SChristoph Hellwig __iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, int flags,
547c039b997SGoldwyn Rodrigues 		struct page *page, struct iomap *srcmap)
548afc51aaaSDarrick J. Wong {
549afc51aaaSDarrick J. Wong 	struct iomap_page *iop = iomap_page_create(inode, page);
550afc51aaaSDarrick J. Wong 	loff_t block_size = i_blocksize(inode);
5516cc19c5fSNikolay Borisov 	loff_t block_start = round_down(pos, block_size);
5526cc19c5fSNikolay Borisov 	loff_t block_end = round_up(pos + len, block_size);
553afc51aaaSDarrick J. Wong 	unsigned from = offset_in_page(pos), to = from + len, poff, plen;
554afc51aaaSDarrick J. Wong 
555afc51aaaSDarrick J. Wong 	if (PageUptodate(page))
556afc51aaaSDarrick J. Wong 		return 0;
557e6e7ca92SMatthew Wilcox (Oracle) 	ClearPageError(page);
558afc51aaaSDarrick J. Wong 
559afc51aaaSDarrick J. Wong 	do {
560afc51aaaSDarrick J. Wong 		iomap_adjust_read_range(inode, iop, &block_start,
561afc51aaaSDarrick J. Wong 				block_end - block_start, &poff, &plen);
562afc51aaaSDarrick J. Wong 		if (plen == 0)
563afc51aaaSDarrick J. Wong 			break;
564afc51aaaSDarrick J. Wong 
56532a38a49SChristoph Hellwig 		if (!(flags & IOMAP_WRITE_F_UNSHARE) &&
56632a38a49SChristoph Hellwig 		    (from <= poff || from >= poff + plen) &&
567d3b40439SChristoph Hellwig 		    (to <= poff || to >= poff + plen))
568d3b40439SChristoph Hellwig 			continue;
569d3b40439SChristoph Hellwig 
570c039b997SGoldwyn Rodrigues 		if (iomap_block_needs_zeroing(inode, srcmap, block_start)) {
57132a38a49SChristoph Hellwig 			if (WARN_ON_ONCE(flags & IOMAP_WRITE_F_UNSHARE))
57232a38a49SChristoph Hellwig 				return -EIO;
573d3b40439SChristoph Hellwig 			zero_user_segments(page, poff, from, to, poff + plen);
57414284fedSMatthew Wilcox (Oracle) 		} else {
57514284fedSMatthew Wilcox (Oracle) 			int status = iomap_read_page_sync(block_start, page,
57614284fedSMatthew Wilcox (Oracle) 					poff, plen, srcmap);
577d3b40439SChristoph Hellwig 			if (status)
578d3b40439SChristoph Hellwig 				return status;
57914284fedSMatthew Wilcox (Oracle) 		}
58014284fedSMatthew Wilcox (Oracle) 		iomap_set_range_uptodate(page, poff, plen);
581afc51aaaSDarrick J. Wong 	} while ((block_start += plen) < block_end);
582afc51aaaSDarrick J. Wong 
583d3b40439SChristoph Hellwig 	return 0;
584afc51aaaSDarrick J. Wong }
585afc51aaaSDarrick J. Wong 
58669f4a26cSGao Xiang static int iomap_write_begin_inline(struct inode *inode,
58769f4a26cSGao Xiang 		struct page *page, struct iomap *srcmap)
58869f4a26cSGao Xiang {
589b405435bSMatthew Wilcox (Oracle) 	int ret;
590b405435bSMatthew Wilcox (Oracle) 
59169f4a26cSGao Xiang 	/* needs more work for the tailpacking case; disable for now */
59269f4a26cSGao Xiang 	if (WARN_ON_ONCE(srcmap->offset != 0))
59369f4a26cSGao Xiang 		return -EIO;
594b405435bSMatthew Wilcox (Oracle) 	ret = iomap_read_inline_data(inode, page, srcmap);
595b405435bSMatthew Wilcox (Oracle) 	if (ret < 0)
596b405435bSMatthew Wilcox (Oracle) 		return ret;
597b405435bSMatthew Wilcox (Oracle) 	return 0;
59869f4a26cSGao Xiang }
59969f4a26cSGao Xiang 
600afc51aaaSDarrick J. Wong static int
601afc51aaaSDarrick J. Wong iomap_write_begin(struct inode *inode, loff_t pos, unsigned len, unsigned flags,
602c039b997SGoldwyn Rodrigues 		struct page **pagep, struct iomap *iomap, struct iomap *srcmap)
603afc51aaaSDarrick J. Wong {
604afc51aaaSDarrick J. Wong 	const struct iomap_page_ops *page_ops = iomap->page_ops;
605afc51aaaSDarrick J. Wong 	struct page *page;
606afc51aaaSDarrick J. Wong 	int status = 0;
607afc51aaaSDarrick J. Wong 
608afc51aaaSDarrick J. Wong 	BUG_ON(pos + len > iomap->offset + iomap->length);
609c039b997SGoldwyn Rodrigues 	if (srcmap != iomap)
610c039b997SGoldwyn Rodrigues 		BUG_ON(pos + len > srcmap->offset + srcmap->length);
611afc51aaaSDarrick J. Wong 
612afc51aaaSDarrick J. Wong 	if (fatal_signal_pending(current))
613afc51aaaSDarrick J. Wong 		return -EINTR;
614afc51aaaSDarrick J. Wong 
615afc51aaaSDarrick J. Wong 	if (page_ops && page_ops->page_prepare) {
616afc51aaaSDarrick J. Wong 		status = page_ops->page_prepare(inode, pos, len, iomap);
617afc51aaaSDarrick J. Wong 		if (status)
618afc51aaaSDarrick J. Wong 			return status;
619afc51aaaSDarrick J. Wong 	}
620afc51aaaSDarrick J. Wong 
621dcd6158dSChristoph Hellwig 	page = grab_cache_page_write_begin(inode->i_mapping, pos >> PAGE_SHIFT,
622dcd6158dSChristoph Hellwig 			AOP_FLAG_NOFS);
623afc51aaaSDarrick J. Wong 	if (!page) {
624afc51aaaSDarrick J. Wong 		status = -ENOMEM;
625afc51aaaSDarrick J. Wong 		goto out_no_page;
626afc51aaaSDarrick J. Wong 	}
627afc51aaaSDarrick J. Wong 
628c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_INLINE)
62969f4a26cSGao Xiang 		status = iomap_write_begin_inline(inode, page, srcmap);
630afc51aaaSDarrick J. Wong 	else if (iomap->flags & IOMAP_F_BUFFER_HEAD)
631c039b997SGoldwyn Rodrigues 		status = __block_write_begin_int(page, pos, len, NULL, srcmap);
632afc51aaaSDarrick J. Wong 	else
63332a38a49SChristoph Hellwig 		status = __iomap_write_begin(inode, pos, len, flags, page,
634c039b997SGoldwyn Rodrigues 				srcmap);
635afc51aaaSDarrick J. Wong 
636afc51aaaSDarrick J. Wong 	if (unlikely(status))
637afc51aaaSDarrick J. Wong 		goto out_unlock;
638afc51aaaSDarrick J. Wong 
639afc51aaaSDarrick J. Wong 	*pagep = page;
640afc51aaaSDarrick J. Wong 	return 0;
641afc51aaaSDarrick J. Wong 
642afc51aaaSDarrick J. Wong out_unlock:
643afc51aaaSDarrick J. Wong 	unlock_page(page);
644afc51aaaSDarrick J. Wong 	put_page(page);
645afc51aaaSDarrick J. Wong 	iomap_write_failed(inode, pos, len);
646afc51aaaSDarrick J. Wong 
647afc51aaaSDarrick J. Wong out_no_page:
648afc51aaaSDarrick J. Wong 	if (page_ops && page_ops->page_done)
649afc51aaaSDarrick J. Wong 		page_ops->page_done(inode, pos, 0, NULL, iomap);
650afc51aaaSDarrick J. Wong 	return status;
651afc51aaaSDarrick J. Wong }
652afc51aaaSDarrick J. Wong 
653e25ba8cbSMatthew Wilcox (Oracle) static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
654e25ba8cbSMatthew Wilcox (Oracle) 		size_t copied, struct page *page)
655afc51aaaSDarrick J. Wong {
656afc51aaaSDarrick J. Wong 	flush_dcache_page(page);
657afc51aaaSDarrick J. Wong 
658afc51aaaSDarrick J. Wong 	/*
659afc51aaaSDarrick J. Wong 	 * The blocks that were entirely written will now be uptodate, so we
660afc51aaaSDarrick J. Wong 	 * don't have to worry about a readpage reading them and overwriting a
661f1f264b4SAndreas Gruenbacher 	 * partial write.  However, if we've encountered a short write and only
662afc51aaaSDarrick J. Wong 	 * partially written into a block, it will not be marked uptodate, so a
663afc51aaaSDarrick J. Wong 	 * readpage might come in and destroy our partial write.
664afc51aaaSDarrick J. Wong 	 *
665f1f264b4SAndreas Gruenbacher 	 * Do the simplest thing and just treat any short write to a
666f1f264b4SAndreas Gruenbacher 	 * non-uptodate page as a zero-length write, and force the caller to
667f1f264b4SAndreas Gruenbacher 	 * redo the whole thing.
668afc51aaaSDarrick J. Wong 	 */
669afc51aaaSDarrick J. Wong 	if (unlikely(copied < len && !PageUptodate(page)))
670afc51aaaSDarrick J. Wong 		return 0;
671afc51aaaSDarrick J. Wong 	iomap_set_range_uptodate(page, offset_in_page(pos), len);
672fd7353f8SMatthew Wilcox (Oracle) 	__set_page_dirty_nobuffers(page);
673afc51aaaSDarrick J. Wong 	return copied;
674afc51aaaSDarrick J. Wong }
675afc51aaaSDarrick J. Wong 
676e25ba8cbSMatthew Wilcox (Oracle) static size_t iomap_write_end_inline(struct inode *inode, struct page *page,
677e25ba8cbSMatthew Wilcox (Oracle) 		struct iomap *iomap, loff_t pos, size_t copied)
678afc51aaaSDarrick J. Wong {
679afc51aaaSDarrick J. Wong 	void *addr;
680afc51aaaSDarrick J. Wong 
681afc51aaaSDarrick J. Wong 	WARN_ON_ONCE(!PageUptodate(page));
68269f4a26cSGao Xiang 	BUG_ON(!iomap_inline_data_valid(iomap));
683afc51aaaSDarrick J. Wong 
6847ed3cd1aSMatthew Wilcox (Oracle) 	flush_dcache_page(page);
685*ab069d5fSMatthew Wilcox (Oracle) 	addr = kmap_local_page(page) + pos;
686*ab069d5fSMatthew Wilcox (Oracle) 	memcpy(iomap_inline_data(iomap, pos), addr, copied);
687*ab069d5fSMatthew Wilcox (Oracle) 	kunmap_local(addr);
688afc51aaaSDarrick J. Wong 
689afc51aaaSDarrick J. Wong 	mark_inode_dirty(inode);
690afc51aaaSDarrick J. Wong 	return copied;
691afc51aaaSDarrick J. Wong }
692afc51aaaSDarrick J. Wong 
693e25ba8cbSMatthew Wilcox (Oracle) /* Returns the number of bytes copied.  May be 0.  Cannot be an errno. */
694e25ba8cbSMatthew Wilcox (Oracle) static size_t iomap_write_end(struct inode *inode, loff_t pos, size_t len,
695e25ba8cbSMatthew Wilcox (Oracle) 		size_t copied, struct page *page, struct iomap *iomap,
696e25ba8cbSMatthew Wilcox (Oracle) 		struct iomap *srcmap)
697afc51aaaSDarrick J. Wong {
698afc51aaaSDarrick J. Wong 	const struct iomap_page_ops *page_ops = iomap->page_ops;
699afc51aaaSDarrick J. Wong 	loff_t old_size = inode->i_size;
700e25ba8cbSMatthew Wilcox (Oracle) 	size_t ret;
701afc51aaaSDarrick J. Wong 
702c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_INLINE) {
703afc51aaaSDarrick J. Wong 		ret = iomap_write_end_inline(inode, page, iomap, pos, copied);
704c039b997SGoldwyn Rodrigues 	} else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
705afc51aaaSDarrick J. Wong 		ret = block_write_end(NULL, inode->i_mapping, pos, len, copied,
706afc51aaaSDarrick J. Wong 				page, NULL);
707afc51aaaSDarrick J. Wong 	} else {
708c12d6fa8SChristoph Hellwig 		ret = __iomap_write_end(inode, pos, len, copied, page);
709afc51aaaSDarrick J. Wong 	}
710afc51aaaSDarrick J. Wong 
711afc51aaaSDarrick J. Wong 	/*
712afc51aaaSDarrick J. Wong 	 * Update the in-memory inode size after copying the data into the page
713afc51aaaSDarrick J. Wong 	 * cache.  It's up to the file system to write the updated size to disk,
714afc51aaaSDarrick J. Wong 	 * preferably after I/O completion so that no stale data is exposed.
715afc51aaaSDarrick J. Wong 	 */
716afc51aaaSDarrick J. Wong 	if (pos + ret > old_size) {
717afc51aaaSDarrick J. Wong 		i_size_write(inode, pos + ret);
718afc51aaaSDarrick J. Wong 		iomap->flags |= IOMAP_F_SIZE_CHANGED;
719afc51aaaSDarrick J. Wong 	}
720afc51aaaSDarrick J. Wong 	unlock_page(page);
721afc51aaaSDarrick J. Wong 
722afc51aaaSDarrick J. Wong 	if (old_size < pos)
723afc51aaaSDarrick J. Wong 		pagecache_isize_extended(inode, old_size, pos);
724afc51aaaSDarrick J. Wong 	if (page_ops && page_ops->page_done)
725afc51aaaSDarrick J. Wong 		page_ops->page_done(inode, pos, ret, page, iomap);
726afc51aaaSDarrick J. Wong 	put_page(page);
727afc51aaaSDarrick J. Wong 
728afc51aaaSDarrick J. Wong 	if (ret < len)
729afc51aaaSDarrick J. Wong 		iomap_write_failed(inode, pos, len);
730afc51aaaSDarrick J. Wong 	return ret;
731afc51aaaSDarrick J. Wong }
732afc51aaaSDarrick J. Wong 
733afc51aaaSDarrick J. Wong static loff_t
734afc51aaaSDarrick J. Wong iomap_write_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
735c039b997SGoldwyn Rodrigues 		struct iomap *iomap, struct iomap *srcmap)
736afc51aaaSDarrick J. Wong {
737afc51aaaSDarrick J. Wong 	struct iov_iter *i = data;
738afc51aaaSDarrick J. Wong 	long status = 0;
739afc51aaaSDarrick J. Wong 	ssize_t written = 0;
740afc51aaaSDarrick J. Wong 
741afc51aaaSDarrick J. Wong 	do {
742afc51aaaSDarrick J. Wong 		struct page *page;
743afc51aaaSDarrick J. Wong 		unsigned long offset;	/* Offset into pagecache page */
744afc51aaaSDarrick J. Wong 		unsigned long bytes;	/* Bytes to write to page */
745afc51aaaSDarrick J. Wong 		size_t copied;		/* Bytes copied from user */
746afc51aaaSDarrick J. Wong 
747afc51aaaSDarrick J. Wong 		offset = offset_in_page(pos);
748afc51aaaSDarrick J. Wong 		bytes = min_t(unsigned long, PAGE_SIZE - offset,
749afc51aaaSDarrick J. Wong 						iov_iter_count(i));
750afc51aaaSDarrick J. Wong again:
751afc51aaaSDarrick J. Wong 		if (bytes > length)
752afc51aaaSDarrick J. Wong 			bytes = length;
753afc51aaaSDarrick J. Wong 
754afc51aaaSDarrick J. Wong 		/*
755f1f264b4SAndreas Gruenbacher 		 * Bring in the user page that we'll copy from _first_.
756afc51aaaSDarrick J. Wong 		 * Otherwise there's a nasty deadlock on copying from the
757afc51aaaSDarrick J. Wong 		 * same page as we're writing to, without it being marked
758afc51aaaSDarrick J. Wong 		 * up-to-date.
759afc51aaaSDarrick J. Wong 		 */
760afc51aaaSDarrick J. Wong 		if (unlikely(iov_iter_fault_in_readable(i, bytes))) {
761afc51aaaSDarrick J. Wong 			status = -EFAULT;
762afc51aaaSDarrick J. Wong 			break;
763afc51aaaSDarrick J. Wong 		}
764afc51aaaSDarrick J. Wong 
765c039b997SGoldwyn Rodrigues 		status = iomap_write_begin(inode, pos, bytes, 0, &page, iomap,
766c039b997SGoldwyn Rodrigues 				srcmap);
767afc51aaaSDarrick J. Wong 		if (unlikely(status))
768afc51aaaSDarrick J. Wong 			break;
769afc51aaaSDarrick J. Wong 
770afc51aaaSDarrick J. Wong 		if (mapping_writably_mapped(inode->i_mapping))
771afc51aaaSDarrick J. Wong 			flush_dcache_page(page);
772afc51aaaSDarrick J. Wong 
773f0b65f39SAl Viro 		copied = copy_page_from_iter_atomic(page, offset, bytes, i);
774afc51aaaSDarrick J. Wong 
775bc1bb416SAl Viro 		status = iomap_write_end(inode, pos, bytes, copied, page, iomap,
776c039b997SGoldwyn Rodrigues 				srcmap);
777afc51aaaSDarrick J. Wong 
778f0b65f39SAl Viro 		if (unlikely(copied != status))
779f0b65f39SAl Viro 			iov_iter_revert(i, copied - status);
780afc51aaaSDarrick J. Wong 
781f0b65f39SAl Viro 		cond_resched();
782bc1bb416SAl Viro 		if (unlikely(status == 0)) {
783afc51aaaSDarrick J. Wong 			/*
784bc1bb416SAl Viro 			 * A short copy made iomap_write_end() reject the
785bc1bb416SAl Viro 			 * thing entirely.  Might be memory poisoning
786bc1bb416SAl Viro 			 * halfway through, might be a race with munmap,
787bc1bb416SAl Viro 			 * might be severe memory pressure.
788afc51aaaSDarrick J. Wong 			 */
789bc1bb416SAl Viro 			if (copied)
790bc1bb416SAl Viro 				bytes = copied;
791afc51aaaSDarrick J. Wong 			goto again;
792afc51aaaSDarrick J. Wong 		}
793f0b65f39SAl Viro 		pos += status;
794f0b65f39SAl Viro 		written += status;
795f0b65f39SAl Viro 		length -= status;
796afc51aaaSDarrick J. Wong 
797afc51aaaSDarrick J. Wong 		balance_dirty_pages_ratelimited(inode->i_mapping);
798afc51aaaSDarrick J. Wong 	} while (iov_iter_count(i) && length);
799afc51aaaSDarrick J. Wong 
800afc51aaaSDarrick J. Wong 	return written ? written : status;
801afc51aaaSDarrick J. Wong }
802afc51aaaSDarrick J. Wong 
803afc51aaaSDarrick J. Wong ssize_t
804afc51aaaSDarrick J. Wong iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *iter,
805afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
806afc51aaaSDarrick J. Wong {
807afc51aaaSDarrick J. Wong 	struct inode *inode = iocb->ki_filp->f_mapping->host;
808afc51aaaSDarrick J. Wong 	loff_t pos = iocb->ki_pos, ret = 0, written = 0;
809afc51aaaSDarrick J. Wong 
810afc51aaaSDarrick J. Wong 	while (iov_iter_count(iter)) {
811afc51aaaSDarrick J. Wong 		ret = iomap_apply(inode, pos, iov_iter_count(iter),
812afc51aaaSDarrick J. Wong 				IOMAP_WRITE, ops, iter, iomap_write_actor);
813afc51aaaSDarrick J. Wong 		if (ret <= 0)
814afc51aaaSDarrick J. Wong 			break;
815afc51aaaSDarrick J. Wong 		pos += ret;
816afc51aaaSDarrick J. Wong 		written += ret;
817afc51aaaSDarrick J. Wong 	}
818afc51aaaSDarrick J. Wong 
819afc51aaaSDarrick J. Wong 	return written ? written : ret;
820afc51aaaSDarrick J. Wong }
821afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
822afc51aaaSDarrick J. Wong 
823afc51aaaSDarrick J. Wong static loff_t
8243590c4d8SChristoph Hellwig iomap_unshare_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
825c039b997SGoldwyn Rodrigues 		struct iomap *iomap, struct iomap *srcmap)
826afc51aaaSDarrick J. Wong {
827afc51aaaSDarrick J. Wong 	long status = 0;
828d4ff3b2eSMatthew Wilcox (Oracle) 	loff_t written = 0;
829afc51aaaSDarrick J. Wong 
8303590c4d8SChristoph Hellwig 	/* don't bother with blocks that are not shared to start with */
8313590c4d8SChristoph Hellwig 	if (!(iomap->flags & IOMAP_F_SHARED))
8323590c4d8SChristoph Hellwig 		return length;
8333590c4d8SChristoph Hellwig 	/* don't bother with holes or unwritten extents */
834c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
8353590c4d8SChristoph Hellwig 		return length;
8363590c4d8SChristoph Hellwig 
837afc51aaaSDarrick J. Wong 	do {
83832a38a49SChristoph Hellwig 		unsigned long offset = offset_in_page(pos);
83932a38a49SChristoph Hellwig 		unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length);
84032a38a49SChristoph Hellwig 		struct page *page;
841afc51aaaSDarrick J. Wong 
84232a38a49SChristoph Hellwig 		status = iomap_write_begin(inode, pos, bytes,
843c039b997SGoldwyn Rodrigues 				IOMAP_WRITE_F_UNSHARE, &page, iomap, srcmap);
844afc51aaaSDarrick J. Wong 		if (unlikely(status))
845afc51aaaSDarrick J. Wong 			return status;
846afc51aaaSDarrick J. Wong 
847c039b997SGoldwyn Rodrigues 		status = iomap_write_end(inode, pos, bytes, bytes, page, iomap,
848c039b997SGoldwyn Rodrigues 				srcmap);
849afc51aaaSDarrick J. Wong 		if (WARN_ON_ONCE(status == 0))
850afc51aaaSDarrick J. Wong 			return -EIO;
851afc51aaaSDarrick J. Wong 
852afc51aaaSDarrick J. Wong 		cond_resched();
853afc51aaaSDarrick J. Wong 
854afc51aaaSDarrick J. Wong 		pos += status;
855afc51aaaSDarrick J. Wong 		written += status;
856afc51aaaSDarrick J. Wong 		length -= status;
857afc51aaaSDarrick J. Wong 
858afc51aaaSDarrick J. Wong 		balance_dirty_pages_ratelimited(inode->i_mapping);
859afc51aaaSDarrick J. Wong 	} while (length);
860afc51aaaSDarrick J. Wong 
861afc51aaaSDarrick J. Wong 	return written;
862afc51aaaSDarrick J. Wong }
863afc51aaaSDarrick J. Wong 
864afc51aaaSDarrick J. Wong int
8653590c4d8SChristoph Hellwig iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
866afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
867afc51aaaSDarrick J. Wong {
868afc51aaaSDarrick J. Wong 	loff_t ret;
869afc51aaaSDarrick J. Wong 
870afc51aaaSDarrick J. Wong 	while (len) {
871afc51aaaSDarrick J. Wong 		ret = iomap_apply(inode, pos, len, IOMAP_WRITE, ops, NULL,
8723590c4d8SChristoph Hellwig 				iomap_unshare_actor);
873afc51aaaSDarrick J. Wong 		if (ret <= 0)
874afc51aaaSDarrick J. Wong 			return ret;
875afc51aaaSDarrick J. Wong 		pos += ret;
876afc51aaaSDarrick J. Wong 		len -= ret;
877afc51aaaSDarrick J. Wong 	}
878afc51aaaSDarrick J. Wong 
879afc51aaaSDarrick J. Wong 	return 0;
880afc51aaaSDarrick J. Wong }
8813590c4d8SChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_file_unshare);
882afc51aaaSDarrick J. Wong 
88381ee8e52SMatthew Wilcox (Oracle) static s64 iomap_zero(struct inode *inode, loff_t pos, u64 length,
88481ee8e52SMatthew Wilcox (Oracle) 		struct iomap *iomap, struct iomap *srcmap)
885afc51aaaSDarrick J. Wong {
886afc51aaaSDarrick J. Wong 	struct page *page;
887afc51aaaSDarrick J. Wong 	int status;
88881ee8e52SMatthew Wilcox (Oracle) 	unsigned offset = offset_in_page(pos);
88981ee8e52SMatthew Wilcox (Oracle) 	unsigned bytes = min_t(u64, PAGE_SIZE - offset, length);
890afc51aaaSDarrick J. Wong 
891c039b997SGoldwyn Rodrigues 	status = iomap_write_begin(inode, pos, bytes, 0, &page, iomap, srcmap);
892afc51aaaSDarrick J. Wong 	if (status)
893afc51aaaSDarrick J. Wong 		return status;
894afc51aaaSDarrick J. Wong 
895afc51aaaSDarrick J. Wong 	zero_user(page, offset, bytes);
896afc51aaaSDarrick J. Wong 	mark_page_accessed(page);
897afc51aaaSDarrick J. Wong 
898c039b997SGoldwyn Rodrigues 	return iomap_write_end(inode, pos, bytes, bytes, page, iomap, srcmap);
899afc51aaaSDarrick J. Wong }
900afc51aaaSDarrick J. Wong 
90181ee8e52SMatthew Wilcox (Oracle) static loff_t iomap_zero_range_actor(struct inode *inode, loff_t pos,
90281ee8e52SMatthew Wilcox (Oracle) 		loff_t length, void *data, struct iomap *iomap,
90381ee8e52SMatthew Wilcox (Oracle) 		struct iomap *srcmap)
904afc51aaaSDarrick J. Wong {
905afc51aaaSDarrick J. Wong 	bool *did_zero = data;
906afc51aaaSDarrick J. Wong 	loff_t written = 0;
907afc51aaaSDarrick J. Wong 
908afc51aaaSDarrick J. Wong 	/* already zeroed?  we're done. */
909c039b997SGoldwyn Rodrigues 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
91081ee8e52SMatthew Wilcox (Oracle) 		return length;
911afc51aaaSDarrick J. Wong 
912afc51aaaSDarrick J. Wong 	do {
91381ee8e52SMatthew Wilcox (Oracle) 		s64 bytes;
914afc51aaaSDarrick J. Wong 
915afc51aaaSDarrick J. Wong 		if (IS_DAX(inode))
91681ee8e52SMatthew Wilcox (Oracle) 			bytes = dax_iomap_zero(pos, length, iomap);
917afc51aaaSDarrick J. Wong 		else
91881ee8e52SMatthew Wilcox (Oracle) 			bytes = iomap_zero(inode, pos, length, iomap, srcmap);
91981ee8e52SMatthew Wilcox (Oracle) 		if (bytes < 0)
92081ee8e52SMatthew Wilcox (Oracle) 			return bytes;
921afc51aaaSDarrick J. Wong 
922afc51aaaSDarrick J. Wong 		pos += bytes;
92381ee8e52SMatthew Wilcox (Oracle) 		length -= bytes;
924afc51aaaSDarrick J. Wong 		written += bytes;
925afc51aaaSDarrick J. Wong 		if (did_zero)
926afc51aaaSDarrick J. Wong 			*did_zero = true;
92781ee8e52SMatthew Wilcox (Oracle) 	} while (length > 0);
928afc51aaaSDarrick J. Wong 
929afc51aaaSDarrick J. Wong 	return written;
930afc51aaaSDarrick J. Wong }
931afc51aaaSDarrick J. Wong 
932afc51aaaSDarrick J. Wong int
933afc51aaaSDarrick J. Wong iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
934afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
935afc51aaaSDarrick J. Wong {
936afc51aaaSDarrick J. Wong 	loff_t ret;
937afc51aaaSDarrick J. Wong 
938afc51aaaSDarrick J. Wong 	while (len > 0) {
939afc51aaaSDarrick J. Wong 		ret = iomap_apply(inode, pos, len, IOMAP_ZERO,
940afc51aaaSDarrick J. Wong 				ops, did_zero, iomap_zero_range_actor);
941afc51aaaSDarrick J. Wong 		if (ret <= 0)
942afc51aaaSDarrick J. Wong 			return ret;
943afc51aaaSDarrick J. Wong 
944afc51aaaSDarrick J. Wong 		pos += ret;
945afc51aaaSDarrick J. Wong 		len -= ret;
946afc51aaaSDarrick J. Wong 	}
947afc51aaaSDarrick J. Wong 
948afc51aaaSDarrick J. Wong 	return 0;
949afc51aaaSDarrick J. Wong }
950afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_zero_range);
951afc51aaaSDarrick J. Wong 
952afc51aaaSDarrick J. Wong int
953afc51aaaSDarrick J. Wong iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
954afc51aaaSDarrick J. Wong 		const struct iomap_ops *ops)
955afc51aaaSDarrick J. Wong {
956afc51aaaSDarrick J. Wong 	unsigned int blocksize = i_blocksize(inode);
957afc51aaaSDarrick J. Wong 	unsigned int off = pos & (blocksize - 1);
958afc51aaaSDarrick J. Wong 
959afc51aaaSDarrick J. Wong 	/* Block boundary? Nothing to do */
960afc51aaaSDarrick J. Wong 	if (!off)
961afc51aaaSDarrick J. Wong 		return 0;
962afc51aaaSDarrick J. Wong 	return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
963afc51aaaSDarrick J. Wong }
964afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_truncate_page);
965afc51aaaSDarrick J. Wong 
966afc51aaaSDarrick J. Wong static loff_t
967afc51aaaSDarrick J. Wong iomap_page_mkwrite_actor(struct inode *inode, loff_t pos, loff_t length,
968c039b997SGoldwyn Rodrigues 		void *data, struct iomap *iomap, struct iomap *srcmap)
969afc51aaaSDarrick J. Wong {
970afc51aaaSDarrick J. Wong 	struct page *page = data;
971afc51aaaSDarrick J. Wong 	int ret;
972afc51aaaSDarrick J. Wong 
973afc51aaaSDarrick J. Wong 	if (iomap->flags & IOMAP_F_BUFFER_HEAD) {
974afc51aaaSDarrick J. Wong 		ret = __block_write_begin_int(page, pos, length, NULL, iomap);
975afc51aaaSDarrick J. Wong 		if (ret)
976afc51aaaSDarrick J. Wong 			return ret;
977afc51aaaSDarrick J. Wong 		block_commit_write(page, 0, length);
978afc51aaaSDarrick J. Wong 	} else {
979afc51aaaSDarrick J. Wong 		WARN_ON_ONCE(!PageUptodate(page));
980afc51aaaSDarrick J. Wong 		set_page_dirty(page);
981afc51aaaSDarrick J. Wong 	}
982afc51aaaSDarrick J. Wong 
983afc51aaaSDarrick J. Wong 	return length;
984afc51aaaSDarrick J. Wong }
985afc51aaaSDarrick J. Wong 
986afc51aaaSDarrick J. Wong vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
987afc51aaaSDarrick J. Wong {
988afc51aaaSDarrick J. Wong 	struct page *page = vmf->page;
989afc51aaaSDarrick J. Wong 	struct inode *inode = file_inode(vmf->vma->vm_file);
990afc51aaaSDarrick J. Wong 	unsigned long length;
991243145bcSAndreas Gruenbacher 	loff_t offset;
992afc51aaaSDarrick J. Wong 	ssize_t ret;
993afc51aaaSDarrick J. Wong 
994afc51aaaSDarrick J. Wong 	lock_page(page);
995243145bcSAndreas Gruenbacher 	ret = page_mkwrite_check_truncate(page, inode);
996243145bcSAndreas Gruenbacher 	if (ret < 0)
997afc51aaaSDarrick J. Wong 		goto out_unlock;
998243145bcSAndreas Gruenbacher 	length = ret;
999afc51aaaSDarrick J. Wong 
1000243145bcSAndreas Gruenbacher 	offset = page_offset(page);
1001afc51aaaSDarrick J. Wong 	while (length > 0) {
1002afc51aaaSDarrick J. Wong 		ret = iomap_apply(inode, offset, length,
1003afc51aaaSDarrick J. Wong 				IOMAP_WRITE | IOMAP_FAULT, ops, page,
1004afc51aaaSDarrick J. Wong 				iomap_page_mkwrite_actor);
1005afc51aaaSDarrick J. Wong 		if (unlikely(ret <= 0))
1006afc51aaaSDarrick J. Wong 			goto out_unlock;
1007afc51aaaSDarrick J. Wong 		offset += ret;
1008afc51aaaSDarrick J. Wong 		length -= ret;
1009afc51aaaSDarrick J. Wong 	}
1010afc51aaaSDarrick J. Wong 
1011afc51aaaSDarrick J. Wong 	wait_for_stable_page(page);
1012afc51aaaSDarrick J. Wong 	return VM_FAULT_LOCKED;
1013afc51aaaSDarrick J. Wong out_unlock:
1014afc51aaaSDarrick J. Wong 	unlock_page(page);
1015afc51aaaSDarrick J. Wong 	return block_page_mkwrite_return(ret);
1016afc51aaaSDarrick J. Wong }
1017afc51aaaSDarrick J. Wong EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1018598ecfbaSChristoph Hellwig 
1019598ecfbaSChristoph Hellwig static void
102048d64cd1SChristoph Hellwig iomap_finish_page_writeback(struct inode *inode, struct page *page,
10210fb2d720SMatthew Wilcox (Oracle) 		int error, unsigned int len)
1022598ecfbaSChristoph Hellwig {
102348d64cd1SChristoph Hellwig 	struct iomap_page *iop = to_iomap_page(page);
1024598ecfbaSChristoph Hellwig 
1025598ecfbaSChristoph Hellwig 	if (error) {
102648d64cd1SChristoph Hellwig 		SetPageError(page);
1027598ecfbaSChristoph Hellwig 		mapping_set_error(inode->i_mapping, -EIO);
1028598ecfbaSChristoph Hellwig 	}
1029598ecfbaSChristoph Hellwig 
103024addd84SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(i_blocks_per_page(inode, page) > 1 && !iop);
10310fb2d720SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0);
1032598ecfbaSChristoph Hellwig 
10330fb2d720SMatthew Wilcox (Oracle) 	if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending))
103448d64cd1SChristoph Hellwig 		end_page_writeback(page);
1035598ecfbaSChristoph Hellwig }
1036598ecfbaSChristoph Hellwig 
1037598ecfbaSChristoph Hellwig /*
1038598ecfbaSChristoph Hellwig  * We're now finished for good with this ioend structure.  Update the page
1039598ecfbaSChristoph Hellwig  * state, release holds on bios, and finally free up memory.  Do not use the
1040598ecfbaSChristoph Hellwig  * ioend after this.
1041598ecfbaSChristoph Hellwig  */
1042598ecfbaSChristoph Hellwig static void
1043598ecfbaSChristoph Hellwig iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1044598ecfbaSChristoph Hellwig {
1045598ecfbaSChristoph Hellwig 	struct inode *inode = ioend->io_inode;
1046598ecfbaSChristoph Hellwig 	struct bio *bio = &ioend->io_inline_bio;
1047598ecfbaSChristoph Hellwig 	struct bio *last = ioend->io_bio, *next;
1048598ecfbaSChristoph Hellwig 	u64 start = bio->bi_iter.bi_sector;
1049c275779fSZorro Lang 	loff_t offset = ioend->io_offset;
1050598ecfbaSChristoph Hellwig 	bool quiet = bio_flagged(bio, BIO_QUIET);
1051598ecfbaSChristoph Hellwig 
1052598ecfbaSChristoph Hellwig 	for (bio = &ioend->io_inline_bio; bio; bio = next) {
1053598ecfbaSChristoph Hellwig 		struct bio_vec *bv;
1054598ecfbaSChristoph Hellwig 		struct bvec_iter_all iter_all;
1055598ecfbaSChristoph Hellwig 
1056598ecfbaSChristoph Hellwig 		/*
1057598ecfbaSChristoph Hellwig 		 * For the last bio, bi_private points to the ioend, so we
1058598ecfbaSChristoph Hellwig 		 * need to explicitly end the iteration here.
1059598ecfbaSChristoph Hellwig 		 */
1060598ecfbaSChristoph Hellwig 		if (bio == last)
1061598ecfbaSChristoph Hellwig 			next = NULL;
1062598ecfbaSChristoph Hellwig 		else
1063598ecfbaSChristoph Hellwig 			next = bio->bi_private;
1064598ecfbaSChristoph Hellwig 
1065598ecfbaSChristoph Hellwig 		/* walk each page on bio, ending page IO on them */
1066598ecfbaSChristoph Hellwig 		bio_for_each_segment_all(bv, bio, iter_all)
10670fb2d720SMatthew Wilcox (Oracle) 			iomap_finish_page_writeback(inode, bv->bv_page, error,
10680fb2d720SMatthew Wilcox (Oracle) 					bv->bv_len);
1069598ecfbaSChristoph Hellwig 		bio_put(bio);
1070598ecfbaSChristoph Hellwig 	}
1071c275779fSZorro Lang 	/* The ioend has been freed by bio_put() */
1072598ecfbaSChristoph Hellwig 
1073598ecfbaSChristoph Hellwig 	if (unlikely(error && !quiet)) {
1074598ecfbaSChristoph Hellwig 		printk_ratelimited(KERN_ERR
10759cd0ed63SDarrick J. Wong "%s: writeback error on inode %lu, offset %lld, sector %llu",
1076c275779fSZorro Lang 			inode->i_sb->s_id, inode->i_ino, offset, start);
1077598ecfbaSChristoph Hellwig 	}
1078598ecfbaSChristoph Hellwig }
1079598ecfbaSChristoph Hellwig 
1080598ecfbaSChristoph Hellwig void
1081598ecfbaSChristoph Hellwig iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1082598ecfbaSChristoph Hellwig {
1083598ecfbaSChristoph Hellwig 	struct list_head tmp;
1084598ecfbaSChristoph Hellwig 
1085598ecfbaSChristoph Hellwig 	list_replace_init(&ioend->io_list, &tmp);
1086598ecfbaSChristoph Hellwig 	iomap_finish_ioend(ioend, error);
1087598ecfbaSChristoph Hellwig 
1088598ecfbaSChristoph Hellwig 	while (!list_empty(&tmp)) {
1089598ecfbaSChristoph Hellwig 		ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1090598ecfbaSChristoph Hellwig 		list_del_init(&ioend->io_list);
1091598ecfbaSChristoph Hellwig 		iomap_finish_ioend(ioend, error);
1092598ecfbaSChristoph Hellwig 	}
1093598ecfbaSChristoph Hellwig }
1094598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1095598ecfbaSChristoph Hellwig 
1096598ecfbaSChristoph Hellwig /*
1097598ecfbaSChristoph Hellwig  * We can merge two adjacent ioends if they have the same set of work to do.
1098598ecfbaSChristoph Hellwig  */
1099598ecfbaSChristoph Hellwig static bool
1100598ecfbaSChristoph Hellwig iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1101598ecfbaSChristoph Hellwig {
1102598ecfbaSChristoph Hellwig 	if (ioend->io_bio->bi_status != next->io_bio->bi_status)
1103598ecfbaSChristoph Hellwig 		return false;
1104598ecfbaSChristoph Hellwig 	if ((ioend->io_flags & IOMAP_F_SHARED) ^
1105598ecfbaSChristoph Hellwig 	    (next->io_flags & IOMAP_F_SHARED))
1106598ecfbaSChristoph Hellwig 		return false;
1107598ecfbaSChristoph Hellwig 	if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1108598ecfbaSChristoph Hellwig 	    (next->io_type == IOMAP_UNWRITTEN))
1109598ecfbaSChristoph Hellwig 		return false;
1110598ecfbaSChristoph Hellwig 	if (ioend->io_offset + ioend->io_size != next->io_offset)
1111598ecfbaSChristoph Hellwig 		return false;
1112598ecfbaSChristoph Hellwig 	return true;
1113598ecfbaSChristoph Hellwig }
1114598ecfbaSChristoph Hellwig 
1115598ecfbaSChristoph Hellwig void
11166e552494SBrian Foster iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
1117598ecfbaSChristoph Hellwig {
1118598ecfbaSChristoph Hellwig 	struct iomap_ioend *next;
1119598ecfbaSChristoph Hellwig 
1120598ecfbaSChristoph Hellwig 	INIT_LIST_HEAD(&ioend->io_list);
1121598ecfbaSChristoph Hellwig 
1122598ecfbaSChristoph Hellwig 	while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1123598ecfbaSChristoph Hellwig 			io_list))) {
1124598ecfbaSChristoph Hellwig 		if (!iomap_ioend_can_merge(ioend, next))
1125598ecfbaSChristoph Hellwig 			break;
1126598ecfbaSChristoph Hellwig 		list_move_tail(&next->io_list, &ioend->io_list);
1127598ecfbaSChristoph Hellwig 		ioend->io_size += next->io_size;
1128598ecfbaSChristoph Hellwig 	}
1129598ecfbaSChristoph Hellwig }
1130598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1131598ecfbaSChristoph Hellwig 
1132598ecfbaSChristoph Hellwig static int
11334f0f586bSSami Tolvanen iomap_ioend_compare(void *priv, const struct list_head *a,
11344f0f586bSSami Tolvanen 		const struct list_head *b)
1135598ecfbaSChristoph Hellwig {
1136b3d423ecSChristoph Hellwig 	struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1137b3d423ecSChristoph Hellwig 	struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
1138598ecfbaSChristoph Hellwig 
1139598ecfbaSChristoph Hellwig 	if (ia->io_offset < ib->io_offset)
1140598ecfbaSChristoph Hellwig 		return -1;
1141b3d423ecSChristoph Hellwig 	if (ia->io_offset > ib->io_offset)
1142598ecfbaSChristoph Hellwig 		return 1;
1143598ecfbaSChristoph Hellwig 	return 0;
1144598ecfbaSChristoph Hellwig }
1145598ecfbaSChristoph Hellwig 
1146598ecfbaSChristoph Hellwig void
1147598ecfbaSChristoph Hellwig iomap_sort_ioends(struct list_head *ioend_list)
1148598ecfbaSChristoph Hellwig {
1149598ecfbaSChristoph Hellwig 	list_sort(NULL, ioend_list, iomap_ioend_compare);
1150598ecfbaSChristoph Hellwig }
1151598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1152598ecfbaSChristoph Hellwig 
1153598ecfbaSChristoph Hellwig static void iomap_writepage_end_bio(struct bio *bio)
1154598ecfbaSChristoph Hellwig {
1155598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend = bio->bi_private;
1156598ecfbaSChristoph Hellwig 
1157598ecfbaSChristoph Hellwig 	iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
1158598ecfbaSChristoph Hellwig }
1159598ecfbaSChristoph Hellwig 
1160598ecfbaSChristoph Hellwig /*
1161598ecfbaSChristoph Hellwig  * Submit the final bio for an ioend.
1162598ecfbaSChristoph Hellwig  *
1163598ecfbaSChristoph Hellwig  * If @error is non-zero, it means that we have a situation where some part of
1164f1f264b4SAndreas Gruenbacher  * the submission process has failed after we've marked pages for writeback
1165598ecfbaSChristoph Hellwig  * and unlocked them.  In this situation, we need to fail the bio instead of
1166598ecfbaSChristoph Hellwig  * submitting it.  This typically only happens on a filesystem shutdown.
1167598ecfbaSChristoph Hellwig  */
1168598ecfbaSChristoph Hellwig static int
1169598ecfbaSChristoph Hellwig iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
1170598ecfbaSChristoph Hellwig 		int error)
1171598ecfbaSChristoph Hellwig {
1172598ecfbaSChristoph Hellwig 	ioend->io_bio->bi_private = ioend;
1173598ecfbaSChristoph Hellwig 	ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
1174598ecfbaSChristoph Hellwig 
1175598ecfbaSChristoph Hellwig 	if (wpc->ops->prepare_ioend)
1176598ecfbaSChristoph Hellwig 		error = wpc->ops->prepare_ioend(ioend, error);
1177598ecfbaSChristoph Hellwig 	if (error) {
1178598ecfbaSChristoph Hellwig 		/*
1179f1f264b4SAndreas Gruenbacher 		 * If we're failing the IO now, just mark the ioend with an
1180598ecfbaSChristoph Hellwig 		 * error and finish it.  This will run IO completion immediately
1181598ecfbaSChristoph Hellwig 		 * as there is only one reference to the ioend at this point in
1182598ecfbaSChristoph Hellwig 		 * time.
1183598ecfbaSChristoph Hellwig 		 */
1184598ecfbaSChristoph Hellwig 		ioend->io_bio->bi_status = errno_to_blk_status(error);
1185598ecfbaSChristoph Hellwig 		bio_endio(ioend->io_bio);
1186598ecfbaSChristoph Hellwig 		return error;
1187598ecfbaSChristoph Hellwig 	}
1188598ecfbaSChristoph Hellwig 
1189598ecfbaSChristoph Hellwig 	submit_bio(ioend->io_bio);
1190598ecfbaSChristoph Hellwig 	return 0;
1191598ecfbaSChristoph Hellwig }
1192598ecfbaSChristoph Hellwig 
1193598ecfbaSChristoph Hellwig static struct iomap_ioend *
1194598ecfbaSChristoph Hellwig iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
1195598ecfbaSChristoph Hellwig 		loff_t offset, sector_t sector, struct writeback_control *wbc)
1196598ecfbaSChristoph Hellwig {
1197598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend;
1198598ecfbaSChristoph Hellwig 	struct bio *bio;
1199598ecfbaSChristoph Hellwig 
1200a8affc03SChristoph Hellwig 	bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_VECS, &iomap_ioend_bioset);
1201598ecfbaSChristoph Hellwig 	bio_set_dev(bio, wpc->iomap.bdev);
1202598ecfbaSChristoph Hellwig 	bio->bi_iter.bi_sector = sector;
1203598ecfbaSChristoph Hellwig 	bio->bi_opf = REQ_OP_WRITE | wbc_to_write_flags(wbc);
1204598ecfbaSChristoph Hellwig 	bio->bi_write_hint = inode->i_write_hint;
1205598ecfbaSChristoph Hellwig 	wbc_init_bio(wbc, bio);
1206598ecfbaSChristoph Hellwig 
1207598ecfbaSChristoph Hellwig 	ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
1208598ecfbaSChristoph Hellwig 	INIT_LIST_HEAD(&ioend->io_list);
1209598ecfbaSChristoph Hellwig 	ioend->io_type = wpc->iomap.type;
1210598ecfbaSChristoph Hellwig 	ioend->io_flags = wpc->iomap.flags;
1211598ecfbaSChristoph Hellwig 	ioend->io_inode = inode;
1212598ecfbaSChristoph Hellwig 	ioend->io_size = 0;
1213598ecfbaSChristoph Hellwig 	ioend->io_offset = offset;
1214598ecfbaSChristoph Hellwig 	ioend->io_bio = bio;
1215598ecfbaSChristoph Hellwig 	return ioend;
1216598ecfbaSChristoph Hellwig }
1217598ecfbaSChristoph Hellwig 
1218598ecfbaSChristoph Hellwig /*
1219598ecfbaSChristoph Hellwig  * Allocate a new bio, and chain the old bio to the new one.
1220598ecfbaSChristoph Hellwig  *
1221f1f264b4SAndreas Gruenbacher  * Note that we have to perform the chaining in this unintuitive order
1222598ecfbaSChristoph Hellwig  * so that the bi_private linkage is set up in the right direction for the
1223598ecfbaSChristoph Hellwig  * traversal in iomap_finish_ioend().
1224598ecfbaSChristoph Hellwig  */
1225598ecfbaSChristoph Hellwig static struct bio *
1226598ecfbaSChristoph Hellwig iomap_chain_bio(struct bio *prev)
1227598ecfbaSChristoph Hellwig {
1228598ecfbaSChristoph Hellwig 	struct bio *new;
1229598ecfbaSChristoph Hellwig 
1230a8affc03SChristoph Hellwig 	new = bio_alloc(GFP_NOFS, BIO_MAX_VECS);
1231598ecfbaSChristoph Hellwig 	bio_copy_dev(new, prev);/* also copies over blkcg information */
1232598ecfbaSChristoph Hellwig 	new->bi_iter.bi_sector = bio_end_sector(prev);
1233598ecfbaSChristoph Hellwig 	new->bi_opf = prev->bi_opf;
1234598ecfbaSChristoph Hellwig 	new->bi_write_hint = prev->bi_write_hint;
1235598ecfbaSChristoph Hellwig 
1236598ecfbaSChristoph Hellwig 	bio_chain(prev, new);
1237598ecfbaSChristoph Hellwig 	bio_get(prev);		/* for iomap_finish_ioend */
1238598ecfbaSChristoph Hellwig 	submit_bio(prev);
1239598ecfbaSChristoph Hellwig 	return new;
1240598ecfbaSChristoph Hellwig }
1241598ecfbaSChristoph Hellwig 
1242598ecfbaSChristoph Hellwig static bool
1243598ecfbaSChristoph Hellwig iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
1244598ecfbaSChristoph Hellwig 		sector_t sector)
1245598ecfbaSChristoph Hellwig {
1246598ecfbaSChristoph Hellwig 	if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1247598ecfbaSChristoph Hellwig 	    (wpc->ioend->io_flags & IOMAP_F_SHARED))
1248598ecfbaSChristoph Hellwig 		return false;
1249598ecfbaSChristoph Hellwig 	if (wpc->iomap.type != wpc->ioend->io_type)
1250598ecfbaSChristoph Hellwig 		return false;
1251598ecfbaSChristoph Hellwig 	if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
1252598ecfbaSChristoph Hellwig 		return false;
1253598ecfbaSChristoph Hellwig 	if (sector != bio_end_sector(wpc->ioend->io_bio))
1254598ecfbaSChristoph Hellwig 		return false;
1255598ecfbaSChristoph Hellwig 	return true;
1256598ecfbaSChristoph Hellwig }
1257598ecfbaSChristoph Hellwig 
1258598ecfbaSChristoph Hellwig /*
1259598ecfbaSChristoph Hellwig  * Test to see if we have an existing ioend structure that we could append to
1260f1f264b4SAndreas Gruenbacher  * first; otherwise finish off the current ioend and start another.
1261598ecfbaSChristoph Hellwig  */
1262598ecfbaSChristoph Hellwig static void
1263598ecfbaSChristoph Hellwig iomap_add_to_ioend(struct inode *inode, loff_t offset, struct page *page,
1264598ecfbaSChristoph Hellwig 		struct iomap_page *iop, struct iomap_writepage_ctx *wpc,
1265598ecfbaSChristoph Hellwig 		struct writeback_control *wbc, struct list_head *iolist)
1266598ecfbaSChristoph Hellwig {
1267598ecfbaSChristoph Hellwig 	sector_t sector = iomap_sector(&wpc->iomap, offset);
1268598ecfbaSChristoph Hellwig 	unsigned len = i_blocksize(inode);
1269598ecfbaSChristoph Hellwig 	unsigned poff = offset & (PAGE_SIZE - 1);
1270598ecfbaSChristoph Hellwig 
1271598ecfbaSChristoph Hellwig 	if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, offset, sector)) {
1272598ecfbaSChristoph Hellwig 		if (wpc->ioend)
1273598ecfbaSChristoph Hellwig 			list_add(&wpc->ioend->io_list, iolist);
1274598ecfbaSChristoph Hellwig 		wpc->ioend = iomap_alloc_ioend(inode, wpc, offset, sector, wbc);
1275598ecfbaSChristoph Hellwig 	}
1276598ecfbaSChristoph Hellwig 
1277c1b79f11SChristoph Hellwig 	if (bio_add_page(wpc->ioend->io_bio, page, len, poff) != len) {
1278c1b79f11SChristoph Hellwig 		wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio);
1279c1b79f11SChristoph Hellwig 		__bio_add_page(wpc->ioend->io_bio, page, len, poff);
1280c1b79f11SChristoph Hellwig 	}
1281c1b79f11SChristoph Hellwig 
12820fb2d720SMatthew Wilcox (Oracle) 	if (iop)
12830fb2d720SMatthew Wilcox (Oracle) 		atomic_add(len, &iop->write_bytes_pending);
1284598ecfbaSChristoph Hellwig 	wpc->ioend->io_size += len;
1285598ecfbaSChristoph Hellwig 	wbc_account_cgroup_owner(wbc, page, len);
1286598ecfbaSChristoph Hellwig }
1287598ecfbaSChristoph Hellwig 
1288598ecfbaSChristoph Hellwig /*
1289598ecfbaSChristoph Hellwig  * We implement an immediate ioend submission policy here to avoid needing to
1290598ecfbaSChristoph Hellwig  * chain multiple ioends and hence nest mempool allocations which can violate
1291f1f264b4SAndreas Gruenbacher  * the forward progress guarantees we need to provide. The current ioend we're
1292f1f264b4SAndreas Gruenbacher  * adding blocks to is cached in the writepage context, and if the new block
1293f1f264b4SAndreas Gruenbacher  * doesn't append to the cached ioend, it will create a new ioend and cache that
1294598ecfbaSChristoph Hellwig  * instead.
1295598ecfbaSChristoph Hellwig  *
1296598ecfbaSChristoph Hellwig  * If a new ioend is created and cached, the old ioend is returned and queued
1297598ecfbaSChristoph Hellwig  * locally for submission once the entire page is processed or an error has been
1298598ecfbaSChristoph Hellwig  * detected.  While ioends are submitted immediately after they are completed,
1299598ecfbaSChristoph Hellwig  * batching optimisations are provided by higher level block plugging.
1300598ecfbaSChristoph Hellwig  *
1301598ecfbaSChristoph Hellwig  * At the end of a writeback pass, there will be a cached ioend remaining on the
1302598ecfbaSChristoph Hellwig  * writepage context that the caller will need to submit.
1303598ecfbaSChristoph Hellwig  */
1304598ecfbaSChristoph Hellwig static int
1305598ecfbaSChristoph Hellwig iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1306598ecfbaSChristoph Hellwig 		struct writeback_control *wbc, struct inode *inode,
1307598ecfbaSChristoph Hellwig 		struct page *page, u64 end_offset)
1308598ecfbaSChristoph Hellwig {
13098e1bcef8SAndreas Gruenbacher 	struct iomap_page *iop = iomap_page_create(inode, page);
1310598ecfbaSChristoph Hellwig 	struct iomap_ioend *ioend, *next;
1311598ecfbaSChristoph Hellwig 	unsigned len = i_blocksize(inode);
1312598ecfbaSChristoph Hellwig 	u64 file_offset; /* file offset of page */
1313598ecfbaSChristoph Hellwig 	int error = 0, count = 0, i;
1314598ecfbaSChristoph Hellwig 	LIST_HEAD(submit_list);
1315598ecfbaSChristoph Hellwig 
13160fb2d720SMatthew Wilcox (Oracle) 	WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0);
1317598ecfbaSChristoph Hellwig 
1318598ecfbaSChristoph Hellwig 	/*
1319598ecfbaSChristoph Hellwig 	 * Walk through the page to find areas to write back. If we run off the
1320598ecfbaSChristoph Hellwig 	 * end of the current map or find the current map invalid, grab a new
1321598ecfbaSChristoph Hellwig 	 * one.
1322598ecfbaSChristoph Hellwig 	 */
1323598ecfbaSChristoph Hellwig 	for (i = 0, file_offset = page_offset(page);
1324598ecfbaSChristoph Hellwig 	     i < (PAGE_SIZE >> inode->i_blkbits) && file_offset < end_offset;
1325598ecfbaSChristoph Hellwig 	     i++, file_offset += len) {
1326598ecfbaSChristoph Hellwig 		if (iop && !test_bit(i, iop->uptodate))
1327598ecfbaSChristoph Hellwig 			continue;
1328598ecfbaSChristoph Hellwig 
1329598ecfbaSChristoph Hellwig 		error = wpc->ops->map_blocks(wpc, inode, file_offset);
1330598ecfbaSChristoph Hellwig 		if (error)
1331598ecfbaSChristoph Hellwig 			break;
13323e19e6f3SChristoph Hellwig 		if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
13333e19e6f3SChristoph Hellwig 			continue;
1334598ecfbaSChristoph Hellwig 		if (wpc->iomap.type == IOMAP_HOLE)
1335598ecfbaSChristoph Hellwig 			continue;
1336598ecfbaSChristoph Hellwig 		iomap_add_to_ioend(inode, file_offset, page, iop, wpc, wbc,
1337598ecfbaSChristoph Hellwig 				 &submit_list);
1338598ecfbaSChristoph Hellwig 		count++;
1339598ecfbaSChristoph Hellwig 	}
1340598ecfbaSChristoph Hellwig 
1341598ecfbaSChristoph Hellwig 	WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
1342598ecfbaSChristoph Hellwig 	WARN_ON_ONCE(!PageLocked(page));
1343598ecfbaSChristoph Hellwig 	WARN_ON_ONCE(PageWriteback(page));
134450e7d6c7SBrian Foster 	WARN_ON_ONCE(PageDirty(page));
1345598ecfbaSChristoph Hellwig 
1346598ecfbaSChristoph Hellwig 	/*
1347598ecfbaSChristoph Hellwig 	 * We cannot cancel the ioend directly here on error.  We may have
1348598ecfbaSChristoph Hellwig 	 * already set other pages under writeback and hence we have to run I/O
1349598ecfbaSChristoph Hellwig 	 * completion to mark the error state of the pages under writeback
1350598ecfbaSChristoph Hellwig 	 * appropriately.
1351598ecfbaSChristoph Hellwig 	 */
1352598ecfbaSChristoph Hellwig 	if (unlikely(error)) {
1353598ecfbaSChristoph Hellwig 		/*
1354763e4cdcSBrian Foster 		 * Let the filesystem know what portion of the current page
1355f1f264b4SAndreas Gruenbacher 		 * failed to map. If the page hasn't been added to ioend, it
1356763e4cdcSBrian Foster 		 * won't be affected by I/O completion and we must unlock it
1357763e4cdcSBrian Foster 		 * now.
1358598ecfbaSChristoph Hellwig 		 */
1359598ecfbaSChristoph Hellwig 		if (wpc->ops->discard_page)
1360763e4cdcSBrian Foster 			wpc->ops->discard_page(page, file_offset);
1361763e4cdcSBrian Foster 		if (!count) {
1362598ecfbaSChristoph Hellwig 			ClearPageUptodate(page);
1363598ecfbaSChristoph Hellwig 			unlock_page(page);
1364598ecfbaSChristoph Hellwig 			goto done;
1365598ecfbaSChristoph Hellwig 		}
1366598ecfbaSChristoph Hellwig 	}
1367598ecfbaSChristoph Hellwig 
136850e7d6c7SBrian Foster 	set_page_writeback(page);
1369598ecfbaSChristoph Hellwig 	unlock_page(page);
1370598ecfbaSChristoph Hellwig 
1371598ecfbaSChristoph Hellwig 	/*
1372f1f264b4SAndreas Gruenbacher 	 * Preserve the original error if there was one; catch
1373598ecfbaSChristoph Hellwig 	 * submission errors here and propagate into subsequent ioend
1374598ecfbaSChristoph Hellwig 	 * submissions.
1375598ecfbaSChristoph Hellwig 	 */
1376598ecfbaSChristoph Hellwig 	list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
1377598ecfbaSChristoph Hellwig 		int error2;
1378598ecfbaSChristoph Hellwig 
1379598ecfbaSChristoph Hellwig 		list_del_init(&ioend->io_list);
1380598ecfbaSChristoph Hellwig 		error2 = iomap_submit_ioend(wpc, ioend, error);
1381598ecfbaSChristoph Hellwig 		if (error2 && !error)
1382598ecfbaSChristoph Hellwig 			error = error2;
1383598ecfbaSChristoph Hellwig 	}
1384598ecfbaSChristoph Hellwig 
1385598ecfbaSChristoph Hellwig 	/*
1386598ecfbaSChristoph Hellwig 	 * We can end up here with no error and nothing to write only if we race
1387598ecfbaSChristoph Hellwig 	 * with a partial page truncate on a sub-page block sized filesystem.
1388598ecfbaSChristoph Hellwig 	 */
1389598ecfbaSChristoph Hellwig 	if (!count)
1390598ecfbaSChristoph Hellwig 		end_page_writeback(page);
1391598ecfbaSChristoph Hellwig done:
1392598ecfbaSChristoph Hellwig 	mapping_set_error(page->mapping, error);
1393598ecfbaSChristoph Hellwig 	return error;
1394598ecfbaSChristoph Hellwig }
1395598ecfbaSChristoph Hellwig 
1396598ecfbaSChristoph Hellwig /*
1397598ecfbaSChristoph Hellwig  * Write out a dirty page.
1398598ecfbaSChristoph Hellwig  *
1399f1f264b4SAndreas Gruenbacher  * For delalloc space on the page, we need to allocate space and flush it.
1400f1f264b4SAndreas Gruenbacher  * For unwritten space on the page, we need to start the conversion to
1401598ecfbaSChristoph Hellwig  * regular allocated space.
1402598ecfbaSChristoph Hellwig  */
1403598ecfbaSChristoph Hellwig static int
1404598ecfbaSChristoph Hellwig iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)
1405598ecfbaSChristoph Hellwig {
1406598ecfbaSChristoph Hellwig 	struct iomap_writepage_ctx *wpc = data;
1407598ecfbaSChristoph Hellwig 	struct inode *inode = page->mapping->host;
1408598ecfbaSChristoph Hellwig 	pgoff_t end_index;
1409598ecfbaSChristoph Hellwig 	u64 end_offset;
1410598ecfbaSChristoph Hellwig 	loff_t offset;
1411598ecfbaSChristoph Hellwig 
14121ac99452SMatthew Wilcox (Oracle) 	trace_iomap_writepage(inode, page_offset(page), PAGE_SIZE);
1413598ecfbaSChristoph Hellwig 
1414598ecfbaSChristoph Hellwig 	/*
1415f1f264b4SAndreas Gruenbacher 	 * Refuse to write the page out if we're called from reclaim context.
1416598ecfbaSChristoph Hellwig 	 *
1417598ecfbaSChristoph Hellwig 	 * This avoids stack overflows when called from deeply used stacks in
1418598ecfbaSChristoph Hellwig 	 * random callers for direct reclaim or memcg reclaim.  We explicitly
1419598ecfbaSChristoph Hellwig 	 * allow reclaim from kswapd as the stack usage there is relatively low.
1420598ecfbaSChristoph Hellwig 	 *
1421598ecfbaSChristoph Hellwig 	 * This should never happen except in the case of a VM regression so
1422598ecfbaSChristoph Hellwig 	 * warn about it.
1423598ecfbaSChristoph Hellwig 	 */
1424598ecfbaSChristoph Hellwig 	if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
1425598ecfbaSChristoph Hellwig 			PF_MEMALLOC))
1426598ecfbaSChristoph Hellwig 		goto redirty;
1427598ecfbaSChristoph Hellwig 
1428598ecfbaSChristoph Hellwig 	/*
1429598ecfbaSChristoph Hellwig 	 * Is this page beyond the end of the file?
1430598ecfbaSChristoph Hellwig 	 *
1431598ecfbaSChristoph Hellwig 	 * The page index is less than the end_index, adjust the end_offset
1432598ecfbaSChristoph Hellwig 	 * to the highest offset that this page should represent.
1433598ecfbaSChristoph Hellwig 	 * -----------------------------------------------------
1434598ecfbaSChristoph Hellwig 	 * |			file mapping	       | <EOF> |
1435598ecfbaSChristoph Hellwig 	 * -----------------------------------------------------
1436598ecfbaSChristoph Hellwig 	 * | Page ... | Page N-2 | Page N-1 |  Page N  |       |
1437598ecfbaSChristoph Hellwig 	 * ^--------------------------------^----------|--------
1438598ecfbaSChristoph Hellwig 	 * |     desired writeback range    |      see else    |
1439598ecfbaSChristoph Hellwig 	 * ---------------------------------^------------------|
1440598ecfbaSChristoph Hellwig 	 */
1441598ecfbaSChristoph Hellwig 	offset = i_size_read(inode);
1442598ecfbaSChristoph Hellwig 	end_index = offset >> PAGE_SHIFT;
1443598ecfbaSChristoph Hellwig 	if (page->index < end_index)
1444598ecfbaSChristoph Hellwig 		end_offset = (loff_t)(page->index + 1) << PAGE_SHIFT;
1445598ecfbaSChristoph Hellwig 	else {
1446598ecfbaSChristoph Hellwig 		/*
1447598ecfbaSChristoph Hellwig 		 * Check whether the page to write out is beyond or straddles
1448598ecfbaSChristoph Hellwig 		 * i_size or not.
1449598ecfbaSChristoph Hellwig 		 * -------------------------------------------------------
1450598ecfbaSChristoph Hellwig 		 * |		file mapping		        | <EOF>  |
1451598ecfbaSChristoph Hellwig 		 * -------------------------------------------------------
1452598ecfbaSChristoph Hellwig 		 * | Page ... | Page N-2 | Page N-1 |  Page N   | Beyond |
1453598ecfbaSChristoph Hellwig 		 * ^--------------------------------^-----------|---------
1454598ecfbaSChristoph Hellwig 		 * |				    |      Straddles     |
1455598ecfbaSChristoph Hellwig 		 * ---------------------------------^-----------|--------|
1456598ecfbaSChristoph Hellwig 		 */
1457598ecfbaSChristoph Hellwig 		unsigned offset_into_page = offset & (PAGE_SIZE - 1);
1458598ecfbaSChristoph Hellwig 
1459598ecfbaSChristoph Hellwig 		/*
1460f1f264b4SAndreas Gruenbacher 		 * Skip the page if it's fully outside i_size, e.g. due to a
1461f1f264b4SAndreas Gruenbacher 		 * truncate operation that's in progress. We must redirty the
1462598ecfbaSChristoph Hellwig 		 * page so that reclaim stops reclaiming it. Otherwise
1463598ecfbaSChristoph Hellwig 		 * iomap_vm_releasepage() is called on it and gets confused.
1464598ecfbaSChristoph Hellwig 		 *
1465f1f264b4SAndreas Gruenbacher 		 * Note that the end_index is unsigned long.  If the given
1466f1f264b4SAndreas Gruenbacher 		 * offset is greater than 16TB on a 32-bit system then if we
1467f1f264b4SAndreas Gruenbacher 		 * checked if the page is fully outside i_size with
1468f1f264b4SAndreas Gruenbacher 		 * "if (page->index >= end_index + 1)", "end_index + 1" would
1469f1f264b4SAndreas Gruenbacher 		 * overflow and evaluate to 0.  Hence this page would be
1470f1f264b4SAndreas Gruenbacher 		 * redirtied and written out repeatedly, which would result in
1471f1f264b4SAndreas Gruenbacher 		 * an infinite loop; the user program performing this operation
1472f1f264b4SAndreas Gruenbacher 		 * would hang.  Instead, we can detect this situation by
1473f1f264b4SAndreas Gruenbacher 		 * checking if the page is totally beyond i_size or if its
1474598ecfbaSChristoph Hellwig 		 * offset is just equal to the EOF.
1475598ecfbaSChristoph Hellwig 		 */
1476598ecfbaSChristoph Hellwig 		if (page->index > end_index ||
1477598ecfbaSChristoph Hellwig 		    (page->index == end_index && offset_into_page == 0))
1478598ecfbaSChristoph Hellwig 			goto redirty;
1479598ecfbaSChristoph Hellwig 
1480598ecfbaSChristoph Hellwig 		/*
1481598ecfbaSChristoph Hellwig 		 * The page straddles i_size.  It must be zeroed out on each
1482598ecfbaSChristoph Hellwig 		 * and every writepage invocation because it may be mmapped.
1483598ecfbaSChristoph Hellwig 		 * "A file is mapped in multiples of the page size.  For a file
1484598ecfbaSChristoph Hellwig 		 * that is not a multiple of the page size, the remaining
1485598ecfbaSChristoph Hellwig 		 * memory is zeroed when mapped, and writes to that region are
1486598ecfbaSChristoph Hellwig 		 * not written out to the file."
1487598ecfbaSChristoph Hellwig 		 */
1488598ecfbaSChristoph Hellwig 		zero_user_segment(page, offset_into_page, PAGE_SIZE);
1489598ecfbaSChristoph Hellwig 
1490598ecfbaSChristoph Hellwig 		/* Adjust the end_offset to the end of file */
1491598ecfbaSChristoph Hellwig 		end_offset = offset;
1492598ecfbaSChristoph Hellwig 	}
1493598ecfbaSChristoph Hellwig 
1494598ecfbaSChristoph Hellwig 	return iomap_writepage_map(wpc, wbc, inode, page, end_offset);
1495598ecfbaSChristoph Hellwig 
1496598ecfbaSChristoph Hellwig redirty:
1497598ecfbaSChristoph Hellwig 	redirty_page_for_writepage(wbc, page);
1498598ecfbaSChristoph Hellwig 	unlock_page(page);
1499598ecfbaSChristoph Hellwig 	return 0;
1500598ecfbaSChristoph Hellwig }
1501598ecfbaSChristoph Hellwig 
1502598ecfbaSChristoph Hellwig int
1503598ecfbaSChristoph Hellwig iomap_writepage(struct page *page, struct writeback_control *wbc,
1504598ecfbaSChristoph Hellwig 		struct iomap_writepage_ctx *wpc,
1505598ecfbaSChristoph Hellwig 		const struct iomap_writeback_ops *ops)
1506598ecfbaSChristoph Hellwig {
1507598ecfbaSChristoph Hellwig 	int ret;
1508598ecfbaSChristoph Hellwig 
1509598ecfbaSChristoph Hellwig 	wpc->ops = ops;
1510598ecfbaSChristoph Hellwig 	ret = iomap_do_writepage(page, wbc, wpc);
1511598ecfbaSChristoph Hellwig 	if (!wpc->ioend)
1512598ecfbaSChristoph Hellwig 		return ret;
1513598ecfbaSChristoph Hellwig 	return iomap_submit_ioend(wpc, wpc->ioend, ret);
1514598ecfbaSChristoph Hellwig }
1515598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_writepage);
1516598ecfbaSChristoph Hellwig 
1517598ecfbaSChristoph Hellwig int
1518598ecfbaSChristoph Hellwig iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1519598ecfbaSChristoph Hellwig 		struct iomap_writepage_ctx *wpc,
1520598ecfbaSChristoph Hellwig 		const struct iomap_writeback_ops *ops)
1521598ecfbaSChristoph Hellwig {
1522598ecfbaSChristoph Hellwig 	int			ret;
1523598ecfbaSChristoph Hellwig 
1524598ecfbaSChristoph Hellwig 	wpc->ops = ops;
1525598ecfbaSChristoph Hellwig 	ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
1526598ecfbaSChristoph Hellwig 	if (!wpc->ioend)
1527598ecfbaSChristoph Hellwig 		return ret;
1528598ecfbaSChristoph Hellwig 	return iomap_submit_ioend(wpc, wpc->ioend, ret);
1529598ecfbaSChristoph Hellwig }
1530598ecfbaSChristoph Hellwig EXPORT_SYMBOL_GPL(iomap_writepages);
1531598ecfbaSChristoph Hellwig 
1532598ecfbaSChristoph Hellwig static int __init iomap_init(void)
1533598ecfbaSChristoph Hellwig {
1534598ecfbaSChristoph Hellwig 	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
1535598ecfbaSChristoph Hellwig 			   offsetof(struct iomap_ioend, io_inline_bio),
1536598ecfbaSChristoph Hellwig 			   BIOSET_NEED_BVECS);
1537598ecfbaSChristoph Hellwig }
1538598ecfbaSChristoph Hellwig fs_initcall(iomap_init);
1539