2017-04-12 00:49:49 +08:00
|
|
|
/*
|
|
|
|
* Copyright(c) 2017 Intel Corporation. All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of version 2 of the GNU General Public License as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*/
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mount.h>
|
|
|
|
#include <linux/magic.h>
|
|
|
|
#include <linux/cdev.h>
|
|
|
|
#include <linux/hash.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
|
|
|
|
static int nr_dax = CONFIG_NR_DEV_DAX;
|
|
|
|
module_param(nr_dax, int, S_IRUGO);
|
|
|
|
MODULE_PARM_DESC(nr_dax, "max number of dax device instances");
|
|
|
|
|
|
|
|
static dev_t dax_devt;
|
|
|
|
DEFINE_STATIC_SRCU(dax_srcu);
|
|
|
|
static struct vfsmount *dax_mnt;
|
|
|
|
static DEFINE_IDA(dax_minor_ida);
|
|
|
|
static struct kmem_cache *dax_cache __read_mostly;
|
|
|
|
static struct super_block *dax_superblock __read_mostly;
|
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
#define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
|
|
|
|
static struct hlist_head dax_host_list[DAX_HASH_SIZE];
|
|
|
|
static DEFINE_SPINLOCK(dax_host_lock);
|
|
|
|
|
2017-04-12 00:49:49 +08:00
|
|
|
int dax_read_lock(void)
|
|
|
|
{
|
|
|
|
return srcu_read_lock(&dax_srcu);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dax_read_lock);
|
|
|
|
|
|
|
|
void dax_read_unlock(int id)
|
|
|
|
{
|
|
|
|
srcu_read_unlock(&dax_srcu, id);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dax_read_unlock);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* struct dax_device - anchor object for dax services
|
|
|
|
* @inode: core vfs
|
|
|
|
* @cdev: optional character interface for "device dax"
|
2017-04-20 06:14:31 +08:00
|
|
|
* @host: optional name for lookups where the device path is not available
|
2017-04-12 00:49:49 +08:00
|
|
|
* @private: dax driver private data
|
|
|
|
* @alive: !alive + rcu grace period == no new operations / mappings
|
|
|
|
*/
|
|
|
|
struct dax_device {
|
2017-04-20 06:14:31 +08:00
|
|
|
struct hlist_node list;
|
2017-04-12 00:49:49 +08:00
|
|
|
struct inode inode;
|
|
|
|
struct cdev cdev;
|
2017-04-20 06:14:31 +08:00
|
|
|
const char *host;
|
2017-04-12 00:49:49 +08:00
|
|
|
void *private;
|
|
|
|
bool alive;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool dax_alive(struct dax_device *dax_dev)
|
|
|
|
{
|
|
|
|
lockdep_assert_held(&dax_srcu);
|
|
|
|
return dax_dev->alive;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dax_alive);
|
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
static int dax_host_hash(const char *host)
|
|
|
|
{
|
|
|
|
return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
|
|
|
|
}
|
|
|
|
|
2017-04-12 00:49:49 +08:00
|
|
|
/*
|
|
|
|
* Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
|
|
|
|
* that any fault handlers or operations that might have seen
|
|
|
|
* dax_alive(), have completed. Any operations that start after
|
|
|
|
* synchronize_srcu() has run will abort upon seeing !dax_alive().
|
|
|
|
*/
|
|
|
|
void kill_dax(struct dax_device *dax_dev)
|
|
|
|
{
|
|
|
|
if (!dax_dev)
|
|
|
|
return;
|
|
|
|
|
|
|
|
dax_dev->alive = false;
|
2017-04-20 06:14:31 +08:00
|
|
|
|
2017-04-12 00:49:49 +08:00
|
|
|
synchronize_srcu(&dax_srcu);
|
2017-04-20 06:14:31 +08:00
|
|
|
|
|
|
|
spin_lock(&dax_host_lock);
|
|
|
|
hlist_del_init(&dax_dev->list);
|
|
|
|
spin_unlock(&dax_host_lock);
|
|
|
|
|
2017-04-12 00:49:49 +08:00
|
|
|
dax_dev->private = NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(kill_dax);
|
|
|
|
|
|
|
|
static struct inode *dax_alloc_inode(struct super_block *sb)
|
|
|
|
{
|
|
|
|
struct dax_device *dax_dev;
|
|
|
|
|
|
|
|
dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
|
|
|
|
return &dax_dev->inode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dax_device *to_dax_dev(struct inode *inode)
|
|
|
|
{
|
|
|
|
return container_of(inode, struct dax_device, inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dax_i_callback(struct rcu_head *head)
|
|
|
|
{
|
|
|
|
struct inode *inode = container_of(head, struct inode, i_rcu);
|
|
|
|
struct dax_device *dax_dev = to_dax_dev(inode);
|
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
kfree(dax_dev->host);
|
|
|
|
dax_dev->host = NULL;
|
2017-04-12 00:49:49 +08:00
|
|
|
ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
|
|
|
|
kmem_cache_free(dax_cache, dax_dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dax_destroy_inode(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct dax_device *dax_dev = to_dax_dev(inode);
|
|
|
|
|
|
|
|
WARN_ONCE(dax_dev->alive,
|
|
|
|
"kill_dax() must be called before final iput()\n");
|
|
|
|
call_rcu(&inode->i_rcu, dax_i_callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct super_operations dax_sops = {
|
|
|
|
.statfs = simple_statfs,
|
|
|
|
.alloc_inode = dax_alloc_inode,
|
|
|
|
.destroy_inode = dax_destroy_inode,
|
|
|
|
.drop_inode = generic_delete_inode,
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct dentry *dax_mount(struct file_system_type *fs_type,
|
|
|
|
int flags, const char *dev_name, void *data)
|
|
|
|
{
|
|
|
|
return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct file_system_type dax_fs_type = {
|
|
|
|
.name = "dax",
|
|
|
|
.mount = dax_mount,
|
|
|
|
.kill_sb = kill_anon_super,
|
|
|
|
};
|
|
|
|
|
|
|
|
static int dax_test(struct inode *inode, void *data)
|
|
|
|
{
|
|
|
|
dev_t devt = *(dev_t *) data;
|
|
|
|
|
|
|
|
return inode->i_rdev == devt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int dax_set(struct inode *inode, void *data)
|
|
|
|
{
|
|
|
|
dev_t devt = *(dev_t *) data;
|
|
|
|
|
|
|
|
inode->i_rdev = devt;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct dax_device *dax_dev_get(dev_t devt)
|
|
|
|
{
|
|
|
|
struct dax_device *dax_dev;
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
|
|
|
|
dax_test, dax_set, &devt);
|
|
|
|
|
|
|
|
if (!inode)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
dax_dev = to_dax_dev(inode);
|
|
|
|
if (inode->i_state & I_NEW) {
|
|
|
|
dax_dev->alive = true;
|
|
|
|
inode->i_cdev = &dax_dev->cdev;
|
|
|
|
inode->i_mode = S_IFCHR;
|
|
|
|
inode->i_flags = S_DAX;
|
|
|
|
mapping_set_gfp_mask(&inode->i_data, GFP_USER);
|
|
|
|
unlock_new_inode(inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dax_dev;
|
|
|
|
}
|
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
static void dax_add_host(struct dax_device *dax_dev, const char *host)
|
|
|
|
{
|
|
|
|
int hash;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unconditionally init dax_dev since it's coming from a
|
|
|
|
* non-zeroed slab cache
|
|
|
|
*/
|
|
|
|
INIT_HLIST_NODE(&dax_dev->list);
|
|
|
|
dax_dev->host = host;
|
|
|
|
if (!host)
|
|
|
|
return;
|
|
|
|
|
|
|
|
hash = dax_host_hash(host);
|
|
|
|
spin_lock(&dax_host_lock);
|
|
|
|
hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
|
|
|
|
spin_unlock(&dax_host_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct dax_device *alloc_dax(void *private, const char *__host)
|
2017-04-12 00:49:49 +08:00
|
|
|
{
|
|
|
|
struct dax_device *dax_dev;
|
2017-04-20 06:14:31 +08:00
|
|
|
const char *host;
|
2017-04-12 00:49:49 +08:00
|
|
|
dev_t devt;
|
|
|
|
int minor;
|
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
host = kstrdup(__host, GFP_KERNEL);
|
|
|
|
if (__host && !host)
|
|
|
|
return NULL;
|
|
|
|
|
2017-04-12 00:49:49 +08:00
|
|
|
minor = ida_simple_get(&dax_minor_ida, 0, nr_dax, GFP_KERNEL);
|
|
|
|
if (minor < 0)
|
2017-04-20 06:14:31 +08:00
|
|
|
goto err_minor;
|
2017-04-12 00:49:49 +08:00
|
|
|
|
|
|
|
devt = MKDEV(MAJOR(dax_devt), minor);
|
|
|
|
dax_dev = dax_dev_get(devt);
|
|
|
|
if (!dax_dev)
|
2017-04-20 06:14:31 +08:00
|
|
|
goto err_dev;
|
2017-04-12 00:49:49 +08:00
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
dax_add_host(dax_dev, host);
|
2017-04-12 00:49:49 +08:00
|
|
|
dax_dev->private = private;
|
|
|
|
return dax_dev;
|
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
err_dev:
|
2017-04-12 00:49:49 +08:00
|
|
|
ida_simple_remove(&dax_minor_ida, minor);
|
2017-04-20 06:14:31 +08:00
|
|
|
err_minor:
|
|
|
|
kfree(host);
|
2017-04-12 00:49:49 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(alloc_dax);
|
|
|
|
|
|
|
|
void put_dax(struct dax_device *dax_dev)
|
|
|
|
{
|
|
|
|
if (!dax_dev)
|
|
|
|
return;
|
|
|
|
iput(&dax_dev->inode);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(put_dax);
|
|
|
|
|
2017-04-20 06:14:31 +08:00
|
|
|
/**
|
|
|
|
* dax_get_by_host() - temporary lookup mechanism for filesystem-dax
|
|
|
|
* @host: alternate name for the device registered by a dax driver
|
|
|
|
*/
|
|
|
|
struct dax_device *dax_get_by_host(const char *host)
|
|
|
|
{
|
|
|
|
struct dax_device *dax_dev, *found = NULL;
|
|
|
|
int hash, id;
|
|
|
|
|
|
|
|
if (!host)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
hash = dax_host_hash(host);
|
|
|
|
|
|
|
|
id = dax_read_lock();
|
|
|
|
spin_lock(&dax_host_lock);
|
|
|
|
hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
|
|
|
|
if (!dax_alive(dax_dev)
|
|
|
|
|| strcmp(host, dax_dev->host) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (igrab(&dax_dev->inode))
|
|
|
|
found = dax_dev;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
spin_unlock(&dax_host_lock);
|
|
|
|
dax_read_unlock(id);
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dax_get_by_host);
|
|
|
|
|
2017-04-12 00:49:49 +08:00
|
|
|
/**
|
|
|
|
* inode_dax: convert a public inode into its dax_dev
|
|
|
|
* @inode: An inode with i_cdev pointing to a dax_dev
|
|
|
|
*
|
|
|
|
* Note this is not equivalent to to_dax_dev() which is for private
|
|
|
|
* internal use where we know the inode filesystem type == dax_fs_type.
|
|
|
|
*/
|
|
|
|
struct dax_device *inode_dax(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct cdev *cdev = inode->i_cdev;
|
|
|
|
|
|
|
|
return container_of(cdev, struct dax_device, cdev);
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(inode_dax);
|
|
|
|
|
|
|
|
struct inode *dax_inode(struct dax_device *dax_dev)
|
|
|
|
{
|
|
|
|
return &dax_dev->inode;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dax_inode);
|
|
|
|
|
|
|
|
void *dax_get_private(struct dax_device *dax_dev)
|
|
|
|
{
|
|
|
|
return dax_dev->private;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(dax_get_private);
|
|
|
|
|
|
|
|
static void init_once(void *_dax_dev)
|
|
|
|
{
|
|
|
|
struct dax_device *dax_dev = _dax_dev;
|
|
|
|
struct inode *inode = &dax_dev->inode;
|
|
|
|
|
|
|
|
inode_init_once(inode);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __dax_fs_init(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
|
|
|
|
(SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
|
|
|
|
SLAB_MEM_SPREAD|SLAB_ACCOUNT),
|
|
|
|
init_once);
|
|
|
|
if (!dax_cache)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
rc = register_filesystem(&dax_fs_type);
|
|
|
|
if (rc)
|
|
|
|
goto err_register_fs;
|
|
|
|
|
|
|
|
dax_mnt = kern_mount(&dax_fs_type);
|
|
|
|
if (IS_ERR(dax_mnt)) {
|
|
|
|
rc = PTR_ERR(dax_mnt);
|
|
|
|
goto err_mount;
|
|
|
|
}
|
|
|
|
dax_superblock = dax_mnt->mnt_sb;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_mount:
|
|
|
|
unregister_filesystem(&dax_fs_type);
|
|
|
|
err_register_fs:
|
|
|
|
kmem_cache_destroy(dax_cache);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __dax_fs_exit(void)
|
|
|
|
{
|
|
|
|
kern_unmount(dax_mnt);
|
|
|
|
unregister_filesystem(&dax_fs_type);
|
|
|
|
kmem_cache_destroy(dax_cache);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int __init dax_fs_init(void)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = __dax_fs_init();
|
|
|
|
if (rc)
|
|
|
|
return rc;
|
|
|
|
|
|
|
|
nr_dax = max(nr_dax, 256);
|
|
|
|
rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
|
|
|
|
if (rc)
|
|
|
|
__dax_fs_exit();
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void __exit dax_fs_exit(void)
|
|
|
|
{
|
|
|
|
unregister_chrdev_region(dax_devt, nr_dax);
|
|
|
|
ida_destroy(&dax_minor_ida);
|
|
|
|
__dax_fs_exit();
|
|
|
|
}
|
|
|
|
|
|
|
|
MODULE_AUTHOR("Intel Corporation");
|
|
|
|
MODULE_LICENSE("GPL v2");
|
|
|
|
subsys_initcall(dax_fs_init);
|
|
|
|
module_exit(dax_fs_exit);
|