blob: 8bf71195921b50b975494323b2e019efbfbd83c6 [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>
Dan Williamsef510422017-05-08 10:55:27 -070017#include <linux/genhd.h>
Dan Williams7b6be842017-04-11 09:49:49 -070018#include <linux/cdev.h>
19#include <linux/hash.h>
20#include <linux/slab.h>
Dan Williams7e026c82017-05-29 12:57:56 -070021#include <linux/uio.h>
Dan Williams6568b082017-01-24 18:44:18 -080022#include <linux/dax.h>
Dan Williams7b6be842017-04-11 09:49:49 -070023#include <linux/fs.h>
24
Dan Williams7b6be842017-04-11 09:49:49 -070025static dev_t dax_devt;
26DEFINE_STATIC_SRCU(dax_srcu);
27static struct vfsmount *dax_mnt;
28static DEFINE_IDA(dax_minor_ida);
29static struct kmem_cache *dax_cache __read_mostly;
30static struct super_block *dax_superblock __read_mostly;
31
Dan Williams72058002017-04-19 15:14:31 -070032#define DAX_HASH_SIZE (PAGE_SIZE / sizeof(struct hlist_head))
33static struct hlist_head dax_host_list[DAX_HASH_SIZE];
34static DEFINE_SPINLOCK(dax_host_lock);
35
Dan Williams7b6be842017-04-11 09:49:49 -070036int dax_read_lock(void)
37{
38 return srcu_read_lock(&dax_srcu);
39}
40EXPORT_SYMBOL_GPL(dax_read_lock);
41
42void dax_read_unlock(int id)
43{
44 srcu_read_unlock(&dax_srcu, id);
45}
46EXPORT_SYMBOL_GPL(dax_read_unlock);
47
Dan Williams9d109082017-05-13 16:18:21 -070048#ifdef CONFIG_BLOCK
Dan Williamsef510422017-05-08 10:55:27 -070049int bdev_dax_pgoff(struct block_device *bdev, sector_t sector, size_t size,
50 pgoff_t *pgoff)
51{
52 phys_addr_t phys_off = (get_start_sect(bdev) + sector) * 512;
53
54 if (pgoff)
55 *pgoff = PHYS_PFN(phys_off);
56 if (phys_off % PAGE_SIZE || size % PAGE_SIZE)
57 return -EINVAL;
58 return 0;
59}
60EXPORT_SYMBOL(bdev_dax_pgoff);
61
62/**
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
66 *
67 * This is a library function for filesystems to check if the block device
68 * can be mounted with dax option.
69 *
70 * Return: negative errno if unsupported, 0 if supported.
71 */
72int __bdev_dax_supported(struct super_block *sb, int blocksize)
73{
74 struct block_device *bdev = sb->s_bdev;
75 struct dax_device *dax_dev;
76 pgoff_t pgoff;
77 int err, id;
78 void *kaddr;
79 pfn_t pfn;
80 long len;
81
82 if (blocksize != PAGE_SIZE) {
83 pr_err("VFS (%s): error: unsupported blocksize for dax\n",
84 sb->s_id);
85 return -EINVAL;
86 }
87
88 err = bdev_dax_pgoff(bdev, 0, PAGE_SIZE, &pgoff);
89 if (err) {
90 pr_err("VFS (%s): error: unaligned partition for dax\n",
91 sb->s_id);
92 return err;
93 }
94
95 dax_dev = dax_get_by_host(bdev->bd_disk->disk_name);
96 if (!dax_dev) {
97 pr_err("VFS (%s): error: device does not support dax\n",
98 sb->s_id);
99 return -EOPNOTSUPP;
100 }
101
102 id = dax_read_lock();
103 len = dax_direct_access(dax_dev, pgoff, 1, &kaddr, &pfn);
104 dax_read_unlock(id);
105
106 put_dax(dax_dev);
107
108 if (len < 1) {
109 pr_err("VFS (%s): error: dax access failed (%ld)",
110 sb->s_id, len);
111 return len < 0 ? len : -EIO;
112 }
113
114 return 0;
115}
116EXPORT_SYMBOL_GPL(__bdev_dax_supported);
Dan Williams9d109082017-05-13 16:18:21 -0700117#endif
Dan Williamsef510422017-05-08 10:55:27 -0700118
Dan Williams9a60c3e2017-06-27 17:59:28 -0700119enum dax_device_flags {
120 /* !alive + rcu grace period == no new operations / mappings */
121 DAXDEV_ALIVE,
122};
123
Dan Williams7b6be842017-04-11 09:49:49 -0700124/**
125 * struct dax_device - anchor object for dax services
126 * @inode: core vfs
127 * @cdev: optional character interface for "device dax"
Dan Williams72058002017-04-19 15:14:31 -0700128 * @host: optional name for lookups where the device path is not available
Dan Williams7b6be842017-04-11 09:49:49 -0700129 * @private: dax driver private data
Dan Williams9a60c3e2017-06-27 17:59:28 -0700130 * @flags: state and boolean properties
Dan Williams7b6be842017-04-11 09:49:49 -0700131 */
132struct dax_device {
Dan Williams72058002017-04-19 15:14:31 -0700133 struct hlist_node list;
Dan Williams7b6be842017-04-11 09:49:49 -0700134 struct inode inode;
135 struct cdev cdev;
Dan Williams72058002017-04-19 15:14:31 -0700136 const char *host;
Dan Williams7b6be842017-04-11 09:49:49 -0700137 void *private;
Dan Williams9a60c3e2017-06-27 17:59:28 -0700138 unsigned long flags;
Dan Williams6568b082017-01-24 18:44:18 -0800139 const struct dax_operations *ops;
Dan Williams7b6be842017-04-11 09:49:49 -0700140};
141
Dan Williamsb0686262017-01-26 20:37:35 -0800142/**
143 * dax_direct_access() - translate a device pgoff to an absolute pfn
144 * @dax_dev: a dax_device instance representing the logical memory range
145 * @pgoff: offset in pages from the start of the device to translate
146 * @nr_pages: number of consecutive pages caller can handle relative to @pfn
147 * @kaddr: output parameter that returns a virtual address mapping of pfn
148 * @pfn: output parameter that returns an absolute pfn translation of @pgoff
149 *
150 * Return: negative errno if an error occurs, otherwise the number of
151 * pages accessible at the device relative @pgoff.
152 */
153long dax_direct_access(struct dax_device *dax_dev, pgoff_t pgoff, long nr_pages,
154 void **kaddr, pfn_t *pfn)
155{
156 long avail;
157
158 /*
159 * The device driver is allowed to sleep, in order to make the
160 * memory directly accessible.
161 */
162 might_sleep();
163
164 if (!dax_dev)
165 return -EOPNOTSUPP;
166
167 if (!dax_alive(dax_dev))
168 return -ENXIO;
169
170 if (nr_pages < 0)
171 return nr_pages;
172
173 avail = dax_dev->ops->direct_access(dax_dev, pgoff, nr_pages,
174 kaddr, pfn);
175 if (!avail)
176 return -ERANGE;
177 return min(avail, nr_pages);
178}
179EXPORT_SYMBOL_GPL(dax_direct_access);
180
Dan Williams7e026c82017-05-29 12:57:56 -0700181size_t dax_copy_from_iter(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
182 size_t bytes, struct iov_iter *i)
183{
184 if (!dax_alive(dax_dev))
185 return 0;
186
Dan Williams7e026c82017-05-29 12:57:56 -0700187 return dax_dev->ops->copy_from_iter(dax_dev, pgoff, addr, bytes, i);
188}
189EXPORT_SYMBOL_GPL(dax_copy_from_iter);
190
Dan Williamsabebfbe2017-05-29 13:02:52 -0700191void dax_flush(struct dax_device *dax_dev, pgoff_t pgoff, void *addr,
192 size_t size)
193{
194 if (!dax_alive(dax_dev))
195 return;
196
197 if (dax_dev->ops->flush)
198 dax_dev->ops->flush(dax_dev, pgoff, addr, size);
199}
200EXPORT_SYMBOL_GPL(dax_flush);
201
Dan Williams7b6be842017-04-11 09:49:49 -0700202bool dax_alive(struct dax_device *dax_dev)
203{
204 lockdep_assert_held(&dax_srcu);
Dan Williams9a60c3e2017-06-27 17:59:28 -0700205 return test_bit(DAXDEV_ALIVE, &dax_dev->flags);
Dan Williams7b6be842017-04-11 09:49:49 -0700206}
207EXPORT_SYMBOL_GPL(dax_alive);
208
Dan Williams72058002017-04-19 15:14:31 -0700209static int dax_host_hash(const char *host)
210{
211 return hashlen_hash(hashlen_string("DAX", host)) % DAX_HASH_SIZE;
212}
213
Dan Williams7b6be842017-04-11 09:49:49 -0700214/*
215 * Note, rcu is not protecting the liveness of dax_dev, rcu is ensuring
216 * that any fault handlers or operations that might have seen
217 * dax_alive(), have completed. Any operations that start after
218 * synchronize_srcu() has run will abort upon seeing !dax_alive().
219 */
220void kill_dax(struct dax_device *dax_dev)
221{
222 if (!dax_dev)
223 return;
224
Dan Williams9a60c3e2017-06-27 17:59:28 -0700225 clear_bit(DAXDEV_ALIVE, &dax_dev->flags);
Dan Williams72058002017-04-19 15:14:31 -0700226
Dan Williams7b6be842017-04-11 09:49:49 -0700227 synchronize_srcu(&dax_srcu);
Dan Williams72058002017-04-19 15:14:31 -0700228
229 spin_lock(&dax_host_lock);
230 hlist_del_init(&dax_dev->list);
231 spin_unlock(&dax_host_lock);
232
Dan Williams7b6be842017-04-11 09:49:49 -0700233 dax_dev->private = NULL;
234}
235EXPORT_SYMBOL_GPL(kill_dax);
236
237static struct inode *dax_alloc_inode(struct super_block *sb)
238{
239 struct dax_device *dax_dev;
240
241 dax_dev = kmem_cache_alloc(dax_cache, GFP_KERNEL);
242 return &dax_dev->inode;
243}
244
245static struct dax_device *to_dax_dev(struct inode *inode)
246{
247 return container_of(inode, struct dax_device, inode);
248}
249
250static void dax_i_callback(struct rcu_head *head)
251{
252 struct inode *inode = container_of(head, struct inode, i_rcu);
253 struct dax_device *dax_dev = to_dax_dev(inode);
254
Dan Williams72058002017-04-19 15:14:31 -0700255 kfree(dax_dev->host);
256 dax_dev->host = NULL;
Dan Williams7b6be842017-04-11 09:49:49 -0700257 ida_simple_remove(&dax_minor_ida, MINOR(inode->i_rdev));
258 kmem_cache_free(dax_cache, dax_dev);
259}
260
261static void dax_destroy_inode(struct inode *inode)
262{
263 struct dax_device *dax_dev = to_dax_dev(inode);
264
Dan Williams9a60c3e2017-06-27 17:59:28 -0700265 WARN_ONCE(test_bit(DAXDEV_ALIVE, &dax_dev->flags),
Dan Williams7b6be842017-04-11 09:49:49 -0700266 "kill_dax() must be called before final iput()\n");
267 call_rcu(&inode->i_rcu, dax_i_callback);
268}
269
270static const struct super_operations dax_sops = {
271 .statfs = simple_statfs,
272 .alloc_inode = dax_alloc_inode,
273 .destroy_inode = dax_destroy_inode,
274 .drop_inode = generic_delete_inode,
275};
276
277static struct dentry *dax_mount(struct file_system_type *fs_type,
278 int flags, const char *dev_name, void *data)
279{
280 return mount_pseudo(fs_type, "dax:", &dax_sops, NULL, DAXFS_MAGIC);
281}
282
283static struct file_system_type dax_fs_type = {
284 .name = "dax",
285 .mount = dax_mount,
286 .kill_sb = kill_anon_super,
287};
288
289static int dax_test(struct inode *inode, void *data)
290{
291 dev_t devt = *(dev_t *) data;
292
293 return inode->i_rdev == devt;
294}
295
296static int dax_set(struct inode *inode, void *data)
297{
298 dev_t devt = *(dev_t *) data;
299
300 inode->i_rdev = devt;
301 return 0;
302}
303
304static struct dax_device *dax_dev_get(dev_t devt)
305{
306 struct dax_device *dax_dev;
307 struct inode *inode;
308
309 inode = iget5_locked(dax_superblock, hash_32(devt + DAXFS_MAGIC, 31),
310 dax_test, dax_set, &devt);
311
312 if (!inode)
313 return NULL;
314
315 dax_dev = to_dax_dev(inode);
316 if (inode->i_state & I_NEW) {
Dan Williams9a60c3e2017-06-27 17:59:28 -0700317 set_bit(DAXDEV_ALIVE, &dax_dev->flags);
Dan Williams7b6be842017-04-11 09:49:49 -0700318 inode->i_cdev = &dax_dev->cdev;
319 inode->i_mode = S_IFCHR;
320 inode->i_flags = S_DAX;
321 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
322 unlock_new_inode(inode);
323 }
324
325 return dax_dev;
326}
327
Dan Williams72058002017-04-19 15:14:31 -0700328static void dax_add_host(struct dax_device *dax_dev, const char *host)
329{
330 int hash;
331
332 /*
333 * Unconditionally init dax_dev since it's coming from a
334 * non-zeroed slab cache
335 */
336 INIT_HLIST_NODE(&dax_dev->list);
337 dax_dev->host = host;
338 if (!host)
339 return;
340
341 hash = dax_host_hash(host);
342 spin_lock(&dax_host_lock);
343 hlist_add_head(&dax_dev->list, &dax_host_list[hash]);
344 spin_unlock(&dax_host_lock);
345}
346
Dan Williams6568b082017-01-24 18:44:18 -0800347struct dax_device *alloc_dax(void *private, const char *__host,
348 const struct dax_operations *ops)
Dan Williams7b6be842017-04-11 09:49:49 -0700349{
350 struct dax_device *dax_dev;
Dan Williams72058002017-04-19 15:14:31 -0700351 const char *host;
Dan Williams7b6be842017-04-11 09:49:49 -0700352 dev_t devt;
353 int minor;
354
Dan Williams72058002017-04-19 15:14:31 -0700355 host = kstrdup(__host, GFP_KERNEL);
356 if (__host && !host)
357 return NULL;
358
Dan Williamscf1e2282017-05-08 12:33:53 -0700359 minor = ida_simple_get(&dax_minor_ida, 0, MINORMASK+1, GFP_KERNEL);
Dan Williams7b6be842017-04-11 09:49:49 -0700360 if (minor < 0)
Dan Williams72058002017-04-19 15:14:31 -0700361 goto err_minor;
Dan Williams7b6be842017-04-11 09:49:49 -0700362
363 devt = MKDEV(MAJOR(dax_devt), minor);
364 dax_dev = dax_dev_get(devt);
365 if (!dax_dev)
Dan Williams72058002017-04-19 15:14:31 -0700366 goto err_dev;
Dan Williams7b6be842017-04-11 09:49:49 -0700367
Dan Williams72058002017-04-19 15:14:31 -0700368 dax_add_host(dax_dev, host);
Dan Williams6568b082017-01-24 18:44:18 -0800369 dax_dev->ops = ops;
Dan Williams7b6be842017-04-11 09:49:49 -0700370 dax_dev->private = private;
371 return dax_dev;
372
Dan Williams72058002017-04-19 15:14:31 -0700373 err_dev:
Dan Williams7b6be842017-04-11 09:49:49 -0700374 ida_simple_remove(&dax_minor_ida, minor);
Dan Williams72058002017-04-19 15:14:31 -0700375 err_minor:
376 kfree(host);
Dan Williams7b6be842017-04-11 09:49:49 -0700377 return NULL;
378}
379EXPORT_SYMBOL_GPL(alloc_dax);
380
381void put_dax(struct dax_device *dax_dev)
382{
383 if (!dax_dev)
384 return;
385 iput(&dax_dev->inode);
386}
387EXPORT_SYMBOL_GPL(put_dax);
388
389/**
Dan Williams72058002017-04-19 15:14:31 -0700390 * dax_get_by_host() - temporary lookup mechanism for filesystem-dax
391 * @host: alternate name for the device registered by a dax driver
392 */
393struct dax_device *dax_get_by_host(const char *host)
394{
395 struct dax_device *dax_dev, *found = NULL;
396 int hash, id;
397
398 if (!host)
399 return NULL;
400
401 hash = dax_host_hash(host);
402
403 id = dax_read_lock();
404 spin_lock(&dax_host_lock);
405 hlist_for_each_entry(dax_dev, &dax_host_list[hash], list) {
406 if (!dax_alive(dax_dev)
407 || strcmp(host, dax_dev->host) != 0)
408 continue;
409
410 if (igrab(&dax_dev->inode))
411 found = dax_dev;
412 break;
413 }
414 spin_unlock(&dax_host_lock);
415 dax_read_unlock(id);
416
417 return found;
418}
419EXPORT_SYMBOL_GPL(dax_get_by_host);
420
421/**
Dan Williams7b6be842017-04-11 09:49:49 -0700422 * inode_dax: convert a public inode into its dax_dev
423 * @inode: An inode with i_cdev pointing to a dax_dev
424 *
425 * Note this is not equivalent to to_dax_dev() which is for private
426 * internal use where we know the inode filesystem type == dax_fs_type.
427 */
428struct dax_device *inode_dax(struct inode *inode)
429{
430 struct cdev *cdev = inode->i_cdev;
431
432 return container_of(cdev, struct dax_device, cdev);
433}
434EXPORT_SYMBOL_GPL(inode_dax);
435
436struct inode *dax_inode(struct dax_device *dax_dev)
437{
438 return &dax_dev->inode;
439}
440EXPORT_SYMBOL_GPL(dax_inode);
441
442void *dax_get_private(struct dax_device *dax_dev)
443{
444 return dax_dev->private;
445}
446EXPORT_SYMBOL_GPL(dax_get_private);
447
448static void init_once(void *_dax_dev)
449{
450 struct dax_device *dax_dev = _dax_dev;
451 struct inode *inode = &dax_dev->inode;
452
453 inode_init_once(inode);
454}
455
456static int __dax_fs_init(void)
457{
458 int rc;
459
460 dax_cache = kmem_cache_create("dax_cache", sizeof(struct dax_device), 0,
461 (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
462 SLAB_MEM_SPREAD|SLAB_ACCOUNT),
463 init_once);
464 if (!dax_cache)
465 return -ENOMEM;
466
467 rc = register_filesystem(&dax_fs_type);
468 if (rc)
469 goto err_register_fs;
470
471 dax_mnt = kern_mount(&dax_fs_type);
472 if (IS_ERR(dax_mnt)) {
473 rc = PTR_ERR(dax_mnt);
474 goto err_mount;
475 }
476 dax_superblock = dax_mnt->mnt_sb;
477
478 return 0;
479
480 err_mount:
481 unregister_filesystem(&dax_fs_type);
482 err_register_fs:
483 kmem_cache_destroy(dax_cache);
484
485 return rc;
486}
487
488static void __dax_fs_exit(void)
489{
490 kern_unmount(dax_mnt);
491 unregister_filesystem(&dax_fs_type);
492 kmem_cache_destroy(dax_cache);
493}
494
495static int __init dax_fs_init(void)
496{
497 int rc;
498
499 rc = __dax_fs_init();
500 if (rc)
501 return rc;
502
Dan Williamscf1e2282017-05-08 12:33:53 -0700503 rc = alloc_chrdev_region(&dax_devt, 0, MINORMASK+1, "dax");
Dan Williams7b6be842017-04-11 09:49:49 -0700504 if (rc)
505 __dax_fs_exit();
506 return rc;
507}
508
509static void __exit dax_fs_exit(void)
510{
Dan Williamscf1e2282017-05-08 12:33:53 -0700511 unregister_chrdev_region(dax_devt, MINORMASK+1);
Dan Williams7b6be842017-04-11 09:49:49 -0700512 ida_destroy(&dax_minor_ida);
513 __dax_fs_exit();
514}
515
516MODULE_AUTHOR("Intel Corporation");
517MODULE_LICENSE("GPL v2");
518subsys_initcall(dax_fs_init);
519module_exit(dax_fs_exit);