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/cdev.h>
18 #include <linux/hash.h>
19 #include <linux/slab.h>
20 #include <linux/dax.h>
23 static int nr_dax
= CONFIG_NR_DEV_DAX
;
24 module_param(nr_dax
, int, S_IRUGO
);
25 MODULE_PARM_DESC(nr_dax
, "max number of dax device instances");
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 * struct dax_device - anchor object for dax services
53 * @cdev: optional character interface for "device dax"
54 * @host: optional name for lookups where the device path is not available
55 * @private: dax driver private data
56 * @alive: !alive + rcu grace period == no new operations / mappings
59 struct hlist_node list
;
65 const struct dax_operations
*ops
;
69 * dax_direct_access() - translate a device pgoff to an absolute pfn
70 * @dax_dev: a dax_device instance representing the logical memory range
71 * @pgoff: offset in pages from the start of the device to translate
72 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
73 * @kaddr: output parameter that returns a virtual address mapping of pfn
74 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
76 * Return: negative errno if an error occurs, otherwise the number of
77 * pages accessible at the device relative @pgoff.
79 long dax_direct_access(struct dax_device
*dax_dev
, pgoff_t pgoff
, long nr_pages
,
80 void **kaddr
, pfn_t
*pfn
)
85 * The device driver is allowed to sleep, in order to make the
86 * memory directly accessible.
93 if (!dax_alive(dax_dev
))
99 avail
= dax_dev
->ops
->direct_access(dax_dev
, pgoff
, nr_pages
,
103 return min(avail
, nr_pages
);
105 EXPORT_SYMBOL_GPL(dax_direct_access
);
107 bool dax_alive(struct dax_device
*dax_dev
)
109 lockdep_assert_held(&dax_srcu
);
110 return dax_dev
->alive
;
112 EXPORT_SYMBOL_GPL(dax_alive
);
114 static int dax_host_hash(const char *host
)
116 return hashlen_hash(hashlen_string("DAX", host
)) % DAX_HASH_SIZE
;
120 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
121 * that any fault handlers or operations that might have seen
122 * dax_alive(), have completed. Any operations that start after
123 * synchronize_srcu() has run will abort upon seeing !dax_alive().
125 void kill_dax(struct dax_device
*dax_dev
)
130 dax_dev
->alive
= false;
132 synchronize_srcu(&dax_srcu
);
134 spin_lock(&dax_host_lock
);
135 hlist_del_init(&dax_dev
->list
);
136 spin_unlock(&dax_host_lock
);
138 dax_dev
->private = NULL
;
140 EXPORT_SYMBOL_GPL(kill_dax
);
142 static struct inode
*dax_alloc_inode(struct super_block
*sb
)
144 struct dax_device
*dax_dev
;
146 dax_dev
= kmem_cache_alloc(dax_cache
, GFP_KERNEL
);
147 return &dax_dev
->inode
;
150 static struct dax_device
*to_dax_dev(struct inode
*inode
)
152 return container_of(inode
, struct dax_device
, inode
);
155 static void dax_i_callback(struct rcu_head
*head
)
157 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
158 struct dax_device
*dax_dev
= to_dax_dev(inode
);
160 kfree(dax_dev
->host
);
161 dax_dev
->host
= NULL
;
162 ida_simple_remove(&dax_minor_ida
, MINOR(inode
->i_rdev
));
163 kmem_cache_free(dax_cache
, dax_dev
);
166 static void dax_destroy_inode(struct inode
*inode
)
168 struct dax_device
*dax_dev
= to_dax_dev(inode
);
170 WARN_ONCE(dax_dev
->alive
,
171 "kill_dax() must be called before final iput()\n");
172 call_rcu(&inode
->i_rcu
, dax_i_callback
);
175 static const struct super_operations dax_sops
= {
176 .statfs
= simple_statfs
,
177 .alloc_inode
= dax_alloc_inode
,
178 .destroy_inode
= dax_destroy_inode
,
179 .drop_inode
= generic_delete_inode
,
182 static struct dentry
*dax_mount(struct file_system_type
*fs_type
,
183 int flags
, const char *dev_name
, void *data
)
185 return mount_pseudo(fs_type
, "dax:", &dax_sops
, NULL
, DAXFS_MAGIC
);
188 static struct file_system_type dax_fs_type
= {
191 .kill_sb
= kill_anon_super
,
194 static int dax_test(struct inode
*inode
, void *data
)
196 dev_t devt
= *(dev_t
*) data
;
198 return inode
->i_rdev
== devt
;
201 static int dax_set(struct inode
*inode
, void *data
)
203 dev_t devt
= *(dev_t
*) data
;
205 inode
->i_rdev
= devt
;
209 static struct dax_device
*dax_dev_get(dev_t devt
)
211 struct dax_device
*dax_dev
;
214 inode
= iget5_locked(dax_superblock
, hash_32(devt
+ DAXFS_MAGIC
, 31),
215 dax_test
, dax_set
, &devt
);
220 dax_dev
= to_dax_dev(inode
);
221 if (inode
->i_state
& I_NEW
) {
222 dax_dev
->alive
= true;
223 inode
->i_cdev
= &dax_dev
->cdev
;
224 inode
->i_mode
= S_IFCHR
;
225 inode
->i_flags
= S_DAX
;
226 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
227 unlock_new_inode(inode
);
233 static void dax_add_host(struct dax_device
*dax_dev
, const char *host
)
238 * Unconditionally init dax_dev since it's coming from a
239 * non-zeroed slab cache
241 INIT_HLIST_NODE(&dax_dev
->list
);
242 dax_dev
->host
= host
;
246 hash
= dax_host_hash(host
);
247 spin_lock(&dax_host_lock
);
248 hlist_add_head(&dax_dev
->list
, &dax_host_list
[hash
]);
249 spin_unlock(&dax_host_lock
);
252 struct dax_device
*alloc_dax(void *private, const char *__host
,
253 const struct dax_operations
*ops
)
255 struct dax_device
*dax_dev
;
260 host
= kstrdup(__host
, GFP_KERNEL
);
264 minor
= ida_simple_get(&dax_minor_ida
, 0, nr_dax
, GFP_KERNEL
);
268 devt
= MKDEV(MAJOR(dax_devt
), minor
);
269 dax_dev
= dax_dev_get(devt
);
273 dax_add_host(dax_dev
, host
);
275 dax_dev
->private = private;
279 ida_simple_remove(&dax_minor_ida
, minor
);
284 EXPORT_SYMBOL_GPL(alloc_dax
);
286 void put_dax(struct dax_device
*dax_dev
)
290 iput(&dax_dev
->inode
);
292 EXPORT_SYMBOL_GPL(put_dax
);
295 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
296 * @host: alternate name for the device registered by a dax driver
298 struct dax_device
*dax_get_by_host(const char *host
)
300 struct dax_device
*dax_dev
, *found
= NULL
;
306 hash
= dax_host_hash(host
);
308 id
= dax_read_lock();
309 spin_lock(&dax_host_lock
);
310 hlist_for_each_entry(dax_dev
, &dax_host_list
[hash
], list
) {
311 if (!dax_alive(dax_dev
)
312 || strcmp(host
, dax_dev
->host
) != 0)
315 if (igrab(&dax_dev
->inode
))
319 spin_unlock(&dax_host_lock
);
324 EXPORT_SYMBOL_GPL(dax_get_by_host
);
327 * inode_dax: convert a public inode into its dax_dev
328 * @inode: An inode with i_cdev pointing to a dax_dev
330 * Note this is not equivalent to to_dax_dev() which is for private
331 * internal use where we know the inode filesystem type == dax_fs_type.
333 struct dax_device
*inode_dax(struct inode
*inode
)
335 struct cdev
*cdev
= inode
->i_cdev
;
337 return container_of(cdev
, struct dax_device
, cdev
);
339 EXPORT_SYMBOL_GPL(inode_dax
);
341 struct inode
*dax_inode(struct dax_device
*dax_dev
)
343 return &dax_dev
->inode
;
345 EXPORT_SYMBOL_GPL(dax_inode
);
347 void *dax_get_private(struct dax_device
*dax_dev
)
349 return dax_dev
->private;
351 EXPORT_SYMBOL_GPL(dax_get_private
);
353 static void init_once(void *_dax_dev
)
355 struct dax_device
*dax_dev
= _dax_dev
;
356 struct inode
*inode
= &dax_dev
->inode
;
358 inode_init_once(inode
);
361 static int __dax_fs_init(void)
365 dax_cache
= kmem_cache_create("dax_cache", sizeof(struct dax_device
), 0,
366 (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
367 SLAB_MEM_SPREAD
|SLAB_ACCOUNT
),
372 rc
= register_filesystem(&dax_fs_type
);
374 goto err_register_fs
;
376 dax_mnt
= kern_mount(&dax_fs_type
);
377 if (IS_ERR(dax_mnt
)) {
378 rc
= PTR_ERR(dax_mnt
);
381 dax_superblock
= dax_mnt
->mnt_sb
;
386 unregister_filesystem(&dax_fs_type
);
388 kmem_cache_destroy(dax_cache
);
393 static void __dax_fs_exit(void)
395 kern_unmount(dax_mnt
);
396 unregister_filesystem(&dax_fs_type
);
397 kmem_cache_destroy(dax_cache
);
400 static int __init
dax_fs_init(void)
404 rc
= __dax_fs_init();
408 nr_dax
= max(nr_dax
, 256);
409 rc
= alloc_chrdev_region(&dax_devt
, 0, nr_dax
, "dax");
415 static void __exit
dax_fs_exit(void)
417 unregister_chrdev_region(dax_devt
, nr_dax
);
418 ida_destroy(&dax_minor_ida
);
422 MODULE_AUTHOR("Intel Corporation");
423 MODULE_LICENSE("GPL v2");
424 subsys_initcall(dax_fs_init
);
425 module_exit(dax_fs_exit
);