xref: /linux-6.15/include/linux/iomap.h (revision ae4a0502)
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/mm.h>
8 #include <linux/types.h>
9 #include <linux/mm_types.h>
10 
11 struct address_space;
12 struct fiemap_extent_info;
13 struct inode;
14 struct iov_iter;
15 struct kiocb;
16 struct page;
17 struct vm_area_struct;
18 struct vm_fault;
19 
20 /*
21  * Types of block ranges for iomap mappings:
22  */
23 #define IOMAP_HOLE	0x01	/* no blocks allocated, need allocation */
24 #define IOMAP_DELALLOC	0x02	/* delayed allocation blocks */
25 #define IOMAP_MAPPED	0x03	/* blocks allocated at @addr */
26 #define IOMAP_UNWRITTEN	0x04	/* blocks allocated at @addr in unwritten state */
27 #define IOMAP_INLINE	0x05	/* data inline in the inode */
28 
29 /*
30  * Flags for all iomap mappings:
31  *
32  * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
33  * written data and requires fdatasync to commit them to persistent storage.
34  */
35 #define IOMAP_F_NEW		0x01	/* blocks have been newly allocated */
36 #define IOMAP_F_DIRTY		0x02	/* uncommitted metadata */
37 #define IOMAP_F_BUFFER_HEAD	0x04	/* file system requires buffer heads */
38 #define IOMAP_F_SIZE_CHANGED	0x08	/* file size has changed */
39 
40 /*
41  * Flags that only need to be reported for IOMAP_REPORT requests:
42  */
43 #define IOMAP_F_MERGED		0x10	/* contains multiple blocks/extents */
44 #define IOMAP_F_SHARED		0x20	/* block shared with another file */
45 
46 /*
47  * Flags from 0x1000 up are for file system specific usage:
48  */
49 #define IOMAP_F_PRIVATE		0x1000
50 
51 
52 /*
53  * Magic value for addr:
54  */
55 #define IOMAP_NULL_ADDR -1ULL	/* addr is not valid */
56 
57 struct iomap_page_ops;
58 
59 struct iomap {
60 	u64			addr; /* disk offset of mapping, bytes */
61 	loff_t			offset;	/* file offset of mapping, bytes */
62 	u64			length;	/* length of mapping, bytes */
63 	u16			type;	/* type of mapping */
64 	u16			flags;	/* flags for mapping */
65 	struct block_device	*bdev;	/* block device for I/O */
66 	struct dax_device	*dax_dev; /* dax_dev for dax operations */
67 	void			*inline_data;
68 	void			*private; /* filesystem private */
69 	const struct iomap_page_ops *page_ops;
70 };
71 
72 /*
73  * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
74  * and page_done will be called for each page written to.  This only applies to
75  * buffered writes as unbuffered writes will not typically have pages
76  * associated with them.
77  *
78  * When page_prepare succeeds, page_done will always be called to do any
79  * cleanup work necessary.  In that page_done call, @page will be NULL if the
80  * associated page could not be obtained.
81  */
82 struct iomap_page_ops {
83 	int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len,
84 			struct iomap *iomap);
85 	void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
86 			struct page *page, struct iomap *iomap);
87 };
88 
89 /*
90  * Flags for iomap_begin / iomap_end.  No flag implies a read.
91  */
92 #define IOMAP_WRITE		(1 << 0) /* writing, must allocate blocks */
93 #define IOMAP_ZERO		(1 << 1) /* zeroing operation, may skip holes */
94 #define IOMAP_REPORT		(1 << 2) /* report extent status, e.g. FIEMAP */
95 #define IOMAP_FAULT		(1 << 3) /* mapping for page fault */
96 #define IOMAP_DIRECT		(1 << 4) /* direct I/O */
97 #define IOMAP_NOWAIT		(1 << 5) /* do not block */
98 
99 struct iomap_ops {
100 	/*
101 	 * Return the existing mapping at pos, or reserve space starting at
102 	 * pos for up to length, as long as we can do it as a single mapping.
103 	 * The actual length is returned in iomap->length.
104 	 */
105 	int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
106 			unsigned flags, struct iomap *iomap);
107 
108 	/*
109 	 * Commit and/or unreserve space previous allocated using iomap_begin.
110 	 * Written indicates the length of the successful write operation which
111 	 * needs to be commited, while the rest needs to be unreserved.
112 	 * Written might be zero if no data was written.
113 	 */
114 	int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
115 			ssize_t written, unsigned flags, struct iomap *iomap);
116 };
117 
118 /*
119  * Structure allocate for each page when block size < PAGE_SIZE to track
120  * sub-page uptodate status and I/O completions.
121  */
122 struct iomap_page {
123 	atomic_t		read_count;
124 	atomic_t		write_count;
125 	DECLARE_BITMAP(uptodate, PAGE_SIZE / 512);
126 };
127 
128 static inline struct iomap_page *to_iomap_page(struct page *page)
129 {
130 	if (page_has_private(page))
131 		return (struct iomap_page *)page_private(page);
132 	return NULL;
133 }
134 
135 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
136 		const struct iomap_ops *ops);
137 int iomap_readpage(struct page *page, const struct iomap_ops *ops);
138 int iomap_readpages(struct address_space *mapping, struct list_head *pages,
139 		unsigned nr_pages, const struct iomap_ops *ops);
140 int iomap_set_page_dirty(struct page *page);
141 int iomap_is_partially_uptodate(struct page *page, unsigned long from,
142 		unsigned long count);
143 int iomap_releasepage(struct page *page, gfp_t gfp_mask);
144 void iomap_invalidatepage(struct page *page, unsigned int offset,
145 		unsigned int len);
146 #ifdef CONFIG_MIGRATION
147 int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
148 		struct page *page, enum migrate_mode mode);
149 #else
150 #define iomap_migrate_page NULL
151 #endif
152 int iomap_file_dirty(struct inode *inode, loff_t pos, loff_t len,
153 		const struct iomap_ops *ops);
154 int iomap_zero_range(struct inode *inode, loff_t pos, loff_t len,
155 		bool *did_zero, const struct iomap_ops *ops);
156 int iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
157 		const struct iomap_ops *ops);
158 vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf,
159 			const struct iomap_ops *ops);
160 int iomap_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
161 		loff_t start, loff_t len, const struct iomap_ops *ops);
162 loff_t iomap_seek_hole(struct inode *inode, loff_t offset,
163 		const struct iomap_ops *ops);
164 loff_t iomap_seek_data(struct inode *inode, loff_t offset,
165 		const struct iomap_ops *ops);
166 sector_t iomap_bmap(struct address_space *mapping, sector_t bno,
167 		const struct iomap_ops *ops);
168 
169 /*
170  * Flags for direct I/O ->end_io:
171  */
172 #define IOMAP_DIO_UNWRITTEN	(1 << 0)	/* covers unwritten extent(s) */
173 #define IOMAP_DIO_COW		(1 << 1)	/* covers COW extent(s) */
174 typedef int (iomap_dio_end_io_t)(struct kiocb *iocb, ssize_t ret,
175 		unsigned flags);
176 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
177 		const struct iomap_ops *ops, iomap_dio_end_io_t end_io);
178 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
179 
180 #ifdef CONFIG_SWAP
181 struct file;
182 struct swap_info_struct;
183 
184 int iomap_swapfile_activate(struct swap_info_struct *sis,
185 		struct file *swap_file, sector_t *pagespan,
186 		const struct iomap_ops *ops);
187 #else
188 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops)	(-EIO)
189 #endif /* CONFIG_SWAP */
190 
191 #endif /* LINUX_IOMAP_H */
192