xref: /linux/drivers/dax/super.c (revision e511c4a3d2a1f64aafc1f5df37a2ffcf7ef91b55)
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  * @mode: indicator on normal access or recovery write
121  * @kaddr: output parameter that returns a virtual address mapping of pfn
122  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
123  *
124  * Return: negative errno if an error occurs, otherwise the number of
125  * pages accessible at the device relative @pgoff.
126  */
127 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
128 		enum dax_access_mode mode, void **kaddr, pfn_t *pfn)
129 {
130 	long avail;
131 
132 	if (!dax_dev)
133 		return -EOPNOTSUPP;
134 
135 	if (!dax_alive(dax_dev))
136 		return -ENXIO;
137 
138 	if (nr_pages < 0)
139 		return -EINVAL;
140 
141 	avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
142 			mode, kaddr, pfn);
143 	if (!avail)
144 		return -ERANGE;
145 	return min(avail, nr_pages);
146 }
147 EXPORT_SYMBOL_GPL(dax_direct_access);
148 
149 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
150 		size_t bytes, struct iov_iter *i)
151 {
152 	if (!dax_alive(dax_dev))
153 		return 0;
154 
155 	/*
156 	 * The userspace address for the memory copy has already been validated
157 	 * via access_ok() in vfs_write, so use the 'no check' version to bypass
158 	 * the HARDENED_USERCOPY overhead.
159 	 */
160 	if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
161 		return _copy_from_iter_flushcache(addr, bytes, i);
162 	return _copy_from_iter(addr, bytes, i);
163 }
164 
165 size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
166 		size_t bytes, struct iov_iter *i)
167 {
168 	if (!dax_alive(dax_dev))
169 		return 0;
170 
171 	/*
172 	 * The userspace address for the memory copy has already been validated
173 	 * via access_ok() in vfs_red, so use the 'no check' version to bypass
174 	 * the HARDENED_USERCOPY overhead.
175 	 */
176 	if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
177 		return _copy_mc_to_iter(addr, bytes, i);
178 	return _copy_to_iter(addr, bytes, i);
179 }
180 
181 int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
182 			size_t nr_pages)
183 {
184 	if (!dax_alive(dax_dev))
185 		return -ENXIO;
186 	/*
187 	 * There are no callers that want to zero more than one page as of now.
188 	 * Once users are there, this check can be removed after the
189 	 * device mapper code has been updated to split ranges across targets.
190 	 */
191 	if (nr_pages != 1)
192 		return -EIO;
193 
194 	return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
195 }
196 EXPORT_SYMBOL_GPL(dax_zero_page_range);
197 
198 #ifdef CONFIG_ARCH_HAS_PMEM_API
199 void arch_wb_cache_pmem(void *addr, size_t size);
200 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
201 {
202 	if (unlikely(!dax_write_cache_enabled(dax_dev)))
203 		return;
204 
205 	arch_wb_cache_pmem(addr, size);
206 }
207 #else
208 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
209 {
210 }
211 #endif
212 EXPORT_SYMBOL_GPL(dax_flush);
213 
214 void dax_write_cache(struct dax_device *dax_dev, bool wc)
215 {
216 	if (wc)
217 		set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
218 	else
219 		clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
220 }
221 EXPORT_SYMBOL_GPL(dax_write_cache);
222 
223 bool dax_write_cache_enabled(struct dax_device *dax_dev)
224 {
225 	return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
226 }
227 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
228 
229 bool dax_synchronous(struct dax_device *dax_dev)
230 {
231 	return test_bit(DAXDEV_SYNC, &dax_dev->flags);
232 }
233 EXPORT_SYMBOL_GPL(dax_synchronous);
234 
235 void set_dax_synchronous(struct dax_device *dax_dev)
236 {
237 	set_bit(DAXDEV_SYNC, &dax_dev->flags);
238 }
239 EXPORT_SYMBOL_GPL(set_dax_synchronous);
240 
241 void set_dax_nocache(struct dax_device *dax_dev)
242 {
243 	set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
244 }
245 EXPORT_SYMBOL_GPL(set_dax_nocache);
246 
247 void set_dax_nomc(struct dax_device *dax_dev)
248 {
249 	set_bit(DAXDEV_NOMC, &dax_dev->flags);
250 }
251 EXPORT_SYMBOL_GPL(set_dax_nomc);
252 
253 bool dax_alive(struct dax_device *dax_dev)
254 {
255 	lockdep_assert_held(&dax_srcu);
256 	return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
257 }
258 EXPORT_SYMBOL_GPL(dax_alive);
259 
260 /*
261  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
262  * that any fault handlers or operations that might have seen
263  * dax_alive(), have completed.  Any operations that start after
264  * synchronize_srcu() has run will abort upon seeing !dax_alive().
265  */
266 void kill_dax(struct dax_device *dax_dev)
267 {
268 	if (!dax_dev)
269 		return;
270 
271 	clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
272 	synchronize_srcu(&dax_srcu);
273 }
274 EXPORT_SYMBOL_GPL(kill_dax);
275 
276 void run_dax(struct dax_device *dax_dev)
277 {
278 	set_bit(DAXDEV_ALIVE, &dax_dev->flags);
279 }
280 EXPORT_SYMBOL_GPL(run_dax);
281 
282 static struct inode *dax_alloc_inode(struct super_block *sb)
283 {
284 	struct dax_device *dax_dev;
285 	struct inode *inode;
286 
287 	dax_dev = alloc_inode_sb(sb, dax_cache, GFP_KERNEL);
288 	if (!dax_dev)
289 		return NULL;
290 
291 	inode = &dax_dev->inode;
292 	inode->i_rdev = 0;
293 	return inode;
294 }
295 
296 static struct dax_device *to_dax_dev(struct inode *inode)
297 {
298 	return container_of(inode, struct dax_device, inode);
299 }
300 
301 static void dax_free_inode(struct inode *inode)
302 {
303 	struct dax_device *dax_dev = to_dax_dev(inode);
304 	if (inode->i_rdev)
305 		ida_simple_remove(&dax_minor_ida, iminor(inode));
306 	kmem_cache_free(dax_cache, dax_dev);
307 }
308 
309 static void dax_destroy_inode(struct inode *inode)
310 {
311 	struct dax_device *dax_dev = to_dax_dev(inode);
312 	WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
313 			"kill_dax() must be called before final iput()\n");
314 }
315 
316 static const struct super_operations dax_sops = {
317 	.statfs = simple_statfs,
318 	.alloc_inode = dax_alloc_inode,
319 	.destroy_inode = dax_destroy_inode,
320 	.free_inode = dax_free_inode,
321 	.drop_inode = generic_delete_inode,
322 };
323 
324 static int dax_init_fs_context(struct fs_context *fc)
325 {
326 	struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
327 	if (!ctx)
328 		return -ENOMEM;
329 	ctx->ops = &dax_sops;
330 	return 0;
331 }
332 
333 static struct file_system_type dax_fs_type = {
334 	.name		= "dax",
335 	.init_fs_context = dax_init_fs_context,
336 	.kill_sb	= kill_anon_super,
337 };
338 
339 static int dax_test(struct inode *inode, void *data)
340 {
341 	dev_t devt = *(dev_t *) data;
342 
343 	return inode->i_rdev == devt;
344 }
345 
346 static int dax_set(struct inode *inode, void *data)
347 {
348 	dev_t devt = *(dev_t *) data;
349 
350 	inode->i_rdev = devt;
351 	return 0;
352 }
353 
354 static struct dax_device *dax_dev_get(dev_t devt)
355 {
356 	struct dax_device *dax_dev;
357 	struct inode *inode;
358 
359 	inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
360 			dax_test, dax_set, &devt);
361 
362 	if (!inode)
363 		return NULL;
364 
365 	dax_dev = to_dax_dev(inode);
366 	if (inode->i_state & I_NEW) {
367 		set_bit(DAXDEV_ALIVE, &dax_dev->flags);
368 		inode->i_cdev = &dax_dev->cdev;
369 		inode->i_mode = S_IFCHR;
370 		inode->i_flags = S_DAX;
371 		mapping_set_gfp_mask(&inode->i_data, GFP_USER);
372 		unlock_new_inode(inode);
373 	}
374 
375 	return dax_dev;
376 }
377 
378 struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
379 {
380 	struct dax_device *dax_dev;
381 	dev_t devt;
382 	int minor;
383 
384 	if (WARN_ON_ONCE(ops && !ops->zero_page_range))
385 		return ERR_PTR(-EINVAL);
386 
387 	minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
388 	if (minor < 0)
389 		return ERR_PTR(-ENOMEM);
390 
391 	devt = MKDEV(MAJOR(dax_devt), minor);
392 	dax_dev = dax_dev_get(devt);
393 	if (!dax_dev)
394 		goto err_dev;
395 
396 	dax_dev->ops = ops;
397 	dax_dev->private = private;
398 	return dax_dev;
399 
400  err_dev:
401 	ida_simple_remove(&dax_minor_ida, minor);
402 	return ERR_PTR(-ENOMEM);
403 }
404 EXPORT_SYMBOL_GPL(alloc_dax);
405 
406 void put_dax(struct dax_device *dax_dev)
407 {
408 	if (!dax_dev)
409 		return;
410 	iput(&dax_dev->inode);
411 }
412 EXPORT_SYMBOL_GPL(put_dax);
413 
414 /**
415  * inode_dax: convert a public inode into its dax_dev
416  * @inode: An inode with i_cdev pointing to a dax_dev
417  *
418  * Note this is not equivalent to to_dax_dev() which is for private
419  * internal use where we know the inode filesystem type == dax_fs_type.
420  */
421 struct dax_device *inode_dax(struct inode *inode)
422 {
423 	struct cdev *cdev = inode->i_cdev;
424 
425 	return container_of(cdev, struct dax_device, cdev);
426 }
427 EXPORT_SYMBOL_GPL(inode_dax);
428 
429 struct inode *dax_inode(struct dax_device *dax_dev)
430 {
431 	return &dax_dev->inode;
432 }
433 EXPORT_SYMBOL_GPL(dax_inode);
434 
435 void *dax_get_private(struct dax_device *dax_dev)
436 {
437 	if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
438 		return NULL;
439 	return dax_dev->private;
440 }
441 EXPORT_SYMBOL_GPL(dax_get_private);
442 
443 static void init_once(void *_dax_dev)
444 {
445 	struct dax_device *dax_dev = _dax_dev;
446 	struct inode *inode = &dax_dev->inode;
447 
448 	memset(dax_dev, 0, sizeof(*dax_dev));
449 	inode_init_once(inode);
450 }
451 
452 static int dax_fs_init(void)
453 {
454 	int rc;
455 
456 	dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
457 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
458 			 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
459 			init_once);
460 	if (!dax_cache)
461 		return -ENOMEM;
462 
463 	dax_mnt = kern_mount(&dax_fs_type);
464 	if (IS_ERR(dax_mnt)) {
465 		rc = PTR_ERR(dax_mnt);
466 		goto err_mount;
467 	}
468 	dax_superblock = dax_mnt->mnt_sb;
469 
470 	return 0;
471 
472  err_mount:
473 	kmem_cache_destroy(dax_cache);
474 
475 	return rc;
476 }
477 
478 static void dax_fs_exit(void)
479 {
480 	kern_unmount(dax_mnt);
481 	rcu_barrier();
482 	kmem_cache_destroy(dax_cache);
483 }
484 
485 static int __init dax_core_init(void)
486 {
487 	int rc;
488 
489 	rc = dax_fs_init();
490 	if (rc)
491 		return rc;
492 
493 	rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
494 	if (rc)
495 		goto err_chrdev;
496 
497 	rc = dax_bus_init();
498 	if (rc)
499 		goto err_bus;
500 	return 0;
501 
502 err_bus:
503 	unregister_chrdev_region(dax_devt, MINORMASK+1);
504 err_chrdev:
505 	dax_fs_exit();
506 	return 0;
507 }
508 
509 static void __exit dax_core_exit(void)
510 {
511 	dax_bus_exit();
512 	unregister_chrdev_region(dax_devt, MINORMASK+1);
513 	ida_destroy(&dax_minor_ida);
514 	dax_fs_exit();
515 }
516 
517 MODULE_AUTHOR("Intel Corporation");
518 MODULE_LICENSE("GPL v2");
519 subsys_initcall(dax_core_init);
520 module_exit(dax_core_exit);
521