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/dax.h>
24 static dev_t dax_devt;
25 DEFINE_STATIC_SRCU(dax_srcu);
26 static struct vfsmount *dax_mnt;
27 static DEFINE_IDA(dax_minor_ida);
28 static struct kmem_cache *dax_cache __read_mostly;
29 static struct super_block *dax_superblock __read_mostly;
31 #define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
32 static struct hlist_head dax_host_list[DAX_HASH_SIZE];
33 static DEFINE_SPINLOCK(dax_host_lock);
35 int dax_read_lock(void)
37 return srcu_read_lock(&dax_srcu);
39 EXPORT_SYMBOL_GPL(dax_read_lock);
41 void dax_read_unlock(int id)
43 srcu_read_unlock(&dax_srcu, id);
45 EXPORT_SYMBOL_GPL(dax_read_unlock);
47 int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
50 phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
53 *pgoff = PHYS_PFN(phys_off);
54 if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
58 EXPORT_SYMBOL(bdev_dax_pgoff);
61 * __bdev_dax_supported() - Check if the device supports dax for filesystem
62 * @sb: The superblock of the device
63 * @blocksize: The block size of the device
65 * This is a library function for filesystems to check if the block device
66 * can be mounted with dax option.
68 * Return: negative errno if unsupported, 0 if supported.
70 int __bdev_dax_supported(struct super_block *sb, int blocksize)
72 struct block_device *bdev = sb->s_bdev;
73 struct dax_device *dax_dev;
80 if (blocksize != PAGE_SIZE) {
81 pr_err("VFS (%s): error: unsupported blocksize for dax\n",
86 err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
88 pr_err("VFS (%s): error: unaligned partition for dax\n",
93 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
95 pr_err("VFS (%s): error: device does not support dax\n",
100 id = dax_read_lock();
101 len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
107 pr_err("VFS (%s): error: dax access failed (%ld)",
109 return len < 0 ? len : -EIO;
114 EXPORT_SYMBOL_GPL(__bdev_dax_supported);
117 * struct dax_device - anchor object for dax services
119 * @cdev: optional character interface for "device dax"
120 * @host: optional name for lookups where the device path is not available
121 * @private: dax driver private data
122 * @alive: !alive + rcu grace period == no new operations / mappings
125 struct hlist_node list;
131 const struct dax_operations *ops;
135 * dax_direct_access() - translate a device pgoff to an absolute pfn
136 * @dax_dev: a dax_device instance representing the logical memory range
137 * @pgoff: offset in pages from the start of the device to translate
138 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
139 * @kaddr: output parameter that returns a virtual address mapping of pfn
140 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
142 * Return: negative errno if an error occurs, otherwise the number of
143 * pages accessible at the device relative @pgoff.
145 long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
146 void **kaddr, pfn_t *pfn)
151 * The device driver is allowed to sleep, in order to make the
152 * memory directly accessible.
159 if (!dax_alive(dax_dev))
165 avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
169 return min(avail, nr_pages);
171 EXPORT_SYMBOL_GPL(dax_direct_access);
173 bool dax_alive(struct dax_device *dax_dev)
175 lockdep_assert_held(&dax_srcu);
176 return dax_dev->alive;
178 EXPORT_SYMBOL_GPL(dax_alive);
180 static int dax_host_hash(const char *host)
182 return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
186 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
187 * that any fault handlers or operations that might have seen
188 * dax_alive(), have completed. Any operations that start after
189 * synchronize_srcu() has run will abort upon seeing !dax_alive().
191 void kill_dax(struct dax_device *dax_dev)
196 dax_dev->alive = false;
198 synchronize_srcu(&dax_srcu);
200 spin_lock(&dax_host_lock);
201 hlist_del_init(&dax_dev->list);
202 spin_unlock(&dax_host_lock);
204 dax_dev->private = NULL;
206 EXPORT_SYMBOL_GPL(kill_dax);
208 static struct inode *dax_alloc_inode(struct super_block *sb)
210 struct dax_device *dax_dev;
212 dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
213 return &dax_dev->inode;
216 static struct dax_device *to_dax_dev(struct inode *inode)
218 return container_of(inode, struct dax_device, inode);
221 static void dax_i_callback(struct rcu_head *head)
223 struct inode *inode = container_of(head, struct inode, i_rcu);
224 struct dax_device *dax_dev = to_dax_dev(inode);
226 kfree(dax_dev->host);
227 dax_dev->host = NULL;
228 ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
229 kmem_cache_free(dax_cache, dax_dev);
232 static void dax_destroy_inode(struct inode *inode)
234 struct dax_device *dax_dev = to_dax_dev(inode);
236 WARN_ONCE(dax_dev->alive,
237 "kill_dax() must be called before final iput()\n");
238 call_rcu(&inode->i_rcu, dax_i_callback);
241 static const struct super_operations dax_sops = {
242 .statfs = simple_statfs,
243 .alloc_inode = dax_alloc_inode,
244 .destroy_inode = dax_destroy_inode,
245 .drop_inode = generic_delete_inode,
248 static struct dentry *dax_mount(struct file_system_type *fs_type,
249 int flags, const char *dev_name, void *data)
251 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
254 static struct file_system_type dax_fs_type = {
257 .kill_sb = kill_anon_super,
260 static int dax_test(struct inode *inode, void *data)
262 dev_t devt = *(dev_t *) data;
264 return inode->i_rdev == devt;
267 static int dax_set(struct inode *inode, void *data)
269 dev_t devt = *(dev_t *) data;
271 inode->i_rdev = devt;
275 static struct dax_device *dax_dev_get(dev_t devt)
277 struct dax_device *dax_dev;
280 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
281 dax_test, dax_set, &devt);
286 dax_dev = to_dax_dev(inode);
287 if (inode->i_state & I_NEW) {
288 dax_dev->alive = true;
289 inode->i_cdev = &dax_dev->cdev;
290 inode->i_mode = S_IFCHR;
291 inode->i_flags = S_DAX;
292 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
293 unlock_new_inode(inode);
299 static void dax_add_host(struct dax_device *dax_dev, const char *host)
304 * Unconditionally init dax_dev since it's coming from a
305 * non-zeroed slab cache
307 INIT_HLIST_NODE(&dax_dev->list);
308 dax_dev->host = host;
312 hash = dax_host_hash(host);
313 spin_lock(&dax_host_lock);
314 hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
315 spin_unlock(&dax_host_lock);
318 struct dax_device *alloc_dax(void *private, const char *__host,
319 const struct dax_operations *ops)
321 struct dax_device *dax_dev;
326 host = kstrdup(__host, GFP_KERNEL);
330 minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
334 devt = MKDEV(MAJOR(dax_devt), minor);
335 dax_dev = dax_dev_get(devt);
339 dax_add_host(dax_dev, host);
341 dax_dev->private = private;
345 ida_simple_remove(&dax_minor_ida, minor);
350 EXPORT_SYMBOL_GPL(alloc_dax);
352 void put_dax(struct dax_device *dax_dev)
356 iput(&dax_dev->inode);
358 EXPORT_SYMBOL_GPL(put_dax);
361 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
362 * @host: alternate name for the device registered by a dax driver
364 struct dax_device *dax_get_by_host(const char *host)
366 struct dax_device *dax_dev, *found = NULL;
372 hash = dax_host_hash(host);
374 id = dax_read_lock();
375 spin_lock(&dax_host_lock);
376 hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
377 if (!dax_alive(dax_dev)
378 || strcmp(host, dax_dev->host) != 0)
381 if (igrab(&dax_dev->inode))
385 spin_unlock(&dax_host_lock);
390 EXPORT_SYMBOL_GPL(dax_get_by_host);
393 * inode_dax: convert a public inode into its dax_dev
394 * @inode: An inode with i_cdev pointing to a dax_dev
396 * Note this is not equivalent to to_dax_dev() which is for private
397 * internal use where we know the inode filesystem type == dax_fs_type.
399 struct dax_device *inode_dax(struct inode *inode)
401 struct cdev *cdev = inode->i_cdev;
403 return container_of(cdev, struct dax_device, cdev);
405 EXPORT_SYMBOL_GPL(inode_dax);
407 struct inode *dax_inode(struct dax_device *dax_dev)
409 return &dax_dev->inode;
411 EXPORT_SYMBOL_GPL(dax_inode);
413 void *dax_get_private(struct dax_device *dax_dev)
415 return dax_dev->private;
417 EXPORT_SYMBOL_GPL(dax_get_private);
419 static void init_once(void *_dax_dev)
421 struct dax_device *dax_dev = _dax_dev;
422 struct inode *inode = &dax_dev->inode;
424 inode_init_once(inode);
427 static int __dax_fs_init(void)
431 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
432 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
433 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
438 rc = register_filesystem(&dax_fs_type);
440 goto err_register_fs;
442 dax_mnt = kern_mount(&dax_fs_type);
443 if (IS_ERR(dax_mnt)) {
444 rc = PTR_ERR(dax_mnt);
447 dax_superblock = dax_mnt->mnt_sb;
452 unregister_filesystem(&dax_fs_type);
454 kmem_cache_destroy(dax_cache);
459 static void __dax_fs_exit(void)
461 kern_unmount(dax_mnt);
462 unregister_filesystem(&dax_fs_type);
463 kmem_cache_destroy(dax_cache);
466 static int __init dax_fs_init(void)
470 rc = __dax_fs_init();
474 rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
480 static void __exit dax_fs_exit(void)
482 unregister_chrdev_region(dax_devt, MINORMASK+1);
483 ida_destroy(&dax_minor_ida);
487 MODULE_AUTHOR("Intel Corporation");
488 MODULE_LICENSE("GPL v2");
489 subsys_initcall(dax_fs_init);
490 module_exit(dax_fs_exit);