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