blob: c9f85f1c086eb6ff4fe1c38e67507a2606a22704 [file] [log] [blame]
Dan Williams7b6be842017-04-11 09:49:49 -07001/*
2 * Copyright(c) 2017 Intel Corporation. All rights reserved.
3 *
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.
7 *
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.
12 */
13#include <linux/pagemap.h>
14#include <linux/module.h>
15#include <linux/mount.h>
16#include <linux/magic.h>
17#include <linux/cdev.h>
18#include <linux/hash.h>
19#include <linux/slab.h>
20#include <linux/fs.h>
21
22static int nr_dax = CONFIG_NR_DEV_DAX;
23module_param(nr_dax, int, S_IRUGO);
24MODULE_PARM_DESC(nr_dax, "max number of dax device instances");
25
26static dev_t dax_devt;
27DEFINE_STATIC_SRCU(dax_srcu);
28static struct vfsmount *dax_mnt;
29static DEFINE_IDA(dax_minor_ida);
30static struct kmem_cache *dax_cache __read_mostly;
31static struct super_block *dax_superblock __read_mostly;
32
33int dax_read_lock(void)
34{
35 return srcu_read_lock(&dax_srcu);
36}
37EXPORT_SYMBOL_GPL(dax_read_lock);
38
39void dax_read_unlock(int id)
40{
41 srcu_read_unlock(&dax_srcu, id);
42}
43EXPORT_SYMBOL_GPL(dax_read_unlock);
44
45/**
46 * struct dax_device - anchor object for dax services
47 * @inode: core vfs
48 * @cdev: optional character interface for "device dax"
49 * @private: dax driver private data
50 * @alive: !alive + rcu grace period == no new operations / mappings
51 */
52struct dax_device {
53 struct inode inode;
54 struct cdev cdev;
55 void *private;
56 bool alive;
57};
58
59bool dax_alive(struct dax_device *dax_dev)
60{
61 lockdep_assert_held(&dax_srcu);
62 return dax_dev->alive;
63}
64EXPORT_SYMBOL_GPL(dax_alive);
65
66/*
67 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
68 * that any fault handlers or operations that might have seen
69 * dax_alive(), have completed. Any operations that start after
70 * synchronize_srcu() has run will abort upon seeing !dax_alive().
71 */
72void kill_dax(struct dax_device *dax_dev)
73{
74 if (!dax_dev)
75 return;
76
77 dax_dev->alive = false;
78 synchronize_srcu(&dax_srcu);
79 dax_dev->private = NULL;
80}
81EXPORT_SYMBOL_GPL(kill_dax);
82
83static struct inode *dax_alloc_inode(struct super_block *sb)
84{
85 struct dax_device *dax_dev;
86
87 dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
88 return &dax_dev->inode;
89}
90
91static struct dax_device *to_dax_dev(struct inode *inode)
92{
93 return container_of(inode, struct dax_device, inode);
94}
95
96static void dax_i_callback(struct rcu_head *head)
97{
98 struct inode *inode = container_of(head, struct inode, i_rcu);
99 struct dax_device *dax_dev = to_dax_dev(inode);
100
101 ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
102 kmem_cache_free(dax_cache, dax_dev);
103}
104
105static void dax_destroy_inode(struct inode *inode)
106{
107 struct dax_device *dax_dev = to_dax_dev(inode);
108
109 WARN_ONCE(dax_dev->alive,
110 "kill_dax() must be called before final iput()\n");
111 call_rcu(&inode->i_rcu, dax_i_callback);
112}
113
114static const struct super_operations dax_sops = {
115 .statfs = simple_statfs,
116 .alloc_inode = dax_alloc_inode,
117 .destroy_inode = dax_destroy_inode,
118 .drop_inode = generic_delete_inode,
119};
120
121static struct dentry *dax_mount(struct file_system_type *fs_type,
122 int flags, const char *dev_name, void *data)
123{
124 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
125}
126
127static struct file_system_type dax_fs_type = {
128 .name = "dax",
129 .mount = dax_mount,
130 .kill_sb = kill_anon_super,
131};
132
133static int dax_test(struct inode *inode, void *data)
134{
135 dev_t devt = *(dev_t *) data;
136
137 return inode->i_rdev == devt;
138}
139
140static int dax_set(struct inode *inode, void *data)
141{
142 dev_t devt = *(dev_t *) data;
143
144 inode->i_rdev = devt;
145 return 0;
146}
147
148static struct dax_device *dax_dev_get(dev_t devt)
149{
150 struct dax_device *dax_dev;
151 struct inode *inode;
152
153 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
154 dax_test, dax_set, &devt);
155
156 if (!inode)
157 return NULL;
158
159 dax_dev = to_dax_dev(inode);
160 if (inode->i_state & I_NEW) {
161 dax_dev->alive = true;
162 inode->i_cdev = &dax_dev->cdev;
163 inode->i_mode = S_IFCHR;
164 inode->i_flags = S_DAX;
165 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
166 unlock_new_inode(inode);
167 }
168
169 return dax_dev;
170}
171
172struct dax_device *alloc_dax(void *private)
173{
174 struct dax_device *dax_dev;
175 dev_t devt;
176 int minor;
177
178 minor = ida_simple_get(&dax_minor_ida, 0, nr_dax, GFP_KERNEL);
179 if (minor < 0)
180 return NULL;
181
182 devt = MKDEV(MAJOR(dax_devt), minor);
183 dax_dev = dax_dev_get(devt);
184 if (!dax_dev)
185 goto err_inode;
186
187 dax_dev->private = private;
188 return dax_dev;
189
190 err_inode:
191 ida_simple_remove(&dax_minor_ida, minor);
192 return NULL;
193}
194EXPORT_SYMBOL_GPL(alloc_dax);
195
196void put_dax(struct dax_device *dax_dev)
197{
198 if (!dax_dev)
199 return;
200 iput(&dax_dev->inode);
201}
202EXPORT_SYMBOL_GPL(put_dax);
203
204/**
205 * inode_dax: convert a public inode into its dax_dev
206 * @inode: An inode with i_cdev pointing to a dax_dev
207 *
208 * Note this is not equivalent to to_dax_dev() which is for private
209 * internal use where we know the inode filesystem type == dax_fs_type.
210 */
211struct dax_device *inode_dax(struct inode *inode)
212{
213 struct cdev *cdev = inode->i_cdev;
214
215 return container_of(cdev, struct dax_device, cdev);
216}
217EXPORT_SYMBOL_GPL(inode_dax);
218
219struct inode *dax_inode(struct dax_device *dax_dev)
220{
221 return &dax_dev->inode;
222}
223EXPORT_SYMBOL_GPL(dax_inode);
224
225void *dax_get_private(struct dax_device *dax_dev)
226{
227 return dax_dev->private;
228}
229EXPORT_SYMBOL_GPL(dax_get_private);
230
231static void init_once(void *_dax_dev)
232{
233 struct dax_device *dax_dev = _dax_dev;
234 struct inode *inode = &dax_dev->inode;
235
236 inode_init_once(inode);
237}
238
239static int __dax_fs_init(void)
240{
241 int rc;
242
243 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
244 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
245 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
246 init_once);
247 if (!dax_cache)
248 return -ENOMEM;
249
250 rc = register_filesystem(&dax_fs_type);
251 if (rc)
252 goto err_register_fs;
253
254 dax_mnt = kern_mount(&dax_fs_type);
255 if (IS_ERR(dax_mnt)) {
256 rc = PTR_ERR(dax_mnt);
257 goto err_mount;
258 }
259 dax_superblock = dax_mnt->mnt_sb;
260
261 return 0;
262
263 err_mount:
264 unregister_filesystem(&dax_fs_type);
265 err_register_fs:
266 kmem_cache_destroy(dax_cache);
267
268 return rc;
269}
270
271static void __dax_fs_exit(void)
272{
273 kern_unmount(dax_mnt);
274 unregister_filesystem(&dax_fs_type);
275 kmem_cache_destroy(dax_cache);
276}
277
278static int __init dax_fs_init(void)
279{
280 int rc;
281
282 rc = __dax_fs_init();
283 if (rc)
284 return rc;
285
286 nr_dax = max(nr_dax, 256);
287 rc = alloc_chrdev_region(&dax_devt, 0, nr_dax, "dax");
288 if (rc)
289 __dax_fs_exit();
290 return rc;
291}
292
293static void __exit dax_fs_exit(void)
294{
295 unregister_chrdev_region(dax_devt, nr_dax);
296 ida_destroy(&dax_minor_ida);
297 __dax_fs_exit();
298}
299
300MODULE_AUTHOR("Intel Corporation");
301MODULE_LICENSE("GPL v2");
302subsys_initcall(dax_fs_init);
303module_exit(dax_fs_exit);