xref: /openbmc/linux/drivers/dax/super.c (revision e511c4a3)
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
24db8cd5efSIra Weiny  * @ops: operations for this device
251b764601SChristoph Hellwig  */
261b764601SChristoph Hellwig struct dax_device {
271b764601SChristoph Hellwig 	struct inode inode;
281b764601SChristoph Hellwig 	struct cdev cdev;
291b764601SChristoph Hellwig 	void *private;
301b764601SChristoph Hellwig 	unsigned long flags;
311b764601SChristoph Hellwig 	const struct dax_operations *ops;
321b764601SChristoph Hellwig };
331b764601SChristoph Hellwig 
347b6be844SDan Williams static dev_t dax_devt;
357b6be844SDan Williams DEFINE_STATIC_SRCU(dax_srcu);
367b6be844SDan Williams static struct vfsmount *dax_mnt;
377b6be844SDan Williams static DEFINE_IDA(dax_minor_ida);
387b6be844SDan Williams static struct kmem_cache *dax_cache __read_mostly;
397b6be844SDan Williams static struct super_block *dax_superblock __read_mostly;
407b6be844SDan Williams 
417b6be844SDan Williams int dax_read_lock(void)
427b6be844SDan Williams {
437b6be844SDan Williams 	return srcu_read_lock(&dax_srcu);
447b6be844SDan Williams }
457b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_lock);
467b6be844SDan Williams 
477b6be844SDan Williams void dax_read_unlock(int id)
487b6be844SDan Williams {
497b6be844SDan Williams 	srcu_read_unlock(&dax_srcu, id);
507b6be844SDan Williams }
517b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_unlock);
527b6be844SDan Williams 
535d2a228bSChristoph Hellwig #if defined(CONFIG_BLOCK) && defined(CONFIG_FS_DAX)
54e765f13eSChristoph Hellwig #include <linux/blkdev.h>
55e765f13eSChristoph Hellwig 
56fb08a190SChristoph Hellwig static DEFINE_XARRAY(dax_hosts);
57fb08a190SChristoph Hellwig 
58fb08a190SChristoph Hellwig int dax_add_host(struct dax_device *dax_dev, struct gendisk *disk)
59fb08a190SChristoph Hellwig {
60fb08a190SChristoph Hellwig 	return xa_insert(&dax_hosts, (unsigned long)disk, dax_dev, GFP_KERNEL);
61fb08a190SChristoph Hellwig }
62fb08a190SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_add_host);
63fb08a190SChristoph Hellwig 
64fb08a190SChristoph Hellwig void dax_remove_host(struct gendisk *disk)
65fb08a190SChristoph Hellwig {
66fb08a190SChristoph Hellwig 	xa_erase(&dax_hosts, (unsigned long)disk);
67fb08a190SChristoph Hellwig }
68fb08a190SChristoph Hellwig EXPORT_SYMBOL_GPL(dax_remove_host);
69fb08a190SChristoph Hellwig 
701b764601SChristoph Hellwig /**
71fb08a190SChristoph Hellwig  * fs_dax_get_by_bdev() - temporary lookup mechanism for filesystem-dax
72fb08a190SChristoph Hellwig  * @bdev: block device to find a dax_device for
73cd913c76SChristoph Hellwig  * @start_off: returns the byte offset into the dax_device that @bdev starts
741b764601SChristoph Hellwig  */
75cd913c76SChristoph Hellwig struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev, u64 *start_off)
7678f35473SDan Williams {
77fb08a190SChristoph Hellwig 	struct dax_device *dax_dev;
78cd913c76SChristoph Hellwig 	u64 part_size;
79fb08a190SChristoph Hellwig 	int id;
80fb08a190SChristoph Hellwig 
81e556f6baSChristoph Hellwig 	if (!blk_queue_dax(bdev->bd_disk->queue))
8278f35473SDan Williams 		return NULL;
83fb08a190SChristoph Hellwig 
84cd913c76SChristoph Hellwig 	*start_off = get_start_sect(bdev) * SECTOR_SIZE;
85cd913c76SChristoph Hellwig 	part_size = bdev_nr_sectors(bdev) * SECTOR_SIZE;
86cd913c76SChristoph Hellwig 	if (*start_off % PAGE_SIZE || part_size % PAGE_SIZE) {
870c445871SChristoph Hellwig 		pr_info("%pg: error: unaligned partition for dax\n", bdev);
880c445871SChristoph Hellwig 		return NULL;
890c445871SChristoph Hellwig 	}
900c445871SChristoph Hellwig 
91fb08a190SChristoph Hellwig 	id = dax_read_lock();
92fb08a190SChristoph Hellwig 	dax_dev = xa_load(&dax_hosts, (unsigned long)bdev->bd_disk);
93fb08a190SChristoph Hellwig 	if (!dax_dev || !dax_alive(dax_dev) || !igrab(&dax_dev->inode))
94fb08a190SChristoph Hellwig 		dax_dev = NULL;
95fb08a190SChristoph Hellwig 	dax_read_unlock(id);
96fb08a190SChristoph Hellwig 
97fb08a190SChristoph Hellwig 	return dax_dev;
9878f35473SDan Williams }
9978f35473SDan Williams EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
1005d2a228bSChristoph Hellwig #endif /* CONFIG_BLOCK && CONFIG_FS_DAX */
101ef510424SDan Williams 
1029a60c3efSDan Williams enum dax_device_flags {
1039a60c3efSDan Williams 	/* !alive + rcu grace period == no new operations / mappings */
1049a60c3efSDan Williams 	DAXDEV_ALIVE,
1056e0c90d6SDan Williams 	/* gate whether dax_flush() calls the low level flush routine */
1066e0c90d6SDan Williams 	DAXDEV_WRITE_CACHE,
107fefc1d97SPankaj Gupta 	/* flag to check if device supports synchronous flush */
108fefc1d97SPankaj Gupta 	DAXDEV_SYNC,
1097ac5360cSChristoph Hellwig 	/* do not leave the caches dirty after writes */
1107ac5360cSChristoph Hellwig 	DAXDEV_NOCACHE,
1117ac5360cSChristoph Hellwig 	/* handle CPU fetch exceptions during reads */
1127ac5360cSChristoph Hellwig 	DAXDEV_NOMC,
1139a60c3efSDan Williams };
1149a60c3efSDan Williams 
115b0686260SDan Williams /**
116b0686260SDan Williams  * dax_direct_access() - translate a device pgoff to an absolute pfn
117b0686260SDan Williams  * @dax_dev: a dax_device instance representing the logical memory range
118b0686260SDan Williams  * @pgoff: offset in pages from the start of the device to translate
119b0686260SDan Williams  * @nr_pages: number of consecutive pages caller can handle relative to @pfn
120*e511c4a3SJane Chu  * @mode: indicator on normal access or recovery write
121b0686260SDan Williams  * @kaddr: output parameter that returns a virtual address mapping of pfn
122b0686260SDan Williams  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
123b0686260SDan Williams  *
124b0686260SDan Williams  * Return: negative errno if an error occurs, otherwise the number of
125b0686260SDan Williams  * pages accessible at the device relative @pgoff.
126b0686260SDan Williams  */
127b0686260SDan Williams long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
128*e511c4a3SJane Chu 		enum dax_access_mode mode, void **kaddr, pfn_t *pfn)
129b0686260SDan Williams {
130b0686260SDan Williams 	long avail;
131b0686260SDan Williams 
132b0686260SDan Williams 	if (!dax_dev)
133b0686260SDan Williams 		return -EOPNOTSUPP;
134b0686260SDan Williams 
135b0686260SDan Williams 	if (!dax_alive(dax_dev))
136b0686260SDan Williams 		return -ENXIO;
137b0686260SDan Williams 
138b0686260SDan Williams 	if (nr_pages < 0)
139b05d4c57SIra Weiny 		return -EINVAL;
140b0686260SDan Williams 
141b0686260SDan Williams 	avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
142*e511c4a3SJane Chu 			mode, kaddr, pfn);
143b0686260SDan Williams 	if (!avail)
144b0686260SDan Williams 		return -ERANGE;
145b0686260SDan Williams 	return min(avail, nr_pages);
146b0686260SDan Williams }
147b0686260SDan Williams EXPORT_SYMBOL_GPL(dax_direct_access);
148b0686260SDan Williams 
1497e026c8cSDan Williams size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
1507e026c8cSDan Williams 		size_t bytes, struct iov_iter *i)
1517e026c8cSDan Williams {
1527e026c8cSDan Williams 	if (!dax_alive(dax_dev))
1537e026c8cSDan Williams 		return 0;
1547e026c8cSDan Williams 
1557ac5360cSChristoph Hellwig 	/*
1567ac5360cSChristoph Hellwig 	 * The userspace address for the memory copy has already been validated
1577ac5360cSChristoph Hellwig 	 * via access_ok() in vfs_write, so use the 'no check' version to bypass
1587ac5360cSChristoph Hellwig 	 * the HARDENED_USERCOPY overhead.
1597ac5360cSChristoph Hellwig 	 */
1607ac5360cSChristoph Hellwig 	if (test_bit(DAXDEV_NOCACHE, &dax_dev->flags))
1617ac5360cSChristoph Hellwig 		return _copy_from_iter_flushcache(addr, bytes, i);
1627ac5360cSChristoph Hellwig 	return _copy_from_iter(addr, bytes, i);
1637e026c8cSDan Williams }
1647e026c8cSDan Williams 
165b3a9a0c3SDan Williams size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
166b3a9a0c3SDan Williams 		size_t bytes, struct iov_iter *i)
167b3a9a0c3SDan Williams {
168b3a9a0c3SDan Williams 	if (!dax_alive(dax_dev))
169b3a9a0c3SDan Williams 		return 0;
170b3a9a0c3SDan Williams 
1717ac5360cSChristoph Hellwig 	/*
1727ac5360cSChristoph Hellwig 	 * The userspace address for the memory copy has already been validated
1737ac5360cSChristoph Hellwig 	 * via access_ok() in vfs_red, so use the 'no check' version to bypass
1747ac5360cSChristoph Hellwig 	 * the HARDENED_USERCOPY overhead.
1757ac5360cSChristoph Hellwig 	 */
1767ac5360cSChristoph Hellwig 	if (test_bit(DAXDEV_NOMC, &dax_dev->flags))
1777ac5360cSChristoph Hellwig 		return _copy_mc_to_iter(addr, bytes, i);
1787ac5360cSChristoph Hellwig 	return _copy_to_iter(addr, bytes, i);
179b3a9a0c3SDan Williams }
180b3a9a0c3SDan Williams 
181f605a263SVivek Goyal int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
182f605a263SVivek Goyal 			size_t nr_pages)
183f605a263SVivek Goyal {
184f605a263SVivek Goyal 	if (!dax_alive(dax_dev))
185f605a263SVivek Goyal 		return -ENXIO;
186f605a263SVivek Goyal 	/*
187f605a263SVivek Goyal 	 * There are no callers that want to zero more than one page as of now.
188f605a263SVivek Goyal 	 * Once users are there, this check can be removed after the
189f605a263SVivek Goyal 	 * device mapper code has been updated to split ranges across targets.
190f605a263SVivek Goyal 	 */
191f605a263SVivek Goyal 	if (nr_pages != 1)
192f605a263SVivek Goyal 		return -EIO;
193f605a263SVivek Goyal 
194f605a263SVivek Goyal 	return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
195f605a263SVivek Goyal }
196f605a263SVivek Goyal EXPORT_SYMBOL_GPL(dax_zero_page_range);
197f605a263SVivek Goyal 
198c3ca015fSMikulas Patocka #ifdef CONFIG_ARCH_HAS_PMEM_API
199c3ca015fSMikulas Patocka void arch_wb_cache_pmem(void *addr, size_t size);
200c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
201abebfbe2SDan Williams {
202808c340bSRoss Zwisler 	if (unlikely(!dax_write_cache_enabled(dax_dev)))
2036e0c90d6SDan Williams 		return;
2046e0c90d6SDan Williams 
205c3ca015fSMikulas Patocka 	arch_wb_cache_pmem(addr, size);
206abebfbe2SDan Williams }
207c3ca015fSMikulas Patocka #else
208c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
209c3ca015fSMikulas Patocka {
210c3ca015fSMikulas Patocka }
211c3ca015fSMikulas Patocka #endif
212abebfbe2SDan Williams EXPORT_SYMBOL_GPL(dax_flush);
213abebfbe2SDan Williams 
2146e0c90d6SDan Williams void dax_write_cache(struct dax_device *dax_dev, bool wc)
2156e0c90d6SDan Williams {
2166e0c90d6SDan Williams 	if (wc)
2176e0c90d6SDan Williams 		set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
2186e0c90d6SDan Williams 	else
2196e0c90d6SDan Williams 		clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
2206e0c90d6SDan Williams }
2216e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_write_cache);
2226e0c90d6SDan Williams 
223273752c9SVivek Goyal bool dax_write_cache_enabled(struct dax_device *dax_dev)
224273752c9SVivek Goyal {
225273752c9SVivek Goyal 	return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
226273752c9SVivek Goyal }
227273752c9SVivek Goyal EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
228273752c9SVivek Goyal 
229fd1d00ecSChristoph Hellwig bool dax_synchronous(struct dax_device *dax_dev)
230fefc1d97SPankaj Gupta {
231fefc1d97SPankaj Gupta 	return test_bit(DAXDEV_SYNC, &dax_dev->flags);
232fefc1d97SPankaj Gupta }
233fd1d00ecSChristoph Hellwig EXPORT_SYMBOL_GPL(dax_synchronous);
234fefc1d97SPankaj Gupta 
235fd1d00ecSChristoph Hellwig void set_dax_synchronous(struct dax_device *dax_dev)
236fefc1d97SPankaj Gupta {
237fefc1d97SPankaj Gupta 	set_bit(DAXDEV_SYNC, &dax_dev->flags);
238fefc1d97SPankaj Gupta }
239fd1d00ecSChristoph Hellwig EXPORT_SYMBOL_GPL(set_dax_synchronous);
240fefc1d97SPankaj Gupta 
2417ac5360cSChristoph Hellwig void set_dax_nocache(struct dax_device *dax_dev)
2427ac5360cSChristoph Hellwig {
2437ac5360cSChristoph Hellwig 	set_bit(DAXDEV_NOCACHE, &dax_dev->flags);
2447ac5360cSChristoph Hellwig }
2457ac5360cSChristoph Hellwig EXPORT_SYMBOL_GPL(set_dax_nocache);
2467ac5360cSChristoph Hellwig 
2477ac5360cSChristoph Hellwig void set_dax_nomc(struct dax_device *dax_dev)
2487ac5360cSChristoph Hellwig {
2497ac5360cSChristoph Hellwig 	set_bit(DAXDEV_NOMC, &dax_dev->flags);
2507ac5360cSChristoph Hellwig }
2517ac5360cSChristoph Hellwig EXPORT_SYMBOL_GPL(set_dax_nomc);
2527ac5360cSChristoph Hellwig 
2537b6be844SDan Williams bool dax_alive(struct dax_device *dax_dev)
2547b6be844SDan Williams {
2557b6be844SDan Williams 	lockdep_assert_held(&dax_srcu);
2569a60c3efSDan Williams 	return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
2577b6be844SDan Williams }
2587b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_alive);
2597b6be844SDan Williams 
2607b6be844SDan Williams /*
2617b6be844SDan Williams  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
2627b6be844SDan Williams  * that any fault handlers or operations that might have seen
2637b6be844SDan Williams  * dax_alive(), have completed.  Any operations that start after
2647b6be844SDan Williams  * synchronize_srcu() has run will abort upon seeing !dax_alive().
2657b6be844SDan Williams  */
2667b6be844SDan Williams void kill_dax(struct dax_device *dax_dev)
2677b6be844SDan Williams {
2687b6be844SDan Williams 	if (!dax_dev)
2697b6be844SDan Williams 		return;
2707b6be844SDan Williams 
2719a60c3efSDan Williams 	clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
2727b6be844SDan Williams 	synchronize_srcu(&dax_srcu);
2737b6be844SDan Williams }
2747b6be844SDan Williams EXPORT_SYMBOL_GPL(kill_dax);
2757b6be844SDan Williams 
2769567da0bSDan Williams void run_dax(struct dax_device *dax_dev)
2779567da0bSDan Williams {
2789567da0bSDan Williams 	set_bit(DAXDEV_ALIVE, &dax_dev->flags);
2799567da0bSDan Williams }
2809567da0bSDan Williams EXPORT_SYMBOL_GPL(run_dax);
2819567da0bSDan Williams 
2827b6be844SDan Williams static struct inode *dax_alloc_inode(struct super_block *sb)
2837b6be844SDan Williams {
2847b6be844SDan Williams 	struct dax_device *dax_dev;
285b9d39d17SDan Williams 	struct inode *inode;
2867b6be844SDan Williams 
287fd60b288SMuchun Song 	dax_dev = alloc_inode_sb(sb, dax_cache, GFP_KERNEL);
2889f586fffSMikulas Patocka 	if (!dax_dev)
2899f586fffSMikulas Patocka 		return NULL;
2909f586fffSMikulas Patocka 
291b9d39d17SDan Williams 	inode = &dax_dev->inode;
292b9d39d17SDan Williams 	inode->i_rdev = 0;
293b9d39d17SDan Williams 	return inode;
2947b6be844SDan Williams }
2957b6be844SDan Williams 
2967b6be844SDan Williams static struct dax_device *to_dax_dev(struct inode *inode)
2977b6be844SDan Williams {
2987b6be844SDan Williams 	return container_of(inode, struct dax_device, inode);
2997b6be844SDan Williams }
3007b6be844SDan Williams 
30153e22829SAl Viro static void dax_free_inode(struct inode *inode)
3027b6be844SDan Williams {
3037b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
304b9d39d17SDan Williams 	if (inode->i_rdev)
3056f24784fSAl Viro 		ida_simple_remove(&dax_minor_ida, iminor(inode));
3067b6be844SDan Williams 	kmem_cache_free(dax_cache, dax_dev);
3077b6be844SDan Williams }
3087b6be844SDan Williams 
3097b6be844SDan Williams static void dax_destroy_inode(struct inode *inode)
3107b6be844SDan Williams {
3117b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
3129a60c3efSDan Williams 	WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
3137b6be844SDan Williams 			"kill_dax() must be called before final iput()\n");
3147b6be844SDan Williams }
3157b6be844SDan Williams 
3167b6be844SDan Williams static const struct super_operations dax_sops = {
3177b6be844SDan Williams 	.statfs = simple_statfs,
3187b6be844SDan Williams 	.alloc_inode = dax_alloc_inode,
3197b6be844SDan Williams 	.destroy_inode = dax_destroy_inode,
32053e22829SAl Viro 	.free_inode = dax_free_inode,
3217b6be844SDan Williams 	.drop_inode = generic_delete_inode,
3227b6be844SDan Williams };
3237b6be844SDan Williams 
32475d4e06fSDavid Howells static int dax_init_fs_context(struct fs_context *fc)
3257b6be844SDan Williams {
32675d4e06fSDavid Howells 	struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
32775d4e06fSDavid Howells 	if (!ctx)
32875d4e06fSDavid Howells 		return -ENOMEM;
32975d4e06fSDavid Howells 	ctx->ops = &dax_sops;
33075d4e06fSDavid Howells 	return 0;
3317b6be844SDan Williams }
3327b6be844SDan Williams 
3337b6be844SDan Williams static struct file_system_type dax_fs_type = {
3347b6be844SDan Williams 	.name		= "dax",
33575d4e06fSDavid Howells 	.init_fs_context = dax_init_fs_context,
3367b6be844SDan Williams 	.kill_sb	= kill_anon_super,
3377b6be844SDan Williams };
3387b6be844SDan Williams 
3397b6be844SDan Williams static int dax_test(struct inode *inode, void *data)
3407b6be844SDan Williams {
3417b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
3427b6be844SDan Williams 
3437b6be844SDan Williams 	return inode->i_rdev == devt;
3447b6be844SDan Williams }
3457b6be844SDan Williams 
3467b6be844SDan Williams static int dax_set(struct inode *inode, void *data)
3477b6be844SDan Williams {
3487b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
3497b6be844SDan Williams 
3507b6be844SDan Williams 	inode->i_rdev = devt;
3517b6be844SDan Williams 	return 0;
3527b6be844SDan Williams }
3537b6be844SDan Williams 
3547b6be844SDan Williams static struct dax_device *dax_dev_get(dev_t devt)
3557b6be844SDan Williams {
3567b6be844SDan Williams 	struct dax_device *dax_dev;
3577b6be844SDan Williams 	struct inode *inode;
3587b6be844SDan Williams 
3597b6be844SDan Williams 	inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
3607b6be844SDan Williams 			dax_test, dax_set, &devt);
3617b6be844SDan Williams 
3627b6be844SDan Williams 	if (!inode)
3637b6be844SDan Williams 		return NULL;
3647b6be844SDan Williams 
3657b6be844SDan Williams 	dax_dev = to_dax_dev(inode);
3667b6be844SDan Williams 	if (inode->i_state & I_NEW) {
3679a60c3efSDan Williams 		set_bit(DAXDEV_ALIVE, &dax_dev->flags);
3687b6be844SDan Williams 		inode->i_cdev = &dax_dev->cdev;
3697b6be844SDan Williams 		inode->i_mode = S_IFCHR;
3707b6be844SDan Williams 		inode->i_flags = S_DAX;
3717b6be844SDan Williams 		mapping_set_gfp_mask(&inode->i_data, GFP_USER);
3727b6be844SDan Williams 		unlock_new_inode(inode);
3737b6be844SDan Williams 	}
3747b6be844SDan Williams 
3757b6be844SDan Williams 	return dax_dev;
3767b6be844SDan Williams }
3777b6be844SDan Williams 
37830c6828aSChristoph Hellwig struct dax_device *alloc_dax(void *private, const struct dax_operations *ops)
3797b6be844SDan Williams {
3807b6be844SDan Williams 	struct dax_device *dax_dev;
3817b6be844SDan Williams 	dev_t devt;
3827b6be844SDan Williams 	int minor;
3837b6be844SDan Williams 
384fb08a190SChristoph Hellwig 	if (WARN_ON_ONCE(ops && !ops->zero_page_range))
3854e4ced93SVivek Goyal 		return ERR_PTR(-EINVAL);
38672058005SDan Williams 
387cf1e2289SDan Williams 	minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
3887b6be844SDan Williams 	if (minor < 0)
389fb08a190SChristoph Hellwig 		return ERR_PTR(-ENOMEM);
3907b6be844SDan Williams 
3917b6be844SDan Williams 	devt = MKDEV(MAJOR(dax_devt), minor);
3927b6be844SDan Williams 	dax_dev = dax_dev_get(devt);
3937b6be844SDan Williams 	if (!dax_dev)
39472058005SDan Williams 		goto err_dev;
3957b6be844SDan Williams 
3966568b08bSDan Williams 	dax_dev->ops = ops;
3977b6be844SDan Williams 	dax_dev->private = private;
3987b6be844SDan Williams 	return dax_dev;
3997b6be844SDan Williams 
40072058005SDan Williams  err_dev:
4017b6be844SDan Williams 	ida_simple_remove(&dax_minor_ida, minor);
4024e4ced93SVivek Goyal 	return ERR_PTR(-ENOMEM);
4037b6be844SDan Williams }
4047b6be844SDan Williams EXPORT_SYMBOL_GPL(alloc_dax);
4057b6be844SDan Williams 
4067b6be844SDan Williams void put_dax(struct dax_device *dax_dev)
4077b6be844SDan Williams {
4087b6be844SDan Williams 	if (!dax_dev)
4097b6be844SDan Williams 		return;
4107b6be844SDan Williams 	iput(&dax_dev->inode);
4117b6be844SDan Williams }
4127b6be844SDan Williams EXPORT_SYMBOL_GPL(put_dax);
4137b6be844SDan Williams 
4147b6be844SDan Williams /**
4157b6be844SDan Williams  * inode_dax: convert a public inode into its dax_dev
4167b6be844SDan Williams  * @inode: An inode with i_cdev pointing to a dax_dev
4177b6be844SDan Williams  *
4187b6be844SDan Williams  * Note this is not equivalent to to_dax_dev() which is for private
4197b6be844SDan Williams  * internal use where we know the inode filesystem type == dax_fs_type.
4207b6be844SDan Williams  */
4217b6be844SDan Williams struct dax_device *inode_dax(struct inode *inode)
4227b6be844SDan Williams {
4237b6be844SDan Williams 	struct cdev *cdev = inode->i_cdev;
4247b6be844SDan Williams 
4257b6be844SDan Williams 	return container_of(cdev, struct dax_device, cdev);
4267b6be844SDan Williams }
4277b6be844SDan Williams EXPORT_SYMBOL_GPL(inode_dax);
4287b6be844SDan Williams 
4297b6be844SDan Williams struct inode *dax_inode(struct dax_device *dax_dev)
4307b6be844SDan Williams {
4317b6be844SDan Williams 	return &dax_dev->inode;
4327b6be844SDan Williams }
4337b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_inode);
4347b6be844SDan Williams 
4357b6be844SDan Williams void *dax_get_private(struct dax_device *dax_dev)
4367b6be844SDan Williams {
4379567da0bSDan Williams 	if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
4389567da0bSDan Williams 		return NULL;
4397b6be844SDan Williams 	return dax_dev->private;
4407b6be844SDan Williams }
4417b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_get_private);
4427b6be844SDan Williams 
4437b6be844SDan Williams static void init_once(void *_dax_dev)
4447b6be844SDan Williams {
4457b6be844SDan Williams 	struct dax_device *dax_dev = _dax_dev;
4467b6be844SDan Williams 	struct inode *inode = &dax_dev->inode;
4477b6be844SDan Williams 
448b9d39d17SDan Williams 	memset(dax_dev, 0, sizeof(*dax_dev));
4497b6be844SDan Williams 	inode_init_once(inode);
4507b6be844SDan Williams }
4517b6be844SDan Williams 
4529567da0bSDan Williams static int dax_fs_init(void)
4537b6be844SDan Williams {
4547b6be844SDan Williams 	int rc;
4557b6be844SDan Williams 
4567b6be844SDan Williams 	dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
4577b6be844SDan Williams 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
4587b6be844SDan Williams 			 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
4597b6be844SDan Williams 			init_once);
4607b6be844SDan Williams 	if (!dax_cache)
4617b6be844SDan Williams 		return -ENOMEM;
4627b6be844SDan Williams 
4637b6be844SDan Williams 	dax_mnt = kern_mount(&dax_fs_type);
4647b6be844SDan Williams 	if (IS_ERR(dax_mnt)) {
4657b6be844SDan Williams 		rc = PTR_ERR(dax_mnt);
4667b6be844SDan Williams 		goto err_mount;
4677b6be844SDan Williams 	}
4687b6be844SDan Williams 	dax_superblock = dax_mnt->mnt_sb;
4697b6be844SDan Williams 
4707b6be844SDan Williams 	return 0;
4717b6be844SDan Williams 
4727b6be844SDan Williams  err_mount:
4737b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
4747b6be844SDan Williams 
4757b6be844SDan Williams 	return rc;
4767b6be844SDan Williams }
4777b6be844SDan Williams 
4789567da0bSDan Williams static void dax_fs_exit(void)
4797b6be844SDan Williams {
4807b6be844SDan Williams 	kern_unmount(dax_mnt);
481a7e8de82STong Zhang 	rcu_barrier();
4827b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
4837b6be844SDan Williams }
4847b6be844SDan Williams 
4859567da0bSDan Williams static int __init dax_core_init(void)
4867b6be844SDan Williams {
4877b6be844SDan Williams 	int rc;
4887b6be844SDan Williams 
4899567da0bSDan Williams 	rc = dax_fs_init();
4907b6be844SDan Williams 	if (rc)
4917b6be844SDan Williams 		return rc;
4927b6be844SDan Williams 
493cf1e2289SDan Williams 	rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
4947b6be844SDan Williams 	if (rc)
4959567da0bSDan Williams 		goto err_chrdev;
4969567da0bSDan Williams 
4979567da0bSDan Williams 	rc = dax_bus_init();
4989567da0bSDan Williams 	if (rc)
4999567da0bSDan Williams 		goto err_bus;
5009567da0bSDan Williams 	return 0;
5019567da0bSDan Williams 
5029567da0bSDan Williams err_bus:
5039567da0bSDan Williams 	unregister_chrdev_region(dax_devt, MINORMASK+1);
5049567da0bSDan Williams err_chrdev:
5059567da0bSDan Williams 	dax_fs_exit();
5069567da0bSDan Williams 	return 0;
5077b6be844SDan Williams }
5087b6be844SDan Williams 
5099567da0bSDan Williams static void __exit dax_core_exit(void)
5107b6be844SDan Williams {
5111aa57431SWang Hai 	dax_bus_exit();
512cf1e2289SDan Williams 	unregister_chrdev_region(dax_devt, MINORMASK+1);
5137b6be844SDan Williams 	ida_destroy(&dax_minor_ida);
5149567da0bSDan Williams 	dax_fs_exit();
5157b6be844SDan Williams }
5167b6be844SDan Williams 
5177b6be844SDan Williams MODULE_AUTHOR("Intel Corporation");
5187b6be844SDan Williams MODULE_LICENSE("GPL v2");
5199567da0bSDan Williams subsys_initcall(dax_core_init);
5209567da0bSDan Williams module_exit(dax_core_exit);
521