xref: /linux/include/linux/iomap.h (revision 27dd2772e6c62cf0627d97fe03950710df334cf4)
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 
iomap_sector(const struct iomap * iomap,loff_t pos)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  */
iomap_inline_data(const struct iomap * iomap,loff_t pos)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_begin / iomap_end.  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 struct iomap_ops {
243 	iomap_iter_begin_fn iomap_begin;
244 	iomap_iter_end_fn iomap_end;
245 	iomap_iter_next_fn iomap_next;
246 };
247 
248 /**
249  * struct iomap_iter - Iterate through a range of a file
250  * @inode: Set at the start of the iteration and should not change.
251  * @pos: The current file position we are operating on.  It is updated by
252  *	calls to iomap_iter().  Treat as read-only in the body.
253  * @len: The remaining length of the file segment we're operating on.
254  *	It is updated at the same time as @pos.
255  * @iter_start_pos: The original start pos for the current iomap. Used for
256  *	incremental iter advance.
257  * @status: Status of the most recent iteration. Zero on success or a negative
258  *	errno on error.
259  * @flags: Zero or more of the iomap_begin flags above.
260  * @iomap: Map describing the I/O iteration
261  * @srcmap: Source map for COW operations
262  */
263 struct iomap_iter {
264 	struct inode *inode;
265 	loff_t pos;
266 	u64 len;
267 	loff_t iter_start_pos;
268 	int status;
269 	unsigned flags;
270 	struct iomap iomap;
271 	struct iomap srcmap;
272 	struct folio_batch *fbatch;
273 	void *private;
274 };
275 
276 int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops);
277 int iomap_iter_advance(struct iomap_iter *iter, u64 count);
278 
279 /**
280  * iomap_length_trim - trimmed length of the current iomap iteration
281  * @iter: iteration structure
282  * @pos: File position to trim from.
283  * @len: Length of the mapping to trim to.
284  *
285  * Returns a trimmed length that the operation applies to for the current
286  * iteration.
287  */
iomap_length_trim(const struct iomap_iter * iter,loff_t pos,u64 len)288 static inline u64 iomap_length_trim(const struct iomap_iter *iter, loff_t pos,
289 		u64 len)
290 {
291 	u64 end = iter->iomap.offset + iter->iomap.length;
292 
293 	if (iter->srcmap.type != IOMAP_HOLE)
294 		end = min(end, iter->srcmap.offset + iter->srcmap.length);
295 	return min(len, end - pos);
296 }
297 
298 /**
299  * iomap_length - length of the current iomap iteration
300  * @iter: iteration structure
301  *
302  * Returns the length that the operation applies to for the current iteration.
303  */
iomap_length(const struct iomap_iter * iter)304 static inline u64 iomap_length(const struct iomap_iter *iter)
305 {
306 	return iomap_length_trim(iter, iter->pos, iter->len);
307 }
308 
309 /**
310  * iomap_iter_advance_full - advance by the full length of current map
311  */
iomap_iter_advance_full(struct iomap_iter * iter)312 static inline int iomap_iter_advance_full(struct iomap_iter *iter)
313 {
314 	return iomap_iter_advance(iter, iomap_length(iter));
315 }
316 
317 /**
318  * iomap_iter_srcmap - return the source map for the current iomap iteration
319  * @i: iteration structure
320  *
321  * Write operations on file systems with reflink support might require a
322  * source and a destination map.  This function retourns the source map
323  * for a given operation, which may or may no be identical to the destination
324  * map in &i->iomap.
325  */
iomap_iter_srcmap(const struct iomap_iter * i)326 static inline const struct iomap *iomap_iter_srcmap(const struct iomap_iter *i)
327 {
328 	if (i->srcmap.type != IOMAP_HOLE)
329 		return &i->srcmap;
330 	return &i->iomap;
331 }
332 
333 int iomap_iter_continue(const struct iomap_iter *iter, struct iomap *iomap,
334 		struct iomap *srcmap, int ret);
335 
336 /**
337  * iomap_iter_next - finish the previous mapping and produce the next one
338  * @iter: iteration structure
339  * @iomap: mapping to finish and then repopulate
340  * @srcmap: source mapping to finish and then repopulate
341  * @begin: callback that produces a mapping for the current position
342  * @end: optional callback that finishes the previous mapping, or NULL
343  *
344  * Inline helper that implements the common body of an ->iomap_next()
345  * callback: it finishes the previous mapping via @end (if present), decides
346  * via iomap_iter_continue() whether to keep going, and obtains the next
347  * mapping via @begin.
348  *
349  * This helper is marked __always_inline so that when a caller passes
350  * compile-time-constant @begin and @end callbacks, the compiler can call them
351  * directly, avoiding the indirect-call overhead.
352  *
353  * Returns 1 to continue iterating, 0 once the range is fully consumed, or a
354  * negative errno on error.
355  */
iomap_iter_next(const struct iomap_iter * iter,struct iomap * iomap,struct iomap * srcmap,iomap_iter_begin_fn begin,iomap_iter_end_fn end)356 static __always_inline int iomap_iter_next(const struct iomap_iter *iter,
357 		struct iomap *iomap, struct iomap *srcmap,
358 		iomap_iter_begin_fn begin, iomap_iter_end_fn end)
359 {
360 	int ret = 0;
361 
362 	if (iomap->length) {
363 		if (end) {
364 			/*
365 			 * Calculate how far the iter was advanced and the
366 			 * original length bytes for end().
367 			 */
368 			ssize_t advanced = iter->pos - iter->iter_start_pos;
369 			loff_t len;
370 
371 			len = iomap_length_trim(iter, iter->iter_start_pos,
372 					iter->len + advanced);
373 
374 			ret = end(iter->inode, iter->iter_start_pos, len,
375 					advanced, iter->flags, iomap);
376 		}
377 		ret = iomap_iter_continue(iter, iomap, srcmap, ret);
378 		if (ret <= 0)
379 			return ret;
380 	}
381 
382 	ret = begin(iter->inode, iter->pos, iter->len, iter->flags, iomap,
383 			srcmap);
384 
385 	return ret < 0 ? ret : 1;
386 }
387 
388 #define DEFINE_IOMAP_ITER_NEXT_END(name, begin_fn, end_fn)		\
389 int name(const struct iomap_iter *iter, struct iomap *iomap,		\
390 		struct iomap *srcmap)					\
391 {									\
392 	return iomap_iter_next(iter, iomap, srcmap, begin_fn, end_fn);	\
393 }
394 
395 #define DEFINE_IOMAP_ITER_NEXT(name, begin_fn)				\
396 	DEFINE_IOMAP_ITER_NEXT_END(name, begin_fn, NULL)
397 
398 /*
399  * Return the file offset for the first unchanged block after a short write.
400  *
401  * If nothing was written, round @pos down to point at the first block in
402  * the range, else round up to include the partially written block.
403  */
iomap_last_written_block(struct inode * inode,loff_t pos,ssize_t written)404 static inline loff_t iomap_last_written_block(struct inode *inode, loff_t pos,
405 		ssize_t written)
406 {
407 	if (unlikely(!written))
408 		return round_down(pos, i_blocksize(inode));
409 	return round_up(pos + written, i_blocksize(inode));
410 }
411 
412 /*
413  * Check if the range needs to be unshared for a FALLOC_FL_UNSHARE_RANGE
414  * operation.
415  *
416  * Don't bother with blocks that are not shared to start with; or mappings that
417  * cannot be shared, such as inline data, delalloc reservations, holes or
418  * unwritten extents.
419  *
420  * Note that we use srcmap directly instead of iomap_iter_srcmap as unsharing
421  * requires providing a separate source map, and the presence of one is a good
422  * indicator that unsharing is needed, unlike IOMAP_F_SHARED which can be set
423  * for any data that goes into the COW fork for XFS.
424  */
iomap_want_unshare_iter(const struct iomap_iter * iter)425 static inline bool iomap_want_unshare_iter(const struct iomap_iter *iter)
426 {
427 	return (iter->iomap.flags & IOMAP_F_SHARED) &&
428 		iter->srcmap.type == IOMAP_MAPPED;
429 }
430 
431 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
432 		const struct iomap_ops *ops,
433 		const struct iomap_write_ops *write_ops, void *private);
434 int iomap_fsverity_write(struct file *file, loff_t pos, size_t length,
435 		const void *buf, const struct iomap_ops *ops,
436 		const struct iomap_write_ops *write_ops);
437 void iomap_read_folio(const struct iomap_ops *ops,
438 		struct iomap_read_folio_ctx *ctx, void *private);
439 void iomap_readahead(const struct iomap_ops *ops,
440 		struct iomap_read_folio_ctx *ctx, void *private);
441 bool iomap_is_partially_uptodate(struct folio *, size_t from, size_t count);
442 struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len);
443 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
444 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
445 bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio);
446 void iomap_folio_mark_uptodate(struct folio *folio);
447 int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
448 		const struct iomap_ops *ops,
449 		const struct iomap_write_ops *write_ops);
450 unsigned int iomap_fill_dirty_folios(struct iomap_iter *iter, loff_t *start,
451 		loff_t end, unsigned int *iomap_flags);
452 int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
453 		bool *did_zero, const struct iomap_ops *ops,
454 		const struct iomap_write_ops *write_ops, void *private);
455 int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
456 		const struct iomap_ops *ops,
457 		const struct iomap_write_ops *write_ops, void *private);
458 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops,
459 		void *private);
460 typedef void (*iomap_punch_t)(struct inode *inode, loff_t offset, loff_t length,
461 		struct iomap *iomap);
462 void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte,
463 		loff_t end_byte, unsigned flags, struct iomap *iomap,
464 		iomap_punch_t punch);
465 
466 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
467 		u64 start, u64 len, const struct iomap_ops *ops);
468 loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
469 		const struct iomap_ops *ops);
470 loff_t iomap_seek_data(struct inode *inode, loff_t offset,
471 		const struct iomap_ops *ops);
472 sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
473 		const struct iomap_ops *ops);
474 
475 /*
476  * Flags for iomap_ioend->io_flags.
477  */
478 /* shared COW extent */
479 #define IOMAP_IOEND_SHARED		(1U << 0)
480 /* unwritten extent */
481 #define IOMAP_IOEND_UNWRITTEN		(1U << 1)
482 /* don't merge into previous ioend */
483 #define IOMAP_IOEND_BOUNDARY		(1U << 2)
484 /* is direct I/O */
485 #define IOMAP_IOEND_DIRECT		(1U << 3)
486 /* generate integrity (PI) information */
487 #ifdef CONFIG_BLK_DEV_INTEGRITY
488 #define IOMAP_IOEND_INTEGRITY		(1U << 4)
489 #else
490 #define IOMAP_IOEND_INTEGRITY		0
491 #endif /* CONFIG_BLK_DEV_INTEGRITY */
492 
493 /*
494  * Flags that if set on either ioend prevent the merge of two ioends.
495  * (IOMAP_IOEND_BOUNDARY also prevents merges, but only one-way)
496  */
497 #define IOMAP_IOEND_NOMERGE_FLAGS \
498 	(IOMAP_IOEND_SHARED | IOMAP_IOEND_UNWRITTEN | IOMAP_IOEND_DIRECT | \
499 	 IOMAP_IOEND_INTEGRITY)
500 
501 /* ioend flags directly implied by iomap flags */
iomap_ioend_flags(const struct iomap * iomap)502 static inline u16 iomap_ioend_flags(const struct iomap *iomap)
503 {
504 	unsigned int flags = 0;
505 
506 	if (iomap->type == IOMAP_UNWRITTEN)
507 		flags |= IOMAP_IOEND_UNWRITTEN;
508 	if (iomap->flags & IOMAP_F_SHARED)
509 		flags |= IOMAP_IOEND_SHARED;
510 	if (iomap->flags & IOMAP_F_INTEGRITY)
511 		flags |= IOMAP_IOEND_INTEGRITY;
512 
513 	return flags;
514 }
515 
516 /*
517  * Structure for writeback I/O completions.
518  *
519  * File systems can split a bio generated by iomap.  In that case the parent
520  * ioend it was split from is recorded in ioend->io_parent.
521  */
522 struct iomap_ioend {
523 	struct list_head	io_list;	/* next ioend in chain */
524 	u16			io_flags;	/* IOMAP_IOEND_* */
525 	u32			io_bvec_offset;	/* offset into first bvec */
526 	struct inode		*io_inode;	/* file being written to */
527 	size_t			io_size;	/* size of the extent */
528 	atomic_t		io_remaining;	/* completetion defer count */
529 	int			io_error;	/* stashed away status */
530 	struct iomap_ioend	*io_parent;	/* parent for completions */
531 	loff_t			io_offset;	/* offset in the file */
532 	sector_t		io_sector;	/* start sector of ioend */
533 	void			*io_private;	/* file system private data */
534 	struct fsverity_info	*io_vi;		/* fsverity info */
535 	struct bio		io_bio;		/* MUST BE LAST! */
536 };
537 
iomap_ioend_from_bio(struct bio * bio)538 static inline struct iomap_ioend *iomap_ioend_from_bio(struct bio *bio)
539 {
540 	return container_of(bio, struct iomap_ioend, io_bio);
541 }
542 
543 #define BVEC_ITER_IOEND(_ioend)				\
544 {							\
545 	.bi_sector	= (_ioend)->io_sector,		\
546 	.bi_size	= (_ioend)->io_size,		\
547 	.bi_offset	= (_ioend)->io_bvec_offset,	\
548 }
549 
550 struct iomap_writeback_ops {
551 	/*
552 	 * Performs writeback on the passed in range
553 	 *
554 	 * Can map arbitrarily large regions, but we need to call into it at
555 	 * least once per folio to allow the file systems to synchronize with
556 	 * the write path that could be invalidating mappings.
557 	 *
558 	 * An existing mapping from a previous call to this method can be reused
559 	 * by the file system if it is still valid.
560 	 *
561 	 * If this succeeds, iomap_finish_folio_write() must be called once
562 	 * writeback completes for the range, regardless of whether the
563 	 * writeback succeeded or failed.
564 	 *
565 	 * Returns the number of bytes processed or a negative errno.
566 	 */
567 	ssize_t (*writeback_range)(struct iomap_writepage_ctx *wpc,
568 			struct folio *folio, u64 pos, unsigned int len,
569 			u64 end_pos);
570 
571 	/*
572 	 * Submit a writeback context previously build up by ->writeback_range.
573 	 *
574 	 * Returns 0 if the context was successfully submitted, or a negative
575 	 * error code if not.  If @error is non-zero a failure occurred, and
576 	 * the writeback context should be completed with an error.
577 	 */
578 	int (*writeback_submit)(struct iomap_writepage_ctx *wpc, int error);
579 };
580 
581 struct iomap_writepage_ctx {
582 	struct iomap		iomap;
583 	struct inode		*inode;
584 	struct writeback_control *wbc;
585 	const struct iomap_writeback_ops *ops;
586 	u32			nr_folios;	/* folios added to the ioend */
587 	void			*wb_ctx;	/* pending writeback context */
588 };
589 
590 struct iomap_ioend *iomap_init_ioend(struct inode *inode, struct bio *bio,
591 		loff_t file_offset, u16 ioend_flags);
592 struct iomap_ioend *iomap_split_ioend(struct iomap_ioend *ioend,
593 		unsigned int max_len, bool is_append);
594 void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
595 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
596 		struct list_head *more_ioends);
597 void iomap_sort_ioends(struct list_head *ioend_list);
598 int iomap_ioend_integrity_verify(struct iomap_ioend *ioend);
599 ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,
600 		loff_t pos, loff_t end_pos, unsigned int dirty_len);
601 int iomap_ioend_writeback_submit(struct iomap_writepage_ctx *wpc, int error);
602 
603 void iomap_finish_folio_read(struct folio *folio, size_t off, size_t len,
604 		int error);
605 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
606 		size_t len);
607 
608 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio);
609 int iomap_writepages(struct iomap_writepage_ctx *wpc);
610 
611 void iomap_bounce_read(struct iomap_ioend *orig_ioend, unsigned int minsize,
612 		void (*submit_ioend)(struct iomap_ioend *ioend));
613 void iomap_bounce_read_end_io(struct iomap_ioend *ioend, struct bio *orig_bio,
614 		int error);
615 
616 struct iomap_read_folio_ctx {
617 	const struct iomap_read_ops *ops;
618 	struct folio		*cur_folio;
619 	struct readahead_control *rac;
620 	void			*read_ctx;
621 	loff_t			read_ctx_file_offset;
622 	struct fsverity_info	*vi;
623 };
624 
625 struct iomap_read_ops {
626 	/*
627 	 * Read in a folio range.
628 	 *
629 	 * If this succeeds, iomap_finish_folio_read() must be called after the
630 	 * range is read in, regardless of whether the read succeeded or failed.
631 	 *
632 	 * Returns 0 on success or a negative error on failure.
633 	 */
634 	int (*read_folio_range)(const struct iomap_iter *iter,
635 			struct iomap_read_folio_ctx *ctx, size_t len);
636 
637 	/*
638 	 * Submit any pending read requests.
639 	 *
640 	 * This is optional.
641 	 */
642 	void (*submit_read)(const struct iomap_iter *iter,
643 			struct iomap_read_folio_ctx *ctx);
644 
645 	/*
646 	 * Optional, allows filesystem to specify own bio_set, so new bio's
647 	 * can be allocated from the provided bio_set.
648 	 */
649 	struct bio_set *bio_set;
650 };
651 
652 /*
653  * Flags for direct I/O ->end_io:
654  */
655 #define IOMAP_DIO_UNWRITTEN	(1 << 0)	/* covers unwritten extent(s) */
656 #define IOMAP_DIO_COW		(1 << 1)	/* covers COW extent(s) */
657 
658 struct iomap_dio_ops {
659 	int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
660 		      unsigned flags);
661 	void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
662 		          loff_t file_offset);
663 
664 	/*
665 	 * Filesystems wishing to attach private information to a direct io bio
666 	 * must provide a ->submit_io method that attaches the additional
667 	 * information to the bio and changes the ->bi_end_io callback to a
668 	 * custom function.  This function should, at a minimum, perform any
669 	 * relevant post-processing of the bio and end with a call to
670 	 * iomap_dio_bio_end_io.
671 	 */
672 	struct bio_set *bio_set;
673 };
674 
675 /*
676  * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
677  * synchronous.
678  */
679 #define IOMAP_DIO_FORCE_WAIT	(1 << 0)
680 
681 /*
682  * Do not allocate blocks or zero partial blocks, but instead fall back to
683  * the caller by returning -EAGAIN.  Used to optimize direct I/O writes that
684  * are not aligned to the file system block size.
685   */
686 #define IOMAP_DIO_OVERWRITE_ONLY	(1 << 1)
687 
688 /*
689  * When a page fault occurs, return a partial synchronous result and allow
690  * the caller to retry the rest of the operation after dealing with the page
691  * fault.
692  */
693 #define IOMAP_DIO_PARTIAL		(1 << 2)
694 
695 /*
696  * Ensure each bio is aligned to fs block size.
697  *
698  * For filesystems which need to calculate/verify the checksum of each fs
699  * block. Otherwise they may not be able to handle unaligned bios.
700  */
701 #define IOMAP_DIO_FSBLOCK_ALIGNED	(1 << 3)
702 
703 /*
704  * Bounce buffer instead of using zero copy access.
705  *
706  * This is needed if the device needs stable data to checksum or generate
707  * parity.  The file system must hook into the I/O submission and offload
708  * completions to user context for reads when this is set.
709  */
710 #define IOMAP_DIO_BOUNCE		(1 << 4)
711 
712 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
713 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
714 		unsigned int dio_flags, void *private, size_t done_before);
715 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
716 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
717 		unsigned int dio_flags, void *private, size_t done_before);
718 ssize_t iomap_dio_complete(struct iomap_dio *dio);
719 void iomap_dio_bio_end_io(struct bio *bio);
720 
721 /*
722  * Fast path for small, block-aligned direct I/Os that map to a single
723  * contiguous on-disk extent.
724  *
725  * @iter must describe a non-empty READ no larger than the inode block size:
726  * writes, zero-length I/O, and larger requests need the generic iomap direct
727  * I/O path.
728  *
729  * Does not support iomap_dio_ops, dio_flags, done_before or private data.
730  * The range must also stay within i_size and encrypted inodes must use the
731  * generic iomap direct I/O path.
732  *
733  * -ENOTBLK indicates the generic path must be used by the caller instead.
734  * Any other errno is a real result and is propagated as-is, in particular
735  * -EAGAIN for IOCB_NOWAIT must reach the caller.
736  *
737  * The caller can only provide an iomap begin handler, and the iterator
738  * is never advanced.
739  */
740 ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter,
741 		struct iomap_iter *iomi);
iomap_dio_read_simple(struct kiocb * iocb,struct iov_iter * iter,iomap_iter_begin_fn begin)742 static __always_inline ssize_t iomap_dio_read_simple(struct kiocb *iocb,
743 		struct iov_iter *iter, iomap_iter_begin_fn begin)
744 {
745 	struct iomap_iter iomi = {
746 		.inode		= file_inode(iocb->ki_filp),
747 		.pos		= iocb->ki_pos,
748 		.len		= iov_iter_count(iter),
749 		.flags		= IOMAP_DIRECT,
750 	};
751 	ssize_t ret;
752 
753 	if (!iomi.len)
754 		return 0;
755 
756 	/*
757 	 * Simple dio is an optimization for small IO. Filter out large IO
758 	 * early as it's the most common case to fail for typical direct IO
759 	 * workloads.
760 	 */
761 	if (iomi.len > iomi.inode->i_sb->s_blocksize)
762 		return -ENOTBLK;
763 	if (iocb->ki_pos + iomi.len > i_size_read(iomi.inode))
764 		return -ENOTBLK;
765 	if (IS_ENCRYPTED(iomi.inode))
766 		return -ENOTBLK;
767 
768 	ret = kiocb_write_and_wait(iocb, iomi.len);
769 	if (ret)
770 		return ret;
771 
772 	if (iocb->ki_flags & IOCB_NOWAIT)
773 		iomi.flags |= IOMAP_NOWAIT;
774 
775 	inode_dio_begin(iomi.inode);
776 	ret = begin(iomi.inode, iomi.pos, iomi.len, iomi.flags, &iomi.iomap,
777 			&iomi.srcmap);
778 	if (ret) {
779 		inode_dio_end(iomi.inode);
780 		return ret;
781 	}
782 
783 	return __iomap_dio_read_simple(iocb, iter, &iomi);
784 }
785 
786 #ifdef CONFIG_SWAP
787 struct file;
788 struct swap_info_struct;
789 
790 int iomap_swapfile_activate(struct swap_info_struct *sis,
791 		struct file *swap_file, sector_t *pagespan,
792 		const struct iomap_ops *ops);
793 #else
794 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops)	(-EIO)
795 #endif /* CONFIG_SWAP */
796 
797 extern struct bio_set iomap_ioend_bioset;
798 
799 #ifdef CONFIG_BLOCK
800 int iomap_bio_read_folio_range(const struct iomap_iter *iter,
801 		struct iomap_read_folio_ctx *ctx, size_t plen);
802 void iomap_bio_submit_read_endio(const struct iomap_iter *iter,
803 		struct iomap_read_folio_ctx *ctx, bio_end_io_t end_io);
804 
805 extern const struct iomap_read_ops iomap_bio_read_ops;
806 
iomap_bio_read_folio(struct folio * folio,const struct iomap_ops * ops)807 static inline void iomap_bio_read_folio(struct folio *folio,
808 		const struct iomap_ops *ops)
809 {
810 	struct iomap_read_folio_ctx ctx = {
811 		.ops		= &iomap_bio_read_ops,
812 		.cur_folio	= folio,
813 	};
814 
815 	iomap_read_folio(ops, &ctx, NULL);
816 }
817 
iomap_bio_readahead(struct readahead_control * rac,const struct iomap_ops * ops)818 static inline void iomap_bio_readahead(struct readahead_control *rac,
819 		const struct iomap_ops *ops)
820 {
821 	struct iomap_read_folio_ctx ctx = {
822 		.ops		= &iomap_bio_read_ops,
823 		.rac		= rac,
824 	};
825 
826 	iomap_readahead(ops, &ctx, NULL);
827 }
828 #endif /* CONFIG_BLOCK */
829 
830 #endif /* LINUX_IOMAP_H */
831