blob: b8ec145b2975c1e9c348fe3dc446f9a66ce03e8b [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Christoph Hellwig199a31c2016-06-21 09:22:39 +10002#ifndef LINUX_IOMAP_H
3#define LINUX_IOMAP_H 1
4
Christoph Hellwig9dc55f12018-07-11 22:26:05 -07005#include <linux/atomic.h>
6#include <linux/bitmap.h>
Christoph Hellwig598ecfb2019-10-17 13:12:15 -07007#include <linux/blk_types.h>
Christoph Hellwig9dc55f12018-07-11 22:26:05 -07008#include <linux/mm.h>
Christoph Hellwig199a31c2016-06-21 09:22:39 +10009#include <linux/types.h>
Souptick Joarder5780a022018-10-26 15:02:59 -070010#include <linux/mm_types.h>
Darrick J. Wongdb074432019-07-15 08:50:59 -070011#include <linux/blkdev.h>
Christoph Hellwig199a31c2016-06-21 09:22:39 +100012
Christoph Hellwig89eb1902018-06-01 09:03:08 -070013struct address_space;
Christoph Hellwig8be9f562016-06-21 09:38:45 +100014struct fiemap_extent_info;
Christoph Hellwigae259a92016-06-21 09:23:11 +100015struct inode;
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -070016struct iomap_dio;
Christoph Hellwig598ecfb2019-10-17 13:12:15 -070017struct iomap_writepage_ctx;
Christoph Hellwigae259a92016-06-21 09:23:11 +100018struct iov_iter;
19struct kiocb;
Christoph Hellwig63899c62018-06-19 15:10:56 -070020struct page;
Christoph Hellwigae259a92016-06-21 09:23:11 +100021struct vm_area_struct;
22struct vm_fault;
23
24/*
25 * Types of block ranges for iomap mappings:
26 */
Christoph Hellwigeb81cf92019-10-18 16:43:08 -070027#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 */
Christoph Hellwig199a31c2016-06-21 09:22:39 +100032
Christoph Hellwigae259a92016-06-21 09:23:11 +100033/*
Christoph Hellwig65a60e82019-10-18 16:40:17 -070034 * 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.
Linus Torvaldsa3841f92017-11-17 09:51:57 -080038 *
Jan Karacaa51d22017-11-01 16:36:42 +010039 * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
40 * written data and requires fdatasync to commit them to persistent storage.
Dave Chinner7684e2c2019-10-17 13:12:01 -070041 * This needs to take into account metadata changes that *may* be made at IO
42 * completion, such as file size updates from direct IO.
Christoph Hellwig65a60e82019-10-18 16:40:17 -070043 *
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.
Christoph Hellwig17de0a92016-08-29 11:33:58 +100052 */
Christoph Hellwig65a60e82019-10-18 16:40:17 -070053#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
Naohiro Aotac3b0e882021-02-04 19:21:41 +090058#define IOMAP_F_ZONE_APPEND 0x20
Christoph Hellwigd33fd772016-10-20 15:51:28 +110059
60/*
Christoph Hellwig65a60e82019-10-18 16:40:17 -070061 * 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.
Christoph Hellwigd33fd772016-10-20 15:51:28 +110065 */
Christoph Hellwig65a60e82019-10-18 16:40:17 -070066#define IOMAP_F_SIZE_CHANGED 0x100
Christoph Hellwig17de0a92016-08-29 11:33:58 +100067
68/*
Christoph Hellwig7ee66c02018-06-01 09:03:07 -070069 * Flags from 0x1000 up are for file system specific usage:
70 */
71#define IOMAP_F_PRIVATE 0x1000
72
73
74/*
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040075 * Magic value for addr:
Christoph Hellwigae259a92016-06-21 09:23:11 +100076 */
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040077#define IOMAP_NULL_ADDR -1ULL /* addr is not valid */
Christoph Hellwig199a31c2016-06-21 09:22:39 +100078
Andreas Gruenbacherdf0db3e2019-04-30 08:45:34 -070079struct iomap_page_ops;
80
Christoph Hellwig199a31c2016-06-21 09:22:39 +100081struct iomap {
Andreas Gruenbacher19fe5f62017-10-01 17:55:54 -040082 u64 addr; /* disk offset of mapping, bytes */
Christoph Hellwigae259a92016-06-21 09:23:11 +100083 loff_t offset; /* file offset of mapping, bytes */
84 u64 length; /* length of mapping, bytes */
Christoph Hellwig17de0a92016-08-29 11:33:58 +100085 u16 type; /* type of mapping */
86 u16 flags; /* flags for mapping */
Christoph Hellwigae259a92016-06-21 09:23:11 +100087 struct block_device *bdev; /* block device for I/O */
Dan Williamsfa5d9322017-01-27 12:04:59 -080088 struct dax_device *dax_dev; /* dax_dev for dax operations */
Andreas Gruenbacher19e0c582018-06-19 15:10:56 -070089 void *inline_data;
Andreas Gruenbachere184fde2018-06-19 15:10:57 -070090 void *private; /* filesystem private */
Andreas Gruenbacherdf0db3e2019-04-30 08:45:34 -070091 const struct iomap_page_ops *page_ops;
92};
Christoph Hellwig63899c62018-06-19 15:10:56 -070093
Darrick J. Wongdb074432019-07-15 08:50:59 -070094static inline sector_t
95iomap_sector(struct iomap *iomap, loff_t pos)
96{
97 return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
98}
99
Andreas Gruenbacherdf0db3e2019-04-30 08:45:34 -0700100/*
Gao Xiang69f4a262021-08-03 09:38:22 -0700101 * Returns the inline data pointer for logical offset @pos.
102 */
103static inline void *iomap_inline_data(struct iomap *iomap, loff_t pos)
104{
105 return iomap->inline_data + pos - iomap->offset;
106}
107
108/*
109 * Check if the mapping's length is within the valid range for inline data.
110 * This is used to guard against accessing data beyond the page inline_data
111 * points at.
112 */
113static inline bool iomap_inline_data_valid(struct iomap *iomap)
114{
115 return iomap->length <= PAGE_SIZE - offset_in_page(iomap->inline_data);
116}
117
118/*
Andreas Gruenbacherdf0db3e2019-04-30 08:45:34 -0700119 * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
120 * and page_done will be called for each page written to. This only applies to
121 * buffered writes as unbuffered writes will not typically have pages
122 * associated with them.
123 *
124 * When page_prepare succeeds, page_done will always be called to do any
125 * cleanup work necessary. In that page_done call, @page will be NULL if the
126 * associated page could not be obtained.
127 */
128struct iomap_page_ops {
129 int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len,
130 struct iomap *iomap);
Christoph Hellwig63899c62018-06-19 15:10:56 -0700131 void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
132 struct page *page, struct iomap *iomap);
Christoph Hellwig199a31c2016-06-21 09:22:39 +1000133};
134
Christoph Hellwigae259a92016-06-21 09:23:11 +1000135/*
136 * Flags for iomap_begin / iomap_end. No flag implies a read.
137 */
Christoph Hellwigd33fd772016-10-20 15:51:28 +1100138#define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
139#define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
140#define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
Jan Kara9484ab12016-11-10 10:26:50 +1100141#define IOMAP_FAULT (1 << 3) /* mapping for page fault */
Christoph Hellwigff6a9292016-11-30 14:36:01 +1100142#define IOMAP_DIRECT (1 << 4) /* direct I/O */
Christoph Hellwig9ecac0e2018-06-01 09:03:07 -0700143#define IOMAP_NOWAIT (1 << 5) /* do not block */
Christoph Hellwig213f6272021-01-23 10:06:10 -0800144#define IOMAP_OVERWRITE_ONLY (1 << 6) /* only pure overwrites allowed */
Christoph Hellwigae259a92016-06-21 09:23:11 +1000145
146struct iomap_ops {
147 /*
148 * Return the existing mapping at pos, or reserve space starting at
149 * pos for up to length, as long as we can do it as a single mapping.
150 * The actual length is returned in iomap->length.
151 */
152 int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700153 unsigned flags, struct iomap *iomap,
154 struct iomap *srcmap);
Christoph Hellwigae259a92016-06-21 09:23:11 +1000155
156 /*
157 * Commit and/or unreserve space previous allocated using iomap_begin.
158 * Written indicates the length of the successful write operation which
159 * needs to be commited, while the rest needs to be unreserved.
160 * Written might be zero if no data was written.
161 */
162 int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
163 ssize_t written, unsigned flags, struct iomap *iomap);
164};
165
Christoph Hellwig9dc55f12018-07-11 22:26:05 -0700166/*
Darrick J. Wong5d9073072019-07-15 08:51:01 -0700167 * Main iomap iterator function.
168 */
169typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len,
Goldwyn Rodriguesc039b992019-10-18 16:44:10 -0700170 void *data, struct iomap *iomap, struct iomap *srcmap);
Darrick J. Wong5d9073072019-07-15 08:51:01 -0700171
172loff_t iomap_apply(struct inode *inode, loff_t pos, loff_t length,
173 unsigned flags, const struct iomap_ops *ops, void *data,
174 iomap_actor_t actor);
175
Christoph Hellwigae259a92016-06-21 09:23:11 +1000176ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800177 const struct iomap_ops *ops);
Christoph Hellwig72b4daa2018-06-19 15:10:57 -0700178int iomap_readpage(struct page *page, const struct iomap_ops *ops);
Matthew Wilcox (Oracle)9d24a132020-06-01 21:47:34 -0700179void iomap_readahead(struct readahead_control *, const struct iomap_ops *ops);
Christoph Hellwig9dc55f12018-07-11 22:26:05 -0700180int iomap_is_partially_uptodate(struct page *page, unsigned long from,
181 unsigned long count);
182int iomap_releasepage(struct page *page, gfp_t gfp_mask);
183void iomap_invalidatepage(struct page *page, unsigned int offset,
184 unsigned int len);
185#ifdef CONFIG_MIGRATION
186int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
187 struct page *page, enum migrate_mode mode);
188#else
189#define iomap_migrate_page NULL
190#endif
Christoph Hellwig3590c4d2019-10-18 16:41:34 -0700191int iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800192 const struct iomap_ops *ops);
Christoph Hellwigae259a92016-06-21 09:23:11 +1000193int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800194 bool *did_zero, const struct iomap_ops *ops);
Christoph Hellwigae259a92016-06-21 09:23:11 +1000195int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
Christoph Hellwig8ff6daa2017-01-27 23:20:26 -0800196 const struct iomap_ops *ops);
Souptick Joarder5780a022018-10-26 15:02:59 -0700197vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
198 const struct iomap_ops *ops);
Christoph Hellwig8be9f562016-06-21 09:38:45 +1000199int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
Christoph Hellwig27328812020-05-23 09:30:12 +0200200 u64 start, u64 len, const struct iomap_ops *ops);
Andreas Gruenbacher0ed3b0d2017-06-29 11:43:21 -0700201loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
202 const struct iomap_ops *ops);
203loff_t iomap_seek_data(struct inode *inode, loff_t offset,
204 const struct iomap_ops *ops);
Christoph Hellwig89eb1902018-06-01 09:03:08 -0700205sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
206 const struct iomap_ops *ops);
Christoph Hellwigae259a92016-06-21 09:23:11 +1000207
Christoph Hellwigff6a9292016-11-30 14:36:01 +1100208/*
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700209 * Structure for writeback I/O completions.
210 */
211struct iomap_ioend {
212 struct list_head io_list; /* next ioend in chain */
213 u16 io_type;
214 u16 io_flags; /* IOMAP_F_* */
215 struct inode *io_inode; /* file being written to */
216 size_t io_size; /* size of the extent */
217 loff_t io_offset; /* offset in the file */
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700218 struct bio *io_bio; /* bio being built */
219 struct bio io_inline_bio; /* MUST BE LAST! */
220};
221
222struct iomap_writeback_ops {
223 /*
224 * Required, maps the blocks so that writeback can be performed on
225 * the range starting at offset.
226 */
227 int (*map_blocks)(struct iomap_writepage_ctx *wpc, struct inode *inode,
228 loff_t offset);
229
230 /*
231 * Optional, allows the file systems to perform actions just before
232 * submitting the bio and/or override the bio end_io handler for complex
233 * operations like copy on write extent manipulation or unwritten extent
234 * conversions.
235 */
236 int (*prepare_ioend)(struct iomap_ioend *ioend, int status);
237
238 /*
239 * Optional, allows the file system to discard state on a page where
240 * we failed to submit any I/O.
241 */
Brian Foster763e4cd2020-10-29 14:30:48 -0700242 void (*discard_page)(struct page *page, loff_t fileoff);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700243};
244
245struct iomap_writepage_ctx {
246 struct iomap iomap;
247 struct iomap_ioend *ioend;
248 const struct iomap_writeback_ops *ops;
249};
250
251void iomap_finish_ioends(struct iomap_ioend *ioend, int error);
252void iomap_ioend_try_merge(struct iomap_ioend *ioend,
Brian Foster6e552492021-05-04 08:54:29 -0700253 struct list_head *more_ioends);
Christoph Hellwig598ecfb2019-10-17 13:12:15 -0700254void iomap_sort_ioends(struct list_head *ioend_list);
255int iomap_writepage(struct page *page, struct writeback_control *wbc,
256 struct iomap_writepage_ctx *wpc,
257 const struct iomap_writeback_ops *ops);
258int iomap_writepages(struct address_space *mapping,
259 struct writeback_control *wbc, struct iomap_writepage_ctx *wpc,
260 const struct iomap_writeback_ops *ops);
261
262/*
Christoph Hellwigff6a9292016-11-30 14:36:01 +1100263 * Flags for direct I/O ->end_io:
264 */
265#define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
266#define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
Christoph Hellwig838c4f32019-09-19 15:32:45 -0700267
268struct iomap_dio_ops {
269 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
270 unsigned flags);
Goldwyn Rodrigues8cecd0b2019-05-14 18:54:27 -0500271 blk_qc_t (*submit_io)(struct inode *inode, struct iomap *iomap,
272 struct bio *bio, loff_t file_offset);
Christoph Hellwig838c4f32019-09-19 15:32:45 -0700273};
274
Christoph Hellwig2f632962021-01-23 10:06:09 -0800275/*
276 * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not
277 * synchronous.
278 */
279#define IOMAP_DIO_FORCE_WAIT (1 << 0)
280
Christoph Hellwig213f6272021-01-23 10:06:10 -0800281/*
282 * Do not allocate blocks or zero partial blocks, but instead fall back to
283 * the caller by returning -EAGAIN. Used to optimize direct I/O writes that
284 * are not aligned to the file system block size.
285 */
286#define IOMAP_DIO_OVERWRITE_ONLY (1 << 1)
287
Christoph Hellwigff6a9292016-11-30 14:36:01 +1100288ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
Jan Kara13ef9542019-10-15 08:43:42 -0700289 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
Christoph Hellwig2f632962021-01-23 10:06:09 -0800290 unsigned int dio_flags);
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700291struct iomap_dio *__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
292 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
Christoph Hellwig2f632962021-01-23 10:06:09 -0800293 unsigned int dio_flags);
Christoph Hellwigc3d4ed12020-09-28 08:51:08 -0700294ssize_t iomap_dio_complete(struct iomap_dio *dio);
Christoph Hellwig81214ba2018-12-04 11:12:08 -0700295int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
Christoph Hellwigff6a9292016-11-30 14:36:01 +1100296
Darrick J. Wong674821292018-05-10 08:38:15 -0700297#ifdef CONFIG_SWAP
298struct file;
299struct swap_info_struct;
300
301int iomap_swapfile_activate(struct swap_info_struct *sis,
302 struct file *swap_file, sector_t *pagespan,
303 const struct iomap_ops *ops);
304#else
305# define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
306#endif /* CONFIG_SWAP */
307
Christoph Hellwig199a31c2016-06-21 09:22:39 +1000308#endif /* LINUX_IOMAP_H */