1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_DAX_H
3 #define _LINUX_DAX_H
4
5 #include <linux/fs.h>
6 #include <linux/mm.h>
7 #include <linux/radix-tree.h>
8 #include <asm/pgtable.h>
9
10 /* Flag for synchronous flush */
11 #define DAXDEV_F_SYNC (1UL << 0)
12
13 typedef unsigned long dax_entry_t;
14
15 struct iomap_ops;
16 struct dax_device;
17 struct dax_operations {
18 /*
19 * direct_access: translate a device-relative
20 * logical-page-offset into an absolute physical pfn. Return the
21 * number of pages available for DAX at that pfn.
22 */
23 long (*direct_access)(struct dax_device *, pgoff_t, long,
24 void **, pfn_t *);
25 /*
26 * Validate whether this device is usable as an fsdax backing
27 * device.
28 */
29 bool (*dax_supported)(struct dax_device *, struct block_device *, int,
30 sector_t, sector_t);
31 /* copy_from_iter: required operation for fs-dax direct-i/o */
32 size_t (*copy_from_iter)(struct dax_device *, pgoff_t, void *, size_t,
33 struct iov_iter *);
34 /* copy_to_iter: required operation for fs-dax direct-i/o */
35 size_t (*copy_to_iter)(struct dax_device *, pgoff_t, void *, size_t,
36 struct iov_iter *);
37 };
38
39 extern struct attribute_group dax_attribute_group;
40
41 #if IS_ENABLED(CONFIG_DAX)
42 struct dax_device *dax_get_by_host(const char *host);
43 struct dax_device *alloc_dax(void *private, const char *host,
44 const struct dax_operations *ops, unsigned long flags);
45 void put_dax(struct dax_device *dax_dev);
46 void kill_dax(struct dax_device *dax_dev);
47 void dax_write_cache(struct dax_device *dax_dev, bool wc);
48 bool dax_write_cache_enabled(struct dax_device *dax_dev);
49 bool __dax_synchronous(struct dax_device *dax_dev);
dax_synchronous(struct dax_device * dax_dev)50 static inline bool dax_synchronous(struct dax_device *dax_dev)
51 {
52 return __dax_synchronous(dax_dev);
53 }
54 void __set_dax_synchronous(struct dax_device *dax_dev);
set_dax_synchronous(struct dax_device * dax_dev)55 static inline void set_dax_synchronous(struct dax_device *dax_dev)
56 {
57 __set_dax_synchronous(dax_dev);
58 }
59 bool dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
60 int blocksize, sector_t start, sector_t len);
61 /*
62 * Check if given mapping is supported by the file / underlying device.
63 */
daxdev_mapping_supported(struct vm_area_struct * vma,struct dax_device * dax_dev)64 static inline bool daxdev_mapping_supported(struct vm_area_struct *vma,
65 struct dax_device *dax_dev)
66 {
67 if (!(vma->vm_flags & VM_SYNC))
68 return true;
69 if (!IS_DAX(file_inode(vma->vm_file)))
70 return false;
71 return dax_synchronous(dax_dev);
72 }
73 #else
dax_get_by_host(const char * host)74 static inline struct dax_device *dax_get_by_host(const char *host)
75 {
76 return NULL;
77 }
alloc_dax(void * private,const char * host,const struct dax_operations * ops,unsigned long flags)78 static inline struct dax_device *alloc_dax(void *private, const char *host,
79 const struct dax_operations *ops, unsigned long flags)
80 {
81 /*
82 * Callers should check IS_ENABLED(CONFIG_DAX) to know if this
83 * NULL is an error or expected.
84 */
85 return NULL;
86 }
put_dax(struct dax_device * dax_dev)87 static inline void put_dax(struct dax_device *dax_dev)
88 {
89 }
kill_dax(struct dax_device * dax_dev)90 static inline void kill_dax(struct dax_device *dax_dev)
91 {
92 }
dax_write_cache(struct dax_device * dax_dev,bool wc)93 static inline void dax_write_cache(struct dax_device *dax_dev, bool wc)
94 {
95 }
dax_write_cache_enabled(struct dax_device * dax_dev)96 static inline bool dax_write_cache_enabled(struct dax_device *dax_dev)
97 {
98 return false;
99 }
dax_synchronous(struct dax_device * dax_dev)100 static inline bool dax_synchronous(struct dax_device *dax_dev)
101 {
102 return true;
103 }
set_dax_synchronous(struct dax_device * dax_dev)104 static inline void set_dax_synchronous(struct dax_device *dax_dev)
105 {
106 }
dax_supported(struct dax_device * dax_dev,struct block_device * bdev,int blocksize,sector_t start,sector_t len)107 static inline bool dax_supported(struct dax_device *dax_dev,
108 struct block_device *bdev, int blocksize, sector_t start,
109 sector_t len)
110 {
111 return false;
112 }
daxdev_mapping_supported(struct vm_area_struct * vma,struct dax_device * dax_dev)113 static inline bool daxdev_mapping_supported(struct vm_area_struct *vma,
114 struct dax_device *dax_dev)
115 {
116 return !(vma->vm_flags & VM_SYNC);
117 }
118 #endif
119
120 struct writeback_control;
121 int bdev_dax_pgoff(struct block_device *, sector_t, size_t, pgoff_t *pgoff);
122 #if IS_ENABLED(CONFIG_FS_DAX)
123 bool __bdev_dax_supported(struct block_device *bdev, int blocksize);
bdev_dax_supported(struct block_device * bdev,int blocksize)124 static inline bool bdev_dax_supported(struct block_device *bdev, int blocksize)
125 {
126 return __bdev_dax_supported(bdev, blocksize);
127 }
128
129 bool __generic_fsdax_supported(struct dax_device *dax_dev,
130 struct block_device *bdev, int blocksize, sector_t start,
131 sector_t sectors);
generic_fsdax_supported(struct dax_device * dax_dev,struct block_device * bdev,int blocksize,sector_t start,sector_t sectors)132 static inline bool generic_fsdax_supported(struct dax_device *dax_dev,
133 struct block_device *bdev, int blocksize, sector_t start,
134 sector_t sectors)
135 {
136 return __generic_fsdax_supported(dax_dev, bdev, blocksize, start,
137 sectors);
138 }
139
fs_dax_get_by_host(const char * host)140 static inline struct dax_device *fs_dax_get_by_host(const char *host)
141 {
142 return dax_get_by_host(host);
143 }
144
fs_put_dax(struct dax_device * dax_dev)145 static inline void fs_put_dax(struct dax_device *dax_dev)
146 {
147 put_dax(dax_dev);
148 }
149
150 struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev);
151 int dax_writeback_mapping_range(struct address_space *mapping,
152 struct block_device *bdev, struct writeback_control *wbc);
153
154 struct page *dax_layout_busy_page(struct address_space *mapping);
155 dax_entry_t dax_lock_page(struct page *page);
156 void dax_unlock_page(struct page *page, dax_entry_t cookie);
157 #else
bdev_dax_supported(struct block_device * bdev,int blocksize)158 static inline bool bdev_dax_supported(struct block_device *bdev,
159 int blocksize)
160 {
161 return false;
162 }
163
generic_fsdax_supported(struct dax_device * dax_dev,struct block_device * bdev,int blocksize,sector_t start,sector_t sectors)164 static inline bool generic_fsdax_supported(struct dax_device *dax_dev,
165 struct block_device *bdev, int blocksize, sector_t start,
166 sector_t sectors)
167 {
168 return false;
169 }
170
fs_dax_get_by_host(const char * host)171 static inline struct dax_device *fs_dax_get_by_host(const char *host)
172 {
173 return NULL;
174 }
175
fs_put_dax(struct dax_device * dax_dev)176 static inline void fs_put_dax(struct dax_device *dax_dev)
177 {
178 }
179
fs_dax_get_by_bdev(struct block_device * bdev)180 static inline struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev)
181 {
182 return NULL;
183 }
184
dax_layout_busy_page(struct address_space * mapping)185 static inline struct page *dax_layout_busy_page(struct address_space *mapping)
186 {
187 return NULL;
188 }
189
dax_writeback_mapping_range(struct address_space * mapping,struct block_device * bdev,struct writeback_control * wbc)190 static inline int dax_writeback_mapping_range(struct address_space *mapping,
191 struct block_device *bdev, struct writeback_control *wbc)
192 {
193 return -EOPNOTSUPP;
194 }
195
dax_lock_page(struct page * page)196 static inline dax_entry_t dax_lock_page(struct page *page)
197 {
198 if (IS_DAX(page->mapping->host))
199 return ~0UL;
200 return 0;
201 }
202
dax_unlock_page(struct page * page,dax_entry_t cookie)203 static inline void dax_unlock_page(struct page *page, dax_entry_t cookie)
204 {
205 }
206 #endif
207
208 #if IS_ENABLED(CONFIG_DAX)
209 int dax_read_lock(void);
210 void dax_read_unlock(int id);
211 #else
dax_read_lock(void)212 static inline int dax_read_lock(void)
213 {
214 return 0;
215 }
216
dax_read_unlock(int id)217 static inline void dax_read_unlock(int id)
218 {
219 }
220 #endif /* CONFIG_DAX */
221 bool dax_alive(struct dax_device *dax_dev);
222 void *dax_get_private(struct dax_device *dax_dev);
223 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
224 void **kaddr, pfn_t *pfn);
225 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
226 size_t bytes, struct iov_iter *i);
227 size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
228 size_t bytes, struct iov_iter *i);
229 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size);
230
231 ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
232 const struct iomap_ops *ops);
233 vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
234 pfn_t *pfnp, int *errp, const struct iomap_ops *ops);
235 vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
236 enum page_entry_size pe_size, pfn_t pfn);
237 int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index);
238 int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
239 pgoff_t index);
240
241 #ifdef CONFIG_FS_DAX
242 int __dax_zero_page_range(struct block_device *bdev,
243 struct dax_device *dax_dev, sector_t sector,
244 unsigned int offset, unsigned int length);
245 #else
__dax_zero_page_range(struct block_device * bdev,struct dax_device * dax_dev,sector_t sector,unsigned int offset,unsigned int length)246 static inline int __dax_zero_page_range(struct block_device *bdev,
247 struct dax_device *dax_dev, sector_t sector,
248 unsigned int offset, unsigned int length)
249 {
250 return -ENXIO;
251 }
252 #endif
253
dax_mapping(struct address_space * mapping)254 static inline bool dax_mapping(struct address_space *mapping)
255 {
256 return mapping->host && IS_DAX(mapping->host);
257 }
258
259 #endif
260