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