xref: /linux/fs/iomap/buffered-io.c (revision 0d6ccfe6b319d56da63b7d7cfbcecd92780a680d)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2010 Red Hat, Inc.
4  * Copyright (C) 2016-2023 Christoph Hellwig.
5  */
6 #include <linux/module.h>
7 #include <linux/compiler.h>
8 #include <linux/fs.h>
9 #include <linux/iomap.h>
10 #include <linux/pagemap.h>
11 #include <linux/uio.h>
12 #include <linux/buffer_head.h>
13 #include <linux/dax.h>
14 #include <linux/writeback.h>
15 #include <linux/list_sort.h>
16 #include <linux/swap.h>
17 #include <linux/bio.h>
18 #include <linux/sched/signal.h>
19 #include <linux/migrate.h>
20 #include "trace.h"
21 
22 #include "../internal.h"
23 
24 #define IOEND_BATCH_SIZE	4096
25 
26 typedef int (*iomap_punch_t)(struct inode *inode, loff_t offset, loff_t length);
27 /*
28  * Structure allocated for each folio to track per-block uptodate, dirty state
29  * and I/O completions.
30  */
31 struct iomap_folio_state {
32 	spinlock_t		state_lock;
33 	unsigned int		read_bytes_pending;
34 	atomic_t		write_bytes_pending;
35 
36 	/*
37 	 * Each block has two bits in this bitmap:
38 	 * Bits [0..blocks_per_folio) has the uptodate status.
39 	 * Bits [b_p_f...(2*b_p_f))   has the dirty status.
40 	 */
41 	unsigned long		state[];
42 };
43 
44 static struct bio_set iomap_ioend_bioset;
45 
46 static inline bool ifs_is_fully_uptodate(struct folio *folio,
47 		struct iomap_folio_state *ifs)
48 {
49 	struct inode *inode = folio->mapping->host;
50 
51 	return bitmap_full(ifs->state, i_blocks_per_folio(inode, folio));
52 }
53 
54 static inline bool ifs_block_is_uptodate(struct iomap_folio_state *ifs,
55 		unsigned int block)
56 {
57 	return test_bit(block, ifs->state);
58 }
59 
60 static bool ifs_set_range_uptodate(struct folio *folio,
61 		struct iomap_folio_state *ifs, size_t off, size_t len)
62 {
63 	struct inode *inode = folio->mapping->host;
64 	unsigned int first_blk = off >> inode->i_blkbits;
65 	unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
66 	unsigned int nr_blks = last_blk - first_blk + 1;
67 
68 	bitmap_set(ifs->state, first_blk, nr_blks);
69 	return ifs_is_fully_uptodate(folio, ifs);
70 }
71 
72 static void iomap_set_range_uptodate(struct folio *folio, size_t off,
73 		size_t len)
74 {
75 	struct iomap_folio_state *ifs = folio->private;
76 	unsigned long flags;
77 	bool uptodate = true;
78 
79 	if (ifs) {
80 		spin_lock_irqsave(&ifs->state_lock, flags);
81 		uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
82 		spin_unlock_irqrestore(&ifs->state_lock, flags);
83 	}
84 
85 	if (uptodate)
86 		folio_mark_uptodate(folio);
87 }
88 
89 static inline bool ifs_block_is_dirty(struct folio *folio,
90 		struct iomap_folio_state *ifs, int block)
91 {
92 	struct inode *inode = folio->mapping->host;
93 	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
94 
95 	return test_bit(block + blks_per_folio, ifs->state);
96 }
97 
98 static unsigned ifs_find_dirty_range(struct folio *folio,
99 		struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
100 {
101 	struct inode *inode = folio->mapping->host;
102 	unsigned start_blk =
103 		offset_in_folio(folio, *range_start) >> inode->i_blkbits;
104 	unsigned end_blk = min_not_zero(
105 		offset_in_folio(folio, range_end) >> inode->i_blkbits,
106 		i_blocks_per_folio(inode, folio));
107 	unsigned nblks = 1;
108 
109 	while (!ifs_block_is_dirty(folio, ifs, start_blk))
110 		if (++start_blk == end_blk)
111 			return 0;
112 
113 	while (start_blk + nblks < end_blk) {
114 		if (!ifs_block_is_dirty(folio, ifs, start_blk + nblks))
115 			break;
116 		nblks++;
117 	}
118 
119 	*range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
120 	return nblks << inode->i_blkbits;
121 }
122 
123 static unsigned iomap_find_dirty_range(struct folio *folio, u64 *range_start,
124 		u64 range_end)
125 {
126 	struct iomap_folio_state *ifs = folio->private;
127 
128 	if (*range_start >= range_end)
129 		return 0;
130 
131 	if (ifs)
132 		return ifs_find_dirty_range(folio, ifs, range_start, range_end);
133 	return range_end - *range_start;
134 }
135 
136 static void ifs_clear_range_dirty(struct folio *folio,
137 		struct iomap_folio_state *ifs, size_t off, size_t len)
138 {
139 	struct inode *inode = folio->mapping->host;
140 	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
141 	unsigned int first_blk = (off >> inode->i_blkbits);
142 	unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
143 	unsigned int nr_blks = last_blk - first_blk + 1;
144 	unsigned long flags;
145 
146 	spin_lock_irqsave(&ifs->state_lock, flags);
147 	bitmap_clear(ifs->state, first_blk + blks_per_folio, nr_blks);
148 	spin_unlock_irqrestore(&ifs->state_lock, flags);
149 }
150 
151 static void iomap_clear_range_dirty(struct folio *folio, size_t off, size_t len)
152 {
153 	struct iomap_folio_state *ifs = folio->private;
154 
155 	if (ifs)
156 		ifs_clear_range_dirty(folio, ifs, off, len);
157 }
158 
159 static void ifs_set_range_dirty(struct folio *folio,
160 		struct iomap_folio_state *ifs, size_t off, size_t len)
161 {
162 	struct inode *inode = folio->mapping->host;
163 	unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
164 	unsigned int first_blk = (off >> inode->i_blkbits);
165 	unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
166 	unsigned int nr_blks = last_blk - first_blk + 1;
167 	unsigned long flags;
168 
169 	spin_lock_irqsave(&ifs->state_lock, flags);
170 	bitmap_set(ifs->state, first_blk + blks_per_folio, nr_blks);
171 	spin_unlock_irqrestore(&ifs->state_lock, flags);
172 }
173 
174 static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
175 {
176 	struct iomap_folio_state *ifs = folio->private;
177 
178 	if (ifs)
179 		ifs_set_range_dirty(folio, ifs, off, len);
180 }
181 
182 static struct iomap_folio_state *ifs_alloc(struct inode *inode,
183 		struct folio *folio, unsigned int flags)
184 {
185 	struct iomap_folio_state *ifs = folio->private;
186 	unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
187 	gfp_t gfp;
188 
189 	if (ifs || nr_blocks <= 1)
190 		return ifs;
191 
192 	if (flags & IOMAP_NOWAIT)
193 		gfp = GFP_NOWAIT;
194 	else
195 		gfp = GFP_NOFS | __GFP_NOFAIL;
196 
197 	/*
198 	 * ifs->state tracks two sets of state flags when the
199 	 * filesystem block size is smaller than the folio size.
200 	 * The first state tracks per-block uptodate and the
201 	 * second tracks per-block dirty state.
202 	 */
203 	ifs = kzalloc(struct_size(ifs, state,
204 		      BITS_TO_LONGS(2 * nr_blocks)), gfp);
205 	if (!ifs)
206 		return ifs;
207 
208 	spin_lock_init(&ifs->state_lock);
209 	if (folio_test_uptodate(folio))
210 		bitmap_set(ifs->state, 0, nr_blocks);
211 	if (folio_test_dirty(folio))
212 		bitmap_set(ifs->state, nr_blocks, nr_blocks);
213 	folio_attach_private(folio, ifs);
214 
215 	return ifs;
216 }
217 
218 static void ifs_free(struct folio *folio)
219 {
220 	struct iomap_folio_state *ifs = folio_detach_private(folio);
221 
222 	if (!ifs)
223 		return;
224 	WARN_ON_ONCE(ifs->read_bytes_pending != 0);
225 	WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending));
226 	WARN_ON_ONCE(ifs_is_fully_uptodate(folio, ifs) !=
227 			folio_test_uptodate(folio));
228 	kfree(ifs);
229 }
230 
231 /*
232  * Calculate the range inside the folio that we actually need to read.
233  */
234 static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
235 		loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
236 {
237 	struct iomap_folio_state *ifs = folio->private;
238 	loff_t orig_pos = *pos;
239 	loff_t isize = i_size_read(inode);
240 	unsigned block_bits = inode->i_blkbits;
241 	unsigned block_size = (1 << block_bits);
242 	size_t poff = offset_in_folio(folio, *pos);
243 	size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
244 	size_t orig_plen = plen;
245 	unsigned first = poff >> block_bits;
246 	unsigned last = (poff + plen - 1) >> block_bits;
247 
248 	/*
249 	 * If the block size is smaller than the page size, we need to check the
250 	 * per-block uptodate status and adjust the offset and length if needed
251 	 * to avoid reading in already uptodate ranges.
252 	 */
253 	if (ifs) {
254 		unsigned int i;
255 
256 		/* move forward for each leading block marked uptodate */
257 		for (i = first; i <= last; i++) {
258 			if (!ifs_block_is_uptodate(ifs, i))
259 				break;
260 			*pos += block_size;
261 			poff += block_size;
262 			plen -= block_size;
263 			first++;
264 		}
265 
266 		/* truncate len if we find any trailing uptodate block(s) */
267 		for ( ; i <= last; i++) {
268 			if (ifs_block_is_uptodate(ifs, i)) {
269 				plen -= (last - i + 1) * block_size;
270 				last = i - 1;
271 				break;
272 			}
273 		}
274 	}
275 
276 	/*
277 	 * If the extent spans the block that contains the i_size, we need to
278 	 * handle both halves separately so that we properly zero data in the
279 	 * page cache for blocks that are entirely outside of i_size.
280 	 */
281 	if (orig_pos <= isize && orig_pos + orig_plen > isize) {
282 		unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
283 
284 		if (first <= end && last > end)
285 			plen -= (last - end) * block_size;
286 	}
287 
288 	*offp = poff;
289 	*lenp = plen;
290 }
291 
292 static void iomap_finish_folio_read(struct folio *folio, size_t off,
293 		size_t len, int error)
294 {
295 	struct iomap_folio_state *ifs = folio->private;
296 	bool uptodate = !error;
297 	bool finished = true;
298 
299 	if (ifs) {
300 		unsigned long flags;
301 
302 		spin_lock_irqsave(&ifs->state_lock, flags);
303 		if (!error)
304 			uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
305 		ifs->read_bytes_pending -= len;
306 		finished = !ifs->read_bytes_pending;
307 		spin_unlock_irqrestore(&ifs->state_lock, flags);
308 	}
309 
310 	if (finished)
311 		folio_end_read(folio, uptodate);
312 }
313 
314 static void iomap_read_end_io(struct bio *bio)
315 {
316 	int error = blk_status_to_errno(bio->bi_status);
317 	struct folio_iter fi;
318 
319 	bio_for_each_folio_all(fi, bio)
320 		iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error);
321 	bio_put(bio);
322 }
323 
324 struct iomap_readpage_ctx {
325 	struct folio		*cur_folio;
326 	bool			cur_folio_in_bio;
327 	struct bio		*bio;
328 	struct readahead_control *rac;
329 };
330 
331 /**
332  * iomap_read_inline_data - copy inline data into the page cache
333  * @iter: iteration structure
334  * @folio: folio to copy to
335  *
336  * Copy the inline data in @iter into @folio and zero out the rest of the folio.
337  * Only a single IOMAP_INLINE extent is allowed at the end of each file.
338  * Returns zero for success to complete the read, or the usual negative errno.
339  */
340 static int iomap_read_inline_data(const struct iomap_iter *iter,
341 		struct folio *folio)
342 {
343 	const struct iomap *iomap = iomap_iter_srcmap(iter);
344 	size_t size = i_size_read(iter->inode) - iomap->offset;
345 	size_t offset = offset_in_folio(folio, iomap->offset);
346 
347 	if (folio_test_uptodate(folio))
348 		return 0;
349 
350 	if (WARN_ON_ONCE(size > iomap->length))
351 		return -EIO;
352 	if (offset > 0)
353 		ifs_alloc(iter->inode, folio, iter->flags);
354 
355 	folio_fill_tail(folio, offset, iomap->inline_data, size);
356 	iomap_set_range_uptodate(folio, offset, folio_size(folio) - offset);
357 	return 0;
358 }
359 
360 static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
361 		loff_t pos)
362 {
363 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
364 
365 	return srcmap->type != IOMAP_MAPPED ||
366 		(srcmap->flags & IOMAP_F_NEW) ||
367 		pos >= i_size_read(iter->inode);
368 }
369 
370 static loff_t iomap_readpage_iter(const struct iomap_iter *iter,
371 		struct iomap_readpage_ctx *ctx, loff_t offset)
372 {
373 	const struct iomap *iomap = &iter->iomap;
374 	loff_t pos = iter->pos + offset;
375 	loff_t length = iomap_length(iter) - offset;
376 	struct folio *folio = ctx->cur_folio;
377 	struct iomap_folio_state *ifs;
378 	loff_t orig_pos = pos;
379 	size_t poff, plen;
380 	sector_t sector;
381 
382 	if (iomap->type == IOMAP_INLINE)
383 		return iomap_read_inline_data(iter, folio);
384 
385 	/* zero post-eof blocks as the page may be mapped */
386 	ifs = ifs_alloc(iter->inode, folio, iter->flags);
387 	iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen);
388 	if (plen == 0)
389 		goto done;
390 
391 	if (iomap_block_needs_zeroing(iter, pos)) {
392 		folio_zero_range(folio, poff, plen);
393 		iomap_set_range_uptodate(folio, poff, plen);
394 		goto done;
395 	}
396 
397 	ctx->cur_folio_in_bio = true;
398 	if (ifs) {
399 		spin_lock_irq(&ifs->state_lock);
400 		ifs->read_bytes_pending += plen;
401 		spin_unlock_irq(&ifs->state_lock);
402 	}
403 
404 	sector = iomap_sector(iomap, pos);
405 	if (!ctx->bio ||
406 	    bio_end_sector(ctx->bio) != sector ||
407 	    !bio_add_folio(ctx->bio, folio, plen, poff)) {
408 		gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
409 		gfp_t orig_gfp = gfp;
410 		unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
411 
412 		if (ctx->bio)
413 			submit_bio(ctx->bio);
414 
415 		if (ctx->rac) /* same as readahead_gfp_mask */
416 			gfp |= __GFP_NORETRY | __GFP_NOWARN;
417 		ctx->bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs),
418 				     REQ_OP_READ, gfp);
419 		/*
420 		 * If the bio_alloc fails, try it again for a single page to
421 		 * avoid having to deal with partial page reads.  This emulates
422 		 * what do_mpage_read_folio does.
423 		 */
424 		if (!ctx->bio) {
425 			ctx->bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ,
426 					     orig_gfp);
427 		}
428 		if (ctx->rac)
429 			ctx->bio->bi_opf |= REQ_RAHEAD;
430 		ctx->bio->bi_iter.bi_sector = sector;
431 		ctx->bio->bi_end_io = iomap_read_end_io;
432 		bio_add_folio_nofail(ctx->bio, folio, plen, poff);
433 	}
434 
435 done:
436 	/*
437 	 * Move the caller beyond our range so that it keeps making progress.
438 	 * For that, we have to include any leading non-uptodate ranges, but
439 	 * we can skip trailing ones as they will be handled in the next
440 	 * iteration.
441 	 */
442 	return pos - orig_pos + plen;
443 }
444 
445 static loff_t iomap_read_folio_iter(const struct iomap_iter *iter,
446 		struct iomap_readpage_ctx *ctx)
447 {
448 	struct folio *folio = ctx->cur_folio;
449 	size_t offset = offset_in_folio(folio, iter->pos);
450 	loff_t length = min_t(loff_t, folio_size(folio) - offset,
451 			      iomap_length(iter));
452 	loff_t done, ret;
453 
454 	for (done = 0; done < length; done += ret) {
455 		ret = iomap_readpage_iter(iter, ctx, done);
456 		if (ret <= 0)
457 			return ret;
458 	}
459 
460 	return done;
461 }
462 
463 int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops)
464 {
465 	struct iomap_iter iter = {
466 		.inode		= folio->mapping->host,
467 		.pos		= folio_pos(folio),
468 		.len		= folio_size(folio),
469 	};
470 	struct iomap_readpage_ctx ctx = {
471 		.cur_folio	= folio,
472 	};
473 	int ret;
474 
475 	trace_iomap_readpage(iter.inode, 1);
476 
477 	while ((ret = iomap_iter(&iter, ops)) > 0)
478 		iter.processed = iomap_read_folio_iter(&iter, &ctx);
479 
480 	if (ctx.bio) {
481 		submit_bio(ctx.bio);
482 		WARN_ON_ONCE(!ctx.cur_folio_in_bio);
483 	} else {
484 		WARN_ON_ONCE(ctx.cur_folio_in_bio);
485 		folio_unlock(folio);
486 	}
487 
488 	/*
489 	 * Just like mpage_readahead and block_read_full_folio, we always
490 	 * return 0 and just set the folio error flag on errors.  This
491 	 * should be cleaned up throughout the stack eventually.
492 	 */
493 	return 0;
494 }
495 EXPORT_SYMBOL_GPL(iomap_read_folio);
496 
497 static loff_t iomap_readahead_iter(const struct iomap_iter *iter,
498 		struct iomap_readpage_ctx *ctx)
499 {
500 	loff_t length = iomap_length(iter);
501 	loff_t done, ret;
502 
503 	for (done = 0; done < length; done += ret) {
504 		if (ctx->cur_folio &&
505 		    offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) {
506 			if (!ctx->cur_folio_in_bio)
507 				folio_unlock(ctx->cur_folio);
508 			ctx->cur_folio = NULL;
509 		}
510 		if (!ctx->cur_folio) {
511 			ctx->cur_folio = readahead_folio(ctx->rac);
512 			ctx->cur_folio_in_bio = false;
513 		}
514 		ret = iomap_readpage_iter(iter, ctx, done);
515 		if (ret <= 0)
516 			return ret;
517 	}
518 
519 	return done;
520 }
521 
522 /**
523  * iomap_readahead - Attempt to read pages from a file.
524  * @rac: Describes the pages to be read.
525  * @ops: The operations vector for the filesystem.
526  *
527  * This function is for filesystems to call to implement their readahead
528  * address_space operation.
529  *
530  * Context: The @ops callbacks may submit I/O (eg to read the addresses of
531  * blocks from disc), and may wait for it.  The caller may be trying to
532  * access a different page, and so sleeping excessively should be avoided.
533  * It may allocate memory, but should avoid costly allocations.  This
534  * function is called with memalloc_nofs set, so allocations will not cause
535  * the filesystem to be reentered.
536  */
537 void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
538 {
539 	struct iomap_iter iter = {
540 		.inode	= rac->mapping->host,
541 		.pos	= readahead_pos(rac),
542 		.len	= readahead_length(rac),
543 	};
544 	struct iomap_readpage_ctx ctx = {
545 		.rac	= rac,
546 	};
547 
548 	trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
549 
550 	while (iomap_iter(&iter, ops) > 0)
551 		iter.processed = iomap_readahead_iter(&iter, &ctx);
552 
553 	if (ctx.bio)
554 		submit_bio(ctx.bio);
555 	if (ctx.cur_folio) {
556 		if (!ctx.cur_folio_in_bio)
557 			folio_unlock(ctx.cur_folio);
558 	}
559 }
560 EXPORT_SYMBOL_GPL(iomap_readahead);
561 
562 /*
563  * iomap_is_partially_uptodate checks whether blocks within a folio are
564  * uptodate or not.
565  *
566  * Returns true if all blocks which correspond to the specified part
567  * of the folio are uptodate.
568  */
569 bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
570 {
571 	struct iomap_folio_state *ifs = folio->private;
572 	struct inode *inode = folio->mapping->host;
573 	unsigned first, last, i;
574 
575 	if (!ifs)
576 		return false;
577 
578 	/* Caller's range may extend past the end of this folio */
579 	count = min(folio_size(folio) - from, count);
580 
581 	/* First and last blocks in range within folio */
582 	first = from >> inode->i_blkbits;
583 	last = (from + count - 1) >> inode->i_blkbits;
584 
585 	for (i = first; i <= last; i++)
586 		if (!ifs_block_is_uptodate(ifs, i))
587 			return false;
588 	return true;
589 }
590 EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
591 
592 /**
593  * iomap_get_folio - get a folio reference for writing
594  * @iter: iteration structure
595  * @pos: start offset of write
596  * @len: Suggested size of folio to create.
597  *
598  * Returns a locked reference to the folio at @pos, or an error pointer if the
599  * folio could not be obtained.
600  */
601 struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len)
602 {
603 	fgf_t fgp = FGP_WRITEBEGIN | FGP_NOFS;
604 
605 	if (iter->flags & IOMAP_NOWAIT)
606 		fgp |= FGP_NOWAIT;
607 	fgp |= fgf_set_order(len);
608 
609 	return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
610 			fgp, mapping_gfp_mask(iter->inode->i_mapping));
611 }
612 EXPORT_SYMBOL_GPL(iomap_get_folio);
613 
614 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags)
615 {
616 	trace_iomap_release_folio(folio->mapping->host, folio_pos(folio),
617 			folio_size(folio));
618 
619 	/*
620 	 * If the folio is dirty, we refuse to release our metadata because
621 	 * it may be partially dirty.  Once we track per-block dirty state,
622 	 * we can release the metadata if every block is dirty.
623 	 */
624 	if (folio_test_dirty(folio))
625 		return false;
626 	ifs_free(folio);
627 	return true;
628 }
629 EXPORT_SYMBOL_GPL(iomap_release_folio);
630 
631 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
632 {
633 	trace_iomap_invalidate_folio(folio->mapping->host,
634 					folio_pos(folio) + offset, len);
635 
636 	/*
637 	 * If we're invalidating the entire folio, clear the dirty state
638 	 * from it and release it to avoid unnecessary buildup of the LRU.
639 	 */
640 	if (offset == 0 && len == folio_size(folio)) {
641 		WARN_ON_ONCE(folio_test_writeback(folio));
642 		folio_cancel_dirty(folio);
643 		ifs_free(folio);
644 	}
645 }
646 EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
647 
648 bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
649 {
650 	struct inode *inode = mapping->host;
651 	size_t len = folio_size(folio);
652 
653 	ifs_alloc(inode, folio, 0);
654 	iomap_set_range_dirty(folio, 0, len);
655 	return filemap_dirty_folio(mapping, folio);
656 }
657 EXPORT_SYMBOL_GPL(iomap_dirty_folio);
658 
659 static void
660 iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
661 {
662 	loff_t i_size = i_size_read(inode);
663 
664 	/*
665 	 * Only truncate newly allocated pages beyoned EOF, even if the
666 	 * write started inside the existing inode size.
667 	 */
668 	if (pos + len > i_size)
669 		truncate_pagecache_range(inode, max(pos, i_size),
670 					 pos + len - 1);
671 }
672 
673 static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
674 		size_t poff, size_t plen, const struct iomap *iomap)
675 {
676 	struct bio_vec bvec;
677 	struct bio bio;
678 
679 	bio_init(&bio, iomap->bdev, &bvec, 1, REQ_OP_READ);
680 	bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
681 	bio_add_folio_nofail(&bio, folio, plen, poff);
682 	return submit_bio_wait(&bio);
683 }
684 
685 static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
686 		size_t len, struct folio *folio)
687 {
688 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
689 	struct iomap_folio_state *ifs;
690 	loff_t block_size = i_blocksize(iter->inode);
691 	loff_t block_start = round_down(pos, block_size);
692 	loff_t block_end = round_up(pos + len, block_size);
693 	unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio);
694 	size_t from = offset_in_folio(folio, pos), to = from + len;
695 	size_t poff, plen;
696 
697 	/*
698 	 * If the write or zeroing completely overlaps the current folio, then
699 	 * entire folio will be dirtied so there is no need for
700 	 * per-block state tracking structures to be attached to this folio.
701 	 * For the unshare case, we must read in the ondisk contents because we
702 	 * are not changing pagecache contents.
703 	 */
704 	if (!(iter->flags & IOMAP_UNSHARE) && pos <= folio_pos(folio) &&
705 	    pos + len >= folio_pos(folio) + folio_size(folio))
706 		return 0;
707 
708 	ifs = ifs_alloc(iter->inode, folio, iter->flags);
709 	if ((iter->flags & IOMAP_NOWAIT) && !ifs && nr_blocks > 1)
710 		return -EAGAIN;
711 
712 	if (folio_test_uptodate(folio))
713 		return 0;
714 
715 	do {
716 		iomap_adjust_read_range(iter->inode, folio, &block_start,
717 				block_end - block_start, &poff, &plen);
718 		if (plen == 0)
719 			break;
720 
721 		if (!(iter->flags & IOMAP_UNSHARE) &&
722 		    (from <= poff || from >= poff + plen) &&
723 		    (to <= poff || to >= poff + plen))
724 			continue;
725 
726 		if (iomap_block_needs_zeroing(iter, block_start)) {
727 			if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
728 				return -EIO;
729 			folio_zero_segments(folio, poff, from, to, poff + plen);
730 		} else {
731 			int status;
732 
733 			if (iter->flags & IOMAP_NOWAIT)
734 				return -EAGAIN;
735 
736 			status = iomap_read_folio_sync(block_start, folio,
737 					poff, plen, srcmap);
738 			if (status)
739 				return status;
740 		}
741 		iomap_set_range_uptodate(folio, poff, plen);
742 	} while ((block_start += plen) < block_end);
743 
744 	return 0;
745 }
746 
747 static struct folio *__iomap_get_folio(struct iomap_iter *iter, loff_t pos,
748 		size_t len)
749 {
750 	const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
751 
752 	if (folio_ops && folio_ops->get_folio)
753 		return folio_ops->get_folio(iter, pos, len);
754 	else
755 		return iomap_get_folio(iter, pos, len);
756 }
757 
758 static void __iomap_put_folio(struct iomap_iter *iter, loff_t pos, size_t ret,
759 		struct folio *folio)
760 {
761 	const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
762 
763 	if (folio_ops && folio_ops->put_folio) {
764 		folio_ops->put_folio(iter->inode, pos, ret, folio);
765 	} else {
766 		folio_unlock(folio);
767 		folio_put(folio);
768 	}
769 }
770 
771 static int iomap_write_begin_inline(const struct iomap_iter *iter,
772 		struct folio *folio)
773 {
774 	/* needs more work for the tailpacking case; disable for now */
775 	if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
776 		return -EIO;
777 	return iomap_read_inline_data(iter, folio);
778 }
779 
780 static int iomap_write_begin(struct iomap_iter *iter, loff_t pos,
781 		size_t len, struct folio **foliop)
782 {
783 	const struct iomap_folio_ops *folio_ops = iter->iomap.folio_ops;
784 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
785 	struct folio *folio;
786 	int status = 0;
787 
788 	BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
789 	if (srcmap != &iter->iomap)
790 		BUG_ON(pos + len > srcmap->offset + srcmap->length);
791 
792 	if (fatal_signal_pending(current))
793 		return -EINTR;
794 
795 	if (!mapping_large_folio_support(iter->inode->i_mapping))
796 		len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
797 
798 	folio = __iomap_get_folio(iter, pos, len);
799 	if (IS_ERR(folio))
800 		return PTR_ERR(folio);
801 
802 	/*
803 	 * Now we have a locked folio, before we do anything with it we need to
804 	 * check that the iomap we have cached is not stale. The inode extent
805 	 * mapping can change due to concurrent IO in flight (e.g.
806 	 * IOMAP_UNWRITTEN state can change and memory reclaim could have
807 	 * reclaimed a previously partially written page at this index after IO
808 	 * completion before this write reaches this file offset) and hence we
809 	 * could do the wrong thing here (zero a page range incorrectly or fail
810 	 * to zero) and corrupt data.
811 	 */
812 	if (folio_ops && folio_ops->iomap_valid) {
813 		bool iomap_valid = folio_ops->iomap_valid(iter->inode,
814 							 &iter->iomap);
815 		if (!iomap_valid) {
816 			iter->iomap.flags |= IOMAP_F_STALE;
817 			status = 0;
818 			goto out_unlock;
819 		}
820 	}
821 
822 	if (pos + len > folio_pos(folio) + folio_size(folio))
823 		len = folio_pos(folio) + folio_size(folio) - pos;
824 
825 	if (srcmap->type == IOMAP_INLINE)
826 		status = iomap_write_begin_inline(iter, folio);
827 	else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
828 		status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
829 	else
830 		status = __iomap_write_begin(iter, pos, len, folio);
831 
832 	if (unlikely(status))
833 		goto out_unlock;
834 
835 	*foliop = folio;
836 	return 0;
837 
838 out_unlock:
839 	__iomap_put_folio(iter, pos, 0, folio);
840 
841 	return status;
842 }
843 
844 static bool __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
845 		size_t copied, struct folio *folio)
846 {
847 	flush_dcache_folio(folio);
848 
849 	/*
850 	 * The blocks that were entirely written will now be uptodate, so we
851 	 * don't have to worry about a read_folio reading them and overwriting a
852 	 * partial write.  However, if we've encountered a short write and only
853 	 * partially written into a block, it will not be marked uptodate, so a
854 	 * read_folio might come in and destroy our partial write.
855 	 *
856 	 * Do the simplest thing and just treat any short write to a
857 	 * non-uptodate page as a zero-length write, and force the caller to
858 	 * redo the whole thing.
859 	 */
860 	if (unlikely(copied < len && !folio_test_uptodate(folio)))
861 		return false;
862 	iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
863 	iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
864 	filemap_dirty_folio(inode->i_mapping, folio);
865 	return true;
866 }
867 
868 static void iomap_write_end_inline(const struct iomap_iter *iter,
869 		struct folio *folio, loff_t pos, size_t copied)
870 {
871 	const struct iomap *iomap = &iter->iomap;
872 	void *addr;
873 
874 	WARN_ON_ONCE(!folio_test_uptodate(folio));
875 	BUG_ON(!iomap_inline_data_valid(iomap));
876 
877 	flush_dcache_folio(folio);
878 	addr = kmap_local_folio(folio, pos);
879 	memcpy(iomap_inline_data(iomap, pos), addr, copied);
880 	kunmap_local(addr);
881 
882 	mark_inode_dirty(iter->inode);
883 }
884 
885 /*
886  * Returns true if all copied bytes have been written to the pagecache,
887  * otherwise return false.
888  */
889 static bool iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len,
890 		size_t copied, struct folio *folio)
891 {
892 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
893 
894 	if (srcmap->type == IOMAP_INLINE) {
895 		iomap_write_end_inline(iter, folio, pos, copied);
896 		return true;
897 	}
898 
899 	if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
900 		size_t bh_written;
901 
902 		bh_written = block_write_end(NULL, iter->inode->i_mapping, pos,
903 					len, copied, &folio->page, NULL);
904 		WARN_ON_ONCE(bh_written != copied && bh_written != 0);
905 		return bh_written == copied;
906 	}
907 
908 	return __iomap_write_end(iter->inode, pos, len, copied, folio);
909 }
910 
911 static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
912 {
913 	loff_t length = iomap_length(iter);
914 	loff_t pos = iter->pos;
915 	ssize_t total_written = 0;
916 	long status = 0;
917 	struct address_space *mapping = iter->inode->i_mapping;
918 	size_t chunk = mapping_max_folio_size(mapping);
919 	unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0;
920 
921 	do {
922 		struct folio *folio;
923 		loff_t old_size;
924 		size_t offset;		/* Offset into folio */
925 		size_t bytes;		/* Bytes to write to folio */
926 		size_t copied;		/* Bytes copied from user */
927 		size_t written;		/* Bytes have been written */
928 
929 		bytes = iov_iter_count(i);
930 retry:
931 		offset = pos & (chunk - 1);
932 		bytes = min(chunk - offset, bytes);
933 		status = balance_dirty_pages_ratelimited_flags(mapping,
934 							       bdp_flags);
935 		if (unlikely(status))
936 			break;
937 
938 		if (bytes > length)
939 			bytes = length;
940 
941 		/*
942 		 * Bring in the user page that we'll copy from _first_.
943 		 * Otherwise there's a nasty deadlock on copying from the
944 		 * same page as we're writing to, without it being marked
945 		 * up-to-date.
946 		 *
947 		 * For async buffered writes the assumption is that the user
948 		 * page has already been faulted in. This can be optimized by
949 		 * faulting the user page.
950 		 */
951 		if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
952 			status = -EFAULT;
953 			break;
954 		}
955 
956 		status = iomap_write_begin(iter, pos, bytes, &folio);
957 		if (unlikely(status)) {
958 			iomap_write_failed(iter->inode, pos, bytes);
959 			break;
960 		}
961 		if (iter->iomap.flags & IOMAP_F_STALE)
962 			break;
963 
964 		offset = offset_in_folio(folio, pos);
965 		if (bytes > folio_size(folio) - offset)
966 			bytes = folio_size(folio) - offset;
967 
968 		if (mapping_writably_mapped(mapping))
969 			flush_dcache_folio(folio);
970 
971 		copied = copy_folio_from_iter_atomic(folio, offset, bytes, i);
972 		written = iomap_write_end(iter, pos, bytes, copied, folio) ?
973 			  copied : 0;
974 
975 		/*
976 		 * Update the in-memory inode size after copying the data into
977 		 * the page cache.  It's up to the file system to write the
978 		 * updated size to disk, preferably after I/O completion so that
979 		 * no stale data is exposed.  Only once that's done can we
980 		 * unlock and release the folio.
981 		 */
982 		old_size = iter->inode->i_size;
983 		if (pos + written > old_size) {
984 			i_size_write(iter->inode, pos + written);
985 			iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
986 		}
987 		__iomap_put_folio(iter, pos, written, folio);
988 
989 		if (old_size < pos)
990 			pagecache_isize_extended(iter->inode, old_size, pos);
991 
992 		cond_resched();
993 		if (unlikely(written == 0)) {
994 			/*
995 			 * A short copy made iomap_write_end() reject the
996 			 * thing entirely.  Might be memory poisoning
997 			 * halfway through, might be a race with munmap,
998 			 * might be severe memory pressure.
999 			 */
1000 			iomap_write_failed(iter->inode, pos, bytes);
1001 			iov_iter_revert(i, copied);
1002 
1003 			if (chunk > PAGE_SIZE)
1004 				chunk /= 2;
1005 			if (copied) {
1006 				bytes = copied;
1007 				goto retry;
1008 			}
1009 		} else {
1010 			pos += written;
1011 			total_written += written;
1012 			length -= written;
1013 		}
1014 	} while (iov_iter_count(i) && length);
1015 
1016 	if (status == -EAGAIN) {
1017 		iov_iter_revert(i, total_written);
1018 		return -EAGAIN;
1019 	}
1020 	return total_written ? total_written : status;
1021 }
1022 
1023 ssize_t
1024 iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
1025 		const struct iomap_ops *ops)
1026 {
1027 	struct iomap_iter iter = {
1028 		.inode		= iocb->ki_filp->f_mapping->host,
1029 		.pos		= iocb->ki_pos,
1030 		.len		= iov_iter_count(i),
1031 		.flags		= IOMAP_WRITE,
1032 	};
1033 	ssize_t ret;
1034 
1035 	if (iocb->ki_flags & IOCB_NOWAIT)
1036 		iter.flags |= IOMAP_NOWAIT;
1037 
1038 	while ((ret = iomap_iter(&iter, ops)) > 0)
1039 		iter.processed = iomap_write_iter(&iter, i);
1040 
1041 	if (unlikely(iter.pos == iocb->ki_pos))
1042 		return ret;
1043 	ret = iter.pos - iocb->ki_pos;
1044 	iocb->ki_pos = iter.pos;
1045 	return ret;
1046 }
1047 EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
1048 
1049 static int iomap_write_delalloc_ifs_punch(struct inode *inode,
1050 		struct folio *folio, loff_t start_byte, loff_t end_byte,
1051 		iomap_punch_t punch)
1052 {
1053 	unsigned int first_blk, last_blk, i;
1054 	loff_t last_byte;
1055 	u8 blkbits = inode->i_blkbits;
1056 	struct iomap_folio_state *ifs;
1057 	int ret = 0;
1058 
1059 	/*
1060 	 * When we have per-block dirty tracking, there can be
1061 	 * blocks within a folio which are marked uptodate
1062 	 * but not dirty. In that case it is necessary to punch
1063 	 * out such blocks to avoid leaking any delalloc blocks.
1064 	 */
1065 	ifs = folio->private;
1066 	if (!ifs)
1067 		return ret;
1068 
1069 	last_byte = min_t(loff_t, end_byte - 1,
1070 			folio_pos(folio) + folio_size(folio) - 1);
1071 	first_blk = offset_in_folio(folio, start_byte) >> blkbits;
1072 	last_blk = offset_in_folio(folio, last_byte) >> blkbits;
1073 	for (i = first_blk; i <= last_blk; i++) {
1074 		if (!ifs_block_is_dirty(folio, ifs, i)) {
1075 			ret = punch(inode, folio_pos(folio) + (i << blkbits),
1076 				    1 << blkbits);
1077 			if (ret)
1078 				return ret;
1079 		}
1080 	}
1081 
1082 	return ret;
1083 }
1084 
1085 
1086 static int iomap_write_delalloc_punch(struct inode *inode, struct folio *folio,
1087 		loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
1088 		iomap_punch_t punch)
1089 {
1090 	int ret = 0;
1091 
1092 	if (!folio_test_dirty(folio))
1093 		return ret;
1094 
1095 	/* if dirty, punch up to offset */
1096 	if (start_byte > *punch_start_byte) {
1097 		ret = punch(inode, *punch_start_byte,
1098 				start_byte - *punch_start_byte);
1099 		if (ret)
1100 			return ret;
1101 	}
1102 
1103 	/* Punch non-dirty blocks within folio */
1104 	ret = iomap_write_delalloc_ifs_punch(inode, folio, start_byte,
1105 			end_byte, punch);
1106 	if (ret)
1107 		return ret;
1108 
1109 	/*
1110 	 * Make sure the next punch start is correctly bound to
1111 	 * the end of this data range, not the end of the folio.
1112 	 */
1113 	*punch_start_byte = min_t(loff_t, end_byte,
1114 				folio_pos(folio) + folio_size(folio));
1115 
1116 	return ret;
1117 }
1118 
1119 /*
1120  * Scan the data range passed to us for dirty page cache folios. If we find a
1121  * dirty folio, punch out the preceding range and update the offset from which
1122  * the next punch will start from.
1123  *
1124  * We can punch out storage reservations under clean pages because they either
1125  * contain data that has been written back - in which case the delalloc punch
1126  * over that range is a no-op - or they have been read faults in which case they
1127  * contain zeroes and we can remove the delalloc backing range and any new
1128  * writes to those pages will do the normal hole filling operation...
1129  *
1130  * This makes the logic simple: we only need to keep the delalloc extents only
1131  * over the dirty ranges of the page cache.
1132  *
1133  * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
1134  * simplify range iterations.
1135  */
1136 static int iomap_write_delalloc_scan(struct inode *inode,
1137 		loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
1138 		iomap_punch_t punch)
1139 {
1140 	while (start_byte < end_byte) {
1141 		struct folio	*folio;
1142 		int ret;
1143 
1144 		/* grab locked page */
1145 		folio = filemap_lock_folio(inode->i_mapping,
1146 				start_byte >> PAGE_SHIFT);
1147 		if (IS_ERR(folio)) {
1148 			start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) +
1149 					PAGE_SIZE;
1150 			continue;
1151 		}
1152 
1153 		ret = iomap_write_delalloc_punch(inode, folio, punch_start_byte,
1154 						 start_byte, end_byte, punch);
1155 		if (ret) {
1156 			folio_unlock(folio);
1157 			folio_put(folio);
1158 			return ret;
1159 		}
1160 
1161 		/* move offset to start of next folio in range */
1162 		start_byte = folio_next_index(folio) << PAGE_SHIFT;
1163 		folio_unlock(folio);
1164 		folio_put(folio);
1165 	}
1166 	return 0;
1167 }
1168 
1169 /*
1170  * Punch out all the delalloc blocks in the range given except for those that
1171  * have dirty data still pending in the page cache - those are going to be
1172  * written and so must still retain the delalloc backing for writeback.
1173  *
1174  * As we are scanning the page cache for data, we don't need to reimplement the
1175  * wheel - mapping_seek_hole_data() does exactly what we need to identify the
1176  * start and end of data ranges correctly even for sub-folio block sizes. This
1177  * byte range based iteration is especially convenient because it means we
1178  * don't have to care about variable size folios, nor where the start or end of
1179  * the data range lies within a folio, if they lie within the same folio or even
1180  * if there are multiple discontiguous data ranges within the folio.
1181  *
1182  * It should be noted that mapping_seek_hole_data() is not aware of EOF, and so
1183  * can return data ranges that exist in the cache beyond EOF. e.g. a page fault
1184  * spanning EOF will initialise the post-EOF data to zeroes and mark it up to
1185  * date. A write page fault can then mark it dirty. If we then fail a write()
1186  * beyond EOF into that up to date cached range, we allocate a delalloc block
1187  * beyond EOF and then have to punch it out. Because the range is up to date,
1188  * mapping_seek_hole_data() will return it, and we will skip the punch because
1189  * the folio is dirty. THis is incorrect - we always need to punch out delalloc
1190  * beyond EOF in this case as writeback will never write back and covert that
1191  * delalloc block beyond EOF. Hence we limit the cached data scan range to EOF,
1192  * resulting in always punching out the range from the EOF to the end of the
1193  * range the iomap spans.
1194  *
1195  * Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it
1196  * matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA
1197  * returns the start of a data range (start_byte), and SEEK_HOLE(start_byte)
1198  * returns the end of the data range (data_end). Using closed intervals would
1199  * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose
1200  * the code to subtle off-by-one bugs....
1201  */
1202 static int iomap_write_delalloc_release(struct inode *inode,
1203 		loff_t start_byte, loff_t end_byte, iomap_punch_t punch)
1204 {
1205 	loff_t punch_start_byte = start_byte;
1206 	loff_t scan_end_byte = min(i_size_read(inode), end_byte);
1207 	int error = 0;
1208 
1209 	/*
1210 	 * Lock the mapping to avoid races with page faults re-instantiating
1211 	 * folios and dirtying them via ->page_mkwrite whilst we walk the
1212 	 * cache and perform delalloc extent removal. Failing to do this can
1213 	 * leave dirty pages with no space reservation in the cache.
1214 	 */
1215 	filemap_invalidate_lock(inode->i_mapping);
1216 	while (start_byte < scan_end_byte) {
1217 		loff_t		data_end;
1218 
1219 		start_byte = mapping_seek_hole_data(inode->i_mapping,
1220 				start_byte, scan_end_byte, SEEK_DATA);
1221 		/*
1222 		 * If there is no more data to scan, all that is left is to
1223 		 * punch out the remaining range.
1224 		 */
1225 		if (start_byte == -ENXIO || start_byte == scan_end_byte)
1226 			break;
1227 		if (start_byte < 0) {
1228 			error = start_byte;
1229 			goto out_unlock;
1230 		}
1231 		WARN_ON_ONCE(start_byte < punch_start_byte);
1232 		WARN_ON_ONCE(start_byte > scan_end_byte);
1233 
1234 		/*
1235 		 * We find the end of this contiguous cached data range by
1236 		 * seeking from start_byte to the beginning of the next hole.
1237 		 */
1238 		data_end = mapping_seek_hole_data(inode->i_mapping, start_byte,
1239 				scan_end_byte, SEEK_HOLE);
1240 		if (data_end < 0) {
1241 			error = data_end;
1242 			goto out_unlock;
1243 		}
1244 		WARN_ON_ONCE(data_end <= start_byte);
1245 		WARN_ON_ONCE(data_end > scan_end_byte);
1246 
1247 		error = iomap_write_delalloc_scan(inode, &punch_start_byte,
1248 				start_byte, data_end, punch);
1249 		if (error)
1250 			goto out_unlock;
1251 
1252 		/* The next data search starts at the end of this one. */
1253 		start_byte = data_end;
1254 	}
1255 
1256 	if (punch_start_byte < end_byte)
1257 		error = punch(inode, punch_start_byte,
1258 				end_byte - punch_start_byte);
1259 out_unlock:
1260 	filemap_invalidate_unlock(inode->i_mapping);
1261 	return error;
1262 }
1263 
1264 /*
1265  * When a short write occurs, the filesystem may need to remove reserved space
1266  * that was allocated in ->iomap_begin from it's ->iomap_end method. For
1267  * filesystems that use delayed allocation, we need to punch out delalloc
1268  * extents from the range that are not dirty in the page cache. As the write can
1269  * race with page faults, there can be dirty pages over the delalloc extent
1270  * outside the range of a short write but still within the delalloc extent
1271  * allocated for this iomap.
1272  *
1273  * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
1274  * simplify range iterations.
1275  *
1276  * The punch() callback *must* only punch delalloc extents in the range passed
1277  * to it. It must skip over all other types of extents in the range and leave
1278  * them completely unchanged. It must do this punch atomically with respect to
1279  * other extent modifications.
1280  *
1281  * The punch() callback may be called with a folio locked to prevent writeback
1282  * extent allocation racing at the edge of the range we are currently punching.
1283  * The locked folio may or may not cover the range being punched, so it is not
1284  * safe for the punch() callback to lock folios itself.
1285  *
1286  * Lock order is:
1287  *
1288  * inode->i_rwsem (shared or exclusive)
1289  *   inode->i_mapping->invalidate_lock (exclusive)
1290  *     folio_lock()
1291  *       ->punch
1292  *         internal filesystem allocation lock
1293  */
1294 int iomap_file_buffered_write_punch_delalloc(struct inode *inode,
1295 		struct iomap *iomap, loff_t pos, loff_t length,
1296 		ssize_t written, iomap_punch_t punch)
1297 {
1298 	loff_t			start_byte;
1299 	loff_t			end_byte;
1300 	unsigned int		blocksize = i_blocksize(inode);
1301 
1302 	if (iomap->type != IOMAP_DELALLOC)
1303 		return 0;
1304 
1305 	/* If we didn't reserve the blocks, we're not allowed to punch them. */
1306 	if (!(iomap->flags & IOMAP_F_NEW))
1307 		return 0;
1308 
1309 	/*
1310 	 * start_byte refers to the first unused block after a short write. If
1311 	 * nothing was written, round offset down to point at the first block in
1312 	 * the range.
1313 	 */
1314 	if (unlikely(!written))
1315 		start_byte = round_down(pos, blocksize);
1316 	else
1317 		start_byte = round_up(pos + written, blocksize);
1318 	end_byte = round_up(pos + length, blocksize);
1319 
1320 	/* Nothing to do if we've written the entire delalloc extent */
1321 	if (start_byte >= end_byte)
1322 		return 0;
1323 
1324 	return iomap_write_delalloc_release(inode, start_byte, end_byte,
1325 					punch);
1326 }
1327 EXPORT_SYMBOL_GPL(iomap_file_buffered_write_punch_delalloc);
1328 
1329 static loff_t iomap_unshare_iter(struct iomap_iter *iter)
1330 {
1331 	struct iomap *iomap = &iter->iomap;
1332 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
1333 	loff_t pos = iter->pos;
1334 	loff_t length = iomap_length(iter);
1335 	loff_t written = 0;
1336 
1337 	/* don't bother with blocks that are not shared to start with */
1338 	if (!(iomap->flags & IOMAP_F_SHARED))
1339 		return length;
1340 	/* don't bother with holes or unwritten extents */
1341 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
1342 		return length;
1343 
1344 	do {
1345 		struct folio *folio;
1346 		int status;
1347 		size_t offset;
1348 		size_t bytes = min_t(u64, SIZE_MAX, length);
1349 		bool ret;
1350 
1351 		status = iomap_write_begin(iter, pos, bytes, &folio);
1352 		if (unlikely(status))
1353 			return status;
1354 		if (iomap->flags & IOMAP_F_STALE)
1355 			break;
1356 
1357 		offset = offset_in_folio(folio, pos);
1358 		if (bytes > folio_size(folio) - offset)
1359 			bytes = folio_size(folio) - offset;
1360 
1361 		ret = iomap_write_end(iter, pos, bytes, bytes, folio);
1362 		__iomap_put_folio(iter, pos, bytes, folio);
1363 		if (WARN_ON_ONCE(!ret))
1364 			return -EIO;
1365 
1366 		cond_resched();
1367 
1368 		pos += bytes;
1369 		written += bytes;
1370 		length -= bytes;
1371 
1372 		balance_dirty_pages_ratelimited(iter->inode->i_mapping);
1373 	} while (length > 0);
1374 
1375 	return written;
1376 }
1377 
1378 int
1379 iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
1380 		const struct iomap_ops *ops)
1381 {
1382 	struct iomap_iter iter = {
1383 		.inode		= inode,
1384 		.pos		= pos,
1385 		.len		= len,
1386 		.flags		= IOMAP_WRITE | IOMAP_UNSHARE,
1387 	};
1388 	int ret;
1389 
1390 	while ((ret = iomap_iter(&iter, ops)) > 0)
1391 		iter.processed = iomap_unshare_iter(&iter);
1392 	return ret;
1393 }
1394 EXPORT_SYMBOL_GPL(iomap_file_unshare);
1395 
1396 static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
1397 {
1398 	const struct iomap *srcmap = iomap_iter_srcmap(iter);
1399 	loff_t pos = iter->pos;
1400 	loff_t length = iomap_length(iter);
1401 	loff_t written = 0;
1402 
1403 	/* already zeroed?  we're done. */
1404 	if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
1405 		return length;
1406 
1407 	do {
1408 		struct folio *folio;
1409 		int status;
1410 		size_t offset;
1411 		size_t bytes = min_t(u64, SIZE_MAX, length);
1412 		bool ret;
1413 
1414 		status = iomap_write_begin(iter, pos, bytes, &folio);
1415 		if (status)
1416 			return status;
1417 		if (iter->iomap.flags & IOMAP_F_STALE)
1418 			break;
1419 
1420 		offset = offset_in_folio(folio, pos);
1421 		if (bytes > folio_size(folio) - offset)
1422 			bytes = folio_size(folio) - offset;
1423 
1424 		folio_zero_range(folio, offset, bytes);
1425 		folio_mark_accessed(folio);
1426 
1427 		ret = iomap_write_end(iter, pos, bytes, bytes, folio);
1428 		__iomap_put_folio(iter, pos, bytes, folio);
1429 		if (WARN_ON_ONCE(!ret))
1430 			return -EIO;
1431 
1432 		pos += bytes;
1433 		length -= bytes;
1434 		written += bytes;
1435 	} while (length > 0);
1436 
1437 	if (did_zero)
1438 		*did_zero = true;
1439 	return written;
1440 }
1441 
1442 int
1443 iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
1444 		const struct iomap_ops *ops)
1445 {
1446 	struct iomap_iter iter = {
1447 		.inode		= inode,
1448 		.pos		= pos,
1449 		.len		= len,
1450 		.flags		= IOMAP_ZERO,
1451 	};
1452 	int ret;
1453 
1454 	while ((ret = iomap_iter(&iter, ops)) > 0)
1455 		iter.processed = iomap_zero_iter(&iter, did_zero);
1456 	return ret;
1457 }
1458 EXPORT_SYMBOL_GPL(iomap_zero_range);
1459 
1460 int
1461 iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
1462 		const struct iomap_ops *ops)
1463 {
1464 	unsigned int blocksize = i_blocksize(inode);
1465 	unsigned int off = pos & (blocksize - 1);
1466 
1467 	/* Block boundary? Nothing to do */
1468 	if (!off)
1469 		return 0;
1470 	return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
1471 }
1472 EXPORT_SYMBOL_GPL(iomap_truncate_page);
1473 
1474 static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter,
1475 		struct folio *folio)
1476 {
1477 	loff_t length = iomap_length(iter);
1478 	int ret;
1479 
1480 	if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
1481 		ret = __block_write_begin_int(folio, iter->pos, length, NULL,
1482 					      &iter->iomap);
1483 		if (ret)
1484 			return ret;
1485 		block_commit_write(&folio->page, 0, length);
1486 	} else {
1487 		WARN_ON_ONCE(!folio_test_uptodate(folio));
1488 		folio_mark_dirty(folio);
1489 	}
1490 
1491 	return length;
1492 }
1493 
1494 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
1495 {
1496 	struct iomap_iter iter = {
1497 		.inode		= file_inode(vmf->vma->vm_file),
1498 		.flags		= IOMAP_WRITE | IOMAP_FAULT,
1499 	};
1500 	struct folio *folio = page_folio(vmf->page);
1501 	ssize_t ret;
1502 
1503 	folio_lock(folio);
1504 	ret = folio_mkwrite_check_truncate(folio, iter.inode);
1505 	if (ret < 0)
1506 		goto out_unlock;
1507 	iter.pos = folio_pos(folio);
1508 	iter.len = ret;
1509 	while ((ret = iomap_iter(&iter, ops)) > 0)
1510 		iter.processed = iomap_folio_mkwrite_iter(&iter, folio);
1511 
1512 	if (ret < 0)
1513 		goto out_unlock;
1514 	folio_wait_stable(folio);
1515 	return VM_FAULT_LOCKED;
1516 out_unlock:
1517 	folio_unlock(folio);
1518 	return vmf_fs_error(ret);
1519 }
1520 EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
1521 
1522 static void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
1523 		size_t len)
1524 {
1525 	struct iomap_folio_state *ifs = folio->private;
1526 
1527 	WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs);
1528 	WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
1529 
1530 	if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
1531 		folio_end_writeback(folio);
1532 }
1533 
1534 /*
1535  * We're now finished for good with this ioend structure.  Update the page
1536  * state, release holds on bios, and finally free up memory.  Do not use the
1537  * ioend after this.
1538  */
1539 static u32
1540 iomap_finish_ioend(struct iomap_ioend *ioend, int error)
1541 {
1542 	struct inode *inode = ioend->io_inode;
1543 	struct bio *bio = &ioend->io_bio;
1544 	struct folio_iter fi;
1545 	u32 folio_count = 0;
1546 
1547 	if (error) {
1548 		mapping_set_error(inode->i_mapping, error);
1549 		if (!bio_flagged(bio, BIO_QUIET)) {
1550 			pr_err_ratelimited(
1551 "%s: writeback error on inode %lu, offset %lld, sector %llu",
1552 				inode->i_sb->s_id, inode->i_ino,
1553 				ioend->io_offset, ioend->io_sector);
1554 		}
1555 	}
1556 
1557 	/* walk all folios in bio, ending page IO on them */
1558 	bio_for_each_folio_all(fi, bio) {
1559 		iomap_finish_folio_write(inode, fi.folio, fi.length);
1560 		folio_count++;
1561 	}
1562 
1563 	bio_put(bio);	/* frees the ioend */
1564 	return folio_count;
1565 }
1566 
1567 /*
1568  * Ioend completion routine for merged bios. This can only be called from task
1569  * contexts as merged ioends can be of unbound length. Hence we have to break up
1570  * the writeback completions into manageable chunks to avoid long scheduler
1571  * holdoffs. We aim to keep scheduler holdoffs down below 10ms so that we get
1572  * good batch processing throughput without creating adverse scheduler latency
1573  * conditions.
1574  */
1575 void
1576 iomap_finish_ioends(struct iomap_ioend *ioend, int error)
1577 {
1578 	struct list_head tmp;
1579 	u32 completions;
1580 
1581 	might_sleep();
1582 
1583 	list_replace_init(&ioend->io_list, &tmp);
1584 	completions = iomap_finish_ioend(ioend, error);
1585 
1586 	while (!list_empty(&tmp)) {
1587 		if (completions > IOEND_BATCH_SIZE * 8) {
1588 			cond_resched();
1589 			completions = 0;
1590 		}
1591 		ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
1592 		list_del_init(&ioend->io_list);
1593 		completions += iomap_finish_ioend(ioend, error);
1594 	}
1595 }
1596 EXPORT_SYMBOL_GPL(iomap_finish_ioends);
1597 
1598 /*
1599  * We can merge two adjacent ioends if they have the same set of work to do.
1600  */
1601 static bool
1602 iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
1603 {
1604 	if (ioend->io_bio.bi_status != next->io_bio.bi_status)
1605 		return false;
1606 	if ((ioend->io_flags & IOMAP_F_SHARED) ^
1607 	    (next->io_flags & IOMAP_F_SHARED))
1608 		return false;
1609 	if ((ioend->io_type == IOMAP_UNWRITTEN) ^
1610 	    (next->io_type == IOMAP_UNWRITTEN))
1611 		return false;
1612 	if (ioend->io_offset + ioend->io_size != next->io_offset)
1613 		return false;
1614 	/*
1615 	 * Do not merge physically discontiguous ioends. The filesystem
1616 	 * completion functions will have to iterate the physical
1617 	 * discontiguities even if we merge the ioends at a logical level, so
1618 	 * we don't gain anything by merging physical discontiguities here.
1619 	 *
1620 	 * We cannot use bio->bi_iter.bi_sector here as it is modified during
1621 	 * submission so does not point to the start sector of the bio at
1622 	 * completion.
1623 	 */
1624 	if (ioend->io_sector + (ioend->io_size >> 9) != next->io_sector)
1625 		return false;
1626 	return true;
1627 }
1628 
1629 void
1630 iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
1631 {
1632 	struct iomap_ioend *next;
1633 
1634 	INIT_LIST_HEAD(&ioend->io_list);
1635 
1636 	while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
1637 			io_list))) {
1638 		if (!iomap_ioend_can_merge(ioend, next))
1639 			break;
1640 		list_move_tail(&next->io_list, &ioend->io_list);
1641 		ioend->io_size += next->io_size;
1642 	}
1643 }
1644 EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
1645 
1646 static int
1647 iomap_ioend_compare(void *priv, const struct list_head *a,
1648 		const struct list_head *b)
1649 {
1650 	struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
1651 	struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
1652 
1653 	if (ia->io_offset < ib->io_offset)
1654 		return -1;
1655 	if (ia->io_offset > ib->io_offset)
1656 		return 1;
1657 	return 0;
1658 }
1659 
1660 void
1661 iomap_sort_ioends(struct list_head *ioend_list)
1662 {
1663 	list_sort(NULL, ioend_list, iomap_ioend_compare);
1664 }
1665 EXPORT_SYMBOL_GPL(iomap_sort_ioends);
1666 
1667 static void iomap_writepage_end_bio(struct bio *bio)
1668 {
1669 	iomap_finish_ioend(iomap_ioend_from_bio(bio),
1670 			blk_status_to_errno(bio->bi_status));
1671 }
1672 
1673 /*
1674  * Submit the final bio for an ioend.
1675  *
1676  * If @error is non-zero, it means that we have a situation where some part of
1677  * the submission process has failed after we've marked pages for writeback.
1678  * We cannot cancel ioend directly in that case, so call the bio end I/O handler
1679  * with the error status here to run the normal I/O completion handler to clear
1680  * the writeback bit and let the file system proess the errors.
1681  */
1682 static int iomap_submit_ioend(struct iomap_writepage_ctx *wpc, int error)
1683 {
1684 	if (!wpc->ioend)
1685 		return error;
1686 
1687 	/*
1688 	 * Let the file systems prepare the I/O submission and hook in an I/O
1689 	 * comletion handler.  This also needs to happen in case after a
1690 	 * failure happened so that the file system end I/O handler gets called
1691 	 * to clean up.
1692 	 */
1693 	if (wpc->ops->prepare_ioend)
1694 		error = wpc->ops->prepare_ioend(wpc->ioend, error);
1695 
1696 	if (error) {
1697 		wpc->ioend->io_bio.bi_status = errno_to_blk_status(error);
1698 		bio_endio(&wpc->ioend->io_bio);
1699 	} else {
1700 		submit_bio(&wpc->ioend->io_bio);
1701 	}
1702 
1703 	wpc->ioend = NULL;
1704 	return error;
1705 }
1706 
1707 static struct iomap_ioend *iomap_alloc_ioend(struct iomap_writepage_ctx *wpc,
1708 		struct writeback_control *wbc, struct inode *inode, loff_t pos)
1709 {
1710 	struct iomap_ioend *ioend;
1711 	struct bio *bio;
1712 
1713 	bio = bio_alloc_bioset(wpc->iomap.bdev, BIO_MAX_VECS,
1714 			       REQ_OP_WRITE | wbc_to_write_flags(wbc),
1715 			       GFP_NOFS, &iomap_ioend_bioset);
1716 	bio->bi_iter.bi_sector = iomap_sector(&wpc->iomap, pos);
1717 	bio->bi_end_io = iomap_writepage_end_bio;
1718 	wbc_init_bio(wbc, bio);
1719 	bio->bi_write_hint = inode->i_write_hint;
1720 
1721 	ioend = iomap_ioend_from_bio(bio);
1722 	INIT_LIST_HEAD(&ioend->io_list);
1723 	ioend->io_type = wpc->iomap.type;
1724 	ioend->io_flags = wpc->iomap.flags;
1725 	ioend->io_inode = inode;
1726 	ioend->io_size = 0;
1727 	ioend->io_offset = pos;
1728 	ioend->io_sector = bio->bi_iter.bi_sector;
1729 
1730 	wpc->nr_folios = 0;
1731 	return ioend;
1732 }
1733 
1734 static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos)
1735 {
1736 	if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
1737 	    (wpc->ioend->io_flags & IOMAP_F_SHARED))
1738 		return false;
1739 	if (wpc->iomap.type != wpc->ioend->io_type)
1740 		return false;
1741 	if (pos != wpc->ioend->io_offset + wpc->ioend->io_size)
1742 		return false;
1743 	if (iomap_sector(&wpc->iomap, pos) !=
1744 	    bio_end_sector(&wpc->ioend->io_bio))
1745 		return false;
1746 	/*
1747 	 * Limit ioend bio chain lengths to minimise IO completion latency. This
1748 	 * also prevents long tight loops ending page writeback on all the
1749 	 * folios in the ioend.
1750 	 */
1751 	if (wpc->nr_folios >= IOEND_BATCH_SIZE)
1752 		return false;
1753 	return true;
1754 }
1755 
1756 /*
1757  * Test to see if we have an existing ioend structure that we could append to
1758  * first; otherwise finish off the current ioend and start another.
1759  *
1760  * If a new ioend is created and cached, the old ioend is submitted to the block
1761  * layer instantly.  Batching optimisations are provided by higher level block
1762  * plugging.
1763  *
1764  * At the end of a writeback pass, there will be a cached ioend remaining on the
1765  * writepage context that the caller will need to submit.
1766  */
1767 static int iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,
1768 		struct writeback_control *wbc, struct folio *folio,
1769 		struct inode *inode, loff_t pos, unsigned len)
1770 {
1771 	struct iomap_folio_state *ifs = folio->private;
1772 	size_t poff = offset_in_folio(folio, pos);
1773 	int error;
1774 
1775 	if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, pos)) {
1776 new_ioend:
1777 		error = iomap_submit_ioend(wpc, 0);
1778 		if (error)
1779 			return error;
1780 		wpc->ioend = iomap_alloc_ioend(wpc, wbc, inode, pos);
1781 	}
1782 
1783 	if (!bio_add_folio(&wpc->ioend->io_bio, folio, len, poff))
1784 		goto new_ioend;
1785 
1786 	if (ifs)
1787 		atomic_add(len, &ifs->write_bytes_pending);
1788 	wpc->ioend->io_size += len;
1789 	wbc_account_cgroup_owner(wbc, &folio->page, len);
1790 	return 0;
1791 }
1792 
1793 static int iomap_writepage_map_blocks(struct iomap_writepage_ctx *wpc,
1794 		struct writeback_control *wbc, struct folio *folio,
1795 		struct inode *inode, u64 pos, unsigned dirty_len,
1796 		unsigned *count)
1797 {
1798 	int error;
1799 
1800 	do {
1801 		unsigned map_len;
1802 
1803 		error = wpc->ops->map_blocks(wpc, inode, pos, dirty_len);
1804 		if (error)
1805 			break;
1806 		trace_iomap_writepage_map(inode, pos, dirty_len, &wpc->iomap);
1807 
1808 		map_len = min_t(u64, dirty_len,
1809 			wpc->iomap.offset + wpc->iomap.length - pos);
1810 		WARN_ON_ONCE(!folio->private && map_len < dirty_len);
1811 
1812 		switch (wpc->iomap.type) {
1813 		case IOMAP_INLINE:
1814 			WARN_ON_ONCE(1);
1815 			error = -EIO;
1816 			break;
1817 		case IOMAP_HOLE:
1818 			break;
1819 		default:
1820 			error = iomap_add_to_ioend(wpc, wbc, folio, inode, pos,
1821 					map_len);
1822 			if (!error)
1823 				(*count)++;
1824 			break;
1825 		}
1826 		dirty_len -= map_len;
1827 		pos += map_len;
1828 	} while (dirty_len && !error);
1829 
1830 	/*
1831 	 * We cannot cancel the ioend directly here on error.  We may have
1832 	 * already set other pages under writeback and hence we have to run I/O
1833 	 * completion to mark the error state of the pages under writeback
1834 	 * appropriately.
1835 	 *
1836 	 * Just let the file system know what portion of the folio failed to
1837 	 * map.
1838 	 */
1839 	if (error && wpc->ops->discard_folio)
1840 		wpc->ops->discard_folio(folio, pos);
1841 	return error;
1842 }
1843 
1844 /*
1845  * Check interaction of the folio with the file end.
1846  *
1847  * If the folio is entirely beyond i_size, return false.  If it straddles
1848  * i_size, adjust end_pos and zero all data beyond i_size.
1849  */
1850 static bool iomap_writepage_handle_eof(struct folio *folio, struct inode *inode,
1851 		u64 *end_pos)
1852 {
1853 	u64 isize = i_size_read(inode);
1854 
1855 	if (*end_pos > isize) {
1856 		size_t poff = offset_in_folio(folio, isize);
1857 		pgoff_t end_index = isize >> PAGE_SHIFT;
1858 
1859 		/*
1860 		 * If the folio is entirely ouside of i_size, skip it.
1861 		 *
1862 		 * This can happen due to a truncate operation that is in
1863 		 * progress and in that case truncate will finish it off once
1864 		 * we've dropped the folio lock.
1865 		 *
1866 		 * Note that the pgoff_t used for end_index is an unsigned long.
1867 		 * If the given offset is greater than 16TB on a 32-bit system,
1868 		 * then if we checked if the folio is fully outside i_size with
1869 		 * "if (folio->index >= end_index + 1)", "end_index + 1" would
1870 		 * overflow and evaluate to 0.  Hence this folio would be
1871 		 * redirtied and written out repeatedly, which would result in
1872 		 * an infinite loop; the user program performing this operation
1873 		 * would hang.  Instead, we can detect this situation by
1874 		 * checking if the folio is totally beyond i_size or if its
1875 		 * offset is just equal to the EOF.
1876 		 */
1877 		if (folio->index > end_index ||
1878 		    (folio->index == end_index && poff == 0))
1879 			return false;
1880 
1881 		/*
1882 		 * The folio straddles i_size.
1883 		 *
1884 		 * It must be zeroed out on each and every writepage invocation
1885 		 * because it may be mmapped:
1886 		 *
1887 		 *    A file is mapped in multiples of the page size.  For a
1888 		 *    file that is not a multiple of the page size, the
1889 		 *    remaining memory is zeroed when mapped, and writes to that
1890 		 *    region are not written out to the file.
1891 		 *
1892 		 * Also adjust the writeback range to skip all blocks entirely
1893 		 * beyond i_size.
1894 		 */
1895 		folio_zero_segment(folio, poff, folio_size(folio));
1896 		*end_pos = round_up(isize, i_blocksize(inode));
1897 	}
1898 
1899 	return true;
1900 }
1901 
1902 static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,
1903 		struct writeback_control *wbc, struct folio *folio)
1904 {
1905 	struct iomap_folio_state *ifs = folio->private;
1906 	struct inode *inode = folio->mapping->host;
1907 	u64 pos = folio_pos(folio);
1908 	u64 end_pos = pos + folio_size(folio);
1909 	unsigned count = 0;
1910 	int error = 0;
1911 	u32 rlen;
1912 
1913 	WARN_ON_ONCE(!folio_test_locked(folio));
1914 	WARN_ON_ONCE(folio_test_dirty(folio));
1915 	WARN_ON_ONCE(folio_test_writeback(folio));
1916 
1917 	trace_iomap_writepage(inode, pos, folio_size(folio));
1918 
1919 	if (!iomap_writepage_handle_eof(folio, inode, &end_pos)) {
1920 		folio_unlock(folio);
1921 		return 0;
1922 	}
1923 	WARN_ON_ONCE(end_pos <= pos);
1924 
1925 	if (i_blocks_per_folio(inode, folio) > 1) {
1926 		if (!ifs) {
1927 			ifs = ifs_alloc(inode, folio, 0);
1928 			iomap_set_range_dirty(folio, 0, end_pos - pos);
1929 		}
1930 
1931 		/*
1932 		 * Keep the I/O completion handler from clearing the writeback
1933 		 * bit until we have submitted all blocks by adding a bias to
1934 		 * ifs->write_bytes_pending, which is dropped after submitting
1935 		 * all blocks.
1936 		 */
1937 		WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending) != 0);
1938 		atomic_inc(&ifs->write_bytes_pending);
1939 	}
1940 
1941 	/*
1942 	 * Set the writeback bit ASAP, as the I/O completion for the single
1943 	 * block per folio case happen hit as soon as we're submitting the bio.
1944 	 */
1945 	folio_start_writeback(folio);
1946 
1947 	/*
1948 	 * Walk through the folio to find dirty areas to write back.
1949 	 */
1950 	while ((rlen = iomap_find_dirty_range(folio, &pos, end_pos))) {
1951 		error = iomap_writepage_map_blocks(wpc, wbc, folio, inode,
1952 				pos, rlen, &count);
1953 		if (error)
1954 			break;
1955 		pos += rlen;
1956 	}
1957 
1958 	if (count)
1959 		wpc->nr_folios++;
1960 
1961 	/*
1962 	 * We can have dirty bits set past end of file in page_mkwrite path
1963 	 * while mapping the last partial folio. Hence it's better to clear
1964 	 * all the dirty bits in the folio here.
1965 	 */
1966 	iomap_clear_range_dirty(folio, 0, folio_size(folio));
1967 
1968 	/*
1969 	 * Usually the writeback bit is cleared by the I/O completion handler.
1970 	 * But we may end up either not actually writing any blocks, or (when
1971 	 * there are multiple blocks in a folio) all I/O might have finished
1972 	 * already at this point.  In that case we need to clear the writeback
1973 	 * bit ourselves right after unlocking the page.
1974 	 */
1975 	folio_unlock(folio);
1976 	if (ifs) {
1977 		if (atomic_dec_and_test(&ifs->write_bytes_pending))
1978 			folio_end_writeback(folio);
1979 	} else {
1980 		if (!count)
1981 			folio_end_writeback(folio);
1982 	}
1983 	mapping_set_error(inode->i_mapping, error);
1984 	return error;
1985 }
1986 
1987 int
1988 iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
1989 		struct iomap_writepage_ctx *wpc,
1990 		const struct iomap_writeback_ops *ops)
1991 {
1992 	struct folio *folio = NULL;
1993 	int error;
1994 
1995 	/*
1996 	 * Writeback from reclaim context should never happen except in the case
1997 	 * of a VM regression so warn about it and refuse to write the data.
1998 	 */
1999 	if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC | PF_KSWAPD)) ==
2000 			PF_MEMALLOC))
2001 		return -EIO;
2002 
2003 	wpc->ops = ops;
2004 	while ((folio = writeback_iter(mapping, wbc, folio, &error)))
2005 		error = iomap_writepage_map(wpc, wbc, folio);
2006 	return iomap_submit_ioend(wpc, error);
2007 }
2008 EXPORT_SYMBOL_GPL(iomap_writepages);
2009 
2010 static int __init iomap_init(void)
2011 {
2012 	return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
2013 			   offsetof(struct iomap_ioend, io_bio),
2014 			   BIOSET_NEED_BVECS);
2015 }
2016 fs_initcall(iomap_init);
2017