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