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