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/cdev.h>
19 #include <linux/hash.h>
20 #include <linux/slab.h>
21 #include <linux/uio.h>
22 #include <linux/dax.h>
25 static dev_t dax_devt;
26 DEFINE_STATIC_SRCU(dax_srcu);
27 static struct vfsmount *dax_mnt;
28 static DEFINE_IDA(dax_minor_ida);
29 static struct kmem_cache *dax_cache __read_mostly;
30 static struct super_block *dax_superblock __read_mostly;
32 #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
33 static struct hlist_head dax_host_list[DAX_HASH_SIZE];
34 static DEFINE_SPINLOCK(dax_host_lock);
36 int dax_read_lock(void)
38 return srcu_read_lock(&dax_srcu);
40 EXPORT_SYMBOL_GPL(dax_read_lock);
42 void dax_read_unlock(int id)
44 srcu_read_unlock(&dax_srcu, id);
46 EXPORT_SYMBOL_GPL(dax_read_unlock);
49 int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
52 phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
55 *pgoff = PHYS_PFN(phys_off);
56 if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
60 EXPORT_SYMBOL(bdev_dax_pgoff);
63 * __bdev_dax_supported() - Check if the device supports dax for filesystem
64 * @sb: The superblock of the device
65 * @blocksize: The block size of the device
67 * This is a library function for filesystems to check if the block device
68 * can be mounted with dax option.
70 * Return: negative errno if unsupported, 0 if supported.
72 int __bdev_dax_supported(struct super_block *sb, int blocksize)
74 struct block_device *bdev = sb->s_bdev;
75 struct dax_device *dax_dev;
82 if (blocksize != PAGE_SIZE) {
83 pr_err("VFS (%s): error: unsupported blocksize for dax\n",
88 err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
90 pr_err("VFS (%s): error: unaligned partition for dax\n",
95 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
97 pr_err("VFS (%s): error: device does not support dax\n",
102 id = dax_read_lock();
103 len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
109 pr_err("VFS (%s): error: dax access failed (%ld)",
111 return len < 0 ? len : -EIO;
116 EXPORT_SYMBOL_GPL(__bdev_dax_supported);
119 enum dax_device_flags {
120 /* !alive + rcu grace period == no new operations / mappings */
122 /* gate whether dax_flush() calls the low level flush routine */
127 * struct dax_device - anchor object for dax services
129 * @cdev: optional character interface for "device dax"
130 * @host: optional name for lookups where the device path is not available
131 * @private: dax driver private data
132 * @flags: state and boolean properties
135 struct hlist_node list;
141 const struct dax_operations *ops;
144 static ssize_t write_cache_show(struct device *dev,
145 struct device_attribute *attr, char *buf)
147 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
150 WARN_ON_ONCE(!dax_dev);
154 rc = sprintf(buf, "%d\n", !!test_bit(DAXDEV_WRITE_CACHE,
160 static ssize_t write_cache_store(struct device *dev,
161 struct device_attribute *attr, const char *buf, size_t len)
164 int rc = strtobool(buf, &write_cache);
165 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
167 WARN_ON_ONCE(!dax_dev);
173 else if (write_cache)
174 set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
176 clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
181 static DEVICE_ATTR_RW(write_cache);
183 static umode_t dax_visible(struct kobject *kobj, struct attribute *a, int n)
185 struct device *dev = container_of(kobj, typeof(*dev), kobj);
186 struct dax_device *dax_dev = dax_get_by_host(dev_name(dev));
188 WARN_ON_ONCE(!dax_dev);
192 if (a == &dev_attr_write_cache.attr && !dax_dev->ops->flush)
197 static struct attribute *dax_attributes[] = {
198 &dev_attr_write_cache.attr,
202 struct attribute_group dax_attribute_group = {
204 .attrs = dax_attributes,
205 .is_visible = dax_visible,
207 EXPORT_SYMBOL_GPL(dax_attribute_group);
210 * dax_direct_access() - translate a device pgoff to an absolute pfn
211 * @dax_dev: a dax_device instance representing the logical memory range
212 * @pgoff: offset in pages from the start of the device to translate
213 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
214 * @kaddr: output parameter that returns a virtual address mapping of pfn
215 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
217 * Return: negative errno if an error occurs, otherwise the number of
218 * pages accessible at the device relative @pgoff.
220 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
221 void **kaddr, pfn_t *pfn)
226 * The device driver is allowed to sleep, in order to make the
227 * memory directly accessible.
234 if (!dax_alive(dax_dev))
240 avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
244 return min(avail, nr_pages);
246 EXPORT_SYMBOL_GPL(dax_direct_access);
248 size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
249 size_t bytes, struct iov_iter *i)
251 if (!dax_alive(dax_dev))
254 return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
256 EXPORT_SYMBOL_GPL(dax_copy_from_iter);
258 void dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
261 if (!dax_alive(dax_dev))
264 if (!test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags))
267 if (dax_dev->ops->flush)
268 dax_dev->ops->flush(dax_dev, pgoff, addr, size);
270 EXPORT_SYMBOL_GPL(dax_flush);
272 void dax_write_cache(struct dax_device *dax_dev, bool wc)
275 set_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
277 clear_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
279 EXPORT_SYMBOL_GPL(dax_write_cache);
281 bool dax_write_cache_enabled(struct dax_device *dax_dev)
283 return test_bit(DAXDEV_WRITE_CACHE, &dax_dev->flags);
285 EXPORT_SYMBOL_GPL(dax_write_cache_enabled);
287 bool dax_alive(struct dax_device *dax_dev)
289 lockdep_assert_held(&dax_srcu);
290 return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
292 EXPORT_SYMBOL_GPL(dax_alive);
294 static int dax_host_hash(const char *host)
296 return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
300 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
301 * that any fault handlers or operations that might have seen
302 * dax_alive(), have completed. Any operations that start after
303 * synchronize_srcu() has run will abort upon seeing !dax_alive().
305 void kill_dax(struct dax_device *dax_dev)
310 clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
312 synchronize_srcu(&dax_srcu);
314 spin_lock(&dax_host_lock);
315 hlist_del_init(&dax_dev->list);
316 spin_unlock(&dax_host_lock);
318 dax_dev->private = NULL;
320 EXPORT_SYMBOL_GPL(kill_dax);
322 static struct inode *dax_alloc_inode(struct super_block *sb)
324 struct dax_device *dax_dev;
327 dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
328 inode = &dax_dev->inode;
333 static struct dax_device *to_dax_dev(struct inode *inode)
335 return container_of(inode, struct dax_device, inode);
338 static void dax_i_callback(struct rcu_head *head)
340 struct inode *inode = container_of(head, struct inode, i_rcu);
341 struct dax_device *dax_dev = to_dax_dev(inode);
343 kfree(dax_dev->host);
344 dax_dev->host = NULL;
346 ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
347 kmem_cache_free(dax_cache, dax_dev);
350 static void dax_destroy_inode(struct inode *inode)
352 struct dax_device *dax_dev = to_dax_dev(inode);
354 WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
355 "kill_dax() must be called before final iput()\n");
356 call_rcu(&inode->i_rcu, dax_i_callback);
359 static const struct super_operations dax_sops = {
360 .statfs = simple_statfs,
361 .alloc_inode = dax_alloc_inode,
362 .destroy_inode = dax_destroy_inode,
363 .drop_inode = generic_delete_inode,
366 static struct dentry *dax_mount(struct file_system_type *fs_type,
367 int flags, const char *dev_name, void *data)
369 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
372 static struct file_system_type dax_fs_type = {
375 .kill_sb = kill_anon_super,
378 static int dax_test(struct inode *inode, void *data)
380 dev_t devt = *(dev_t *) data;
382 return inode->i_rdev == devt;
385 static int dax_set(struct inode *inode, void *data)
387 dev_t devt = *(dev_t *) data;
389 inode->i_rdev = devt;
393 static struct dax_device *dax_dev_get(dev_t devt)
395 struct dax_device *dax_dev;
398 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
399 dax_test, dax_set, &devt);
404 dax_dev = to_dax_dev(inode);
405 if (inode->i_state & I_NEW) {
406 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
407 inode->i_cdev = &dax_dev->cdev;
408 inode->i_mode = S_IFCHR;
409 inode->i_flags = S_DAX;
410 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
411 unlock_new_inode(inode);
417 static void dax_add_host(struct dax_device *dax_dev, const char *host)
422 * Unconditionally init dax_dev since it's coming from a
423 * non-zeroed slab cache
425 INIT_HLIST_NODE(&dax_dev->list);
426 dax_dev->host = host;
430 hash = dax_host_hash(host);
431 spin_lock(&dax_host_lock);
432 hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
433 spin_unlock(&dax_host_lock);
436 struct dax_device *alloc_dax(void *private, const char *__host,
437 const struct dax_operations *ops)
439 struct dax_device *dax_dev;
444 host = kstrdup(__host, GFP_KERNEL);
448 minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
452 devt = MKDEV(MAJOR(dax_devt), minor);
453 dax_dev = dax_dev_get(devt);
457 dax_add_host(dax_dev, host);
459 dax_dev->private = private;
463 ida_simple_remove(&dax_minor_ida, minor);
468 EXPORT_SYMBOL_GPL(alloc_dax);
470 void put_dax(struct dax_device *dax_dev)
474 iput(&dax_dev->inode);
476 EXPORT_SYMBOL_GPL(put_dax);
479 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
480 * @host: alternate name for the device registered by a dax driver
482 struct dax_device *dax_get_by_host(const char *host)
484 struct dax_device *dax_dev, *found = NULL;
490 hash = dax_host_hash(host);
492 id = dax_read_lock();
493 spin_lock(&dax_host_lock);
494 hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
495 if (!dax_alive(dax_dev)
496 || strcmp(host, dax_dev->host) != 0)
499 if (igrab(&dax_dev->inode))
503 spin_unlock(&dax_host_lock);
508 EXPORT_SYMBOL_GPL(dax_get_by_host);
511 * inode_dax: convert a public inode into its dax_dev
512 * @inode: An inode with i_cdev pointing to a dax_dev
514 * Note this is not equivalent to to_dax_dev() which is for private
515 * internal use where we know the inode filesystem type == dax_fs_type.
517 struct dax_device *inode_dax(struct inode *inode)
519 struct cdev *cdev = inode->i_cdev;
521 return container_of(cdev, struct dax_device, cdev);
523 EXPORT_SYMBOL_GPL(inode_dax);
525 struct inode *dax_inode(struct dax_device *dax_dev)
527 return &dax_dev->inode;
529 EXPORT_SYMBOL_GPL(dax_inode);
531 void *dax_get_private(struct dax_device *dax_dev)
533 return dax_dev->private;
535 EXPORT_SYMBOL_GPL(dax_get_private);
537 static void init_once(void *_dax_dev)
539 struct dax_device *dax_dev = _dax_dev;
540 struct inode *inode = &dax_dev->inode;
542 memset(dax_dev, 0, sizeof(*dax_dev));
543 inode_init_once(inode);
546 static int __dax_fs_init(void)
550 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
551 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
552 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
557 rc = register_filesystem(&dax_fs_type);
559 goto err_register_fs;
561 dax_mnt = kern_mount(&dax_fs_type);
562 if (IS_ERR(dax_mnt)) {
563 rc = PTR_ERR(dax_mnt);
566 dax_superblock = dax_mnt->mnt_sb;
571 unregister_filesystem(&dax_fs_type);
573 kmem_cache_destroy(dax_cache);
578 static void __dax_fs_exit(void)
580 kern_unmount(dax_mnt);
581 unregister_filesystem(&dax_fs_type);
582 kmem_cache_destroy(dax_cache);
585 static int __init dax_fs_init(void)
589 rc = __dax_fs_init();
593 rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
599 static void __exit dax_fs_exit(void)
601 unregister_chrdev_region(dax_devt, MINORMASK+1);
602 ida_destroy(&dax_minor_ida);
606 MODULE_AUTHOR("Intel Corporation");
607 MODULE_LICENSE("GPL v2");
608 subsys_initcall(dax_fs_init);
609 module_exit(dax_fs_exit);