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
13 struct address_space;
14 struct fiemap_extent_info;
15 struct inode;
16 struct iomap_iter;
17 struct iomap_dio;
18 struct iomap_writepage_ctx;
19 struct iov_iter;
20 struct kiocb;
21 struct page;
22 struct vm_area_struct;
23 struct vm_fault;
24
25 /*
26 * Types of block ranges for iomap mappings:
27 */
28 #define IOMAP_HOLE 0 /* no blocks allocated, need allocation */
29 #define IOMAP_DELALLOC 1 /* delayed allocation blocks */
30 #define IOMAP_MAPPED 2 /* blocks allocated at @addr */
31 #define IOMAP_UNWRITTEN 3 /* blocks allocated at @addr in unwritten state */
32 #define IOMAP_INLINE 4 /* data inline in the inode */
33
34 /*
35 * Flags reported by the file system from iomap_begin:
36 *
37 * IOMAP_F_NEW indicates that the blocks have been newly allocated and need
38 * zeroing for areas that no data is copied to.
39 *
40 * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
41 * written data and requires fdatasync to commit them to persistent storage.
42 * This needs to take into account metadata changes that *may* be made at IO
43 * completion, such as file size updates from direct IO.
44 *
45 * IOMAP_F_SHARED indicates that the blocks are shared, and will need to be
46 * unshared as part a write.
47 *
48 * IOMAP_F_MERGED indicates that the iomap contains the merge of multiple block
49 * mappings.
50 *
51 * IOMAP_F_BUFFER_HEAD indicates that the file system requires the use of
52 * buffer heads for this mapping.
53 *
54 * IOMAP_F_XATTR indicates that the iomap is for an extended attribute extent
55 * rather than a file data extent.
56 *
57 * IOMAP_F_BOUNDARY indicates that I/O and I/O completions for this iomap must
58 * never be merged with the mapping before it.
59 *
60 * IOMAP_F_ANON_WRITE indicates that (write) I/O does not have a target block
61 * assigned to it yet and the file system will do that in the bio submission
62 * handler, splitting the I/O as needed.
63 *
64 * IOMAP_F_ATOMIC_BIO indicates that (write) I/O will be issued as an atomic
65 * bio, i.e. set REQ_ATOMIC.
66 */
67 #define IOMAP_F_NEW (1U << 0)
68 #define IOMAP_F_DIRTY (1U << 1)
69 #define IOMAP_F_SHARED (1U << 2)
70 #define IOMAP_F_MERGED (1U << 3)
71 #ifdef CONFIG_BUFFER_HEAD
72 #define IOMAP_F_BUFFER_HEAD (1U << 4)
73 #else
74 #define IOMAP_F_BUFFER_HEAD 0
75 #endif /* CONFIG_BUFFER_HEAD */
76 #define IOMAP_F_XATTR (1U << 5)
77 #define IOMAP_F_BOUNDARY (1U << 6)
78 #define IOMAP_F_ANON_WRITE (1U << 7)
79 #define IOMAP_F_ATOMIC_BIO (1U << 8)
80
81 /*
82 * Flag reserved for file system specific usage
83 */
84 #define IOMAP_F_PRIVATE (1U << 12)
85
86 /*
87 * Flags set by the core iomap code during operations:
88 *
89 * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
90 * has changed as the result of this write operation.
91 *
92 * IOMAP_F_STALE indicates that the iomap is not valid any longer and the file
93 * range it covers needs to be remapped by the high level before the operation
94 * can proceed.
95 */
96 #define IOMAP_F_SIZE_CHANGED (1U << 14)
97 #define IOMAP_F_STALE (1U << 15)
98
99 /*
100 * Magic value for addr:
101 */
102 #define IOMAP_NULL_ADDR -1ULL /* addr is not valid */
103
104 struct iomap {
105 u64 addr; /* disk offset of mapping, bytes */
106 loff_t offset; /* file offset of mapping, bytes */
107 u64 length; /* length of mapping, bytes */
108 u16 type; /* type of mapping */
109 u16 flags; /* flags for mapping */
110 struct block_device *bdev; /* block device for I/O */
111 struct dax_device *dax_dev; /* dax_dev for dax operations */
112 void *inline_data;
113 void *private; /* filesystem private */
114 u64 validity_cookie; /* used with .iomap_valid() */
115 };
116
iomap_sector(const struct iomap * iomap,loff_t pos)117 static inline sector_t iomap_sector(const struct iomap *iomap, loff_t pos)
118 {
119 if (iomap->flags & IOMAP_F_ANON_WRITE)
120 return U64_MAX; /* invalid */
121 return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
122 }
123
124 /*
125 * Returns the inline data pointer for logical offset @pos.
126 */
iomap_inline_data(const struct iomap * iomap,loff_t pos)127 static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
128 {
129 return iomap->inline_data + pos - iomap->offset;
130 }
131
132 /*
133 * Check if the mapping's length is within the valid range for inline data.
134 * This is used to guard against accessing data beyond the page inline_data
135 * points at.
136 */
iomap_inline_data_valid(const struct iomap * iomap)137 static inline bool iomap_inline_data_valid(const struct iomap *iomap)
138 {
139 return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
140 }
141
142 /*
143 * When get_folio succeeds, put_folio will always be called to do any
144 * cleanup work necessary. put_folio is responsible for unlocking and putting
145 * @folio.
146 */
147 struct iomap_write_ops {
148 struct folio *(*get_folio)(struct iomap_iter *iter, loff_t pos,
149 unsigned len);
150 void (*put_folio)(struct inode *inode, loff_t pos, unsigned copied,
151 struct folio *folio);
152
153 /*
154 * Check that the cached iomap still maps correctly to the filesystem's
155 * internal extent map. FS internal extent maps can change while iomap
156 * is iterating a cached iomap, so this hook allows iomap to detect that
157 * the iomap needs to be refreshed during a long running write
158 * operation.
159 *
160 * The filesystem can store internal state (e.g. a sequence number) in
161 * iomap->validity_cookie when the iomap is first mapped to be able to
162 * detect changes between mapping time and whenever .iomap_valid() is
163 * called.
164 *
165 * This is called with the folio over the specified file position held
166 * locked by the iomap code.
167 */
168 bool (*iomap_valid)(struct inode *inode, const struct iomap *iomap);
169
170 /*
171 * Optional if the filesystem wishes to provide a custom handler for
172 * reading in the contents of a folio, otherwise iomap will default to
173 * submitting a bio read request.
174 *
175 * The read must be done synchronously.
176 */
177 int (*read_folio_range)(const struct iomap_iter *iter,
178 struct folio *folio, loff_t pos, size_t len);
179 };
180
181 /*
182 * Flags for iomap_begin / iomap_end. No flag implies a read.
183 */
184 #define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
185 #define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
186 #define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
187 #define IOMAP_FAULT (1 << 3) /* mapping for page fault */
188 #define IOMAP_DIRECT (1 << 4) /* direct I/O */
189 #define IOMAP_NOWAIT (1 << 5) /* do not block */
190 #define IOMAP_OVERWRITE_ONLY (1 << 6) /* only pure overwrites allowed */
191 #define IOMAP_UNSHARE (1 << 7) /* unshare_file_range */
192 #ifdef CONFIG_FS_DAX
193 #define IOMAP_DAX (1 << 8) /* DAX mapping */
194 #else
195 #define IOMAP_DAX 0
196 #endif /* CONFIG_FS_DAX */
197 #define IOMAP_ATOMIC (1 << 9) /* torn-write protection */
198 #define IOMAP_DONTCACHE (1 << 10)
199
200 struct iomap_ops {
201 /*
202 * Return the existing mapping at pos, or reserve space starting at
203 * pos for up to length, as long as we can do it as a single mapping.
204 * The actual length is returned in iomap->length.
205 */
206 int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
207 unsigned flags, struct iomap *iomap,
208 struct iomap *srcmap);
209
210 /*
211 * Commit and/or unreserve space previous allocated using iomap_begin.
212 * Written indicates the length of the successful write operation which
213 * needs to be commited, while the rest needs to be unreserved.
214 * Written might be zero if no data was written.
215 */
216 int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
217 ssize_t written, unsigned flags, struct iomap *iomap);
218 };
219
220 /**
221 * struct iomap_iter - Iterate through a range of a file
222 * @inode: Set at the start of the iteration and should not change.
223 * @pos: The current file position we are operating on. It is updated by
224 * calls to iomap_iter(). Treat as read-only in the body.
225 * @len: The remaining length of the file segment we're operating on.
226 * It is updated at the same time as @pos.
227 * @iter_start_pos: The original start pos for the current iomap. Used for
228 * incremental iter advance.
229 * @status: Status of the most recent iteration. Zero on success or a negative
230 * errno on error.
231 * @flags: Zero or more of the iomap_begin flags above.
232 * @iomap: Map describing the I/O iteration
233 * @srcmap: Source map for COW operations
234 */
235 struct iomap_iter {
236 struct inode *inode;
237 loff_t pos;
238 u64 len;
239 loff_t iter_start_pos;
240 int status;
241 unsigned flags;
242 struct iomap iomap;
243 struct iomap srcmap;
244 void *private;
245 };
246
247 int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops);
248 int iomap_iter_advance(struct iomap_iter *iter, u64 *count);
249
250 /**
251 * iomap_length_trim - trimmed length of the current iomap iteration
252 * @iter: iteration structure
253 * @pos: File position to trim from.
254 * @len: Length of the mapping to trim to.
255 *
256 * Returns a trimmed length that the operation applies to for the current
257 * iteration.
258 */
iomap_length_trim(const struct iomap_iter * iter,loff_t pos,u64 len)259 static inline u64 iomap_length_trim(const struct iomap_iter *iter, loff_t pos,
260 u64 len)
261 {
262 u64 end = iter->iomap.offset + iter->iomap.length;
263
264 if (iter->srcmap.type != IOMAP_HOLE)
265 end = min(end, iter->srcmap.offset + iter->srcmap.length);
266 return min(len, end - pos);
267 }
268
269 /**
270 * iomap_length - length of the current iomap iteration
271 * @iter: iteration structure
272 *
273 * Returns the length that the operation applies to for the current iteration.
274 */
iomap_length(const struct iomap_iter * iter)275 static inline u64 iomap_length(const struct iomap_iter *iter)
276 {
277 return iomap_length_trim(iter, iter->pos, iter->len);
278 }
279
280 /**
281 * iomap_iter_advance_full - advance by the full length of current map
282 */
iomap_iter_advance_full(struct iomap_iter * iter)283 static inline int iomap_iter_advance_full(struct iomap_iter *iter)
284 {
285 u64 length = iomap_length(iter);
286
287 return iomap_iter_advance(iter, &length);
288 }
289
290 /**
291 * iomap_iter_srcmap - return the source map for the current iomap iteration
292 * @i: iteration structure
293 *
294 * Write operations on file systems with reflink support might require a
295 * source and a destination map. This function retourns the source map
296 * for a given operation, which may or may no be identical to the destination
297 * map in &i->iomap.
298 */
iomap_iter_srcmap(const struct iomap_iter * i)299 static inline const struct iomap *iomap_iter_srcmap(const struct iomap_iter *i)
300 {
301 if (i->srcmap.type != IOMAP_HOLE)
302 return &i->srcmap;
303 return &i->iomap;
304 }
305
306 /*
307 * Return the file offset for the first unchanged block after a short write.
308 *
309 * If nothing was written, round @pos down to point at the first block in
310 * the range, else round up to include the partially written block.
311 */
iomap_last_written_block(struct inode * inode,loff_t pos,ssize_t written)312 static inline loff_t iomap_last_written_block(struct inode *inode, loff_t pos,
313 ssize_t written)
314 {
315 if (unlikely(!written))
316 return round_down(pos, i_blocksize(inode));
317 return round_up(pos + written, i_blocksize(inode));
318 }
319
320 /*
321 * Check if the range needs to be unshared for a FALLOC_FL_UNSHARE_RANGE
322 * operation.
323 *
324 * Don't bother with blocks that are not shared to start with; or mappings that
325 * cannot be shared, such as inline data, delalloc reservations, holes or
326 * unwritten extents.
327 *
328 * Note that we use srcmap directly instead of iomap_iter_srcmap as unsharing
329 * requires providing a separate source map, and the presence of one is a good
330 * indicator that unsharing is needed, unlike IOMAP_F_SHARED which can be set
331 * for any data that goes into the COW fork for XFS.
332 */
iomap_want_unshare_iter(const struct iomap_iter * iter)333 static inline bool iomap_want_unshare_iter(const struct iomap_iter *iter)
334 {
335 return (iter->iomap.flags & IOMAP_F_SHARED) &&
336 iter->srcmap.type == IOMAP_MAPPED;
337 }
338
339 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
340 const struct iomap_ops *ops,
341 const struct iomap_write_ops *write_ops, void *private);
342 int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops);
343 void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
344 bool iomap_is_partially_uptodate(struct folio *, size_t from, size_t count);
345 struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len);
346 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
347 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
348 bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio);
349 int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
350 const struct iomap_ops *ops,
351 const struct iomap_write_ops *write_ops);
352 int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
353 bool *did_zero, const struct iomap_ops *ops,
354 const struct iomap_write_ops *write_ops, void *private);
355 int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
356 const struct iomap_ops *ops,
357 const struct iomap_write_ops *write_ops, void *private);
358 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops,
359 void *private);
360 typedef void (*iomap_punch_t)(struct inode *inode, loff_t offset, loff_t length,
361 struct iomap *iomap);
362 void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte,
363 loff_t end_byte, unsigned flags, struct iomap *iomap,
364 iomap_punch_t punch);
365
366 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
367 u64 start, u64 len, const struct iomap_ops *ops);
368 loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
369 const struct iomap_ops *ops);
370 loff_t iomap_seek_data(struct inode *inode, loff_t offset,
371 const struct iomap_ops *ops);
372 sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
373 const struct iomap_ops *ops);
374
375 /*
376 * Flags for iomap_ioend->io_flags.
377 */
378 /* shared COW extent */
379 #define IOMAP_IOEND_SHARED (1U << 0)
380 /* unwritten extent */
381 #define IOMAP_IOEND_UNWRITTEN (1U << 1)
382 /* don't merge into previous ioend */
383 #define IOMAP_IOEND_BOUNDARY (1U << 2)
384 /* is direct I/O */
385 #define IOMAP_IOEND_DIRECT (1U << 3)
386 /* is DONTCACHE I/O */
387 #define IOMAP_IOEND_DONTCACHE (1U << 4)
388
389 /*
390 * Flags that if set on either ioend prevent the merge of two ioends.
391 * (IOMAP_IOEND_BOUNDARY also prevents merges, but only one-way)
392 */
393 #define IOMAP_IOEND_NOMERGE_FLAGS \
394 (IOMAP_IOEND_SHARED | IOMAP_IOEND_UNWRITTEN | IOMAP_IOEND_DIRECT | \
395 IOMAP_IOEND_DONTCACHE)
396
397 /*
398 * Structure for writeback I/O completions.
399 *
400 * File systems can split a bio generated by iomap. In that case the parent
401 * ioend it was split from is recorded in ioend->io_parent.
402 */
403 struct iomap_ioend {
404 struct list_head io_list; /* next ioend in chain */
405 u16 io_flags; /* IOMAP_IOEND_* */
406 struct inode *io_inode; /* file being written to */
407 size_t io_size; /* size of the extent */
408 atomic_t io_remaining; /* completetion defer count */
409 int io_error; /* stashed away status */
410 struct iomap_ioend *io_parent; /* parent for completions */
411 loff_t io_offset; /* offset in the file */
412 sector_t io_sector; /* start sector of ioend */
413 void *io_private; /* file system private data */
414 struct bio io_bio; /* MUST BE LAST! */
415 };
416
iomap_ioend_from_bio(struct bio * bio)417 static inline struct iomap_ioend *iomap_ioend_from_bio(struct bio *bio)
418 {
419 return container_of(bio, struct iomap_ioend, io_bio);
420 }
421
422 struct iomap_writeback_ops {
423 /*
424 * Performs writeback on the passed in range
425 *
426 * Can map arbitrarily large regions, but we need to call into it at
427 * least once per folio to allow the file systems to synchronize with
428 * the write path that could be invalidating mappings.
429 *
430 * An existing mapping from a previous call to this method can be reused
431 * by the file system if it is still valid.
432 *
433 * Returns the number of bytes processed or a negative errno.
434 */
435 ssize_t (*writeback_range)(struct iomap_writepage_ctx *wpc,
436 struct folio *folio, u64 pos, unsigned int len,
437 u64 end_pos);
438
439 /*
440 * Submit a writeback context previously build up by ->writeback_range.
441 *
442 * Returns 0 if the context was successfully submitted, or a negative
443 * error code if not. If @error is non-zero a failure occurred, and
444 * the writeback context should be completed with an error.
445 */
446 int (*writeback_submit)(struct iomap_writepage_ctx *wpc, int error);
447 };
448
449 struct iomap_writepage_ctx {
450 struct iomap iomap;
451 struct inode *inode;
452 struct writeback_control *wbc;
453 const struct iomap_writeback_ops *ops;
454 u32 nr_folios; /* folios added to the ioend */
455 void *wb_ctx; /* pending writeback context */
456 };
457
458 struct iomap_ioend *iomap_init_ioend(struct inode *inode, struct bio *bio,
459 loff_t file_offset, u16 ioend_flags);
460 struct iomap_ioend *iomap_split_ioend(struct iomap_ioend *ioend,
461 unsigned int max_len, bool is_append);
462 void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
463 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
464 struct list_head *more_ioends);
465 void iomap_sort_ioends(struct list_head *ioend_list);
466 ssize_t iomap_add_to_ioend(struct iomap_writepage_ctx *wpc, struct folio *folio,
467 loff_t pos, loff_t end_pos, unsigned int dirty_len);
468 int iomap_ioend_writeback_submit(struct iomap_writepage_ctx *wpc, int error);
469
470 void iomap_start_folio_write(struct inode *inode, struct folio *folio,
471 size_t len);
472 void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
473 size_t len);
474
475 int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio);
476 int iomap_writepages(struct iomap_writepage_ctx *wpc);
477
478 /*
479 * Flags for direct I/O ->end_io:
480 */
481 #define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
482 #define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
483
484 struct iomap_dio_ops {
485 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
486 unsigned flags);
487 void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
488 loff_t file_offset);
489
490 /*
491 * Filesystems wishing to attach private information to a direct io bio
492 * must provide a ->submit_io method that attaches the additional
493 * information to the bio and changes the ->bi_end_io callback to a
494 * custom function. This function should, at a minimum, perform any
495 * relevant post-processing of the bio and end with a call to
496 * iomap_dio_bio_end_io.
497 */
498 struct bio_set *bio_set;
499 };
500
501 /*
502 * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
503 * synchronous.
504 */
505 #define IOMAP_DIO_FORCE_WAIT (1 << 0)
506
507 /*
508 * Do not allocate blocks or zero partial blocks, but instead fall back to
509 * the caller by returning -EAGAIN. Used to optimize direct I/O writes that
510 * are not aligned to the file system block size.
511 */
512 #define IOMAP_DIO_OVERWRITE_ONLY (1 << 1)
513
514 /*
515 * When a page fault occurs, return a partial synchronous result and allow
516 * the caller to retry the rest of the operation after dealing with the page
517 * fault.
518 */
519 #define IOMAP_DIO_PARTIAL (1 << 2)
520
521 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
522 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
523 unsigned int dio_flags, void *private, size_t done_before);
524 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
525 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
526 unsigned int dio_flags, void *private, size_t done_before);
527 ssize_t iomap_dio_complete(struct iomap_dio *dio);
528 void iomap_dio_bio_end_io(struct bio *bio);
529
530 #ifdef CONFIG_SWAP
531 struct file;
532 struct swap_info_struct;
533
534 int iomap_swapfile_activate(struct swap_info_struct *sis,
535 struct file *swap_file, sector_t *pagespan,
536 const struct iomap_ops *ops);
537 #else
538 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
539 #endif /* CONFIG_SWAP */
540
541 extern struct bio_set iomap_ioend_bioset;
542
543 #endif /* LINUX_IOMAP_H */
544