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_dio;
17 struct iomap_writepage_ctx;
18 struct iov_iter;
19 struct kiocb;
20 struct page;
21 struct vm_area_struct;
22 struct vm_fault;
23 
24 /*
25  * Types of block ranges for iomap mappings:
26  */
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 */
32 
33 /*
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.
38  *
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.
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.
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.
52  */
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
58 
59 /*
60  * Flags set by the core iomap code during operations:
61  *
62  * IOMAP_F_SIZE_CHANGED indicates to the iomap_end method that the file size
63  * has changed as the result of this write operation.
64  */
65 #define IOMAP_F_SIZE_CHANGED	0x100
66 
67 /*
68  * Flags from 0x1000 up are for file system specific usage:
69  */
70 #define IOMAP_F_PRIVATE		0x1000
71 
72 
73 /*
74  * Magic value for addr:
75  */
76 #define IOMAP_NULL_ADDR -1ULL	/* addr is not valid */
77 
78 struct iomap_page_ops;
79 
80 struct iomap {
81 	u64			addr; /* disk offset of mapping, bytes */
82 	loff_t			offset;	/* file offset of mapping, bytes */
83 	u64			length;	/* length of mapping, bytes */
84 	u16			type;	/* type of mapping */
85 	u16			flags;	/* flags for mapping */
86 	struct block_device	*bdev;	/* block device for I/O */
87 	struct dax_device	*dax_dev; /* dax_dev for dax operations */
88 	void			*inline_data;
89 	void			*private; /* filesystem private */
90 	const struct iomap_page_ops *page_ops;
91 };
92 
93 static inline sector_t
iomap_sector(struct iomap * iomap,loff_t pos)94 iomap_sector(struct iomap *iomap, loff_t pos)
95 {
96 	return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
97 }
98 
99 /*
100  * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
101  * and page_done will be called for each page written to.  This only applies to
102  * buffered writes as unbuffered writes will not typically have pages
103  * associated with them.
104  *
105  * When page_prepare succeeds, page_done will always be called to do any
106  * cleanup work necessary.  In that page_done call, @page will be NULL if the
107  * associated page could not be obtained.
108  */
109 struct iomap_page_ops {
110 	int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len,
111 			struct iomap *iomap);
112 	void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
113 			struct page *page, struct iomap *iomap);
114 };
115 
116 /*
117  * Flags for iomap_begin / iomap_end.  No flag implies a read.
118  */
119 #define IOMAP_WRITE		(1 << 0) /* writing, must allocate blocks */
120 #define IOMAP_ZERO		(1 << 1) /* zeroing operation, may skip holes */
121 #define IOMAP_REPORT		(1 << 2) /* report extent status, e.g. FIEMAP */
122 #define IOMAP_FAULT		(1 << 3) /* mapping for page fault */
123 #define IOMAP_DIRECT		(1 << 4) /* direct I/O */
124 #define IOMAP_NOWAIT		(1 << 5) /* do not block */
125 
126 struct iomap_ops {
127 	/*
128 	 * Return the existing mapping at pos, or reserve space starting at
129 	 * pos for up to length, as long as we can do it as a single mapping.
130 	 * The actual length is returned in iomap->length.
131 	 */
132 	int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
133 			unsigned flags, struct iomap *iomap,
134 			struct iomap *srcmap);
135 
136 	/*
137 	 * Commit and/or unreserve space previous allocated using iomap_begin.
138 	 * Written indicates the length of the successful write operation which
139 	 * needs to be commited, while the rest needs to be unreserved.
140 	 * Written might be zero if no data was written.
141 	 */
142 	int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
143 			ssize_t written, unsigned flags, struct iomap *iomap);
144 };
145 
146 /*
147  * Main iomap iterator function.
148  */
149 typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len,
150 		void *data, struct iomap *iomap, struct iomap *srcmap);
151 
152 loff_t iomap_apply(struct inode *inode, loff_t pos, loff_t length,
153 		unsigned flags, const struct iomap_ops *ops, void *data,
154 		iomap_actor_t actor);
155 
156 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
157 		const struct iomap_ops *ops);
158 int iomap_readpage(struct page *page, const struct iomap_ops *ops);
159 void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
160 int iomap_set_page_dirty(struct page *page);
161 int iomap_is_partially_uptodate(struct page *page, unsigned long from,
162 		unsigned long count);
163 int iomap_releasepage(struct page *page, gfp_t gfp_mask);
164 void iomap_invalidatepage(struct page *page, unsigned int offset,
165 		unsigned int len);
166 #ifdef CONFIG_MIGRATION
167 int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
168 		struct page *page, enum migrate_mode mode);
169 #else
170 #define iomap_migrate_page NULL
171 #endif
172 int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
173 		const struct iomap_ops *ops);
174 int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
175 		bool *did_zero, const struct iomap_ops *ops);
176 int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
177 		const struct iomap_ops *ops);
178 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
179 			const struct iomap_ops *ops);
180 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
181 		u64 start, u64 len, const struct iomap_ops *ops);
182 loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
183 		const struct iomap_ops *ops);
184 loff_t iomap_seek_data(struct inode *inode, loff_t offset,
185 		const struct iomap_ops *ops);
186 sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
187 		const struct iomap_ops *ops);
188 
189 /*
190  * Structure for writeback I/O completions.
191  */
192 struct iomap_ioend {
193 	struct list_head	io_list;	/* next ioend in chain */
194 	u16			io_type;
195 	u16			io_flags;	/* IOMAP_F_* */
196 	struct inode		*io_inode;	/* file being written to */
197 	size_t			io_size;	/* size of the extent */
198 	loff_t			io_offset;	/* offset in the file */
199 	void			*io_private;	/* file system private data */
200 	struct bio		*io_bio;	/* bio being built */
201 	struct bio		io_inline_bio;	/* MUST BE LAST! */
202 };
203 
204 struct iomap_writeback_ops {
205 	/*
206 	 * Required, maps the blocks so that writeback can be performed on
207 	 * the range starting at offset.
208 	 */
209 	int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
210 				loff_t offset);
211 
212 	/*
213 	 * Optional, allows the file systems to perform actions just before
214 	 * submitting the bio and/or override the bio end_io handler for complex
215 	 * operations like copy on write extent manipulation or unwritten extent
216 	 * conversions.
217 	 */
218 	int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
219 
220 	/*
221 	 * Optional, allows the file system to discard state on a page where
222 	 * we failed to submit any I/O.
223 	 */
224 	void (*discard_page)(struct page *page, loff_t fileoff);
225 };
226 
227 struct iomap_writepage_ctx {
228 	struct iomap		iomap;
229 	struct iomap_ioend	*ioend;
230 	const struct iomap_writeback_ops *ops;
231 };
232 
233 void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
234 void iomap_ioend_try_merge(struct iomap_ioend *ioend,
235 		struct list_head *more_ioends,
236 		void (*merge_private)(struct iomap_ioend *ioend,
237 				struct iomap_ioend *next));
238 void iomap_sort_ioends(struct list_head *ioend_list);
239 int iomap_writepage(struct page *page, struct writeback_control *wbc,
240 		struct iomap_writepage_ctx *wpc,
241 		const struct iomap_writeback_ops *ops);
242 int iomap_writepages(struct address_space *mapping,
243 		struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
244 		const struct iomap_writeback_ops *ops);
245 
246 /*
247  * Flags for direct I/O ->end_io:
248  */
249 #define IOMAP_DIO_UNWRITTEN	(1 << 0)	/* covers unwritten extent(s) */
250 #define IOMAP_DIO_COW		(1 << 1)	/* covers COW extent(s) */
251 
252 struct iomap_dio_ops {
253 	int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
254 		      unsigned flags);
255 	blk_qc_t (*submit_io)(struct inode *inode, struct iomap *iomap,
256 			struct bio *bio, loff_t file_offset);
257 };
258 
259 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
260 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
261 		bool wait_for_completion);
262 struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
263 		const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
264 		bool wait_for_completion);
265 ssize_t iomap_dio_complete(struct iomap_dio *dio);
266 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
267 
268 #ifdef CONFIG_SWAP
269 struct file;
270 struct swap_info_struct;
271 
272 int iomap_swapfile_activate(struct swap_info_struct *sis,
273 		struct file *swap_file, sector_t *pagespan,
274 		const struct iomap_ops *ops);
275 #else
276 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops)	(-EIO)
277 #endif /* CONFIG_SWAP */
278 
279 #endif /* LINUX_IOMAP_H */
280