xref: /openbmc/linux/drivers/dax/super.c (revision 31eeb6b0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright(c) 2017 Intel Corporation. All rights reserved.
4  */
5 #include <linux/pagemap.h>
6 #include <linux/module.h>
7 #include <linux/mount.h>
8 #include <linux/pseudo_fs.h>
9 #include <linux/magic.h>
10 #include <linux/pfn_t.h>
11 #include <linux/cdev.h>
12 #include <linux/slab.h>
13 #include <linux/uio.h>
14 #include <linux/dax.h>
15 #include <linux/fs.h>
16 #include "dax-private.h"
17 
18 /**
19  * struct dax_device - anchor object for dax services
20  * @inode: core vfs
21  * @cdev: optional character interface for "device dax"
22  * @private: dax driver private data
23  * @flags: state and boolean properties
24  */
25 struct dax_device {
26 	struct inode inode;
27 	struct cdev cdev;
28 	void *private;
29 	unsigned long flags;
30 	const struct dax_operations *ops;
31 };
32 
33 static dev_t dax_devt;
34 DEFINE_STATIC_SRCU(dax_srcu);
35 static struct vfsmount *dax_mnt;
36 static DEFINE_IDA(dax_minor_ida);
37 static struct kmem_cache *dax_cache __read_mostly;
38 static struct super_block *dax_superblock __read_mostly;
39 
40 int dax_read_lock(void)
41 {
42 	return srcu_read_lock(&dax_srcu);
43 }
44 EXPORT_SYMBOL_GPL(dax_read_lock);
45 
46 void dax_read_unlock(int id)
47 {
48 	srcu_read_unlock(&dax_srcu, id);
49 }
50 EXPORT_SYMBOL_GPL(dax_read_unlock);
51 
52 #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
53 #include <linux/blkdev.h>
54 
55 static DEFINE_XARRAY(dax_hosts);
56 
57 int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
58 {
59 	return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
60 }
61 EXPORT_SYMBOL_GPL(dax_add_host);
62 
63 void dax_remove_host(struct gendisk *disk)
64 {
65 	xa_erase(&dax_hosts, (unsigned long)disk);
66 }
67 EXPORT_SYMBOL_GPL(dax_remove_host);
68 
69 /**
70  * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
71  * @bdev: block device to find a dax_device for
72  * @start_off: returns the byte offset into the dax_device that @bdev starts
73  */
74 struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off)
75 {
76 	struct dax_device *dax_dev;
77 	u64 part_size;
78 	int id;
79 
80 	if (!blk_queue_dax(bdev->bd_disk->queue))
81 		return NULL;
82 
83 	*start_off = get_start_sect(bdev) * SECTOR_SIZE;
84 	part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
85 	if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
86 		pr_info("%pg: error: unaligned partition for dax\n", bdev);
87 		return NULL;
88 	}
89 
90 	id = dax_read_lock();
91 	dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
92 	if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
93 		dax_dev = NULL;
94 	dax_read_unlock(id);
95 
96 	return dax_dev;
97 }
98 EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
99 #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
100 
101 enum dax_device_flags {
102 	/* !alive + rcu grace period == no new operations / mappings */
103 	DAXDEV_ALIVE,
104 	/* gate whether dax_flush() calls the low level flush routine */
105 	DAXDEV_WRITE_CACHE,
106 	/* flag to check if device supports synchronous flush */
107 	DAXDEV_SYNC,
108 	/* do not leave the caches dirty after writes */
109 	DAXDEV_NOCACHE,
110 	/* handle CPU fetch exceptions during reads */
111 	DAXDEV_NOMC,
112 };
113 
114 /**
115  * dax_direct_access() - translate a device pgoff to an absolute pfn
116  * @dax_dev: a dax_device instance representing the logical memory range
117  * @pgoff: offset in pages from the start of the device to translate
118  * @nr_pages: number of consecutive pages caller can handle relative to @pfn
119  * @kaddr: output parameter that returns a virtual address mapping of pfn
120  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
121  *
122  * Return: negative errno if an error occurs, otherwise the number of
123  * pages accessible at the device relative @pgoff.
124  */
125 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
126 		void **kaddr, pfn_t *pfn)
127 {
128 	long avail;
129 
130 	if (!dax_dev)
131 		return -EOPNOTSUPP;
132 
133 	if (!dax_alive(dax_dev))
134 		return -ENXIO;
135 
136 	if (nr_pages < 0)
137 		return -EINVAL;
138 
139 	avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
140 			kaddr, pfn);
141 	if (!avail)
142 		return -ERANGE;
143 	return min(avail, nr_pages);
144 }
145 EXPORT_SYMBOL_GPL(dax_direct_access);
146 
147 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
148 		size_t bytes, struct iov_iter *i)
149 {
150 	if (!dax_alive(dax_dev))
151 		return 0;
152 
153 	/*
154 	 * The userspace address for the memory copy has already been validated
155 	 * via access_ok() in vfs_write, so use the 'no check' version to bypass
156 	 * the HARDENED_USERCOPY overhead.
157 	 */
158 	if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
159 		return _copy_from_iter_flushcache(addr, bytes, i);
160 	return _copy_from_iter(addr, bytes, i);
161 }
162 
163 size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
164 		size_t bytes, struct iov_iter *i)
165 {
166 	if (!dax_alive(dax_dev))
167 		return 0;
168 
169 	/*
170 	 * The userspace address for the memory copy has already been validated
171 	 * via access_ok() in vfs_red, so use the 'no check' version to bypass
172 	 * the HARDENED_USERCOPY overhead.
173 	 */
174 	if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
175 		return _copy_mc_to_iter(addr, bytes, i);
176 	return _copy_to_iter(addr, bytes, i);
177 }
178 
179 int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
180 			size_t nr_pages)
181 {
182 	if (!dax_alive(dax_dev))
183 		return -ENXIO;
184 	/*
185 	 * There are no callers that want to zero more than one page as of now.
186 	 * Once users are there, this check can be removed after the
187 	 * device mapper code has been updated to split ranges across targets.
188 	 */
189 	if (nr_pages != 1)
190 		return -EIO;
191 
192 	return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
193 }
194 EXPORT_SYMBOL_GPL(dax_zero_page_range);
195 
196 #ifdef CONFIG_ARCH_HAS_PMEM_API
197 void arch_wb_cache_pmem(void *addr, size_t size);
198 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
199 {
200 	if (unlikely(!dax_write_cache_enabled(dax_dev)))
201 		return;
202 
203 	arch_wb_cache_pmem(addr, size);
204 }
205 #else
206 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
207 {
208 }
209 #endif
210 EXPORT_SYMBOL_GPL(dax_flush);
211 
212 void dax_write_cache(struct dax_device *dax_dev, bool wc)
213 {
214 	if (wc)
215 		set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
216 	else
217 		clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
218 }
219 EXPORT_SYMBOL_GPL(dax_write_cache);
220 
221 bool dax_write_cache_enabled(struct dax_device *dax_dev)
222 {
223 	return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
224 }
225 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
226 
227 bool dax_synchronous(struct dax_device *dax_dev)
228 {
229 	return test_bit(DAXDEV_SYNC, &dax_dev->flags);
230 }
231 EXPORT_SYMBOL_GPL(dax_synchronous);
232 
233 void set_dax_synchronous(struct dax_device *dax_dev)
234 {
235 	set_bit(DAXDEV_SYNC, &dax_dev->flags);
236 }
237 EXPORT_SYMBOL_GPL(set_dax_synchronous);
238 
239 void set_dax_nocache(struct dax_device *dax_dev)
240 {
241 	set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
242 }
243 EXPORT_SYMBOL_GPL(set_dax_nocache);
244 
245 void set_dax_nomc(struct dax_device *dax_dev)
246 {
247 	set_bit(DAXDEV_NOMC, &dax_dev->flags);
248 }
249 EXPORT_SYMBOL_GPL(set_dax_nomc);
250 
251 bool dax_alive(struct dax_device *dax_dev)
252 {
253 	lockdep_assert_held(&dax_srcu);
254 	return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
255 }
256 EXPORT_SYMBOL_GPL(dax_alive);
257 
258 /*
259  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
260  * that any fault handlers or operations that might have seen
261  * dax_alive(), have completed.  Any operations that start after
262  * synchronize_srcu() has run will abort upon seeing !dax_alive().
263  */
264 void kill_dax(struct dax_device *dax_dev)
265 {
266 	if (!dax_dev)
267 		return;
268 
269 	clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
270 	synchronize_srcu(&dax_srcu);
271 }
272 EXPORT_SYMBOL_GPL(kill_dax);
273 
274 void run_dax(struct dax_device *dax_dev)
275 {
276 	set_bit(DAXDEV_ALIVE, &dax_dev->flags);
277 }
278 EXPORT_SYMBOL_GPL(run_dax);
279 
280 static struct inode *dax_alloc_inode(struct super_block *sb)
281 {
282 	struct dax_device *dax_dev;
283 	struct inode *inode;
284 
285 	dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
286 	if (!dax_dev)
287 		return NULL;
288 
289 	inode = &dax_dev->inode;
290 	inode->i_rdev = 0;
291 	return inode;
292 }
293 
294 static struct dax_device *to_dax_dev(struct inode *inode)
295 {
296 	return container_of(inode, struct dax_device, inode);
297 }
298 
299 static void dax_free_inode(struct inode *inode)
300 {
301 	struct dax_device *dax_dev = to_dax_dev(inode);
302 	if (inode->i_rdev)
303 		ida_simple_remove(&dax_minor_ida, iminor(inode));
304 	kmem_cache_free(dax_cache, dax_dev);
305 }
306 
307 static void dax_destroy_inode(struct inode *inode)
308 {
309 	struct dax_device *dax_dev = to_dax_dev(inode);
310 	WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
311 			"kill_dax() must be called before final iput()\n");
312 }
313 
314 static const struct super_operations dax_sops = {
315 	.statfs = simple_statfs,
316 	.alloc_inode = dax_alloc_inode,
317 	.destroy_inode = dax_destroy_inode,
318 	.free_inode = dax_free_inode,
319 	.drop_inode = generic_delete_inode,
320 };
321 
322 static int dax_init_fs_context(struct fs_context *fc)
323 {
324 	struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
325 	if (!ctx)
326 		return -ENOMEM;
327 	ctx->ops = &dax_sops;
328 	return 0;
329 }
330 
331 static struct file_system_type dax_fs_type = {
332 	.name		= "dax",
333 	.init_fs_context = dax_init_fs_context,
334 	.kill_sb	= kill_anon_super,
335 };
336 
337 static int dax_test(struct inode *inode, void *data)
338 {
339 	dev_t devt = *(dev_t *) data;
340 
341 	return inode->i_rdev == devt;
342 }
343 
344 static int dax_set(struct inode *inode, void *data)
345 {
346 	dev_t devt = *(dev_t *) data;
347 
348 	inode->i_rdev = devt;
349 	return 0;
350 }
351 
352 static struct dax_device *dax_dev_get(dev_t devt)
353 {
354 	struct dax_device *dax_dev;
355 	struct inode *inode;
356 
357 	inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
358 			dax_test, dax_set, &devt);
359 
360 	if (!inode)
361 		return NULL;
362 
363 	dax_dev = to_dax_dev(inode);
364 	if (inode->i_state & I_NEW) {
365 		set_bit(DAXDEV_ALIVE, &dax_dev->flags);
366 		inode->i_cdev = &dax_dev->cdev;
367 		inode->i_mode = S_IFCHR;
368 		inode->i_flags = S_DAX;
369 		mapping_set_gfp_mask(&inode->i_data, GFP_USER);
370 		unlock_new_inode(inode);
371 	}
372 
373 	return dax_dev;
374 }
375 
376 struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
377 {
378 	struct dax_device *dax_dev;
379 	dev_t devt;
380 	int minor;
381 
382 	if (WARN_ON_ONCE(ops && !ops->zero_page_range))
383 		return ERR_PTR(-EINVAL);
384 
385 	minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
386 	if (minor < 0)
387 		return ERR_PTR(-ENOMEM);
388 
389 	devt = MKDEV(MAJOR(dax_devt), minor);
390 	dax_dev = dax_dev_get(devt);
391 	if (!dax_dev)
392 		goto err_dev;
393 
394 	dax_dev->ops = ops;
395 	dax_dev->private = private;
396 	return dax_dev;
397 
398  err_dev:
399 	ida_simple_remove(&dax_minor_ida, minor);
400 	return ERR_PTR(-ENOMEM);
401 }
402 EXPORT_SYMBOL_GPL(alloc_dax);
403 
404 void put_dax(struct dax_device *dax_dev)
405 {
406 	if (!dax_dev)
407 		return;
408 	iput(&dax_dev->inode);
409 }
410 EXPORT_SYMBOL_GPL(put_dax);
411 
412 /**
413  * inode_dax: convert a public inode into its dax_dev
414  * @inode: An inode with i_cdev pointing to a dax_dev
415  *
416  * Note this is not equivalent to to_dax_dev() which is for private
417  * internal use where we know the inode filesystem type == dax_fs_type.
418  */
419 struct dax_device *inode_dax(struct inode *inode)
420 {
421 	struct cdev *cdev = inode->i_cdev;
422 
423 	return container_of(cdev, struct dax_device, cdev);
424 }
425 EXPORT_SYMBOL_GPL(inode_dax);
426 
427 struct inode *dax_inode(struct dax_device *dax_dev)
428 {
429 	return &dax_dev->inode;
430 }
431 EXPORT_SYMBOL_GPL(dax_inode);
432 
433 void *dax_get_private(struct dax_device *dax_dev)
434 {
435 	if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
436 		return NULL;
437 	return dax_dev->private;
438 }
439 EXPORT_SYMBOL_GPL(dax_get_private);
440 
441 static void init_once(void *_dax_dev)
442 {
443 	struct dax_device *dax_dev = _dax_dev;
444 	struct inode *inode = &dax_dev->inode;
445 
446 	memset(dax_dev, 0, sizeof(*dax_dev));
447 	inode_init_once(inode);
448 }
449 
450 static int dax_fs_init(void)
451 {
452 	int rc;
453 
454 	dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
455 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
456 			 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
457 			init_once);
458 	if (!dax_cache)
459 		return -ENOMEM;
460 
461 	dax_mnt = kern_mount(&dax_fs_type);
462 	if (IS_ERR(dax_mnt)) {
463 		rc = PTR_ERR(dax_mnt);
464 		goto err_mount;
465 	}
466 	dax_superblock = dax_mnt->mnt_sb;
467 
468 	return 0;
469 
470  err_mount:
471 	kmem_cache_destroy(dax_cache);
472 
473 	return rc;
474 }
475 
476 static void dax_fs_exit(void)
477 {
478 	kern_unmount(dax_mnt);
479 	kmem_cache_destroy(dax_cache);
480 }
481 
482 static int __init dax_core_init(void)
483 {
484 	int rc;
485 
486 	rc = dax_fs_init();
487 	if (rc)
488 		return rc;
489 
490 	rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
491 	if (rc)
492 		goto err_chrdev;
493 
494 	rc = dax_bus_init();
495 	if (rc)
496 		goto err_bus;
497 	return 0;
498 
499 err_bus:
500 	unregister_chrdev_region(dax_devt, MINORMASK+1);
501 err_chrdev:
502 	dax_fs_exit();
503 	return 0;
504 }
505 
506 static void __exit dax_core_exit(void)
507 {
508 	dax_bus_exit();
509 	unregister_chrdev_region(dax_devt, MINORMASK+1);
510 	ida_destroy(&dax_minor_ida);
511 	dax_fs_exit();
512 }
513 
514 MODULE_AUTHOR("Intel Corporation");
515 MODULE_LICENSE("GPL v2");
516 subsys_initcall(dax_core_init);
517 module_exit(dax_core_exit);
518