Merge tag 'for-linus' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[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;
598ecfba 16struct iomap_writepage_ctx;
ae259a9c
CH
17struct iov_iter;
18struct kiocb;
63899c6f 19struct page;
ae259a9c
CH
20struct vm_area_struct;
21struct vm_fault;
22
23/*
24 * Types of block ranges for iomap mappings:
25 */
eb81cf9d
CH
26#define IOMAP_HOLE 0 /* no blocks allocated, need allocation */
27#define IOMAP_DELALLOC 1 /* delayed allocation blocks */
28#define IOMAP_MAPPED 2 /* blocks allocated at @addr */
29#define IOMAP_UNWRITTEN 3 /* blocks allocated at @addr in unwritten state */
30#define IOMAP_INLINE 4 /* data inline in the inode */
199a31c6 31
17de0a9f 32/*
65a60e86
CH
33 * Flags reported by the file system from iomap_begin:
34 *
35 * IOMAP_F_NEW indicates that the blocks have been newly allocated and need
36 * zeroing for areas that no data is copied to.
a3841f94 37 *
caa51d26
JK
38 * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
39 * written data and requires fdatasync to commit them to persistent storage.
7684e2c4
DC
40 * This needs to take into account metadata changes that *may* be made at IO
41 * completion, such as file size updates from direct IO.
65a60e86
CH
42 *
43 * IOMAP_F_SHARED indicates that the blocks are shared, and will need to be
44 * unshared as part a write.
45 *
46 * IOMAP_F_MERGED indicates that the iomap contains the merge of multiple block
47 * mappings.
48 *
49 * IOMAP_F_BUFFER_HEAD indicates that the file system requires the use of
50 * buffer heads for this mapping.
17de0a9f 51 */
65a60e86
CH
52#define IOMAP_F_NEW 0x01
53#define IOMAP_F_DIRTY 0x02
54#define IOMAP_F_SHARED 0x04
55#define IOMAP_F_MERGED 0x08
56#define IOMAP_F_BUFFER_HEAD 0x10
d33fd776
CH
57
58/*
65a60e86
CH
59 * Flags set by the core iomap code during operations:
60 *
61 * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
62 * has changed as the result of this write operation.
d33fd776 63 */
65a60e86 64#define IOMAP_F_SIZE_CHANGED 0x100
17de0a9f 65
7ee66c03
CH
66/*
67 * Flags from 0x1000 up are for file system specific usage:
68 */
69#define IOMAP_F_PRIVATE 0x1000
70
71
ae259a9c 72/*
19fe5f64 73 * Magic value for addr:
ae259a9c 74 */
19fe5f64 75#define IOMAP_NULL_ADDR -1ULL /* addr is not valid */
199a31c6 76
df0db3ec
AG
77struct iomap_page_ops;
78
199a31c6 79struct iomap {
19fe5f64 80 u64 addr; /* disk offset of mapping, bytes */
ae259a9c
CH
81 loff_t offset; /* file offset of mapping, bytes */
82 u64 length; /* length of mapping, bytes */
17de0a9f
CH
83 u16 type; /* type of mapping */
84 u16 flags; /* flags for mapping */
ae259a9c 85 struct block_device *bdev; /* block device for I/O */
fa5d932c 86 struct dax_device *dax_dev; /* dax_dev for dax operations */
19e0c58f 87 void *inline_data;
e184fde6 88 void *private; /* filesystem private */
df0db3ec
AG
89 const struct iomap_page_ops *page_ops;
90};
63899c6f 91
db074436
DW
92static inline sector_t
93iomap_sector(struct iomap *iomap, loff_t pos)
94{
95 return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
96}
97
df0db3ec
AG
98/*
99 * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
100 * and page_done will be called for each page written to. This only applies to
101 * buffered writes as unbuffered writes will not typically have pages
102 * associated with them.
103 *
104 * When page_prepare succeeds, page_done will always be called to do any
105 * cleanup work necessary. In that page_done call, @page will be NULL if the
106 * associated page could not be obtained.
107 */
108struct iomap_page_ops {
109 int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len,
110 struct iomap *iomap);
63899c6f
CH
111 void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
112 struct page *page, struct iomap *iomap);
ae259a9c
CH
113};
114
115/*
116 * Flags for iomap_begin / iomap_end. No flag implies a read.
117 */
d33fd776
CH
118#define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
119#define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
120#define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
9484ab1b 121#define IOMAP_FAULT (1 << 3) /* mapping for page fault */
ff6a9292 122#define IOMAP_DIRECT (1 << 4) /* direct I/O */
9ecac0ef 123#define IOMAP_NOWAIT (1 << 5) /* do not block */
ae259a9c
CH
124
125struct iomap_ops {
126 /*
127 * Return the existing mapping at pos, or reserve space starting at
128 * pos for up to length, as long as we can do it as a single mapping.
129 * The actual length is returned in iomap->length.
130 */
131 int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
c039b997
GR
132 unsigned flags, struct iomap *iomap,
133 struct iomap *srcmap);
ae259a9c
CH
134
135 /*
136 * Commit and/or unreserve space previous allocated using iomap_begin.
137 * Written indicates the length of the successful write operation which
138 * needs to be commited, while the rest needs to be unreserved.
139 * Written might be zero if no data was written.
140 */
141 int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
142 ssize_t written, unsigned flags, struct iomap *iomap);
199a31c6
CH
143};
144
5d907307
DW
145/*
146 * Main iomap iterator function.
147 */
148typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len,
c039b997 149 void *data, struct iomap *iomap, struct iomap *srcmap);
5d907307
DW
150
151loff_t iomap_apply(struct inode *inode, loff_t pos, loff_t length,
152 unsigned flags, const struct iomap_ops *ops, void *data,
153 iomap_actor_t actor);
154
ae259a9c 155ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
8ff6daa1 156 const struct iomap_ops *ops);
72b4daa2 157int iomap_readpage(struct page *page, const struct iomap_ops *ops);
9d24a13a 158void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
c03cea42 159int iomap_set_page_dirty(struct page *page);
9dc55f13
CH
160int iomap_is_partially_uptodate(struct page *page, unsigned long from,
161 unsigned long count);
162int iomap_releasepage(struct page *page, gfp_t gfp_mask);
163void iomap_invalidatepage(struct page *page, unsigned int offset,
164 unsigned int len);
165#ifdef CONFIG_MIGRATION
166int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
167 struct page *page, enum migrate_mode mode);
168#else
169#define iomap_migrate_page NULL
170#endif
3590c4d8 171int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
8ff6daa1 172 const struct iomap_ops *ops);
ae259a9c 173int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
8ff6daa1 174 bool *did_zero, const struct iomap_ops *ops);
ae259a9c 175int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
8ff6daa1 176 const struct iomap_ops *ops);
5780a02f
SJ
177vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
178 const struct iomap_ops *ops);
8be9f564 179int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
27328818 180 u64 start, u64 len, const struct iomap_ops *ops);
0ed3b0d4
AG
181loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
182 const struct iomap_ops *ops);
183loff_t iomap_seek_data(struct inode *inode, loff_t offset,
184 const struct iomap_ops *ops);
89eb1906
CH
185sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
186 const struct iomap_ops *ops);
ae259a9c 187
598ecfba
CH
188/*
189 * Structure for writeback I/O completions.
190 */
191struct iomap_ioend {
192 struct list_head io_list; /* next ioend in chain */
193 u16 io_type;
194 u16 io_flags; /* IOMAP_F_* */
195 struct inode *io_inode; /* file being written to */
196 size_t io_size; /* size of the extent */
197 loff_t io_offset; /* offset in the file */
198 void *io_private; /* file system private data */
199 struct bio *io_bio; /* bio being built */
200 struct bio io_inline_bio; /* MUST BE LAST! */
201};
202
203struct iomap_writeback_ops {
204 /*
205 * Required, maps the blocks so that writeback can be performed on
206 * the range starting at offset.
207 */
208 int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
209 loff_t offset);
210
211 /*
212 * Optional, allows the file systems to perform actions just before
213 * submitting the bio and/or override the bio end_io handler for complex
214 * operations like copy on write extent manipulation or unwritten extent
215 * conversions.
216 */
217 int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
218
219 /*
220 * Optional, allows the file system to discard state on a page where
221 * we failed to submit any I/O.
222 */
223 void (*discard_page)(struct page *page);
224};
225
226struct iomap_writepage_ctx {
227 struct iomap iomap;
228 struct iomap_ioend *ioend;
229 const struct iomap_writeback_ops *ops;
230};
231
232void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
233void iomap_ioend_try_merge(struct iomap_ioend *ioend,
234 struct list_head *more_ioends,
235 void (*merge_private)(struct iomap_ioend *ioend,
236 struct iomap_ioend *next));
237void iomap_sort_ioends(struct list_head *ioend_list);
238int iomap_writepage(struct page *page, struct writeback_control *wbc,
239 struct iomap_writepage_ctx *wpc,
240 const struct iomap_writeback_ops *ops);
241int iomap_writepages(struct address_space *mapping,
242 struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
243 const struct iomap_writeback_ops *ops);
244
ff6a9292
CH
245/*
246 * Flags for direct I/O ->end_io:
247 */
248#define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
249#define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
838c4f3d
CH
250
251struct iomap_dio_ops {
252 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
253 unsigned flags);
8cecd0ba
GR
254 blk_qc_t (*submit_io)(struct inode *inode, struct iomap *iomap,
255 struct bio *bio, loff_t file_offset);
838c4f3d
CH
256};
257
ff6a9292 258ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
13ef9544
JK
259 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
260 bool wait_for_completion);
81214bab 261int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
ff6a9292 262
67482129
DW
263#ifdef CONFIG_SWAP
264struct file;
265struct swap_info_struct;
266
267int iomap_swapfile_activate(struct swap_info_struct *sis,
268 struct file *swap_file, sector_t *pagespan,
269 const struct iomap_ops *ops);
270#else
271# define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
272#endif /* CONFIG_SWAP */
273
199a31c6 274#endif /* LINUX_IOMAP_H */