dax.h 7.4 KB

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