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