xref: /linux/include/linux/iomap.h (revision 9e8fc16dc6b474b9a87843c3d3ee12fc83f2bdcc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef LINUX_IOMAP_H
3 #define LINUX_IOMAP_H 1
4 
5 #include <linux/atomic.h>
6 #include <linux/bitmap.h>
7 #include <linux/blk_types.h>
8 #include <linux/mm.h>
9 #include <linux/types.h>
10 #include <linux/mm_types.h>
11 #include <linux/blkdev.h>
12 #include <linux/folio_batch.h>
13 #include <linux/pagemap.h>
14 
15 struct address_space;
16 struct fiemap_extent_info;
17 struct inode;
18 struct iomap_iter;
19 struct iomap_dio;
20 struct iomap_writepage_ctx;
21 struct iomap_read_folio_ctx;
22 struct iov_iter;
23 struct kiocb;
24 struct page;
25 struct vm_area_struct;
26 struct vm_fault;
27 
28 /*
29  * Types of block ranges for iomap mappings:
30  */
31 #define IOMAP_HOLE	0	/* no blocks allocated, need allocation */
32 #define IOMAP_DELALLOC	1	/* delayed allocation blocks */
33 #define IOMAP_MAPPED	2	/* blocks allocated at @addr */
34 #define IOMAP_UNWRITTEN	3	/* blocks allocated at @addr in unwritten state */
35 #define IOMAP_INLINE	4	/* data inline in the inode */
36 
37 /*
38  * Flags reported by the file system from iomap_begin:
39  *
40  * IOMAP_F_NEW indicates that the blocks have been newly allocated and need
41  * zeroing for areas that no data is copied to.
42  *
43  * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
44  * written data and requires fdatasync to commit them to persistent storage.
45  * This needs to take into account metadata changes that *may* be made at IO
46  * completion, such as file size updates from direct IO.
47  *
48  * IOMAP_F_SHARED indicates that the blocks are shared, and will need to be
49  * unshared as part a write.
50  *
51  * IOMAP_F_MERGED indicates that the iomap contains the merge of multiple block
52  * mappings.
53  *
54  * IOMAP_F_BUFFER_HEAD indicates that the file system requires the use of
55  * buffer heads for this mapping.
56  *
57  * IOMAP_F_XATTR indicates that the iomap is for an extended attribute extent
58  * rather than a file data extent.
59  *
60  * IOMAP_F_BOUNDARY indicates that I/O and I/O completions for this iomap must
61  * never be merged with the mapping before it.
62  *
63  * IOMAP_F_ANON_WRITE indicates that (write) I/O does not have a target block
64  * assigned to it yet and the file system will do that in the bio submission
65  * handler, splitting the I/O as needed.
66  *
67  * IOMAP_F_ATOMIC_BIO indicates that (write) I/O will be issued as an atomic
68  * bio, i.e. set REQ_ATOMIC.
69  *
70  * IOMAP_F_INTEGRITY indicates that the filesystems handles integrity metadata.
71  *
72  * IOMAP_F_ZERO_TAIL indicates the remainder of the block after the data
73  * written should be zeroed.
74  */
75 #define IOMAP_F_NEW		(1U << 0)
76 #define IOMAP_F_DIRTY		(1U << 1)
77 #define IOMAP_F_SHARED		(1U << 2)
78 #define IOMAP_F_MERGED		(1U << 3)
79 #ifdef CONFIG_BUFFER_HEAD
80 #define IOMAP_F_BUFFER_HEAD	(1U << 4)
81 #else
82 #define IOMAP_F_BUFFER_HEAD	0
83 #endif /* CONFIG_BUFFER_HEAD */
84 #define IOMAP_F_XATTR		(1U << 5)
85 #define IOMAP_F_BOUNDARY	(1U << 6)
86 #define IOMAP_F_ANON_WRITE	(1U << 7)
87 #define IOMAP_F_ATOMIC_BIO	(1U << 8)
88 #ifdef CONFIG_BLK_DEV_INTEGRITY
89 #define IOMAP_F_INTEGRITY	(1U << 9)
90 #else
91 #define IOMAP_F_INTEGRITY	0
92 #endif /* CONFIG_BLK_DEV_INTEGRITY */
93 #define IOMAP_F_ZERO_TAIL	(1U << 10)
94 
95 /*
96  * Indicates reads and writes of fsverity metadata.
97  *
98  * Fsverity metadata is stored after the regular file data and thus beyond
99  * i_size.
100  */
101 #define IOMAP_F_FSVERITY	(1U << 11)
102 
103 /*
104  * Flag reserved for file system specific usage
105  */
106 #define IOMAP_F_PRIVATE		(1U << 12)
107 
108 /*
109  * Flags set by the core iomap code during operations:
110  *
111  * IOMAP_F_FOLIO_BATCH indicates that the folio batch mechanism is active
112  * for this operation, set by iomap_fill_dirty_folios().
113  *
114  * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
115  * has changed as the result of this write operation.
116  *
117  * IOMAP_F_STALE indicates that the iomap is not valid any longer and the file
118  * range it covers needs to be remapped by the high level before the operation
119  * can proceed.
120  */
121 #define IOMAP_F_FOLIO_BATCH	(1U << 13)
122 #define IOMAP_F_SIZE_CHANGED	(1U << 14)
123 #define IOMAP_F_STALE		(1U << 15)
124 
125 /*
126  * Magic value for addr:
127  */
128 #define IOMAP_NULL_ADDR -1ULL	/* addr is not valid */
129 
130 struct iomap {
131 	u64			addr; /* disk offset of mapping, bytes */
132 	loff_t			offset;	/* file offset of mapping, bytes */
133 	u64			length;	/* length of mapping, bytes */
134 	u16			type;	/* type of mapping */
135 	u16			flags;	/* flags for mapping */
136 	struct block_device	*bdev;	/* block device for I/O */
137 	struct dax_device	*dax_dev; /* dax_dev for dax operations */
138 	void			*inline_data;
139 	void			*private; /* filesystem private */
140 	u64			validity_cookie; /* used with .iomap_valid() */
141 };
142 
143 static inline sector_t iomap_sector(const struct iomap *iomap, loff_t pos)
144 {
145 	if (iomap->flags & IOMAP_F_ANON_WRITE)
146 		return U64_MAX; /* invalid */
147 	return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
148 }
149 
150 /*
151  * Returns the inline data pointer for logical offset @pos.
152  */
153 static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
154 {
155 	return iomap->inline_data + pos - iomap->offset;
156 }
157 
158 /*
159  * When get_folio succeeds, put_folio will always be called to do any
160  * cleanup work necessary.  put_folio is responsible for unlocking and putting
161  * @folio.
162  */
163 struct iomap_write_ops {
164 	struct folio *(*get_folio)(struct iomap_iter *iter, loff_t pos,
165 			unsigned len);
166 	void (*put_folio)(struct inode *inode, loff_t pos, unsigned copied,
167 			struct folio *folio);
168 
169 	/*
170 	 * Check that the cached iomap still maps correctly to the filesystem's
171 	 * internal extent map. FS internal extent maps can change while iomap
172 	 * is iterating a cached iomap, so this hook allows iomap to detect that
173 	 * the iomap needs to be refreshed during a long running write
174 	 * operation.
175 	 *
176 	 * The filesystem can store internal state (e.g. a sequence number) in
177 	 * iomap->validity_cookie when the iomap is first mapped to be able to
178 	 * detect changes between mapping time and whenever .iomap_valid() is
179 	 * called.
180 	 *
181 	 * This is called with the folio over the specified file position held
182 	 * locked by the iomap code.
183 	 */
184 	bool (*iomap_valid)(struct inode *inode, const struct iomap *iomap);
185 
186 	/*
187 	 * Optional if the filesystem wishes to provide a custom handler for
188 	 * reading in the contents of a folio, otherwise iomap will default to
189 	 * submitting a bio read request.
190 	 *
191 	 * The read must be done synchronously.
192 	 */
193 	int (*read_folio_range)(const struct iomap_iter *iter,
194 			struct folio *folio, loff_t pos, size_t len);
195 };
196 
197 /*
198  * Flags for iomap_next.  No flag implies a read.
199  */
200 #define IOMAP_WRITE		(1 << 0) /* writing, must allocate blocks */
201 #define IOMAP_ZERO		(1 << 1) /* zeroing operation, may skip holes */
202 #define IOMAP_REPORT		(1 << 2) /* report extent status, e.g. FIEMAP */
203 #define IOMAP_FAULT		(1 << 3) /* mapping for page fault */
204 #define IOMAP_DIRECT		(1 << 4) /* direct I/O */
205 #define IOMAP_NOWAIT		(1 << 5) /* do not block */
206 #define IOMAP_OVERWRITE_ONLY	(1 << 6) /* only pure overwrites allowed */
207 #define IOMAP_UNSHARE		(1 << 7) /* unshare_file_range */
208 #ifdef CONFIG_FS_DAX
209 #define IOMAP_DAX		(1 << 8) /* DAX mapping */
210 #else
211 #define IOMAP_DAX		0
212 #endif /* CONFIG_FS_DAX */
213 #define IOMAP_ATOMIC		(1 << 9) /* torn-write protection */
214 #define IOMAP_DONTCACHE		(1 << 10)
215 
216 /*
217  * Return the existing mapping at pos, or reserve space starting at pos for up
218  * to length, as long as we can do it as a single mapping.
219  * The actual length is returned in iomap->length.
220  */
221 typedef int (*iomap_iter_begin_fn)(struct inode *inode, loff_t pos,
222 		loff_t length, unsigned flags, struct iomap *iomap,
223 		struct iomap *srcmap);
224 
225 /*
226  * Commit and/or unreserve space previously allocated by iomap_iter_begin_fn.
227  * Written indicates the length of the successful write operation which needs
228  * to be committed, while the rest needs to be unreserved.
229  * Written might be zero if no data was written.
230  */
231 typedef int (*iomap_iter_end_fn)(struct inode *inode, loff_t pos, loff_t length,
232 		ssize_t written, unsigned flags, struct iomap *iomap);
233 
234 /*
235  * Produce the next mapping (finishing the previous one if needed).
236  * Return 1 to continue iterating, 0 if the range is fully consumed, or a
237  * negative error on failure.
238  */
239 typedef int (*iomap_iter_next_fn)(const struct iomap_iter *iter,
240 		struct iomap *iomap, struct iomap *srcmap);
241 
242 /**
243  * struct iomap_iter - Iterate through a range of a file
244  * @inode: Set at the start of the iteration and should not change.
245  * @pos: The current file position we are operating on.  It is updated by
246  *	calls to iomap_iter().  Treat as read-only in the body.
247  * @len: The remaining length of the file segment we're operating on.
248  *	It is updated at the same time as @pos.
249  * @iter_start_pos: The original start pos for the current iomap. Used for
250  *	incremental iter advance.
251  * @status: Status of the most recent iteration. Zero on success or a negative
252  *	errno on error.
253  * @flags: Zero or more of the iomap_iter_next flags above.
254  * @iomap: Map describing the I/O iteration
255  * @srcmap: Source map for COW operations
256  */
257 struct iomap_iter {
258 	struct inode *inode;
259 	loff_t pos;
260 	u64 len;
261 	loff_t iter_start_pos;
262 	int status;
263 	unsigned flags;
264 	struct iomap iomap;
265 	struct iomap srcmap;
266 	struct folio_batch *fbatch;
267 	void *private;
268 };
269 
270 int iomap_iter(struct iomap_iter *iter, iomap_iter_next_fn next);
271 int iomap_iter_advance(struct iomap_iter *iter, u64 count);
272 
273 /**
274  * iomap_length_trim - trimmed length of the current iomap iteration
275  * @iter: iteration structure
276  * @pos: File position to trim from.
277  * @len: Length of the mapping to trim to.
278  *
279  * Returns a trimmed length that the operation applies to for the current
280  * iteration.
281  */
282 static inline u64 iomap_length_trim(const struct iomap_iter *iter, loff_t pos,
283 		u64 len)
284 {
285 	u64 end = iter->iomap.offset + iter->iomap.length;
286 
287 	if (iter->srcmap.type != IOMAP_HOLE)
288 		end = min(end, iter->srcmap.offset + iter->srcmap.length);
289 	return min(len, end - pos);
290 }
291 
292 /**
293  * iomap_length - length of the current iomap iteration
294  * @iter: iteration structure
295  *
296  * Returns the length that the operation applies to for the current iteration.
297  */
298 static inline u64 iomap_length(const struct iomap_iter *iter)
299 {
300 	return iomap_length_trim(iter, iter->pos, iter->len);
301 }
302 
303 /**
304  * iomap_iter_advance_full - advance by the full length of current map
305  */
306 static inline int iomap_iter_advance_full(struct iomap_iter *iter)
307 {
308 	return iomap_iter_advance(iter, iomap_length(iter));
309 }
310 
311 /**
312  * iomap_iter_srcmap - return the source map for the current iomap iteration
313  * @i: iteration structure
314  *
315  * Write operations on file systems with reflink support might require a
316  * source and a destination map.  This function retourns the source map
317  * for a given operation, which may or may no be identical to the destination
318  * map in &i->iomap.
319  */
320 static inline const struct iomap *iomap_iter_srcmap(const struct iomap_iter *i)
321 {
322 	if (i->srcmap.type != IOMAP_HOLE)
323 		return &i->srcmap;
324 	return &i->iomap;
325 }
326 
327 int iomap_iter_continue(const struct iomap_iter *iter, struct iomap *iomap,
328 		struct iomap *srcmap, int ret);
329 
330 /**
331  * iomap_iter_next - finish the previous mapping and produce the next one
332  * @iter: iteration structure
333  * @iomap: mapping to finish and then repopulate
334  * @srcmap: source mapping to finish and then repopulate
335  * @begin: callback that produces a mapping for the current position
336  * @end: optional callback that finishes the previous mapping, or NULL
337  *
338  * Inline helper that implements the common body of an iomap_iter_next_fn
339  * callback: it finishes the previous mapping via @end (if present), decides
340  * via iomap_iter_continue() whether to keep going, and obtains the next
341  * mapping via @begin.
342  *
343  * This helper is marked __always_inline so that when a caller passes
344  * compile-time-constant @begin and @end callbacks, the compiler can call them
345  * directly, avoiding the indirect-call overhead.
346  *
347  * Returns 1 to continue iterating, 0 once the range is fully consumed, or a
348  * negative errno on error.
349  */
350 static __always_inline int iomap_iter_next(const struct iomap_iter *iter,
351 		struct iomap *iomap, struct iomap *srcmap,
352 		iomap_iter_begin_fn begin, iomap_iter_end_fn end)
353 {
354 	int ret = 0;
355 
356 	if (iomap->length) {
357 		if (end) {
358 			/*
359 			 * Calculate how far the iter was advanced and the
360 			 * original length bytes for end().
361 			 */
362 			ssize_t advanced = iter->pos - iter->iter_start_pos;
363 			loff_t len;
364 
365 			len = iomap_length_trim(iter, iter->iter_start_pos,
366 					iter->len + advanced);
367 
368 			ret = end(iter->inode, iter->iter_start_pos, len,
369 					advanced, iter->flags, iomap);
370 		}
371 		ret = iomap_iter_continue(iter, iomap, srcmap, ret);
372 		if (ret <= 0)
373 			return ret;
374 	}
375 
376 	ret = begin(iter->inode, iter->pos, iter->len, iter->flags, iomap,
377 			srcmap);
378 
379 	return ret < 0 ? ret : 1;
380 }
381 
382 #define DEFINE_IOMAP_ITER_NEXT_END(name, begin_fn, end_fn)		\
383 int name(const struct iomap_iter *iter, struct iomap *iomap,		\
384 		struct iomap *srcmap)					\
385 {									\
386 	return iomap_iter_next(iter, iomap, srcmap, begin_fn, end_fn);	\
387 }
388 
389 #define DEFINE_IOMAP_ITER_NEXT(name, begin_fn)				\
390 	DEFINE_IOMAP_ITER_NEXT_END(name, begin_fn, NULL)
391 
392 /*
393  * Forward-declare an iomap_iter_next_fn callback generated by
394  * DEFINE_IOMAP_ITER_NEXT().
395  */
396 #define DECLARE_IOMAP_ITER_NEXT(name)					\
397 int name(const struct iomap_iter *iter, struct iomap *iomap,		\
398 		struct iomap *srcmap)
399 
400 /*
401  * Return the file offset for the first unchanged block after a short write.
402  *
403  * If nothing was written, round @pos down to point at the first block in
404  * the range, else round up to include the partially written block.
405  */
406 static inline loff_t iomap_last_written_block(struct inode *inode, loff_t pos,
407 		ssize_t written)
408 {
409 	if (unlikely(!written))
410 		return round_down(pos, i_blocksize(inode));
411 	return round_up(pos + written, i_blocksize(inode));
412 }
413 
414 /*
415  * Check if the range needs to be unshared for a FALLOC_FL_UNSHARE_RANGE
416  * operation.
417  *
418  * Don't bother with blocks that are not shared to start with; or mappings that
419  * cannot be shared, such as inline data, delalloc reservations, holes or
420  * unwritten extents.
421  *
422  * Note that we use srcmap directly instead of iomap_iter_srcmap as unsharing
423  * requires providing a separate source map, and the presence of one is a good
424  * indicator that unsharing is needed, unlike IOMAP_F_SHARED which can be set
425  * for any data that goes into the COW fork for XFS.
426  */
427 static inline bool iomap_want_unshare_iter(const struct iomap_iter *iter)
428 {
429 	return (iter->iomap.flags & IOMAP_F_SHARED) &&
430 		iter->srcmap.type == IOMAP_MAPPED;
431 }
432 
433 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
434 		iomap_iter_next_fn next,
435 		const struct iomap_write_ops *write_ops, void *private);
436 int iomap_fsverity_write(struct file *file, loff_t pos, size_t length,
437 		const void *buf, iomap_iter_next_fn next,
438 		const struct iomap_write_ops *write_ops);
439 void iomap_read_folio(iomap_iter_next_fn next, struct iomap_read_folio_ctx *ctx,
440 		void *private);
441 void iomap_readahead(iomap_iter_next_fn next, struct iomap_read_folio_ctx *ctx,
442 		void *private);
443 bool iomap_is_partially_uptodate(struct folio *, size_t from, size_t count);
444 struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len);
445 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
446 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
447 bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio);
448 void iomap_folio_mark_uptodate(struct folio *folio);
449 int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
450 		iomap_iter_next_fn next,
451 		const struct iomap_write_ops *write_ops);
452 unsigned int iomap_fill_dirty_folios(struct iomap_iter *iter, loff_t *start,
453 		loff_t end, unsigned int *iomap_flags);
454 int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
455 		bool *did_zero, iomap_iter_next_fn next,
456 		const struct iomap_write_ops *write_ops, void *private);
457 int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
458 		iomap_iter_next_fn next,
459 		const struct iomap_write_ops *write_ops, void *private);
460 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, iomap_iter_next_fn next,
461 		void *private);
462 typedef void (*iomap_punch_t)(struct inode *inode, loff_t offset, loff_t length,
463 		struct iomap *iomap);
464 void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte,
465 		loff_t end_byte, unsigned flags, struct iomap *iomap,
466 		iomap_punch_t punch);
467 
468 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
469 		u64 start, u64 len, iomap_iter_next_fn next);
470 loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
471 		iomap_iter_next_fn next);
472 loff_t iomap_seek_data(struct inode *inode, loff_t offset,
473 		iomap_iter_next_fn next);
474 sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
475 		iomap_iter_next_fn next);
476 
477 /*
478  * Flags for iomap_ioend->io_flags.
479  */
480 /* shared COW extent */
481 #define IOMAP_IOEND_SHARED		(1U << 0)
482 /* unwritten extent */
483 #define IOMAP_IOEND_UNWRITTEN		(1U << 1)
484 /* don't merge into previous ioend */
485 #define IOMAP_IOEND_BOUNDARY		(1U << 2)
486 /* is direct I/O */
487 #define IOMAP_IOEND_DIRECT		(1U << 3)
488 /* generate integrity (PI) information */
489 #ifdef CONFIG_BLK_DEV_INTEGRITY
490 #define IOMAP_IOEND_INTEGRITY		(1U << 4)
491 #else
492 #define IOMAP_IOEND_INTEGRITY		0
493 #endif /* CONFIG_BLK_DEV_INTEGRITY */
494 
495 /*
496  * Flags that if set on either ioend prevent the merge of two ioends.
497  * (IOMAP_IOEND_BOUNDARY also prevents merges, but only one-way)
498  */
499 #define IOMAP_IOEND_NOMERGE_FLAGS \
500 	(IOMAP_IOEND_SHARED | IOMAP_IOEND_UNWRITTEN | IOMAP_IOEND_DIRECT | \
501 	 IOMAP_IOEND_INTEGRITY)
502 
503 /* ioend flags directly implied by iomap flags */
504 static inline u16 iomap_ioend_flags(const struct iomap *iomap)
505 {
506 	unsigned int flags = 0;
507 
508 	if (iomap->type == IOMAP_UNWRITTEN)
509 		flags |= IOMAP_IOEND_UNWRITTEN;
510 	if (iomap->flags & IOMAP_F_SHARED)
511 		flags |= IOMAP_IOEND_SHARED;
512 	if (iomap->flags & IOMAP_F_INTEGRITY)
513 		flags |= IOMAP_IOEND_INTEGRITY;
514 
515 	return flags;
516 }
517 
518 /*
519  * Structure for writeback I/O completions.
520  *
521  * File systems can split a bio generated by iomap.  In that case the parent
522  * ioend it was split from is recorded in ioend->io_parent.
523  */
524 struct iomap_ioend {
525 	struct list_head	io_list;	/* next ioend in chain */
526 	u16			io_flags;	/* IOMAP_IOEND_* */
527 	u32			io_bvec_offset;	/* offset into first bvec */
528 	struct inode		*io_inode;	/* file being written to */
529 	size_t			io_size;	/* size of the extent */
530 	atomic_t		io_remaining;	/* completetion defer count */
531 	int			io_error;	/* stashed away status */
532 	struct iomap_ioend	*io_parent;	/* parent for completions */
533 	loff_t			io_offset;	/* offset in the file */
534 	sector_t		io_sector;	/* start sector of ioend */
535 	void			*io_private;	/* file system private data */
536 	struct fsverity_info	*io_vi;		/* fsverity info */
537 	struct bio		io_bio;		/* MUST BE LAST! */
538 };
539 
540 static inline struct iomap_ioend *iomap_ioend_from_bio(struct bio *bio)
541 {
542 	return container_of(bio, struct iomap_ioend, io_bio);
543 }
544 
545 #define BVEC_ITER_IOEND(_ioend)				\
546 {							\
547 	.bi_sector	= (_ioend)->io_sector,		\
548 	.bi_size	= (_ioend)->io_size,		\
549 	.bi_offset	= (_ioend)->io_bvec_offset,	\
550 }
551 
552 struct iomap_writeback_ops {
553 	/*
554 	 * Performs writeback on the passed in range
555 	 *
556 	 * Can map arbitrarily large regions, but we need to call into it at
557 	 * least once per folio to allow the file systems to synchronize with
558 	 * the write path that could be invalidating mappings.
559 	 *
560 	 * An existing mapping from a previous call to this method can be reused
561 	 * by the file system if it is still valid.
562 	 *
563 	 * If this succeeds, iomap_finish_folio_write() must be called once
564 	 * writeback completes for the range, regardless of whether the
565 	 * writeback succeeded or failed.
566 	 *
567 	 * Returns the number of bytes processed or a negative errno.
568 	 */
569 	ssize_t (*writeback_range)(struct iomap_writepage_ctx *wpc,
570 			struct folio *folio, u64 pos, unsigned int len,
571 			u64 end_pos);
572 
573 	/*
574 	 * Submit a writeback context previously build up by ->writeback_range.
575 	 *
576 	 * Returns 0 if the context was successfully submitted, or a negative
577 	 * error code if not.  If @error is non-zero a failure occurred, and
578 	 * the writeback context should be completed with an error.
579 	 */
580 	int (*writeback_submit)(struct iomap_writepage_ctx *wpc, int error);
581 };
582 
583 struct iomap_writepage_ctx {
584 	struct iomap		iomap;
585 	struct inode		*inode;
586 	struct writeback_control *wbc;
587 	const struct iomap_writeback_ops *ops;
588 	u32			nr_folios;	/* folios added to the ioend */
589 	void			*wb_ctx;	/* pending writeback context */
590 };
591 
592 struct iomap_ioend *iomap_init_ioend(struct inode *inode, struct bio *bio,
593 		loff_t file_offset, u16 ioend_flags);
594 struct iomap_ioend *iomap_split_ioend(struct iomap_ioend *ioend,
595 		unsigned int max_len, bool is_append);
596 void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
597 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
598 		struct list_head *more_ioends);
599 void iomap_sort_ioends(struct list_head *ioend_list);
600 int iomap_ioend_integrity_verify(struct iomap_ioend *ioend);
601 ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,
602 		loff_t pos, loff_t end_pos, unsigned int dirty_len);
603 int iomap_ioend_writeback_submit(struct iomap_writepage_ctx *wpc, int error);
604 
605 void iomap_finish_folio_read(struct folio *folio, size_t off, size_t len,
606 		int error);
607 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
608 		size_t len);
609 
610 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio);
611 int iomap_writepages(struct iomap_writepage_ctx *wpc);
612 
613 void iomap_bounce_read(struct iomap_ioend *orig_ioend, unsigned int minsize,
614 		void (*submit_ioend)(struct iomap_ioend *ioend));
615 void iomap_bounce_read_end_io(struct iomap_ioend *ioend, struct bio *orig_bio,
616 		int error);
617 
618 struct iomap_read_folio_ctx {
619 	const struct iomap_read_ops *ops;
620 	struct folio		*cur_folio;
621 	struct readahead_control *rac;
622 	void			*read_ctx;
623 	loff_t			read_ctx_file_offset;
624 	struct fsverity_info	*vi;
625 };
626 
627 struct iomap_read_ops {
628 	/*
629 	 * Read in a folio range.
630 	 *
631 	 * If this succeeds, iomap_finish_folio_read() must be called after the
632 	 * range is read in, regardless of whether the read succeeded or failed.
633 	 *
634 	 * Returns 0 on success or a negative error on failure.
635 	 */
636 	int (*read_folio_range)(const struct iomap_iter *iter,
637 			struct iomap_read_folio_ctx *ctx, size_t len);
638 
639 	/*
640 	 * Submit any pending read requests.
641 	 *
642 	 * This is optional.
643 	 */
644 	void (*submit_read)(const struct iomap_iter *iter,
645 			struct iomap_read_folio_ctx *ctx);
646 
647 	/*
648 	 * Optional, allows filesystem to specify own bio_set, so new bio's
649 	 * can be allocated from the provided bio_set.
650 	 */
651 	struct bio_set *bio_set;
652 };
653 
654 /*
655  * Flags for direct I/O ->end_io:
656  */
657 #define IOMAP_DIO_UNWRITTEN	(1 << 0)	/* covers unwritten extent(s) */
658 #define IOMAP_DIO_COW		(1 << 1)	/* covers COW extent(s) */
659 
660 struct iomap_dio_ops {
661 	int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
662 		      unsigned flags);
663 	void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
664 		          loff_t file_offset);
665 
666 	/*
667 	 * Filesystems wishing to attach private information to a direct io bio
668 	 * must provide a ->submit_io method that attaches the additional
669 	 * information to the bio and changes the ->bi_end_io callback to a
670 	 * custom function.  This function should, at a minimum, perform any
671 	 * relevant post-processing of the bio and end with a call to
672 	 * iomap_dio_bio_end_io.
673 	 */
674 	struct bio_set *bio_set;
675 };
676 
677 /*
678  * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
679  * synchronous.
680  */
681 #define IOMAP_DIO_FORCE_WAIT	(1 << 0)
682 
683 /*
684  * Do not allocate blocks or zero partial blocks, but instead fall back to
685  * the caller by returning -EAGAIN.  Used to optimize direct I/O writes that
686  * are not aligned to the file system block size.
687   */
688 #define IOMAP_DIO_OVERWRITE_ONLY	(1 << 1)
689 
690 /*
691  * When a page fault occurs, return a partial synchronous result and allow
692  * the caller to retry the rest of the operation after dealing with the page
693  * fault.
694  */
695 #define IOMAP_DIO_PARTIAL		(1 << 2)
696 
697 /*
698  * Ensure every operation, including bio and hole zeroing, is aligned to fs
699  * block size.
700  *
701  * For filesystems which need to calculate/verify the checksum of each fs
702  * block. Otherwise they may not be able to handle unaligned bios.
703  */
704 #define IOMAP_DIO_FSBLOCK_ALIGNED	(1 << 3)
705 
706 /*
707  * Bounce buffer instead of using zero copy access.
708  *
709  * This is needed if the device needs stable data to checksum or generate
710  * parity.  The file system must hook into the I/O submission and offload
711  * completions to user context for reads when this is set.
712  */
713 #define IOMAP_DIO_BOUNCE		(1 << 4)
714 
715 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
716 		iomap_iter_next_fn next, const struct iomap_dio_ops *dops,
717 		unsigned int dio_flags, void *private, size_t done_before);
718 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
719 		iomap_iter_next_fn next, const struct iomap_dio_ops *dops,
720 		unsigned int dio_flags, void *private, size_t done_before);
721 ssize_t iomap_dio_complete(struct iomap_dio *dio);
722 void iomap_dio_bio_end_io(struct bio *bio);
723 
724 /*
725  * Fast path for small, block-aligned direct I/Os that map to a single
726  * contiguous on-disk extent.
727  *
728  * @iter must describe a non-empty READ no larger than the inode block size:
729  * writes, zero-length I/O, and larger requests need the generic iomap direct
730  * I/O path.
731  *
732  * Does not support iomap_dio_ops, dio_flags, done_before or private data.
733  * The range must also stay within i_size and encrypted inodes must use the
734  * generic iomap direct I/O path.
735  *
736  * -ENOTBLK indicates the generic path must be used by the caller instead.
737  * Any other errno is a real result and is propagated as-is, in particular
738  * -EAGAIN for IOCB_NOWAIT must reach the caller.
739  *
740  * The caller can only provide an iomap begin handler, and the iterator
741  * is never advanced.
742  */
743 ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter,
744 		struct iomap_iter *iomi);
745 static __always_inline ssize_t iomap_dio_read_simple(struct kiocb *iocb,
746 		struct iov_iter *iter, iomap_iter_begin_fn begin)
747 {
748 	struct iomap_iter iomi = {
749 		.inode		= file_inode(iocb->ki_filp),
750 		.pos		= iocb->ki_pos,
751 		.len		= iov_iter_count(iter),
752 		.flags		= IOMAP_DIRECT,
753 	};
754 	ssize_t ret;
755 
756 	if (!iomi.len)
757 		return 0;
758 
759 	/*
760 	 * Simple dio is an optimization for small IO. Filter out large IO
761 	 * early as it's the most common case to fail for typical direct IO
762 	 * workloads.
763 	 */
764 	if (iomi.len > iomi.inode->i_sb->s_blocksize)
765 		return -ENOTBLK;
766 	if (iocb->ki_pos + iomi.len > i_size_read(iomi.inode))
767 		return -ENOTBLK;
768 	if (IS_ENCRYPTED(iomi.inode))
769 		return -ENOTBLK;
770 
771 	ret = kiocb_write_and_wait(iocb, iomi.len);
772 	if (ret)
773 		return ret;
774 
775 	if (iocb->ki_flags & IOCB_NOWAIT)
776 		iomi.flags |= IOMAP_NOWAIT;
777 
778 	inode_dio_begin(iomi.inode);
779 	ret = begin(iomi.inode, iomi.pos, iomi.len, iomi.flags, &iomi.iomap,
780 			&iomi.srcmap);
781 	if (ret) {
782 		inode_dio_end(iomi.inode);
783 		return ret;
784 	}
785 
786 	return __iomap_dio_read_simple(iocb, iter, &iomi);
787 }
788 
789 #ifdef CONFIG_SWAP
790 struct file;
791 struct swap_info_struct;
792 
793 int iomap_swapfile_activate(struct swap_info_struct *sis,
794 		struct file *swap_file, sector_t *pagespan,
795 		iomap_iter_next_fn next);
796 #else
797 # define iomap_swapfile_activate(sis, swapfile, pagespan, next) (-EIO)
798 #endif /* CONFIG_SWAP */
799 
800 extern struct bio_set iomap_ioend_bioset;
801 
802 #ifdef CONFIG_BLOCK
803 int iomap_bio_read_folio_range(const struct iomap_iter *iter,
804 		struct iomap_read_folio_ctx *ctx, size_t plen);
805 void iomap_bio_submit_read_endio(const struct iomap_iter *iter,
806 		struct iomap_read_folio_ctx *ctx, bio_end_io_t end_io);
807 
808 extern const struct iomap_read_ops iomap_bio_read_ops;
809 
810 static inline void iomap_bio_read_folio(struct folio *folio,
811 		iomap_iter_next_fn next)
812 {
813 	struct iomap_read_folio_ctx ctx = {
814 		.ops		= &iomap_bio_read_ops,
815 		.cur_folio	= folio,
816 	};
817 
818 	iomap_read_folio(next, &ctx, NULL);
819 }
820 
821 static inline void iomap_bio_readahead(struct readahead_control *rac,
822 		iomap_iter_next_fn next)
823 {
824 	struct iomap_read_folio_ctx ctx = {
825 		.ops		= &iomap_bio_read_ops,
826 		.rac		= rac,
827 	};
828 
829 	iomap_readahead(next, &ctx, NULL);
830 }
831 #endif /* CONFIG_BLOCK */
832 
833 #endif /* LINUX_IOMAP_H */
834