xref: /openbmc/linux/drivers/dax/super.c (revision eedfd73d)
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>
10ef510424SDan Williams #include <linux/genhd.h>
11569d0365SDan Williams #include <linux/pfn_t.h>
127b6be844SDan Williams #include <linux/cdev.h>
137b6be844SDan Williams #include <linux/hash.h>
147b6be844SDan Williams #include <linux/slab.h>
157e026c8cSDan Williams #include <linux/uio.h>
166568b08bSDan Williams #include <linux/dax.h>
177b6be844SDan Williams #include <linux/fs.h>
1851cf784cSDan Williams #include "dax-private.h"
197b6be844SDan Williams 
207b6be844SDan Williams static dev_t dax_devt;
217b6be844SDan Williams DEFINE_STATIC_SRCU(dax_srcu);
227b6be844SDan Williams static struct vfsmount *dax_mnt;
237b6be844SDan Williams static DEFINE_IDA(dax_minor_ida);
247b6be844SDan Williams static struct kmem_cache *dax_cache __read_mostly;
257b6be844SDan Williams static struct super_block *dax_superblock __read_mostly;
267b6be844SDan Williams 
2772058005SDan Williams #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
2872058005SDan Williams static struct hlist_head dax_host_list[DAX_HASH_SIZE];
2972058005SDan Williams static DEFINE_SPINLOCK(dax_host_lock);
3072058005SDan Williams 
317b6be844SDan Williams int dax_read_lock(void)
327b6be844SDan Williams {
337b6be844SDan Williams 	return srcu_read_lock(&dax_srcu);
347b6be844SDan Williams }
357b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_lock);
367b6be844SDan Williams 
377b6be844SDan Williams void dax_read_unlock(int id)
387b6be844SDan Williams {
397b6be844SDan Williams 	srcu_read_unlock(&dax_srcu, id);
407b6be844SDan Williams }
417b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_read_unlock);
427b6be844SDan Williams 
439d109081SDan Williams #ifdef CONFIG_BLOCK
4478f35473SDan Williams #include <linux/blkdev.h>
4578f35473SDan Williams 
46ef510424SDan Williams int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
47ef510424SDan Williams 		pgoff_t *pgoff)
48ef510424SDan Williams {
49ef510424SDan Williams 	phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
50ef510424SDan Williams 
51ef510424SDan Williams 	if (pgoff)
52ef510424SDan Williams 		*pgoff = PHYS_PFN(phys_off);
53ef510424SDan Williams 	if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
54ef510424SDan Williams 		return -EINVAL;
55ef510424SDan Williams 	return 0;
56ef510424SDan Williams }
57ef510424SDan Williams EXPORT_SYMBOL(bdev_dax_pgoff);
58ef510424SDan Williams 
5926f2f4deSDan Williams #if IS_ENABLED(CONFIG_FS_DAX)
6078f35473SDan Williams struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev)
6178f35473SDan Williams {
6278f35473SDan Williams 	if (!blk_queue_dax(bdev->bd_queue))
6378f35473SDan Williams 		return NULL;
64f01b16a8SVivek Goyal 	return dax_get_by_host(bdev->bd_disk->disk_name);
6578f35473SDan Williams }
6678f35473SDan Williams EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
6726f2f4deSDan Williams #endif
6878f35473SDan Williams 
697bf7eac8SDan Williams bool __generic_fsdax_supported(struct dax_device *dax_dev,
707bf7eac8SDan Williams 		struct block_device *bdev, int blocksize, sector_t start,
717bf7eac8SDan Williams 		sector_t sectors)
72ef510424SDan Williams {
73e7638488SDan Williams 	bool dax_enabled = false;
74ad428cdbSDan Williams 	pgoff_t pgoff, pgoff_end;
75ba23cba9SDarrick J. Wong 	char buf[BDEVNAME_SIZE];
76ad428cdbSDan Williams 	void *kaddr, *end_kaddr;
77ad428cdbSDan Williams 	pfn_t pfn, end_pfn;
78ad428cdbSDan Williams 	sector_t last_page;
79ad428cdbSDan Williams 	long len, len2;
80ad428cdbSDan Williams 	int err, id;
81ef510424SDan Williams 
82ef510424SDan Williams 	if (blocksize != PAGE_SIZE) {
8323160978SColy Li 		pr_info("%s: error: unsupported blocksize for dax\n",
84ba23cba9SDarrick J. Wong 				bdevname(bdev, buf));
8580660f20SDave Jiang 		return false;
86ef510424SDan Williams 	}
87ef510424SDan Williams 
887bf7eac8SDan Williams 	err = bdev_dax_pgoff(bdev, start, PAGE_SIZE, &pgoff);
89ef510424SDan Williams 	if (err) {
9023160978SColy Li 		pr_info("%s: error: unaligned partition for dax\n",
91ba23cba9SDarrick J. Wong 				bdevname(bdev, buf));
9280660f20SDave Jiang 		return false;
93ef510424SDan Williams 	}
94ef510424SDan Williams 
957bf7eac8SDan Williams 	last_page = PFN_DOWN((start + sectors - 1) * 512) * PAGE_SIZE / 512;
96ad428cdbSDan Williams 	err = bdev_dax_pgoff(bdev, last_page, PAGE_SIZE, &pgoff_end);
97ad428cdbSDan Williams 	if (err) {
9823160978SColy Li 		pr_info("%s: error: unaligned partition for dax\n",
99ad428cdbSDan Williams 				bdevname(bdev, buf));
100ad428cdbSDan Williams 		return false;
101ad428cdbSDan Williams 	}
102ad428cdbSDan Williams 
103ef510424SDan Williams 	id = dax_read_lock();
104ad428cdbSDan Williams 	len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
105ad428cdbSDan Williams 	len2 = dax_direct_access(dax_dev, pgoff_end, 1, &end_kaddr, &end_pfn);
106ef510424SDan Williams 
107ad428cdbSDan Williams 	if (len < 1 || len2 < 1) {
10823160978SColy Li 		pr_info("%s: error: dax access failed (%ld)\n",
109ad428cdbSDan Williams 				bdevname(bdev, buf), len < 1 ? len : len2);
110eedfd73dSIra Weiny 		dax_read_unlock(id);
11180660f20SDave Jiang 		return false;
112ef510424SDan Williams 	}
113ef510424SDan Williams 
1143fe0791cSDan Williams 	if (IS_ENABLED(CONFIG_FS_DAX_LIMITED) && pfn_t_special(pfn)) {
1153fe0791cSDan Williams 		/*
1163fe0791cSDan Williams 		 * An arch that has enabled the pmem api should also
1173fe0791cSDan Williams 		 * have its drivers support pfn_t_devmap()
1183fe0791cSDan Williams 		 *
1193fe0791cSDan Williams 		 * This is a developer warning and should not trigger in
1203fe0791cSDan Williams 		 * production. dax_flush() will crash since it depends
1213fe0791cSDan Williams 		 * on being able to do (page_address(pfn_to_page())).
1223fe0791cSDan Williams 		 */
1233fe0791cSDan Williams 		WARN_ON(IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API));
124e7638488SDan Williams 		dax_enabled = true;
125ad428cdbSDan Williams 	} else if (pfn_t_devmap(pfn) && pfn_t_devmap(end_pfn)) {
126ad428cdbSDan Williams 		struct dev_pagemap *pgmap, *end_pgmap;
127e7638488SDan Williams 
128e7638488SDan Williams 		pgmap = get_dev_pagemap(pfn_t_to_pfn(pfn), NULL);
129ad428cdbSDan Williams 		end_pgmap = get_dev_pagemap(pfn_t_to_pfn(end_pfn), NULL);
130ad428cdbSDan Williams 		if (pgmap && pgmap == end_pgmap && pgmap->type == MEMORY_DEVICE_FS_DAX
131ad428cdbSDan Williams 				&& pfn_t_to_page(pfn)->pgmap == pgmap
132ad428cdbSDan Williams 				&& pfn_t_to_page(end_pfn)->pgmap == pgmap
133ad428cdbSDan Williams 				&& pfn_t_to_pfn(pfn) == PHYS_PFN(__pa(kaddr))
134ad428cdbSDan Williams 				&& pfn_t_to_pfn(end_pfn) == PHYS_PFN(__pa(end_kaddr)))
135e7638488SDan Williams 			dax_enabled = true;
136e7638488SDan Williams 		put_dev_pagemap(pgmap);
137ad428cdbSDan Williams 		put_dev_pagemap(end_pgmap);
138ad428cdbSDan Williams 
139e7638488SDan Williams 	}
140eedfd73dSIra Weiny 	dax_read_unlock(id);
141e7638488SDan Williams 
142e7638488SDan Williams 	if (!dax_enabled) {
14323160978SColy Li 		pr_info("%s: error: dax support not enabled\n",
144ba23cba9SDarrick J. Wong 				bdevname(bdev, buf));
14580660f20SDave Jiang 		return false;
146569d0365SDan Williams 	}
14780660f20SDave Jiang 	return true;
148ef510424SDan Williams }
1497bf7eac8SDan Williams EXPORT_SYMBOL_GPL(__generic_fsdax_supported);
1507bf7eac8SDan Williams 
1517bf7eac8SDan Williams /**
1527bf7eac8SDan Williams  * __bdev_dax_supported() - Check if the device supports dax for filesystem
1537bf7eac8SDan Williams  * @bdev: block device to check
1547bf7eac8SDan Williams  * @blocksize: The block size of the device
1557bf7eac8SDan Williams  *
1567bf7eac8SDan Williams  * This is a library function for filesystems to check if the block device
1577bf7eac8SDan Williams  * can be mounted with dax option.
1587bf7eac8SDan Williams  *
1597bf7eac8SDan Williams  * Return: true if supported, false if unsupported
1607bf7eac8SDan Williams  */
1617bf7eac8SDan Williams bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
1627bf7eac8SDan Williams {
1637bf7eac8SDan Williams 	struct dax_device *dax_dev;
1647bf7eac8SDan Williams 	struct request_queue *q;
1657bf7eac8SDan Williams 	char buf[BDEVNAME_SIZE];
1667bf7eac8SDan Williams 	bool ret;
1677bf7eac8SDan Williams 	int id;
1687bf7eac8SDan Williams 
1697bf7eac8SDan Williams 	q = bdev_get_queue(bdev);
1707bf7eac8SDan Williams 	if (!q || !blk_queue_dax(q)) {
1717bf7eac8SDan Williams 		pr_debug("%s: error: request queue doesn't support dax\n",
1727bf7eac8SDan Williams 				bdevname(bdev, buf));
1737bf7eac8SDan Williams 		return false;
1747bf7eac8SDan Williams 	}
1757bf7eac8SDan Williams 
1767bf7eac8SDan Williams 	dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
1777bf7eac8SDan Williams 	if (!dax_dev) {
1787bf7eac8SDan Williams 		pr_debug("%s: error: device does not support dax\n",
1797bf7eac8SDan Williams 				bdevname(bdev, buf));
1807bf7eac8SDan Williams 		return false;
1817bf7eac8SDan Williams 	}
1827bf7eac8SDan Williams 
1837bf7eac8SDan Williams 	id = dax_read_lock();
1847bf7eac8SDan Williams 	ret = dax_supported(dax_dev, bdev, blocksize, 0,
1857bf7eac8SDan Williams 			i_size_read(bdev->bd_inode) / 512);
1867bf7eac8SDan Williams 	dax_read_unlock(id);
1877bf7eac8SDan Williams 
1887bf7eac8SDan Williams 	put_dax(dax_dev);
1897bf7eac8SDan Williams 
1907bf7eac8SDan Williams 	return ret;
1917bf7eac8SDan Williams }
192ef510424SDan Williams EXPORT_SYMBOL_GPL(__bdev_dax_supported);
1939d109081SDan Williams #endif
194ef510424SDan Williams 
1959a60c3efSDan Williams enum dax_device_flags {
1969a60c3efSDan Williams 	/* !alive + rcu grace period == no new operations / mappings */
1979a60c3efSDan Williams 	DAXDEV_ALIVE,
1986e0c90d6SDan Williams 	/* gate whether dax_flush() calls the low level flush routine */
1996e0c90d6SDan Williams 	DAXDEV_WRITE_CACHE,
200fefc1d97SPankaj Gupta 	/* flag to check if device supports synchronous flush */
201fefc1d97SPankaj Gupta 	DAXDEV_SYNC,
2029a60c3efSDan Williams };
2039a60c3efSDan Williams 
2047b6be844SDan Williams /**
2057b6be844SDan Williams  * struct dax_device - anchor object for dax services
2067b6be844SDan Williams  * @inode: core vfs
2077b6be844SDan Williams  * @cdev: optional character interface for "device dax"
20872058005SDan Williams  * @host: optional name for lookups where the device path is not available
2097b6be844SDan Williams  * @private: dax driver private data
2109a60c3efSDan Williams  * @flags: state and boolean properties
2117b6be844SDan Williams  */
2127b6be844SDan Williams struct dax_device {
21372058005SDan Williams 	struct hlist_node list;
2147b6be844SDan Williams 	struct inode inode;
2157b6be844SDan Williams 	struct cdev cdev;
21672058005SDan Williams 	const char *host;
2177b6be844SDan Williams 	void *private;
2189a60c3efSDan Williams 	unsigned long flags;
2196568b08bSDan Williams 	const struct dax_operations *ops;
2207b6be844SDan Williams };
2217b6be844SDan Williams 
2226e0c90d6SDan Williams static ssize_t write_cache_show(struct device *dev,
2236e0c90d6SDan Williams 		struct device_attribute *attr, char *buf)
2246e0c90d6SDan Williams {
2256e0c90d6SDan Williams 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
2266e0c90d6SDan Williams 	ssize_t rc;
2276e0c90d6SDan Williams 
2286e0c90d6SDan Williams 	WARN_ON_ONCE(!dax_dev);
2296e0c90d6SDan Williams 	if (!dax_dev)
2306e0c90d6SDan Williams 		return -ENXIO;
2316e0c90d6SDan Williams 
232808c340bSRoss Zwisler 	rc = sprintf(buf, "%d\n", !!dax_write_cache_enabled(dax_dev));
2336e0c90d6SDan Williams 	put_dax(dax_dev);
2346e0c90d6SDan Williams 	return rc;
2356e0c90d6SDan Williams }
2366e0c90d6SDan Williams 
2376e0c90d6SDan Williams static ssize_t write_cache_store(struct device *dev,
2386e0c90d6SDan Williams 		struct device_attribute *attr, const char *buf, size_t len)
2396e0c90d6SDan Williams {
2406e0c90d6SDan Williams 	bool write_cache;
2416e0c90d6SDan Williams 	int rc = strtobool(buf, &write_cache);
2426e0c90d6SDan Williams 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
2436e0c90d6SDan Williams 
2446e0c90d6SDan Williams 	WARN_ON_ONCE(!dax_dev);
2456e0c90d6SDan Williams 	if (!dax_dev)
2466e0c90d6SDan Williams 		return -ENXIO;
2476e0c90d6SDan Williams 
2486e0c90d6SDan Williams 	if (rc)
2496e0c90d6SDan Williams 		len = rc;
2506e0c90d6SDan Williams 	else
251808c340bSRoss Zwisler 		dax_write_cache(dax_dev, write_cache);
2526e0c90d6SDan Williams 
2536e0c90d6SDan Williams 	put_dax(dax_dev);
2546e0c90d6SDan Williams 	return len;
2556e0c90d6SDan Williams }
2566e0c90d6SDan Williams static DEVICE_ATTR_RW(write_cache);
2576e0c90d6SDan Williams 
2586e0c90d6SDan Williams static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
2596e0c90d6SDan Williams {
2606e0c90d6SDan Williams 	struct device *dev = container_of(kobj, typeof(*dev), kobj);
2616e0c90d6SDan Williams 	struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
2626e0c90d6SDan Williams 
2636e0c90d6SDan Williams 	WARN_ON_ONCE(!dax_dev);
2646e0c90d6SDan Williams 	if (!dax_dev)
2656e0c90d6SDan Williams 		return 0;
2666e0c90d6SDan Williams 
267c3ca015fSMikulas Patocka #ifndef CONFIG_ARCH_HAS_PMEM_API
268c3ca015fSMikulas Patocka 	if (a == &dev_attr_write_cache.attr)
2696e0c90d6SDan Williams 		return 0;
270c3ca015fSMikulas Patocka #endif
2716e0c90d6SDan Williams 	return a->mode;
2726e0c90d6SDan Williams }
2736e0c90d6SDan Williams 
2746e0c90d6SDan Williams static struct attribute *dax_attributes[] = {
2756e0c90d6SDan Williams 	&dev_attr_write_cache.attr,
2766e0c90d6SDan Williams 	NULL,
2776e0c90d6SDan Williams };
2786e0c90d6SDan Williams 
2796e0c90d6SDan Williams struct attribute_group dax_attribute_group = {
2806e0c90d6SDan Williams 	.name = "dax",
2816e0c90d6SDan Williams 	.attrs = dax_attributes,
2826e0c90d6SDan Williams 	.is_visible = dax_visible,
2836e0c90d6SDan Williams };
2846e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_attribute_group);
2856e0c90d6SDan Williams 
286b0686260SDan Williams /**
287b0686260SDan Williams  * dax_direct_access() - translate a device pgoff to an absolute pfn
288b0686260SDan Williams  * @dax_dev: a dax_device instance representing the logical memory range
289b0686260SDan Williams  * @pgoff: offset in pages from the start of the device to translate
290b0686260SDan Williams  * @nr_pages: number of consecutive pages caller can handle relative to @pfn
291b0686260SDan Williams  * @kaddr: output parameter that returns a virtual address mapping of pfn
292b0686260SDan Williams  * @pfn: output parameter that returns an absolute pfn translation of @pgoff
293b0686260SDan Williams  *
294b0686260SDan Williams  * Return: negative errno if an error occurs, otherwise the number of
295b0686260SDan Williams  * pages accessible at the device relative @pgoff.
296b0686260SDan Williams  */
297b0686260SDan Williams long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
298b0686260SDan Williams 		void **kaddr, pfn_t *pfn)
299b0686260SDan Williams {
300b0686260SDan Williams 	long avail;
301b0686260SDan Williams 
302b0686260SDan Williams 	if (!dax_dev)
303b0686260SDan Williams 		return -EOPNOTSUPP;
304b0686260SDan Williams 
305b0686260SDan Williams 	if (!dax_alive(dax_dev))
306b0686260SDan Williams 		return -ENXIO;
307b0686260SDan Williams 
308b0686260SDan Williams 	if (nr_pages < 0)
309b0686260SDan Williams 		return nr_pages;
310b0686260SDan Williams 
311b0686260SDan Williams 	avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
312b0686260SDan Williams 			kaddr, pfn);
313b0686260SDan Williams 	if (!avail)
314b0686260SDan Williams 		return -ERANGE;
315b0686260SDan Williams 	return min(avail, nr_pages);
316b0686260SDan Williams }
317b0686260SDan Williams EXPORT_SYMBOL_GPL(dax_direct_access);
318b0686260SDan Williams 
3197bf7eac8SDan Williams bool dax_supported(struct dax_device *dax_dev, struct block_device *bdev,
3207bf7eac8SDan Williams 		int blocksize, sector_t start, sector_t len)
3217bf7eac8SDan Williams {
3227bf7eac8SDan Williams 	if (!dax_alive(dax_dev))
3237bf7eac8SDan Williams 		return false;
3247bf7eac8SDan Williams 
3257bf7eac8SDan Williams 	return dax_dev->ops->dax_supported(dax_dev, bdev, blocksize, start, len);
3267bf7eac8SDan Williams }
3277bf7eac8SDan Williams 
3287e026c8cSDan Williams size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
3297e026c8cSDan Williams 		size_t bytes, struct iov_iter *i)
3307e026c8cSDan Williams {
3317e026c8cSDan Williams 	if (!dax_alive(dax_dev))
3327e026c8cSDan Williams 		return 0;
3337e026c8cSDan Williams 
3347e026c8cSDan Williams 	return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
3357e026c8cSDan Williams }
3367e026c8cSDan Williams EXPORT_SYMBOL_GPL(dax_copy_from_iter);
3377e026c8cSDan Williams 
338b3a9a0c3SDan Williams size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
339b3a9a0c3SDan Williams 		size_t bytes, struct iov_iter *i)
340b3a9a0c3SDan Williams {
341b3a9a0c3SDan Williams 	if (!dax_alive(dax_dev))
342b3a9a0c3SDan Williams 		return 0;
343b3a9a0c3SDan Williams 
344b3a9a0c3SDan Williams 	return dax_dev->ops->copy_to_iter(dax_dev, pgoff, addr, bytes, i);
345b3a9a0c3SDan Williams }
346b3a9a0c3SDan Williams EXPORT_SYMBOL_GPL(dax_copy_to_iter);
347b3a9a0c3SDan Williams 
348f605a263SVivek Goyal int dax_zero_page_range(struct dax_device *dax_dev, pgoff_t pgoff,
349f605a263SVivek Goyal 			size_t nr_pages)
350f605a263SVivek Goyal {
351f605a263SVivek Goyal 	if (!dax_alive(dax_dev))
352f605a263SVivek Goyal 		return -ENXIO;
353f605a263SVivek Goyal 	/*
354f605a263SVivek Goyal 	 * There are no callers that want to zero more than one page as of now.
355f605a263SVivek Goyal 	 * Once users are there, this check can be removed after the
356f605a263SVivek Goyal 	 * device mapper code has been updated to split ranges across targets.
357f605a263SVivek Goyal 	 */
358f605a263SVivek Goyal 	if (nr_pages != 1)
359f605a263SVivek Goyal 		return -EIO;
360f605a263SVivek Goyal 
361f605a263SVivek Goyal 	return dax_dev->ops->zero_page_range(dax_dev, pgoff, nr_pages);
362f605a263SVivek Goyal }
363f605a263SVivek Goyal EXPORT_SYMBOL_GPL(dax_zero_page_range);
364f605a263SVivek Goyal 
365c3ca015fSMikulas Patocka #ifdef CONFIG_ARCH_HAS_PMEM_API
366c3ca015fSMikulas Patocka void arch_wb_cache_pmem(void *addr, size_t size);
367c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
368abebfbe2SDan Williams {
369808c340bSRoss Zwisler 	if (unlikely(!dax_write_cache_enabled(dax_dev)))
3706e0c90d6SDan Williams 		return;
3716e0c90d6SDan Williams 
372c3ca015fSMikulas Patocka 	arch_wb_cache_pmem(addr, size);
373abebfbe2SDan Williams }
374c3ca015fSMikulas Patocka #else
375c3ca015fSMikulas Patocka void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
376c3ca015fSMikulas Patocka {
377c3ca015fSMikulas Patocka }
378c3ca015fSMikulas Patocka #endif
379abebfbe2SDan Williams EXPORT_SYMBOL_GPL(dax_flush);
380abebfbe2SDan Williams 
3816e0c90d6SDan Williams void dax_write_cache(struct dax_device *dax_dev, bool wc)
3826e0c90d6SDan Williams {
3836e0c90d6SDan Williams 	if (wc)
3846e0c90d6SDan Williams 		set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
3856e0c90d6SDan Williams 	else
3866e0c90d6SDan Williams 		clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
3876e0c90d6SDan Williams }
3886e0c90d6SDan Williams EXPORT_SYMBOL_GPL(dax_write_cache);
3896e0c90d6SDan Williams 
390273752c9SVivek Goyal bool dax_write_cache_enabled(struct dax_device *dax_dev)
391273752c9SVivek Goyal {
392273752c9SVivek Goyal 	return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
393273752c9SVivek Goyal }
394273752c9SVivek Goyal EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
395273752c9SVivek Goyal 
396fefc1d97SPankaj Gupta bool __dax_synchronous(struct dax_device *dax_dev)
397fefc1d97SPankaj Gupta {
398fefc1d97SPankaj Gupta 	return test_bit(DAXDEV_SYNC, &dax_dev->flags);
399fefc1d97SPankaj Gupta }
400fefc1d97SPankaj Gupta EXPORT_SYMBOL_GPL(__dax_synchronous);
401fefc1d97SPankaj Gupta 
402fefc1d97SPankaj Gupta void __set_dax_synchronous(struct dax_device *dax_dev)
403fefc1d97SPankaj Gupta {
404fefc1d97SPankaj Gupta 	set_bit(DAXDEV_SYNC, &dax_dev->flags);
405fefc1d97SPankaj Gupta }
406fefc1d97SPankaj Gupta EXPORT_SYMBOL_GPL(__set_dax_synchronous);
407fefc1d97SPankaj Gupta 
4087b6be844SDan Williams bool dax_alive(struct dax_device *dax_dev)
4097b6be844SDan Williams {
4107b6be844SDan Williams 	lockdep_assert_held(&dax_srcu);
4119a60c3efSDan Williams 	return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
4127b6be844SDan Williams }
4137b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_alive);
4147b6be844SDan Williams 
41572058005SDan Williams static int dax_host_hash(const char *host)
41672058005SDan Williams {
41772058005SDan Williams 	return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
41872058005SDan Williams }
41972058005SDan Williams 
4207b6be844SDan Williams /*
4217b6be844SDan Williams  * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
4227b6be844SDan Williams  * that any fault handlers or operations that might have seen
4237b6be844SDan Williams  * dax_alive(), have completed.  Any operations that start after
4247b6be844SDan Williams  * synchronize_srcu() has run will abort upon seeing !dax_alive().
4257b6be844SDan Williams  */
4267b6be844SDan Williams void kill_dax(struct dax_device *dax_dev)
4277b6be844SDan Williams {
4287b6be844SDan Williams 	if (!dax_dev)
4297b6be844SDan Williams 		return;
4307b6be844SDan Williams 
4319a60c3efSDan Williams 	clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
43272058005SDan Williams 
4337b6be844SDan Williams 	synchronize_srcu(&dax_srcu);
43472058005SDan Williams 
43572058005SDan Williams 	spin_lock(&dax_host_lock);
43672058005SDan Williams 	hlist_del_init(&dax_dev->list);
43772058005SDan Williams 	spin_unlock(&dax_host_lock);
4387b6be844SDan Williams }
4397b6be844SDan Williams EXPORT_SYMBOL_GPL(kill_dax);
4407b6be844SDan Williams 
4419567da0bSDan Williams void run_dax(struct dax_device *dax_dev)
4429567da0bSDan Williams {
4439567da0bSDan Williams 	set_bit(DAXDEV_ALIVE, &dax_dev->flags);
4449567da0bSDan Williams }
4459567da0bSDan Williams EXPORT_SYMBOL_GPL(run_dax);
4469567da0bSDan Williams 
4477b6be844SDan Williams static struct inode *dax_alloc_inode(struct super_block *sb)
4487b6be844SDan Williams {
4497b6be844SDan Williams 	struct dax_device *dax_dev;
450b9d39d17SDan Williams 	struct inode *inode;
4517b6be844SDan Williams 
4527b6be844SDan Williams 	dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
4539f586fffSMikulas Patocka 	if (!dax_dev)
4549f586fffSMikulas Patocka 		return NULL;
4559f586fffSMikulas Patocka 
456b9d39d17SDan Williams 	inode = &dax_dev->inode;
457b9d39d17SDan Williams 	inode->i_rdev = 0;
458b9d39d17SDan Williams 	return inode;
4597b6be844SDan Williams }
4607b6be844SDan Williams 
4617b6be844SDan Williams static struct dax_device *to_dax_dev(struct inode *inode)
4627b6be844SDan Williams {
4637b6be844SDan Williams 	return container_of(inode, struct dax_device, inode);
4647b6be844SDan Williams }
4657b6be844SDan Williams 
46653e22829SAl Viro static void dax_free_inode(struct inode *inode)
4677b6be844SDan Williams {
4687b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
46972058005SDan Williams 	kfree(dax_dev->host);
47072058005SDan Williams 	dax_dev->host = NULL;
471b9d39d17SDan Williams 	if (inode->i_rdev)
4727b6be844SDan Williams 		ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
4737b6be844SDan Williams 	kmem_cache_free(dax_cache, dax_dev);
4747b6be844SDan Williams }
4757b6be844SDan Williams 
4767b6be844SDan Williams static void dax_destroy_inode(struct inode *inode)
4777b6be844SDan Williams {
4787b6be844SDan Williams 	struct dax_device *dax_dev = to_dax_dev(inode);
4799a60c3efSDan Williams 	WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
4807b6be844SDan Williams 			"kill_dax() must be called before final iput()\n");
4817b6be844SDan Williams }
4827b6be844SDan Williams 
4837b6be844SDan Williams static const struct super_operations dax_sops = {
4847b6be844SDan Williams 	.statfs = simple_statfs,
4857b6be844SDan Williams 	.alloc_inode = dax_alloc_inode,
4867b6be844SDan Williams 	.destroy_inode = dax_destroy_inode,
48753e22829SAl Viro 	.free_inode = dax_free_inode,
4887b6be844SDan Williams 	.drop_inode = generic_delete_inode,
4897b6be844SDan Williams };
4907b6be844SDan Williams 
49175d4e06fSDavid Howells static int dax_init_fs_context(struct fs_context *fc)
4927b6be844SDan Williams {
49375d4e06fSDavid Howells 	struct pseudo_fs_context *ctx = init_pseudo(fc, DAXFS_MAGIC);
49475d4e06fSDavid Howells 	if (!ctx)
49575d4e06fSDavid Howells 		return -ENOMEM;
49675d4e06fSDavid Howells 	ctx->ops = &dax_sops;
49775d4e06fSDavid Howells 	return 0;
4987b6be844SDan Williams }
4997b6be844SDan Williams 
5007b6be844SDan Williams static struct file_system_type dax_fs_type = {
5017b6be844SDan Williams 	.name		= "dax",
50275d4e06fSDavid Howells 	.init_fs_context = dax_init_fs_context,
5037b6be844SDan Williams 	.kill_sb	= kill_anon_super,
5047b6be844SDan Williams };
5057b6be844SDan Williams 
5067b6be844SDan Williams static int dax_test(struct inode *inode, void *data)
5077b6be844SDan Williams {
5087b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
5097b6be844SDan Williams 
5107b6be844SDan Williams 	return inode->i_rdev == devt;
5117b6be844SDan Williams }
5127b6be844SDan Williams 
5137b6be844SDan Williams static int dax_set(struct inode *inode, void *data)
5147b6be844SDan Williams {
5157b6be844SDan Williams 	dev_t devt = *(dev_t *) data;
5167b6be844SDan Williams 
5177b6be844SDan Williams 	inode->i_rdev = devt;
5187b6be844SDan Williams 	return 0;
5197b6be844SDan Williams }
5207b6be844SDan Williams 
5217b6be844SDan Williams static struct dax_device *dax_dev_get(dev_t devt)
5227b6be844SDan Williams {
5237b6be844SDan Williams 	struct dax_device *dax_dev;
5247b6be844SDan Williams 	struct inode *inode;
5257b6be844SDan Williams 
5267b6be844SDan Williams 	inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
5277b6be844SDan Williams 			dax_test, dax_set, &devt);
5287b6be844SDan Williams 
5297b6be844SDan Williams 	if (!inode)
5307b6be844SDan Williams 		return NULL;
5317b6be844SDan Williams 
5327b6be844SDan Williams 	dax_dev = to_dax_dev(inode);
5337b6be844SDan Williams 	if (inode->i_state & I_NEW) {
5349a60c3efSDan Williams 		set_bit(DAXDEV_ALIVE, &dax_dev->flags);
5357b6be844SDan Williams 		inode->i_cdev = &dax_dev->cdev;
5367b6be844SDan Williams 		inode->i_mode = S_IFCHR;
5377b6be844SDan Williams 		inode->i_flags = S_DAX;
5387b6be844SDan Williams 		mapping_set_gfp_mask(&inode->i_data, GFP_USER);
5397b6be844SDan Williams 		unlock_new_inode(inode);
5407b6be844SDan Williams 	}
5417b6be844SDan Williams 
5427b6be844SDan Williams 	return dax_dev;
5437b6be844SDan Williams }
5447b6be844SDan Williams 
54572058005SDan Williams static void dax_add_host(struct dax_device *dax_dev, const char *host)
54672058005SDan Williams {
54772058005SDan Williams 	int hash;
54872058005SDan Williams 
54972058005SDan Williams 	/*
55072058005SDan Williams 	 * Unconditionally init dax_dev since it's coming from a
55172058005SDan Williams 	 * non-zeroed slab cache
55272058005SDan Williams 	 */
55372058005SDan Williams 	INIT_HLIST_NODE(&dax_dev->list);
55472058005SDan Williams 	dax_dev->host = host;
55572058005SDan Williams 	if (!host)
55672058005SDan Williams 		return;
55772058005SDan Williams 
55872058005SDan Williams 	hash = dax_host_hash(host);
55972058005SDan Williams 	spin_lock(&dax_host_lock);
56072058005SDan Williams 	hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
56172058005SDan Williams 	spin_unlock(&dax_host_lock);
56272058005SDan Williams }
56372058005SDan Williams 
5646568b08bSDan Williams struct dax_device *alloc_dax(void *private, const char *__host,
565fefc1d97SPankaj Gupta 		const struct dax_operations *ops, unsigned long flags)
5667b6be844SDan Williams {
5677b6be844SDan Williams 	struct dax_device *dax_dev;
56872058005SDan Williams 	const char *host;
5697b6be844SDan Williams 	dev_t devt;
5707b6be844SDan Williams 	int minor;
5717b6be844SDan Williams 
5724e4ced93SVivek Goyal 	if (ops && !ops->zero_page_range) {
5734e4ced93SVivek Goyal 		pr_debug("%s: error: device does not provide dax"
5744e4ced93SVivek Goyal 			 " operation zero_page_range()\n",
5754e4ced93SVivek Goyal 			 __host ? __host : "Unknown");
5764e4ced93SVivek Goyal 		return ERR_PTR(-EINVAL);
5774e4ced93SVivek Goyal 	}
5784e4ced93SVivek Goyal 
57972058005SDan Williams 	host = kstrdup(__host, GFP_KERNEL);
58072058005SDan Williams 	if (__host && !host)
5814e4ced93SVivek Goyal 		return ERR_PTR(-ENOMEM);
58272058005SDan Williams 
583cf1e2289SDan Williams 	minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
5847b6be844SDan Williams 	if (minor < 0)
58572058005SDan Williams 		goto err_minor;
5867b6be844SDan Williams 
5877b6be844SDan Williams 	devt = MKDEV(MAJOR(dax_devt), minor);
5887b6be844SDan Williams 	dax_dev = dax_dev_get(devt);
5897b6be844SDan Williams 	if (!dax_dev)
59072058005SDan Williams 		goto err_dev;
5917b6be844SDan Williams 
59272058005SDan Williams 	dax_add_host(dax_dev, host);
5936568b08bSDan Williams 	dax_dev->ops = ops;
5947b6be844SDan Williams 	dax_dev->private = private;
595fefc1d97SPankaj Gupta 	if (flags & DAXDEV_F_SYNC)
596fefc1d97SPankaj Gupta 		set_dax_synchronous(dax_dev);
597fefc1d97SPankaj Gupta 
5987b6be844SDan Williams 	return dax_dev;
5997b6be844SDan Williams 
60072058005SDan Williams  err_dev:
6017b6be844SDan Williams 	ida_simple_remove(&dax_minor_ida, minor);
60272058005SDan Williams  err_minor:
60372058005SDan Williams 	kfree(host);
6044e4ced93SVivek Goyal 	return ERR_PTR(-ENOMEM);
6057b6be844SDan Williams }
6067b6be844SDan Williams EXPORT_SYMBOL_GPL(alloc_dax);
6077b6be844SDan Williams 
6087b6be844SDan Williams void put_dax(struct dax_device *dax_dev)
6097b6be844SDan Williams {
6107b6be844SDan Williams 	if (!dax_dev)
6117b6be844SDan Williams 		return;
6127b6be844SDan Williams 	iput(&dax_dev->inode);
6137b6be844SDan Williams }
6147b6be844SDan Williams EXPORT_SYMBOL_GPL(put_dax);
6157b6be844SDan Williams 
6167b6be844SDan Williams /**
61772058005SDan Williams  * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
61872058005SDan Williams  * @host: alternate name for the device registered by a dax driver
61972058005SDan Williams  */
62072058005SDan Williams struct dax_device *dax_get_by_host(const char *host)
62172058005SDan Williams {
62272058005SDan Williams 	struct dax_device *dax_dev, *found = NULL;
62372058005SDan Williams 	int hash, id;
62472058005SDan Williams 
62572058005SDan Williams 	if (!host)
62672058005SDan Williams 		return NULL;
62772058005SDan Williams 
62872058005SDan Williams 	hash = dax_host_hash(host);
62972058005SDan Williams 
63072058005SDan Williams 	id = dax_read_lock();
63172058005SDan Williams 	spin_lock(&dax_host_lock);
63272058005SDan Williams 	hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
63372058005SDan Williams 		if (!dax_alive(dax_dev)
63472058005SDan Williams 				|| strcmp(host, dax_dev->host) != 0)
63572058005SDan Williams 			continue;
63672058005SDan Williams 
63772058005SDan Williams 		if (igrab(&dax_dev->inode))
63872058005SDan Williams 			found = dax_dev;
63972058005SDan Williams 		break;
64072058005SDan Williams 	}
64172058005SDan Williams 	spin_unlock(&dax_host_lock);
64272058005SDan Williams 	dax_read_unlock(id);
64372058005SDan Williams 
64472058005SDan Williams 	return found;
64572058005SDan Williams }
64672058005SDan Williams EXPORT_SYMBOL_GPL(dax_get_by_host);
64772058005SDan Williams 
64872058005SDan Williams /**
6497b6be844SDan Williams  * inode_dax: convert a public inode into its dax_dev
6507b6be844SDan Williams  * @inode: An inode with i_cdev pointing to a dax_dev
6517b6be844SDan Williams  *
6527b6be844SDan Williams  * Note this is not equivalent to to_dax_dev() which is for private
6537b6be844SDan Williams  * internal use where we know the inode filesystem type == dax_fs_type.
6547b6be844SDan Williams  */
6557b6be844SDan Williams struct dax_device *inode_dax(struct inode *inode)
6567b6be844SDan Williams {
6577b6be844SDan Williams 	struct cdev *cdev = inode->i_cdev;
6587b6be844SDan Williams 
6597b6be844SDan Williams 	return container_of(cdev, struct dax_device, cdev);
6607b6be844SDan Williams }
6617b6be844SDan Williams EXPORT_SYMBOL_GPL(inode_dax);
6627b6be844SDan Williams 
6637b6be844SDan Williams struct inode *dax_inode(struct dax_device *dax_dev)
6647b6be844SDan Williams {
6657b6be844SDan Williams 	return &dax_dev->inode;
6667b6be844SDan Williams }
6677b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_inode);
6687b6be844SDan Williams 
6697b6be844SDan Williams void *dax_get_private(struct dax_device *dax_dev)
6707b6be844SDan Williams {
6719567da0bSDan Williams 	if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
6729567da0bSDan Williams 		return NULL;
6737b6be844SDan Williams 	return dax_dev->private;
6747b6be844SDan Williams }
6757b6be844SDan Williams EXPORT_SYMBOL_GPL(dax_get_private);
6767b6be844SDan Williams 
6777b6be844SDan Williams static void init_once(void *_dax_dev)
6787b6be844SDan Williams {
6797b6be844SDan Williams 	struct dax_device *dax_dev = _dax_dev;
6807b6be844SDan Williams 	struct inode *inode = &dax_dev->inode;
6817b6be844SDan Williams 
682b9d39d17SDan Williams 	memset(dax_dev, 0, sizeof(*dax_dev));
6837b6be844SDan Williams 	inode_init_once(inode);
6847b6be844SDan Williams }
6857b6be844SDan Williams 
6869567da0bSDan Williams static int dax_fs_init(void)
6877b6be844SDan Williams {
6887b6be844SDan Williams 	int rc;
6897b6be844SDan Williams 
6907b6be844SDan Williams 	dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
6917b6be844SDan Williams 			(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
6927b6be844SDan Williams 			 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
6937b6be844SDan Williams 			init_once);
6947b6be844SDan Williams 	if (!dax_cache)
6957b6be844SDan Williams 		return -ENOMEM;
6967b6be844SDan Williams 
6977b6be844SDan Williams 	dax_mnt = kern_mount(&dax_fs_type);
6987b6be844SDan Williams 	if (IS_ERR(dax_mnt)) {
6997b6be844SDan Williams 		rc = PTR_ERR(dax_mnt);
7007b6be844SDan Williams 		goto err_mount;
7017b6be844SDan Williams 	}
7027b6be844SDan Williams 	dax_superblock = dax_mnt->mnt_sb;
7037b6be844SDan Williams 
7047b6be844SDan Williams 	return 0;
7057b6be844SDan Williams 
7067b6be844SDan Williams  err_mount:
7077b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
7087b6be844SDan Williams 
7097b6be844SDan Williams 	return rc;
7107b6be844SDan Williams }
7117b6be844SDan Williams 
7129567da0bSDan Williams static void dax_fs_exit(void)
7137b6be844SDan Williams {
7147b6be844SDan Williams 	kern_unmount(dax_mnt);
7157b6be844SDan Williams 	kmem_cache_destroy(dax_cache);
7167b6be844SDan Williams }
7177b6be844SDan Williams 
7189567da0bSDan Williams static int __init dax_core_init(void)
7197b6be844SDan Williams {
7207b6be844SDan Williams 	int rc;
7217b6be844SDan Williams 
7229567da0bSDan Williams 	rc = dax_fs_init();
7237b6be844SDan Williams 	if (rc)
7247b6be844SDan Williams 		return rc;
7257b6be844SDan Williams 
726cf1e2289SDan Williams 	rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
7277b6be844SDan Williams 	if (rc)
7289567da0bSDan Williams 		goto err_chrdev;
7299567da0bSDan Williams 
7309567da0bSDan Williams 	rc = dax_bus_init();
7319567da0bSDan Williams 	if (rc)
7329567da0bSDan Williams 		goto err_bus;
7339567da0bSDan Williams 	return 0;
7349567da0bSDan Williams 
7359567da0bSDan Williams err_bus:
7369567da0bSDan Williams 	unregister_chrdev_region(dax_devt, MINORMASK+1);
7379567da0bSDan Williams err_chrdev:
7389567da0bSDan Williams 	dax_fs_exit();
7399567da0bSDan Williams 	return 0;
7407b6be844SDan Williams }
7417b6be844SDan Williams 
7429567da0bSDan Williams static void __exit dax_core_exit(void)
7437b6be844SDan Williams {
744cf1e2289SDan Williams 	unregister_chrdev_region(dax_devt, MINORMASK+1);
7457b6be844SDan Williams 	ida_destroy(&dax_minor_ida);
7469567da0bSDan Williams 	dax_fs_exit();
7477b6be844SDan Williams }
7487b6be844SDan Williams 
7497b6be844SDan Williams MODULE_AUTHOR("Intel Corporation");
7507b6be844SDan Williams MODULE_LICENSE("GPL v2");
7519567da0bSDan Williams subsys_initcall(dax_core_init);
7529567da0bSDan Williams module_exit(dax_core_exit);
753