iomap: Rename page_ops to folio_ops
[linux-block.git] / include / linux / iomap.h
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 #define IOMAP_F_NEW             (1U << 0)
58 #define IOMAP_F_DIRTY           (1U << 1)
59 #define IOMAP_F_SHARED          (1U << 2)
60 #define IOMAP_F_MERGED          (1U << 3)
61 #define IOMAP_F_BUFFER_HEAD     (1U << 4)
62 #define IOMAP_F_ZONE_APPEND     (1U << 5)
63 #define IOMAP_F_XATTR           (1U << 6)
64
65 /*
66  * Flags set by the core iomap code during operations:
67  *
68  * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
69  * has changed as the result of this write operation.
70  *
71  * IOMAP_F_STALE indicates that the iomap is not valid any longer and the file
72  * range it covers needs to be remapped by the high level before the operation
73  * can proceed.
74  */
75 #define IOMAP_F_SIZE_CHANGED    (1U << 8)
76 #define IOMAP_F_STALE           (1U << 9)
77
78 /*
79  * Flags from 0x1000 up are for file system specific usage:
80  */
81 #define IOMAP_F_PRIVATE         (1U << 12)
82
83
84 /*
85  * Magic value for addr:
86  */
87 #define IOMAP_NULL_ADDR -1ULL   /* addr is not valid */
88
89 struct iomap_folio_ops;
90
91 struct iomap {
92         u64                     addr; /* disk offset of mapping, bytes */
93         loff_t                  offset; /* file offset of mapping, bytes */
94         u64                     length; /* length of mapping, bytes */
95         u16                     type;   /* type of mapping */
96         u16                     flags;  /* flags for mapping */
97         struct block_device     *bdev;  /* block device for I/O */
98         struct dax_device       *dax_dev; /* dax_dev for dax operations */
99         void                    *inline_data;
100         void                    *private; /* filesystem private */
101         const struct iomap_folio_ops *folio_ops;
102         u64                     validity_cookie; /* used with .iomap_valid() */
103 };
104
105 static inline sector_t iomap_sector(const struct iomap *iomap, loff_t pos)
106 {
107         return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
108 }
109
110 /*
111  * Returns the inline data pointer for logical offset @pos.
112  */
113 static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
114 {
115         return iomap->inline_data + pos - iomap->offset;
116 }
117
118 /*
119  * Check if the mapping's length is within the valid range for inline data.
120  * This is used to guard against accessing data beyond the page inline_data
121  * points at.
122  */
123 static inline bool iomap_inline_data_valid(const struct iomap *iomap)
124 {
125         return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
126 }
127
128 /*
129  * When a filesystem sets folio_ops in an iomap mapping it returns, get_folio
130  * and put_folio will be called for each folio written to.  This only applies
131  * to buffered writes as unbuffered writes will not typically have folios
132  * associated with them.
133  *
134  * When get_folio succeeds, put_folio will always be called to do any
135  * cleanup work necessary.  put_folio is responsible for unlocking and putting
136  * @folio.
137  */
138 struct iomap_folio_ops {
139         struct folio *(*get_folio)(struct iomap_iter *iter, loff_t pos,
140                         unsigned len);
141         void (*put_folio)(struct inode *inode, loff_t pos, unsigned copied,
142                         struct folio *folio);
143
144         /*
145          * Check that the cached iomap still maps correctly to the filesystem's
146          * internal extent map. FS internal extent maps can change while iomap
147          * is iterating a cached iomap, so this hook allows iomap to detect that
148          * the iomap needs to be refreshed during a long running write
149          * operation.
150          *
151          * The filesystem can store internal state (e.g. a sequence number) in
152          * iomap->validity_cookie when the iomap is first mapped to be able to
153          * detect changes between mapping time and whenever .iomap_valid() is
154          * called.
155          *
156          * This is called with the folio over the specified file position held
157          * locked by the iomap code.
158          */
159         bool (*iomap_valid)(struct inode *inode, const struct iomap *iomap);
160 };
161
162 /*
163  * Flags for iomap_begin / iomap_end.  No flag implies a read.
164  */
165 #define IOMAP_WRITE             (1 << 0) /* writing, must allocate blocks */
166 #define IOMAP_ZERO              (1 << 1) /* zeroing operation, may skip holes */
167 #define IOMAP_REPORT            (1 << 2) /* report extent status, e.g. FIEMAP */
168 #define IOMAP_FAULT             (1 << 3) /* mapping for page fault */
169 #define IOMAP_DIRECT            (1 << 4) /* direct I/O */
170 #define IOMAP_NOWAIT            (1 << 5) /* do not block */
171 #define IOMAP_OVERWRITE_ONLY    (1 << 6) /* only pure overwrites allowed */
172 #define IOMAP_UNSHARE           (1 << 7) /* unshare_file_range */
173 #ifdef CONFIG_FS_DAX
174 #define IOMAP_DAX               (1 << 8) /* DAX mapping */
175 #else
176 #define IOMAP_DAX               0
177 #endif /* CONFIG_FS_DAX */
178
179 struct iomap_ops {
180         /*
181          * Return the existing mapping at pos, or reserve space starting at
182          * pos for up to length, as long as we can do it as a single mapping.
183          * The actual length is returned in iomap->length.
184          */
185         int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
186                         unsigned flags, struct iomap *iomap,
187                         struct iomap *srcmap);
188
189         /*
190          * Commit and/or unreserve space previous allocated using iomap_begin.
191          * Written indicates the length of the successful write operation which
192          * needs to be commited, while the rest needs to be unreserved.
193          * Written might be zero if no data was written.
194          */
195         int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
196                         ssize_t written, unsigned flags, struct iomap *iomap);
197 };
198
199 /**
200  * struct iomap_iter - Iterate through a range of a file
201  * @inode: Set at the start of the iteration and should not change.
202  * @pos: The current file position we are operating on.  It is updated by
203  *      calls to iomap_iter().  Treat as read-only in the body.
204  * @len: The remaining length of the file segment we're operating on.
205  *      It is updated at the same time as @pos.
206  * @processed: The number of bytes processed by the body in the most recent
207  *      iteration, or a negative errno. 0 causes the iteration to stop.
208  * @flags: Zero or more of the iomap_begin flags above.
209  * @iomap: Map describing the I/O iteration
210  * @srcmap: Source map for COW operations
211  */
212 struct iomap_iter {
213         struct inode *inode;
214         loff_t pos;
215         u64 len;
216         s64 processed;
217         unsigned flags;
218         struct iomap iomap;
219         struct iomap srcmap;
220         void *private;
221 };
222
223 int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops);
224
225 /**
226  * iomap_length - length of the current iomap iteration
227  * @iter: iteration structure
228  *
229  * Returns the length that the operation applies to for the current iteration.
230  */
231 static inline u64 iomap_length(const struct iomap_iter *iter)
232 {
233         u64 end = iter->iomap.offset + iter->iomap.length;
234
235         if (iter->srcmap.type != IOMAP_HOLE)
236                 end = min(end, iter->srcmap.offset + iter->srcmap.length);
237         return min(iter->len, end - iter->pos);
238 }
239
240 /**
241  * iomap_iter_srcmap - return the source map for the current iomap iteration
242  * @i: iteration structure
243  *
244  * Write operations on file systems with reflink support might require a
245  * source and a destination map.  This function retourns the source map
246  * for a given operation, which may or may no be identical to the destination
247  * map in &i->iomap.
248  */
249 static inline const struct iomap *iomap_iter_srcmap(const struct iomap_iter *i)
250 {
251         if (i->srcmap.type != IOMAP_HOLE)
252                 return &i->srcmap;
253         return &i->iomap;
254 }
255
256 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
257                 const struct iomap_ops *ops);
258 int iomap_file_buffered_write_punch_delalloc(struct inode *inode,
259                 struct iomap *iomap, loff_t pos, loff_t length, ssize_t written,
260                 int (*punch)(struct inode *inode, loff_t pos, loff_t length));
261
262 int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops);
263 void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
264 bool iomap_is_partially_uptodate(struct folio *, size_t from, size_t count);
265 struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos);
266 bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags);
267 void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len);
268 int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
269                 const struct iomap_ops *ops);
270 int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
271                 bool *did_zero, const struct iomap_ops *ops);
272 int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
273                 const struct iomap_ops *ops);
274 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
275                         const struct iomap_ops *ops);
276 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
277                 u64 start, u64 len, const struct iomap_ops *ops);
278 loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
279                 const struct iomap_ops *ops);
280 loff_t iomap_seek_data(struct inode *inode, loff_t offset,
281                 const struct iomap_ops *ops);
282 sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
283                 const struct iomap_ops *ops);
284
285 /*
286  * Structure for writeback I/O completions.
287  */
288 struct iomap_ioend {
289         struct list_head        io_list;        /* next ioend in chain */
290         u16                     io_type;
291         u16                     io_flags;       /* IOMAP_F_* */
292         u32                     io_folios;      /* folios added to ioend */
293         struct inode            *io_inode;      /* file being written to */
294         size_t                  io_size;        /* size of the extent */
295         loff_t                  io_offset;      /* offset in the file */
296         sector_t                io_sector;      /* start sector of ioend */
297         struct bio              *io_bio;        /* bio being built */
298         struct bio              io_inline_bio;  /* MUST BE LAST! */
299 };
300
301 struct iomap_writeback_ops {
302         /*
303          * Required, maps the blocks so that writeback can be performed on
304          * the range starting at offset.
305          */
306         int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
307                                 loff_t offset);
308
309         /*
310          * Optional, allows the file systems to perform actions just before
311          * submitting the bio and/or override the bio end_io handler for complex
312          * operations like copy on write extent manipulation or unwritten extent
313          * conversions.
314          */
315         int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
316
317         /*
318          * Optional, allows the file system to discard state on a page where
319          * we failed to submit any I/O.
320          */
321         void (*discard_folio)(struct folio *folio, loff_t pos);
322 };
323
324 struct iomap_writepage_ctx {
325         struct iomap            iomap;
326         struct iomap_ioend      *ioend;
327         const struct iomap_writeback_ops *ops;
328 };
329
330 void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
331 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
332                 struct list_head *more_ioends);
333 void iomap_sort_ioends(struct list_head *ioend_list);
334 int iomap_writepages(struct address_space *mapping,
335                 struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
336                 const struct iomap_writeback_ops *ops);
337
338 /*
339  * Flags for direct I/O ->end_io:
340  */
341 #define IOMAP_DIO_UNWRITTEN     (1 << 0)        /* covers unwritten extent(s) */
342 #define IOMAP_DIO_COW           (1 << 1)        /* covers COW extent(s) */
343
344 struct iomap_dio_ops {
345         int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
346                       unsigned flags);
347         void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
348                           loff_t file_offset);
349
350         /*
351          * Filesystems wishing to attach private information to a direct io bio
352          * must provide a ->submit_io method that attaches the additional
353          * information to the bio and changes the ->bi_end_io callback to a
354          * custom function.  This function should, at a minimum, perform any
355          * relevant post-processing of the bio and end with a call to
356          * iomap_dio_bio_end_io.
357          */
358         struct bio_set *bio_set;
359 };
360
361 /*
362  * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
363  * synchronous.
364  */
365 #define IOMAP_DIO_FORCE_WAIT    (1 << 0)
366
367 /*
368  * Do not allocate blocks or zero partial blocks, but instead fall back to
369  * the caller by returning -EAGAIN.  Used to optimize direct I/O writes that
370  * are not aligned to the file system block size.
371   */
372 #define IOMAP_DIO_OVERWRITE_ONLY        (1 << 1)
373
374 /*
375  * When a page fault occurs, return a partial synchronous result and allow
376  * the caller to retry the rest of the operation after dealing with the page
377  * fault.
378  */
379 #define IOMAP_DIO_PARTIAL               (1 << 2)
380
381 /*
382  * The caller will sync the write if needed; do not sync it within
383  * iomap_dio_rw.  Overrides IOMAP_DIO_FORCE_WAIT.
384  */
385 #define IOMAP_DIO_NOSYNC                (1 << 3)
386
387 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
388                 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
389                 unsigned int dio_flags, void *private, size_t done_before);
390 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
391                 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
392                 unsigned int dio_flags, void *private, size_t done_before);
393 ssize_t iomap_dio_complete(struct iomap_dio *dio);
394 void iomap_dio_bio_end_io(struct bio *bio);
395
396 #ifdef CONFIG_SWAP
397 struct file;
398 struct swap_info_struct;
399
400 int iomap_swapfile_activate(struct swap_info_struct *sis,
401                 struct file *swap_file, sector_t *pagespan,
402                 const struct iomap_ops *ops);
403 #else
404 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops)  (-EIO)
405 #endif /* CONFIG_SWAP */
406
407 #endif /* LINUX_IOMAP_H */