1 /* SPDX-License-Identifier: GPL-2.0 */
3 #define LINUX_IOMAP_H 1
5 #include <linux/atomic.h>
6 #include <linux/bitmap.h>
8 #include <linux/types.h>
9 #include <linux/mm_types.h>
10 #include <linux/blkdev.h>
13 struct fiemap_extent_info;
18 struct vm_area_struct;
22 * Types of block ranges for iomap mappings:
24 #define IOMAP_HOLE 0x01 /* no blocks allocated, need allocation */
25 #define IOMAP_DELALLOC 0x02 /* delayed allocation blocks */
26 #define IOMAP_MAPPED 0x03 /* blocks allocated at @addr */
27 #define IOMAP_UNWRITTEN 0x04 /* blocks allocated at @addr in unwritten state */
28 #define IOMAP_INLINE 0x05 /* data inline in the inode */
31 * Flags for all iomap mappings:
33 * IOMAP_F_DIRTY indicates the inode has uncommitted metadata needed to access
34 * written data and requires fdatasync to commit them to persistent storage.
35 * This needs to take into account metadata changes that *may* be made at IO
36 * completion, such as file size updates from direct IO.
38 #define IOMAP_F_NEW 0x01 /* blocks have been newly allocated */
39 #define IOMAP_F_DIRTY 0x02 /* uncommitted metadata */
40 #define IOMAP_F_BUFFER_HEAD 0x04 /* file system requires buffer heads */
41 #define IOMAP_F_SIZE_CHANGED 0x08 /* file size has changed */
44 * Flags that only need to be reported for IOMAP_REPORT requests:
46 #define IOMAP_F_MERGED 0x10 /* contains multiple blocks/extents */
47 #define IOMAP_F_SHARED 0x20 /* block shared with another file */
50 * Flags from 0x1000 up are for file system specific usage:
52 #define IOMAP_F_PRIVATE 0x1000
56 * Magic value for addr:
58 #define IOMAP_NULL_ADDR -1ULL /* addr is not valid */
60 struct iomap_page_ops;
63 u64 addr; /* disk offset of mapping, bytes */
64 loff_t offset; /* file offset of mapping, bytes */
65 u64 length; /* length of mapping, bytes */
66 u16 type; /* type of mapping */
67 u16 flags; /* flags for mapping */
68 struct block_device *bdev; /* block device for I/O */
69 struct dax_device *dax_dev; /* dax_dev for dax operations */
71 void *private; /* filesystem private */
72 const struct iomap_page_ops *page_ops;
75 static inline sector_t
76 iomap_sector(struct iomap *iomap, loff_t pos)
78 return (iomap->addr + pos - iomap->offset) >> SECTOR_SHIFT;
82 * When a filesystem sets page_ops in an iomap mapping it returns, page_prepare
83 * and page_done will be called for each page written to. This only applies to
84 * buffered writes as unbuffered writes will not typically have pages
85 * associated with them.
87 * When page_prepare succeeds, page_done will always be called to do any
88 * cleanup work necessary. In that page_done call, @page will be NULL if the
89 * associated page could not be obtained.
91 struct iomap_page_ops {
92 int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len,
94 void (*page_done)(struct inode *inode, loff_t pos, unsigned copied,
95 struct page *page, struct iomap *iomap);
99 * Flags for iomap_begin / iomap_end. No flag implies a read.
101 #define IOMAP_WRITE (1 << 0) /* writing, must allocate blocks */
102 #define IOMAP_ZERO (1 << 1) /* zeroing operation, may skip holes */
103 #define IOMAP_REPORT (1 << 2) /* report extent status, e.g. FIEMAP */
104 #define IOMAP_FAULT (1 << 3) /* mapping for page fault */
105 #define IOMAP_DIRECT (1 << 4) /* direct I/O */
106 #define IOMAP_NOWAIT (1 << 5) /* do not block */
110 * Return the existing mapping at pos, or reserve space starting at
111 * pos for up to length, as long as we can do it as a single mapping.
112 * The actual length is returned in iomap->length.
114 int (*iomap_begin)(struct inode *inode, loff_t pos, loff_t length,
115 unsigned flags, struct iomap *iomap);
118 * Commit and/or unreserve space previous allocated using iomap_begin.
119 * Written indicates the length of the successful write operation which
120 * needs to be commited, while the rest needs to be unreserved.
121 * Written might be zero if no data was written.
123 int (*iomap_end)(struct inode *inode, loff_t pos, loff_t length,
124 ssize_t written, unsigned flags, struct iomap *iomap);
128 * Main iomap iterator function.
130 typedef loff_t (*iomap_actor_t)(struct inode *inode, loff_t pos, loff_t len,
131 void *data, struct iomap *iomap);
133 loff_t iomap_apply(struct inode *inode, loff_t pos, loff_t length,
134 unsigned flags, const struct iomap_ops *ops, void *data,
135 iomap_actor_t actor);
138 * Structure allocate for each page when block size < PAGE_SIZE to track
139 * sub-page uptodate status and I/O completions.
143 atomic_t write_count;
144 spinlock_t uptodate_lock;
145 DECLARE_BITMAP(uptodate, PAGE_SIZE / 512);
148 static inline struct iomap_page *to_iomap_page(struct page *page)
150 if (page_has_private(page))
151 return (struct iomap_page *)page_private(page);
155 ssize_t iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *from,
156 const struct iomap_ops *ops);
157 int iomap_readpage(struct page *page, const struct iomap_ops *ops);
158 int iomap_readpages(struct address_space *mapping, struct list_head *pages,
159 unsigned nr_pages, 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,
166 #ifdef CONFIG_MIGRATION
167 int iomap_migrate_page(struct address_space *mapping, struct page *newpage,
168 struct page *page, enum migrate_mode mode);
170 #define iomap_migrate_page NULL
172 int iomap_file_dirty(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 loff_t start, loff_t 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);
190 * Flags for direct I/O ->end_io:
192 #define IOMAP_DIO_UNWRITTEN (1 << 0) /* covers unwritten extent(s) */
193 #define IOMAP_DIO_COW (1 << 1) /* covers COW extent(s) */
195 struct iomap_dio_ops {
196 int (*end_io)(struct kiocb *iocb, ssize_t size, int error,
200 ssize_t iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
201 const struct iomap_ops *ops, const struct iomap_dio_ops *dops);
202 int iomap_dio_iopoll(struct kiocb *kiocb, bool spin);
206 struct swap_info_struct;
208 int iomap_swapfile_activate(struct swap_info_struct *sis,
209 struct file *swap_file, sector_t *pagespan,
210 const struct iomap_ops *ops);
212 # define iomap_swapfile_activate(sis, swapfile, pagespan, ops) (-EIO)
213 #endif /* CONFIG_SWAP */
215 #endif /* LINUX_IOMAP_H */