xref: /openbmc/linux/drivers/dax/super.c (revision 51cf784c)
17b6be844SDan Williams /*
27b6be844SDan Williams  * Copyright(c) 2017 Intel Corporation. All rights reserved.
37b6be844SDan Williams  *
47b6be844SDan Williams  * This program is free software; you can redistribute it and/or modify
57b6be844SDan Williams  * it under the terms of version 2 of the GNU General Public License as
67b6be844SDan Williams  * published by the Free Software Foundation.
77b6be844SDan Williams  *
87b6be844SDan Williams  * This program is distributed in the hope that it will be useful, but
97b6be844SDan Williams  * WITHOUT ANY WARRANTY; without even the implied warranty of
107b6be844SDan Williams  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
117b6be844SDan Williams  * General Public License for more details.
127b6be844SDan Williams  */
137b6be844SDan Williams #include <linux/pagemap.h>
147b6be844SDan Williams #include <linux/module.h>
157b6be844SDan Williams #include <linux/mount.h>
167b6be844SDan Williams #include <linux/magic.h>
17ef510424SDan Williams #include <linux/genhd.h>
18569d0365SDan Williams #include <linux/pfn_t.h>
197b6be844SDan Williams #include <linux/cdev.h>
207b6be844SDan Williams #include <linux/hash.h>
217b6be844SDan Williams #include <linux/slab.h>
227e026c8cSDan Williams #include <linux/uio.h>
236568b08bSDan Williams #include <linux/dax.h>
247b6be844SDan Williams #include <linux/fs.h>
2551cf784cSDan Williams #include "dax-private.h"
267b6be844SDan Williams 
277b6be844SDan Williams static dev_t dax_devt;
287b6be844SDan Williams DEFINE_STATIC_SRCU(dax_srcu);
297b6be844SDan Williams static struct vfsmount *dax_mnt;
307b6be844SDan Williams static DEFINE_IDA(dax_minor_ida);
317b6be844SDan Williams static struct kmem_cache *dax_cache __read_mostly;
327b6be844SDan Williams static struct super_block *dax_superblock __read_mostly;
337b6be844SDan Williams 
3472058005SDan Williams #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
3572058005SDan Williams static struct hlist_head dax_host_list[DAX_HASH_SIZE];
3672058005SDan Williams static DEFINE_SPINLOCK(dax_host_lock);
3772058005SDan Williams 
387b6be844SDan Williams int dax_read_lock(void)
397b6be844SDan Williams {
407b6be844SDan Williams 	return srcu_read_lock(&dax_srcu);
417b6be844SDan Williams }
427b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_lock);
437b6be844SDan Williams 
447b6be844SDan Williams void dax_read_unlock(int id)
457b6be844SDan Williams {
467b6be844SDan Williams 	srcu_read_unlock(&dax_srcu, id);
477b6be844SDan Williams }
487b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_unlock);
497b6be844SDan Williams 
509d109081SDan Williams #ifdef CONFIG_BLOCK
5178f35473SDan Williams #include <linux/blkdev.h>
5278f35473SDan Williams 
53ef510424SDan Williams int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
54ef510424SDan Williams 		pgoff_t *pgoff)
55ef510424SDan Williams {
56ef510424SDan Williams 	phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
57ef510424SDan Williams 
58ef510424SDan Williams 	if (pgoff)
59ef510424SDan Williams 		*pgoff = PHYS_PFN(phys_off);
60ef510424SDan Williams 	if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
61ef510424SDan Williams 		return -EINVAL;
62ef510424SDan Williams 	return 0;
63ef510424SDan Williams }
64ef510424SDan Williams EXPORT_SYMBOL(bdev_dax_pgoff);
65ef510424SDan Williams 
6626f2f4deSDan Williams #if IS_ENABLED(CONFIG_FS_DAX)
6778f35473SDan Williams struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev)
6878f35473SDan Williams {
6978f35473SDan Williams 	if (!blk_queue_dax(bdev->bd_queue))
7078f35473SDan Williams 		return NULL;
7178f35473SDan Williams 	return fs_dax_get_by_host(bdev->bd_disk->disk_name);
7278f35473SDan Williams }
7378f35473SDan Williams EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
7426f2f4deSDan Williams #endif
7578f35473SDan Williams 
76ef510424SDan Williams /**
77ef510424SDan Williams  * __bdev_dax_supported() - Check if the device supports dax for filesystem
78ba23cba9SDarrick J. Wong  * @bdev: block device to check
79ef510424SDan Williams  * @blocksize: The block size of the device
80ef510424SDan Williams  *
81ef510424SDan Williams  * This is a library function for filesystems to check if the block device
82ef510424SDan Williams  * can be mounted with dax option.
83ef510424SDan Williams  *
8480660f20SDave Jiang  * Return: true if supported, false if unsupported
85ef510424SDan Williams  */
8680660f20SDave Jiang bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
87ef510424SDan Williams {
88ef510424SDan Williams 	struct dax_device *dax_dev;
89e7638488SDan Williams 	bool dax_enabled = false;
9015256f6cSRoss Zwisler 	struct request_queue *q;
91ef510424SDan Williams 	pgoff_t pgoff;
92ef510424SDan Williams 	int err, id;
93ef510424SDan Williams 	pfn_t pfn;
94ef510424SDan Williams 	long len;
95ba23cba9SDarrick J. Wong 	char buf[BDEVNAME_SIZE];
96ef510424SDan Williams 
97ef510424SDan Williams 	if (blocksize != PAGE_SIZE) {
98ba23cba9SDarrick J. Wong 		pr_debug("%s: error: unsupported blocksize for dax\n",
99ba23cba9SDarrick J. Wong 				bdevname(bdev, buf));
10080660f20SDave Jiang 		return false;
101ef510424SDan Williams 	}
102ef510424SDan Williams 
10315256f6cSRoss Zwisler 	q = bdev_get_queue(bdev);
10415256f6cSRoss Zwisler 	if (!q || !blk_queue_dax(q)) {
10515256f6cSRoss Zwisler 		pr_debug("%s: error: request queue doesn't support dax\n",
10615256f6cSRoss Zwisler 				bdevname(bdev, buf));
10715256f6cSRoss Zwisler 		return false;
10815256f6cSRoss Zwisler 	}
10915256f6cSRoss Zwisler 
110ef510424SDan Williams 	err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
111ef510424SDan Williams 	if (err) {
112ba23cba9SDarrick J. Wong 		pr_debug("%s: error: unaligned partition for dax\n",
113ba23cba9SDarrick J. Wong 				bdevname(bdev, buf));
11480660f20SDave Jiang 		return false;
115ef510424SDan Williams 	}
116ef510424SDan Williams 
117ef510424SDan Williams 	dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
118ef510424SDan Williams 	if (!dax_dev) {
119ba23cba9SDarrick J. Wong 		pr_debug("%s: error: device does not support dax\n",
120ba23cba9SDarrick J. Wong 				bdevname(bdev, buf));
12180660f20SDave Jiang 		return false;
122ef510424SDan Williams 	}
123ef510424SDan Williams 
124ef510424SDan Williams 	id = dax_read_lock();
125e0b401e3SHuaisheng Ye 	len = dax_direct_access(dax_dev, pgoff, 1, NULL, &pfn);
126ef510424SDan Williams 	dax_read_unlock(id);
127ef510424SDan Williams 
128ef510424SDan Williams 	put_dax(dax_dev);
129ef510424SDan Williams 
130ef510424SDan Williams 	if (len < 1) {
131ba23cba9SDarrick J. Wong 		pr_debug("%s: error: dax access failed (%ld)\n",
132ba23cba9SDarrick J. Wong 				bdevname(bdev, buf), len);
13380660f20SDave Jiang 		return false;
134ef510424SDan Williams 	}
135ef510424SDan Williams 
1363fe0791cSDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED) && pfn_t_special(pfn)) {
1373fe0791cSDan Williams 		/*
1383fe0791cSDan Williams 		 * An arch that has enabled the pmem api should also
1393fe0791cSDan Williams 		 * have its drivers support pfn_t_devmap()
1403fe0791cSDan Williams 		 *
1413fe0791cSDan Williams 		 * This is a developer warning and should not trigger in
1423fe0791cSDan Williams 		 * production. dax_flush() will crash since it depends
1433fe0791cSDan Williams 		 * on being able to do (page_address(pfn_to_page())).
1443fe0791cSDan Williams 		 */
1453fe0791cSDan Williams 		WARN_ON(IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API));
146e7638488SDan Williams 		dax_enabled = true;
1473fe0791cSDan Williams 	} else if (pfn_t_devmap(pfn)) {
148e7638488SDan Williams 		struct dev_pagemap *pgmap;
149e7638488SDan Williams 
150e7638488SDan Williams 		pgmap = get_dev_pagemap(pfn_t_to_pfn(pfn), NULL);
151e7638488SDan Williams 		if (pgmap && pgmap->type == MEMORY_DEVICE_FS_DAX)
152e7638488SDan Williams 			dax_enabled = true;
153e7638488SDan Williams 		put_dev_pagemap(pgmap);
154e7638488SDan Williams 	}
155e7638488SDan Williams 
156e7638488SDan Williams 	if (!dax_enabled) {
157ba23cba9SDarrick J. Wong 		pr_debug("%s: error: dax support not enabled\n",
158ba23cba9SDarrick J. Wong 				bdevname(bdev, buf));
15980660f20SDave Jiang 		return false;
160569d0365SDan Williams 	}
16180660f20SDave Jiang 	return true;
162ef510424SDan Williams }
163ef510424SDan Williams EXPORT_SYMBOL_GPL(__bdev_dax_supported);
1649d109081SDan Williams #endif
165ef510424SDan Williams 
1669a60c3efSDan Williams enum dax_device_flags {
1679a60c3efSDan Williams 	/* !alive + rcu grace period == no new operations / mappings */
1689a60c3efSDan Williams 	DAXDEV_ALIVE,
1696e0c90d6SDan Williams 	/* gate whether dax_flush() calls the low level flush routine */
1706e0c90d6SDan Williams 	DAXDEV_WRITE_CACHE,
1719a60c3efSDan Williams };
1729a60c3efSDan Williams 
1737b6be844SDan Williams /**
1747b6be844SDan Williams  * struct dax_device - anchor object for dax services
1757b6be844SDan Williams  * @inode: core vfs
1767b6be844SDan Williams  * @cdev: optional character interface for "device dax"
17772058005SDan Williams  * @host: optional name for lookups where the device path is not available
1787b6be844SDan Williams  * @private: dax driver private data
1799a60c3efSDan Williams  * @flags: state and boolean properties
1807b6be844SDan Williams  */
1817b6be844SDan Williams struct dax_device {
18272058005SDan Williams 	struct hlist_node list;
1837b6be844SDan Williams 	struct inode inode;
1847b6be844SDan Williams 	struct cdev cdev;
18572058005SDan Williams 	const char *host;
1867b6be844SDan Williams 	void *private;
1879a60c3efSDan Williams 	unsigned long flags;
1886568b08bSDan Williams 	const struct dax_operations *ops;
1897b6be844SDan Williams };
1907b6be844SDan Williams 
1916e0c90d6SDan Williams static ssize_t write_cache_show(struct device *dev,
1926e0c90d6SDan Williams 		struct device_attribute *attr, char *buf)
1936e0c90d6SDan Williams {
1946e0c90d6SDan Williams 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
1956e0c90d6SDan Williams 	ssize_t rc;
1966e0c90d6SDan Williams 
1976e0c90d6SDan Williams 	WARN_ON_ONCE(!dax_dev);
1986e0c90d6SDan Williams 	if (!dax_dev)
1996e0c90d6SDan Williams 		return -ENXIO;
2006e0c90d6SDan Williams 
201808c340bSRoss Zwisler 	rc = sprintf(buf, "%d\n", !!dax_write_cache_enabled(dax_dev));
2026e0c90d6SDan Williams 	put_dax(dax_dev);
2036e0c90d6SDan Williams 	return rc;
2046e0c90d6SDan Williams }
2056e0c90d6SDan Williams 
2066e0c90d6SDan Williams static ssize_t write_cache_store(struct device *dev,
2076e0c90d6SDan Williams 		struct device_attribute *attr, const char *buf, size_t len)
2086e0c90d6SDan Williams {
2096e0c90d6SDan Williams 	bool write_cache;
2106e0c90d6SDan Williams 	int rc = strtobool(buf, &write_cache);
2116e0c90d6SDan Williams 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
2126e0c90d6SDan Williams 
2136e0c90d6SDan Williams 	WARN_ON_ONCE(!dax_dev);
2146e0c90d6SDan Williams 	if (!dax_dev)
2156e0c90d6SDan Williams 		return -ENXIO;
2166e0c90d6SDan Williams 
2176e0c90d6SDan Williams 	if (rc)
2186e0c90d6SDan Williams 		len = rc;
2196e0c90d6SDan Williams 	else
220808c340bSRoss Zwisler 		dax_write_cache(dax_dev, write_cache);
2216e0c90d6SDan Williams 
2226e0c90d6SDan Williams 	put_dax(dax_dev);
2236e0c90d6SDan Williams 	return len;
2246e0c90d6SDan Williams }
2256e0c90d6SDan Williams static DEVICE_ATTR_RW(write_cache);
2266e0c90d6SDan Williams 
2276e0c90d6SDan Williams static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
2286e0c90d6SDan Williams {
2296e0c90d6SDan Williams 	struct device *dev = container_of(kobj, typeof(*dev), kobj);
2306e0c90d6SDan Williams 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
2316e0c90d6SDan Williams 
2326e0c90d6SDan Williams 	WARN_ON_ONCE(!dax_dev);
2336e0c90d6SDan Williams 	if (!dax_dev)
2346e0c90d6SDan Williams 		return 0;
2356e0c90d6SDan Williams 
236c3ca015fSMikulas Patocka #ifndef CONFIG_ARCH_HAS_PMEM_API
237c3ca015fSMikulas Patocka 	if (a == &dev_attr_write_cache.attr)
2386e0c90d6SDan Williams 		return 0;
239c3ca015fSMikulas Patocka #endif
2406e0c90d6SDan Williams 	return a->mode;
2416e0c90d6SDan Williams }
2426e0c90d6SDan Williams 
2436e0c90d6SDan Williams static struct attribute *dax_attributes[] = {
2446e0c90d6SDan Williams 	&dev_attr_write_cache.attr,
2456e0c90d6SDan Williams 	NULL,
2466e0c90d6SDan Williams };
2476e0c90d6SDan Williams 
2486e0c90d6SDan Williams struct attribute_group dax_attribute_group = {
2496e0c90d6SDan Williams 	.name = "dax",
2506e0c90d6SDan Williams 	.attrs = dax_attributes,
2516e0c90d6SDan Williams 	.is_visible = dax_visible,
2526e0c90d6SDan Williams };
2536e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_attribute_group);
2546e0c90d6SDan Williams 
255b0686260SDan Williams /**
256b0686260SDan Williams  * dax_direct_access() - translate a device pgoff to an absolute pfn
257b0686260SDan Williams  * @dax_dev: a dax_device instance representing the logical memory range
258b0686260SDan Williams  * @pgoff: offset in pages from the start of the device to translate
259b0686260SDan Williams  * @nr_pages: number of consecutive pages caller can handle relative to @pfn
260b0686260SDan Williams  * @kaddr: output parameter that returns a virtual address mapping of pfn
261b0686260SDan Williams  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
262b0686260SDan Williams  *
263b0686260SDan Williams  * Return: negative errno if an error occurs, otherwise the number of
264b0686260SDan Williams  * pages accessible at the device relative @pgoff.
265b0686260SDan Williams  */
266b0686260SDan Williams long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
267b0686260SDan Williams 		void **kaddr, pfn_t *pfn)
268b0686260SDan Williams {
269b0686260SDan Williams 	long avail;
270b0686260SDan Williams 
271b0686260SDan Williams 	if (!dax_dev)
272b0686260SDan Williams 		return -EOPNOTSUPP;
273b0686260SDan Williams 
274b0686260SDan Williams 	if (!dax_alive(dax_dev))
275b0686260SDan Williams 		return -ENXIO;
276b0686260SDan Williams 
277b0686260SDan Williams 	if (nr_pages < 0)
278b0686260SDan Williams 		return nr_pages;
279b0686260SDan Williams 
280b0686260SDan Williams 	avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
281b0686260SDan Williams 			kaddr, pfn);
282b0686260SDan Williams 	if (!avail)
283b0686260SDan Williams 		return -ERANGE;
284b0686260SDan Williams 	return min(avail, nr_pages);
285b0686260SDan Williams }
286b0686260SDan Williams EXPORT_SYMBOL_GPL(dax_direct_access);
287b0686260SDan Williams 
2887e026c8cSDan Williams size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
2897e026c8cSDan Williams 		size_t bytes, struct iov_iter *i)
2907e026c8cSDan Williams {
2917e026c8cSDan Williams 	if (!dax_alive(dax_dev))
2927e026c8cSDan Williams 		return 0;
2937e026c8cSDan Williams 
2947e026c8cSDan Williams 	return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
2957e026c8cSDan Williams }
2967e026c8cSDan Williams EXPORT_SYMBOL_GPL(dax_copy_from_iter);
2977e026c8cSDan Williams 
298b3a9a0c3SDan Williams size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
299b3a9a0c3SDan Williams 		size_t bytes, struct iov_iter *i)
300b3a9a0c3SDan Williams {
301b3a9a0c3SDan Williams 	if (!dax_alive(dax_dev))
302b3a9a0c3SDan Williams 		return 0;
303b3a9a0c3SDan Williams 
304b3a9a0c3SDan Williams 	return dax_dev->ops->copy_to_iter(dax_dev, pgoff, addr, bytes, i);
305b3a9a0c3SDan Williams }
306b3a9a0c3SDan Williams EXPORT_SYMBOL_GPL(dax_copy_to_iter);
307b3a9a0c3SDan Williams 
308c3ca015fSMikulas Patocka #ifdef CONFIG_ARCH_HAS_PMEM_API
309c3ca015fSMikulas Patocka void arch_wb_cache_pmem(void *addr, size_t size);
310c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
311abebfbe2SDan Williams {
312808c340bSRoss Zwisler 	if (unlikely(!dax_write_cache_enabled(dax_dev)))
3136e0c90d6SDan Williams 		return;
3146e0c90d6SDan Williams 
315c3ca015fSMikulas Patocka 	arch_wb_cache_pmem(addr, size);
316abebfbe2SDan Williams }
317c3ca015fSMikulas Patocka #else
318c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
319c3ca015fSMikulas Patocka {
320c3ca015fSMikulas Patocka }
321c3ca015fSMikulas Patocka #endif
322abebfbe2SDan Williams EXPORT_SYMBOL_GPL(dax_flush);
323abebfbe2SDan Williams 
3246e0c90d6SDan Williams void dax_write_cache(struct dax_device *dax_dev, bool wc)
3256e0c90d6SDan Williams {
3266e0c90d6SDan Williams 	if (wc)
3276e0c90d6SDan Williams 		set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
3286e0c90d6SDan Williams 	else
3296e0c90d6SDan Williams 		clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
3306e0c90d6SDan Williams }
3316e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_write_cache);
3326e0c90d6SDan Williams 
333273752c9SVivek Goyal bool dax_write_cache_enabled(struct dax_device *dax_dev)
334273752c9SVivek Goyal {
335273752c9SVivek Goyal 	return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
336273752c9SVivek Goyal }
337273752c9SVivek Goyal EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
338273752c9SVivek Goyal 
3397b6be844SDan Williams bool dax_alive(struct dax_device *dax_dev)
3407b6be844SDan Williams {
3417b6be844SDan Williams 	lockdep_assert_held(&dax_srcu);
3429a60c3efSDan Williams 	return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
3437b6be844SDan Williams }
3447b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_alive);
3457b6be844SDan Williams 
34672058005SDan Williams static int dax_host_hash(const char *host)
34772058005SDan Williams {
34872058005SDan Williams 	return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
34972058005SDan Williams }
35072058005SDan Williams 
3517b6be844SDan Williams /*
3527b6be844SDan Williams  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
3537b6be844SDan Williams  * that any fault handlers or operations that might have seen
3547b6be844SDan Williams  * dax_alive(), have completed.  Any operations that start after
3557b6be844SDan Williams  * synchronize_srcu() has run will abort upon seeing !dax_alive().
3567b6be844SDan Williams  */
3577b6be844SDan Williams void kill_dax(struct dax_device *dax_dev)
3587b6be844SDan Williams {
3597b6be844SDan Williams 	if (!dax_dev)
3607b6be844SDan Williams 		return;
3617b6be844SDan Williams 
3629a60c3efSDan Williams 	clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
36372058005SDan Williams 
3647b6be844SDan Williams 	synchronize_srcu(&dax_srcu);
36572058005SDan Williams 
36672058005SDan Williams 	spin_lock(&dax_host_lock);
36772058005SDan Williams 	hlist_del_init(&dax_dev->list);
36872058005SDan Williams 	spin_unlock(&dax_host_lock);
36972058005SDan Williams 
3707b6be844SDan Williams 	dax_dev->private = NULL;
3717b6be844SDan Williams }
3727b6be844SDan Williams EXPORT_SYMBOL_GPL(kill_dax);
3737b6be844SDan Williams 
3747b6be844SDan Williams static struct inode *dax_alloc_inode(struct super_block *sb)
3757b6be844SDan Williams {
3767b6be844SDan Williams 	struct dax_device *dax_dev;
377b9d39d17SDan Williams 	struct inode *inode;
3787b6be844SDan Williams 
3797b6be844SDan Williams 	dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
3809f586fffSMikulas Patocka 	if (!dax_dev)
3819f586fffSMikulas Patocka 		return NULL;
3829f586fffSMikulas Patocka 
383b9d39d17SDan Williams 	inode = &dax_dev->inode;
384b9d39d17SDan Williams 	inode->i_rdev = 0;
385b9d39d17SDan Williams 	return inode;
3867b6be844SDan Williams }
3877b6be844SDan Williams 
3887b6be844SDan Williams static struct dax_device *to_dax_dev(struct inode *inode)
3897b6be844SDan Williams {
3907b6be844SDan Williams 	return container_of(inode, struct dax_device, inode);
3917b6be844SDan Williams }
3927b6be844SDan Williams 
3937b6be844SDan Williams static void dax_i_callback(struct rcu_head *head)
3947b6be844SDan Williams {
3957b6be844SDan Williams 	struct inode *inode = container_of(head, struct inode, i_rcu);
3967b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
3977b6be844SDan Williams 
39872058005SDan Williams 	kfree(dax_dev->host);
39972058005SDan Williams 	dax_dev->host = NULL;
400b9d39d17SDan Williams 	if (inode->i_rdev)
4017b6be844SDan Williams 		ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
4027b6be844SDan Williams 	kmem_cache_free(dax_cache, dax_dev);
4037b6be844SDan Williams }
4047b6be844SDan Williams 
4057b6be844SDan Williams static void dax_destroy_inode(struct inode *inode)
4067b6be844SDan Williams {
4077b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
4087b6be844SDan Williams 
4099a60c3efSDan Williams 	WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
4107b6be844SDan Williams 			"kill_dax() must be called before final iput()\n");
4117b6be844SDan Williams 	call_rcu(&inode->i_rcu, dax_i_callback);
4127b6be844SDan Williams }
4137b6be844SDan Williams 
4147b6be844SDan Williams static const struct super_operations dax_sops = {
4157b6be844SDan Williams 	.statfs = simple_statfs,
4167b6be844SDan Williams 	.alloc_inode = dax_alloc_inode,
4177b6be844SDan Williams 	.destroy_inode = dax_destroy_inode,
4187b6be844SDan Williams 	.drop_inode = generic_delete_inode,
4197b6be844SDan Williams };
4207b6be844SDan Williams 
4217b6be844SDan Williams static struct dentry *dax_mount(struct file_system_type *fs_type,
4227b6be844SDan Williams 		int flags, const char *dev_name, void *data)
4237b6be844SDan Williams {
4247b6be844SDan Williams 	return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
4257b6be844SDan Williams }
4267b6be844SDan Williams 
4277b6be844SDan Williams static struct file_system_type dax_fs_type = {
4287b6be844SDan Williams 	.name = "dax",
4297b6be844SDan Williams 	.mount = dax_mount,
4307b6be844SDan Williams 	.kill_sb = kill_anon_super,
4317b6be844SDan Williams };
4327b6be844SDan Williams 
4337b6be844SDan Williams static int dax_test(struct inode *inode, void *data)
4347b6be844SDan Williams {
4357b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
4367b6be844SDan Williams 
4377b6be844SDan Williams 	return inode->i_rdev == devt;
4387b6be844SDan Williams }
4397b6be844SDan Williams 
4407b6be844SDan Williams static int dax_set(struct inode *inode, void *data)
4417b6be844SDan Williams {
4427b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
4437b6be844SDan Williams 
4447b6be844SDan Williams 	inode->i_rdev = devt;
4457b6be844SDan Williams 	return 0;
4467b6be844SDan Williams }
4477b6be844SDan Williams 
4487b6be844SDan Williams static struct dax_device *dax_dev_get(dev_t devt)
4497b6be844SDan Williams {
4507b6be844SDan Williams 	struct dax_device *dax_dev;
4517b6be844SDan Williams 	struct inode *inode;
4527b6be844SDan Williams 
4537b6be844SDan Williams 	inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
4547b6be844SDan Williams 			dax_test, dax_set, &devt);
4557b6be844SDan Williams 
4567b6be844SDan Williams 	if (!inode)
4577b6be844SDan Williams 		return NULL;
4587b6be844SDan Williams 
4597b6be844SDan Williams 	dax_dev = to_dax_dev(inode);
4607b6be844SDan Williams 	if (inode->i_state & I_NEW) {
4619a60c3efSDan Williams 		set_bit(DAXDEV_ALIVE, &dax_dev->flags);
4627b6be844SDan Williams 		inode->i_cdev = &dax_dev->cdev;
4637b6be844SDan Williams 		inode->i_mode = S_IFCHR;
4647b6be844SDan Williams 		inode->i_flags = S_DAX;
4657b6be844SDan Williams 		mapping_set_gfp_mask(&inode->i_data, GFP_USER);
4667b6be844SDan Williams 		unlock_new_inode(inode);
4677b6be844SDan Williams 	}
4687b6be844SDan Williams 
4697b6be844SDan Williams 	return dax_dev;
4707b6be844SDan Williams }
4717b6be844SDan Williams 
47272058005SDan Williams static void dax_add_host(struct dax_device *dax_dev, const char *host)
47372058005SDan Williams {
47472058005SDan Williams 	int hash;
47572058005SDan Williams 
47672058005SDan Williams 	/*
47772058005SDan Williams 	 * Unconditionally init dax_dev since it's coming from a
47872058005SDan Williams 	 * non-zeroed slab cache
47972058005SDan Williams 	 */
48072058005SDan Williams 	INIT_HLIST_NODE(&dax_dev->list);
48172058005SDan Williams 	dax_dev->host = host;
48272058005SDan Williams 	if (!host)
48372058005SDan Williams 		return;
48472058005SDan Williams 
48572058005SDan Williams 	hash = dax_host_hash(host);
48672058005SDan Williams 	spin_lock(&dax_host_lock);
48772058005SDan Williams 	hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
48872058005SDan Williams 	spin_unlock(&dax_host_lock);
48972058005SDan Williams }
49072058005SDan Williams 
4916568b08bSDan Williams struct dax_device *alloc_dax(void *private, const char *__host,
4926568b08bSDan Williams 		const struct dax_operations *ops)
4937b6be844SDan Williams {
4947b6be844SDan Williams 	struct dax_device *dax_dev;
49572058005SDan Williams 	const char *host;
4967b6be844SDan Williams 	dev_t devt;
4977b6be844SDan Williams 	int minor;
4987b6be844SDan Williams 
49972058005SDan Williams 	host = kstrdup(__host, GFP_KERNEL);
50072058005SDan Williams 	if (__host && !host)
50172058005SDan Williams 		return NULL;
50272058005SDan Williams 
503cf1e2289SDan Williams 	minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
5047b6be844SDan Williams 	if (minor < 0)
50572058005SDan Williams 		goto err_minor;
5067b6be844SDan Williams 
5077b6be844SDan Williams 	devt = MKDEV(MAJOR(dax_devt), minor);
5087b6be844SDan Williams 	dax_dev = dax_dev_get(devt);
5097b6be844SDan Williams 	if (!dax_dev)
51072058005SDan Williams 		goto err_dev;
5117b6be844SDan Williams 
51272058005SDan Williams 	dax_add_host(dax_dev, host);
5136568b08bSDan Williams 	dax_dev->ops = ops;
5147b6be844SDan Williams 	dax_dev->private = private;
5157b6be844SDan Williams 	return dax_dev;
5167b6be844SDan Williams 
51772058005SDan Williams  err_dev:
5187b6be844SDan Williams 	ida_simple_remove(&dax_minor_ida, minor);
51972058005SDan Williams  err_minor:
52072058005SDan Williams 	kfree(host);
5217b6be844SDan Williams 	return NULL;
5227b6be844SDan Williams }
5237b6be844SDan Williams EXPORT_SYMBOL_GPL(alloc_dax);
5247b6be844SDan Williams 
5257b6be844SDan Williams void put_dax(struct dax_device *dax_dev)
5267b6be844SDan Williams {
5277b6be844SDan Williams 	if (!dax_dev)
5287b6be844SDan Williams 		return;
5297b6be844SDan Williams 	iput(&dax_dev->inode);
5307b6be844SDan Williams }
5317b6be844SDan Williams EXPORT_SYMBOL_GPL(put_dax);
5327b6be844SDan Williams 
5337b6be844SDan Williams /**
53472058005SDan Williams  * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
53572058005SDan Williams  * @host: alternate name for the device registered by a dax driver
53672058005SDan Williams  */
53772058005SDan Williams struct dax_device *dax_get_by_host(const char *host)
53872058005SDan Williams {
53972058005SDan Williams 	struct dax_device *dax_dev, *found = NULL;
54072058005SDan Williams 	int hash, id;
54172058005SDan Williams 
54272058005SDan Williams 	if (!host)
54372058005SDan Williams 		return NULL;
54472058005SDan Williams 
54572058005SDan Williams 	hash = dax_host_hash(host);
54672058005SDan Williams 
54772058005SDan Williams 	id = dax_read_lock();
54872058005SDan Williams 	spin_lock(&dax_host_lock);
54972058005SDan Williams 	hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
55072058005SDan Williams 		if (!dax_alive(dax_dev)
55172058005SDan Williams 				|| strcmp(host, dax_dev->host) != 0)
55272058005SDan Williams 			continue;
55372058005SDan Williams 
55472058005SDan Williams 		if (igrab(&dax_dev->inode))
55572058005SDan Williams 			found = dax_dev;
55672058005SDan Williams 		break;
55772058005SDan Williams 	}
55872058005SDan Williams 	spin_unlock(&dax_host_lock);
55972058005SDan Williams 	dax_read_unlock(id);
56072058005SDan Williams 
56172058005SDan Williams 	return found;
56272058005SDan Williams }
56372058005SDan Williams EXPORT_SYMBOL_GPL(dax_get_by_host);
56472058005SDan Williams 
56572058005SDan Williams /**
5667b6be844SDan Williams  * inode_dax: convert a public inode into its dax_dev
5677b6be844SDan Williams  * @inode: An inode with i_cdev pointing to a dax_dev
5687b6be844SDan Williams  *
5697b6be844SDan Williams  * Note this is not equivalent to to_dax_dev() which is for private
5707b6be844SDan Williams  * internal use where we know the inode filesystem type == dax_fs_type.
5717b6be844SDan Williams  */
5727b6be844SDan Williams struct dax_device *inode_dax(struct inode *inode)
5737b6be844SDan Williams {
5747b6be844SDan Williams 	struct cdev *cdev = inode->i_cdev;
5757b6be844SDan Williams 
5767b6be844SDan Williams 	return container_of(cdev, struct dax_device, cdev);
5777b6be844SDan Williams }
5787b6be844SDan Williams EXPORT_SYMBOL_GPL(inode_dax);
5797b6be844SDan Williams 
5807b6be844SDan Williams struct inode *dax_inode(struct dax_device *dax_dev)
5817b6be844SDan Williams {
5827b6be844SDan Williams 	return &dax_dev->inode;
5837b6be844SDan Williams }
5847b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_inode);
5857b6be844SDan Williams 
5867b6be844SDan Williams void *dax_get_private(struct dax_device *dax_dev)
5877b6be844SDan Williams {
5887b6be844SDan Williams 	return dax_dev->private;
5897b6be844SDan Williams }
5907b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_get_private);
5917b6be844SDan Williams 
5927b6be844SDan Williams static void init_once(void *_dax_dev)
5937b6be844SDan Williams {
5947b6be844SDan Williams 	struct dax_device *dax_dev = _dax_dev;
5957b6be844SDan Williams 	struct inode *inode = &dax_dev->inode;
5967b6be844SDan Williams 
597b9d39d17SDan Williams 	memset(dax_dev, 0, sizeof(*dax_dev));
5987b6be844SDan Williams 	inode_init_once(inode);
5997b6be844SDan Williams }
6007b6be844SDan Williams 
6017b6be844SDan Williams static int __dax_fs_init(void)
6027b6be844SDan Williams {
6037b6be844SDan Williams 	int rc;
6047b6be844SDan Williams 
6057b6be844SDan Williams 	dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
6067b6be844SDan Williams 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
6077b6be844SDan Williams 			 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
6087b6be844SDan Williams 			init_once);
6097b6be844SDan Williams 	if (!dax_cache)
6107b6be844SDan Williams 		return -ENOMEM;
6117b6be844SDan Williams 
6127b6be844SDan Williams 	rc = register_filesystem(&dax_fs_type);
6137b6be844SDan Williams 	if (rc)
6147b6be844SDan Williams 		goto err_register_fs;
6157b6be844SDan Williams 
6167b6be844SDan Williams 	dax_mnt = kern_mount(&dax_fs_type);
6177b6be844SDan Williams 	if (IS_ERR(dax_mnt)) {
6187b6be844SDan Williams 		rc = PTR_ERR(dax_mnt);
6197b6be844SDan Williams 		goto err_mount;
6207b6be844SDan Williams 	}
6217b6be844SDan Williams 	dax_superblock = dax_mnt->mnt_sb;
6227b6be844SDan Williams 
6237b6be844SDan Williams 	return 0;
6247b6be844SDan Williams 
6257b6be844SDan Williams  err_mount:
6267b6be844SDan Williams 	unregister_filesystem(&dax_fs_type);
6277b6be844SDan Williams  err_register_fs:
6287b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
6297b6be844SDan Williams 
6307b6be844SDan Williams 	return rc;
6317b6be844SDan Williams }
6327b6be844SDan Williams 
6337b6be844SDan Williams static void __dax_fs_exit(void)
6347b6be844SDan Williams {
6357b6be844SDan Williams 	kern_unmount(dax_mnt);
6367b6be844SDan Williams 	unregister_filesystem(&dax_fs_type);
6377b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
6387b6be844SDan Williams }
6397b6be844SDan Williams 
6407b6be844SDan Williams static int __init dax_fs_init(void)
6417b6be844SDan Williams {
6427b6be844SDan Williams 	int rc;
6437b6be844SDan Williams 
6447b6be844SDan Williams 	rc = __dax_fs_init();
6457b6be844SDan Williams 	if (rc)
6467b6be844SDan Williams 		return rc;
6477b6be844SDan Williams 
648cf1e2289SDan Williams 	rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
6497b6be844SDan Williams 	if (rc)
6507b6be844SDan Williams 		__dax_fs_exit();
6517b6be844SDan Williams 	return rc;
6527b6be844SDan Williams }
6537b6be844SDan Williams 
6547b6be844SDan Williams static void __exit dax_fs_exit(void)
6557b6be844SDan Williams {
656cf1e2289SDan Williams 	unregister_chrdev_region(dax_devt, MINORMASK+1);
6577b6be844SDan Williams 	ida_destroy(&dax_minor_ida);
6587b6be844SDan Williams 	__dax_fs_exit();
6597b6be844SDan Williams }
6607b6be844SDan Williams 
6617b6be844SDan Williams MODULE_AUTHOR("Intel Corporation");
6627b6be844SDan Williams MODULE_LICENSE("GPL v2");
6637b6be844SDan Williams subsys_initcall(dax_fs_init);
6647b6be844SDan Williams module_exit(dax_fs_exit);
665