2 * Copyright(c) 2017 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 #include <linux/pagemap.h>
14 #include <linux/module.h>
15 #include <linux/mount.h>
16 #include <linux/magic.h>
17 #include <linux/genhd.h>
18 #include <linux/pfn_t.h>
19 #include <linux/cdev.h>
20 #include <linux/hash.h>
21 #include <linux/slab.h>
22 #include <linux/uio.h>
23 #include <linux/dax.h>
25 #include "dax-private.h"
27 static dev_t dax_devt;
28 DEFINE_STATIC_SRCU(dax_srcu);
29 static struct vfsmount *dax_mnt;
30 static DEFINE_IDA(dax_minor_ida);
31 static struct kmem_cache *dax_cache __read_mostly;
32 static struct super_block *dax_superblock __read_mostly;
34 #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
35 static struct hlist_head dax_host_list[DAX_HASH_SIZE];
36 static DEFINE_SPINLOCK(dax_host_lock);
38 int dax_read_lock(void)
40 return srcu_read_lock(&dax_srcu);
42 EXPORT_SYMBOL_GPL(dax_read_lock);
44 void dax_read_unlock(int id)
46 srcu_read_unlock(&dax_srcu, id);
48 EXPORT_SYMBOL_GPL(dax_read_unlock);
51 #include <linux/blkdev.h>
53 int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
56 phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
59 *pgoff = PHYS_PFN(phys_off);
60 if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
64 EXPORT_SYMBOL(bdev_dax_pgoff);
66 #if IS_ENABLED(CONFIG_FS_DAX)
67 struct dax_device *fs_dax_get_by_bdev(struct block_device *bdev)
69 if (!blk_queue_dax(bdev->bd_queue))
71 return fs_dax_get_by_host(bdev->bd_disk->disk_name);
73 EXPORT_SYMBOL_GPL(fs_dax_get_by_bdev);
77 * __bdev_dax_supported() - Check if the device supports dax for filesystem
78 * @bdev: block device to check
79 * @blocksize: The block size of the device
81 * This is a library function for filesystems to check if the block device
82 * can be mounted with dax option.
84 * Return: true if supported, false if unsupported
86 bool __bdev_dax_supported(struct block_device *bdev, int blocksize)
88 struct dax_device *dax_dev;
89 bool dax_enabled = false;
90 pgoff_t pgoff, pgoff_end;
91 struct request_queue *q;
92 char buf[BDEVNAME_SIZE];
93 void *kaddr, *end_kaddr;
99 if (blocksize != PAGE_SIZE) {
100 pr_debug("%s: error: unsupported blocksize for dax\n",
101 bdevname(bdev, buf));
105 q = bdev_get_queue(bdev);
106 if (!q || !blk_queue_dax(q)) {
107 pr_debug("%s: error: request queue doesn't support dax\n",
108 bdevname(bdev, buf));
112 err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
114 pr_debug("%s: error: unaligned partition for dax\n",
115 bdevname(bdev, buf));
119 last_page = PFN_DOWN(i_size_read(bdev->bd_inode) - 1) * 8;
120 err = bdev_dax_pgoff(bdev, last_page, PAGE_SIZE, &pgoff_end);
122 pr_debug("%s: error: unaligned partition for dax\n",
123 bdevname(bdev, buf));
127 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
129 pr_debug("%s: error: device does not support dax\n",
130 bdevname(bdev, buf));
134 id = dax_read_lock();
135 len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
136 len2 = dax_direct_access(dax_dev, pgoff_end, 1, &end_kaddr, &end_pfn);
141 if (len < 1 || len2 < 1) {
142 pr_debug("%s: error: dax access failed (%ld)\n",
143 bdevname(bdev, buf), len < 1 ? len : len2);
147 if (IS_ENABLED(CONFIG_FS_DAX_LIMITED) && pfn_t_special(pfn)) {
149 * An arch that has enabled the pmem api should also
150 * have its drivers support pfn_t_devmap()
152 * This is a developer warning and should not trigger in
153 * production. dax_flush() will crash since it depends
154 * on being able to do (page_address(pfn_to_page())).
156 WARN_ON(IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API));
158 } else if (pfn_t_devmap(pfn) && pfn_t_devmap(end_pfn)) {
159 struct dev_pagemap *pgmap, *end_pgmap;
161 pgmap = get_dev_pagemap(pfn_t_to_pfn(pfn), NULL);
162 end_pgmap = get_dev_pagemap(pfn_t_to_pfn(end_pfn), NULL);
163 if (pgmap && pgmap == end_pgmap && pgmap->type == MEMORY_DEVICE_FS_DAX
164 && pfn_t_to_page(pfn)->pgmap == pgmap
165 && pfn_t_to_page(end_pfn)->pgmap == pgmap
166 && pfn_t_to_pfn(pfn) == PHYS_PFN(__pa(kaddr))
167 && pfn_t_to_pfn(end_pfn) == PHYS_PFN(__pa(end_kaddr)))
169 put_dev_pagemap(pgmap);
170 put_dev_pagemap(end_pgmap);
175 pr_debug("%s: error: dax support not enabled\n",
176 bdevname(bdev, buf));
181 EXPORT_SYMBOL_GPL(__bdev_dax_supported);
184 enum dax_device_flags {
185 /* !alive + rcu grace period == no new operations / mappings */
187 /* gate whether dax_flush() calls the low level flush routine */
192 * struct dax_device - anchor object for dax services
194 * @cdev: optional character interface for "device dax"
195 * @host: optional name for lookups where the device path is not available
196 * @private: dax driver private data
197 * @flags: state and boolean properties
200 struct hlist_node list;
206 const struct dax_operations *ops;
209 static ssize_t write_cache_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
215 WARN_ON_ONCE(!dax_dev);
219 rc = sprintf(buf, "%d\n", !!dax_write_cache_enabled(dax_dev));
224 static ssize_t write_cache_store(struct device *dev,
225 struct device_attribute *attr, const char *buf, size_t len)
228 int rc = strtobool(buf, &write_cache);
229 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
231 WARN_ON_ONCE(!dax_dev);
238 dax_write_cache(dax_dev, write_cache);
243 static DEVICE_ATTR_RW(write_cache);
245 static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
247 struct device *dev = container_of(kobj, typeof(*dev), kobj);
248 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
250 WARN_ON_ONCE(!dax_dev);
254 #ifndef CONFIG_ARCH_HAS_PMEM_API
255 if (a == &dev_attr_write_cache.attr)
261 static struct attribute *dax_attributes[] = {
262 &dev_attr_write_cache.attr,
266 struct attribute_group dax_attribute_group = {
268 .attrs = dax_attributes,
269 .is_visible = dax_visible,
271 EXPORT_SYMBOL_GPL(dax_attribute_group);
274 * dax_direct_access() - translate a device pgoff to an absolute pfn
275 * @dax_dev: a dax_device instance representing the logical memory range
276 * @pgoff: offset in pages from the start of the device to translate
277 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
278 * @kaddr: output parameter that returns a virtual address mapping of pfn
279 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
281 * Return: negative errno if an error occurs, otherwise the number of
282 * pages accessible at the device relative @pgoff.
284 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
285 void **kaddr, pfn_t *pfn)
292 if (!dax_alive(dax_dev))
298 avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
302 return min(avail, nr_pages);
304 EXPORT_SYMBOL_GPL(dax_direct_access);
306 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
307 size_t bytes, struct iov_iter *i)
309 if (!dax_alive(dax_dev))
312 return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
314 EXPORT_SYMBOL_GPL(dax_copy_from_iter);
316 size_t dax_copy_to_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
317 size_t bytes, struct iov_iter *i)
319 if (!dax_alive(dax_dev))
322 return dax_dev->ops->copy_to_iter(dax_dev, pgoff, addr, bytes, i);
324 EXPORT_SYMBOL_GPL(dax_copy_to_iter);
326 #ifdef CONFIG_ARCH_HAS_PMEM_API
327 void arch_wb_cache_pmem(void *addr, size_t size);
328 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
330 if (unlikely(!dax_write_cache_enabled(dax_dev)))
333 arch_wb_cache_pmem(addr, size);
336 void dax_flush(struct dax_device *dax_dev, void *addr, size_t size)
340 EXPORT_SYMBOL_GPL(dax_flush);
342 void dax_write_cache(struct dax_device *dax_dev, bool wc)
345 set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
347 clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
349 EXPORT_SYMBOL_GPL(dax_write_cache);
351 bool dax_write_cache_enabled(struct dax_device *dax_dev)
353 return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
355 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
357 bool dax_alive(struct dax_device *dax_dev)
359 lockdep_assert_held(&dax_srcu);
360 return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
362 EXPORT_SYMBOL_GPL(dax_alive);
364 static int dax_host_hash(const char *host)
366 return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
370 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
371 * that any fault handlers or operations that might have seen
372 * dax_alive(), have completed. Any operations that start after
373 * synchronize_srcu() has run will abort upon seeing !dax_alive().
375 void kill_dax(struct dax_device *dax_dev)
380 clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
382 synchronize_srcu(&dax_srcu);
384 spin_lock(&dax_host_lock);
385 hlist_del_init(&dax_dev->list);
386 spin_unlock(&dax_host_lock);
388 EXPORT_SYMBOL_GPL(kill_dax);
390 void run_dax(struct dax_device *dax_dev)
392 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
394 EXPORT_SYMBOL_GPL(run_dax);
396 static struct inode *dax_alloc_inode(struct super_block *sb)
398 struct dax_device *dax_dev;
401 dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
405 inode = &dax_dev->inode;
410 static struct dax_device *to_dax_dev(struct inode *inode)
412 return container_of(inode, struct dax_device, inode);
415 static void dax_i_callback(struct rcu_head *head)
417 struct inode *inode = container_of(head, struct inode, i_rcu);
418 struct dax_device *dax_dev = to_dax_dev(inode);
420 kfree(dax_dev->host);
421 dax_dev->host = NULL;
423 ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
424 kmem_cache_free(dax_cache, dax_dev);
427 static void dax_destroy_inode(struct inode *inode)
429 struct dax_device *dax_dev = to_dax_dev(inode);
431 WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
432 "kill_dax() must be called before final iput()\n");
433 call_rcu(&inode->i_rcu, dax_i_callback);
436 static const struct super_operations dax_sops = {
437 .statfs = simple_statfs,
438 .alloc_inode = dax_alloc_inode,
439 .destroy_inode = dax_destroy_inode,
440 .drop_inode = generic_delete_inode,
443 static struct dentry *dax_mount(struct file_system_type *fs_type,
444 int flags, const char *dev_name, void *data)
446 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
449 static struct file_system_type dax_fs_type = {
452 .kill_sb = kill_anon_super,
455 static int dax_test(struct inode *inode, void *data)
457 dev_t devt = *(dev_t *) data;
459 return inode->i_rdev == devt;
462 static int dax_set(struct inode *inode, void *data)
464 dev_t devt = *(dev_t *) data;
466 inode->i_rdev = devt;
470 static struct dax_device *dax_dev_get(dev_t devt)
472 struct dax_device *dax_dev;
475 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
476 dax_test, dax_set, &devt);
481 dax_dev = to_dax_dev(inode);
482 if (inode->i_state & I_NEW) {
483 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
484 inode->i_cdev = &dax_dev->cdev;
485 inode->i_mode = S_IFCHR;
486 inode->i_flags = S_DAX;
487 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
488 unlock_new_inode(inode);
494 static void dax_add_host(struct dax_device *dax_dev, const char *host)
499 * Unconditionally init dax_dev since it's coming from a
500 * non-zeroed slab cache
502 INIT_HLIST_NODE(&dax_dev->list);
503 dax_dev->host = host;
507 hash = dax_host_hash(host);
508 spin_lock(&dax_host_lock);
509 hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
510 spin_unlock(&dax_host_lock);
513 struct dax_device *alloc_dax(void *private, const char *__host,
514 const struct dax_operations *ops)
516 struct dax_device *dax_dev;
521 host = kstrdup(__host, GFP_KERNEL);
525 minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
529 devt = MKDEV(MAJOR(dax_devt), minor);
530 dax_dev = dax_dev_get(devt);
534 dax_add_host(dax_dev, host);
536 dax_dev->private = private;
540 ida_simple_remove(&dax_minor_ida, minor);
545 EXPORT_SYMBOL_GPL(alloc_dax);
547 void put_dax(struct dax_device *dax_dev)
551 iput(&dax_dev->inode);
553 EXPORT_SYMBOL_GPL(put_dax);
556 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
557 * @host: alternate name for the device registered by a dax driver
559 struct dax_device *dax_get_by_host(const char *host)
561 struct dax_device *dax_dev, *found = NULL;
567 hash = dax_host_hash(host);
569 id = dax_read_lock();
570 spin_lock(&dax_host_lock);
571 hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
572 if (!dax_alive(dax_dev)
573 || strcmp(host, dax_dev->host) != 0)
576 if (igrab(&dax_dev->inode))
580 spin_unlock(&dax_host_lock);
585 EXPORT_SYMBOL_GPL(dax_get_by_host);
588 * inode_dax: convert a public inode into its dax_dev
589 * @inode: An inode with i_cdev pointing to a dax_dev
591 * Note this is not equivalent to to_dax_dev() which is for private
592 * internal use where we know the inode filesystem type == dax_fs_type.
594 struct dax_device *inode_dax(struct inode *inode)
596 struct cdev *cdev = inode->i_cdev;
598 return container_of(cdev, struct dax_device, cdev);
600 EXPORT_SYMBOL_GPL(inode_dax);
602 struct inode *dax_inode(struct dax_device *dax_dev)
604 return &dax_dev->inode;
606 EXPORT_SYMBOL_GPL(dax_inode);
608 void *dax_get_private(struct dax_device *dax_dev)
610 if (!test_bit(DAXDEV_ALIVE, &dax_dev->flags))
612 return dax_dev->private;
614 EXPORT_SYMBOL_GPL(dax_get_private);
616 static void init_once(void *_dax_dev)
618 struct dax_device *dax_dev = _dax_dev;
619 struct inode *inode = &dax_dev->inode;
621 memset(dax_dev, 0, sizeof(*dax_dev));
622 inode_init_once(inode);
625 static int dax_fs_init(void)
629 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
630 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
631 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
636 rc = register_filesystem(&dax_fs_type);
638 goto err_register_fs;
640 dax_mnt = kern_mount(&dax_fs_type);
641 if (IS_ERR(dax_mnt)) {
642 rc = PTR_ERR(dax_mnt);
645 dax_superblock = dax_mnt->mnt_sb;
650 unregister_filesystem(&dax_fs_type);
652 kmem_cache_destroy(dax_cache);
657 static void dax_fs_exit(void)
659 kern_unmount(dax_mnt);
660 unregister_filesystem(&dax_fs_type);
661 kmem_cache_destroy(dax_cache);
664 static int __init dax_core_init(void)
672 rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
682 unregister_chrdev_region(dax_devt, MINORMASK+1);
688 static void __exit dax_core_exit(void)
690 unregister_chrdev_region(dax_devt, MINORMASK+1);
691 ida_destroy(&dax_minor_ida);
695 MODULE_AUTHOR("Intel Corporation");
696 MODULE_LICENSE("GPL v2");
697 subsys_initcall(dax_core_init);
698 module_exit(dax_core_exit);