]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/iomap.h
mm: memcontrol: convert NR_ANON_THPS account to pages
[mirror_ubuntu-jammy-kernel.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
db074436
DW
94static inline sector_t
95iomap_sector(struct iomap *iomap, loff_t pos)
96{
97 return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
98}
99
df0db3ec
AG
100/*
101 * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
102 * and page_done will be called for each page written to. This only applies to
103 * buffered writes as unbuffered writes will not typically have pages
104 * associated with them.
105 *
106 * When page_prepare succeeds, page_done will always be called to do any
107 * cleanup work necessary. In that page_done call, @page will be NULL if the
108 * associated page could not be obtained.
109 */
110struct iomap_page_ops {
111 int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len,
112 struct iomap *iomap);
63899c6f
CH
113 void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
114 struct page *page, struct iomap *iomap);
ae259a9c
CH
115};
116
117/*
118 * Flags for iomap_begin / iomap_end. No flag implies a read.
119 */
d33fd776
CH
120#define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
121#define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
122#define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
9484ab1b 123#define IOMAP_FAULT (1 << 3) /* mapping for page fault */
ff6a9292 124#define IOMAP_DIRECT (1 << 4) /* direct I/O */
9ecac0ef 125#define IOMAP_NOWAIT (1 << 5) /* do not block */
213f6271 126#define IOMAP_OVERWRITE_ONLY (1 << 6) /* only pure overwrites allowed */
ae259a9c
CH
127
128struct iomap_ops {
129 /*
130 * Return the existing mapping at pos, or reserve space starting at
131 * pos for up to length, as long as we can do it as a single mapping.
132 * The actual length is returned in iomap->length.
133 */
134 int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
c039b997
GR
135 unsigned flags, struct iomap *iomap,
136 struct iomap *srcmap);
ae259a9c
CH
137
138 /*
139 * Commit and/or unreserve space previous allocated using iomap_begin.
140 * Written indicates the length of the successful write operation which
141 * needs to be commited, while the rest needs to be unreserved.
142 * Written might be zero if no data was written.
143 */
144 int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
145 ssize_t written, unsigned flags, struct iomap *iomap);
199a31c6
CH
146};
147
5d907307
DW
148/*
149 * Main iomap iterator function.
150 */
151typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len,
c039b997 152 void *data, struct iomap *iomap, struct iomap *srcmap);
5d907307
DW
153
154loff_t iomap_apply(struct inode *inode, loff_t pos, loff_t length,
155 unsigned flags, const struct iomap_ops *ops, void *data,
156 iomap_actor_t actor);
157
ae259a9c 158ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
8ff6daa1 159 const struct iomap_ops *ops);
72b4daa2 160int iomap_readpage(struct page *page, const struct iomap_ops *ops);
9d24a13a 161void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
c03cea42 162int iomap_set_page_dirty(struct page *page);
9dc55f13
CH
163int iomap_is_partially_uptodate(struct page *page, unsigned long from,
164 unsigned long count);
165int iomap_releasepage(struct page *page, gfp_t gfp_mask);
166void iomap_invalidatepage(struct page *page, unsigned int offset,
167 unsigned int len);
168#ifdef CONFIG_MIGRATION
169int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
170 struct page *page, enum migrate_mode mode);
171#else
172#define iomap_migrate_page NULL
173#endif
3590c4d8 174int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
8ff6daa1 175 const struct iomap_ops *ops);
ae259a9c 176int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
8ff6daa1 177 bool *did_zero, const struct iomap_ops *ops);
ae259a9c 178int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
8ff6daa1 179 const struct iomap_ops *ops);
5780a02f
SJ
180vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
181 const struct iomap_ops *ops);
8be9f564 182int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
27328818 183 u64 start, u64 len, const struct iomap_ops *ops);
0ed3b0d4
AG
184loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
185 const struct iomap_ops *ops);
186loff_t iomap_seek_data(struct inode *inode, loff_t offset,
187 const struct iomap_ops *ops);
89eb1906
CH
188sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
189 const struct iomap_ops *ops);
ae259a9c 190
598ecfba
CH
191/*
192 * Structure for writeback I/O completions.
193 */
194struct iomap_ioend {
195 struct list_head io_list; /* next ioend in chain */
196 u16 io_type;
197 u16 io_flags; /* IOMAP_F_* */
198 struct inode *io_inode; /* file being written to */
199 size_t io_size; /* size of the extent */
200 loff_t io_offset; /* offset in the file */
201 void *io_private; /* file system private data */
202 struct bio *io_bio; /* bio being built */
203 struct bio io_inline_bio; /* MUST BE LAST! */
204};
205
206struct iomap_writeback_ops {
207 /*
208 * Required, maps the blocks so that writeback can be performed on
209 * the range starting at offset.
210 */
211 int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
212 loff_t offset);
213
214 /*
215 * Optional, allows the file systems to perform actions just before
216 * submitting the bio and/or override the bio end_io handler for complex
217 * operations like copy on write extent manipulation or unwritten extent
218 * conversions.
219 */
220 int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
221
222 /*
223 * Optional, allows the file system to discard state on a page where
224 * we failed to submit any I/O.
225 */
763e4cdc 226 void (*discard_page)(struct page *page, loff_t fileoff);
598ecfba
CH
227};
228
229struct iomap_writepage_ctx {
230 struct iomap iomap;
231 struct iomap_ioend *ioend;
232 const struct iomap_writeback_ops *ops;
233};
234
235void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
236void iomap_ioend_try_merge(struct iomap_ioend *ioend,
237 struct list_head *more_ioends,
238 void (*merge_private)(struct iomap_ioend *ioend,
239 struct iomap_ioend *next));
240void iomap_sort_ioends(struct list_head *ioend_list);
241int iomap_writepage(struct page *page, struct writeback_control *wbc,
242 struct iomap_writepage_ctx *wpc,
243 const struct iomap_writeback_ops *ops);
244int iomap_writepages(struct address_space *mapping,
245 struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
246 const struct iomap_writeback_ops *ops);
247
ff6a9292
CH
248/*
249 * Flags for direct I/O ->end_io:
250 */
251#define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
252#define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
838c4f3d
CH
253
254struct iomap_dio_ops {
255 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
256 unsigned flags);
8cecd0ba
GR
257 blk_qc_t (*submit_io)(struct inode *inode, struct iomap *iomap,
258 struct bio *bio, loff_t file_offset);
838c4f3d
CH
259};
260
2f632965
CH
261/*
262 * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
263 * synchronous.
264 */
265#define IOMAP_DIO_FORCE_WAIT (1 << 0)
266
213f6271
CH
267/*
268 * Do not allocate blocks or zero partial blocks, but instead fall back to
269 * the caller by returning -EAGAIN. Used to optimize direct I/O writes that
270 * are not aligned to the file system block size.
271 */
272#define IOMAP_DIO_OVERWRITE_ONLY (1 << 1)
273
ff6a9292 274ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
13ef9544 275 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
2f632965 276 unsigned int dio_flags);
c3d4ed1a
CH
277struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
278 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
2f632965 279 unsigned int dio_flags);
c3d4ed1a 280ssize_t iomap_dio_complete(struct iomap_dio *dio);
81214bab 281int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
ff6a9292 282
67482129
DW
283#ifdef CONFIG_SWAP
284struct file;
285struct swap_info_struct;
286
287int iomap_swapfile_activate(struct swap_info_struct *sis,
288 struct file *swap_file, sector_t *pagespan,
289 const struct iomap_ops *ops);
290#else
291# define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
292#endif /* CONFIG_SWAP */
293
199a31c6 294#endif /* LINUX_IOMAP_H */