xref: /openbmc/linux/drivers/dax/super.c (revision fd1d00ec)
15b497af4SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
27b6be844SDan Williams /*
37b6be844SDan Williams  * Copyright(c) 2017 Intel Corporation. All rights reserved.
47b6be844SDan Williams  */
57b6be844SDan Williams #include <linux/pagemap.h>
67b6be844SDan Williams #include <linux/module.h>
77b6be844SDan Williams #include <linux/mount.h>
875d4e06fSDavid Howells #include <linux/pseudo_fs.h>
97b6be844SDan Williams #include <linux/magic.h>
10569d0365SDan Williams #include <linux/pfn_t.h>
117b6be844SDan Williams #include <linux/cdev.h>
127b6be844SDan Williams #include <linux/slab.h>
137e026c8cSDan Williams #include <linux/uio.h>
146568b08bSDan Williams #include <linux/dax.h>
157b6be844SDan Williams #include <linux/fs.h>
1651cf784cSDan Williams #include "dax-private.h"
177b6be844SDan Williams 
181b764601SChristoph Hellwig /**
191b764601SChristoph Hellwig  * struct dax_device - anchor object for dax services
201b764601SChristoph Hellwig  * @inode: core vfs
211b764601SChristoph Hellwig  * @cdev: optional character interface for "device dax"
221b764601SChristoph Hellwig  * @private: dax driver private data
231b764601SChristoph Hellwig  * @flags: state and boolean properties
241b764601SChristoph Hellwig  */
251b764601SChristoph Hellwig struct dax_device {
261b764601SChristoph Hellwig 	struct inode inode;
271b764601SChristoph Hellwig 	struct cdev cdev;
281b764601SChristoph Hellwig 	void *private;
291b764601SChristoph Hellwig 	unsigned long flags;
301b764601SChristoph Hellwig 	const struct dax_operations *ops;
311b764601SChristoph Hellwig };
321b764601SChristoph Hellwig 
337b6be844SDan Williams static dev_t dax_devt;
347b6be844SDan Williams DEFINE_STATIC_SRCU(dax_srcu);
357b6be844SDan Williams static struct vfsmount *dax_mnt;
367b6be844SDan Williams static DEFINE_IDA(dax_minor_ida);
377b6be844SDan Williams static struct kmem_cache *dax_cache __read_mostly;
387b6be844SDan Williams static struct super_block *dax_superblock __read_mostly;
397b6be844SDan Williams 
407b6be844SDan Williams int dax_read_lock(void)
417b6be844SDan Williams {
427b6be844SDan Williams 	return srcu_read_lock(&dax_srcu);
437b6be844SDan Williams }
447b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_lock);
457b6be844SDan Williams 
467b6be844SDan Williams void dax_read_unlock(int id)
477b6be844SDan Williams {
487b6be844SDan Williams 	srcu_read_unlock(&dax_srcu, id);
497b6be844SDan Williams }
507b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_unlock);
517b6be844SDan Williams 
525d2a228bSChristoph Hellwig #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
53e765f13eSChristoph Hellwig #include <linux/blkdev.h>
54e765f13eSChristoph Hellwig 
55fb08a190SChristoph Hellwig static DEFINE_XARRAY(dax_hosts);
56fb08a190SChristoph Hellwig 
57fb08a190SChristoph Hellwig int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
58fb08a190SChristoph Hellwig {
59fb08a190SChristoph Hellwig 	return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
60fb08a190SChristoph Hellwig }
61fb08a190SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_add_host);
62fb08a190SChristoph Hellwig 
63fb08a190SChristoph Hellwig void dax_remove_host(struct gendisk *disk)
64fb08a190SChristoph Hellwig {
65fb08a190SChristoph Hellwig 	xa_erase(&dax_hosts, (unsigned long)disk);
66fb08a190SChristoph Hellwig }
67fb08a190SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_remove_host);
68fb08a190SChristoph Hellwig 
691b764601SChristoph Hellwig /**
70fb08a190SChristoph Hellwig  * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
71fb08a190SChristoph Hellwig  * @bdev: block device to find a dax_device for
72cd913c76SChristoph Hellwig  * @start_off: returns the byte offset into the dax_device that @bdev starts
731b764601SChristoph Hellwig  */
74cd913c76SChristoph Hellwig struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off)
7578f35473SDan Williams {
76fb08a190SChristoph Hellwig 	struct dax_device *dax_dev;
77cd913c76SChristoph Hellwig 	u64 part_size;
78fb08a190SChristoph Hellwig 	int id;
79fb08a190SChristoph Hellwig 
80e556f6baSChristoph Hellwig 	if (!blk_queue_dax(bdev->bd_disk->queue))
8178f35473SDan Williams 		return NULL;
82fb08a190SChristoph Hellwig 
83cd913c76SChristoph Hellwig 	*start_off = get_start_sect(bdev) * SECTOR_SIZE;
84cd913c76SChristoph Hellwig 	part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
85cd913c76SChristoph Hellwig 	if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
860c445871SChristoph Hellwig 		pr_info("%pg: error: unaligned partition for dax\n", bdev);
870c445871SChristoph Hellwig 		return NULL;
880c445871SChristoph Hellwig 	}
890c445871SChristoph Hellwig 
90fb08a190SChristoph Hellwig 	id = dax_read_lock();
91fb08a190SChristoph Hellwig 	dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
92fb08a190SChristoph Hellwig 	if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
93fb08a190SChristoph Hellwig 		dax_dev = NULL;
94fb08a190SChristoph Hellwig 	dax_read_unlock(id);
95fb08a190SChristoph Hellwig 
96fb08a190SChristoph Hellwig 	return dax_dev;
9778f35473SDan Williams }
9878f35473SDan Williams EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
995d2a228bSChristoph Hellwig #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
100ef510424SDan Williams 
1019a60c3efSDan Williams enum dax_device_flags {
1029a60c3efSDan Williams 	/* !alive + rcu grace period == no new operations / mappings */
1039a60c3efSDan Williams 	DAXDEV_ALIVE,
1046e0c90d6SDan Williams 	/* gate whether dax_flush() calls the low level flush routine */
1056e0c90d6SDan Williams 	DAXDEV_WRITE_CACHE,
106fefc1d97SPankaj Gupta 	/* flag to check if device supports synchronous flush */
107fefc1d97SPankaj Gupta 	DAXDEV_SYNC,
1089a60c3efSDan Williams };
1099a60c3efSDan Williams 
110b0686260SDan Williams /**
111b0686260SDan Williams  * dax_direct_access() - translate a device pgoff to an absolute pfn
112b0686260SDan Williams  * @dax_dev: a dax_device instance representing the logical memory range
113b0686260SDan Williams  * @pgoff: offset in pages from the start of the device to translate
114b0686260SDan Williams  * @nr_pages: number of consecutive pages caller can handle relative to @pfn
115b0686260SDan Williams  * @kaddr: output parameter that returns a virtual address mapping of pfn
116b0686260SDan Williams  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
117b0686260SDan Williams  *
118b0686260SDan Williams  * Return: negative errno if an error occurs, otherwise the number of
119b0686260SDan Williams  * pages accessible at the device relative @pgoff.
120b0686260SDan Williams  */
121b0686260SDan Williams long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
122b0686260SDan Williams 		void **kaddr, pfn_t *pfn)
123b0686260SDan Williams {
124b0686260SDan Williams 	long avail;
125b0686260SDan Williams 
126b0686260SDan Williams 	if (!dax_dev)
127b0686260SDan Williams 		return -EOPNOTSUPP;
128b0686260SDan Williams 
129b0686260SDan Williams 	if (!dax_alive(dax_dev))
130b0686260SDan Williams 		return -ENXIO;
131b0686260SDan Williams 
132b0686260SDan Williams 	if (nr_pages < 0)
133b05d4c57SIra Weiny 		return -EINVAL;
134b0686260SDan Williams 
135b0686260SDan Williams 	avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
136b0686260SDan Williams 			kaddr, pfn);
137b0686260SDan Williams 	if (!avail)
138b0686260SDan Williams 		return -ERANGE;
139b0686260SDan Williams 	return min(avail, nr_pages);
140b0686260SDan Williams }
141b0686260SDan Williams EXPORT_SYMBOL_GPL(dax_direct_access);
142b0686260SDan Williams 
1437e026c8cSDan Williams size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
1447e026c8cSDan Williams 		size_t bytes, struct iov_iter *i)
1457e026c8cSDan Williams {
1467e026c8cSDan Williams 	if (!dax_alive(dax_dev))
1477e026c8cSDan Williams 		return 0;
1487e026c8cSDan Williams 
1497e026c8cSDan Williams 	return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
1507e026c8cSDan Williams }
1517e026c8cSDan Williams EXPORT_SYMBOL_GPL(dax_copy_from_iter);
1527e026c8cSDan Williams 
153b3a9a0c3SDan Williams size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
154b3a9a0c3SDan Williams 		size_t bytes, struct iov_iter *i)
155b3a9a0c3SDan Williams {
156b3a9a0c3SDan Williams 	if (!dax_alive(dax_dev))
157b3a9a0c3SDan Williams 		return 0;
158b3a9a0c3SDan Williams 
159b3a9a0c3SDan Williams 	return dax_dev->ops->copy_to_iter(dax_dev, pgoff, addr, bytes, i);
160b3a9a0c3SDan Williams }
161b3a9a0c3SDan Williams EXPORT_SYMBOL_GPL(dax_copy_to_iter);
162b3a9a0c3SDan Williams 
163f605a263SVivek Goyal int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
164f605a263SVivek Goyal 			size_t nr_pages)
165f605a263SVivek Goyal {
166f605a263SVivek Goyal 	if (!dax_alive(dax_dev))
167f605a263SVivek Goyal 		return -ENXIO;
168f605a263SVivek Goyal 	/*
169f605a263SVivek Goyal 	 * There are no callers that want to zero more than one page as of now.
170f605a263SVivek Goyal 	 * Once users are there, this check can be removed after the
171f605a263SVivek Goyal 	 * device mapper code has been updated to split ranges across targets.
172f605a263SVivek Goyal 	 */
173f605a263SVivek Goyal 	if (nr_pages != 1)
174f605a263SVivek Goyal 		return -EIO;
175f605a263SVivek Goyal 
176f605a263SVivek Goyal 	return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
177f605a263SVivek Goyal }
178f605a263SVivek Goyal EXPORT_SYMBOL_GPL(dax_zero_page_range);
179f605a263SVivek Goyal 
180c3ca015fSMikulas Patocka #ifdef CONFIG_ARCH_HAS_PMEM_API
181c3ca015fSMikulas Patocka void arch_wb_cache_pmem(void *addr, size_t size);
182c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
183abebfbe2SDan Williams {
184808c340bSRoss Zwisler 	if (unlikely(!dax_write_cache_enabled(dax_dev)))
1856e0c90d6SDan Williams 		return;
1866e0c90d6SDan Williams 
187c3ca015fSMikulas Patocka 	arch_wb_cache_pmem(addr, size);
188abebfbe2SDan Williams }
189c3ca015fSMikulas Patocka #else
190c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
191c3ca015fSMikulas Patocka {
192c3ca015fSMikulas Patocka }
193c3ca015fSMikulas Patocka #endif
194abebfbe2SDan Williams EXPORT_SYMBOL_GPL(dax_flush);
195abebfbe2SDan Williams 
1966e0c90d6SDan Williams void dax_write_cache(struct dax_device *dax_dev, bool wc)
1976e0c90d6SDan Williams {
1986e0c90d6SDan Williams 	if (wc)
1996e0c90d6SDan Williams 		set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
2006e0c90d6SDan Williams 	else
2016e0c90d6SDan Williams 		clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
2026e0c90d6SDan Williams }
2036e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_write_cache);
2046e0c90d6SDan Williams 
205273752c9SVivek Goyal bool dax_write_cache_enabled(struct dax_device *dax_dev)
206273752c9SVivek Goyal {
207273752c9SVivek Goyal 	return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
208273752c9SVivek Goyal }
209273752c9SVivek Goyal EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
210273752c9SVivek Goyal 
211*fd1d00ecSChristoph Hellwig bool dax_synchronous(struct dax_device *dax_dev)
212fefc1d97SPankaj Gupta {
213fefc1d97SPankaj Gupta 	return test_bit(DAXDEV_SYNC, &dax_dev->flags);
214fefc1d97SPankaj Gupta }
215*fd1d00ecSChristoph Hellwig EXPORT_SYMBOL_GPL(dax_synchronous);
216fefc1d97SPankaj Gupta 
217*fd1d00ecSChristoph Hellwig void set_dax_synchronous(struct dax_device *dax_dev)
218fefc1d97SPankaj Gupta {
219fefc1d97SPankaj Gupta 	set_bit(DAXDEV_SYNC, &dax_dev->flags);
220fefc1d97SPankaj Gupta }
221*fd1d00ecSChristoph Hellwig EXPORT_SYMBOL_GPL(set_dax_synchronous);
222fefc1d97SPankaj Gupta 
2237b6be844SDan Williams bool dax_alive(struct dax_device *dax_dev)
2247b6be844SDan Williams {
2257b6be844SDan Williams 	lockdep_assert_held(&dax_srcu);
2269a60c3efSDan Williams 	return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
2277b6be844SDan Williams }
2287b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_alive);
2297b6be844SDan Williams 
2307b6be844SDan Williams /*
2317b6be844SDan Williams  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
2327b6be844SDan Williams  * that any fault handlers or operations that might have seen
2337b6be844SDan Williams  * dax_alive(), have completed.  Any operations that start after
2347b6be844SDan Williams  * synchronize_srcu() has run will abort upon seeing !dax_alive().
2357b6be844SDan Williams  */
2367b6be844SDan Williams void kill_dax(struct dax_device *dax_dev)
2377b6be844SDan Williams {
2387b6be844SDan Williams 	if (!dax_dev)
2397b6be844SDan Williams 		return;
2407b6be844SDan Williams 
2419a60c3efSDan Williams 	clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
2427b6be844SDan Williams 	synchronize_srcu(&dax_srcu);
2437b6be844SDan Williams }
2447b6be844SDan Williams EXPORT_SYMBOL_GPL(kill_dax);
2457b6be844SDan Williams 
2469567da0bSDan Williams void run_dax(struct dax_device *dax_dev)
2479567da0bSDan Williams {
2489567da0bSDan Williams 	set_bit(DAXDEV_ALIVE, &dax_dev->flags);
2499567da0bSDan Williams }
2509567da0bSDan Williams EXPORT_SYMBOL_GPL(run_dax);
2519567da0bSDan Williams 
2527b6be844SDan Williams static struct inode *dax_alloc_inode(struct super_block *sb)
2537b6be844SDan Williams {
2547b6be844SDan Williams 	struct dax_device *dax_dev;
255b9d39d17SDan Williams 	struct inode *inode;
2567b6be844SDan Williams 
2577b6be844SDan Williams 	dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
2589f586fffSMikulas Patocka 	if (!dax_dev)
2599f586fffSMikulas Patocka 		return NULL;
2609f586fffSMikulas Patocka 
261b9d39d17SDan Williams 	inode = &dax_dev->inode;
262b9d39d17SDan Williams 	inode->i_rdev = 0;
263b9d39d17SDan Williams 	return inode;
2647b6be844SDan Williams }
2657b6be844SDan Williams 
2667b6be844SDan Williams static struct dax_device *to_dax_dev(struct inode *inode)
2677b6be844SDan Williams {
2687b6be844SDan Williams 	return container_of(inode, struct dax_device, inode);
2697b6be844SDan Williams }
2707b6be844SDan Williams 
27153e22829SAl Viro static void dax_free_inode(struct inode *inode)
2727b6be844SDan Williams {
2737b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
274b9d39d17SDan Williams 	if (inode->i_rdev)
2756f24784fSAl Viro 		ida_simple_remove(&dax_minor_ida, iminor(inode));
2767b6be844SDan Williams 	kmem_cache_free(dax_cache, dax_dev);
2777b6be844SDan Williams }
2787b6be844SDan Williams 
2797b6be844SDan Williams static void dax_destroy_inode(struct inode *inode)
2807b6be844SDan Williams {
2817b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
2829a60c3efSDan Williams 	WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
2837b6be844SDan Williams 			"kill_dax() must be called before final iput()\n");
2847b6be844SDan Williams }
2857b6be844SDan Williams 
2867b6be844SDan Williams static const struct super_operations dax_sops = {
2877b6be844SDan Williams 	.statfs = simple_statfs,
2887b6be844SDan Williams 	.alloc_inode = dax_alloc_inode,
2897b6be844SDan Williams 	.destroy_inode = dax_destroy_inode,
29053e22829SAl Viro 	.free_inode = dax_free_inode,
2917b6be844SDan Williams 	.drop_inode = generic_delete_inode,
2927b6be844SDan Williams };
2937b6be844SDan Williams 
29475d4e06fSDavid Howells static int dax_init_fs_context(struct fs_context *fc)
2957b6be844SDan Williams {
29675d4e06fSDavid Howells 	struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
29775d4e06fSDavid Howells 	if (!ctx)
29875d4e06fSDavid Howells 		return -ENOMEM;
29975d4e06fSDavid Howells 	ctx->ops = &dax_sops;
30075d4e06fSDavid Howells 	return 0;
3017b6be844SDan Williams }
3027b6be844SDan Williams 
3037b6be844SDan Williams static struct file_system_type dax_fs_type = {
3047b6be844SDan Williams 	.name		= "dax",
30575d4e06fSDavid Howells 	.init_fs_context = dax_init_fs_context,
3067b6be844SDan Williams 	.kill_sb	= kill_anon_super,
3077b6be844SDan Williams };
3087b6be844SDan Williams 
3097b6be844SDan Williams static int dax_test(struct inode *inode, void *data)
3107b6be844SDan Williams {
3117b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
3127b6be844SDan Williams 
3137b6be844SDan Williams 	return inode->i_rdev == devt;
3147b6be844SDan Williams }
3157b6be844SDan Williams 
3167b6be844SDan Williams static int dax_set(struct inode *inode, void *data)
3177b6be844SDan Williams {
3187b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
3197b6be844SDan Williams 
3207b6be844SDan Williams 	inode->i_rdev = devt;
3217b6be844SDan Williams 	return 0;
3227b6be844SDan Williams }
3237b6be844SDan Williams 
3247b6be844SDan Williams static struct dax_device *dax_dev_get(dev_t devt)
3257b6be844SDan Williams {
3267b6be844SDan Williams 	struct dax_device *dax_dev;
3277b6be844SDan Williams 	struct inode *inode;
3287b6be844SDan Williams 
3297b6be844SDan Williams 	inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
3307b6be844SDan Williams 			dax_test, dax_set, &devt);
3317b6be844SDan Williams 
3327b6be844SDan Williams 	if (!inode)
3337b6be844SDan Williams 		return NULL;
3347b6be844SDan Williams 
3357b6be844SDan Williams 	dax_dev = to_dax_dev(inode);
3367b6be844SDan Williams 	if (inode->i_state & I_NEW) {
3379a60c3efSDan Williams 		set_bit(DAXDEV_ALIVE, &dax_dev->flags);
3387b6be844SDan Williams 		inode->i_cdev = &dax_dev->cdev;
3397b6be844SDan Williams 		inode->i_mode = S_IFCHR;
3407b6be844SDan Williams 		inode->i_flags = S_DAX;
3417b6be844SDan Williams 		mapping_set_gfp_mask(&inode->i_data, GFP_USER);
3427b6be844SDan Williams 		unlock_new_inode(inode);
3437b6be844SDan Williams 	}
3447b6be844SDan Williams 
3457b6be844SDan Williams 	return dax_dev;
3467b6be844SDan Williams }
3477b6be844SDan Williams 
348fb08a190SChristoph Hellwig struct dax_device *alloc_dax(void *private, const struct dax_operations *ops,
349fb08a190SChristoph Hellwig 		unsigned long flags)
3507b6be844SDan Williams {
3517b6be844SDan Williams 	struct dax_device *dax_dev;
3527b6be844SDan Williams 	dev_t devt;
3537b6be844SDan Williams 	int minor;
3547b6be844SDan Williams 
355fb08a190SChristoph Hellwig 	if (WARN_ON_ONCE(ops && !ops->zero_page_range))
3564e4ced93SVivek Goyal 		return ERR_PTR(-EINVAL);
35772058005SDan Williams 
358cf1e2289SDan Williams 	minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
3597b6be844SDan Williams 	if (minor < 0)
360fb08a190SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
3617b6be844SDan Williams 
3627b6be844SDan Williams 	devt = MKDEV(MAJOR(dax_devt), minor);
3637b6be844SDan Williams 	dax_dev = dax_dev_get(devt);
3647b6be844SDan Williams 	if (!dax_dev)
36572058005SDan Williams 		goto err_dev;
3667b6be844SDan Williams 
3676568b08bSDan Williams 	dax_dev->ops = ops;
3687b6be844SDan Williams 	dax_dev->private = private;
369fefc1d97SPankaj Gupta 	if (flags & DAXDEV_F_SYNC)
370fefc1d97SPankaj Gupta 		set_dax_synchronous(dax_dev);
371fefc1d97SPankaj Gupta 
3727b6be844SDan Williams 	return dax_dev;
3737b6be844SDan Williams 
37472058005SDan Williams  err_dev:
3757b6be844SDan Williams 	ida_simple_remove(&dax_minor_ida, minor);
3764e4ced93SVivek Goyal 	return ERR_PTR(-ENOMEM);
3777b6be844SDan Williams }
3787b6be844SDan Williams EXPORT_SYMBOL_GPL(alloc_dax);
3797b6be844SDan Williams 
3807b6be844SDan Williams void put_dax(struct dax_device *dax_dev)
3817b6be844SDan Williams {
3827b6be844SDan Williams 	if (!dax_dev)
3837b6be844SDan Williams 		return;
3847b6be844SDan Williams 	iput(&dax_dev->inode);
3857b6be844SDan Williams }
3867b6be844SDan Williams EXPORT_SYMBOL_GPL(put_dax);
3877b6be844SDan Williams 
3887b6be844SDan Williams /**
3897b6be844SDan Williams  * inode_dax: convert a public inode into its dax_dev
3907b6be844SDan Williams  * @inode: An inode with i_cdev pointing to a dax_dev
3917b6be844SDan Williams  *
3927b6be844SDan Williams  * Note this is not equivalent to to_dax_dev() which is for private
3937b6be844SDan Williams  * internal use where we know the inode filesystem type == dax_fs_type.
3947b6be844SDan Williams  */
3957b6be844SDan Williams struct dax_device *inode_dax(struct inode *inode)
3967b6be844SDan Williams {
3977b6be844SDan Williams 	struct cdev *cdev = inode->i_cdev;
3987b6be844SDan Williams 
3997b6be844SDan Williams 	return container_of(cdev, struct dax_device, cdev);
4007b6be844SDan Williams }
4017b6be844SDan Williams EXPORT_SYMBOL_GPL(inode_dax);
4027b6be844SDan Williams 
4037b6be844SDan Williams struct inode *dax_inode(struct dax_device *dax_dev)
4047b6be844SDan Williams {
4057b6be844SDan Williams 	return &dax_dev->inode;
4067b6be844SDan Williams }
4077b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_inode);
4087b6be844SDan Williams 
4097b6be844SDan Williams void *dax_get_private(struct dax_device *dax_dev)
4107b6be844SDan Williams {
4119567da0bSDan Williams 	if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
4129567da0bSDan Williams 		return NULL;
4137b6be844SDan Williams 	return dax_dev->private;
4147b6be844SDan Williams }
4157b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_get_private);
4167b6be844SDan Williams 
4177b6be844SDan Williams static void init_once(void *_dax_dev)
4187b6be844SDan Williams {
4197b6be844SDan Williams 	struct dax_device *dax_dev = _dax_dev;
4207b6be844SDan Williams 	struct inode *inode = &dax_dev->inode;
4217b6be844SDan Williams 
422b9d39d17SDan Williams 	memset(dax_dev, 0, sizeof(*dax_dev));
4237b6be844SDan Williams 	inode_init_once(inode);
4247b6be844SDan Williams }
4257b6be844SDan Williams 
4269567da0bSDan Williams static int dax_fs_init(void)
4277b6be844SDan Williams {
4287b6be844SDan Williams 	int rc;
4297b6be844SDan Williams 
4307b6be844SDan Williams 	dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
4317b6be844SDan Williams 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
4327b6be844SDan Williams 			 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
4337b6be844SDan Williams 			init_once);
4347b6be844SDan Williams 	if (!dax_cache)
4357b6be844SDan Williams 		return -ENOMEM;
4367b6be844SDan Williams 
4377b6be844SDan Williams 	dax_mnt = kern_mount(&dax_fs_type);
4387b6be844SDan Williams 	if (IS_ERR(dax_mnt)) {
4397b6be844SDan Williams 		rc = PTR_ERR(dax_mnt);
4407b6be844SDan Williams 		goto err_mount;
4417b6be844SDan Williams 	}
4427b6be844SDan Williams 	dax_superblock = dax_mnt->mnt_sb;
4437b6be844SDan Williams 
4447b6be844SDan Williams 	return 0;
4457b6be844SDan Williams 
4467b6be844SDan Williams  err_mount:
4477b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
4487b6be844SDan Williams 
4497b6be844SDan Williams 	return rc;
4507b6be844SDan Williams }
4517b6be844SDan Williams 
4529567da0bSDan Williams static void dax_fs_exit(void)
4537b6be844SDan Williams {
4547b6be844SDan Williams 	kern_unmount(dax_mnt);
4557b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
4567b6be844SDan Williams }
4577b6be844SDan Williams 
4589567da0bSDan Williams static int __init dax_core_init(void)
4597b6be844SDan Williams {
4607b6be844SDan Williams 	int rc;
4617b6be844SDan Williams 
4629567da0bSDan Williams 	rc = dax_fs_init();
4637b6be844SDan Williams 	if (rc)
4647b6be844SDan Williams 		return rc;
4657b6be844SDan Williams 
466cf1e2289SDan Williams 	rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
4677b6be844SDan Williams 	if (rc)
4689567da0bSDan Williams 		goto err_chrdev;
4699567da0bSDan Williams 
4709567da0bSDan Williams 	rc = dax_bus_init();
4719567da0bSDan Williams 	if (rc)
4729567da0bSDan Williams 		goto err_bus;
4739567da0bSDan Williams 	return 0;
4749567da0bSDan Williams 
4759567da0bSDan Williams err_bus:
4769567da0bSDan Williams 	unregister_chrdev_region(dax_devt, MINORMASK+1);
4779567da0bSDan Williams err_chrdev:
4789567da0bSDan Williams 	dax_fs_exit();
4799567da0bSDan Williams 	return 0;
4807b6be844SDan Williams }
4817b6be844SDan Williams 
4829567da0bSDan Williams static void __exit dax_core_exit(void)
4837b6be844SDan Williams {
4841aa57431SWang Hai 	dax_bus_exit();
485cf1e2289SDan Williams 	unregister_chrdev_region(dax_devt, MINORMASK+1);
4867b6be844SDan Williams 	ida_destroy(&dax_minor_ida);
4879567da0bSDan Williams 	dax_fs_exit();
4887b6be844SDan Williams }
4897b6be844SDan Williams 
4907b6be844SDan Williams MODULE_AUTHOR("Intel Corporation");
4917b6be844SDan Williams MODULE_LICENSE("GPL v2");
4929567da0bSDan Williams subsys_initcall(dax_core_init);
4939567da0bSDan Williams module_exit(dax_core_exit);
494