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