xref: /linux/fs/iomap/ioend.c (revision 21ef2d065ad3f0cfbf2ae51260bf962a9fa2c643)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2016-2025 Christoph Hellwig.
4  */
5 #include <linux/bio-integrity.h>
6 #include <linux/iomap.h>
7 #include <linux/list_sort.h>
8 #include <linux/pagemap.h>
9 #include <linux/writeback.h>
10 #include <linux/fserror.h>
11 #include "internal.h"
12 #include "trace.h"
13 
14 struct bio_set iomap_ioend_bioset;
15 EXPORT_SYMBOL_GPL(iomap_ioend_bioset);
16 static struct bio_set iomap_ioend_split_bioset;
17 
18 struct iomap_ioend *iomap_init_ioend(struct inode *inode,
19 		struct bio *bio, loff_t file_offset, u16 ioend_flags)
20 {
21 	struct iomap_ioend *ioend = iomap_ioend_from_bio(bio);
22 
23 	atomic_set(&ioend->io_remaining, 1);
24 	ioend->io_error = 0;
25 	ioend->io_parent = NULL;
26 	INIT_LIST_HEAD(&ioend->io_list);
27 	ioend->io_flags = ioend_flags;
28 	ioend->io_inode = inode;
29 	ioend->io_offset = file_offset;
30 	ioend->io_size = bio->bi_iter.bi_size;
31 	ioend->io_sector = bio->bi_iter.bi_sector;
32 	ioend->io_vi = NULL;
33 	ioend->io_private = NULL;
34 	return ioend;
35 }
36 EXPORT_SYMBOL_GPL(iomap_init_ioend);
37 
38 /*
39  * We're now finished for good with this ioend structure.  Update the folio
40  * state, release holds on bios, and finally free up memory.  Do not use the
41  * ioend after this.
42  */
43 static u32 iomap_finish_ioend_buffered_write(struct iomap_ioend *ioend)
44 {
45 	struct inode *inode = ioend->io_inode;
46 	struct bio *bio = &ioend->io_bio;
47 	struct folio_iter fi;
48 	u32 folio_count = 0;
49 
50 	if (ioend->io_error) {
51 		mapping_set_error(inode->i_mapping, ioend->io_error);
52 		if (!bio_flagged(bio, BIO_QUIET)) {
53 			pr_err_ratelimited(
54 "%s: writeback error on inode %llu, offset %lld, sector %llu",
55 				inode->i_sb->s_id, inode->i_ino,
56 				ioend->io_offset, ioend->io_sector);
57 		}
58 	}
59 
60 	/* walk all folios in bio, ending page IO on them */
61 	bio_for_each_folio_all(fi, bio) {
62 		if (ioend->io_error)
63 			fserror_report_io(inode, FSERR_BUFFERED_WRITE,
64 					  folio_pos(fi.folio) + fi.offset,
65 					  fi.length, ioend->io_error,
66 					  GFP_ATOMIC);
67 		iomap_finish_folio_write(inode, fi.folio, fi.length);
68 		folio_count++;
69 	}
70 
71 	if (bio_integrity(bio))
72 		fs_bio_integrity_free(bio);
73 	bio_put(bio);	/* frees the ioend */
74 	return folio_count;
75 }
76 
77 static DEFINE_SPINLOCK(failed_ioend_lock);
78 static LIST_HEAD(failed_ioend_list);
79 
80 static void
81 iomap_fail_ioends(
82 	struct work_struct	*work)
83 {
84 	struct iomap_ioend	*ioend;
85 	struct list_head	tmp;
86 	unsigned long		flags;
87 
88 	spin_lock_irqsave(&failed_ioend_lock, flags);
89 	list_replace_init(&failed_ioend_list, &tmp);
90 	spin_unlock_irqrestore(&failed_ioend_lock, flags);
91 
92 	while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend,
93 			io_list))) {
94 		list_del_init(&ioend->io_list);
95 		iomap_finish_ioend_buffered_write(ioend);
96 		cond_resched();
97 	}
98 }
99 
100 static DECLARE_WORK(failed_ioend_work, iomap_fail_ioends);
101 
102 static void iomap_fail_ioend_buffered(struct iomap_ioend *ioend)
103 {
104 	unsigned long flags;
105 
106 	/*
107 	 * Bounce I/O errors to a workqueue to avoid nested i_lock acquisitions
108 	 * in the fserror code.  The caller no longer owns the ioend reference
109 	 * after the spinlock drops.
110 	 */
111 	spin_lock_irqsave(&failed_ioend_lock, flags);
112 	if (list_empty(&failed_ioend_list))
113 		WARN_ON_ONCE(!schedule_work(&failed_ioend_work));
114 	list_add_tail(&ioend->io_list, &failed_ioend_list);
115 	spin_unlock_irqrestore(&failed_ioend_lock, flags);
116 }
117 
118 static void ioend_writeback_end_bio(struct bio *bio)
119 {
120 	struct iomap_ioend *ioend = iomap_ioend_from_bio(bio);
121 
122 	ioend->io_error = blk_status_to_errno(bio->bi_status);
123 	if (ioend->io_error) {
124 		iomap_fail_ioend_buffered(ioend);
125 		return;
126 	}
127 
128 	iomap_finish_ioend_buffered_write(ioend);
129 }
130 
131 /*
132  * We cannot cancel the ioend directly in case of an error, so call the bio end
133  * I/O handler with the error status here to run the normal I/O completion
134  * handler.
135  */
136 int iomap_ioend_writeback_submit(struct iomap_writepage_ctx *wpc, int error)
137 {
138 	struct iomap_ioend *ioend = wpc->wb_ctx;
139 
140 	if (!ioend->io_bio.bi_end_io)
141 		ioend->io_bio.bi_end_io = ioend_writeback_end_bio;
142 
143 	if (WARN_ON_ONCE(wpc->iomap.flags & IOMAP_F_ANON_WRITE))
144 		error = -EIO;
145 
146 	if (error) {
147 		ioend->io_bio.bi_status = errno_to_blk_status(error);
148 		bio_endio(&ioend->io_bio);
149 		return error;
150 	}
151 
152 	if (wpc->iomap.flags & IOMAP_F_INTEGRITY)
153 		fs_bio_integrity_generate(&ioend->io_bio);
154 	submit_bio(&ioend->io_bio);
155 	return 0;
156 }
157 EXPORT_SYMBOL_GPL(iomap_ioend_writeback_submit);
158 
159 static struct iomap_ioend *iomap_alloc_ioend(struct iomap_writepage_ctx *wpc,
160 		loff_t pos, u16 ioend_flags)
161 {
162 	struct bio *bio;
163 
164 	bio = bio_alloc_bioset(wpc->iomap.bdev, BIO_MAX_VECS,
165 			       REQ_OP_WRITE | wbc_to_write_flags(wpc->wbc),
166 			       GFP_NOFS, &iomap_ioend_bioset);
167 	bio->bi_iter.bi_sector = iomap_sector(&wpc->iomap, pos);
168 	bio->bi_write_hint = wpc->inode->i_write_hint;
169 	wbc_init_bio(wpc->wbc, bio);
170 	wpc->nr_folios = 0;
171 	return iomap_init_ioend(wpc->inode, bio, pos, ioend_flags);
172 }
173 
174 static bool iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t pos,
175 		unsigned int map_len, u16 ioend_flags)
176 {
177 	struct iomap_ioend *ioend = wpc->wb_ctx;
178 
179 	if (ioend->io_bio.bi_iter.bi_size >
180 	    iomap_max_bio_size(&wpc->iomap) - map_len)
181 		return false;
182 	if (ioend_flags & IOMAP_IOEND_BOUNDARY)
183 		return false;
184 	if ((ioend_flags & IOMAP_IOEND_NOMERGE_FLAGS) !=
185 	    (ioend->io_flags & IOMAP_IOEND_NOMERGE_FLAGS))
186 		return false;
187 	if (pos != ioend->io_offset + ioend->io_size)
188 		return false;
189 	if (!(wpc->iomap.flags & IOMAP_F_ANON_WRITE) &&
190 	    iomap_sector(&wpc->iomap, pos) != bio_end_sector(&ioend->io_bio))
191 		return false;
192 	/*
193 	 * Limit ioend bio chain lengths to minimise IO completion latency. This
194 	 * also prevents long tight loops ending page writeback on all the
195 	 * folios in the ioend.
196 	 */
197 	if (wpc->nr_folios >= IOEND_BATCH_SIZE)
198 		return false;
199 	return true;
200 }
201 
202 /*
203  * Test to see if we have an existing ioend structure that we could append to
204  * first; otherwise finish off the current ioend and start another.
205  *
206  * If a new ioend is created and cached, the old ioend is submitted to the block
207  * layer instantly.  Batching optimisations are provided by higher level block
208  * plugging.
209  *
210  * At the end of a writeback pass, there will be a cached ioend remaining on the
211  * writepage context that the caller will need to submit.
212  */
213 ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,
214 		loff_t pos, loff_t end_pos, unsigned int dirty_len)
215 {
216 	struct iomap_ioend *ioend = wpc->wb_ctx;
217 	size_t poff = offset_in_folio(folio, pos);
218 	unsigned int ioend_flags = 0;
219 	unsigned int map_len = min_t(u64, dirty_len,
220 		wpc->iomap.offset + wpc->iomap.length - pos);
221 	int error;
222 
223 	trace_iomap_add_to_ioend(wpc->inode, pos, dirty_len, &wpc->iomap);
224 
225 	WARN_ON_ONCE(!folio->private && map_len < dirty_len);
226 
227 	switch (wpc->iomap.type) {
228 	case IOMAP_UNWRITTEN:
229 		ioend_flags |= IOMAP_IOEND_UNWRITTEN;
230 		break;
231 	case IOMAP_MAPPED:
232 		break;
233 	case IOMAP_HOLE:
234 		return map_len;
235 	default:
236 		WARN_ON_ONCE(1);
237 		return -EIO;
238 	}
239 
240 	if (wpc->iomap.flags & IOMAP_F_SHARED)
241 		ioend_flags |= IOMAP_IOEND_SHARED;
242 	if (folio_test_dropbehind(folio))
243 		ioend_flags |= IOMAP_IOEND_DONTCACHE;
244 	if (pos == wpc->iomap.offset && (wpc->iomap.flags & IOMAP_F_BOUNDARY))
245 		ioend_flags |= IOMAP_IOEND_BOUNDARY;
246 
247 	if (!ioend || !iomap_can_add_to_ioend(wpc, pos, map_len, ioend_flags)) {
248 new_ioend:
249 		if (ioend) {
250 			error = wpc->ops->writeback_submit(wpc, 0);
251 			if (error)
252 				return error;
253 		}
254 		wpc->wb_ctx = ioend = iomap_alloc_ioend(wpc, pos, ioend_flags);
255 	}
256 
257 	if (!bio_add_folio(&ioend->io_bio, folio, map_len, poff))
258 		goto new_ioend;
259 
260 	/*
261 	 * Clamp io_offset and io_size to the incore EOF so that ondisk
262 	 * file size updates in the ioend completion are byte-accurate.
263 	 * This avoids recovering files with zeroed tail regions when
264 	 * writeback races with appending writes:
265 	 *
266 	 *    Thread 1:                  Thread 2:
267 	 *    ------------               -----------
268 	 *    write [A, A+B]
269 	 *    update inode size to A+B
270 	 *    submit I/O [A, A+BS]
271 	 *                               write [A+B, A+B+C]
272 	 *                               update inode size to A+B+C
273 	 *    <I/O completes, updates disk size to min(A+B+C, A+BS)>
274 	 *    <power failure>
275 	 *
276 	 *  After reboot:
277 	 *    1) with A+B+C < A+BS, the file has zero padding in range
278 	 *       [A+B, A+B+C]
279 	 *
280 	 *    |<     Block Size (BS)   >|
281 	 *    |DDDDDDDDDDDD0000000000000|
282 	 *    ^           ^        ^
283 	 *    A          A+B     A+B+C
284 	 *                       (EOF)
285 	 *
286 	 *    2) with A+B+C > A+BS, the file has zero padding in range
287 	 *       [A+B, A+BS]
288 	 *
289 	 *    |<     Block Size (BS)   >|<     Block Size (BS)    >|
290 	 *    |DDDDDDDDDDDD0000000000000|00000000000000000000000000|
291 	 *    ^           ^             ^           ^
292 	 *    A          A+B           A+BS       A+B+C
293 	 *                             (EOF)
294 	 *
295 	 *    D = Valid Data
296 	 *    0 = Zero Padding
297 	 *
298 	 * Note that this defeats the ability to chain the ioends of
299 	 * appending writes.
300 	 */
301 	ioend->io_size += map_len;
302 	if (ioend->io_offset + ioend->io_size > end_pos) {
303 		if (ioend->io_offset >= end_pos)
304 			ioend->io_size = 0;
305 		else
306 			ioend->io_size = end_pos - ioend->io_offset;
307 	}
308 
309 	wbc_account_cgroup_owner(wpc->wbc, folio, map_len);
310 	return map_len;
311 }
312 EXPORT_SYMBOL_GPL(iomap_add_to_ioend);
313 
314 static u32 iomap_finish_ioend(struct iomap_ioend *ioend, int error)
315 {
316 	if (ioend->io_parent) {
317 		struct bio *bio = &ioend->io_bio;
318 
319 		ioend = ioend->io_parent;
320 		bio_put(bio);
321 	}
322 
323 	if (error)
324 		cmpxchg(&ioend->io_error, 0, error);
325 
326 	if (!atomic_dec_and_test(&ioend->io_remaining))
327 		return 0;
328 
329 	if (!ioend->io_error &&
330 	    bio_integrity(&ioend->io_bio) &&
331 	    bio_op(&ioend->io_bio) == REQ_OP_READ) {
332 		ioend->io_error = fs_bio_integrity_verify(&ioend->io_bio,
333 			ioend->io_sector, ioend->io_size);
334 	}
335 
336 	if (ioend->io_flags & IOMAP_IOEND_DIRECT)
337 		return iomap_finish_ioend_direct(ioend);
338 	if (bio_op(&ioend->io_bio) == REQ_OP_READ)
339 		return iomap_finish_ioend_buffered_read(ioend);
340 	return iomap_finish_ioend_buffered_write(ioend);
341 }
342 
343 /*
344  * Ioend completion routine for merged bios. This can only be called from task
345  * contexts as merged ioends can be of unbound length. Hence we have to break up
346  * the writeback completions into manageable chunks to avoid long scheduler
347  * holdoffs. We aim to keep scheduler holdoffs down below 10ms so that we get
348  * good batch processing throughput without creating adverse scheduler latency
349  * conditions.
350  */
351 void iomap_finish_ioends(struct iomap_ioend *ioend, int error)
352 {
353 	struct list_head tmp;
354 	u32 completions;
355 
356 	might_sleep();
357 
358 	list_replace_init(&ioend->io_list, &tmp);
359 	completions = iomap_finish_ioend(ioend, error);
360 
361 	while (!list_empty(&tmp)) {
362 		if (completions > IOEND_BATCH_SIZE * 8) {
363 			cond_resched();
364 			completions = 0;
365 		}
366 		ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
367 		list_del_init(&ioend->io_list);
368 		completions += iomap_finish_ioend(ioend, error);
369 	}
370 }
371 EXPORT_SYMBOL_GPL(iomap_finish_ioends);
372 
373 /*
374  * We can merge two adjacent ioends if they have the same set of work to do.
375  */
376 static bool iomap_ioend_can_merge(struct iomap_ioend *ioend,
377 		struct iomap_ioend *next)
378 {
379 	/*
380 	 * There is no point in merging reads as there is no completion
381 	 * processing that can be easily batched up for them.
382 	 */
383 	if (bio_op(&ioend->io_bio) == REQ_OP_READ ||
384 	    bio_op(&next->io_bio) == REQ_OP_READ)
385 		return false;
386 
387 	if (ioend->io_bio.bi_status != next->io_bio.bi_status)
388 		return false;
389 	if (ioend->io_private != next->io_private)
390 		return false;
391 	if (next->io_flags & IOMAP_IOEND_BOUNDARY)
392 		return false;
393 	if ((ioend->io_flags & IOMAP_IOEND_NOMERGE_FLAGS) !=
394 	    (next->io_flags & IOMAP_IOEND_NOMERGE_FLAGS))
395 		return false;
396 	if (ioend->io_offset + ioend->io_size != next->io_offset)
397 		return false;
398 	/*
399 	 * Do not merge physically discontiguous ioends. The filesystem
400 	 * completion functions will have to iterate the physical
401 	 * discontiguities even if we merge the ioends at a logical level, so
402 	 * we don't gain anything by merging physical discontiguities here.
403 	 *
404 	 * We cannot use bio->bi_iter.bi_sector here as it is modified during
405 	 * submission so does not point to the start sector of the bio at
406 	 * completion.
407 	 */
408 	if (ioend->io_sector + (ioend->io_size >> SECTOR_SHIFT) !=
409 	    next->io_sector)
410 		return false;
411 	return true;
412 }
413 
414 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
415 		struct list_head *more_ioends)
416 {
417 	struct iomap_ioend *next;
418 
419 	INIT_LIST_HEAD(&ioend->io_list);
420 
421 	while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
422 			io_list))) {
423 		if (!iomap_ioend_can_merge(ioend, next))
424 			break;
425 		list_move_tail(&next->io_list, &ioend->io_list);
426 		ioend->io_size += next->io_size;
427 	}
428 }
429 EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
430 
431 static int iomap_ioend_compare(void *priv, const struct list_head *a,
432 		const struct list_head *b)
433 {
434 	struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
435 	struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
436 
437 	if (ia->io_offset < ib->io_offset)
438 		return -1;
439 	if (ia->io_offset > ib->io_offset)
440 		return 1;
441 	return 0;
442 }
443 
444 void iomap_sort_ioends(struct list_head *ioend_list)
445 {
446 	list_sort(NULL, ioend_list, iomap_ioend_compare);
447 }
448 EXPORT_SYMBOL_GPL(iomap_sort_ioends);
449 
450 /*
451  * Split up to the first @max_len bytes from @ioend if the ioend covers more
452  * than @max_len bytes.
453  *
454  * If @is_append is set, the split will be based on the hardware limits for
455  * REQ_OP_ZONE_APPEND commands and can be less than @max_len if the hardware
456  * limits don't allow the entire @max_len length.
457  *
458  * The bio embedded into @ioend must be a REQ_OP_WRITE because the block layer
459  * does not allow splitting REQ_OP_ZONE_APPEND bios.  The file systems has to
460  * switch the operation after this call, but before submitting the bio.
461  */
462 struct iomap_ioend *iomap_split_ioend(struct iomap_ioend *ioend,
463 		unsigned int max_len, bool is_append)
464 {
465 	struct bio *bio = &ioend->io_bio;
466 	struct iomap_ioend *split_ioend;
467 	unsigned int nr_segs;
468 	int sector_offset;
469 	struct bio *split;
470 
471 	if (is_append) {
472 		struct queue_limits *lim = bdev_limits(bio->bi_bdev);
473 
474 		max_len = min(max_len,
475 			      lim->max_zone_append_sectors << SECTOR_SHIFT);
476 
477 		sector_offset = bio_split_rw_at(bio, lim, &nr_segs, max_len);
478 		if (unlikely(sector_offset < 0))
479 			return ERR_PTR(sector_offset);
480 		if (!sector_offset)
481 			return NULL;
482 	} else {
483 		if (bio->bi_iter.bi_size <= max_len)
484 			return NULL;
485 		sector_offset = max_len >> SECTOR_SHIFT;
486 	}
487 
488 	/* ensure the split ioend is still block size aligned */
489 	sector_offset = ALIGN_DOWN(sector_offset << SECTOR_SHIFT,
490 			i_blocksize(ioend->io_inode)) >> SECTOR_SHIFT;
491 
492 	split = bio_split(bio, sector_offset, GFP_NOFS,
493 			&iomap_ioend_split_bioset);
494 	if (IS_ERR(split))
495 		return ERR_CAST(split);
496 	split->bi_private = bio->bi_private;
497 	split->bi_end_io = bio->bi_end_io;
498 
499 	split_ioend = iomap_init_ioend(ioend->io_inode, split, ioend->io_offset,
500 			ioend->io_flags);
501 	split_ioend->io_parent = ioend;
502 
503 	atomic_inc(&ioend->io_remaining);
504 	ioend->io_offset += split_ioend->io_size;
505 	ioend->io_size -= split_ioend->io_size;
506 
507 	split_ioend->io_sector = ioend->io_sector;
508 	if (!is_append)
509 		ioend->io_sector += (split_ioend->io_size >> SECTOR_SHIFT);
510 	return split_ioend;
511 }
512 EXPORT_SYMBOL_GPL(iomap_split_ioend);
513 
514 static int __init iomap_ioend_init(void)
515 {
516 	const unsigned int nr_mempool_entries = 4 * (PAGE_SIZE / SECTOR_SIZE);
517 	int error;
518 
519 	error = bioset_init(&iomap_ioend_bioset, nr_mempool_entries,
520 			   offsetof(struct iomap_ioend, io_bio),
521 			   BIOSET_NEED_BVECS);
522 	if (error)
523 		return error;
524 	error = bioset_init(&iomap_ioend_split_bioset, nr_mempool_entries,
525 			   offsetof(struct iomap_ioend, io_bio),
526 			   BIOSET_NEED_BVECS);
527 	if (error)
528 		goto out_exit_ioend_bioset;
529 	return 0;
530 
531 out_exit_ioend_bioset:
532 	bioset_exit(&iomap_ioend_bioset);
533 	return error;
534 }
535 fs_initcall(iomap_ioend_init);
536