dax.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #ifndef _LINUX_DAX_H
  2. #define _LINUX_DAX_H
  3. #include <linux/fs.h>
  4. #include <linux/mm.h>
  5. #include <linux/radix-tree.h>
  6. #include <asm/pgtable.h>
  7. struct iomap_ops;
  8. struct dax_device;
  9. struct dax_operations {
  10. /*
  11. * direct_access: translate a device-relative
  12. * logical-page-offset into an absolute physical pfn. Return the
  13. * number of pages available for DAX at that pfn.
  14. */
  15. long (*direct_access)(struct dax_device *, pgoff_t, long,
  16. void **, pfn_t *);
  17. /* copy_from_iter: required operation for fs-dax direct-i/o */
  18. size_t (*copy_from_iter)(struct dax_device *, pgoff_t, void *, size_t,
  19. struct iov_iter *);
  20. /* flush: optional driver-specific cache management after writes */
  21. void (*flush)(struct dax_device *, pgoff_t, void *, size_t);
  22. };
  23. extern struct attribute_group dax_attribute_group;
  24. #if IS_ENABLED(CONFIG_DAX)
  25. struct dax_device *dax_get_by_host(const char *host);
  26. void put_dax(struct dax_device *dax_dev);
  27. #else
  28. static inline struct dax_device *dax_get_by_host(const char *host)
  29. {
  30. return NULL;
  31. }
  32. static inline void put_dax(struct dax_device *dax_dev)
  33. {
  34. }
  35. #endif
  36. int bdev_dax_pgoff(struct block_device *, sector_t, size_t, pgoff_t *pgoff);
  37. #if IS_ENABLED(CONFIG_FS_DAX)
  38. int __bdev_dax_supported(struct super_block *sb, int blocksize);
  39. static inline int bdev_dax_supported(struct super_block *sb, int blocksize)
  40. {
  41. return __bdev_dax_supported(sb, blocksize);
  42. }
  43. static inline struct dax_device *fs_dax_get_by_host(const char *host)
  44. {
  45. return dax_get_by_host(host);
  46. }
  47. static inline void fs_put_dax(struct dax_device *dax_dev)
  48. {
  49. put_dax(dax_dev);
  50. }
  51. #else
  52. static inline int bdev_dax_supported(struct super_block *sb, int blocksize)
  53. {
  54. return -EOPNOTSUPP;
  55. }
  56. static inline struct dax_device *fs_dax_get_by_host(const char *host)
  57. {
  58. return NULL;
  59. }
  60. static inline void fs_put_dax(struct dax_device *dax_dev)
  61. {
  62. }
  63. #endif
  64. int dax_read_lock(void);
  65. void dax_read_unlock(int id);
  66. struct dax_device *alloc_dax(void *private, const char *host,
  67. const struct dax_operations *ops);
  68. bool dax_alive(struct dax_device *dax_dev);
  69. void kill_dax(struct dax_device *dax_dev);
  70. void *dax_get_private(struct dax_device *dax_dev);
  71. long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
  72. void **kaddr, pfn_t *pfn);
  73. size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  74. size_t bytes, struct iov_iter *i);
  75. void dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
  76. size_t size);
  77. void dax_write_cache(struct dax_device *dax_dev, bool wc);
  78. bool dax_write_cache_enabled(struct dax_device *dax_dev);
  79. /*
  80. * We use lowest available bit in exceptional entry for locking, one bit for
  81. * the entry size (PMD) and two more to tell us if the entry is a zero page or
  82. * an empty entry that is just used for locking. In total four special bits.
  83. *
  84. * If the PMD bit isn't set the entry has size PAGE_SIZE, and if the ZERO_PAGE
  85. * and EMPTY bits aren't set the entry is a normal DAX entry with a filesystem
  86. * block allocation.
  87. */
  88. #define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 4)
  89. #define RADIX_DAX_ENTRY_LOCK (1 << RADIX_TREE_EXCEPTIONAL_SHIFT)
  90. #define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1))
  91. #define RADIX_DAX_ZERO_PAGE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2))
  92. #define RADIX_DAX_EMPTY (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 3))
  93. static inline unsigned long dax_radix_sector(void *entry)
  94. {
  95. return (unsigned long)entry >> RADIX_DAX_SHIFT;
  96. }
  97. static inline void *dax_radix_locked_entry(sector_t sector, unsigned long flags)
  98. {
  99. return (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY | flags |
  100. ((unsigned long)sector << RADIX_DAX_SHIFT) |
  101. RADIX_DAX_ENTRY_LOCK);
  102. }
  103. ssize_t dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  104. const struct iomap_ops *ops);
  105. int dax_iomap_fault(struct vm_fault *vmf, enum page_entry_size pe_size,
  106. const struct iomap_ops *ops);
  107. int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index);
  108. int dax_invalidate_mapping_entry_sync(struct address_space *mapping,
  109. pgoff_t index);
  110. #ifdef CONFIG_FS_DAX
  111. int __dax_zero_page_range(struct block_device *bdev,
  112. struct dax_device *dax_dev, sector_t sector,
  113. unsigned int offset, unsigned int length);
  114. #else
  115. static inline int __dax_zero_page_range(struct block_device *bdev,
  116. struct dax_device *dax_dev, sector_t sector,
  117. unsigned int offset, unsigned int length)
  118. {
  119. return -ENXIO;
  120. }
  121. #endif
  122. #ifdef CONFIG_FS_DAX_PMD
  123. static inline unsigned int dax_radix_order(void *entry)
  124. {
  125. if ((unsigned long)entry & RADIX_DAX_PMD)
  126. return PMD_SHIFT - PAGE_SHIFT;
  127. return 0;
  128. }
  129. #else
  130. static inline unsigned int dax_radix_order(void *entry)
  131. {
  132. return 0;
  133. }
  134. #endif
  135. static inline bool dax_mapping(struct address_space *mapping)
  136. {
  137. return mapping->host && IS_DAX(mapping->host);
  138. }
  139. struct writeback_control;
  140. int dax_writeback_mapping_range(struct address_space *mapping,
  141. struct block_device *bdev, struct writeback_control *wbc);
  142. #endif