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 9typedef unsigned long dax_entry_t; 10 11struct dax_device; 12struct gendisk; 13struct iomap_ops; 14struct iomap_iter; 15struct iomap; 16 17enum dax_access_mode { 18 DAX_ACCESS, 19 DAX_RECOVERY_WRITE, 20}; 21 22struct dax_operations { 23 /* 24 * direct_access: translate a device-relative 25 * logical-page-offset into an absolute physical pfn. Return the 26 * number of pages available for DAX at that pfn. 27 */ 28 long (*direct_access)(struct dax_device *, pgoff_t, long, 29 enum dax_access_mode, void **, pfn_t *); 30 /* 31 * Validate whether this device is usable as an fsdax backing 32 * device. 33 */ 34 bool (*dax_supported)(struct dax_device *, struct block_device *, int, 35 sector_t, sector_t); 36 /* zero_page_range: required operation. Zero page range */ 37 int (*zero_page_range)(struct dax_device *, pgoff_t, size_t); 38 /* 39 * recovery_write: recover a poisoned range by DAX device driver 40 * capable of clearing poison. 41 */ 42 size_t (*recovery_write)(struct dax_device *dax_dev, pgoff_t pgoff, 43 void *addr, size_t bytes, struct iov_iter *iter); 44}; 45 46struct dax_holder_operations { 47 /* 48 * notify_failure - notify memory failure into inner holder device 49 * @dax_dev: the dax device which contains the holder 50 * @offset: offset on this dax device where memory failure occurs 51 * @len: length of this memory failure event 52 * @flags: action flags for memory failure handler 53 */ 54 int (*notify_failure)(struct dax_device *dax_dev, u64 offset, 55 u64 len, int mf_flags); 56}; 57 58#if IS_ENABLED(CONFIG_DAX) 59struct dax_device *alloc_dax(void *private, const struct dax_operations *ops); 60void *dax_holder(struct dax_device *dax_dev); 61void put_dax(struct dax_device *dax_dev); 62void kill_dax(struct dax_device *dax_dev); 63void dax_write_cache(struct dax_device *dax_dev, bool wc); 64bool dax_write_cache_enabled(struct dax_device *dax_dev); 65bool dax_synchronous(struct dax_device *dax_dev); 66void set_dax_nocache(struct dax_device *dax_dev); 67void set_dax_nomc(struct dax_device *dax_dev); 68void set_dax_synchronous(struct dax_device *dax_dev); 69size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff, 70 void *addr, size_t bytes, struct iov_iter *i); 71/* 72 * Check if given mapping is supported by the file / underlying device. 73 */ 74static inline bool daxdev_mapping_supported(struct vm_area_struct *vma, 75 struct dax_device *dax_dev) 76{ 77 if (!(vma->vm_flags & VM_SYNC)) 78 return true; 79 if (!IS_DAX(file_inode(vma->vm_file))) 80 return false; 81 return dax_synchronous(dax_dev); 82} 83#else 84static inline void *dax_holder(struct dax_device *dax_dev) 85{ 86 return NULL; 87} 88static inline struct dax_device *alloc_dax(void *private, 89 const struct dax_operations *ops) 90{ 91 return ERR_PTR(-EOPNOTSUPP); 92} 93static inline void put_dax(struct dax_device *dax_dev) 94{ 95} 96static inline void kill_dax(struct dax_device *dax_dev) 97{ 98} 99static inline void dax_write_cache(struct dax_device *dax_dev, bool wc) 100{ 101} 102static inline bool dax_write_cache_enabled(struct dax_device *dax_dev) 103{ 104 return false; 105} 106static inline bool dax_synchronous(struct dax_device *dax_dev) 107{ 108 return true; 109} 110static inline void set_dax_nocache(struct dax_device *dax_dev) 111{ 112} 113static inline void set_dax_nomc(struct dax_device *dax_dev) 114{ 115} 116static inline void set_dax_synchronous(struct dax_device *dax_dev) 117{ 118} 119static inline bool daxdev_mapping_supported(struct vm_area_struct *vma, 120 struct dax_device *dax_dev) 121{ 122 return !(vma->vm_flags & VM_SYNC); 123} 124static inline size_t dax_recovery_write(struct dax_device *dax_dev, 125 pgoff_t pgoff, void *addr, size_t bytes, struct iov_iter *i) 126{ 127 return 0; 128} 129#endif 130 131struct writeback_control; 132#if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX) 133int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk); 134void dax_remove_host(struct gendisk *disk); 135struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off, 136 void *holder, const struct dax_holder_operations *ops); 137void fs_put_dax(struct dax_device *dax_dev, void *holder); 138#else 139static inline int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk) 140{ 141 return 0; 142} 143static inline void dax_remove_host(struct gendisk *disk) 144{ 145} 146static inline struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, 147 u64 *start_off, void *holder, 148 const struct dax_holder_operations *ops) 149{ 150 return NULL; 151} 152static inline void fs_put_dax(struct dax_device *dax_dev, void *holder) 153{ 154} 155#endif /* CONFIG_BLOCK && CONFIG_FS_DAX */ 156 157#if IS_ENABLED(CONFIG_FS_DAX) 158int dax_writeback_mapping_range(struct address_space *mapping, 159 struct dax_device *dax_dev, struct writeback_control *wbc); 160 161struct page *dax_layout_busy_page(struct address_space *mapping); 162struct page *dax_layout_busy_page_range(struct address_space *mapping, loff_t start, loff_t end); 163dax_entry_t dax_lock_folio(struct folio *folio); 164void dax_unlock_folio(struct folio *folio, dax_entry_t cookie); 165dax_entry_t dax_lock_mapping_entry(struct address_space *mapping, 166 unsigned long index, struct page **page); 167void dax_unlock_mapping_entry(struct address_space *mapping, 168 unsigned long index, dax_entry_t cookie); 169#else 170static inline struct page *dax_layout_busy_page(struct address_space *mapping) 171{ 172 return NULL; 173} 174 175static inline struct page *dax_layout_busy_page_range(struct address_space *mapping, pgoff_t start, pgoff_t nr_pages) 176{ 177 return NULL; 178} 179 180static inline int dax_writeback_mapping_range(struct address_space *mapping, 181 struct dax_device *dax_dev, struct writeback_control *wbc) 182{ 183 return -EOPNOTSUPP; 184} 185 186static inline dax_entry_t dax_lock_folio(struct folio *folio) 187{ 188 if (IS_DAX(folio->mapping->host)) 189 return ~0UL; 190 return 0; 191} 192 193static inline void dax_unlock_folio(struct folio *folio, dax_entry_t cookie) 194{ 195} 196 197static inline dax_entry_t dax_lock_mapping_entry(struct address_space *mapping, 198 unsigned long index, struct page **page) 199{ 200 return 0; 201} 202 203static inline void dax_unlock_mapping_entry(struct address_space *mapping, 204 unsigned long index, dax_entry_t cookie) 205{ 206} 207#endif 208 209int dax_file_unshare(struct inode *inode, loff_t pos, loff_t len, 210 const struct iomap_ops *ops); 211int dax_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero, 212 const struct iomap_ops *ops); 213int dax_truncate_page(struct inode *inode, loff_t pos, bool *did_zero, 214 const struct iomap_ops *ops); 215 216#if IS_ENABLED(CONFIG_DAX) 217int dax_read_lock(void); 218void dax_read_unlock(int id); 219#else 220static inline int dax_read_lock(void) 221{ 222 return 0; 223} 224 225static inline void dax_read_unlock(int id) 226{ 227} 228#endif /* CONFIG_DAX */ 229bool dax_alive(struct dax_device *dax_dev); 230void *dax_get_private(struct dax_device *dax_dev); 231long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages, 232 enum dax_access_mode mode, void **kaddr, pfn_t *pfn); 233size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, 234 size_t bytes, struct iov_iter *i); 235size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr, 236 size_t bytes, struct iov_iter *i); 237int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff, 238 size_t nr_pages); 239int dax_holder_notify_failure(struct dax_device *dax_dev, u64 off, u64 len, 240 int mf_flags); 241void dax_flush(struct dax_device *dax_dev, void *addr, size_t size); 242 243ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter, 244 const struct iomap_ops *ops); 245vm_fault_t dax_iomap_fault(struct vm_fault *vmf, unsigned int order, 246 pfn_t *pfnp, int *errp, const struct iomap_ops *ops); 247vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf, 248 unsigned int order, pfn_t pfn); 249int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index); 250int dax_invalidate_mapping_entry_sync(struct address_space *mapping, 251 pgoff_t index); 252int dax_dedupe_file_range_compare(struct inode *src, loff_t srcoff, 253 struct inode *dest, loff_t destoff, 254 loff_t len, bool *is_same, 255 const struct iomap_ops *ops); 256int dax_remap_file_range_prep(struct file *file_in, loff_t pos_in, 257 struct file *file_out, loff_t pos_out, 258 loff_t *len, unsigned int remap_flags, 259 const struct iomap_ops *ops); 260static inline bool dax_mapping(struct address_space *mapping) 261{ 262 return mapping->host && IS_DAX(mapping->host); 263} 264 265/* 266 * Due to dax's memory and block duo personalities, hwpoison reporting 267 * takes into consideration which personality is presently visible. 268 * When dax acts like a block device, such as in block IO, an encounter of 269 * dax hwpoison is reported as -EIO. 270 * When dax acts like memory, such as in page fault, a detection of hwpoison 271 * is reported as -EHWPOISON which leads to VM_FAULT_HWPOISON. 272 */ 273static inline int dax_mem2blk_err(int err) 274{ 275 return (err == -EHWPOISON) ? -EIO : err; 276} 277 278#ifdef CONFIG_DEV_DAX_HMEM_DEVICES 279void hmem_register_resource(int target_nid, struct resource *r); 280#else 281static inline void hmem_register_resource(int target_nid, struct resource *r) 282{ 283} 284#endif 285 286typedef int (*walk_hmem_fn)(struct device *dev, int target_nid, 287 const struct resource *res); 288int walk_hmem_resources(struct device *dev, walk_hmem_fn fn); 289#endif 290