iomap: Convert iomap_releasepage to use a folio
[linux-block.git] / include / linux / iomap.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
199a31c6
CH
2#ifndef LINUX_IOMAP_H
3#define LINUX_IOMAP_H 1
4
9dc55f13
CH
5#include <linux/atomic.h>
6#include <linux/bitmap.h>
598ecfba 7#include <linux/blk_types.h>
9dc55f13 8#include <linux/mm.h>
199a31c6 9#include <linux/types.h>
5780a02f 10#include <linux/mm_types.h>
db074436 11#include <linux/blkdev.h>
199a31c6 12
89eb1906 13struct address_space;
8be9f564 14struct fiemap_extent_info;
ae259a9c 15struct inode;
c3d4ed1a 16struct iomap_dio;
598ecfba 17struct iomap_writepage_ctx;
ae259a9c
CH
18struct iov_iter;
19struct kiocb;
63899c6f 20struct page;
ae259a9c
CH
21struct vm_area_struct;
22struct vm_fault;
23
24/*
25 * Types of block ranges for iomap mappings:
26 */
eb81cf9d
CH
27#define IOMAP_HOLE 0 /* no blocks allocated, need allocation */
28#define IOMAP_DELALLOC 1 /* delayed allocation blocks */
29#define IOMAP_MAPPED 2 /* blocks allocated at @addr */
30#define IOMAP_UNWRITTEN 3 /* blocks allocated at @addr in unwritten state */
31#define IOMAP_INLINE 4 /* data inline in the inode */
199a31c6 32
17de0a9f 33/*
65a60e86
CH
34 * Flags reported by the file system from iomap_begin:
35 *
36 * IOMAP_F_NEW indicates that the blocks have been newly allocated and need
37 * zeroing for areas that no data is copied to.
a3841f94 38 *
caa51d26
JK
39 * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
40 * written data and requires fdatasync to commit them to persistent storage.
7684e2c4
DC
41 * This needs to take into account metadata changes that *may* be made at IO
42 * completion, such as file size updates from direct IO.
65a60e86
CH
43 *
44 * IOMAP_F_SHARED indicates that the blocks are shared, and will need to be
45 * unshared as part a write.
46 *
47 * IOMAP_F_MERGED indicates that the iomap contains the merge of multiple block
48 * mappings.
49 *
50 * IOMAP_F_BUFFER_HEAD indicates that the file system requires the use of
51 * buffer heads for this mapping.
17de0a9f 52 */
65a60e86
CH
53#define IOMAP_F_NEW 0x01
54#define IOMAP_F_DIRTY 0x02
55#define IOMAP_F_SHARED 0x04
56#define IOMAP_F_MERGED 0x08
57#define IOMAP_F_BUFFER_HEAD 0x10
c3b0e880 58#define IOMAP_F_ZONE_APPEND 0x20
d33fd776
CH
59
60/*
65a60e86
CH
61 * Flags set by the core iomap code during operations:
62 *
63 * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
64 * has changed as the result of this write operation.
d33fd776 65 */
65a60e86 66#define IOMAP_F_SIZE_CHANGED 0x100
17de0a9f 67
7ee66c03
CH
68/*
69 * Flags from 0x1000 up are for file system specific usage:
70 */
71#define IOMAP_F_PRIVATE 0x1000
72
73
ae259a9c 74/*
19fe5f64 75 * Magic value for addr:
ae259a9c 76 */
19fe5f64 77#define IOMAP_NULL_ADDR -1ULL /* addr is not valid */
199a31c6 78
df0db3ec
AG
79struct iomap_page_ops;
80
199a31c6 81struct iomap {
19fe5f64 82 u64 addr; /* disk offset of mapping, bytes */
ae259a9c
CH
83 loff_t offset; /* file offset of mapping, bytes */
84 u64 length; /* length of mapping, bytes */
17de0a9f
CH
85 u16 type; /* type of mapping */
86 u16 flags; /* flags for mapping */
ae259a9c 87 struct block_device *bdev; /* block device for I/O */
fa5d932c 88 struct dax_device *dax_dev; /* dax_dev for dax operations */
19e0c58f 89 void *inline_data;
e184fde6 90 void *private; /* filesystem private */
df0db3ec
AG
91 const struct iomap_page_ops *page_ops;
92};
63899c6f 93
66b8165e 94static inline sector_t iomap_sector(const struct iomap *iomap, loff_t pos)
db074436
DW
95{
96 return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
97}
98
69f4a26c
GX
99/*
100 * Returns the inline data pointer for logical offset @pos.
101 */
4495c33e 102static inline void *iomap_inline_data(const struct iomap *iomap, loff_t pos)
69f4a26c
GX
103{
104 return iomap->inline_data + pos - iomap->offset;
105}
106
107/*
108 * Check if the mapping's length is within the valid range for inline data.
109 * This is used to guard against accessing data beyond the page inline_data
110 * points at.
111 */
e3c4ffb0 112static inline bool iomap_inline_data_valid(const struct iomap *iomap)
69f4a26c
GX
113{
114 return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
115}
116
df0db3ec
AG
117/*
118 * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
119 * and page_done will be called for each page written to. This only applies to
120 * buffered writes as unbuffered writes will not typically have pages
121 * associated with them.
122 *
123 * When page_prepare succeeds, page_done will always be called to do any
124 * cleanup work necessary. In that page_done call, @page will be NULL if the
125 * associated page could not be obtained.
126 */
127struct iomap_page_ops {
1d25d0ae 128 int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len);
63899c6f 129 void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
1d25d0ae 130 struct page *page);
ae259a9c
CH
131};
132
133/*
134 * Flags for iomap_begin / iomap_end. No flag implies a read.
135 */
d33fd776
CH
136#define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
137#define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
138#define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
9484ab1b 139#define IOMAP_FAULT (1 << 3) /* mapping for page fault */
ff6a9292 140#define IOMAP_DIRECT (1 << 4) /* direct I/O */
9ecac0ef 141#define IOMAP_NOWAIT (1 << 5) /* do not block */
213f6271 142#define IOMAP_OVERWRITE_ONLY (1 << 6) /* only pure overwrites allowed */
b74b1293 143#define IOMAP_UNSHARE (1 << 7) /* unshare_file_range */
ae259a9c
CH
144
145struct iomap_ops {
146 /*
147 * Return the existing mapping at pos, or reserve space starting at
148 * pos for up to length, as long as we can do it as a single mapping.
149 * The actual length is returned in iomap->length.
150 */
151 int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
c039b997
GR
152 unsigned flags, struct iomap *iomap,
153 struct iomap *srcmap);
ae259a9c
CH
154
155 /*
156 * Commit and/or unreserve space previous allocated using iomap_begin.
157 * Written indicates the length of the successful write operation which
158 * needs to be commited, while the rest needs to be unreserved.
159 * Written might be zero if no data was written.
160 */
161 int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
162 ssize_t written, unsigned flags, struct iomap *iomap);
199a31c6
CH
163};
164
f4b896c2
CH
165/**
166 * struct iomap_iter - Iterate through a range of a file
167 * @inode: Set at the start of the iteration and should not change.
168 * @pos: The current file position we are operating on. It is updated by
169 * calls to iomap_iter(). Treat as read-only in the body.
170 * @len: The remaining length of the file segment we're operating on.
171 * It is updated at the same time as @pos.
172 * @processed: The number of bytes processed by the body in the most recent
173 * iteration, or a negative errno. 0 causes the iteration to stop.
174 * @flags: Zero or more of the iomap_begin flags above.
175 * @iomap: Map describing the I/O iteration
176 * @srcmap: Source map for COW operations
177 */
178struct iomap_iter {
179 struct inode *inode;
180 loff_t pos;
181 u64 len;
182 s64 processed;
183 unsigned flags;
184 struct iomap iomap;
185 struct iomap srcmap;
186};
187
188int iomap_iter(struct iomap_iter *iter, const struct iomap_ops *ops);
189
190/**
191 * iomap_length - length of the current iomap iteration
192 * @iter: iteration structure
193 *
194 * Returns the length that the operation applies to for the current iteration.
195 */
196static inline u64 iomap_length(const struct iomap_iter *iter)
197{
198 u64 end = iter->iomap.offset + iter->iomap.length;
199
200 if (iter->srcmap.type != IOMAP_HOLE)
201 end = min(end, iter->srcmap.offset + iter->srcmap.length);
202 return min(iter->len, end - iter->pos);
203}
204
205/**
206 * iomap_iter_srcmap - return the source map for the current iomap iteration
207 * @i: iteration structure
208 *
209 * Write operations on file systems with reflink support might require a
210 * source and a destination map. This function retourns the source map
211 * for a given operation, which may or may no be identical to the destination
212 * map in &i->iomap.
213 */
fad0a1ab 214static inline const struct iomap *iomap_iter_srcmap(const struct iomap_iter *i)
f4b896c2
CH
215{
216 if (i->srcmap.type != IOMAP_HOLE)
217 return &i->srcmap;
218 return &i->iomap;
219}
220
ae259a9c 221ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
8ff6daa1 222 const struct iomap_ops *ops);
72b4daa2 223int iomap_readpage(struct page *page, const struct iomap_ops *ops);
9d24a13a 224void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
9dc55f13
CH
225int iomap_is_partially_uptodate(struct page *page, unsigned long from,
226 unsigned long count);
227int iomap_releasepage(struct page *page, gfp_t gfp_mask);
228void iomap_invalidatepage(struct page *page, unsigned int offset,
229 unsigned int len);
230#ifdef CONFIG_MIGRATION
231int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
232 struct page *page, enum migrate_mode mode);
233#else
234#define iomap_migrate_page NULL
235#endif
3590c4d8 236int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
8ff6daa1 237 const struct iomap_ops *ops);
ae259a9c 238int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
8ff6daa1 239 bool *did_zero, const struct iomap_ops *ops);
ae259a9c 240int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
8ff6daa1 241 const struct iomap_ops *ops);
5780a02f
SJ
242vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
243 const struct iomap_ops *ops);
8be9f564 244int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
27328818 245 u64 start, u64 len, const struct iomap_ops *ops);
0ed3b0d4
AG
246loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
247 const struct iomap_ops *ops);
248loff_t iomap_seek_data(struct inode *inode, loff_t offset,
249 const struct iomap_ops *ops);
89eb1906
CH
250sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
251 const struct iomap_ops *ops);
ae259a9c 252
598ecfba
CH
253/*
254 * Structure for writeback I/O completions.
255 */
256struct iomap_ioend {
257 struct list_head io_list; /* next ioend in chain */
258 u16 io_type;
259 u16 io_flags; /* IOMAP_F_* */
260 struct inode *io_inode; /* file being written to */
261 size_t io_size; /* size of the extent */
262 loff_t io_offset; /* offset in the file */
598ecfba
CH
263 struct bio *io_bio; /* bio being built */
264 struct bio io_inline_bio; /* MUST BE LAST! */
265};
266
267struct iomap_writeback_ops {
268 /*
269 * Required, maps the blocks so that writeback can be performed on
270 * the range starting at offset.
271 */
272 int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
273 loff_t offset);
274
275 /*
276 * Optional, allows the file systems to perform actions just before
277 * submitting the bio and/or override the bio end_io handler for complex
278 * operations like copy on write extent manipulation or unwritten extent
279 * conversions.
280 */
281 int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
282
283 /*
284 * Optional, allows the file system to discard state on a page where
285 * we failed to submit any I/O.
286 */
763e4cdc 287 void (*discard_page)(struct page *page, loff_t fileoff);
598ecfba
CH
288};
289
290struct iomap_writepage_ctx {
291 struct iomap iomap;
292 struct iomap_ioend *ioend;
293 const struct iomap_writeback_ops *ops;
294};
295
296void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
297void iomap_ioend_try_merge(struct iomap_ioend *ioend,
6e552494 298 struct list_head *more_ioends);
598ecfba
CH
299void iomap_sort_ioends(struct list_head *ioend_list);
300int iomap_writepage(struct page *page, struct writeback_control *wbc,
301 struct iomap_writepage_ctx *wpc,
302 const struct iomap_writeback_ops *ops);
303int iomap_writepages(struct address_space *mapping,
304 struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
305 const struct iomap_writeback_ops *ops);
306
ff6a9292
CH
307/*
308 * Flags for direct I/O ->end_io:
309 */
310#define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
311#define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
838c4f3d
CH
312
313struct iomap_dio_ops {
314 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
315 unsigned flags);
3e08773c
CH
316 void (*submit_io)(const struct iomap_iter *iter, struct bio *bio,
317 loff_t file_offset);
838c4f3d
CH
318};
319
2f632965
CH
320/*
321 * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
322 * synchronous.
323 */
324#define IOMAP_DIO_FORCE_WAIT (1 << 0)
325
213f6271
CH
326/*
327 * Do not allocate blocks or zero partial blocks, but instead fall back to
328 * the caller by returning -EAGAIN. Used to optimize direct I/O writes that
329 * are not aligned to the file system block size.
330 */
331#define IOMAP_DIO_OVERWRITE_ONLY (1 << 1)
332
97308f8b
AG
333/*
334 * When a page fault occurs, return a partial synchronous result and allow
335 * the caller to retry the rest of the operation after dealing with the page
336 * fault.
337 */
338#define IOMAP_DIO_PARTIAL (1 << 2)
339
ff6a9292 340ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
13ef9544 341 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
4fdccaa0 342 unsigned int dio_flags, size_t done_before);
c3d4ed1a
CH
343struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
344 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
4fdccaa0 345 unsigned int dio_flags, size_t done_before);
c3d4ed1a 346ssize_t iomap_dio_complete(struct iomap_dio *dio);
ff6a9292 347
67482129
DW
348#ifdef CONFIG_SWAP
349struct file;
350struct swap_info_struct;
351
352int iomap_swapfile_activate(struct swap_info_struct *sis,
353 struct file *swap_file, sector_t *pagespan,
354 const struct iomap_ops *ops);
355#else
356# define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
357#endif /* CONFIG_SWAP */
358
199a31c6 359#endif /* LINUX_IOMAP_H */