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
487 /*
488 * Flags that if set on either ioend prevent the merge of two ioends.
489 * (IOMAP_IOEND_BOUNDARY also prevents merges, but only one-way)
490 */
491 #define IOMAP_IOEND_NOMERGE_FLAGS \
492 (IOMAP_IOEND_SHARED | IOMAP_IOEND_UNWRITTEN | IOMAP_IOEND_DIRECT)
493
494 /*
495 * Structure for writeback I/O completions.
496 *
497 * File systems can split a bio generated by iomap. In that case the parent
498 * ioend it was split from is recorded in ioend->io_parent.
499 */
500 struct iomap_ioend {
501 struct list_head io_list; /* next ioend in chain */
502 u16 io_flags; /* IOMAP_IOEND_* */
503 struct inode *io_inode; /* file being written to */
504 size_t io_size; /* size of the extent */
505 atomic_t io_remaining; /* completetion defer count */
506 int io_error; /* stashed away status */
507 struct iomap_ioend *io_parent; /* parent for completions */
508 loff_t io_offset; /* offset in the file */
509 sector_t io_sector; /* start sector of ioend */
510 void *io_private; /* file system private data */
511 struct fsverity_info *io_vi; /* fsverity info */
512 struct bio io_bio; /* MUST BE LAST! */
513 };
514
iomap_ioend_from_bio(struct bio * bio)515 static inline struct iomap_ioend *iomap_ioend_from_bio(struct bio *bio)
516 {
517 return container_of(bio, struct iomap_ioend, io_bio);
518 }
519
520 struct iomap_writeback_ops {
521 /*
522 * Performs writeback on the passed in range
523 *
524 * Can map arbitrarily large regions, but we need to call into it at
525 * least once per folio to allow the file systems to synchronize with
526 * the write path that could be invalidating mappings.
527 *
528 * An existing mapping from a previous call to this method can be reused
529 * by the file system if it is still valid.
530 *
531 * If this succeeds, iomap_finish_folio_write() must be called once
532 * writeback completes for the range, regardless of whether the
533 * writeback succeeded or failed.
534 *
535 * Returns the number of bytes processed or a negative errno.
536 */
537 ssize_t (*writeback_range)(struct iomap_writepage_ctx *wpc,
538 struct folio *folio, u64 pos, unsigned int len,
539 u64 end_pos);
540
541 /*
542 * Submit a writeback context previously build up by ->writeback_range.
543 *
544 * Returns 0 if the context was successfully submitted, or a negative
545 * error code if not. If @error is non-zero a failure occurred, and
546 * the writeback context should be completed with an error.
547 */
548 int (*writeback_submit)(struct iomap_writepage_ctx *wpc, int error);
549 };
550
551 struct iomap_writepage_ctx {
552 struct iomap iomap;
553 struct inode *inode;
554 struct writeback_control *wbc;
555 const struct iomap_writeback_ops *ops;
556 u32 nr_folios; /* folios added to the ioend */
557 void *wb_ctx; /* pending writeback context */
558 };
559
560 struct iomap_ioend *iomap_init_ioend(struct inode *inode, struct bio *bio,
561 loff_t file_offset, u16 ioend_flags);
562 struct iomap_ioend *iomap_split_ioend(struct iomap_ioend *ioend,
563 unsigned int max_len, bool is_append);
564 void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
565 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
566 struct list_head *more_ioends);
567 void iomap_sort_ioends(struct list_head *ioend_list);
568 ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,
569 loff_t pos, loff_t end_pos, unsigned int dirty_len);
570 int iomap_ioend_writeback_submit(struct iomap_writepage_ctx *wpc, int error);
571
572 void iomap_finish_folio_read(struct folio *folio, size_t off, size_t len,
573 int error);
574 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
575 size_t len);
576
577 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio);
578 int iomap_writepages(struct iomap_writepage_ctx *wpc);
579
580 struct iomap_read_folio_ctx {
581 const struct iomap_read_ops *ops;
582 struct folio *cur_folio;
583 struct readahead_control *rac;
584 void *read_ctx;
585 loff_t read_ctx_file_offset;
586 struct fsverity_info *vi;
587 };
588
589 struct iomap_read_ops {
590 /*
591 * Read in a folio range.
592 *
593 * If this succeeds, iomap_finish_folio_read() must be called after the
594 * range is read in, regardless of whether the read succeeded or failed.
595 *
596 * Returns 0 on success or a negative error on failure.
597 */
598 int (*read_folio_range)(const struct iomap_iter *iter,
599 struct iomap_read_folio_ctx *ctx, size_t len);
600
601 /*
602 * Submit any pending read requests.
603 *
604 * This is optional.
605 */
606 void (*submit_read)(const struct iomap_iter *iter,
607 struct iomap_read_folio_ctx *ctx);
608
609 /*
610 * Optional, allows filesystem to specify own bio_set, so new bio's
611 * can be allocated from the provided bio_set.
612 */
613 struct bio_set *bio_set;
614 };
615
616 /*
617 * Flags for direct I/O ->end_io:
618 */
619 #define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
620 #define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
621
622 struct iomap_dio_ops {
623 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
624 unsigned flags);
625 void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
626 loff_t file_offset);
627
628 /*
629 * Filesystems wishing to attach private information to a direct io bio
630 * must provide a ->submit_io method that attaches the additional
631 * information to the bio and changes the ->bi_end_io callback to a
632 * custom function. This function should, at a minimum, perform any
633 * relevant post-processing of the bio and end with a call to
634 * iomap_dio_bio_end_io.
635 */
636 struct bio_set *bio_set;
637 };
638
639 /*
640 * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
641 * synchronous.
642 */
643 #define IOMAP_DIO_FORCE_WAIT (1 << 0)
644
645 /*
646 * Do not allocate blocks or zero partial blocks, but instead fall back to
647 * the caller by returning -EAGAIN. Used to optimize direct I/O writes that
648 * are not aligned to the file system block size.
649 */
650 #define IOMAP_DIO_OVERWRITE_ONLY (1 << 1)
651
652 /*
653 * When a page fault occurs, return a partial synchronous result and allow
654 * the caller to retry the rest of the operation after dealing with the page
655 * fault.
656 */
657 #define IOMAP_DIO_PARTIAL (1 << 2)
658
659 /*
660 * Ensure each bio is aligned to fs block size.
661 *
662 * For filesystems which need to calculate/verify the checksum of each fs
663 * block. Otherwise they may not be able to handle unaligned bios.
664 */
665 #define IOMAP_DIO_FSBLOCK_ALIGNED (1 << 3)
666
667 /*
668 * Bounce buffer instead of using zero copy access.
669 *
670 * This is needed if the device needs stable data to checksum or generate
671 * parity. The file system must hook into the I/O submission and offload
672 * completions to user context for reads when this is set.
673 */
674 #define IOMAP_DIO_BOUNCE (1 << 4)
675
676 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
677 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
678 unsigned int dio_flags, void *private, size_t done_before);
679 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
680 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
681 unsigned int dio_flags, void *private, size_t done_before);
682 ssize_t iomap_dio_complete(struct iomap_dio *dio);
683 void iomap_dio_bio_end_io(struct bio *bio);
684
685 /*
686 * Fast path for small, block-aligned direct I/Os that map to a single
687 * contiguous on-disk extent.
688 *
689 * @iter must describe a non-empty READ no larger than the inode block size:
690 * writes, zero-length I/O, and larger requests need the generic iomap direct
691 * I/O path.
692 *
693 * Does not support iomap_dio_ops, dio_flags, done_before or private data.
694 * The range must also stay within i_size and encrypted inodes must use the
695 * generic iomap direct I/O path.
696 *
697 * -ENOTBLK indicates the generic path must be used by the caller instead.
698 * Any other errno is a real result and is propagated as-is, in particular
699 * -EAGAIN for IOCB_NOWAIT must reach the caller.
700 *
701 * The caller can only provide an iomap begin handler, and the iterator
702 * is never advanced.
703 */
704 ssize_t __iomap_dio_read_simple(struct kiocb *iocb, struct iov_iter *iter,
705 struct iomap_iter *iomi);
iomap_dio_read_simple(struct kiocb * iocb,struct iov_iter * iter,iomap_iter_begin_fn begin)706 static __always_inline ssize_t iomap_dio_read_simple(struct kiocb *iocb,
707 struct iov_iter *iter, iomap_iter_begin_fn begin)
708 {
709 struct iomap_iter iomi = {
710 .inode = file_inode(iocb->ki_filp),
711 .pos = iocb->ki_pos,
712 .len = iov_iter_count(iter),
713 .flags = IOMAP_DIRECT,
714 };
715 ssize_t ret;
716
717 if (!iomi.len)
718 return 0;
719
720 /*
721 * Simple dio is an optimization for small IO. Filter out large IO
722 * early as it's the most common case to fail for typical direct IO
723 * workloads.
724 */
725 if (iomi.len > iomi.inode->i_sb->s_blocksize)
726 return -ENOTBLK;
727 if (iocb->ki_pos + iomi.len > i_size_read(iomi.inode))
728 return -ENOTBLK;
729 if (IS_ENCRYPTED(iomi.inode))
730 return -ENOTBLK;
731
732 ret = kiocb_write_and_wait(iocb, iomi.len);
733 if (ret)
734 return ret;
735
736 if (iocb->ki_flags & IOCB_NOWAIT)
737 iomi.flags |= IOMAP_NOWAIT;
738
739 inode_dio_begin(iomi.inode);
740 ret = begin(iomi.inode, iomi.pos, iomi.len, iomi.flags, &iomi.iomap,
741 &iomi.srcmap);
742 if (ret) {
743 inode_dio_end(iomi.inode);
744 return ret;
745 }
746
747 return __iomap_dio_read_simple(iocb, iter, &iomi);
748 }
749
750 #ifdef CONFIG_SWAP
751 struct file;
752 struct swap_info_struct;
753
754 int iomap_swapfile_activate(struct swap_info_struct *sis,
755 struct file *swap_file, sector_t *pagespan,
756 const struct iomap_ops *ops);
757 #else
758 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
759 #endif /* CONFIG_SWAP */
760
761 extern struct bio_set iomap_ioend_bioset;
762
763 #ifdef CONFIG_BLOCK
764 int iomap_bio_read_folio_range(const struct iomap_iter *iter,
765 struct iomap_read_folio_ctx *ctx, size_t plen);
766 void iomap_bio_submit_read_endio(const struct iomap_iter *iter,
767 struct iomap_read_folio_ctx *ctx, bio_end_io_t end_io);
768
769 extern const struct iomap_read_ops iomap_bio_read_ops;
770
iomap_bio_read_folio(struct folio * folio,const struct iomap_ops * ops)771 static inline void iomap_bio_read_folio(struct folio *folio,
772 const struct iomap_ops *ops)
773 {
774 struct iomap_read_folio_ctx ctx = {
775 .ops = &iomap_bio_read_ops,
776 .cur_folio = folio,
777 };
778
779 iomap_read_folio(ops, &ctx, NULL);
780 }
781
iomap_bio_readahead(struct readahead_control * rac,const struct iomap_ops * ops)782 static inline void iomap_bio_readahead(struct readahead_control *rac,
783 const struct iomap_ops *ops)
784 {
785 struct iomap_read_folio_ctx ctx = {
786 .ops = &iomap_bio_read_ops,
787 .rac = rac,
788 };
789
790 iomap_readahead(ops, &ctx, NULL);
791 }
792 #endif /* CONFIG_BLOCK */
793
794 #endif /* LINUX_IOMAP_H */
795