dax.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_DAX_H
  3. #define _LINUX_DAX_H
  4. #include <linux/fs.h>
  5. #include <linux/mm.h>
  6. #include <linux/radix-tree.h>
  7. typedef unsigned long dax_entry_t;
  8. struct dax_device;
  9. struct gendisk;
  10. struct iomap_ops;
  11. struct iomap_iter;
  12. struct iomap;
  13. enum dax_access_mode {
  14. DAX_ACCESS,
  15. DAX_RECOVERY_WRITE,
  16. };
  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. enum dax_access_mode, 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. /* zero_page_range: required operation. Zero page range */
  32. int (*zero_page_range)(struct dax_device *, pgoff_t, size_t);
  33. /*
  34. * recovery_write: recover a poisoned range by DAX device driver
  35. * capable of clearing poison.
  36. */
  37. size_t (*recovery_write)(struct dax_device *dax_dev, pgoff_t pgoff,
  38. void *addr, size_t bytes, struct iov_iter *iter);
  39. };
  40. struct dax_holder_operations {
  41. /*
  42. * notify_failure - notify memory failure into inner holder device
  43. * @dax_dev: the dax device which contains the holder
  44. * @offset: offset on this dax device where memory failure occurs
  45. * @len: length of this memory failure event
  46. * @flags: action flags for memory failure handler
  47. */
  48. int (*notify_failure)(struct dax_device *dax_dev, u64 offset,
  49. u64 len, int mf_flags);
  50. };
  51. #if IS_ENABLED(CONFIG_DAX)
  52. struct dax_device *alloc_dax(void *private, const struct dax_operations *ops);
  53. void *dax_holder(struct dax_device *dax_dev);
  54. void put_dax(struct dax_device *dax_dev);
  55. void kill_dax(struct dax_device *dax_dev);
  56. void dax_write_cache(struct dax_device *dax_dev, bool wc);
  57. bool dax_write_cache_enabled(struct dax_device *dax_dev);
  58. bool dax_synchronous(struct dax_device *dax_dev);
  59. void set_dax_synchronous(struct dax_device *dax_dev);
  60. size_t dax_recovery_write(struct dax_device *dax_dev, pgoff_t pgoff,
  61. void *addr, size_t bytes, struct iov_iter *i);
  62. /*
  63. * Check if given mapping is supported by the file / underlying device.
  64. */
  65. static inline bool daxdev_mapping_supported(struct vm_area_struct *vma,
  66. struct dax_device *dax_dev)
  67. {
  68. if (!(vma->vm_flags & VM_SYNC))
  69. return true;
  70. if (!IS_DAX(file_inode(vma->vm_file)))
  71. return false;
  72. return dax_synchronous(dax_dev);
  73. }
  74. #else
  75. static inline void *dax_holder(struct dax_device *dax_dev)
  76. {
  77. return NULL;
  78. }
  79. static inline struct dax_device *alloc_dax(void *private,
  80. const struct dax_operations *ops)
  81. {
  82. /*
  83. * Callers should check IS_ENABLED(CONFIG_DAX) to know if this
  84. * NULL is an error or expected.
  85. */
  86. return NULL;
  87. }
  88. static inline void put_dax(struct dax_device *dax_dev)
  89. {
  90. }
  91. static inline void kill_dax(struct dax_device *dax_dev)
  92. {
  93. }
  94. static inline void dax_write_cache(struct dax_device *dax_dev, bool wc)
  95. {
  96. }
  97. static inline bool dax_write_cache_enabled(struct dax_device *dax_dev)
  98. {
  99. return false;
  100. }
  101. static inline bool dax_synchronous(struct dax_device *dax_dev)
  102. {
  103. return true;
  104. }
  105. static inline void set_dax_synchronous(struct dax_device *dax_dev)
  106. {
  107. }
  108. static inline bool daxdev_mapping_supported(struct vm_area_struct *vma,
  109. struct dax_device *dax_dev)
  110. {
  111. return !(vma->vm_flags & VM_SYNC);
  112. }
  113. static inline size_t dax_recovery_write(struct dax_device *dax_dev,
  114. pgoff_t pgoff, void *addr, size_t bytes, struct iov_iter *i)
  115. {
  116. return 0;
  117. }
  118. #endif
  119. void set_dax_nocache(struct dax_device *dax_dev);
  120. void set_dax_nomc(struct dax_device *dax_dev);
  121. struct writeback_control;
  122. #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
  123. int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk);
  124. void dax_remove_host(struct gendisk *disk);
  125. struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off,
  126. void *holder, const struct dax_holder_operations *ops);
  127. void fs_put_dax(struct dax_device *dax_dev, void *holder);
  128. #else
  129. static inline int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
  130. {
  131. return 0;
  132. }
  133. static inline void dax_remove_host(struct gendisk *disk)
  134. {
  135. }
  136. static inline struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev,
  137. u64 *start_off, void *holder,
  138. const struct dax_holder_operations *ops)
  139. {
  140. return NULL;
  141. }
  142. static inline void fs_put_dax(struct dax_device *dax_dev, void *holder)
  143. {
  144. }
  145. #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
  146. #if IS_ENABLED(CONFIG_FS_DAX)
  147. int dax_writeback_mapping_range(struct address_space *mapping,
  148. struct dax_device *dax_dev, struct writeback_control *wbc);
  149. struct page *dax_layout_busy_page(struct address_space *mapping);
  150. struct page *dax_layout_busy_page_range(struct address_space *mapping, loff_t start, loff_t end);
  151. dax_entry_t dax_lock_page(struct page *page);
  152. void dax_unlock_page(struct page *page, dax_entry_t cookie);
  153. dax_entry_t dax_lock_mapping_entry(struct address_space *mapping,
  154. unsigned long index, struct page **page);
  155. void dax_unlock_mapping_entry(struct address_space *mapping,
  156. unsigned long index, dax_entry_t cookie);
  157. #else
  158. static inline struct page *dax_layout_busy_page(struct address_space *mapping)
  159. {
  160. return NULL;
  161. }
  162. static inline struct page *dax_layout_busy_page_range(struct address_space *mapping, pgoff_t start, pgoff_t nr_pages)
  163. {
  164. return NULL;
  165. }
  166. static inline int dax_writeback_mapping_range(struct address_space *mapping,
  167. struct dax_device *dax_dev, struct writeback_control *wbc)
  168. {
  169. return -EOPNOTSUPP;
  170. }
  171. static inline dax_entry_t dax_lock_page(struct page *page)
  172. {
  173. if (IS_DAX(page->mapping->host))
  174. return ~0UL;
  175. return 0;
  176. }
  177. static inline void dax_unlock_page(struct page *page, dax_entry_t cookie)
  178. {
  179. }
  180. static inline dax_entry_t dax_lock_mapping_entry(struct address_space *mapping,
  181. unsigned long index, struct page **page)
  182. {
  183. return 0;
  184. }
  185. static inline void dax_unlock_mapping_entry(struct address_space *mapping,
  186. unsigned long index, dax_entry_t cookie)
  187. {
  188. }
  189. #endif
  190. int dax_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
  191. const struct iomap_ops *ops);
  192. int dax_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
  193. const struct iomap_ops *ops);
  194. #if IS_ENABLED(CONFIG_DAX)
  195. int dax_read_lock(void);
  196. void dax_read_unlock(int id);
  197. #else
  198. static inline int dax_read_lock(void)
  199. {
  200. return 0;
  201. }
  202. static inline void dax_read_unlock(int id)
  203. {
  204. }
  205. #endif /* CONFIG_DAX */
  206. bool dax_alive(struct dax_device *dax_dev);
  207. void *dax_get_private(struct dax_device *dax_dev);
  208. long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  209. enum dax_access_mode mode, void **kaddr, pfn_t *pfn);
  210. size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  211. size_t bytes, struct iov_iter *i);
  212. size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  213. size_t bytes, struct iov_iter *i);
  214. int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
  215. size_t nr_pages);
  216. int dax_holder_notify_failure(struct dax_device *dax_dev, u64 off, u64 len,
  217. int mf_flags);
  218. void dax_flush(struct dax_device *dax_dev, void *addr, size_t size);
  219. ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  220. const struct iomap_ops *ops);
  221. vm_fault_t dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
  222. pfn_t *pfnp, int *errp, const struct iomap_ops *ops);
  223. vm_fault_t dax_finish_sync_fault(struct vm_fault *vmf,
  224. enum page_entry_size pe_size, pfn_t pfn);
  225. int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index);
  226. int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
  227. pgoff_t index);
  228. int dax_dedupe_file_range_compare(struct inode *src, loff_t srcoff,
  229. struct inode *dest, loff_t destoff,
  230. loff_t len, bool *is_same,
  231. const struct iomap_ops *ops);
  232. int dax_remap_file_range_prep(struct file *file_in, loff_t pos_in,
  233. struct file *file_out, loff_t pos_out,
  234. loff_t *len, unsigned int remap_flags,
  235. const struct iomap_ops *ops);
  236. static inline bool dax_mapping(struct address_space *mapping)
  237. {
  238. return mapping->host && IS_DAX(mapping->host);
  239. }
  240. #ifdef CONFIG_DEV_DAX_HMEM_DEVICES
  241. void hmem_register_device(int target_nid, struct resource *r);
  242. #else
  243. static inline void hmem_register_device(int target_nid, struct resource *r)
  244. {
  245. }
  246. #endif
  247. #endif